Now that I finally got DX10 to let me write to my shadow map after using it in a render pass, I need to eliminate the the surface acne. I have Shader X5, and it talks about Gradient Shadow Maps, but mostly refers to the chapter in Shader X4, which I do not have. Here's my pixel shader wihout any gradient:
Which turns out as expected, but not good enough.
And then when I try and implement the gradient:
Which turns out all with shadow in random places. What am I doing wrong?
Code:
float4 PS(PS_In input) : SV_TARGET
{
float amb = 0.4f;
float diff = 0;
float spec = 0;
float2 sampleCoord = 0.5f * input.LightPos.xy / input.LightPos.w + float2(0.5, 0.5);
sampleCoord.y = 1.0f - sampleCoord.y;
if(input.LightPos.z < ShadowMap.Sample(Linear, sampleCoord).x + 0.00005f)
{
float3 N = normalize(input.Norm);
float3 V = normalize(EyePos - input.wPos);
float3 R = normalize(reflect(-L, N));
diff = saturate(dot(N, L));
spec = pow(saturate(dot(V, R)), 32);
}
float4 output;
output.rgb = (float3)0.5f * (amb + diff + spec);
output.a = 1;
return output;
}
And then when I try and implement the gradient:
Code:
float4 PS(PS_In input) : SV_TARGET
{
float amb = 0.4f;
float diff = 0;
float spec = 0;
float2 gradientVector = float2(input.LightPos.z / input.LightPos.x, input.LightPos.z / input.LightPos.y);
float gradient = length(gradientVector);
float2 sampleCoord = 0.5f * input.LightPos.xy / input.LightPos.w + float2(0.5, 0.5);
sampleCoord.y = 1.0f - sampleCoord.y;
float difference = ShadowMap.Sample(Linear, sampleCoord).x - input.LightPos.x;
if(saturate(difference / gradient + 1))
{
float3 N = normalize(input.Norm);
float3 V = normalize(EyePos - input.wPos);
float3 R = normalize(reflect(-L, N));
diff = saturate(dot(N, L));
spec = pow(saturate(dot(V, R)), 32);
}
float4 output;
output.rgb = (float3)0.5f * (amb + diff + spec);
output.a = 1;
return output;
}