Sign in to follow this  
SmellyIrishMan

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

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.

 

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

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

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.

 

[attachment=26724:SamplerFixed.png]

[attachment=26725:SamplerWarning.png]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this