Browse Source

Allow using modifier as field names for fxc compatibility (#1419)

Lei Zhang 7 năm trước cách đây
mục cha
commit
d43410e0bc

+ 11 - 4
tools/clang/lib/Parse/ParseExpr.cpp

@@ -1713,13 +1713,20 @@ Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
       }
 
       // HLSL Change Starts
-      // 'sample' is a keyword when used as an interpolation modifier, but it is
-      // also a built-in field of some types. By the time we're considering a
+      // 'sample' and others are keywords when used as modifiers, but they are
+      // also built-in field of some types. By the time we're considering a
       // field access, update the token if necessary to reflect this.
       if (getLangOpts().HLSL) {
-        if (Tok.is(tok::kw_sample)) {
+        switch (auto tk = Tok.getKind()) {
+        case tok::kw_center:
+        case tok::kw_globallycoherent:
+        case tok::kw_precise:
+        case tok::kw_sample:
           Tok.setKind(tok::identifier);
-          Tok.setIdentifierInfo(PP.getIdentifierInfo(StringRef("sample")));
+          Tok.setIdentifierInfo(PP.getIdentifierInfo(getKeywordSpelling(tk)));
+          break;
+        default:
+          break;
         }
       }
       // HLSL Change Ends

+ 19 - 3
tools/clang/test/CodeGenHLSL/quick-test/sample_kwd.hlsl

@@ -1,19 +1,35 @@
-// RUN: %dxc -T ps_6_0 -Od -E main %s | FileCheck %s 
+// RUN: %dxc -T ps_6_0 -Od -E main %s | FileCheck %s
 
 // CHECK: %precise = alloca float, align 4
 // CHECK: %globallycoherent = alloca float, align 4
 // CHECK: %sample = alloca float, align 4
 
+// CHECK: call %dx.types.ResRet.f32 @dx.op.bufferLoad.f32(i32 68, %dx.types.Handle %MyBuffer_UAV_structbuf, i32 0, i32 0)
+// CHECK: call %dx.types.ResRet.f32 @dx.op.bufferLoad.f32(i32 68, %dx.types.Handle %MyBuffer_UAV_structbuf, i32 0, i32 16)
+// CHECK: call %dx.types.ResRet.f32 @dx.op.bufferLoad.f32(i32 68, %dx.types.Handle %MyBuffer_UAV_structbuf, i32 0, i32 32)
+// CHECK: call %dx.types.ResRet.f32 @dx.op.bufferLoad.f32(i32 68, %dx.types.Handle %MyBuffer_UAV_structbuf, i32 0, i32 48)
+
 // Make sure 'precise', 'globallycoherent' and 'sample' can be used as identifiers (FXC back-compat)
 float3 foo(float3 sample) {
     return sample;
 }
 
+struct S {
+  float4 center;
+  float4 precise;
+  float4 sample;
+  float4 globallycoherent;
+};
+
+RWStructuredBuffer<S> MyBuffer;
+
 float3 main(float4 input : SV_POSITION) : SV_TARGET
 {
     float precise = 1.0f;
     float globallycoherent = 1.0f;
     float sample = 1.0f;
-    
-    return foo(float3(precise, globallycoherent, sample));
+
+    return foo(float3(precise, globallycoherent, sample)) +
+           MyBuffer[0].center + MyBuffer[0].precise +
+           MyBuffer[0].sample + MyBuffer[0].globallycoherent;
 }