MonoGame icon indicating copy to clipboard operation
MonoGame copied to clipboard

Texture coords upside down on Android

Open k4G17eYWI opened this issue 1 year ago • 5 comments

Prerequisites

  • [X] I have verified this issue is present in the develop branch
  • [X] I have searched open and closed issues to ensure it has not already been reported.

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);
  1. 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

k4G17eYWI avatar Apr 30 '24 10:04 k4G17eYWI

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 avatar May 09 '24 18:05 cpt-max

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.

cpt-max avatar May 09 '24 18:05 cpt-max

Is it fixable?

k4G17eYWI avatar May 09 '24 19:05 k4G17eYWI

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

cpt-max avatar May 09 '24 19:05 cpt-max

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

cpt-max avatar May 20 '24 19:05 cpt-max