فهرست منبع

Adding built-in BuiltInTriangleIntersectionAttributes

Young Kim 7 سال پیش
والد
کامیت
968eb6176e

+ 28 - 1
tools/clang/lib/Sema/SemaHLSL.cpp

@@ -2446,6 +2446,26 @@ static CXXRecordDecl *CreateRayDescStruct(clang::ASTContext &context,
   return rayDescDecl;
 }
 
+// struct BuiltInTriangleIntersectionAttributes
+// {
+//   float2 barycentrics;
+// };
+static void AddBuiltInTriangleIntersectionAttributes(ASTContext& context, QualType baryType) {
+    DeclContext *curDC = context.getTranslationUnitDecl();
+    IdentifierInfo &attributesId =
+        context.Idents.get(StringRef("BuiltInTriangleIntersectionAttributes"),
+            tok::TokenKind::identifier);
+    CXXRecordDecl *attributesDecl = CXXRecordDecl::Create(
+        context, TagTypeKind::TTK_Struct, curDC, NoLoc, NoLoc,
+        &attributesId, nullptr, DelayTypeCreationTrue);
+    attributesDecl->startDefinition();
+    // float2 barycentrics;
+    CreateSimpleField(context, attributesDecl, "barycentrics", baryType);
+    attributesDecl->completeDefinition();
+    attributesDecl->setImplicit(true);
+    curDC->addDecl(attributesDecl);
+}
+
 //
 // This is similar to clang/Analysis/CallGraph, but the following differences
 // motivate this:
@@ -3190,6 +3210,8 @@ public:
     for (auto && intrinsic : m_intrinsicTables) {
       AddIntrinsicTableMethods(intrinsic);
     }
+    QualType float2Type = LookupVectorType(HLSLScalarType::HLSLScalarType_float, 2);
+    AddBuiltInTriangleIntersectionAttributes(S.getASTContext(), float2Type);
   }
 
   void ForgetSema() override
@@ -3367,8 +3389,13 @@ public:
     }
 
     if (typeRecordDecl && typeRecordDecl->isImplicit()) {
-      if (typeRecordDecl->getDeclContext()->isFileContext())
+      if (typeRecordDecl->getDeclContext()->isFileContext()) {
+        // BuiltInTriangleIntersectionAttributes will be considered as a user
+        // defined type for diagnostic purposes.
+        if (typeRecordDecl->getName().equals("BuiltInTriangleIntersectionAttributes"))
+            return AR_TOBJ_COMPOUND;
         return AR_TOBJ_OBJECT;
+      }
       else
         return AR_TOBJ_INNER_OBJ;
     }

+ 5 - 1
tools/clang/test/CodeGenHLSL/quick-test/raytracing_rayflag_hitkind.hlsl → tools/clang/test/CodeGenHLSL/quick-test/raytracing_builtin.hlsl

@@ -1,5 +1,7 @@
 // RUN: %dxc -T lib_6_2 %s | FileCheck %s
 
+// CHECK: %struct.BuiltInTriangleIntersectionAttributes
+
 // CHECK: call void @dx.op.rawBufferStore.i32({{.*}}, i32 0
 // CHECK: call void @dx.op.rawBufferStore.i32({{.*}}, i32 1
 // CHECK: call void @dx.op.rawBufferStore.i32({{.*}}, i32 2
@@ -14,7 +16,7 @@
 
 RWByteAddressBuffer g_buf;
 
-void check() {
+void check(BuiltInTriangleIntersectionAttributes attr) {
     g_buf.Store(0, RAY_FLAG_NONE);
     g_buf.Store(4, RAY_FLAG_FORCE_OPAQUE);
     g_buf.Store(8, RAY_FLAG_FORCE_NON_OPAQUE);
@@ -26,4 +28,6 @@ void check() {
     g_buf.Store(32, RAY_FLAG_CULL_NON_OPAQUE);
     g_buf.Store(36, HIT_KIND_TRIANGLE_FRONT_FACE);
     g_buf.Store(40, HIT_KIND_TRIANGLE_BACK_FACE);
+    g_buf.Store(44, attr.barycentrics.x);
+    g_buf.Store(48, attr.barycentrics.y);
 }

+ 0 - 5
tools/clang/test/CodeGenHLSL/quick-test/raytracing_raygeneration.hlsl

@@ -29,11 +29,6 @@ typedef uint RAY_FLAG;
 #define RAY_FLAG_CULL_OPAQUE                      0x40
 #define RAY_FLAG_CULL_NON_OPAQUE                  0x80
 
-struct BuiltInTriangleIntersectionAttributes
-{
-    float2 barycentrics;
-};
-
 ////////////////////////////////////////////////////////////////////////////
 
 struct MyPayload {

+ 4 - 0
tools/clang/test/HLSL/raytracings.hlsl

@@ -20,4 +20,8 @@ void run() {
     HIT_KIND_TRIANGLE_FRONT_FACE + HIT_KIND_TRIANGLE_BACK_FACE;
 
   hitKindFlag += HIT_KIND_INVALID;                          /* expected-error {{use of undeclared identifier 'HIT_KIND_INVALID'}} */
+
+  BuiltInTriangleIntersectionAttributes attr;
+  attr.barycentrics = float2(0.3f, 0.4f);
+  attr.barycentrics.z = 3.0f;                               /* expected-error {{vector swizzle 'z' is out of bounds}} */
 }