Skip to content

Commit c7cbaef

Browse files
joaosaffranjoaosaffran
and
joaosaffran
authored
[DirectX] Adding support for static samplers in yaml2obj/obj2yaml (#139963)
- Adds support for static samplers ins dxcontainer binary format. - Adds writing logic to mcdxbc - adds reading logic to Object - adds tests Closes: [126636](#126636) --------- Co-authored-by: joaosaffran <joao.saffran@microsoft.com>
1 parent 572b89a commit c7cbaef

File tree

13 files changed

+459
-1
lines changed

13 files changed

+459
-1
lines changed

llvm/include/llvm/BinaryFormat/DXContainer.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,26 @@ inline bool isValidShaderVisibility(uint32_t V) {
209209
return false;
210210
}
211211

212+
#define STATIC_SAMPLER_FILTER(Val, Enum) Enum = Val,
213+
enum class StaticSamplerFilter : uint32_t {
214+
#include "DXContainerConstants.def"
215+
};
216+
217+
#define TEXTURE_ADDRESS_MODE(Val, Enum) Enum = Val,
218+
enum class TextureAddressMode : uint32_t {
219+
#include "DXContainerConstants.def"
220+
};
221+
222+
#define COMPARISON_FUNCTION(Val, Enum) Enum = Val,
223+
enum class SamplersComparisonFunction : uint32_t {
224+
#include "DXContainerConstants.def"
225+
};
226+
227+
#define STATIC_BORDER_COLOR(Val, Enum) Enum = Val,
228+
enum class SamplersBorderColor : uint32_t {
229+
#include "DXContainerConstants.def"
230+
};
231+
212232
PartType parsePartType(StringRef S);
213233

214234
struct VertexPSVInfo {
@@ -600,6 +620,37 @@ static_assert(sizeof(ProgramSignatureElement) == 32,
600620

601621
namespace RTS0 {
602622
namespace v1 {
623+
struct StaticSampler {
624+
uint32_t Filter;
625+
uint32_t AddressU;
626+
uint32_t AddressV;
627+
uint32_t AddressW;
628+
float MipLODBias;
629+
uint32_t MaxAnisotropy;
630+
uint32_t ComparisonFunc;
631+
uint32_t BorderColor;
632+
float MinLOD;
633+
float MaxLOD;
634+
uint32_t ShaderRegister;
635+
uint32_t RegisterSpace;
636+
uint32_t ShaderVisibility;
637+
void swapBytes() {
638+
sys::swapByteOrder(Filter);
639+
sys::swapByteOrder(AddressU);
640+
sys::swapByteOrder(AddressV);
641+
sys::swapByteOrder(AddressW);
642+
sys::swapByteOrder(MipLODBias);
643+
sys::swapByteOrder(MaxAnisotropy);
644+
sys::swapByteOrder(ComparisonFunc);
645+
sys::swapByteOrder(BorderColor);
646+
sys::swapByteOrder(MinLOD);
647+
sys::swapByteOrder(MaxLOD);
648+
sys::swapByteOrder(ShaderRegister);
649+
sys::swapByteOrder(RegisterSpace);
650+
sys::swapByteOrder(ShaderVisibility);
651+
};
652+
};
653+
603654
struct DescriptorRange {
604655
uint32_t RangeType;
605656
uint32_t NumDescriptors;

llvm/include/llvm/BinaryFormat/DXContainerConstants.def

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,79 @@ SHADER_VISIBILITY(7, Mesh)
121121
#undef SHADER_VISIBILITY
122122
#endif // SHADER_VISIBILITY
123123

124+
#ifdef STATIC_SAMPLER_FILTER
125+
126+
STATIC_SAMPLER_FILTER(0, MIN_MAG_MIP_POINT)
127+
STATIC_SAMPLER_FILTER(0x1, MIN_MAG_POINT_MIP_LINEAR)
128+
STATIC_SAMPLER_FILTER(0x4, MIN_POINT_MAG_LINEAR_MIP_POINT)
129+
STATIC_SAMPLER_FILTER(0x5, MIN_POINT_MAG_MIP_LINEAR)
130+
STATIC_SAMPLER_FILTER(0x10, MIN_LINEAR_MAG_MIP_POINT)
131+
STATIC_SAMPLER_FILTER(0x11, MIN_LINEAR_MAG_POINT_MIP_LINEAR)
132+
STATIC_SAMPLER_FILTER(0x14, MIN_MAG_LINEAR_MIP_POINT)
133+
STATIC_SAMPLER_FILTER(0x15, MIN_MAG_MIP_LINEAR)
134+
STATIC_SAMPLER_FILTER(0x55, ANISOTROPIC)
135+
STATIC_SAMPLER_FILTER(0x80, COMPARISON_MIN_MAG_MIP_POINT)
136+
STATIC_SAMPLER_FILTER(0x81, COMPARISON_MIN_MAG_POINT_MIP_LINEAR)
137+
STATIC_SAMPLER_FILTER(0x84, COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT)
138+
STATIC_SAMPLER_FILTER(0x85, COMPARISON_MIN_POINT_MAG_MIP_LINEAR)
139+
STATIC_SAMPLER_FILTER(0x90, COMPARISON_MIN_LINEAR_MAG_MIP_POINT)
140+
STATIC_SAMPLER_FILTER(0x91, COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR)
141+
STATIC_SAMPLER_FILTER(0x94, COMPARISON_MIN_MAG_LINEAR_MIP_POINT)
142+
STATIC_SAMPLER_FILTER(0x95, COMPARISON_MIN_MAG_MIP_LINEAR)
143+
STATIC_SAMPLER_FILTER(0xd5, COMPARISON_ANISOTROPIC)
144+
STATIC_SAMPLER_FILTER(0x100, MINIMUM_MIN_MAG_MIP_POINT)
145+
STATIC_SAMPLER_FILTER(0x101, MINIMUM_MIN_MAG_POINT_MIP_LINEAR)
146+
STATIC_SAMPLER_FILTER(0x104, MINIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT)
147+
STATIC_SAMPLER_FILTER(0x105, MINIMUM_MIN_POINT_MAG_MIP_LINEAR)
148+
STATIC_SAMPLER_FILTER(0x110, MINIMUM_MIN_LINEAR_MAG_MIP_POINT)
149+
STATIC_SAMPLER_FILTER(0x111, MINIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR)
150+
STATIC_SAMPLER_FILTER(0x114, MINIMUM_MIN_MAG_LINEAR_MIP_POINT)
151+
STATIC_SAMPLER_FILTER(0x115, MINIMUM_MIN_MAG_MIP_LINEAR)
152+
STATIC_SAMPLER_FILTER(0x155, MINIMUM_ANISOTROPIC)
153+
STATIC_SAMPLER_FILTER(0x180, MAXIMUM_MIN_MAG_MIP_POINT)
154+
STATIC_SAMPLER_FILTER(0x181, MAXIMUM_MIN_MAG_POINT_MIP_LINEAR)
155+
STATIC_SAMPLER_FILTER(0x184, MAXIMUM_MIN_POINT_MAG_LINEAR_MIP_POINT)
156+
STATIC_SAMPLER_FILTER(0x185, MAXIMUM_MIN_POINT_MAG_MIP_LINEAR)
157+
STATIC_SAMPLER_FILTER(0x190, MAXIMUM_MIN_LINEAR_MAG_MIP_POINT)
158+
STATIC_SAMPLER_FILTER(0x191, MAXIMUM_MIN_LINEAR_MAG_POINT_MIP_LINEAR)
159+
STATIC_SAMPLER_FILTER(0x194, MAXIMUM_MIN_MAG_LINEAR_MIP_POINT)
160+
STATIC_SAMPLER_FILTER(0x195, MAXIMUM_MIN_MAG_MIP_LINEAR)
161+
STATIC_SAMPLER_FILTER(0x1d5, MAXIMUM_ANISOTROPIC)
162+
#undef STATIC_SAMPLER_FILTER
163+
#endif // STATIC_SAMPLER_FILTER
164+
165+
#ifdef TEXTURE_ADDRESS_MODE
166+
167+
TEXTURE_ADDRESS_MODE( 1, Wrap)
168+
TEXTURE_ADDRESS_MODE( 2, Mirror)
169+
TEXTURE_ADDRESS_MODE( 3, Clamp)
170+
TEXTURE_ADDRESS_MODE( 4, Border)
171+
TEXTURE_ADDRESS_MODE( 5, MirrorOnce)
172+
#undef TEXTURE_ADDRESS_MODE
173+
#endif // TEXTURE_ADDRESS_MODE
174+
175+
#ifdef COMPARISON_FUNCTION
176+
177+
COMPARISON_FUNCTION( 1, Never)
178+
COMPARISON_FUNCTION( 2, Less)
179+
COMPARISON_FUNCTION( 3, Equal)
180+
COMPARISON_FUNCTION( 4, LessEqual)
181+
COMPARISON_FUNCTION( 5, Greater)
182+
COMPARISON_FUNCTION( 6, NotEqual)
183+
COMPARISON_FUNCTION( 7, GreaterEqual)
184+
COMPARISON_FUNCTION( 8, Always)
185+
#undef COMPARISON_FUNCTION
186+
#endif // COMPARISON_FUNCTION
187+
188+
#ifdef STATIC_BORDER_COLOR
189+
STATIC_BORDER_COLOR( 0, TransparentBlack)
190+
STATIC_BORDER_COLOR( 1, OpaqueBlack)
191+
STATIC_BORDER_COLOR( 2, OpaqueWhite)
192+
STATIC_BORDER_COLOR( 3, OpaqueBlackUint)
193+
STATIC_BORDER_COLOR( 4, OpaqueWhiteUint)
194+
#undef STATIC_BORDER_COLOR
195+
#endif // STATIC_BORDER_COLOR
196+
124197
#ifdef DXIL_MODULE_FLAG
125198

126199
// Only save DXIL module flags which not map to feature flags here.

llvm/include/llvm/MC/DXContainerRootSignature.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ struct RootSignatureDesc {
108108
uint32_t StaticSamplersOffset = 0u;
109109
uint32_t NumStaticSamplers = 0u;
110110
mcdxbc::RootParametersContainer ParametersContainer;
111+
SmallVector<dxbc::RTS0::v1::StaticSampler> StaticSamplers;
111112

112113
void write(raw_ostream &OS) const;
113114

llvm/include/llvm/Object/DXContainer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,11 @@ class RootSignature {
236236
uint32_t Flags;
237237
ViewArray<dxbc::RTS0::v1::RootParameterHeader> ParametersHeaders;
238238
StringRef PartData;
239+
ViewArray<dxbc::RTS0::v1::StaticSampler> StaticSamplers;
239240

240241
using param_header_iterator =
241242
ViewArray<dxbc::RTS0::v1::RootParameterHeader>::iterator;
243+
using samplers_iterator = ViewArray<dxbc::RTS0::v1::StaticSampler>::iterator;
242244

243245
public:
244246
RootSignature(StringRef PD) : PartData(PD) {}
@@ -253,6 +255,9 @@ class RootSignature {
253255
llvm::iterator_range<param_header_iterator> param_headers() const {
254256
return llvm::make_range(ParametersHeaders.begin(), ParametersHeaders.end());
255257
}
258+
llvm::iterator_range<samplers_iterator> samplers() const {
259+
return llvm::make_range(StaticSamplers.begin(), StaticSamplers.end());
260+
}
256261
uint32_t getFlags() const { return Flags; }
257262

258263
llvm::Expected<RootParameterView>

llvm/include/llvm/ObjectYAML/DXContainerYAML.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,24 @@ struct RootParameterYamlDesc {
163163
}
164164
};
165165

166+
struct StaticSamplerYamlDesc {
167+
uint32_t Filter = llvm::to_underlying(dxbc::StaticSamplerFilter::ANISOTROPIC);
168+
uint32_t AddressU = llvm::to_underlying(dxbc::TextureAddressMode::Wrap);
169+
uint32_t AddressV = llvm::to_underlying(dxbc::TextureAddressMode::Wrap);
170+
uint32_t AddressW = llvm::to_underlying(dxbc::TextureAddressMode::Wrap);
171+
float MipLODBias = 0.f;
172+
uint32_t MaxAnisotropy = 16u;
173+
uint32_t ComparisonFunc =
174+
llvm::to_underlying(dxbc::SamplersComparisonFunction::LessEqual);
175+
uint32_t BorderColor =
176+
llvm::to_underlying(dxbc::SamplersBorderColor::OpaqueWhite);
177+
float MinLOD = 0.f;
178+
float MaxLOD = std::numeric_limits<float>::max();
179+
uint32_t ShaderRegister;
180+
uint32_t RegisterSpace;
181+
uint32_t ShaderVisibility;
182+
};
183+
166184
struct RootSignatureYamlDesc {
167185
RootSignatureYamlDesc() = default;
168186

@@ -173,9 +191,14 @@ struct RootSignatureYamlDesc {
173191
uint32_t StaticSamplersOffset;
174192

175193
RootParameterYamlDesc Parameters;
194+
SmallVector<StaticSamplerYamlDesc> StaticSamplers;
176195

177196
uint32_t getEncodedFlags();
178197

198+
iterator_range<StaticSamplerYamlDesc *> samplers() {
199+
return make_range(StaticSamplers.begin(), StaticSamplers.end());
200+
}
201+
179202
static llvm::Expected<DXContainerYAML::RootSignatureYamlDesc>
180203
create(const object::DirectX::RootSignature &Data);
181204

@@ -288,6 +311,7 @@ LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::PSVInfo::MaskVector)
288311
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::SignatureParameter)
289312
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::RootParameterLocationYaml)
290313
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::DescriptorRangeYaml)
314+
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::StaticSamplerYamlDesc)
291315
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::SemanticKind)
292316
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::ComponentType)
293317
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::InterpolationMode)
@@ -384,6 +408,10 @@ template <> struct MappingTraits<llvm::DXContainerYAML::DescriptorRangeYaml> {
384408
static void mapping(IO &IO, llvm::DXContainerYAML::DescriptorRangeYaml &D);
385409
};
386410

411+
template <> struct MappingTraits<llvm::DXContainerYAML::StaticSamplerYamlDesc> {
412+
static void mapping(IO &IO, llvm::DXContainerYAML::StaticSamplerYamlDesc &S);
413+
};
414+
387415
} // namespace yaml
388416

389417
} // namespace llvm

llvm/lib/MC/DXContainerRootSignature.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ static void rewriteOffsetToCurrentByte(raw_svector_ostream &Stream,
3131
size_t RootSignatureDesc::getSize() const {
3232
size_t Size =
3333
sizeof(dxbc::RTS0::v1::RootSignatureHeader) +
34-
ParametersContainer.size() * sizeof(dxbc::RTS0::v1::RootParameterHeader);
34+
ParametersContainer.size() * sizeof(dxbc::RTS0::v1::RootParameterHeader) +
35+
StaticSamplers.size() * sizeof(dxbc::RTS0::v1::StaticSampler);
3536

3637
for (const RootParameterInfo &I : ParametersContainer) {
3738
switch (I.Header.ParameterType) {
@@ -141,6 +142,21 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
141142
}
142143
}
143144
}
145+
for (const auto &S : StaticSamplers) {
146+
support::endian::write(BOS, S.Filter, llvm::endianness::little);
147+
support::endian::write(BOS, S.AddressU, llvm::endianness::little);
148+
support::endian::write(BOS, S.AddressV, llvm::endianness::little);
149+
support::endian::write(BOS, S.AddressW, llvm::endianness::little);
150+
support::endian::write(BOS, S.MipLODBias, llvm::endianness::little);
151+
support::endian::write(BOS, S.MaxAnisotropy, llvm::endianness::little);
152+
support::endian::write(BOS, S.ComparisonFunc, llvm::endianness::little);
153+
support::endian::write(BOS, S.BorderColor, llvm::endianness::little);
154+
support::endian::write(BOS, S.MinLOD, llvm::endianness::little);
155+
support::endian::write(BOS, S.MaxLOD, llvm::endianness::little);
156+
support::endian::write(BOS, S.ShaderRegister, llvm::endianness::little);
157+
support::endian::write(BOS, S.RegisterSpace, llvm::endianness::little);
158+
support::endian::write(BOS, S.ShaderVisibility, llvm::endianness::little);
159+
}
144160
assert(Storage.size() == getSize());
145161
OS.write(Storage.data(), Storage.size());
146162
}

llvm/lib/Object/DXContainer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ Error DirectX::RootSignature::parse() {
276276
RootParametersOffset,
277277
NumParameters * sizeof(dxbc::RTS0::v1::RootParameterHeader));
278278

279+
StaticSamplers.Stride = sizeof(dxbc::RTS0::v1::StaticSampler);
280+
StaticSamplers.Data = PartData.substr(
281+
StaticSamplersOffset,
282+
NumStaticSamplers * sizeof(dxbc::RTS0::v1::StaticSampler));
283+
279284
return Error::success();
280285
}
281286

llvm/lib/ObjectYAML/DXContainerEmitter.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,25 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
331331
}
332332
}
333333

334+
for (const auto &Param : P.RootSignature->samplers()) {
335+
dxbc::RTS0::v1::StaticSampler NewSampler;
336+
NewSampler.Filter = Param.Filter;
337+
NewSampler.AddressU = Param.AddressU;
338+
NewSampler.AddressV = Param.AddressV;
339+
NewSampler.AddressW = Param.AddressW;
340+
NewSampler.MipLODBias = Param.MipLODBias;
341+
NewSampler.MaxAnisotropy = Param.MaxAnisotropy;
342+
NewSampler.ComparisonFunc = Param.ComparisonFunc;
343+
NewSampler.BorderColor = Param.BorderColor;
344+
NewSampler.MinLOD = Param.MinLOD;
345+
NewSampler.MaxLOD = Param.MaxLOD;
346+
NewSampler.ShaderRegister = Param.ShaderRegister;
347+
NewSampler.RegisterSpace = Param.RegisterSpace;
348+
NewSampler.ShaderVisibility = Param.ShaderVisibility;
349+
350+
RS.StaticSamplers.push_back(NewSampler);
351+
}
352+
334353
RS.write(OS);
335354
break;
336355
}

llvm/lib/ObjectYAML/DXContainerYAML.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,26 @@ DXContainerYAML::RootSignatureYamlDesc::create(
139139
}
140140
}
141141
}
142+
143+
for (const auto &S : Data.samplers()) {
144+
StaticSamplerYamlDesc NewS;
145+
NewS.Filter = S.Filter;
146+
NewS.AddressU = S.AddressU;
147+
NewS.AddressV = S.AddressV;
148+
NewS.AddressW = S.AddressW;
149+
NewS.MipLODBias = S.MipLODBias;
150+
NewS.MaxAnisotropy = S.MaxAnisotropy;
151+
NewS.ComparisonFunc = S.ComparisonFunc;
152+
NewS.BorderColor = S.BorderColor;
153+
NewS.MinLOD = S.MinLOD;
154+
NewS.MaxLOD = S.MaxLOD;
155+
NewS.ShaderRegister = S.ShaderRegister;
156+
NewS.RegisterSpace = S.RegisterSpace;
157+
NewS.ShaderVisibility = S.ShaderVisibility;
158+
159+
RootSigDesc.StaticSamplers.push_back(NewS);
160+
}
161+
142162
#define ROOT_ELEMENT_FLAG(Num, Val) \
143163
RootSigDesc.Val = \
144164
(Flags & llvm::to_underlying(dxbc::RootElementFlag::Val)) > 0;
@@ -340,6 +360,7 @@ void MappingTraits<DXContainerYAML::RootSignatureYamlDesc>::mapping(
340360
IO.mapRequired("NumStaticSamplers", S.NumStaticSamplers);
341361
IO.mapRequired("StaticSamplersOffset", S.StaticSamplersOffset);
342362
IO.mapRequired("Parameters", S.Parameters.Locations, S);
363+
IO.mapOptional("Samplers", S.StaticSamplers);
343364
#define ROOT_ELEMENT_FLAG(Num, Val) IO.mapOptional(#Val, S.Val, false);
344365
#include "llvm/BinaryFormat/DXContainerConstants.def"
345366
}
@@ -421,6 +442,24 @@ void MappingTraits<llvm::DXContainerYAML::RootDescriptorYaml>::mapping(
421442
#include "llvm/BinaryFormat/DXContainerConstants.def"
422443
}
423444

445+
void MappingTraits<llvm::DXContainerYAML::StaticSamplerYamlDesc>::mapping(
446+
IO &IO, llvm::DXContainerYAML::StaticSamplerYamlDesc &S) {
447+
448+
IO.mapOptional("Filter", S.Filter);
449+
IO.mapOptional("AddressU", S.AddressU);
450+
IO.mapOptional("AddressV", S.AddressV);
451+
IO.mapOptional("AddressW", S.AddressW);
452+
IO.mapOptional("MipLODBias", S.MipLODBias);
453+
IO.mapOptional("MaxAnisotropy", S.MaxAnisotropy);
454+
IO.mapOptional("ComparisonFunc", S.ComparisonFunc);
455+
IO.mapOptional("BorderColor", S.BorderColor);
456+
IO.mapOptional("MinLOD", S.MinLOD);
457+
IO.mapOptional("MaxLOD", S.MaxLOD);
458+
IO.mapRequired("ShaderRegister", S.ShaderRegister);
459+
IO.mapRequired("RegisterSpace", S.RegisterSpace);
460+
IO.mapRequired("ShaderVisibility", S.ShaderVisibility);
461+
}
462+
424463
void MappingTraits<DXContainerYAML::Part>::mapping(IO &IO,
425464
DXContainerYAML::Part &P) {
426465
IO.mapRequired("Name", P.Name);

0 commit comments

Comments
 (0)