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

Avoid duplicate BuiltIn variables for ray tracing matrices (fix #2921) #2955

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions SPIRV/GlslangToSpv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ class TGlslangToSpvTraverser : public glslang::TIntermTraverser {
std::unordered_map<std::string, spv::Id> extBuiltinMap;

std::unordered_map<long long, spv::Id> symbolValues;
std::unordered_map<uint32_t, spv::Id> builtInVariableIds;
std::unordered_set<long long> rValueParameters; // set of formal function parameters passed as rValues,
// rather than a pointer
std::unordered_map<std::string, spv::Function*> functionMap;
Expand Down Expand Up @@ -8750,7 +8751,32 @@ spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol
// it was not found, create it
spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false);
auto forcedType = getForcedType(symbol->getQualifier().builtIn, symbol->getType());

// There are pairs of symbols that map to the same SPIR-V built-in:
// gl_ObjectToWorldEXT and gl_ObjectToWorld3x4EXT, and gl_WorldToObjectEXT
// and gl_WorldToObject3x4EXT. SPIR-V forbids having two OpVariables
// with the same BuiltIn in the same storage class, so we must re-use one.
const bool mayNeedToReuseBuiltIn =
builtIn == spv::BuiltInObjectToWorldKHR ||
builtIn == spv::BuiltInWorldToObjectKHR;

if (mayNeedToReuseBuiltIn) {
auto iter = builtInVariableIds.find(uint32_t(builtIn));
if (builtInVariableIds.end() != iter) {
id = iter->second;
symbolValues[symbol->getId()] = id;
if (forcedType.second != spv::NoType)
forceType[id] = forcedType.second;
return id;
}
}

id = createSpvVariable(symbol, forcedType.first);

if (mayNeedToReuseBuiltIn) {
builtInVariableIds.insert({uint32_t(builtIn), id});
}

symbolValues[symbol->getId()] = id;
if (forcedType.second != spv::NoType)
forceType[id] = forcedType.second;
Expand Down
98 changes: 46 additions & 52 deletions Test/baseResults/spv.ext.AnyHitShader.rahit.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spv.ext.AnyHitShader.rahit
// Module Version 10400
// Generated by (magic number): 8000a
// Id's are bound by 110
// Id's are bound by 108

Capability GroupNonUniform
Capability RayTracingKHR
Expand All @@ -10,7 +10,7 @@ spv.ext.AnyHitShader.rahit
Extension "SPV_KHR_ray_tracing"
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint AnyHitKHR 4 "main" 11 14 20 23 26 33 36 39 42 47 50 53 58 64 67 70 76 80 84 87 101
EntryPoint AnyHitKHR 4 "main" 11 14 20 23 26 33 36 39 42 47 50 53 58 64 67 70 82 85 99
Source GLSL 460
SourceExtension "GL_EXT_ray_cull_mask"
SourceExtension "GL_EXT_ray_tracing"
Expand Down Expand Up @@ -49,13 +49,11 @@ spv.ext.AnyHitShader.rahit
Name 69 "v15"
Name 70 "gl_GeometryIndexEXT"
Name 75 "v16"
Name 76 "gl_ObjectToWorld3x4EXT"
Name 79 "v17"
Name 80 "gl_WorldToObject3x4EXT"
Name 83 "v18"
Name 84 "gl_CullMaskEXT"
Name 87 "incomingPayload"
Name 101 "gl_SubgroupSize"
Name 78 "v17"
Name 81 "v18"
Name 82 "gl_CullMaskEXT"
Name 85 "incomingPayload"
Name 99 "gl_SubgroupSize"
Decorate 11(gl_LaunchIDEXT) BuiltIn LaunchIdKHR
Decorate 14(gl_LaunchSizeEXT) BuiltIn LaunchSizeKHR
Decorate 20(gl_PrimitiveID) BuiltIn PrimitiveId
Expand All @@ -72,13 +70,11 @@ spv.ext.AnyHitShader.rahit
Decorate 64(gl_ObjectToWorldEXT) BuiltIn ObjectToWorldKHR
Decorate 67(gl_WorldToObjectEXT) BuiltIn WorldToObjectKHR
Decorate 70(gl_GeometryIndexEXT) BuiltIn RayGeometryIndexKHR
Decorate 76(gl_ObjectToWorld3x4EXT) BuiltIn ObjectToWorldKHR
Decorate 80(gl_WorldToObject3x4EXT) BuiltIn WorldToObjectKHR
Decorate 84(gl_CullMaskEXT) BuiltIn CullMaskKHR
Decorate 101(gl_SubgroupSize) RelaxedPrecision
Decorate 101(gl_SubgroupSize) BuiltIn SubgroupSize
Decorate 102 RelaxedPrecision
Decorate 103 RelaxedPrecision
Decorate 82(gl_CullMaskEXT) BuiltIn CullMaskKHR
Decorate 99(gl_SubgroupSize) RelaxedPrecision
Decorate 99(gl_SubgroupSize) BuiltIn SubgroupSize
Decorate 100 RelaxedPrecision
Decorate 101 RelaxedPrecision
2: TypeVoid
3: TypeFunction 2
6: TypeInt 32 0
Expand Down Expand Up @@ -118,18 +114,16 @@ spv.ext.AnyHitShader.rahit
72: TypeVector 28(float) 4
73: TypeMatrix 72(fvec4) 3
74: TypePointer Function 73
76(gl_ObjectToWorld3x4EXT): 63(ptr) Variable Input
80(gl_WorldToObject3x4EXT): 63(ptr) Variable Input
84(gl_CullMaskEXT): 57(ptr) Variable Input
86: TypePointer IncomingRayPayloadKHR 72(fvec4)
87(incomingPayload): 86(ptr) Variable IncomingRayPayloadKHR
88: 28(float) Constant 1056964608
89: 72(fvec4) ConstantComposite 88 88 88 88
91: 16(int) Constant 1
92: TypeBool
97: 6(int) Constant 0
101(gl_SubgroupSize): 57(ptr) Variable Input
104: TypePointer IncomingRayPayloadKHR 28(float)
82(gl_CullMaskEXT): 57(ptr) Variable Input
84: TypePointer IncomingRayPayloadKHR 72(fvec4)
85(incomingPayload): 84(ptr) Variable IncomingRayPayloadKHR
86: 28(float) Constant 1056964608
87: 72(fvec4) ConstantComposite 86 86 86 86
89: 16(int) Constant 1
90: TypeBool
95: 6(int) Constant 0
99(gl_SubgroupSize): 57(ptr) Variable Input
102: TypePointer IncomingRayPayloadKHR 28(float)
4(main): 2 Function None 3
5: Label
9(v0): 8(ptr) Variable Function
Expand All @@ -149,8 +143,8 @@ spv.ext.AnyHitShader.rahit
66(v14): 61(ptr) Variable Function
69(v15): 17(ptr) Variable Function
75(v16): 74(ptr) Variable Function
79(v17): 74(ptr) Variable Function
83(v18): 55(ptr) Variable Function
78(v17): 74(ptr) Variable Function
81(v18): 55(ptr) Variable Function
12: 7(ivec3) Load 11(gl_LaunchIDEXT)
Store 9(v0) 12
15: 7(ivec3) Load 14(gl_LaunchSizeEXT)
Expand Down Expand Up @@ -183,28 +177,28 @@ spv.ext.AnyHitShader.rahit
Store 66(v14) 68
71: 16(int) Load 70(gl_GeometryIndexEXT)
Store 69(v15) 71
77: 60 Load 76(gl_ObjectToWorld3x4EXT)
78: 73 Transpose 77
Store 75(v16) 78
81: 60 Load 80(gl_WorldToObject3x4EXT)
82: 73 Transpose 81
Store 79(v17) 82
85: 6(int) Load 84(gl_CullMaskEXT)
Store 83(v18) 85
Store 87(incomingPayload) 89
90: 16(int) Load 18(v2)
93: 92(bool) IEqual 90 91
SelectionMerge 95 None
BranchConditional 93 94 95
94: Label
76: 60 Load 64(gl_ObjectToWorldEXT)
77: 73 Transpose 76
Store 75(v16) 77
79: 60 Load 67(gl_WorldToObjectEXT)
80: 73 Transpose 79
Store 78(v17) 80
83: 6(int) Load 82(gl_CullMaskEXT)
Store 81(v18) 83
Store 85(incomingPayload) 87
88: 16(int) Load 18(v2)
91: 90(bool) IEqual 88 89
SelectionMerge 93 None
BranchConditional 91 92 93
92: Label
IgnoreIntersectionKHR
95: Label
102: 6(int) Load 101(gl_SubgroupSize)
103: 28(float) ConvertUToF 102
105: 104(ptr) AccessChain 87(incomingPayload) 97
106: 28(float) Load 105
107: 28(float) FAdd 106 103
108: 104(ptr) AccessChain 87(incomingPayload) 97
Store 108 107
93: Label
100: 6(int) Load 99(gl_SubgroupSize)
101: 28(float) ConvertUToF 100
103: 102(ptr) AccessChain 85(incomingPayload) 95
104: 28(float) Load 103
105: 28(float) FAdd 104 101
106: 102(ptr) AccessChain 85(incomingPayload) 95
Store 106 105
TerminateRayKHR
FunctionEnd
86 changes: 40 additions & 46 deletions Test/baseResults/spv.ext.ClosestHitShader.rchit.out
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
spv.ext.ClosestHitShader.rchit
// Module Version 10400
// Generated by (magic number): 8000a
// Id's are bound by 104
// Id's are bound by 102

Capability RayTracingKHR
Capability RayCullMaskKHR
Extension "SPV_KHR_ray_cull_mask"
Extension "SPV_KHR_ray_tracing"
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint ClosestHitKHR 4 "main" 11 14 20 23 26 33 36 39 42 47 50 53 58 64 67 70 76 80 84 88 101 103
EntryPoint ClosestHitKHR 4 "main" 11 14 20 23 26 33 36 39 42 47 50 53 58 64 67 70 82 86 99 101
Source GLSL 460
SourceExtension "GL_EXT_ray_cull_mask"
SourceExtension "GL_EXT_ray_tracing"
Expand Down Expand Up @@ -47,14 +47,12 @@ spv.ext.ClosestHitShader.rchit
Name 69 "v15"
Name 70 "gl_GeometryIndexEXT"
Name 75 "v16"
Name 76 "gl_ObjectToWorld3x4EXT"
Name 79 "v17"
Name 80 "gl_WorldToObject3x4EXT"
Name 83 "v18"
Name 84 "gl_CullMaskEXT"
Name 88 "accEXT"
Name 101 "incomingPayload"
Name 103 "localPayload"
Name 78 "v17"
Name 81 "v18"
Name 82 "gl_CullMaskEXT"
Name 86 "accEXT"
Name 99 "incomingPayload"
Name 101 "localPayload"
Decorate 11(gl_LaunchIDEXT) BuiltIn LaunchIdKHR
Decorate 14(gl_LaunchSizeEXT) BuiltIn LaunchSizeKHR
Decorate 20(gl_PrimitiveID) BuiltIn PrimitiveId
Expand All @@ -71,11 +69,9 @@ spv.ext.ClosestHitShader.rchit
Decorate 64(gl_ObjectToWorldEXT) BuiltIn ObjectToWorldKHR
Decorate 67(gl_WorldToObjectEXT) BuiltIn WorldToObjectKHR
Decorate 70(gl_GeometryIndexEXT) BuiltIn RayGeometryIndexKHR
Decorate 76(gl_ObjectToWorld3x4EXT) BuiltIn ObjectToWorldKHR
Decorate 80(gl_WorldToObject3x4EXT) BuiltIn WorldToObjectKHR
Decorate 84(gl_CullMaskEXT) BuiltIn CullMaskKHR
Decorate 88(accEXT) DescriptorSet 0
Decorate 88(accEXT) Binding 0
Decorate 82(gl_CullMaskEXT) BuiltIn CullMaskKHR
Decorate 86(accEXT) DescriptorSet 0
Decorate 86(accEXT) Binding 0
2: TypeVoid
3: TypeFunction 2
6: TypeInt 32 0
Expand Down Expand Up @@ -115,26 +111,24 @@ spv.ext.ClosestHitShader.rchit
72: TypeVector 28(float) 4
73: TypeMatrix 72(fvec4) 3
74: TypePointer Function 73
76(gl_ObjectToWorld3x4EXT): 63(ptr) Variable Input
80(gl_WorldToObject3x4EXT): 63(ptr) Variable Input
84(gl_CullMaskEXT): 57(ptr) Variable Input
86: TypeAccelerationStructureKHR
87: TypePointer UniformConstant 86
88(accEXT): 87(ptr) Variable UniformConstant
90: 6(int) Constant 0
91: 6(int) Constant 1
92: 6(int) Constant 2
93: 6(int) Constant 3
94: 28(float) Constant 1056964608
82(gl_CullMaskEXT): 57(ptr) Variable Input
84: TypeAccelerationStructureKHR
85: TypePointer UniformConstant 84
86(accEXT): 85(ptr) Variable UniformConstant
88: 6(int) Constant 0
89: 6(int) Constant 1
90: 6(int) Constant 2
91: 6(int) Constant 3
92: 28(float) Constant 1056964608
93: 29(fvec3) ConstantComposite 92 92 92
94: 28(float) Constant 1065353216
95: 29(fvec3) ConstantComposite 94 94 94
96: 28(float) Constant 1065353216
97: 29(fvec3) ConstantComposite 96 96 96
98: 28(float) Constant 1061158912
99: 16(int) Constant 1
100: TypePointer IncomingRayPayloadKHR 72(fvec4)
101(incomingPayload): 100(ptr) Variable IncomingRayPayloadKHR
102: TypePointer RayPayloadKHR 72(fvec4)
103(localPayload): 102(ptr) Variable RayPayloadKHR
96: 28(float) Constant 1061158912
97: 16(int) Constant 1
98: TypePointer IncomingRayPayloadKHR 72(fvec4)
99(incomingPayload): 98(ptr) Variable IncomingRayPayloadKHR
100: TypePointer RayPayloadKHR 72(fvec4)
101(localPayload): 100(ptr) Variable RayPayloadKHR
4(main): 2 Function None 3
5: Label
9(v0): 8(ptr) Variable Function
Expand All @@ -154,8 +148,8 @@ spv.ext.ClosestHitShader.rchit
66(v14): 61(ptr) Variable Function
69(v15): 17(ptr) Variable Function
75(v16): 74(ptr) Variable Function
79(v17): 74(ptr) Variable Function
83(v18): 55(ptr) Variable Function
78(v17): 74(ptr) Variable Function
81(v18): 55(ptr) Variable Function
12: 7(ivec3) Load 11(gl_LaunchIDEXT)
Store 9(v0) 12
15: 7(ivec3) Load 14(gl_LaunchSizeEXT)
Expand Down Expand Up @@ -188,15 +182,15 @@ spv.ext.ClosestHitShader.rchit
Store 66(v14) 68
71: 16(int) Load 70(gl_GeometryIndexEXT)
Store 69(v15) 71
77: 60 Load 76(gl_ObjectToWorld3x4EXT)
78: 73 Transpose 77
Store 75(v16) 78
81: 60 Load 80(gl_WorldToObject3x4EXT)
82: 73 Transpose 81
Store 79(v17) 82
85: 6(int) Load 84(gl_CullMaskEXT)
Store 83(v18) 85
89: 86 Load 88(accEXT)
TraceRayKHR 89 90 91 92 93 90 95 94 97 98 101(incomingPayload)
76: 60 Load 64(gl_ObjectToWorldEXT)
77: 73 Transpose 76
Store 75(v16) 77
79: 60 Load 67(gl_WorldToObjectEXT)
80: 73 Transpose 79
Store 78(v17) 80
83: 6(int) Load 82(gl_CullMaskEXT)
Store 81(v18) 83
87: 84 Load 86(accEXT)
TraceRayKHR 87 88 89 90 91 88 93 92 95 96 99(incomingPayload)
Return
FunctionEnd
Loading