Skip to content

[HLSL][RootSignature] Add parsing of filter enum for StaticSampler #140294

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 3 commits into from
May 29, 2025
Merged
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
45 changes: 44 additions & 1 deletion clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
#ifndef SHADER_VISIBILITY_ENUM
#define SHADER_VISIBILITY_ENUM(NAME, LIT) ENUM(NAME, LIT)
#endif
#ifndef FILTER_ENUM
#define FILTER_ENUM(NAME, LIT) ENUM(NAME, LIT)
#endif
#ifndef TEXTURE_ADDRESS_MODE_ENUM
#define TEXTURE_ADDRESS_MODE_ENUM(NAME, LIT) ENUM(NAME, LIT)
#endif
Expand Down Expand Up @@ -104,10 +107,11 @@ KEYWORD(numDescriptors)
KEYWORD(offset)

// StaticSampler Keywords:
KEYWORD(mipLODBias)
KEYWORD(filter)
KEYWORD(addressU)
KEYWORD(addressV)
KEYWORD(addressW)
KEYWORD(mipLODBias)
KEYWORD(maxAnisotropy)
KEYWORD(minLOD)
KEYWORD(maxLOD)
Comment on lines -107 to 117
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this list sorted? Does the order matter? If not, please just alphabetize it to simplify future additions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted to sort them in the same order as presented in the documentation here.

Ordering doesn't matter. I will create a small separate clean-up pr to go through all the token sub-groups in this file and ensure they are ordered alphabetically. There are inconsistencies of this for the other parameter kinds.

Expand Down Expand Up @@ -154,6 +158,44 @@ SHADER_VISIBILITY_ENUM(Pixel, "SHADER_VISIBILITY_PIXEL")
SHADER_VISIBILITY_ENUM(Amplification, "SHADER_VISIBILITY_AMPLIFICATION")
SHADER_VISIBILITY_ENUM(Mesh, "SHADER_VISIBILITY_MESH")

// Filter Enums:
FILTER_ENUM(MinMagMipPoint, "FILTER_MIN_MAG_MIP_POINT")
FILTER_ENUM(MinMagPointMipLinear, "FILTER_MIN_MAG_POINT_MIP_LINEAR")
FILTER_ENUM(MinPointMagLinearMipPoint, "FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT")
FILTER_ENUM(MinPointMagMipLinear, "FILTER_MIN_POINT_MAG_MIP_LINEAR")
FILTER_ENUM(MinLinearMagMipPoint, "FILTER_MIN_LINEAR_MAG_MIP_POINT")
FILTER_ENUM(MinLinearMagPointMipLinear, "FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR")
FILTER_ENUM(MinMagLinearMipPoint, "FILTER_MIN_MAG_LINEAR_MIP_POINT")
FILTER_ENUM(MinMagMipLinear, "FILTER_MIN_MAG_MIP_LINEAR")
FILTER_ENUM(Anisotropic, "FILTER_ANISOTROPIC")
FILTER_ENUM(ComparisonMinMagMipPoint, "FILTER_COMPARISON_MIN_MAG_MIP_POINT")
FILTER_ENUM(ComparisonMinMagPointMipLinear, "FILTER_COMPARISON_MIN_MAG_POINT_MIP_LINEAR")
FILTER_ENUM(ComparisonMinPointMagLinearMipPoint, "FILTER_COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT")
FILTER_ENUM(ComparisonMinPointMagMipLinear, "FILTER_COMPARISON_MIN_POINT_MAG_MIP_LINEAR")
FILTER_ENUM(ComparisonMinLinearMagMipPoint, "FILTER_COMPARISON_MIN_LINEAR_MAG_MIP_POINT")
FILTER_ENUM(ComparisonMinLinearMagPointMipLinear, "FILTER_COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR")
FILTER_ENUM(ComparisonMinMagLinearMipPoint, "FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT")
FILTER_ENUM(ComparisonMinMagMipLinear, "FILTER_COMPARISON_MIN_MAG_MIP_LINEAR")
FILTER_ENUM(ComparisonAnisotropic, "FILTER_COMPARISON_ANISOTROPIC")
FILTER_ENUM(MinimumMinMagMipPoint, "FILTER_MINIMUM_MIN_MAG_MIP_POINT")
FILTER_ENUM(MinimumMinMagPointMipLinear, "FILTER_MINIMUM_MIN_MAG_POINT_MIP_LINEAR")
FILTER_ENUM(MinimumMinPointMagLinearMipPoint, "FILTER_MINIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT")
FILTER_ENUM(MinimumMinPointMagMipLinear, "FILTER_MINIMUM_MIN_POINT_MAG_MIP_LINEAR")
FILTER_ENUM(MinimumMinLinearMagMipPoint, "FILTER_MINIMUM_MIN_LINEAR_MAG_MIP_POINT")
FILTER_ENUM(MinimumMinLinearMagPointMipLinear, "FILTER_MINIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR")
FILTER_ENUM(MinimumMinMagLinearMipPoint, "FILTER_MINIMUM_MIN_MAG_LINEAR_MIP_POINT")
FILTER_ENUM(MinimumMinMagMipLinear, "FILTER_MINIMUM_MIN_MAG_MIP_LINEAR")
FILTER_ENUM(MinimumAnisotropic, "FILTER_MINIMUM_ANISOTROPIC")
FILTER_ENUM(MaximumMinMagMipPoint, "FILTER_MAXIMUM_MIN_MAG_MIP_POINT")
FILTER_ENUM(MaximumMinMagPointMipLinear, "FILTER_MAXIMUM_MIN_MAG_POINT_MIP_LINEAR")
FILTER_ENUM(MaximumMinPointMagLinearMipPoint, "FILTER_MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT")
FILTER_ENUM(MaximumMinPointMagMipLinear, "FILTER_MAXIMUM_MIN_POINT_MAG_MIP_LINEAR")
FILTER_ENUM(MaximumMinLinearMagMipPoint, "FILTER_MAXIMUM_MIN_LINEAR_MAG_MIP_POINT")
FILTER_ENUM(MaximumMinLinearMagPointMipLinear, "FILTER_MAXIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR")
FILTER_ENUM(MaximumMinMagLinearMipPoint, "FILTER_MAXIMUM_MIN_MAG_LINEAR_MIP_POINT")
FILTER_ENUM(MaximumMinMagMipLinear, "FILTER_MAXIMUM_MIN_MAG_MIP_LINEAR")
FILTER_ENUM(MaximumAnisotropic, "FILTER_MAXIMUM_ANISOTROPIC")

// Texture Address Mode Enums:
TEXTURE_ADDRESS_MODE_ENUM(Wrap, "TEXTURE_ADDRESS_WRAP")
TEXTURE_ADDRESS_MODE_ENUM(Mirror, "TEXTURE_ADDRESS_MIRROR")
Expand All @@ -162,6 +204,7 @@ TEXTURE_ADDRESS_MODE_ENUM(Border, "TEXTURE_ADDRESS_BORDER")
TEXTURE_ADDRESS_MODE_ENUM(MirrorOnce, "TEXTURE_ADDRESS_MIRRORONCE")

#undef TEXTURE_ADDRESS_MODE_ENUM
#undef FILTER_ENUM
#undef SHADER_VISIBILITY_ENUM
#undef DESCRIPTOR_RANGE_FLAG_ENUM
#undef DESCRIPTOR_RANGE_FLAG_ENUM_OFF
Expand Down
4 changes: 3 additions & 1 deletion clang/include/clang/Parse/ParseHLSLRootSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ class RootSignatureParser {

struct ParsedStaticSamplerParams {
std::optional<llvm::hlsl::rootsig::Register> Reg;
std::optional<float> MipLODBias;
std::optional<llvm::hlsl::rootsig::Filter> Filter;
std::optional<llvm::hlsl::rootsig::TextureAddressMode> AddressU;
std::optional<llvm::hlsl::rootsig::TextureAddressMode> AddressV;
std::optional<llvm::hlsl::rootsig::TextureAddressMode> AddressW;
std::optional<float> MipLODBias;
std::optional<uint32_t> MaxAnisotropy;
std::optional<float> MinLOD;
std::optional<float> MaxLOD;
Expand All @@ -128,6 +129,7 @@ class RootSignatureParser {

/// Parsing methods of various enums
std::optional<llvm::hlsl::rootsig::ShaderVisibility> parseShaderVisibility();
std::optional<llvm::hlsl::rootsig::Filter> parseFilter();
std::optional<llvm::hlsl::rootsig::TextureAddressMode>
parseTextureAddressMode();
std::optional<llvm::hlsl::rootsig::RootDescriptorFlags>
Expand Down
45 changes: 45 additions & 0 deletions clang/lib/Parse/ParseHLSLRootSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ std::optional<StaticSampler> RootSignatureParser::parseStaticSampler() {
Sampler.Reg = Params->Reg.value();

// Fill in optional values
if (Params->Filter.has_value())
Sampler.Filter = Params->Filter.value();

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

Expand Down Expand Up @@ -684,6 +687,23 @@ RootSignatureParser::parseStaticSamplerParams() {
Params.Reg = Reg;
}

// `filter` `=` FILTER
if (tryConsumeExpectedToken(TokenKind::kw_filter)) {
if (Params.Filter.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 Filter = parseFilter();
if (!Filter.has_value())
return std::nullopt;
Params.Filter = Filter;
}

// `addressU` `=` TEXTURE_ADDRESS
if (tryConsumeExpectedToken(TokenKind::kw_addressU)) {
if (Params.AddressU.has_value()) {
Expand Down Expand Up @@ -909,6 +929,31 @@ RootSignatureParser::parseShaderVisibility() {
return std::nullopt;
}

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

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

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

switch (CurToken.TokKind) {
#define FILTER_ENUM(NAME, LIT) \
case TokenKind::en_##NAME: \
return Filter::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::TextureAddressMode>
RootSignatureParser::parseTextureAddressMode() {
assert(CurToken.TokKind == TokenKind::pu_equal &&
Expand Down
41 changes: 39 additions & 2 deletions clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
space visibility flags
numDescriptors offset

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

unbounded
DESCRIPTOR_RANGE_OFFSET_APPEND
Expand Down Expand Up @@ -170,6 +170,43 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
shader_visibility_amplification
shader_visibility_mesh

FILTER_MIN_MAG_MIP_POINT
FILTER_MIN_MAG_POINT_MIP_LINEAR
FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT
FILTER_MIN_POINT_MAG_MIP_LINEAR
FILTER_MIN_LINEAR_MAG_MIP_POINT
FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR
FILTER_MIN_MAG_LINEAR_MIP_POINT
FILTER_MIN_MAG_MIP_LINEAR
FILTER_ANISOTROPIC
FILTER_COMPARISON_MIN_MAG_MIP_POINT
FILTER_COMPARISON_MIN_MAG_POINT_MIP_LINEAR
FILTER_COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT
FILTER_COMPARISON_MIN_POINT_MAG_MIP_LINEAR
FILTER_COMPARISON_MIN_LINEAR_MAG_MIP_POINT
FILTER_COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR
FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT
FILTER_COMPARISON_MIN_MAG_MIP_LINEAR
FILTER_COMPARISON_ANISOTROPIC
FILTER_MINIMUM_MIN_MAG_MIP_POINT
FILTER_MINIMUM_MIN_MAG_POINT_MIP_LINEAR
FILTER_MINIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT
FILTER_MINIMUM_MIN_POINT_MAG_MIP_LINEAR
FILTER_MINIMUM_MIN_LINEAR_MAG_MIP_POINT
FILTER_MINIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR
FILTER_MINIMUM_MIN_MAG_LINEAR_MIP_POINT
FILTER_MINIMUM_MIN_MAG_MIP_LINEAR
FILTER_MINIMUM_ANISOTROPIC
FILTER_MAXIMUM_MIN_MAG_MIP_POINT
FILTER_MAXIMUM_MIN_MAG_POINT_MIP_LINEAR
FILTER_MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT
FILTER_MAXIMUM_MIN_POINT_MAG_MIP_LINEAR
FILTER_MAXIMUM_MIN_LINEAR_MAG_MIP_POINT
FILTER_MAXIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR
FILTER_MAXIMUM_MIN_MAG_LINEAR_MIP_POINT
FILTER_MAXIMUM_MIN_MAG_MIP_LINEAR
FILTER_MAXIMUM_ANISOTROPIC

TEXTURE_ADDRESS_WRAP
TEXTURE_ADDRESS_MIRROR
TEXTURE_ADDRESS_CLAMP
Expand Down
4 changes: 4 additions & 0 deletions clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
minLOD = 4.2f, mipLODBias = 0.23e+3,
addressW = TEXTURE_ADDRESS_CLAMP,
addressV = TEXTURE_ADDRESS_BORDER,
filter = FILTER_MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT,
maxLOD = 9000, addressU = TEXTURE_ADDRESS_MIRROR
)
)cc";
Expand All @@ -254,6 +255,7 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
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).Filter, Filter::Anisotropic);
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressU, TextureAddressMode::Wrap);
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressV, TextureAddressMode::Wrap);
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressW, TextureAddressMode::Wrap);
Expand All @@ -267,6 +269,8 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
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).Filter,
Filter::MaximumMinPointMagLinearMipPoint);
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressU, TextureAddressMode::Mirror);
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressV, TextureAddressMode::Border);
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressW, TextureAddressMode::Clamp);
Expand Down
47 changes: 46 additions & 1 deletion llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ class Metadata;
namespace hlsl {
namespace rootsig {

// Definition of the various enumerations and flags
// Definition of the various enumerations and flags. The definitions of all
// values here correspond to their description in the d3d12.h header and are
// carried over from their values in DXC. For reference:
// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/

enum class RootFlags : uint32_t {
None = 0,
Expand Down Expand Up @@ -76,6 +79,47 @@ enum class ShaderVisibility {
Mesh = 7,
};

// D3D12_FILTER enumeration:
// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_filter
enum class Filter {
MinMagMipPoint = 0,
MinMagPointMipLinear = 0x1,
MinPointMagLinearMipPoint = 0x4,
MinPointMagMipLinear = 0x5,
MinLinearMagMipPoint = 0x10,
MinLinearMagPointMipLinear = 0x11,
MinMagLinearMipPoint = 0x14,
MinMagMipLinear = 0x15,
Anisotropic = 0x55,
ComparisonMinMagMipPoint = 0x80,
ComparisonMinMagPointMipLinear = 0x81,
ComparisonMinPointMagLinearMipPoint = 0x84,
ComparisonMinPointMagMipLinear = 0x85,
ComparisonMinLinearMagMipPoint = 0x90,
ComparisonMinLinearMagPointMipLinear = 0x91,
ComparisonMinMagLinearMipPoint = 0x94,
ComparisonMinMagMipLinear = 0x95,
ComparisonAnisotropic = 0xd5,
MinimumMinMagMipPoint = 0x100,
MinimumMinMagPointMipLinear = 0x101,
MinimumMinPointMagLinearMipPoint = 0x104,
MinimumMinPointMagMipLinear = 0x105,
MinimumMinLinearMagMipPoint = 0x110,
MinimumMinLinearMagPointMipLinear = 0x111,
MinimumMinMagLinearMipPoint = 0x114,
MinimumMinMagMipLinear = 0x115,
MinimumAnisotropic = 0x155,
MaximumMinMagMipPoint = 0x180,
MaximumMinMagPointMipLinear = 0x181,
MaximumMinPointMagLinearMipPoint = 0x184,
MaximumMinPointMagMipLinear = 0x185,
MaximumMinLinearMagMipPoint = 0x190,
MaximumMinLinearMagPointMipLinear = 0x191,
MaximumMinMagLinearMipPoint = 0x194,
MaximumMinMagMipLinear = 0x195,
MaximumAnisotropic = 0x1d5
};

enum class TextureAddressMode {
Wrap = 1,
Mirror = 2,
Expand Down Expand Up @@ -165,6 +209,7 @@ raw_ostream &operator<<(raw_ostream &OS, const DescriptorTableClause &Clause);

struct StaticSampler {
Register Reg;
Filter Filter = Filter::Anisotropic;
TextureAddressMode AddressU = TextureAddressMode::Wrap;
TextureAddressMode AddressV = TextureAddressMode::Wrap;
TextureAddressMode AddressW = TextureAddressMode::Wrap;
Expand Down
Loading