HLSL 5 interpolation issues
- by metredigm
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?
(This is a repost from my question on Stack Overflow)