Browse Source

Fix [[vk::push_constant]] on ConstantBuffer (#3215)

This commit fix a regression introduced by #3147: when`[[vk::push_constant]]` is apply to a  ConstantBuffer, error shows that `'push_constant' attribute only applies to global variables of struct type`.
Junda Liu 4 years ago
parent
commit
b126db950c

+ 4 - 2
tools/clang/lib/AST/ASTContextHLSL.cpp

@@ -838,8 +838,10 @@ hlsl::DeclareConstantBufferViewType(clang::ASTContext &context, bool bTBuf) {
   // template<typename T> ConstantBuffer { int h; }
   // template<typename T> ConstantBuffer { int h; }
   DeclContext *DC = context.getTranslationUnitDecl();
   DeclContext *DC = context.getTranslationUnitDecl();
 
 
-  BuiltinTypeDeclBuilder typeDeclBuilder(DC, bTBuf ? "TextureBuffer"
-                                                   : "ConstantBuffer");
+  BuiltinTypeDeclBuilder typeDeclBuilder(DC,
+                                         bTBuf ? "TextureBuffer"
+                                               : "ConstantBuffer",
+                                         TagDecl::TagKind::TTK_Struct);
   (void)typeDeclBuilder.addTypeTemplateParam("T");
   (void)typeDeclBuilder.addTypeTemplateParam("T");
   typeDeclBuilder.startDefinition();
   typeDeclBuilder.startDefinition();
   CXXRecordDecl *templateRecordDecl = typeDeclBuilder.getRecordDecl();
   CXXRecordDecl *templateRecordDecl = typeDeclBuilder.getRecordDecl();

+ 14 - 0
tools/clang/test/CodeGenSPIRV/vk.push-constant.constantbuffer.hlsl

@@ -0,0 +1,14 @@
+// Run: %dxc -T ps_6_0 -E main
+
+struct StructA
+{
+    float3 one;
+    float3 two;
+};
+
+// CHECK: %PushConstants = OpVariable %_ptr_PushConstant_type_PushConstant_ConstantBuffer PushConstant
+[[vk::push_constant]] ConstantBuffer<StructA> PushConstants;
+
+void main()
+{
+}

+ 4 - 0
tools/clang/unittests/SPIRV/CodeGenSpirvTest.cpp

@@ -1871,6 +1871,10 @@ TEST_F(FileTest, VulkanMultiplePushConstant) {
   runFileTest("vk.push-constant.multiple.hlsl", Expect::Failure);
   runFileTest("vk.push-constant.multiple.hlsl", Expect::Failure);
 }
 }
 
 
+TEST_F(FileTest, VulkanPushCOnstantOnConstantBuffer) {
+  runFileTest("vk.push-constant.constantbuffer.hlsl");
+}
+
 TEST_F(FileTest, VulkanSpecConstantInit) {
 TEST_F(FileTest, VulkanSpecConstantInit) {
   runFileTest("vk.spec-constant.init.hlsl");
   runFileTest("vk.spec-constant.init.hlsl");
 }
 }