Skip to content

Commit 64ce3dd

Browse files
inbelicrorth
authored andcommitted
Reland "[HLSL][RootSignature] Add parsing of filter enum for StaticSampler" (llvm#142441)
This relands llvm#140294. The initial naming of the enum class Filter and the Filter struct member causes ambiguity when compiling with gcc. This change addresses this my renaming `Filter` to `SamplerFilter`. I have confirmed this builds locally using gcc. Resolves llvm#126574.
1 parent f22cd5d commit 64ce3dd

File tree

6 files changed

+180
-3
lines changed

6 files changed

+180
-3
lines changed

clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
#ifndef SHADER_VISIBILITY_ENUM
5454
#define SHADER_VISIBILITY_ENUM(NAME, LIT) ENUM(NAME, LIT)
5555
#endif
56+
#ifndef FILTER_ENUM
57+
#define FILTER_ENUM(NAME, LIT) ENUM(NAME, LIT)
58+
#endif
5659
#ifndef TEXTURE_ADDRESS_MODE_ENUM
5760
#define TEXTURE_ADDRESS_MODE_ENUM(NAME, LIT) ENUM(NAME, LIT)
5861
#endif
@@ -110,6 +113,7 @@ KEYWORD(numDescriptors)
110113
KEYWORD(offset)
111114

112115
// StaticSampler Keywords:
116+
KEYWORD(filter)
113117
KEYWORD(mipLODBias)
114118
KEYWORD(addressU)
115119
KEYWORD(addressV)
@@ -162,6 +166,44 @@ SHADER_VISIBILITY_ENUM(Pixel, "SHADER_VISIBILITY_PIXEL")
162166
SHADER_VISIBILITY_ENUM(Amplification, "SHADER_VISIBILITY_AMPLIFICATION")
163167
SHADER_VISIBILITY_ENUM(Mesh, "SHADER_VISIBILITY_MESH")
164168

169+
// Filter Enums:
170+
FILTER_ENUM(MinMagMipPoint, "FILTER_MIN_MAG_MIP_POINT")
171+
FILTER_ENUM(MinMagPointMipLinear, "FILTER_MIN_MAG_POINT_MIP_LINEAR")
172+
FILTER_ENUM(MinPointMagLinearMipPoint, "FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT")
173+
FILTER_ENUM(MinPointMagMipLinear, "FILTER_MIN_POINT_MAG_MIP_LINEAR")
174+
FILTER_ENUM(MinLinearMagMipPoint, "FILTER_MIN_LINEAR_MAG_MIP_POINT")
175+
FILTER_ENUM(MinLinearMagPointMipLinear, "FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR")
176+
FILTER_ENUM(MinMagLinearMipPoint, "FILTER_MIN_MAG_LINEAR_MIP_POINT")
177+
FILTER_ENUM(MinMagMipLinear, "FILTER_MIN_MAG_MIP_LINEAR")
178+
FILTER_ENUM(Anisotropic, "FILTER_ANISOTROPIC")
179+
FILTER_ENUM(ComparisonMinMagMipPoint, "FILTER_COMPARISON_MIN_MAG_MIP_POINT")
180+
FILTER_ENUM(ComparisonMinMagPointMipLinear, "FILTER_COMPARISON_MIN_MAG_POINT_MIP_LINEAR")
181+
FILTER_ENUM(ComparisonMinPointMagLinearMipPoint, "FILTER_COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT")
182+
FILTER_ENUM(ComparisonMinPointMagMipLinear, "FILTER_COMPARISON_MIN_POINT_MAG_MIP_LINEAR")
183+
FILTER_ENUM(ComparisonMinLinearMagMipPoint, "FILTER_COMPARISON_MIN_LINEAR_MAG_MIP_POINT")
184+
FILTER_ENUM(ComparisonMinLinearMagPointMipLinear, "FILTER_COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR")
185+
FILTER_ENUM(ComparisonMinMagLinearMipPoint, "FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT")
186+
FILTER_ENUM(ComparisonMinMagMipLinear, "FILTER_COMPARISON_MIN_MAG_MIP_LINEAR")
187+
FILTER_ENUM(ComparisonAnisotropic, "FILTER_COMPARISON_ANISOTROPIC")
188+
FILTER_ENUM(MinimumMinMagMipPoint, "FILTER_MINIMUM_MIN_MAG_MIP_POINT")
189+
FILTER_ENUM(MinimumMinMagPointMipLinear, "FILTER_MINIMUM_MIN_MAG_POINT_MIP_LINEAR")
190+
FILTER_ENUM(MinimumMinPointMagLinearMipPoint, "FILTER_MINIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT")
191+
FILTER_ENUM(MinimumMinPointMagMipLinear, "FILTER_MINIMUM_MIN_POINT_MAG_MIP_LINEAR")
192+
FILTER_ENUM(MinimumMinLinearMagMipPoint, "FILTER_MINIMUM_MIN_LINEAR_MAG_MIP_POINT")
193+
FILTER_ENUM(MinimumMinLinearMagPointMipLinear, "FILTER_MINIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR")
194+
FILTER_ENUM(MinimumMinMagLinearMipPoint, "FILTER_MINIMUM_MIN_MAG_LINEAR_MIP_POINT")
195+
FILTER_ENUM(MinimumMinMagMipLinear, "FILTER_MINIMUM_MIN_MAG_MIP_LINEAR")
196+
FILTER_ENUM(MinimumAnisotropic, "FILTER_MINIMUM_ANISOTROPIC")
197+
FILTER_ENUM(MaximumMinMagMipPoint, "FILTER_MAXIMUM_MIN_MAG_MIP_POINT")
198+
FILTER_ENUM(MaximumMinMagPointMipLinear, "FILTER_MAXIMUM_MIN_MAG_POINT_MIP_LINEAR")
199+
FILTER_ENUM(MaximumMinPointMagLinearMipPoint, "FILTER_MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT")
200+
FILTER_ENUM(MaximumMinPointMagMipLinear, "FILTER_MAXIMUM_MIN_POINT_MAG_MIP_LINEAR")
201+
FILTER_ENUM(MaximumMinLinearMagMipPoint, "FILTER_MAXIMUM_MIN_LINEAR_MAG_MIP_POINT")
202+
FILTER_ENUM(MaximumMinLinearMagPointMipLinear, "FILTER_MAXIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR")
203+
FILTER_ENUM(MaximumMinMagLinearMipPoint, "FILTER_MAXIMUM_MIN_MAG_LINEAR_MIP_POINT")
204+
FILTER_ENUM(MaximumMinMagMipLinear, "FILTER_MAXIMUM_MIN_MAG_MIP_LINEAR")
205+
FILTER_ENUM(MaximumAnisotropic, "FILTER_MAXIMUM_ANISOTROPIC")
206+
165207
// Texture Address Mode Enums:
166208
TEXTURE_ADDRESS_MODE_ENUM(Wrap, "TEXTURE_ADDRESS_WRAP")
167209
TEXTURE_ADDRESS_MODE_ENUM(Mirror, "TEXTURE_ADDRESS_MIRROR")
@@ -189,6 +231,7 @@ STATIC_BORDER_COLOR_ENUM(OpaqueWhiteUint, "STATIC_BORDER_COLOR_OPAQUE_WHITE_UINT
189231
#undef STATIC_BORDER_COLOR_ENUM
190232
#undef COMPARISON_FUNC_ENUM
191233
#undef TEXTURE_ADDRESS_MODE_ENUM
234+
#undef FILTER_ENUM
192235
#undef SHADER_VISIBILITY_ENUM
193236
#undef DESCRIPTOR_RANGE_FLAG_ENUM
194237
#undef DESCRIPTOR_RANGE_FLAG_ENUM_OFF

clang/include/clang/Parse/ParseHLSLRootSignature.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ class RootSignatureParser {
111111

112112
struct ParsedStaticSamplerParams {
113113
std::optional<llvm::hlsl::rootsig::Register> Reg;
114-
std::optional<float> MipLODBias;
114+
std::optional<llvm::hlsl::rootsig::SamplerFilter> Filter;
115115
std::optional<llvm::hlsl::rootsig::TextureAddressMode> AddressU;
116116
std::optional<llvm::hlsl::rootsig::TextureAddressMode> AddressV;
117117
std::optional<llvm::hlsl::rootsig::TextureAddressMode> AddressW;
118+
std::optional<float> MipLODBias;
118119
std::optional<uint32_t> MaxAnisotropy;
119120
std::optional<llvm::hlsl::rootsig::ComparisonFunc> CompFunc;
120121
std::optional<llvm::hlsl::rootsig::StaticBorderColor> BorderColor;
@@ -132,6 +133,7 @@ class RootSignatureParser {
132133

133134
/// Parsing methods of various enums
134135
std::optional<llvm::hlsl::rootsig::ShaderVisibility> parseShaderVisibility();
136+
std::optional<llvm::hlsl::rootsig::SamplerFilter> parseSamplerFilter();
135137
std::optional<llvm::hlsl::rootsig::TextureAddressMode>
136138
parseTextureAddressMode();
137139
std::optional<llvm::hlsl::rootsig::ComparisonFunc> parseComparisonFunc();

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ std::optional<StaticSampler> RootSignatureParser::parseStaticSampler() {
377377
Sampler.Reg = Params->Reg.value();
378378

379379
// Fill in optional values
380+
if (Params->Filter.has_value())
381+
Sampler.Filter = Params->Filter.value();
382+
380383
if (Params->AddressU.has_value())
381384
Sampler.AddressU = Params->AddressU.value();
382385

@@ -696,6 +699,23 @@ RootSignatureParser::parseStaticSamplerParams() {
696699
Params.Reg = Reg;
697700
}
698701

702+
// `filter` `=` FILTER
703+
if (tryConsumeExpectedToken(TokenKind::kw_filter)) {
704+
if (Params.Filter.has_value()) {
705+
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
706+
<< CurToken.TokKind;
707+
return std::nullopt;
708+
}
709+
710+
if (consumeExpectedToken(TokenKind::pu_equal))
711+
return std::nullopt;
712+
713+
auto Filter = parseSamplerFilter();
714+
if (!Filter.has_value())
715+
return std::nullopt;
716+
Params.Filter = Filter;
717+
}
718+
699719
// `addressU` `=` TEXTURE_ADDRESS
700720
if (tryConsumeExpectedToken(TokenKind::kw_addressU)) {
701721
if (Params.AddressU.has_value()) {
@@ -989,6 +1009,32 @@ RootSignatureParser::parseShaderVisibility() {
9891009
return std::nullopt;
9901010
}
9911011

1012+
std::optional<llvm::hlsl::rootsig::SamplerFilter>
1013+
RootSignatureParser::parseSamplerFilter() {
1014+
assert(CurToken.TokKind == TokenKind::pu_equal &&
1015+
"Expects to only be invoked starting at given keyword");
1016+
1017+
TokenKind Expected[] = {
1018+
#define FILTER_ENUM(NAME, LIT) TokenKind::en_##NAME,
1019+
#include "clang/Lex/HLSLRootSignatureTokenKinds.def"
1020+
};
1021+
1022+
if (!tryConsumeExpectedToken(Expected))
1023+
return std::nullopt;
1024+
1025+
switch (CurToken.TokKind) {
1026+
#define FILTER_ENUM(NAME, LIT) \
1027+
case TokenKind::en_##NAME: \
1028+
return SamplerFilter::NAME; \
1029+
break;
1030+
#include "clang/Lex/HLSLRootSignatureTokenKinds.def"
1031+
default:
1032+
llvm_unreachable("Switch for consumed enum token was not provided");
1033+
}
1034+
1035+
return std::nullopt;
1036+
}
1037+
9921038
std::optional<llvm::hlsl::rootsig::TextureAddressMode>
9931039
RootSignatureParser::parseTextureAddressMode() {
9941040
assert(CurToken.TokKind == TokenKind::pu_equal &&

clang/unittests/Lex/LexHLSLRootSignatureTest.cpp

Lines changed: 38 additions & 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 addressU addressV addressW
139+
filter mipLODBias addressU addressV addressW
140140
maxAnisotropy comparisonFunc borderColor
141141
minLOD maxLOD
142142
@@ -171,6 +171,43 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
171171
shader_visibility_amplification
172172
shader_visibility_mesh
173173
174+
FILTER_MIN_MAG_MIP_POINT
175+
FILTER_MIN_MAG_POINT_MIP_LINEAR
176+
FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT
177+
FILTER_MIN_POINT_MAG_MIP_LINEAR
178+
FILTER_MIN_LINEAR_MAG_MIP_POINT
179+
FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR
180+
FILTER_MIN_MAG_LINEAR_MIP_POINT
181+
FILTER_MIN_MAG_MIP_LINEAR
182+
FILTER_ANISOTROPIC
183+
FILTER_COMPARISON_MIN_MAG_MIP_POINT
184+
FILTER_COMPARISON_MIN_MAG_POINT_MIP_LINEAR
185+
FILTER_COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT
186+
FILTER_COMPARISON_MIN_POINT_MAG_MIP_LINEAR
187+
FILTER_COMPARISON_MIN_LINEAR_MAG_MIP_POINT
188+
FILTER_COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR
189+
FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT
190+
FILTER_COMPARISON_MIN_MAG_MIP_LINEAR
191+
FILTER_COMPARISON_ANISOTROPIC
192+
FILTER_MINIMUM_MIN_MAG_MIP_POINT
193+
FILTER_MINIMUM_MIN_MAG_POINT_MIP_LINEAR
194+
FILTER_MINIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT
195+
FILTER_MINIMUM_MIN_POINT_MAG_MIP_LINEAR
196+
FILTER_MINIMUM_MIN_LINEAR_MAG_MIP_POINT
197+
FILTER_MINIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR
198+
FILTER_MINIMUM_MIN_MAG_LINEAR_MIP_POINT
199+
FILTER_MINIMUM_MIN_MAG_MIP_LINEAR
200+
FILTER_MINIMUM_ANISOTROPIC
201+
FILTER_MAXIMUM_MIN_MAG_MIP_POINT
202+
FILTER_MAXIMUM_MIN_MAG_POINT_MIP_LINEAR
203+
FILTER_MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT
204+
FILTER_MAXIMUM_MIN_POINT_MAG_MIP_LINEAR
205+
FILTER_MAXIMUM_MIN_LINEAR_MAG_MIP_POINT
206+
FILTER_MAXIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR
207+
FILTER_MAXIMUM_MIN_MAG_LINEAR_MIP_POINT
208+
FILTER_MAXIMUM_MIN_MAG_MIP_LINEAR
209+
FILTER_MAXIMUM_ANISOTROPIC
210+
174211
TEXTURE_ADDRESS_WRAP
175212
TEXTURE_ADDRESS_MIRROR
176213
TEXTURE_ADDRESS_CLAMP

clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
231231
minLOD = 4.2f, mipLODBias = 0.23e+3,
232232
addressW = TEXTURE_ADDRESS_CLAMP,
233233
addressV = TEXTURE_ADDRESS_BORDER,
234+
filter = FILTER_MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT,
234235
maxLOD = 9000, addressU = TEXTURE_ADDRESS_MIRROR,
235236
comparisonFunc = COMPARISON_NOT_EQUAL,
236237
borderColor = STATIC_BORDER_COLOR_OPAQUE_BLACK_UINT
@@ -257,6 +258,7 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
257258
ASSERT_TRUE(std::holds_alternative<StaticSampler>(Elem));
258259
ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.ViewType, RegisterType::SReg);
259260
ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.Number, 0u);
261+
ASSERT_EQ(std::get<StaticSampler>(Elem).Filter, SamplerFilter::Anisotropic);
260262
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressU, TextureAddressMode::Wrap);
261263
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressV, TextureAddressMode::Wrap);
262264
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressW, TextureAddressMode::Wrap);
@@ -275,6 +277,8 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
275277
ASSERT_TRUE(std::holds_alternative<StaticSampler>(Elem));
276278
ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.ViewType, RegisterType::SReg);
277279
ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.Number, 0u);
280+
ASSERT_EQ(std::get<StaticSampler>(Elem).Filter,
281+
SamplerFilter::MaximumMinPointMagLinearMipPoint);
278282
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressU, TextureAddressMode::Mirror);
279283
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressV, TextureAddressMode::Border);
280284
ASSERT_EQ(std::get<StaticSampler>(Elem).AddressW, TextureAddressMode::Clamp);

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ class Metadata;
2828
namespace hlsl {
2929
namespace rootsig {
3030

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

3336
enum class RootFlags : uint32_t {
3437
None = 0,
@@ -77,6 +80,47 @@ enum class ShaderVisibility {
7780
Mesh = 7,
7881
};
7982

83+
// D3D12_FILTER enumeration:
84+
// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_filter
85+
enum class SamplerFilter {
86+
MinMagMipPoint = 0,
87+
MinMagPointMipLinear = 0x1,
88+
MinPointMagLinearMipPoint = 0x4,
89+
MinPointMagMipLinear = 0x5,
90+
MinLinearMagMipPoint = 0x10,
91+
MinLinearMagPointMipLinear = 0x11,
92+
MinMagLinearMipPoint = 0x14,
93+
MinMagMipLinear = 0x15,
94+
Anisotropic = 0x55,
95+
ComparisonMinMagMipPoint = 0x80,
96+
ComparisonMinMagPointMipLinear = 0x81,
97+
ComparisonMinPointMagLinearMipPoint = 0x84,
98+
ComparisonMinPointMagMipLinear = 0x85,
99+
ComparisonMinLinearMagMipPoint = 0x90,
100+
ComparisonMinLinearMagPointMipLinear = 0x91,
101+
ComparisonMinMagLinearMipPoint = 0x94,
102+
ComparisonMinMagMipLinear = 0x95,
103+
ComparisonAnisotropic = 0xd5,
104+
MinimumMinMagMipPoint = 0x100,
105+
MinimumMinMagPointMipLinear = 0x101,
106+
MinimumMinPointMagLinearMipPoint = 0x104,
107+
MinimumMinPointMagMipLinear = 0x105,
108+
MinimumMinLinearMagMipPoint = 0x110,
109+
MinimumMinLinearMagPointMipLinear = 0x111,
110+
MinimumMinMagLinearMipPoint = 0x114,
111+
MinimumMinMagMipLinear = 0x115,
112+
MinimumAnisotropic = 0x155,
113+
MaximumMinMagMipPoint = 0x180,
114+
MaximumMinMagPointMipLinear = 0x181,
115+
MaximumMinPointMagLinearMipPoint = 0x184,
116+
MaximumMinPointMagMipLinear = 0x185,
117+
MaximumMinLinearMagMipPoint = 0x190,
118+
MaximumMinLinearMagPointMipLinear = 0x191,
119+
MaximumMinMagLinearMipPoint = 0x194,
120+
MaximumMinMagMipLinear = 0x195,
121+
MaximumAnisotropic = 0x1d5
122+
};
123+
80124
enum class TextureAddressMode {
81125
Wrap = 1,
82126
Mirror = 2,
@@ -186,6 +230,7 @@ LLVM_ABI raw_ostream &operator<<(raw_ostream &OS,
186230

187231
struct StaticSampler {
188232
Register Reg;
233+
SamplerFilter Filter = SamplerFilter::Anisotropic;
189234
TextureAddressMode AddressU = TextureAddressMode::Wrap;
190235
TextureAddressMode AddressV = TextureAddressMode::Wrap;
191236
TextureAddressMode AddressW = TextureAddressMode::Wrap;

0 commit comments

Comments
 (0)