Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rdb opened this issue Nov 1, 2020 · 3 comments
Open

Comments

@rdb
Copy link
Contributor

rdb commented Nov 1, 2020

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.

@StarsX
Copy link

StarsX commented Feb 8, 2021

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.

@rdb
Copy link
Contributor Author

rdb commented Mar 29, 2021

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.

@alan-baker
Copy link
Contributor

KhronosGroup/SPIRV-Tools#5695 adds validation for OpSampledImage image types and it flags issues like this. DXC generates the types with a depth operand of 2 (unknown), though it seems to have some optimization based on use (Tint does that too) to avoid always using Depth=2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants