Problem with cascade shadow maps

I am just adding cascade shadow maps to my engine and run into a problem.

I started with a simple test application that just rendered a single mesh, and it worked fine.

So I copied the code into my engine , and now it doesn’t work at all.

I have checked everything I can think of. I know the shadow map is rendering correctly.

I know all the values are set correctly, but everything is black. Not shadowed as it should be, black.

I have tracked the problem down to a single line of code.

return ShadowMap.SampleCmpLevelZero(ShadowSampler, float3(shadowPos.xy, cascadeIdx), lightDepth);

Which is always returning 0

Has anyone come across any issues with this?

It is in a shader include file which works fine in the test app, but the test app is a much simpler shader. Just shadowed, textured mesh with diffuse lighting.

I am trying to add it to my ocean shader which is much more complex.

I am worried it is a shader compiler issue.

Ideas please guys, pretty stuck at the moment.

You are not giving us much to work with…

How did you setup your sampler ?
This how I do it to work with PCF:

        // shadow texture sampler
        shadowSamplerState.Filter = TextureFilter.Linear;
        shadowSamplerState.AddressU = TextureAddressMode.Border;
        shadowSamplerState.AddressV = TextureAddressMode.Border;
        shadowSamplerState.ComparisonFunction = CompareFunction.LessEqual;
        shadowSamplerState.FilterMode = TextureFilterMode.Comparison;
        shadowSamplerState.BorderColor = Color.White;

I think (not 100% sure as it has been a while) that the important ones are Filter, ComparisonFunction and FilterMode.

PS: You could try to not use SampleCmpLevelZero and sample the shadow texture + do the depth test yourself. This would confirm or not that the issue is with SampleCmpLevelZero.

PPS: SampleCmpLevelZero with TextureArray2D requires ps_4.1 or higher

Hi filnet,

Sorry about so little information, but posting huge amounts of code is something I don’t like doing.

Clogs up the website.

I tried setting the sampler state as you suggested, didn’t make any difference.

So I went back to renderdoc.

Now I am totally confused.

I should have three textures.

  • Environment map : TextureCube
  • Normal map :Texture2D
  • Shadow map : Texture2D array

Renderdoc shows two, the environment map and the shadow map

I should have three samplers, renderdoc shows two. The normal map sampler and the environment map sampler

So either renderdoc is talking rubbish … or something is very screwed

Looking at it in Nsight makes more sense

The vertex shader gets the normal map.

The pixel shader gets the environment and shadow maps

All good.

But when I look at the sampler states, I see the sampler for the shadow map is totally wrong. It’s the sampler for the normal map.

So what I think we have is a bug in the shader compiler. It looks like the compiler creates an array of three samplers then indexes into it with the wrong count.

Vertex shader has one sampler (index 0)
Pixel shader has two samplers (index 1 and 2) but is using index 0 and 1

Well it’s a possibility at least , have to think of a way of proving it.

Okay not that,

Added the a tex2D on the normal map to the pixel shader and it’s still wrong.

NSight shows the shadow map sampler as being totally rubbish. None of the settings from the shader are applied.

After some more hunting on the web, I found a couple of cases of the same problem.

Seems setting the sampler up in the effect file may not work.

They solved the problem by creating the sampler in code instead.

Ripping it out for now. Taking too much time to sort out.

Cheers guys