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

Fixes an issue where invalid SPIR-V was generated when
gl_ObjectToWorldEXT and gl_ObjectToWorld3x4EXT, or
gl_WorldToObjectEXT and gl_WorldToObject3x4EXT, were used in the same
shader. The SPIR-V specification requires that there be at most one
OpVariable decorated with a given BuiltIn value.
This commit is contained in:
Andrea Faulds 2022-06-01 10:43:13 +02:00
parent 316f12ac1d
commit 6cdae46314
4 changed files with 142 additions and 134 deletions

View File

@ -260,6 +260,7 @@ protected:
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;
@ -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;

View File

@ -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
@ -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"
@ -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
@ -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
@ -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
@ -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)
@ -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

View File

@ -1,7 +1,7 @@
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
@ -9,7 +9,7 @@ spv.ext.ClosestHitShader.rchit
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"
@ -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
@ -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
@ -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
@ -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)
@ -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

View File

@ -1,7 +1,7 @@
spv.ext.IntersectShader.rint
// Module Version 10400
// Generated by (magic number): 8000a
// Id's are bound by 86
// Id's are bound by 84
Capability RayTracingKHR
Capability RayCullMaskKHR
@ -9,7 +9,7 @@ spv.ext.IntersectShader.rint
Extension "SPV_KHR_ray_tracing"
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint IntersectionKHR 4 "main" 11 14 20 23 26 33 36 39 42 47 50 56 59 65 69 75 78
EntryPoint IntersectionKHR 4 "main" 11 14 20 23 26 33 36 39 42 47 50 56 59 73 76
Source GLSL 460
SourceExtension "GL_EXT_ray_cull_mask"
SourceExtension "GL_EXT_ray_tracing"
@ -41,12 +41,10 @@ spv.ext.IntersectShader.rint
Name 58 "v12"
Name 59 "gl_WorldToObjectEXT"
Name 64 "v13"
Name 65 "gl_ObjectToWorld3x4EXT"
Name 68 "v14"
Name 69 "gl_WorldToObject3x4EXT"
Name 73 "v15"
Name 75 "gl_CullMaskEXT"
Name 78 "iAttr"
Name 67 "v14"
Name 71 "v15"
Name 73 "gl_CullMaskEXT"
Name 76 "iAttr"
Decorate 11(gl_LaunchIDEXT) BuiltIn LaunchIdKHR
Decorate 14(gl_LaunchSizeEXT) BuiltIn LaunchSizeKHR
Decorate 20(gl_PrimitiveID) BuiltIn PrimitiveId
@ -62,9 +60,7 @@ spv.ext.IntersectShader.rint
Decorate 50(gl_RayTmaxEXT) Coherent
Decorate 56(gl_ObjectToWorldEXT) BuiltIn ObjectToWorldKHR
Decorate 59(gl_WorldToObjectEXT) BuiltIn WorldToObjectKHR
Decorate 65(gl_ObjectToWorld3x4EXT) BuiltIn ObjectToWorldKHR
Decorate 69(gl_WorldToObject3x4EXT) BuiltIn WorldToObjectKHR
Decorate 75(gl_CullMaskEXT) BuiltIn CullMaskKHR
Decorate 73(gl_CullMaskEXT) BuiltIn CullMaskKHR
2: TypeVoid
3: TypeFunction 2
6: TypeInt 32 0
@ -99,19 +95,17 @@ spv.ext.IntersectShader.rint
61: TypeVector 28(float) 4
62: TypeMatrix 61(fvec4) 3
63: TypePointer Function 62
65(gl_ObjectToWorld3x4EXT): 55(ptr) Variable Input
69(gl_WorldToObject3x4EXT): 55(ptr) Variable Input
72: TypePointer Function 6(int)
74: TypePointer Input 6(int)
75(gl_CullMaskEXT): 74(ptr) Variable Input
77: TypePointer HitAttributeKHR 61(fvec4)
78(iAttr): 77(ptr) Variable HitAttributeKHR
79: 28(float) Constant 1056964608
80: 28(float) Constant 0
81: 28(float) Constant 1065353216
82: 61(fvec4) ConstantComposite 79 79 80 81
83: 6(int) Constant 1
84: TypeBool
70: TypePointer Function 6(int)
72: TypePointer Input 6(int)
73(gl_CullMaskEXT): 72(ptr) Variable Input
75: TypePointer HitAttributeKHR 61(fvec4)
76(iAttr): 75(ptr) Variable HitAttributeKHR
77: 28(float) Constant 1056964608
78: 28(float) Constant 0
79: 28(float) Constant 1065353216
80: 61(fvec4) ConstantComposite 77 77 78 79
81: 6(int) Constant 1
82: TypeBool
4(main): 2 Function None 3
5: Label
9(v0): 8(ptr) Variable Function
@ -128,8 +122,8 @@ spv.ext.IntersectShader.rint
54(v11): 53(ptr) Variable Function
58(v12): 53(ptr) Variable Function
64(v13): 63(ptr) Variable Function
68(v14): 63(ptr) Variable Function
73(v15): 72(ptr) Variable Function
67(v14): 63(ptr) Variable Function
71(v15): 70(ptr) Variable Function
12: 7(ivec3) Load 11(gl_LaunchIDEXT)
Store 9(v0) 12
15: 7(ivec3) Load 14(gl_LaunchSizeEXT)
@ -156,15 +150,15 @@ spv.ext.IntersectShader.rint
Store 54(v11) 57
60: 52 Load 59(gl_WorldToObjectEXT)
Store 58(v12) 60
66: 52 Load 65(gl_ObjectToWorld3x4EXT)
67: 62 Transpose 66
Store 64(v13) 67
70: 52 Load 69(gl_WorldToObject3x4EXT)
71: 62 Transpose 70
Store 68(v14) 71
76: 6(int) Load 75(gl_CullMaskEXT)
Store 73(v15) 76
Store 78(iAttr) 82
85: 84(bool) ReportIntersectionKHR 79 83
65: 52 Load 56(gl_ObjectToWorldEXT)
66: 62 Transpose 65
Store 64(v13) 66
68: 52 Load 59(gl_WorldToObjectEXT)
69: 62 Transpose 68
Store 67(v14) 69
74: 6(int) Load 73(gl_CullMaskEXT)
Store 71(v15) 74
Store 76(iAttr) 80
83: 82(bool) ReportIntersectionKHR 77 81
Return
FunctionEnd