Forráskód Böngészése

[spirv] More compute shader semantics. (#662)

* SV_GroupID
* SV_GroupIndex
* SV_GroupThreadID
Ehsan 8 éve
szülő
commit
58612be9f3

+ 25 - 19
docs/SPIR-V.rst

@@ -590,25 +590,31 @@ Firstly, under certain `SigPoints <https://github.com/Microsoft/DirectXShaderCom
 some system-value (SV) semantic strings will be translated into SPIR-V
 ``BuiltIn`` decorations:
 
-+----------------------+----------+------------------------+-----------------------+
-| HLSL Semantic        | SigPoint | SPIR-V ``BuiltIn``     | SPIR-V Execution Mode |
-+======================+==========+========================+=======================+
-|                      | VSOut    | ``Position``           | N/A                   |
-| SV_Position          +----------+------------------------+-----------------------+
-|                      | PSIn     | ``FragCoord``          | N/A                   |
-+----------------------+----------+------------------------+-----------------------+
-| SV_VertexID          | VSIn     | ``VertexIndex``        | N/A                   |
-+----------------------+----------+------------------------+-----------------------+
-| SV_InstanceID        | VSIn     | ``InstanceIndex``      | N/A                   |
-+----------------------+----------+------------------------+-----------------------+
-| SV_Depth             | PSOut    | ``FragDepth``          | N/A                   |
-+----------------------+----------+------------------------+-----------------------+
-| SV_DepthGreaterEqual | PSOut    | ``FragDepth``          | ``DepthGreater``      |
-+----------------------+----------+------------------------+-----------------------+
-| SV_DepthLessEqual    | PSOut    | ``FragDepth``          | ``DepthLess``         |
-+----------------------+----------+------------------------+-----------------------+
-| SV_DispatchThreadID  | CSIn     | ``GlobalInvocationId`` | N/A                   |
-+----------------------+----------+------------------------+-----------------------+
++----------------------+----------+--------------------------+-----------------------+
+| HLSL Semantic        | SigPoint | SPIR-V ``BuiltIn``       | SPIR-V Execution Mode |
++======================+==========+==========================+=======================+
+|                      | VSOut    | ``Position``             | N/A                   |
+| SV_Position          +----------+--------------------------+-----------------------+
+|                      | PSIn     | ``FragCoord``            | N/A                   |
++----------------------+----------+--------------------------+-----------------------+
+| SV_VertexID          | VSIn     | ``VertexIndex``          | N/A                   |
++----------------------+----------+--------------------------+-----------------------+
+| SV_InstanceID        | VSIn     | ``InstanceIndex``        | N/A                   |
++----------------------+----------+--------------------------+-----------------------+
+| SV_Depth             | PSOut    | ``FragDepth``            | N/A                   |
++----------------------+----------+--------------------------+-----------------------+
+| SV_DepthGreaterEqual | PSOut    | ``FragDepth``            | ``DepthGreater``      |
++----------------------+----------+--------------------------+-----------------------+
+| SV_DepthLessEqual    | PSOut    | ``FragDepth``            | ``DepthLess``         |
++----------------------+----------+--------------------------+-----------------------+
+| SV_DispatchThreadID  | CSIn     | ``GlobalInvocationId``   | N/A                   |
++----------------------+----------+--------------------------+-----------------------+
+| SV_GroupID           | CSIn     | ``WorkgroupId``          | N/A                   |
++----------------------+----------+--------------------------+-----------------------+
+| SV_GroupThreadID     | CSIn     | ``LocalInvocationId``    | N/A                   |
++----------------------+----------+--------------------------+-----------------------+
+| SV_GroupIndex        | CSIn     | ``LocalInvocationIndex`` | N/A                   |
++----------------------+----------+--------------------------+-----------------------+
 
 [TODO] add other SV semantic strings in the above
 

+ 21 - 0
tools/clang/lib/SPIRV/DeclResultIdMapper.cpp

@@ -763,6 +763,27 @@ uint32_t DeclResultIdMapper::createSpirvStageVar(StageVar *stageVar,
     return theBuilder.addStageBuiltinVar(type, spv::StorageClass::Input,
                                          BuiltIn::GlobalInvocationId);
   }
+  case hlsl::Semantic::Kind::GroupID: {
+    // GroupID semantic is only valid for compute shaders, and it is always an
+    // input.
+    stageVar->setIsSpirvBuiltin();
+    return theBuilder.addStageBuiltinVar(type, spv::StorageClass::Input,
+                                         BuiltIn::WorkgroupId);
+  }
+  case hlsl::Semantic::Kind::GroupThreadID: {
+    // GroupThreadID semantic is only valid for compute shaders, and it is
+    // always an input.
+    stageVar->setIsSpirvBuiltin();
+    return theBuilder.addStageBuiltinVar(type, spv::StorageClass::Input,
+                                         BuiltIn::LocalInvocationId);
+  }
+  case hlsl::Semantic::Kind::GroupIndex: {
+    // GroupIndex semantic is only valid for compute shaders, and it is
+    // always an input.
+    stageVar->setIsSpirvBuiltin();
+    return theBuilder.addStageBuiltinVar(type, spv::StorageClass::Input,
+                                         BuiltIn::LocalInvocationIndex);
+  }
   default:
     emitError("semantic %0 unimplemented yet")
         << stageVar->getSemantic()->GetName();

+ 7 - 0
tools/clang/test/CodeGenSPIRV/semantic.group-id.cs.hlsl

@@ -0,0 +1,7 @@
+// Run: %dxc -T cs_6_0 -E main
+
+// CHECK: OpEntryPoint GLCompute %main "main" %gl_WorkGroupID
+// CHECK: OpDecorate %gl_WorkGroupID BuiltIn WorkgroupId
+// CHECK: %gl_WorkGroupID = OpVariable %_ptr_Input_v3uint Input
+
+void main(uint3 tid : SV_GroupID) {}

+ 7 - 0
tools/clang/test/CodeGenSPIRV/semantic.group-index.cs.hlsl

@@ -0,0 +1,7 @@
+// Run: %dxc -T cs_6_0 -E main
+
+// CHECK: OpEntryPoint GLCompute %main "main" %gl_LocalInvocationIndex
+// CHECK: OpDecorate %gl_LocalInvocationIndex BuiltIn LocalInvocationIndex
+// CHECK: %gl_LocalInvocationIndex = OpVariable %_ptr_Input_uint Input
+
+void main(uint gid : SV_GroupIndex) : A {}

+ 7 - 0
tools/clang/test/CodeGenSPIRV/semantic.group-thread-id.cs.hlsl

@@ -0,0 +1,7 @@
+// Run: %dxc -T cs_6_0 -E main
+
+// CHECK: OpEntryPoint GLCompute %main "main" %gl_LocalInvocationID
+// CHECK: OpDecorate %gl_LocalInvocationID BuiltIn LocalInvocationId
+// CHECK: %gl_LocalInvocationID = OpVariable %_ptr_Input_v3uint Input
+
+void main(uint3 gtid : SV_GroupThreadID) : A {}

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

@@ -360,6 +360,15 @@ TEST_F(FileTest, SemanticDuplication) {
 TEST_F(FileTest, SemanticDispatchThreadId) {
   runFileTest("semantic.dispatch-thread-id.cs.hlsl");
 }
+TEST_F(FileTest, SemanticGroupID) {
+  runFileTest("semantic.group-id.cs.hlsl");
+}
+TEST_F(FileTest, SemanticGroupThreadID) {
+  runFileTest("semantic.group-thread-id.cs.hlsl");
+}
+TEST_F(FileTest, SemanticGroupIndex) {
+  runFileTest("semantic.group-index.cs.hlsl");
+}
 
 // For texture methods
 TEST_F(FileTest, TextureSample) { runFileTest("texture.sample.hlsl"); }