Hello people,
I'm having an issue implementing shadow mapping in DirectX 10. Certain areas of most of the meshes seem to always fail the depth test. I've tried increasing the bias, but to have an effect on these faces, the bias would have to be to the point where all the other shadows begin floating from their point of contact.
I've also tried messing around with the slope scaled bias parameters of the rasterizer state which did not help as the cases do not seem to correspond to the surface normal and light direction. As these shadows don't seem to be affected by changing of the light source position relative to their surface, I ensured the issue is not the normals themselves. Under non-shadowed lighting, the faces are lit as expected.
It seems to be a floating point precision issue, although through changing the light's far plane from 1000 to 60, and then 45, the problem persists. Whether I use orthographic or perspective does not seem to have an effect except that when using an ortho matrix, the bias has to be set a lot higher, but ultimately leading to the same issue.
Depth is rendered as usual, using a null pixel shader with no render target bound. The depth buffer is in format DXGI_FORMAT_D32_FLOAT and shows nothing strange.
Below is the relevant shader code:
inline float2 uvProjectionOfPosition(in float4 position)
{
float2 output =
{
position.x/position.w/ 2.0f + 0.5f,
-position.y/position.w/ 2.0f + 0.5f
};
return output;
}
//=============================================================================================
//needed until border sampler state is debugged
bool uvInRange(uniform float2 uv)
{
return saturate(uv.x)==uv.x&&saturate(uv.y)==uv.y;
}
//=============================================================================================
inline bool pixelIsShadowed(uniform float4 positionFromLigthView, uniform float2 uvToDepthMap, uniform float4 sampledDepth, uniform float bias)
{
return !uvInRange(uvToDepthMap) ||(sampledDepth.x+ bias) < (positionFromLigthView.z /positionFromLigthView.w);
}
//Copied from within the main lighting function
//=============================================================================================
float4 positionInLightProjection = mul(float4(thisWorldPosition, 1.0f), lightProjection(ilight));
float2 projectedLightUV = uvProjectionOfPosition(positionInLightProjection);
float4 depthMapSample = lightDepthMapSample(projectedLightUV);
float bias = 0.0000009f;//<- for projection matrix & about .015f for ortho
if(pixelIsShadowed(positionInLightProjection, projectedLightUV, depthMapSample, bias))
{
thisShadowFactor = 0.0f;
}