glslang icon indicating copy to clipboard operation
glslang copied to clipboard

HLSL: Texture uniform in entry point parameter not being marked as depth

Open rdb opened this issue 4 years ago • 3 comments

Take this test.frag.hlsl file:

uniform Texture2D shadowmap;
SamplerComparisonState state;

float4 main(in float3 coords) : COLOR {
  return shadowmap.SampleCmp(state, coords.xy, coords.z);
}

This gets marked as depth image, as expected:

❯ glslangValidator -e main -H test.frag.hlsl | grep TypeImage
              14:             TypeImage 6(float) 2D depth sampled format:Unknown

But when the Texture2D is instead in the entry point arguments, it doesn't get marked as depth image:

SamplerComparisonState state;

float4 main(in float3 coords, uniform Texture2D shadowmap) : COLOR {
  return shadowmap.SampleCmp(state, coords.xy, coords.z);
}
❯ glslangValidator -e main -H test.frag.hlsl | grep TypeImage
               9:             TypeImage 6(float) 2D sampled format:Unknown

I first thought that fixing this should be as simple as adding a trackLinkage to the right location, but the problem is that the actual uniform TVariable created in transformEntryPoint isn't the one that is seen by handleSamplerTextureCombine—that one only sees the locally-scoped variable created in handleFunctionDefinition. So I'm not really sure what the fix should be.

I ended up working around this by detecting this in our engine and rewriting the variable with a different type, but I am posting this as issue in case others run into it.

rdb avatar Nov 01 '20 16:11 rdb

Has this issue been fixed? It seems good with my Vulkan + HLSL using SamplerComparisonState with the latest Vullkan SDK. However, I suffer another problem related to this topic. It seems OpenGL only supports combined image sampler (such as sampler2DShadow) in shaders, so OpenGL + HLSL needs DX9 style, but standard Shader Model 3 has no comparison sampler for percentage closer filter. I wonder if we need something like [[spv::Sampler_shadow]] to identify a Sampler2D as a shadow sampler. Or any other way? Thanks.

StarsX avatar Feb 08 '21 06:02 StarsX

I'd say it's only half fixed as of the latest commit. It still declares the uniform as a non-depth image: (only relevant lines pasted)

               9:             TypeImage 6(float) 2D sampled format:Unknown
              45:             TypePointer UniformConstant 9
   46(shadowmap):     45(ptr) Variable UniformConstant

…but then it uses that constructs a sampled image that is depth-typed:

              20:             TypeImage 6(float) 2D depth sampled format:Unknown
              21:             TypeSampledImage 20
              47:           9 Load 46(shadowmap)
              58:          21 SampledImage 47 57

I could not find anything in the spec suggesting that the depth flag should match between the sampled image type and the original image type given to OpSampledImage, but I reckon that the intent was that it should use the same OpTypeImage, like it does when the Texture2D is declared at global scope.

rdb avatar Mar 29 '21 12:03 rdb