Pitfalls when Developing HLSL Shader

 

When you only have a pixel shader sometimes you get weird results or it is working in one context (e.g. an actual device) but is not working correctly in another context (e.g. emulator or another device).

If you try something different you will see that if a vertex shader and pixel shader are compiled together as a technique it works.

What could cause this behavior?

If one take a look at the output of the fxc.exe tool one can see that the predefined semantics (e.g. COLOR0 or TEXCOORD0) of former DirectX versions are gone. Instead the parameters are mapped to register in order of the parameter appearance in the signature of the function.

So it’s a good idea to use the complete signature, e.g.

float4 PixelShaderFunction(float4 pos : SV_POSITION, float4 color1 : COLOR0, float2 texCoord : TEXCOORD0) : SV_TARGET0

Output of the fxc tool for this signature:

// Input signature:
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION                 0   xyzw        0     NONE   float
// COLOR                    0   xyzw        1     NONE   float
// TEXCOORD                 0   xy          2     NONE   float   xy

So for example if the second parameter in the signature is omitted the output is:

// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION                 0   xyzw        0     NONE   float
// TEXCOORD                 0   xy          1     NONE   float   xy

So TEXCOORD is wrongly mapped to register 1.

That means one can omit parameter at the end but not at the beginning or in the middle.

Anyway to use the complete signature seems to be a good way to have the desired result in any context.

One thought on “Pitfalls when Developing HLSL Shader

  1. Trevor Andersen

    This is the answer to a problem I was having where my shader was causing my textures to be drawn upside down and at an incorrect resolution. Thanks for the clear explanation.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *