# SampleCmpLevelZero samples return 0 only

## Recommended Posts

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

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

http://www.gamedev.net/topic/625963-samplecmplevelzero-just-returns-black/

http://www.gamedev.net/topic/633909-how-to-use-texture-samplers/

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

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

[attachment=26724:SamplerFixed.png]

[attachment=26725:SamplerWarning.png]

##### Share on other sites

Thanks for posting the resolution to the problem.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628362
• Total Posts
2982266

• 10
• 9
• 13
• 24
• 11