Skip to content

Commit e0b81b0

Browse files
inbelicsvkeerthy
authored andcommitted
[HLSL][RootSiganture] Add parsing of new number params in StaticSampler (#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
1 parent ce0a024 commit e0b81b0

File tree

6 files changed

+90
-3
lines changed

6 files changed

+90
-3
lines changed

clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ KEYWORD(offset)
102102

103103
// StaticSampler Keywords:
104104
KEYWORD(mipLODBias)
105+
KEYWORD(maxAnisotropy)
106+
KEYWORD(minLOD)
107+
KEYWORD(maxLOD)
105108

106109
// Unbounded Enum:
107110
UNBOUNDED_ENUM(unbounded, "unbounded")

clang/include/clang/Parse/ParseHLSLRootSignature.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ class RootSignatureParser {
112112
struct ParsedStaticSamplerParams {
113113
std::optional<llvm::hlsl::rootsig::Register> Reg;
114114
std::optional<float> MipLODBias;
115+
std::optional<uint32_t> MaxAnisotropy;
116+
std::optional<float> MinLOD;
117+
std::optional<float> MaxLOD;
115118
};
116119
std::optional<ParsedStaticSamplerParams> parseStaticSamplerParams();
117120

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,15 @@ std::optional<StaticSampler> RootSignatureParser::parseStaticSampler() {
380380
if (Params->MipLODBias.has_value())
381381
Sampler.MipLODBias = Params->MipLODBias.value();
382382

383+
if (Params->MaxAnisotropy.has_value())
384+
Sampler.MaxAnisotropy = Params->MaxAnisotropy.value();
385+
386+
if (Params->MinLOD.has_value())
387+
Sampler.MinLOD = Params->MinLOD.value();
388+
389+
if (Params->MaxLOD.has_value())
390+
Sampler.MaxLOD = Params->MaxLOD.value();
391+
383392
if (consumeExpectedToken(TokenKind::pu_r_paren,
384393
diag::err_hlsl_unexpected_end_of_params,
385394
/*param of=*/TokenKind::kw_StaticSampler))
@@ -682,6 +691,57 @@ RootSignatureParser::parseStaticSamplerParams() {
682691
return std::nullopt;
683692
Params.MipLODBias = MipLODBias;
684693
}
694+
695+
// `maxAnisotropy` `=` POS_INT
696+
if (tryConsumeExpectedToken(TokenKind::kw_maxAnisotropy)) {
697+
if (Params.MaxAnisotropy.has_value()) {
698+
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
699+
<< CurToken.TokKind;
700+
return std::nullopt;
701+
}
702+
703+
if (consumeExpectedToken(TokenKind::pu_equal))
704+
return std::nullopt;
705+
706+
auto MaxAnisotropy = parseUIntParam();
707+
if (!MaxAnisotropy.has_value())
708+
return std::nullopt;
709+
Params.MaxAnisotropy = MaxAnisotropy;
710+
}
711+
712+
// `minLOD` `=` NUMBER
713+
if (tryConsumeExpectedToken(TokenKind::kw_minLOD)) {
714+
if (Params.MinLOD.has_value()) {
715+
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
716+
<< CurToken.TokKind;
717+
return std::nullopt;
718+
}
719+
720+
if (consumeExpectedToken(TokenKind::pu_equal))
721+
return std::nullopt;
722+
723+
auto MinLOD = parseFloatParam();
724+
if (!MinLOD.has_value())
725+
return std::nullopt;
726+
Params.MinLOD = MinLOD;
727+
}
728+
729+
// `maxLOD` `=` NUMBER
730+
if (tryConsumeExpectedToken(TokenKind::kw_maxLOD)) {
731+
if (Params.MaxLOD.has_value()) {
732+
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
733+
<< CurToken.TokKind;
734+
return std::nullopt;
735+
}
736+
737+
if (consumeExpectedToken(TokenKind::pu_equal))
738+
return std::nullopt;
739+
740+
auto MaxLOD = parseFloatParam();
741+
if (!MaxLOD.has_value())
742+
return std::nullopt;
743+
Params.MaxLOD = MaxLOD;
744+
}
685745
} while (tryConsumeExpectedToken(TokenKind::pu_comma));
686746

687747
return Params;

clang/unittests/Lex/LexHLSLRootSignatureTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
136136
space visibility flags
137137
numDescriptors offset
138138
139-
mipLODBias
139+
mipLODBias maxAnisotropy minLOD maxLOD
140140
141141
unbounded
142142
DESCRIPTOR_RANGE_OFFSET_APPEND

clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,11 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseDTClausesTest) {
225225

226226
TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
227227
const llvm::StringLiteral Source = R"cc(
228-
StaticSampler(s0, mipLODBias = 0)
228+
StaticSampler(s0),
229+
StaticSampler(s0, maxAnisotropy = 3,
230+
minLOD = 4.2f, mipLODBias = 0.23e+3,
231+
maxLOD = 9000,
232+
)
229233
)cc";
230234

231235
TrivialModuleLoader ModLoader;
@@ -241,13 +245,27 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
241245

242246
ASSERT_FALSE(Parser.parse());
243247

244-
ASSERT_EQ(Elements.size(), 1u);
248+
ASSERT_EQ(Elements.size(), 2u);
245249

250+
// Check default values are as expected
246251
RootElement Elem = Elements[0];
247252
ASSERT_TRUE(std::holds_alternative<StaticSampler>(Elem));
248253
ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.ViewType, RegisterType::SReg);
249254
ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.Number, 0u);
250255
ASSERT_FLOAT_EQ(std::get<StaticSampler>(Elem).MipLODBias, 0.f);
256+
ASSERT_EQ(std::get<StaticSampler>(Elem).MaxAnisotropy, 16u);
257+
ASSERT_FLOAT_EQ(std::get<StaticSampler>(Elem).MinLOD, 0.f);
258+
ASSERT_FLOAT_EQ(std::get<StaticSampler>(Elem).MaxLOD, 3.402823466e+38f);
259+
260+
// Check values can be set as expected
261+
Elem = Elements[1];
262+
ASSERT_TRUE(std::holds_alternative<StaticSampler>(Elem));
263+
ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.ViewType, RegisterType::SReg);
264+
ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.Number, 0u);
265+
ASSERT_FLOAT_EQ(std::get<StaticSampler>(Elem).MipLODBias, 230.f);
266+
ASSERT_EQ(std::get<StaticSampler>(Elem).MaxAnisotropy, 3u);
267+
ASSERT_FLOAT_EQ(std::get<StaticSampler>(Elem).MinLOD, 4.2f);
268+
ASSERT_FLOAT_EQ(std::get<StaticSampler>(Elem).MaxLOD, 9000.f);
251269

252270
ASSERT_TRUE(Consumer->isSatisfied());
253271
}

llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ raw_ostream &operator<<(raw_ostream &OS, const DescriptorTableClause &Clause);
158158
struct StaticSampler {
159159
Register Reg;
160160
float MipLODBias = 0.f;
161+
uint32_t MaxAnisotropy = 16;
162+
float MinLOD = 0.f;
163+
float MaxLOD = std::numeric_limits<float>::max();
161164
};
162165

163166
/// Models RootElement : RootFlags | RootConstants | RootParam

0 commit comments

Comments
 (0)