Переглянути джерело

[spirv] Majorness decoration for *StructuredBuffer<matrix> (#1035)

Lei Zhang 7 роки тому
батько
коміт
d81941c4cb

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

@@ -932,16 +932,24 @@ uint32_t TypeTranslator::translateResourceType(QualType type, LayoutRule rule) {
     else
       structName = getName(innerType);
 
+    const bool isRowMajor = isRowMajorMatrix(s);
     llvm::SmallVector<const Decoration *, 4> decorations;
+
     // The stride for the runtime array is the size of S.
     uint32_t size = 0, stride = 0;
     std::tie(std::ignore, size) =
-        getAlignmentAndSize(s, rule, /*isRowMajor*/ false, &stride);
+        getAlignmentAndSize(s, rule, isRowMajor, &stride);
     decorations.push_back(Decoration::getArrayStride(context, size));
     const uint32_t raType =
         theBuilder.getRuntimeArrayType(structType, decorations);
 
     decorations.clear();
+
+    // Attach majorness decoration if this is a *StructuredBuffer<matrix>.
+    if (TypeTranslator::isMxNMatrix(s))
+      decorations.push_back(isRowMajor ? Decoration::getColMajor(context, 0)
+                                       : Decoration::getRowMajor(context, 0));
+
     decorations.push_back(Decoration::getOffset(context, 0, 0));
     if (name == "StructuredBuffer")
       decorations.push_back(Decoration::getNonWritable(context, 0));

+ 33 - 0
tools/clang/test/CodeGenSPIRV/type.matrix.majorness.zpc.hlsl

@@ -0,0 +1,33 @@
+// Run: %dxc -T ps_6_0 -E main -Zpc
+
+// CHECK: OpDecorate %_runtimearr_mat2v3float ArrayStride 24
+// CHECK: OpMemberDecorate %type_StructuredBuffer_mat2v3float 0 RowMajor
+       StructuredBuffer<float2x3> ROSB1;
+// CHECK: OpDecorate %_runtimearr_mat3v2float ArrayStride 32
+// CHECK: OpMemberDecorate %type_RWStructuredBuffer_mat3v2float 0 RowMajor
+     RWStructuredBuffer<float3x2> RWSB1;
+// CHECK: OpDecorate %_runtimearr_mat4v3float ArrayStride 48
+// CHECK: OpMemberDecorate %type_AppendStructuredBuffer_mat4v3float 0 RowMajor
+ AppendStructuredBuffer<float4x3> ASB1;
+// CHECK: OpDecorate %_runtimearr_mat3v4float ArrayStride 64
+// CHECK: OpMemberDecorate %type_ConsumeStructuredBuffer_mat3v4float 0 RowMajor
+ConsumeStructuredBuffer<float3x4> CSB1;
+
+// NOTE: The parsed AST does not convey the majorness information for
+// the following cases right now.
+/*
+       StructuredBuffer<row_major float2x3> ROSB2;
+     RWStructuredBuffer<row_major float3x2> RWSB2;
+ AppendStructuredBuffer<row_major float4x3> ASB2;
+ConsumeStructuredBuffer<row_major float3x4> CSB2;
+
+       StructuredBuffer<column_major float2x3> ROSB3;
+     RWStructuredBuffer<column_major float3x2> RWSB3;
+ AppendStructuredBuffer<column_major float4x3> ASB3;
+ConsumeStructuredBuffer<column_major float3x4> CSB3;
+*/
+
+float4 main() : SV_Target {
+    return ROSB1[0][0][0] // + ROSB2[0][0][0] + ROSB3[0][0][0]
+        ;
+}

+ 27 - 0
tools/clang/test/CodeGenSPIRV/type.matrix.majorness.zpr.hlsl

@@ -44,6 +44,33 @@ struct U {
 
 RWStructuredBuffer<U> MySBuffer;
 
+// CHECK: OpDecorate %_runtimearr_mat2v3float ArrayStride 32
+// CHECK: OpMemberDecorate %type_StructuredBuffer_mat2v3float 0 ColMajor
+       StructuredBuffer<float2x3> ROSB1;
+// CHECK: OpDecorate %_runtimearr_mat3v2float ArrayStride 24
+// CHECK: OpMemberDecorate %type_RWStructuredBuffer_mat3v2float 0 ColMajor
+     RWStructuredBuffer<float3x2> RWSB1;
+// CHECK: OpDecorate %_runtimearr_mat4v3float ArrayStride 64
+// CHECK: OpMemberDecorate %type_AppendStructuredBuffer_mat4v3float 0 ColMajor
+ AppendStructuredBuffer<float4x3> ASB1;
+// CHECK: OpDecorate %_runtimearr_mat3v4float ArrayStride 48
+// CHECK: OpMemberDecorate %type_ConsumeStructuredBuffer_mat3v4float 0 ColMajor
+ConsumeStructuredBuffer<float3x4> CSB1;
+
+// NOTE: The parsed AST does not convey the majorness information for
+// the following cases right now.
+/*
+       StructuredBuffer<row_major float2x3> ROSB2;
+     RWStructuredBuffer<row_major float3x2> RWSB2;
+ AppendStructuredBuffer<row_major float4x3> ASB2;
+ConsumeStructuredBuffer<row_major float3x4> CSB2;
+
+       StructuredBuffer<column_major float2x3> ROSB3;
+     RWStructuredBuffer<column_major float3x2> RWSB3;
+ AppendStructuredBuffer<column_major float4x3> ASB3;
+ConsumeStructuredBuffer<column_major float3x4> CSB3;
+*/
+
 float3 main() : A {
   return MySBuffer[0].mat1[1][1];
 }

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

@@ -48,6 +48,9 @@ TEST_F(FileTest, MatrixTypes) { runFileTest("type.matrix.hlsl"); }
 TEST_F(FileTest, MatrixTypesMajornessZpr) {
   runFileTest("type.matrix.majorness.zpr.hlsl");
 }
+TEST_F(FileTest, MatrixTypesMajornessZpc) {
+  runFileTest("type.matrix.majorness.zpc.hlsl");
+}
 TEST_F(FileTest, MatrixTypesMajorness) {
   runFileTest("type.matrix.majorness.hlsl", Expect::Warning);
 }