Skip to content

[HLSL][RootSignature] Add parsing of remaining enums to StaticSampler #140305

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

Open
wants to merge 4 commits into
base: users/inbelic/pr-140294
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
#ifndef TEXTURE_ADDRESS_MODE_ENUM
#define TEXTURE_ADDRESS_MODE_ENUM(NAME, LIT) ENUM(NAME, LIT)
#endif
#ifndef COMPARISON_FUNC_ENUM
#define COMPARISON_FUNC_ENUM(NAME, LIT) ENUM(NAME, LIT)
#endif
#ifndef STATIC_BORDER_COLOR_ENUM
#define STATIC_BORDER_COLOR_ENUM(NAME, LIT) ENUM(NAME, LIT)
#endif

// General Tokens:
TOK(invalid, "invalid identifier")
Expand Down Expand Up @@ -113,6 +119,8 @@ KEYWORD(addressV)
KEYWORD(addressW)
KEYWORD(mipLODBias)
KEYWORD(maxAnisotropy)
KEYWORD(comparisonFunc)
KEYWORD(borderColor)
KEYWORD(minLOD)
KEYWORD(maxLOD)

Expand Down Expand Up @@ -203,6 +211,25 @@ TEXTURE_ADDRESS_MODE_ENUM(Clamp, "TEXTURE_ADDRESS_CLAMP")
TEXTURE_ADDRESS_MODE_ENUM(Border, "TEXTURE_ADDRESS_BORDER")
TEXTURE_ADDRESS_MODE_ENUM(MirrorOnce, "TEXTURE_ADDRESS_MIRRORONCE")

// Comparison Func Enums:
COMPARISON_FUNC_ENUM(Never, "COMPARISON_NEVER")
COMPARISON_FUNC_ENUM(Less, "COMPARISON_LESS")
COMPARISON_FUNC_ENUM(Equal, "COMPARISON_EQUAL")
COMPARISON_FUNC_ENUM(LessEqual, "COMPARISON_LESS_EQUAL")
COMPARISON_FUNC_ENUM(Greater, "COMPARISON_GREATER")
COMPARISON_FUNC_ENUM(NotEqual, "COMPARISON_NOT_EQUAL")
COMPARISON_FUNC_ENUM(GreaterEqual, "COMPARISON_GREATER_EQUAL")
COMPARISON_FUNC_ENUM(Always, "COMPARISON_ALWAYS")

// Static Border Color Enums:
STATIC_BORDER_COLOR_ENUM(TransparentBlack, "STATIC_BORDER_COLOR_TRANSPARENT_BLACK")
STATIC_BORDER_COLOR_ENUM(OpaqueBlack, "STATIC_BORDER_COLOR_OPAQUE_BLACK")
STATIC_BORDER_COLOR_ENUM(OpaqueWhite, "STATIC_BORDER_COLOR_OPAQUE_WHITE")
STATIC_BORDER_COLOR_ENUM(OpaqueBlackUint, "STATIC_BORDER_COLOR_OPAQUE_BLACK_UINT")
STATIC_BORDER_COLOR_ENUM(OpaqueWhiteUint, "STATIC_BORDER_COLOR_OPAQUE_WHITE_UINT")

#undef STATIC_BORDER_COLOR_ENUM
#undef COMPARISON_FUNC_ENUM
#undef TEXTURE_ADDRESS_MODE_ENUM
#undef FILTER_ENUM
#undef SHADER_VISIBILITY_ENUM
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Parse/ParseHLSLRootSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class RootSignatureParser {
std::optional<llvm::hlsl::rootsig::TextureAddressMode> AddressW;
std::optional<float> MipLODBias;
std::optional<uint32_t> MaxAnisotropy;
std::optional<llvm::hlsl::rootsig::ComparisonFunc> ComparisonFunc;
std::optional<llvm::hlsl::rootsig::StaticBorderColor> BorderColor;
std::optional<float> MinLOD;
std::optional<float> MaxLOD;
};
Expand All @@ -132,6 +134,9 @@ class RootSignatureParser {
std::optional<llvm::hlsl::rootsig::Filter> parseFilter();
std::optional<llvm::hlsl::rootsig::TextureAddressMode>
parseTextureAddressMode();
std::optional<llvm::hlsl::rootsig::ComparisonFunc> parseComparisonFunc();
std::optional<llvm::hlsl::rootsig::StaticBorderColor>
parseStaticBorderColor();
std::optional<llvm::hlsl::rootsig::RootDescriptorFlags>
parseRootDescriptorFlags();
std::optional<llvm::hlsl::rootsig::DescriptorRangeFlags>
Expand Down
92 changes: 92 additions & 0 deletions clang/lib/Parse/ParseHLSLRootSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@ std::optional<StaticSampler> RootSignatureParser::parseStaticSampler() {
if (Params->MaxAnisotropy.has_value())
Sampler.MaxAnisotropy = Params->MaxAnisotropy.value();

if (Params->ComparisonFunc.has_value())
Sampler.ComparisonFunc = Params->ComparisonFunc.value();

if (Params->BorderColor.has_value())
Sampler.BorderColor = Params->BorderColor.value();

if (Params->MinLOD.has_value())
Sampler.MinLOD = Params->MinLOD.value();

Expand Down Expand Up @@ -793,6 +799,40 @@ RootSignatureParser::parseStaticSamplerParams() {
Params.MaxAnisotropy = MaxAnisotropy;
}

// `comparisonFunc` `=` COMPARISON_FUNC
if (tryConsumeExpectedToken(TokenKind::kw_comparisonFunc)) {
if (Params.ComparisonFunc.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 ComparisonFunc = parseComparisonFunc();
if (!ComparisonFunc.has_value())
return std::nullopt;
Params.ComparisonFunc = ComparisonFunc;
}

// `borderColor` `=` STATIC_BORDER_COLOR
if (tryConsumeExpectedToken(TokenKind::kw_borderColor)) {
if (Params.BorderColor.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 BorderColor = parseStaticBorderColor();
if (!BorderColor.has_value())
return std::nullopt;
Params.BorderColor = BorderColor;
}

// `minLOD` `=` NUMBER
if (tryConsumeExpectedToken(TokenKind::kw_minLOD)) {
if (Params.MinLOD.has_value()) {
Expand Down Expand Up @@ -980,6 +1020,58 @@ RootSignatureParser::parseTextureAddressMode() {
return std::nullopt;
}

std::optional<llvm::hlsl::rootsig::ComparisonFunc>
RootSignatureParser::parseComparisonFunc() {
assert(CurToken.TokKind == TokenKind::pu_equal &&
"Expects to only be invoked starting at given keyword");

TokenKind Expected[] = {
#define COMPARISON_FUNC_ENUM(NAME, LIT) TokenKind::en_##NAME,
#include "clang/Lex/HLSLRootSignatureTokenKinds.def"
};

if (!tryConsumeExpectedToken(Expected))
return std::nullopt;

switch (CurToken.TokKind) {
#define COMPARISON_FUNC_ENUM(NAME, LIT) \
case TokenKind::en_##NAME: \
return ComparisonFunc::NAME; \
break;
#include "clang/Lex/HLSLRootSignatureTokenKinds.def"
default:
llvm_unreachable("Switch for consumed enum token was not provided");
}

return std::nullopt;
}

std::optional<llvm::hlsl::rootsig::StaticBorderColor>
RootSignatureParser::parseStaticBorderColor() {
assert(CurToken.TokKind == TokenKind::pu_equal &&
"Expects to only be invoked starting at given keyword");

TokenKind Expected[] = {
#define STATIC_BORDER_COLOR_ENUM(NAME, LIT) TokenKind::en_##NAME,
#include "clang/Lex/HLSLRootSignatureTokenKinds.def"
};

if (!tryConsumeExpectedToken(Expected))
return std::nullopt;

switch (CurToken.TokKind) {
#define STATIC_BORDER_COLOR_ENUM(NAME, LIT) \
case TokenKind::en_##NAME: \
return StaticBorderColor::NAME; \
break;
#include "clang/Lex/HLSLRootSignatureTokenKinds.def"
default:
llvm_unreachable("Switch for consumed enum token was not provided");
}

return std::nullopt;
}

std::optional<llvm::hlsl::rootsig::RootDescriptorFlags>
RootSignatureParser::parseRootDescriptorFlags() {
assert(CurToken.TokKind == TokenKind::pu_equal &&
Expand Down
19 changes: 18 additions & 1 deletion clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
numDescriptors offset

filter addressU addressV addressW
mipLODBias maxAnisotropy minLOD maxLOD
mipLODBias maxAnisotropy
comparisonFunc borderColor
minLOD maxLOD

unbounded
DESCRIPTOR_RANGE_OFFSET_APPEND
Expand Down Expand Up @@ -212,6 +214,21 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
TEXTURE_ADDRESS_CLAMP
TEXTURE_ADDRESS_BORDER
TEXTURE_ADDRESS_MIRRORONCE

comparison_never
comparison_less
comparison_equal
comparison_less_equal
comparison_greater
comparison_not_equal
comparison_greater_equal
comparison_always

STATIC_BORDER_COLOR_TRANSPARENT_BLACK
STATIC_BORDER_COLOR_OPAQUE_BLACK
STATIC_BORDER_COLOR_OPAQUE_WHITE
STATIC_BORDER_COLOR_OPAQUE_BLACK_UINT
STATIC_BORDER_COLOR_OPAQUE_WHITE_UINT
)cc";
auto TokLoc = SourceLocation();
hlsl::RootSignatureLexer Lexer(Source, TokLoc);
Expand Down
12 changes: 11 additions & 1 deletion clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
addressW = TEXTURE_ADDRESS_CLAMP,
addressV = TEXTURE_ADDRESS_BORDER,
filter = FILTER_MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT,
maxLOD = 9000, addressU = TEXTURE_ADDRESS_MIRROR
maxLOD = 9000, addressU = TEXTURE_ADDRESS_MIRROR,
comparisonFunc = COMPARISON_NOT_EQUAL,
borderColor = STATIC_BORDER_COLOR_OPAQUE_BLACK_UINT
)
)cc";

Expand Down Expand Up @@ -261,6 +263,10 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressW, TextureAddressMode::Wrap);
ASSERT_EQ(std::get<StaticSampler>(Elem).MipLODBias, 0.f);
ASSERT_EQ(std::get<StaticSampler>(Elem).MaxAnisotropy, 16u);
ASSERT_EQ(std::get<StaticSampler>(Elem).ComparisonFunc,
ComparisonFunc::LessEqual);
ASSERT_EQ(std::get<StaticSampler>(Elem).BorderColor,
StaticBorderColor::OpaqueWhite);
ASSERT_EQ(std::get<StaticSampler>(Elem).MinLOD, 0.f);
ASSERT_EQ(std::get<StaticSampler>(Elem).MaxLOD, 3.402823466e+38f);

Expand All @@ -276,6 +282,10 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressW, TextureAddressMode::Clamp);
ASSERT_EQ(std::get<StaticSampler>(Elem).MipLODBias, 230.f);
ASSERT_EQ(std::get<StaticSampler>(Elem).MaxAnisotropy, 3u);
ASSERT_EQ(std::get<StaticSampler>(Elem).ComparisonFunc,
ComparisonFunc::NotEqual);
ASSERT_EQ(std::get<StaticSampler>(Elem).BorderColor,
StaticBorderColor::OpaqueBlackUint);
ASSERT_EQ(std::get<StaticSampler>(Elem).MinLOD, 4.2f);
ASSERT_EQ(std::get<StaticSampler>(Elem).MaxLOD, 9000.f);

Expand Down
21 changes: 21 additions & 0 deletions llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ enum class TextureAddressMode {
MirrorOnce = 5
};

enum class ComparisonFunc : unsigned {
Never = 1,
Less = 2,
Equal = 3,
LessEqual = 4,
Greater = 5,
NotEqual = 6,
GreaterEqual = 7,
Always = 8
};

enum class StaticBorderColor {
TransparentBlack = 0,
OpaqueBlack = 1,
OpaqueWhite = 2,
OpaqueBlackUint = 3,
OpaqueWhiteUint = 4
};

// Definitions of the in-memory data layout structures

// Models the different registers: bReg | tReg | uReg | sReg
Expand Down Expand Up @@ -213,6 +232,8 @@ struct StaticSampler {
TextureAddressMode AddressW = TextureAddressMode::Wrap;
float MipLODBias = 0.f;
uint32_t MaxAnisotropy = 16;
ComparisonFunc ComparisonFunc = ComparisonFunc::LessEqual;
StaticBorderColor BorderColor = StaticBorderColor::OpaqueWhite;
float MinLOD = 0.f;
float MaxLOD = 3.402823466e+38f; // FLT_MAX
};
Expand Down