瀏覽代碼

Adding RayFlags and HitKind built in constants

Young Kim 7 年之前
父節點
當前提交
69e2583196

+ 3 - 0
tools/clang/include/clang/AST/HlslTypes.h

@@ -308,6 +308,9 @@ void AddRecordTypeWithHandle(
   _Outptr_  clang::CXXRecordDecl** typeDecl, 
   _In_z_    const char* typeName);
 
+void AddRayFlags(clang::ASTContext& context);
+void AddHitKinds(clang::ASTContext& context);
+
 /// <summary>Adds the implementation for std::is_equal.</summary>
 void AddStdIsEqualImplementation(clang::ASTContext& context, clang::Sema& sema);
 

+ 44 - 0
tools/clang/lib/AST/ASTContextHLSL.cpp

@@ -518,6 +518,50 @@ void hlsl::AddRecordTypeWithHandle(ASTContext& context, _Outptr_ CXXRecordDecl**
   *typeDecl = newDecl;
 }
 
+// creates a global static constant unsigned integer with value.
+// equivalent to: static const uint name = val;
+static void AddConstInt(clang::ASTContext& context, DeclContext *DC, StringRef name, int val) {
+  IdentifierInfo &Id = context.Idents.get(name, tok::TokenKind::identifier);
+  QualType type = context.getConstType(context.UnsignedIntTy);
+  VarDecl *varDecl = VarDecl::Create(context, DC, NoLoc, NoLoc, &Id, type,
+                                context.getTrivialTypeSourceInfo(type),
+                                clang::StorageClass::SC_Static);
+  Expr *exprVal = IntegerLiteral::Create(
+      context, llvm::APInt(context.getIntWidth(type), val), type, NoLoc);
+  varDecl->setInit(exprVal);
+  varDecl->setImplicit(true);
+  DC->addDecl(varDecl);
+}
+
+/// <summary> Adds a const integers for ray flags </summary>
+void hlsl::AddRayFlags(ASTContext& context) {
+  DeclContext *curDC = context.getTranslationUnitDecl();
+  // typedef uint RAY_FLAG;
+  IdentifierInfo &rayFlagId = context.Idents.get(StringRef("RAY_FLAG"), tok::TokenKind::identifier);
+  TypeSourceInfo *uintTypeSource = context.getTrivialTypeSourceInfo(context.UnsignedIntTy, NoLoc);
+  TypedefDecl *rayFlagDecl = TypedefDecl::Create(context, curDC, NoLoc, NoLoc, &rayFlagId, uintTypeSource);
+  curDC->addDecl(rayFlagDecl);
+  rayFlagDecl->setImplicit(true);
+  // static const uint RAY_FLAG_* = *;
+  AddConstInt(context, curDC, StringRef("RAY_FLAG_NONE"), 0x00);
+  AddConstInt(context, curDC, StringRef("RAY_FLAG_FORCE_OPAQUE"), 0x01);
+  AddConstInt(context, curDC, StringRef("RAY_FLAG_FORCE_NON_OPAQUE"), 0x02);
+  AddConstInt(context, curDC, StringRef("RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH"), 0x04);
+  AddConstInt(context, curDC, StringRef("RAY_FLAG_SKIP_CLOSEST_HIT_SHADER"), 0x08);
+  AddConstInt(context, curDC, StringRef("RAY_FLAG_CULL_BACK_FACING_TRIANGLES"), 0x10);
+  AddConstInt(context, curDC, StringRef("RAY_FLAG_CULL_FRONT_FACING_TRIANGLES"), 0x20);
+  AddConstInt(context, curDC, StringRef("RAY_FLAG_CULL_OPAQUE"), 0x40);
+  AddConstInt(context, curDC, StringRef("RAY_FLAG_CULL_NON_OPAQUE"), 0x80);
+}
+
+/// <summary> Adds a constant integers for hit kinds </summary>
+void hlsl::AddHitKinds(ASTContext& context) {
+  DeclContext *curDC = context.getTranslationUnitDecl();
+  // static const uint HIT_KIND_* = *;
+  AddConstInt(context, curDC, StringRef("HIT_KIND_TRIANGLE_FRONT_FACE"), 0xfe);
+  AddConstInt(context, curDC, StringRef("HIT_KIND_TRIANGLE_BACK_FACE"), 0xff);
+}
+
 static
 Expr* IntConstantAsBoolExpr(clang::Sema& sema, uint64_t value)
 {

+ 4 - 0
tools/clang/lib/Sema/SemaHLSL.cpp

@@ -3931,6 +3931,10 @@ public:
     AddHLSLMatrixTemplate(*m_context, m_vectorTemplateDecl, &m_matrixTemplateDecl);
     DXASSERT(m_matrixTemplateDecl != nullptr, "AddHLSLMatrixTypes failed to return the matrix template declaration");
 
+    // Initializing built in integers for ray tracing
+    AddRayFlags(*m_context);
+    AddHitKinds(*m_context);
+
     return true;
   }
 

+ 29 - 0
tools/clang/test/CodeGenHLSL/quick-test/raytracing_rayflag_hitkind.hlsl

@@ -0,0 +1,29 @@
+// RUN: %dxc -T lib_6_2 %s | FileCheck %s
+
+// 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
+// CHECK: call void @dx.op.rawBufferStore.i32({{.*}}, i32 4
+// CHECK: call void @dx.op.rawBufferStore.i32({{.*}}, i32 8
+// CHECK: call void @dx.op.rawBufferStore.i32({{.*}}, i32 16
+// CHECK: call void @dx.op.rawBufferStore.i32({{.*}}, i32 32
+// CHECK: call void @dx.op.rawBufferStore.i32({{.*}}, i32 64
+// CHECK: call void @dx.op.rawBufferStore.i32({{.*}}, i32 128
+// CHECK: call void @dx.op.rawBufferStore.i32({{.*}}, i32 254
+// CHECK: call void @dx.op.rawBufferStore.i32({{.*}}, i32 255
+
+RWByteAddressBuffer g_buf;
+
+void check() {
+    g_buf.Store(0, RAY_FLAG_NONE);
+    g_buf.Store(4, RAY_FLAG_FORCE_OPAQUE);
+    g_buf.Store(8, RAY_FLAG_FORCE_NON_OPAQUE);
+    g_buf.Store(12, RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH);
+    g_buf.Store(16, RAY_FLAG_SKIP_CLOSEST_HIT_SHADER);
+    g_buf.Store(20, RAY_FLAG_CULL_BACK_FACING_TRIANGLES);
+    g_buf.Store(24, RAY_FLAG_CULL_FRONT_FACING_TRIANGLES);
+    g_buf.Store(28, RAY_FLAG_CULL_OPAQUE);
+    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);
+}

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

@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -HV 2018 -Wno-unused-value -fsyntax-only -ffreestanding -verify %s
+
+void run() {
+  RAY_FLAG rayFlag =
+    RAY_FLAG_NONE                            +
+    RAY_FLAG_FORCE_OPAQUE                    +
+    RAY_FLAG_FORCE_NON_OPAQUE                +
+    RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH +
+    RAY_FLAG_SKIP_CLOSEST_HIT_SHADER         +
+    RAY_FLAG_CULL_BACK_FACING_TRIANGLES      +
+    RAY_FLAG_CULL_FRONT_FACING_TRIANGLES     +
+    RAY_FLAG_CULL_OPAQUE                     +
+    RAY_FLAG_CULL_NON_OPAQUE;
+
+  rayFlag += RAY_FLAG_INVALID;                             /* expected-note@? {{'RAY_FLAG_NONE' declared here}} */ /* expected-error {{use of undeclared identifier 'RAY_FLAG_INVALID'; did you mean 'RAY_FLAG_NONE'?}} */
+
+  int intFlag = RAY_FLAG_CULL_OPAQUE;
+
+  int hitKindFlag =
+    HIT_KIND_TRIANGLE_FRONT_FACE + HIT_KIND_TRIANGLE_BACK_FACE;
+
+  hitKindFlag += HIT_KIND_INVALID;                          /* expected-error {{use of undeclared identifier 'HIT_KIND_INVALID'}} */
+}

+ 5 - 0
tools/clang/unittests/HLSL/VerifierTest.cpp

@@ -51,6 +51,7 @@ public:
   TEST_METHOD(RunMoreOperators);
   TEST_METHOD(RunObjectOperators);
   TEST_METHOD(RunPackReg);
+  TEST_METHOD(RunRayTracings);
   TEST_METHOD(RunScalarAssignments);
   TEST_METHOD(RunScalarAssignmentsExactPrecision);
   TEST_METHOD(RunScalarOperatorsAssign);
@@ -210,6 +211,10 @@ TEST_F(VerifierTest, RunPackReg) {
   CheckVerifiesHLSL(L"packreg.hlsl");
 }
 
+TEST_F(VerifierTest, RunRayTracings) {
+  CheckVerifiesHLSL(L"raytracings.hlsl");
+}
+
 TEST_F(VerifierTest, RunScalarAssignments) {
   CheckVerifiesHLSL(L"scalar-assignments.hlsl");
 }

+ 1 - 0
utils/hct/VerifierHelper.py

@@ -58,6 +58,7 @@ VerifierTests = {
     'RunMoreOperators': "more-operators.hlsl",
     'RunObjectOperators': "object-operators.hlsl",
     'RunPackReg': "packreg.hlsl",
+    'RunRayTracings': "raytracing.hlsl",
     'RunScalarAssignments': "scalar-assignments.hlsl",
     'RunScalarOperatorsAssign': "scalar-operators-assign.hlsl",
     'RunScalarOperatorsAssignExactPrecision': "scalar-operators-assign.hlsl",