Skip to content

Commit bc393fd

Browse files
committed
adding flags to static sampler parser
1 parent b73bf24 commit bc393fd

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
#ifndef STATIC_BORDER_COLOR_ENUM
6666
#define STATIC_BORDER_COLOR_ENUM(NAME, LIT) ENUM(NAME, LIT)
6767
#endif
68+
#ifndef STATIC_SAMPLER_FLAG_ENUM
69+
#define STATIC_SAMPLER_FLAG_ENUM(NAME, LIT) ENUM(NAME, LIT)
70+
#endif
6871

6972
// General Tokens:
7073
TOK(invalid, "invalid identifier")
@@ -228,6 +231,10 @@ STATIC_BORDER_COLOR_ENUM(OpaqueWhite, "STATIC_BORDER_COLOR_OPAQUE_WHITE")
228231
STATIC_BORDER_COLOR_ENUM(OpaqueBlackUint, "STATIC_BORDER_COLOR_OPAQUE_BLACK_UINT")
229232
STATIC_BORDER_COLOR_ENUM(OpaqueWhiteUint, "STATIC_BORDER_COLOR_OPAQUE_WHITE_UINT")
230233

234+
// Root Descriptor Flag Enums:
235+
STATIC_SAMPLER_FLAG_ENUM(UintBorderColor, "UINT_BORDER_COLOR")
236+
STATIC_SAMPLER_FLAG_ENUM(NonNormalizedCoordinates, "NON_NORMALIZED_COORDINATES")
237+
231238
#undef STATIC_BORDER_COLOR_ENUM
232239
#undef COMPARISON_FUNC_ENUM
233240
#undef TEXTURE_ADDRESS_MODE_ENUM
@@ -237,6 +244,7 @@ STATIC_BORDER_COLOR_ENUM(OpaqueWhiteUint, "STATIC_BORDER_COLOR_OPAQUE_WHITE_UINT
237244
#undef DESCRIPTOR_RANGE_FLAG_ENUM_OFF
238245
#undef DESCRIPTOR_RANGE_FLAG_ENUM_ON
239246
#undef ROOT_DESCRIPTOR_FLAG_ENUM
247+
#undef STATIC_SAMPLER_FLAG_ENUM
240248
#undef ROOT_FLAG_ENUM
241249
#undef DESCRIPTOR_RANGE_OFFSET_ENUM
242250
#undef UNBOUNDED_ENUM

clang/include/clang/Parse/ParseHLSLRootSignature.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class RootSignatureParser {
130130
std::optional<float> MaxLOD;
131131
std::optional<uint32_t> Space;
132132
std::optional<llvm::dxbc::ShaderVisibility> Visibility;
133+
std::optional<llvm::dxbc::StaticSamplerFlags> Flags;
133134
};
134135
std::optional<ParsedStaticSamplerParams> parseStaticSamplerParams();
135136

@@ -153,6 +154,8 @@ class RootSignatureParser {
153154
parseRootDescriptorFlags(RootSignatureToken::Kind Context);
154155
std::optional<llvm::dxbc::DescriptorRangeFlags>
155156
parseDescriptorRangeFlags(RootSignatureToken::Kind Context);
157+
std::optional<llvm::dxbc::StaticSamplerFlags>
158+
parseStaticSamplerFlags(RootSignatureToken::Kind Context);
156159

157160
/// Use NumericLiteralParser to convert CurToken.NumSpelling into a unsigned
158161
/// 32-bit integer

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ std::optional<StaticSampler> RootSignatureParser::parseStaticSampler() {
485485
if (Params->Visibility.has_value())
486486
Sampler.Visibility = Params->Visibility.value();
487487

488+
if (Params->Flags.has_value())
489+
Sampler.Flags = Params->Flags.value();
490+
488491
return Sampler;
489492
}
490493

@@ -926,6 +929,20 @@ RootSignatureParser::parseStaticSamplerParams() {
926929
if (!Visibility.has_value())
927930
return std::nullopt;
928931
Params.Visibility = Visibility;
932+
} else if (tryConsumeExpectedToken(TokenKind::kw_flags)) {
933+
// `flags` `=` UINT_BORDER_COLOR
934+
if (Params.Flags.has_value()) {
935+
reportDiag(diag::err_hlsl_rootsig_repeat_param) << CurToken.TokKind;
936+
return std::nullopt;
937+
}
938+
939+
if (consumeExpectedToken(TokenKind::pu_equal))
940+
return std::nullopt;
941+
942+
auto Flags = parseStaticSamplerFlags(TokenKind::kw_flags);
943+
if (!Flags.has_value())
944+
return std::nullopt;
945+
Params.Flags = Flags;
929946
} else {
930947
consumeNextToken(); // let diagnostic be at the start of invalid token
931948
reportDiag(diag::err_hlsl_invalid_token)
@@ -1255,6 +1272,50 @@ RootSignatureParser::parseDescriptorRangeFlags(TokenKind Context) {
12551272
return Flags;
12561273
}
12571274

1275+
std::optional<llvm::dxbc::StaticSamplerFlags>
1276+
RootSignatureParser::parseStaticSamplerFlags(TokenKind Context) {
1277+
assert(CurToken.TokKind == TokenKind::pu_equal &&
1278+
"Expects to only be invoked starting at given keyword");
1279+
1280+
// Handle the edge-case of '0' to specify no flags set
1281+
if (tryConsumeExpectedToken(TokenKind::int_literal)) {
1282+
if (!verifyZeroFlag()) {
1283+
reportDiag(diag::err_hlsl_rootsig_non_zero_flag);
1284+
return std::nullopt;
1285+
}
1286+
return llvm::dxbc::StaticSamplerFlags::None;
1287+
}
1288+
1289+
TokenKind Expected[] = {
1290+
#define STATIC_SAMPLER_FLAG_ENUM(NAME, LIT) TokenKind::en_##NAME,
1291+
#include "clang/Lex/HLSLRootSignatureTokenKinds.def"
1292+
};
1293+
1294+
std::optional<llvm::dxbc::StaticSamplerFlags> Flags;
1295+
1296+
do {
1297+
if (tryConsumeExpectedToken(Expected)) {
1298+
switch (CurToken.TokKind) {
1299+
#define STATIC_SAMPLER_FLAG_ENUM(NAME, LIT) \
1300+
case TokenKind::en_##NAME: \
1301+
Flags = maybeOrFlag<llvm::dxbc::StaticSamplerFlags>( \
1302+
Flags, llvm::dxbc::StaticSamplerFlags::NAME); \
1303+
break;
1304+
#include "clang/Lex/HLSLRootSignatureTokenKinds.def"
1305+
default:
1306+
llvm_unreachable("Switch for consumed enum token was not provided");
1307+
}
1308+
} else {
1309+
consumeNextToken(); // consume token to point at invalid token
1310+
reportDiag(diag::err_hlsl_invalid_token)
1311+
<< /*value=*/1 << /*value of*/ Context;
1312+
return std::nullopt;
1313+
}
1314+
} while (tryConsumeExpectedToken(TokenKind::pu_or));
1315+
1316+
return Flags;
1317+
}
1318+
12581319
std::optional<uint32_t> RootSignatureParser::handleUIntLiteral() {
12591320
// Parse the numeric value and do semantic checks on its specification
12601321
clang::NumericLiteralParser Literal(

clang/unittests/Lex/LexHLSLRootSignatureTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
226226
STATIC_BORDER_COLOR_OPAQUE_WHITE
227227
STATIC_BORDER_COLOR_OPAQUE_BLACK_UINT
228228
STATIC_BORDER_COLOR_OPAQUE_WHITE_UINT
229+
230+
UINT_BORDER_COLOR
231+
NON_NORMALIZED_COORDINATES
229232
)cc";
230233
hlsl::RootSignatureLexer Lexer(Source);
231234

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ struct StaticSampler {
131131
float MaxLOD = std::numeric_limits<float>::max();
132132
uint32_t Space = 0;
133133
dxbc::ShaderVisibility Visibility = dxbc::ShaderVisibility::All;
134+
dxbc::StaticSamplerFlags Flags = dxbc::StaticSamplerFlags::None;
134135
};
135136

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

llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ raw_ostream &operator<<(raw_ostream &OS, const StaticSampler &Sampler) {
172172
<< ", borderColor = " << Sampler.BorderColor
173173
<< ", minLOD = " << Sampler.MinLOD << ", maxLOD = " << Sampler.MaxLOD
174174
<< ", space = " << Sampler.Space << ", visibility = " << Sampler.Visibility
175-
<< ")";
175+
<< ", flags = " << Sampler.Flags << ")";
176176
return OS;
177177
}
178178

0 commit comments

Comments
 (0)