1
\$\begingroup\$

I'm having issues with the depth components of my shadowmapping shaders. The shadow map rendering shader is fine, and works very well. The world rendering shader is more problematic. The only value which seems to definitely be off is the pixel's position from the light's perspective, which I pass in parallel to the position.

struct Pixel {
    float4 position : SV_Position;
    float4 light_pos : TEXCOORD2;
    float3 normal : NORMAL;
    float2 texcoord : TEXCOORD; 
};

The reason that I used the semantic 'TEXCOORD2' on the light's pixel position is because I believe that the problem lies with Direct3D's interpolation of values between shaders, and I started trying random semantics and also forcing linear and noperspective interpolations.

In the world rendering shader, I observed in the pixel shader that the Z value of light_pos was always extremely close to, but less than the W value. This resulted in a depth result of 0.999 or similar for every pixel.

Here is the vertex shader code :

struct Vertex {
    float3 position : POSITION;
    float3 normal : NORMAL;
    float2 texcoord : TEXCOORD; 
};

struct Pixel {
    float4 position : SV_Position;
    float4 light_pos : TEXCOORD2;
    float3 normal : NORMAL;
    float2 texcoord : TEXCOORD;
};

cbuffer Camera : register (b0) {
    matrix world;
    matrix view;
    matrix projection;
};

cbuffer Light : register (b1) {
    matrix light_world;
    matrix light_view;
    matrix light_projection;
};

Pixel RenderVertexShader(Vertex input) {

    Pixel output;

    output.position = mul(float4(input.position, 1.0f), world);
    output.position = mul(output.position, view);
    output.position = mul(output.position, projection);

    output.world_pos = mul(float4(input.position, 1.0f), world);
    output.world_pos = mul(output.world_pos, light_view);
    output.world_pos = mul(output.world_pos, light_projection);

    output.texcoord = input.texcoord;
    output.normal = input.normal;

    return output;
}

I suspect interpolation to be the culprit, as I used the camera matrices in place of the light matrices in the vertex shader, and had the same problem. The problem is evident as both of the same vectors were passed to a pixel from the VS, but only one of them showed a change in the PS.

I have already thoroughly debugged the matrices' validity, the cbuffers' validity, and the multiplicative validity. I'm very stumped and have been trying to solve this for quite some time.

Misc info : The light projection matrix and the camera projection matrix are the same, generated from D3DXMatrixPerspectiveFovLH(), with an FOV of 60.0f * 3.141f / 180.0f, a near clipping plane of 0.1f, and a far clipping plane of 1000.0f.

Any ideas on what is happening?

\$\endgroup\$

migrated from stackoverflow.com Jul 21 '14 at 1:45

This question came from our site for professional and enthusiast programmers.

  • \$\begingroup\$ Which Feature Level and shader profile are you using? The title implies vs_5_0, ps_5_0 and Feature Level 11.0 or later, but I wanted to verify. \$\endgroup\$ – Chuck Walbourn Jul 8 '14 at 19:00
0
\$\begingroup\$

Turns out, the depth values were correct the whole time. I was looking at a bad image for reference, where objects would get darker as they got further away from you. Of course, this is completely incorrect.

After seeing how crazy normal depth precision is, I decided to use my own linear depth, which worked much better for shadow mapping.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.