KhronosGroup / KhronosGroup/glslang

HLSL: Duplicate textures may be created

Open
#1,708 1 comment 0 reactions 0 assignees View on GitHub
HLSL question
Dominant language
C++
Stars
3.6k
Forks
989
Avg merge
1d 2h
Merged PRs (30d)
31

Description

Consider this shader:
```
Texture2D g_Texture : register(t0);
SamplerState g_Sampler : register(s0);
SamplerComparisonState g_CompareSampler : register(s1);

float main(float2 TexCoord : TEXCOORD0) : SV_Target
{
return g_Texture.Sample(g_Sampler, TexCoord) + g_Texture.SampleCmpLevelZero(g_CompareSampler, TexCoord, 0.5);
}
```
The output is
```
WARNING: AST will form illegal SPIR-V; need to transform to legalize
// Module Version 10000
// Generated by (magic number): 80007
// Id's are bound by 56

Capability Shader
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint Fragment 4 "main" 49 52
ExecutionMode 4 OriginUpperLeft
Source HLSL 500
Name 4 "main"
Name 11 "@main(vf2;"
Name 10 "TexCoord"
Name 15 "g_Texture"
Name 19 "g_Sampler"
Name 29 "g_Texture"
Name 31 "g_CompareSampler"
Name 47 "TexCoord"
Name 49 "TexCoord"
Name 52 "@entryPointOutput"
Name 53 "param"
Decorate 15(g_Texture) DescriptorSet 0
Decorate 15(g_Texture) Binding 0
Decorate 19(g_Sampler) DescriptorSet 0
Decorate 19(g_Sampler) Binding 0
Decorate 29(g_Texture) DescriptorSet 0
Decorate 29(g_Texture) Binding 0
Decorate 31(g_CompareSampler) DescriptorSet 0
Decorate 31(g_CompareSampler) Binding 1
Decorate 49(TexCoord) Location 0
Decorate 52(@entryPointOutput) Location 0
2: TypeVoid
3: TypeFunction 2
6: TypeFloat 32
7: TypeVector 6(float) 2
8: TypePointer Function 7(fvec2)
9: TypeFunction 6(float) 8(ptr)
13: TypeImage 6(float) 2D sampled format:Unknown
14: TypePointer UniformConstant 13
15(g_Texture): 14(ptr) Variable UniformConstant
17: TypeSampler
18: TypePointer UniformConstant 17
19(g_Sampler): 18(ptr) Variable UniformConstant
21: TypeSampledImage 13
24: TypeVector 6(float) 4
27: TypeImage 6(float) 2D depth sampled format:Unknown
28: TypePointer UniformConstant 27
29(g_Texture): 28(ptr) Variable UniformConstant
31(g_CompareSampler): 18(ptr) Variable UniformConstant
33: TypeSampledImage 27
36: 6(float) Constant 1056964608
37: TypeVector 6(float) 3
41: 6(float) Constant 0
48: TypePointer Input 7(fvec2)
49(TexCoord): 48(ptr) Variable Input
51: TypePointer Output 6(float)
52(@entryPointOutput): 51(ptr) Variable Output
4(main): 2 Function None 3
5: Label
47(TexCoord): 8(ptr) Variable Function
53(param): 8(ptr) Variable Function
50: 7(fvec2) Load 49(TexCoord)
Store 47(TexCoord) 50
54: 7(fvec2) Load 47(TexCoord)
Store 53(param) 54
55: 6(float) FunctionCall 11(@main(vf2;) 53(param)
Store 52(@entryPointOutput) 55
Return
FunctionEnd
11(@main(vf2;): 6(float) Function None 9
10(TexCoord): 8(ptr) FunctionParameter
12: Label
16: 13 Load 15(g_Texture)
20: 17 Load 19(g_Sampler)
22: 21 SampledImage 16 20
23: 7(fvec2) Load 10(TexCoord)
25: 24(fvec4) ImageSampleImplicitLod 22 23
26: 6(float) CompositeExtract 25 0
30: 27 Load 29(g_Texture)
32: 17 Load 31(g_CompareSampler)
34: 33 SampledImage 30 32
35: 7(fvec2) Load 10(TexCoord)
38: 6(float) CompositeExtract 35 0
39: 6(float) CompositeExtract 35 1
40: 37(fvec3) CompositeConstruct 38 39 36
42: 6(float) CompositeExtract 40 2
43: 6(float) ImageSampleDrefExplicitLod 34 40 42 Lod 41
44: 6(float) FAdd 26 43
ReturnValue 44
FunctionEnd
```
The interesting bit is this:
```
Decorate 15(g_Texture) DescriptorSet 0
Decorate 15(g_Texture) Binding 0
Decorate 19(g_Sampler) DescriptorSet 0
Decorate 19(g_Sampler) Binding 0
Decorate 29(g_Texture) DescriptorSet 0
Decorate 29(g_Texture) Binding 0
Decorate 31(g_CompareSampler) DescriptorSet 0
Decorate 31(g_CompareSampler) Binding 1
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the HLSL shader in the report and inspect the generated SPIR-V decorations for the two g_Texture variables. Determine why duplicate texture resources are emitted and verify that the generated resource declarations and bindings no longer duplicate the texture.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.