cpt-max / MonoGame

One framework for creating powerful cross-platform games.
http://www.monogame.net
Other
26 stars 5 forks source link

Texture coords upside down on Android #3

Open k4G17eYWI opened 6 months ago

k4G17eYWI commented 6 months ago

Prerequisites

MonoGame Version

3.8.3

Which MonoGame platform are you using?

MonoGame Android Application (mgandroid)

Operating System

WIndows, Android

Description

I'm drawing on a texture. After switching to 5_0 shaders from 3_0 my textures draws upside down but only on Android. On a desktop it draws as "normal" in without flipping vertically. I have same FX file for Android and Desktop without any platform definitions.

What could cauise such diffirecies in behaviuor and how to deal them? I could detect platform and flip Y optionaly but Y want to wind out the reason.

Steps to Reproduce

  1. Set texture as a RenderTarget

  2. Set transform

        var m = Matrix.CreateScale(
            1f / texture.width * 2,
            1f / texture.height * 2,
            1
        );
        var t = Matrix.CreateTranslation(-1, -1, 0);
        var s = Matrix.CreateScale(1, -1, 1);
        SetTransform(m * t * s);
  3. Load shader

#define VS_SHADERMODEL vs_5_0
#define PS_SHADERMODEL ps_5_0

matrix Transform;

Texture2D MainTex; // primary texture.
SamplerState MySampler
{
    magfilter = Point;
    minfilter = Point;
    mipfilter = Point;
    AddressU = Clamp;
    AddressV = Clamp;
};

struct VsInputQuad
{
    float4 Position : POSITION0;
    float4 Color : COLOR0;
    float2 TexureCoordinateA : TEXCOORD0;
};

struct VsOutputQuad
{
    float4 Position : SV_POSITION0;
    float4 Color : COLOR0;
    float2 TexureCoordinateA : TEXCOORD0;
};

struct PsOutputQuad
{
    float4 Color : SV_TARGET;
};

// ____________________________
VsOutputQuad VertexShaderQuadDraw(VsInputQuad input)
{
    VsOutputQuad output;
    output.Position = mul(input.Position, Transform); // Transform by WorldViewProjection
    output.Color = input.Color;
    output.TexureCoordinateA = input.TexureCoordinateA;
    return output;
}

PsOutputQuad PixelShaderQuadDraw(VsOutputQuad input)
{
    PsOutputQuad output;
    output.Color = MainTex.Sample(MySampler, input.TexureCoordinateA) * input.Color;
    return output;
}

technique Draw
{
    pass
    {
        VertexShader = compile VS_SHADERMODEL VertexShaderQuadDraw();
        PixelShader = compile PS_SHADERMODEL PixelShaderQuadDraw();    
    }
}
  1. Draw some geometry on it on a desktop and Android platform and compare results. Resulting textures will be interflipped vertically.

AppAndroid.csproj:

        <PackageReference Include="MonoGame.Framework.Compute.Android" Version="3.8.3" />
        <PackageReference Include="MonoGame.Content.Builder.Task.Compute" Version="3.8.3" />

AppDesktop.csproj:

        <PackageReference Include="MonoGame.Framework.Compute.DesktopGL" Version="3.8.3"/>
        <PackageReference Include="MonoGame.Content.Builder.Task.Compute" Version="3.8.3"/>

Minimal Example Repo

No response

Expected Behavior

Texture Y axis should be same on Android and Desktop or we need a clarification how to deal with it.

Resulting Behavior

Texture Y axis should be same on Android and Desktop or we need a clarification how to deal with it.

Files

No response

cpt-max commented 5 months ago

There's this code in GraphicsDevice.ActivateShaderProgram that flips the render target vertically for OpenGL:

//If we have a render target bound (rendering offscreen)
if (IsRenderTargetBound)
{
      //flip vertically
      _posFixup[1] *= -1.0f;
      _posFixup[3] *= -1.0f;
}

Either this is not working for Android, or, for whatever reason, it's not needed in Android.

cpt-max commented 5 months ago

It looks like the posFixup parameter is not added to the shader code when compiling a shader for Android, so the flip code above doesn't even run.

k4G17eYWI commented 5 months ago

Is it fixable?

cpt-max commented 5 months ago

That posFixup parameter gets inserted (or should get inserted) in MGFXC, so it should be fixable.

cpt-max commented 5 months ago

I pushed a fix for this to MonoGame.Effect.Compiler, but there are no updated Nugets yet.