0

Newbie HLSL programmer here. I'm trying to create a very basic shader for directional light and i'm experiencing really bizarre behaviour trying to debug the shader code. I dont know if its my lack of understanding of how this all works or if something is really wrong.

This is the vertex shader

struct ShaderOutput
{
    float4 Position : SV_POSITION;
    float4 Normal: NORMAL;
    float2 TexCoord: TEXCOORD;
    float4 Colour: COLOR;

};

struct ShaderInput
{
    float4 Position : POSITION;
    float4 Normal: NORMAL;
    float2 TexCoord: TEXCOORD;
};

ShaderOutput VShader(ShaderInput Input)
{
    float NdotL;
    ShaderOutput Output;

    NdotL = max(0.0f, dot(Input.Normal, LightPosition));
    Output.Colour = NdotL * LightColour;

    Output.Position = mul(Input.Position, WorldMatrix);
    Output.Position = mul(Output.Position, ViewMatrix);
    Output.Position = mul(Output.Position, ProjectionMatrix);
    Output.Normal = Input.Normal;
    Output.TexCoord = Input.TexCoord;   

    return Output;
 }

First issue, when I try to debug the shader code, the debugger starts on this line:

Output.Position = mul(Input.Position, WorldMatrix);

instead of this line:

NdotL = max(0.0f, dot(Input.Normal, LightPosition));

Which confused me, it only executes that first line after the vertex shader returns. Could someone explain why? Could this be the effect of parallel processing?

Second issue, following on from that, once this line is executed:

Output.Colour = NdotL * LightColour;

Output.Colour is blank (all values are NAN)

Third issue, these lines:

Output.Normal = Input.Normal;
Output.TexCoord = Input.TexCoord;   

dont seem to be executed, it skips right over them and jumps to the function return, I dont know why. setting the position values seems to work fine though.

Any ideas?

Walter
  • 664
  • 1
  • 6
  • 19
  • How are you compiling and debugging the shaders? The compiler might re-order operations for better performance (or leave them away if they are unnecessary, e.g. for `Output.Normal = Input.Normal` it is likely that the correct data is already in the correct register). If you don't get valid data in the debugger, you might have forgotten the DEBUG flag somewhere or your debugger just doesn't work. – Nico Schertler Dec 27 '14 at 09:17
  • The shaders are compiled during runtime with this call: `D3DCompileFromFile(L"VertexShader.hlsl", 0, 0, "main", "vs_4_0", D3DCOMPILE_DEBUG, 0, &VertexShaderBin, &Error);` The shader runs fine though, its only when debugging that the bizarre stuff happens. In the case of `Output.Normal = Input.Normal` Output.Normal is filled with NaNs where as Input.Normal has a valid normal. But it still skips over it. Since this shader works when the program runs, I'm assuming its normal behavior of the debugger (which I'm pretty unfamiliar with) – Walter Dec 27 '14 at 17:18

0 Answers0