Skip to content

[HLSL][RootSiganture] Add parsing of new number params in StaticSampler #140291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 29, 2025

Conversation

inbelic
Copy link
Contributor

@inbelic inbelic commented May 16, 2025

  • defines in-memory reprsentation of maxAnisotropy, minLOD and maxLOD
  • integrates parsing of these number parameters with their respective, parseUInt and parseFloat respectively
  • adds basic unit tests to demonstrate setting functionality

Part 3 of #126574

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" HLSL HLSL Language Support labels May 16, 2025
@llvmbot
Copy link
Member

llvmbot commented May 16, 2025

@llvm/pr-subscribers-hlsl

@llvm/pr-subscribers-clang

Author: Finn Plummer (inbelic)

Changes
  • defines in-memory reprsentation of maxAnisotropy, minLOD and maxLOD
  • integrates parsing of these number parameters with their respective, parseUInt and parseFloat respectively
  • adds basic unit tests to demonstrate setting functionality

Part 3 of #126574


Full diff: https://github.com/llvm/llvm-project/pull/140291.diff

6 Files Affected:

  • (modified) clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def (+3)
  • (modified) clang/include/clang/Parse/ParseHLSLRootSignature.h (+3)
  • (modified) clang/lib/Parse/ParseHLSLRootSignature.cpp (+60)
  • (modified) clang/unittests/Lex/LexHLSLRootSignatureTest.cpp (+1-1)
  • (modified) clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp (+20-2)
  • (modified) llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h (+3)
diff --git a/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def b/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def
index 5d16eaa5b72f6..7ca131349fed4 100644
--- a/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def
+++ b/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def
@@ -102,6 +102,9 @@ KEYWORD(offset)
 
 // StaticSampler Keywords:
 KEYWORD(mipLODBias)
+KEYWORD(maxAnisotropy)
+KEYWORD(minLOD)
+KEYWORD(maxLOD)
 
 // Unbounded Enum:
 UNBOUNDED_ENUM(unbounded, "unbounded")
diff --git a/clang/include/clang/Parse/ParseHLSLRootSignature.h b/clang/include/clang/Parse/ParseHLSLRootSignature.h
index c12b022a030ef..e62d5de948a44 100644
--- a/clang/include/clang/Parse/ParseHLSLRootSignature.h
+++ b/clang/include/clang/Parse/ParseHLSLRootSignature.h
@@ -112,6 +112,9 @@ class RootSignatureParser {
   struct ParsedStaticSamplerParams {
     std::optional<llvm::hlsl::rootsig::Register> Reg;
     std::optional<float> MipLODBias;
+    std::optional<uint32_t> MaxAnisotropy;
+    std::optional<float> MinLOD;
+    std::optional<float> MaxLOD;
   };
   std::optional<ParsedStaticSamplerParams> parseStaticSamplerParams();
 
diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp
index db2e922160062..04fe8d00f25cd 100644
--- a/clang/lib/Parse/ParseHLSLRootSignature.cpp
+++ b/clang/lib/Parse/ParseHLSLRootSignature.cpp
@@ -384,6 +384,15 @@ std::optional<StaticSampler> RootSignatureParser::parseStaticSampler() {
   if (Params->MipLODBias.has_value())
     Sampler.MipLODBias = Params->MipLODBias.value();
 
+  if (Params->MaxAnisotropy.has_value())
+    Sampler.MaxAnisotropy = Params->MaxAnisotropy.value();
+
+  if (Params->MinLOD.has_value())
+    Sampler.MinLOD = Params->MinLOD.value();
+
+  if (Params->MaxLOD.has_value())
+    Sampler.MaxLOD = Params->MaxLOD.value();
+
   if (consumeExpectedToken(TokenKind::pu_r_paren,
                            diag::err_hlsl_unexpected_end_of_params,
                            /*param of=*/TokenKind::kw_StaticSampler))
@@ -686,6 +695,57 @@ RootSignatureParser::parseStaticSamplerParams() {
         return std::nullopt;
       Params.MipLODBias = (float)*MipLODBias;
     }
+
+    // `maxAnisotropy` `=` POS_INT
+    if (tryConsumeExpectedToken(TokenKind::kw_maxAnisotropy)) {
+      if (Params.MaxAnisotropy.has_value()) {
+        getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
+            << CurToken.TokKind;
+        return std::nullopt;
+      }
+
+      if (consumeExpectedToken(TokenKind::pu_equal))
+        return std::nullopt;
+
+      auto MaxAnisotropy = parseUIntParam();
+      if (!MaxAnisotropy.has_value())
+        return std::nullopt;
+      Params.MaxAnisotropy = MaxAnisotropy;
+    }
+
+    // `minLOD` `=` NUMBER
+    if (tryConsumeExpectedToken(TokenKind::kw_minLOD)) {
+      if (Params.MinLOD.has_value()) {
+        getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
+            << CurToken.TokKind;
+        return std::nullopt;
+      }
+
+      if (consumeExpectedToken(TokenKind::pu_equal))
+        return std::nullopt;
+
+      auto MinLOD = parseFloatParam();
+      if (!MinLOD.has_value())
+        return std::nullopt;
+      Params.MinLOD = MinLOD;
+    }
+
+    // `maxLOD` `=` NUMBER
+    if (tryConsumeExpectedToken(TokenKind::kw_maxLOD)) {
+      if (Params.MaxLOD.has_value()) {
+        getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
+            << CurToken.TokKind;
+        return std::nullopt;
+      }
+
+      if (consumeExpectedToken(TokenKind::pu_equal))
+        return std::nullopt;
+
+      auto MaxLOD = parseFloatParam();
+      if (!MaxLOD.has_value())
+        return std::nullopt;
+      Params.MaxLOD = MaxLOD;
+    }
   } while (tryConsumeExpectedToken(TokenKind::pu_comma));
 
   return Params;
diff --git a/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp b/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
index b610b8f10f8da..575a97e75a05d 100644
--- a/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
+++ b/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
@@ -136,7 +136,7 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
     space visibility flags
     numDescriptors offset
 
-    mipLODBias
+    mipLODBias maxAnisotropy minLOD maxLOD
 
     unbounded
     DESCRIPTOR_RANGE_OFFSET_APPEND
diff --git a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
index 539232e0bf2c2..94e0464ddf03b 100644
--- a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
+++ b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
@@ -225,7 +225,11 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseDTClausesTest) {
 
 TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
   const llvm::StringLiteral Source = R"cc(
-    StaticSampler(s0, mipLODBias = 0)
+    StaticSampler(s0),
+    StaticSampler(s0, maxAnisotropy = 3,
+      minLOD = 4.2f, mipLODBias = 0.23e+3,
+      maxLOD = 9000,
+    )
   )cc";
 
   TrivialModuleLoader ModLoader;
@@ -241,13 +245,27 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
 
   ASSERT_FALSE(Parser.parse());
 
-  ASSERT_EQ(Elements.size(), 1u);
+  ASSERT_EQ(Elements.size(), 2u);
 
+  // Check default values are as expected
   RootElement Elem = Elements[0];
   ASSERT_TRUE(std::holds_alternative<StaticSampler>(Elem));
   ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.ViewType, RegisterType::SReg);
   ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.Number, 0u);
   ASSERT_EQ(std::get<StaticSampler>(Elem).MipLODBias, 0.f);
+  ASSERT_EQ(std::get<StaticSampler>(Elem).MaxAnisotropy, 16u);
+  ASSERT_EQ(std::get<StaticSampler>(Elem).MinLOD, 0.f);
+  ASSERT_EQ(std::get<StaticSampler>(Elem).MaxLOD, 3.402823466e+38f);
+
+  // Check values can be set as expected
+  Elem = Elements[1];
+  ASSERT_TRUE(std::holds_alternative<StaticSampler>(Elem));
+  ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.ViewType, RegisterType::SReg);
+  ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.Number, 0u);
+  ASSERT_EQ(std::get<StaticSampler>(Elem).MipLODBias, 230.f);
+  ASSERT_EQ(std::get<StaticSampler>(Elem).MaxAnisotropy, 3u);
+  ASSERT_EQ(std::get<StaticSampler>(Elem).MinLOD, 4.2f);
+  ASSERT_EQ(std::get<StaticSampler>(Elem).MaxLOD, 9000.f);
 
   ASSERT_TRUE(Consumer->isSatisfied());
 }
diff --git a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
index 6b4da48a302bc..8ee58b87f5e49 100644
--- a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
+++ b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
@@ -161,6 +161,9 @@ struct DescriptorTableClause {
 struct StaticSampler {
   Register Reg;
   float MipLODBias = 0.f;
+  uint32_t MaxAnisotropy = 16;
+  float MinLOD = 0.f;
+  float MaxLOD = 3.402823466e+38f; // FLT_MAX
 };
 
 /// Models RootElement : RootFlags | RootConstants | RootParam

@inbelic inbelic linked an issue May 16, 2025 that may be closed by this pull request
4 tasks
@inbelic inbelic changed the base branch from users/inbelic/pr-140181 to main May 29, 2025 18:02
@inbelic inbelic force-pushed the inbelic/rs-numbers-sampler branch from 30f8bdd to c7b1673 Compare May 29, 2025 18:03
@inbelic inbelic merged commit c8eb094 into llvm:main May 29, 2025
9 of 11 checks passed
svkeerthy pushed a commit that referenced this pull request May 29, 2025
…er (#140291)

- defines in-memory reprsentation of `maxAnisotropy`, `minLOD` and
`maxLOD`
- integrates parsing of these number parameters with their respective,
`parseUInt` and `parseFloat` respectively
 - adds basic unit tests to demonstrate setting functionality

Part 3 of #126574
google-yfyang pushed a commit to google-yfyang/llvm-project that referenced this pull request May 29, 2025
…er (llvm#140291)

- defines in-memory reprsentation of `maxAnisotropy`, `minLOD` and
`maxLOD`
- integrates parsing of these number parameters with their respective,
`parseUInt` and `parseFloat` respectively
 - adds basic unit tests to demonstrate setting functionality

Part 3 of llvm#126574
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category HLSL HLSL Language Support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[HLSL] Implement Root Signature Parsing of Static Samplers
4 participants