2
0
Эх сурвалжийг харах

[spirv] Avoid getting constant zero for static resources (#1056)

For static resources not explicitly initialized, we cannot initialize
them since there are no meaningful zero values. Just do nothing.

Fixes https://github.com/Microsoft/DirectXShaderCompiler/issues/1055
Lei Zhang 7 жил өмнө
parent
commit
1f1ea2b7f5

+ 9 - 1
tools/clang/lib/SPIRV/SPIRVEmitter.cpp

@@ -4687,6 +4687,11 @@ SpirvEvalInfo SPIRVEmitter::processBinaryOp(const Expr *lhs, const Expr *rhs,
 
 void SPIRVEmitter::initOnce(QualType varType, std::string varName,
                             uint32_t varPtr, const Expr *varInit) {
+  // For uninitialized resource objects, we do nothing since there is no
+  // meaningful zero values for them.
+  if (!varInit && hlsl::IsHLSLResourceType(varType))
+    return;
+
   const uint32_t boolType = theBuilder.getBoolType();
   varName = "init.done." + varName;
 
@@ -8042,7 +8047,10 @@ bool SPIRVEmitter::emitEntryFunctionWrapper(const FunctionDecl *decl,
 
       // Update counter variable associated with global variables
       tryToAssignCounterVar(varDecl, init);
-    } else {
+    }
+    // If not explicitly initialized, initialize with their zero values if not
+    // resource objects
+    else if (!hlsl::IsHLSLResourceType(varDecl->getType())) {
       const auto typeId = typeTranslator.translateType(varDecl->getType());
       theBuilder.createStore(varInfo, theBuilder.getConstantNull(typeId));
     }

+ 24 - 0
tools/clang/test/CodeGenSPIRV/var.static.resource.hlsl

@@ -0,0 +1,24 @@
+// Run: %dxc -T vs_6_0 -E main
+
+// No inline initializer
+
+// CHECK: %sRWTex = OpVariable %_ptr_Private_type_2d_image Private
+// CHECK: %lRWTex = OpVariable %_ptr_Private_type_2d_image Private
+
+static RWTexture2D<float4> sRWTex;
+
+// No OpStore
+
+// CHECK-LABEL:    %main = OpFunction
+// CHECK-NEXT:             OpLabel
+// CHECK-NEXT:             OpFunctionCall %void %src_main
+// CHECK-NEXT:             OpReturn
+// CHECK-NEXT:             OpFunctionEnd
+
+// CHECK:     %src_main = OpFunction
+// CHECK-NEXT:             OpLabel
+// CHECK-NEXT:             OpReturn
+// CHECK-NEXT:             OpFunctionEnd
+void main() {
+    static RWTexture2D<float4> lRWTex;
+}

+ 3 - 0
tools/clang/unittests/SPIRV/CodeGenSPIRVTest.cpp

@@ -120,6 +120,9 @@ TEST_F(FileTest, VarInitCrossStorageClass) {
   runFileTest("var.init.cross-storage-class.hlsl");
 }
 TEST_F(FileTest, StaticVar) { runFileTest("var.static.hlsl"); }
+TEST_F(FileTest, UninitStaticResourceVar) {
+  runFileTest("var.static.resource.hlsl");
+}
 TEST_F(FileTest, GlobalMatrixVar) { runFileTest("var.global-mat.hlsl"); }
 
 // For prefix/postfix increment/decrement