Jump to content
  • Advertisement
Sign in to follow this  

SampleCmpLevelZero samples return 0 only

This topic is 1350 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts



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
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!

Share this post

Link to post
Share on other sites

Amazing what some food can do. So after hours spent on this I finally have my answer.


Don't create your samplers in the .fx file. It's dependent on the Effects library and so it won't actually set the parameters that you ask for, just the default values.


So yeah, moving the sampler description into code and setting it there fit everything right up.


For some similar issues you can look here at least.




Share this post

Link to post
Share on other sites

And just to show that RenderDoc was trying to warn me of my downfall, here's some shots with the unset and set sampler. They've even gone and highlighted it in red for me tongue.png


I had noticed this earlier in my debugging but I dismissed it too easily. I guess I didn't trust my tools. Lesson learned.




Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!