Updating PixelShaders from ps_1_0 to ps_4_0 and a Warning

As I’m going through and updating the code I ran into a following snag that took me a bit to figure out. In this case, I only had a Pixel Shader and no Vertex Shader. The shader worked just fine back in the day, but during the upgrade I was told that MonoGame only supported ps_4_0 and this shader was version 1.0 (ps_1_0).  Simply changing the version number in the shader file fixed that.

But then I had a bug where the shader wasn’t giving me the results I wanted.

After poking around a little bit, I finally found out that the method signature needs to explicitly have all of the parameters.  I found this information here:

http://www.software7.com/blog/pitfalls-when-developing-hlsl-shader/

 

The way my pixel shader looked original was:

 

sampler TextureSampler;

struct PixelInput
{
    float2 TexCoord : TEXCOORD0;   
};

float4 pixelShader(PixelInput input) : COLOR
{
    float4 color = tex2D( TextureSampler, input.TexCoord);
    return( color );
}

technique Default
{
    pass P0
    {
        PixelShader = compile ps_1_1 pixelShader();
    }
}

 

And I had to change it to:

sampler TextureSampler;

struct PixelInput
{
    float4 Pos : SV_POSITION;
    float4 Color : COLOR0;
    float2 TexCoord : TEXCOORD0;
};

float4 pixelShader(PixelInput input) : SV_TARGET0
{
    float4 color = tex2D( TextureSampler, input.TexCoord);
    return( color );
}

technique Default
{
    pass P0
    {
        PixelShader = compile ps_4_0 pixelShader();
    }
}

 

Once I made that small tweak (added in the different parameters (albeit through a struct)) it worked like a charm. A struct doesn’t have to be used, but the parameters should be in the right order it seems – position, then color, then texture coordinates.

 

Note, that this pixel shader doesn’t do anything but return the pixel of the texture passed to it.  Typically you would do some math on the color before returning it. Check out Chapter 15, under SimpleGame for some different pixel shader examples when I update the code to GitHub. (Before the end of the month…)

 

So make sure that your pixel shader parameters are all present and associated with the right semantics!

Tell me what you think!