From fec9c42d4c61d1bdd54d020d55a3155e0d54fce7 Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 22 Jan 2024 15:19:26 -0800 Subject: [PATCH] Bug fixes for the direct spirv backend. (#3474) * Fix GLSL legalization bug that leads to crash. * Update diagnostic id to avoid conflict. * Fix std140 layout logic. --------- Co-authored-by: Yong He --- source/slang/slang-diagnostic-defs.h | 2 +- source/slang/slang-ir-glsl-legalize.cpp | 10 ++++++++-- source/slang/slang-ir-layout.cpp | 5 +++-- tests/spirv/std140-layout.slang | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 tests/spirv/std140-layout.slang diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h index b7189ab0d9..9bad4b7169 100644 --- a/source/slang/slang-diagnostic-defs.h +++ b/source/slang/slang-diagnostic-defs.h @@ -451,7 +451,7 @@ DIAGNOSTIC(30510, Error, loopInDiffFuncRequireUnrollOrMaxIters, "loops inside a DIAGNOSTIC(39999, Fatal, cyclicReference, "cyclic reference '$0'.") DIAGNOSTIC(39999, Error, localVariableUsedBeforeDeclared, "local variable '$0' is being used before its declaration.") DIAGNOSTIC(39999, Error, variableUsedInItsOwnDefinition, "the initial-value expression for variable '$0' depends on the value of the variable itself") -DIAGNOSTIC(39001, Fatal , cannotProcessInclude, "internal compiler error: cannot process '__include' in the current semantic checking context.") +DIAGNOSTIC(39901, Fatal , cannotProcessInclude, "internal compiler error: cannot process '__include' in the current semantic checking context.") // 304xx: generics DIAGNOSTIC(30400, Error, genericTypeNeedsArgs, "generic type '$0' used without argument") diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp index 4d2b12bc45..65efde2143 100644 --- a/source/slang/slang-ir-glsl-legalize.cpp +++ b/source/slang/slang-ir-glsl-legalize.cpp @@ -1599,7 +1599,10 @@ void assign( auto arrayIndexInst = builder->getIntValue(builder->getIntType(), arrayIndex); // Store to the index - auto address = builder->emitElementAddress(right.irValue->getFullType(), left.irValue, arrayIndexInst); + auto address = builder->emitElementAddress( + builder->getPtrType(right.irValue->getFullType()), + left.irValue, + arrayIndexInst); builder->emitStore(address, rhs); break; @@ -1613,7 +1616,10 @@ void assign( auto address = left.irValue; if(index) { - address = builder->emitElementAddress(right.irValue->getFullType(), left.irValue, index); + address = builder->emitElementAddress( + builder->getPtrType(right.irValue->getFullType()), + left.irValue, + index); } builder->emitStore(address, right.irValue); break; diff --git a/source/slang/slang-ir-layout.cpp b/source/slang/slang-ir-layout.cpp index 79012e7ba4..a6684c7b13 100644 --- a/source/slang/slang-ir-layout.cpp +++ b/source/slang/slang-ir-layout.cpp @@ -461,9 +461,10 @@ struct Std140LayoutRules : IRTypeLayoutRules } virtual IRSizeAndAlignment getVectorSizeAndAlignment(IRSizeAndAlignment element, IRIntegerValue count) { + IRIntegerValue alignmentCount = count; if (count == 3) - count = 4; - return IRSizeAndAlignment((int)(element.size * count), (int)(element.size * count)); + alignmentCount = 4; + return IRSizeAndAlignment((int)(element.size * count), (int)(element.size * alignmentCount)); } }; diff --git a/tests/spirv/std140-layout.slang b/tests/spirv/std140-layout.slang new file mode 100644 index 0000000000..9a09dbef33 --- /dev/null +++ b/tests/spirv/std140-layout.slang @@ -0,0 +1,17 @@ + +//TEST:SIMPLE(filecheck=SPIRV): -stage fragment -entry main -target spirv -emit-spirv-directly + +// SPIRV: OpMemberDecorate %{{.*}} 0 Offset 0 +// SPIRV: OpMemberDecorate %{{.*}} 1 Offset 16 +// SPIRV: OpMemberDecorate %{{.*}} 2 Offset 28 + +uniform float3 v0; +uniform float3 v1; +uniform float v2; + + + +float4 main() : SV_Target +{ + return float4(v2); +}