Hello!
It's been a long day of trying to get shadow mapping implemented. It started off pretty well and I'm in a good spot, only this little texture sample is causing me some issues. I'm trying to use SampleCmpLevelZero to get some free PCF but the function returns 0, no matter what I put into it.
My shadow map looks good, and if I do a simple check then everything works out pretty well. For example this piece of code comes out with some good output and shows that there is some depth information that makes sense in both the local pixel depth and the shadowMap.
float depthFromLightToThisPixel = pIn.ShadowPosH.z;
float depthFromLightToClosestPixel = gShadowMap.Sample(sam, pIn.ShadowPosH.xy).r;
float depthDiff = abs(depthFromLightToThisPixel - depthFromLightToClosestPixel);
return float4(0.9f, 0.9f, 0.9f, 1.0f) * depthDiff;
This code however, just returns 0. Fully black for all pixels. Even if I force the depthFromLightToThisPixel to be 0, 1, or any other value.
float depthFromLightToThisPixel = pIn.ShadowPosH.z;
float shadow = gShadowMap.SampleCmpLevelZero(ShadowSampler, pIn.ShadowPosH.xy, depthFromLightToThisPixel);
return float4(0.9f, 0.9f, 0.9f, 1.0f) * shadow;
I've come across a few possible issues and I think I've narrowed it down to either;
1.Sampler is incorrect.
2.Texture format is incorrect.
Here's the sampler. (BorderColor just for testing)
SamplerComparisonState ShadowSampler
{
Filter = COMPARISON_MIN_MAG_LINEAR_MIP_POINT;
AddressU = BORDER;
AddressV = BORDER;
AddressW = BORDER;
BorderColor = float4(0.5f, 0.5f, 0.5f, 1.0f);
ComparisonFunc = LESS_EQUAL;
};
And my textures are set up as follows
Texture2D; R24G8_Typeless
DepthStencilView; D24_UNorm_S8_UInt
ShaderResourceView; R24_UNorm_X8_Typeless
Now R24_UNorm_X8_Typeless should be OK to use the comparison function according to the bottom of the page here (https://msdn.microsoft.com/en-us/library/windows/desktop/ff476132%28v=vs.85%29.aspx). When I run the shader through RenderDoc I get a texture format of R24G8_Typeless but I assume that's OK and the ShaderResourceView is still doing the read in it's own format. I've attached a file showing the texture format in RenderDoc.
Ah. I had a thought before I left that it might be related to mips since that's what level the compare happens on. I should be generating just a single level of mip for the texture. However if I use SampleLevel in the first block of code for example;
depthFromLightToClosestPixel = gShadowMap.SampleLevel(sam, 0, pIn.ShadowPosH.xy).r;
Then I just get 1.0 returned. Solid white instead of solid black. Perhaps this is the root cause.
I'm pretty stumped at the moment as to what's causing this. Hopefully I haven't overlooked anything and left out any information. Thanks for reading! Time for me to eat!