Skip to content

Commit 0abacfc

Browse files
author
joaosaffran
committed
adding support for Root Descriptors
1 parent 67cbfb9 commit 0abacfc

File tree

13 files changed

+514
-15
lines changed

13 files changed

+514
-15
lines changed

llvm/include/llvm/BinaryFormat/DXContainer.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ enum class RootElementFlag : uint32_t {
158158
#include "DXContainerConstants.def"
159159
};
160160

161+
#define ROOT_DESCRIPTOR_FLAG(Num, Val) Val = 1ull << Num,
162+
enum class RootDescriptorFlag : uint32_t {
163+
#include "DXContainerConstants.def"
164+
};
165+
161166
#define ROOT_PARAMETER(Val, Enum) Enum = Val,
162167
enum class RootParameterType : uint32_t {
163168
#include "DXContainerConstants.def"
@@ -422,7 +427,6 @@ struct SignatureElement {
422427

423428
static_assert(sizeof(SignatureElement) == 4 * sizeof(uint32_t),
424429
"PSV Signature elements must fit in 16 bytes.");
425-
426430
} // namespace v0
427431

428432
namespace v1 {
@@ -463,7 +467,6 @@ struct RuntimeInfo : public v0::RuntimeInfo {
463467
sys::swapByteOrder(GeomData.MaxVertexCount);
464468
}
465469
};
466-
467470
} // namespace v1
468471

469472
namespace v2 {
@@ -580,7 +583,28 @@ struct ProgramSignatureElement {
580583

581584
static_assert(sizeof(ProgramSignatureElement) == 32,
582585
"ProgramSignatureElement is misaligned");
586+
namespace RST0 {
587+
namespace v0 {
588+
struct RootDescriptor {
589+
uint32_t ShaderRegister;
590+
uint32_t RegisterSpace;
591+
void swapBytes() {
592+
sys::swapByteOrder(ShaderRegister);
593+
sys::swapByteOrder(RegisterSpace);
594+
}
595+
};
596+
} // namespace v0
583597

598+
namespace v1 {
599+
struct RootDescriptor : public v0::RootDescriptor {
600+
uint32_t Flags;
601+
void swapBytes() {
602+
v0::RootDescriptor::swapBytes();
603+
sys::swapByteOrder(Flags);
604+
}
605+
};
606+
} // namespace v1
607+
} // namespace RST0
584608
// following dx12 naming
585609
// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_root_constants
586610
struct RootConstants {

llvm/include/llvm/BinaryFormat/DXContainerConstants.def

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,24 @@ ROOT_ELEMENT_FLAG(11, SamplerHeapDirectlyIndexed)
7272
#undef ROOT_ELEMENT_FLAG
7373
#endif // ROOT_ELEMENT_FLAG
7474

75+
76+
// ROOT_ELEMENT_FLAG(bit offset for the flag, name).
77+
#ifdef ROOT_DESCRIPTOR_FLAG
78+
79+
ROOT_DESCRIPTOR_FLAG(0, NONE)
80+
ROOT_DESCRIPTOR_FLAG(1, DATA_VOLATILE)
81+
ROOT_DESCRIPTOR_FLAG(2, DATA_STATIC_WHILE_SET_AT_EXECUTE)
82+
ROOT_DESCRIPTOR_FLAG(3, DATA_STATIC)
83+
#undef ROOT_DESCRIPTOR_FLAG
84+
#endif // ROOT_DESCRIPTOR_FLAG
85+
86+
7587
#ifdef ROOT_PARAMETER
7688

7789
ROOT_PARAMETER(1, Constants32Bit)
90+
ROOT_PARAMETER(2, CBV)
91+
ROOT_PARAMETER(3, SRV)
92+
ROOT_PARAMETER(4, UAV)
7893
#undef ROOT_PARAMETER
7994
#endif // ROOT_PARAMETER
8095

llvm/include/llvm/MC/DXContainerRootSignature.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ struct RootParameter {
1919
dxbc::RootParameterHeader Header;
2020
union {
2121
dxbc::RootConstants Constants;
22+
dxbc::RST0::v0::RootDescriptor Descriptor_V10;
23+
dxbc::RST0::v1::RootDescriptor Descriptor_V11;
2224
};
2325
};
2426
struct RootSignatureDesc {
2527

2628
uint32_t Version = 2U;
2729
uint32_t Flags = 0U;
28-
uint32_t RootParameterOffset = 0U;
30+
uint32_t RootParameterOffset = 24U;
2931
uint32_t StaticSamplersOffset = 0u;
3032
uint32_t NumStaticSamplers = 0u;
3133
SmallVector<mcdxbc::RootParameter> Parameters;

llvm/include/llvm/Object/DXContainer.h

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ template <typename T> struct ViewArray {
120120
namespace DirectX {
121121
struct RootParameterView {
122122
const dxbc::RootParameterHeader &Header;
123+
uint32_t Version;
123124
StringRef ParamData;
124-
RootParameterView(const dxbc::RootParameterHeader &H, StringRef P)
125-
: Header(H), ParamData(P) {}
125+
RootParameterView(uint32_t V, const dxbc::RootParameterHeader &H, StringRef P)
126+
: Header(H), Version(V), ParamData(P) {}
126127

127128
template <typename T> Expected<T> readParameter() {
128129
T Struct;
@@ -149,6 +150,38 @@ struct RootConstantView : RootParameterView {
149150
}
150151
};
151152

153+
struct RootDescriptorView_V1_0 : RootParameterView {
154+
static bool classof(const RootParameterView *V) {
155+
return (V->Version == 1 &&
156+
(V->Header.ParameterType ==
157+
llvm::to_underlying(dxbc::RootParameterType::CBV) ||
158+
V->Header.ParameterType ==
159+
llvm::to_underlying(dxbc::RootParameterType::SRV) ||
160+
V->Header.ParameterType ==
161+
llvm::to_underlying(dxbc::RootParameterType::UAV)));
162+
}
163+
164+
llvm::Expected<dxbc::RST0::v0::RootDescriptor> read() {
165+
return readParameter<dxbc::RST0::v0::RootDescriptor>();
166+
}
167+
};
168+
169+
struct RootDescriptorView_V1_1 : RootParameterView {
170+
static bool classof(const RootParameterView *V) {
171+
return (V->Version == 2 &&
172+
(V->Header.ParameterType ==
173+
llvm::to_underlying(dxbc::RootParameterType::CBV) ||
174+
V->Header.ParameterType ==
175+
llvm::to_underlying(dxbc::RootParameterType::SRV) ||
176+
V->Header.ParameterType ==
177+
llvm::to_underlying(dxbc::RootParameterType::UAV)));
178+
}
179+
180+
llvm::Expected<dxbc::RST0::v1::RootDescriptor> read() {
181+
return readParameter<dxbc::RST0::v1::RootDescriptor>();
182+
}
183+
};
184+
152185
static Error parseFailed(const Twine &Msg) {
153186
return make_error<GenericBinaryError>(Msg.str(), object_error::parse_failed);
154187
}
@@ -192,6 +225,14 @@ class RootSignature {
192225
case dxbc::RootParameterType::Constants32Bit:
193226
DataSize = sizeof(dxbc::RootConstants);
194227
break;
228+
case dxbc::RootParameterType::CBV:
229+
case dxbc::RootParameterType::SRV:
230+
case dxbc::RootParameterType::UAV:
231+
if (Version == 1)
232+
DataSize = sizeof(dxbc::RST0::v0::RootDescriptor);
233+
else
234+
DataSize = sizeof(dxbc::RST0::v1::RootDescriptor);
235+
break;
195236
}
196237
size_t EndOfSectionByte = getNumStaticSamplers() == 0
197238
? PartData.size()
@@ -201,7 +242,7 @@ class RootSignature {
201242
return parseFailed("Reading structure out of file bounds");
202243

203244
StringRef Buff = PartData.substr(Header.ParameterOffset, DataSize);
204-
RootParameterView View = RootParameterView(Header, Buff);
245+
RootParameterView View = RootParameterView(Version, Header, Buff);
205246
return View;
206247
}
207248
};

llvm/include/llvm/ObjectYAML/DXContainerYAML.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,50 @@ struct ShaderHash {
7373
std::vector<llvm::yaml::Hex8> Digest;
7474
};
7575

76-
#define ROOT_ELEMENT_FLAG(Num, Val) bool Val = false;
77-
7876
struct RootConstantsYaml {
7977
uint32_t ShaderRegister;
8078
uint32_t RegisterSpace;
8179
uint32_t Num32BitValues;
8280
};
8381

82+
#define ROOT_DESCRIPTOR_FLAG(Num, Val) bool Val = false;
83+
struct RootDescriptorYaml {
84+
RootDescriptorYaml() = default;
85+
86+
uint32_t ShaderRegister;
87+
uint32_t RegisterSpace;
88+
89+
uint32_t getEncodedFlags() const;
90+
91+
#include "llvm/BinaryFormat/DXContainerConstants.def"
92+
};
93+
8494
struct RootParameterYamlDesc {
8595
uint32_t Type;
8696
uint32_t Visibility;
8797
uint32_t Offset;
98+
RootParameterYamlDesc(){};
99+
RootParameterYamlDesc(uint32_t T) : Type(T) {
100+
switch (T) {
101+
102+
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
103+
Constants = RootConstantsYaml();
104+
break;
105+
case llvm::to_underlying(dxbc::RootParameterType::CBV):
106+
case llvm::to_underlying(dxbc::RootParameterType::SRV):
107+
case llvm::to_underlying(dxbc::RootParameterType::UAV):
108+
Descriptor = RootDescriptorYaml();
109+
break;
110+
}
111+
}
88112

89113
union {
90114
RootConstantsYaml Constants;
115+
RootDescriptorYaml Descriptor;
91116
};
92117
};
93118

119+
#define ROOT_ELEMENT_FLAG(Num, Val) bool Val = false;
94120
struct RootSignatureYamlDesc {
95121
RootSignatureYamlDesc() = default;
96122

@@ -298,6 +324,10 @@ template <> struct MappingTraits<llvm::DXContainerYAML::RootConstantsYaml> {
298324
static void mapping(IO &IO, llvm::DXContainerYAML::RootConstantsYaml &C);
299325
};
300326

327+
template <> struct MappingTraits<llvm::DXContainerYAML::RootDescriptorYaml> {
328+
static void mapping(IO &IO, llvm::DXContainerYAML::RootDescriptorYaml &D);
329+
};
330+
301331
} // namespace yaml
302332

303333
} // namespace llvm

llvm/lib/MC/DXContainerRootSignature.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ size_t RootSignatureDesc::getSize() const {
3737
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
3838
Size += sizeof(dxbc::RootConstants);
3939
break;
40+
case llvm::to_underlying(dxbc::RootParameterType::CBV):
41+
case llvm::to_underlying(dxbc::RootParameterType::SRV):
42+
case llvm::to_underlying(dxbc::RootParameterType::UAV):
43+
if (Version == 1)
44+
Size += sizeof(dxbc::RST0::v0::RootDescriptor);
45+
else
46+
Size += sizeof(dxbc::RST0::v1::RootDescriptor);
47+
48+
break;
4049
}
4150
}
4251
return Size;
@@ -80,6 +89,22 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
8089
support::endian::write(BOS, P.Constants.Num32BitValues,
8190
llvm::endianness::little);
8291
break;
92+
case llvm::to_underlying(dxbc::RootParameterType::CBV):
93+
case llvm::to_underlying(dxbc::RootParameterType::SRV):
94+
case llvm::to_underlying(dxbc::RootParameterType::UAV):
95+
if (Version == 1) {
96+
support::endian::write(BOS, P.Descriptor_V10.ShaderRegister,
97+
llvm::endianness::little);
98+
support::endian::write(BOS, P.Descriptor_V10.RegisterSpace,
99+
llvm::endianness::little);
100+
} else {
101+
support::endian::write(BOS, P.Descriptor_V11.ShaderRegister,
102+
llvm::endianness::little);
103+
support::endian::write(BOS, P.Descriptor_V11.RegisterSpace,
104+
llvm::endianness::little);
105+
support::endian::write(BOS, P.Descriptor_V11.Flags,
106+
llvm::endianness::little);
107+
}
83108
}
84109
}
85110
assert(Storage.size() == getSize());

llvm/lib/ObjectYAML/DXContainerEmitter.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,23 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
283283
NewParam.Constants.Num32BitValues = Param.Constants.Num32BitValues;
284284
NewParam.Constants.RegisterSpace = Param.Constants.RegisterSpace;
285285
NewParam.Constants.ShaderRegister = Param.Constants.ShaderRegister;
286+
break;
287+
case llvm::to_underlying(dxbc::RootParameterType::SRV):
288+
case llvm::to_underlying(dxbc::RootParameterType::UAV):
289+
case llvm::to_underlying(dxbc::RootParameterType::CBV):
290+
if (RS.Version == 1) {
291+
NewParam.Descriptor_V10.RegisterSpace =
292+
Param.Descriptor.RegisterSpace;
293+
NewParam.Descriptor_V10.ShaderRegister =
294+
Param.Descriptor.ShaderRegister;
295+
} else {
296+
NewParam.Descriptor_V11.RegisterSpace =
297+
Param.Descriptor.RegisterSpace;
298+
NewParam.Descriptor_V11.ShaderRegister =
299+
Param.Descriptor.ShaderRegister;
300+
NewParam.Descriptor_V11.Flags = Param.Descriptor.getEncodedFlags();
301+
}
302+
286303
break;
287304
}
288305

0 commit comments

Comments
 (0)