[SOLVED] Effect parameter not being set in OpenGL, however works in DirectX project

Hi,
I am experiencing an issue in the development of a custom effect, where a certain parameter (float3) will not set properly in an OpenGL project, however will work fine in a DirectX project. Here is my shader code:

    #if OPENGL
    	#define SV_POSITION POSITION
    	#define VS_SHADERMODEL vs_3_0
    	#define PS_SHADERMODEL ps_3_0
    #else
    	#define VS_SHADERMODEL vs_4_0_level_9_1
    	#define PS_SHADERMODEL ps_4_0_level_9_1
    #endif

    matrix WorldViewProjection;

    float3 swizzle;

    sampler2D TextureSampler : register(s0)
    {
    	Texture = (Texture);
    };

    float4 MainPS(float4 position : SV_Position, float4 color : COLOR0, float2 
    TextureCoordinates : TEXCOORD0) : COLOR0
    {
    	float4 col = tex2D(TextureSampler, TextureCoordinates) * color;
    	return float4(col[swizzle[0]], col[swizzle[1]], col[swizzle[2]], col[3]);

    }

    technique Swizzle
    {
    	pass P0
    	{
    		PixelShader = compile PS_SHADERMODEL MainPS();
    	}
    };

The parameter in question is swizzle which appears to be set to a default value of 1, 1, 1 instead of what is set in my C# code. Interestingly, however, attempting to retrieve the value though C# reveals the value which I set it too, however hardcoding this value causes a different visual result.

Thank you in advance!

If you move the swizzle parameter above the
WorldViewProjection parameter, does that change anything?

1 Like

Yep, I’d recommend trying that, or commenting out the WorldViewProjection parameter entirely if it’s unused.

This reminds me of this issue that I and several other users have run into.

I don’t remember all the details of the issue, but basically the shader compiler will aggressively remove unused parameters (or even unused individual columns of matrix parameters). However MonoGame isn’t made aware of this, so it will still pass parameters by index in the original order. So if your WorldViewProjection is unused, then when you set WorldViewProjection from code, it’s probably actually using the first column (or maybe first row?) to set swizzle.

1 Like

Sorry if I’ve wasted your time but I worked out that the problem was that I was using floats where ints probably should’ve been used. Anyway, thank you to everyone who participated in this topic.