Skip to content

Commit b02e7fe

Browse files
author
joaosaffran
committed
adding support for root constants in dxcontainer for ob2jyaml and yaml2obj tools
1 parent 33bb8cc commit b02e7fe

File tree

11 files changed

+171
-24
lines changed

11 files changed

+171
-24
lines changed

llvm/include/llvm/BinaryFormat/DXContainer.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/Support/SwapByteOrder.h"
1818
#include "llvm/TargetParser/Triple.h"
1919

20+
#include <cstdint>
2021
#include <stdint.h>
2122

2223
namespace llvm {
@@ -63,6 +64,7 @@ struct ShaderHash {
6364
void swapBytes() { sys::swapByteOrder(Flags); }
6465
};
6566

67+
<<<<<<< HEAD
6668
struct RootSignatureDesc {
6769
uint32_t Size;
6870
uint32_t Flags;
@@ -73,6 +75,8 @@ struct RootSignatureDesc {
7375
}
7476
};
7577

78+
=======
79+
>>>>>>> b1b967db8d32 (adding support for root constants in dxcontainer for ob2jyaml and yaml2obj tools)
7680
struct ContainerVersion {
7781
uint16_t Major;
7882
uint16_t Minor;
@@ -167,6 +171,50 @@ enum class RootElementFlag : uint32_t {
167171
#include "DXContainerConstants.def"
168172
};
169173

174+
#define ROOT_PARAMETER(Val, Enum) Enum = Val,
175+
enum class RootParameterType : uint8_t {
176+
#include "DXContainerConstants.def"
177+
};
178+
179+
#define SHADER_VISIBILITY(Val, Enum) Enum = Val,
180+
enum class ShaderVisibilityFlag : uint8_t {
181+
#include "DXContainerConstants.def"
182+
};
183+
184+
struct RootConstants {
185+
uint32_t ShaderRegister;
186+
uint32_t RegisterSpace;
187+
uint32_t Num32BitValues;
188+
189+
void swapBytes() {
190+
sys::swapByteOrder(ShaderRegister);
191+
sys::swapByteOrder(RegisterSpace);
192+
sys::swapByteOrder(Num32BitValues);
193+
}
194+
};
195+
196+
struct RootParameter {
197+
RootParameterType ParameterType;
198+
union {
199+
RootConstants Constants;
200+
};
201+
ShaderVisibilityFlag ShaderVisibility;
202+
203+
void swapBytes() {
204+
switch (ParameterType) {
205+
206+
case RootParameterType::Constants32Bit:
207+
Constants.swapBytes();
208+
break;
209+
case RootParameterType::DescriptorTable:
210+
case RootParameterType::CBV:
211+
case RootParameterType::SRV:
212+
case RootParameterType::UAV:
213+
break;
214+
}
215+
}
216+
};
217+
170218
PartType parsePartType(StringRef S);
171219

172220
struct VertexPSVInfo {
@@ -508,6 +556,8 @@ enum class SigComponentType : uint32_t {
508556
};
509557

510558
ArrayRef<EnumEntry<SigComponentType>> getSigComponentTypes();
559+
ArrayRef<EnumEntry<RootParameterType>> getRootParameterTypes();
560+
ArrayRef<EnumEntry<ShaderVisibilityFlag>> getShaderVisibilityFlags();
511561

512562
struct ProgramSignatureHeader {
513563
uint32_t ParamCount;

llvm/include/llvm/BinaryFormat/DXContainerConstants.def

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,25 @@ SHADER_FEATURE_FLAG(31, 36, NextUnusedBit, "Next reserved shader flag bit (not a
5555

5656
#ifdef ROOT_PARAMETER
5757

58-
ROOT_PARAMETER(DescriptorTable)
59-
ROOT_PARAMETER(Constants32Bit)
60-
ROOT_PARAMETER(CBV)
61-
ROOT_PARAMETER(SRV)
62-
ROOT_PARAMETER(UAV)
58+
ROOT_PARAMETER(0, DescriptorTable)
59+
ROOT_PARAMETER(1, Constants32Bit)
60+
ROOT_PARAMETER(2, CBV)
61+
ROOT_PARAMETER(3, SRV)
62+
ROOT_PARAMETER(4, UAV)
6363
#undef ROOT_PARAMETER
6464
#endif // ROOT_PARAMETER
6565

6666

6767
#ifdef SHADER_VISIBILITY
6868

69-
SHADER_VISIBILITY(All)
70-
SHADER_VISIBILITY(Vertex)
71-
SHADER_VISIBILITY(Hull)
72-
SHADER_VISIBILITY(Domain)
73-
SHADER_VISIBILITY(Geometry)
74-
SHADER_VISIBILITY(Pixel)
75-
SHADER_VISIBILITY(Amplification)
76-
SHADER_VISIBILITY(Mesh)
69+
SHADER_VISIBILITY(0, All)
70+
SHADER_VISIBILITY(1, Vertex)
71+
SHADER_VISIBILITY(2, Hull)
72+
SHADER_VISIBILITY(3, Domain)
73+
SHADER_VISIBILITY(4, Geometry)
74+
SHADER_VISIBILITY(5, Pixel)
75+
SHADER_VISIBILITY(6, Amplification)
76+
SHADER_VISIBILITY(7, Mesh)
7777
#undef SHADER_VISIBILITY
7878
#endif // SHADER_VISIBILITY
7979

llvm/include/llvm/MC/DXContainerRootSignature.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "llvm/ADT/SmallVector.h"
10+
#include "llvm/BinaryFormat/DXContainer.h"
911
#include <cstdint>
1012
#include <limits>
1113

@@ -16,6 +18,7 @@ class raw_ostream;
1618
namespace mcdxbc {
1719
struct RootSignatureHeader {
1820
uint32_t Flags;
21+
SmallVector<dxbc::RootParameter> Parameters;
1922

2023
void swapBytes();
2124
void write(raw_ostream &OS);

llvm/include/llvm/Object/DXContainer.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/Support/MemoryBufferRef.h"
2323
#include "llvm/TargetParser/Triple.h"
2424
#include <array>
25+
#include <cstdint>
2526
#include <variant>
2627

2728
namespace llvm {
@@ -118,10 +119,15 @@ template <typename T> struct ViewArray {
118119
namespace DirectX {
119120

120121
class RootSignature {
122+
123+
using ParametersArray = ViewArray<dxbc::RootParameter>;
124+
121125
private:
122126
StringRef Data;
123127
uint32_t Size;
124128
uint32_t Flags;
129+
uint32_t NParameters;
130+
ParametersArray Parameters;
125131

126132
public:
127133
RootSignature(StringRef Data) : Data(Data) {}
@@ -131,6 +137,10 @@ class RootSignature {
131137
uint32_t getSize() const { return Size; }
132138

133139
uint32_t getFlags() const { return Flags; }
140+
141+
uint32_t getNParameters() const { return NParameters; }
142+
143+
ParametersArray getParameters() const { return Parameters; }
134144
};
135145

136146
class PSVRuntimeInfo {

llvm/include/llvm/ObjectYAML/DXContainerYAML.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::ResourceBindInfo)
187187
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::SignatureElement)
188188
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::PSVInfo::MaskVector)
189189
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::SignatureParameter)
190+
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dxbc::RootParameter)
190191
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::SemanticKind)
191192
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::ComponentType)
192193
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::InterpolationMode)
@@ -195,6 +196,8 @@ LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::ResourceKind)
195196
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::D3DSystemValue)
196197
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::SigComponentType)
197198
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::SigMinPrecision)
199+
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::RootParameterType)
200+
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::ShaderVisibilityFlag)
198201

199202
namespace llvm {
200203

@@ -259,6 +262,14 @@ template <> struct MappingTraits<DXContainerYAML::RootSignatureDesc> {
259262
DXContainerYAML::RootSignatureDesc &RootSignature);
260263
};
261264

265+
template <> struct MappingTraits<dxbc::RootParameter> {
266+
static void mapping(IO &IO, dxbc::RootParameter &RootParameter);
267+
};
268+
269+
template <> struct MappingTraits<dxbc::RootConstants> {
270+
static void mapping(IO &IO, dxbc::RootConstants &RootConstants);
271+
};
272+
262273
} // namespace yaml
263274

264275
} // namespace llvm

llvm/lib/BinaryFormat/DXContainer.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ ArrayRef<EnumEntry<SigComponentType>> dxbc::getSigComponentTypes() {
6060
return ArrayRef(SigComponentTypes);
6161
}
6262

63+
#define SHADER_VISIBILITY(Val, Enum) {#Enum, ShaderVisibilityFlag::Enum},
64+
65+
static const EnumEntry<ShaderVisibilityFlag> ShaderVisibilityFlags[] = {
66+
#include "llvm/BinaryFormat/DXContainerConstants.def"
67+
};
68+
69+
ArrayRef<EnumEntry<ShaderVisibilityFlag>> dxbc::getShaderVisibilityFlags() {
70+
return ArrayRef(ShaderVisibilityFlags);
71+
}
72+
73+
#define ROOT_PARAMETER(Val, Enum) {#Enum, RootParameterType::Enum},
74+
75+
static const EnumEntry<RootParameterType> RootParameterTypes[] = {
76+
#include "llvm/BinaryFormat/DXContainerConstants.def"
77+
};
78+
79+
ArrayRef<EnumEntry<RootParameterType>> dxbc::getRootParameterTypes() {
80+
return ArrayRef(RootParameterTypes);
81+
}
82+
6383
#define SEMANTIC_KIND(Val, Enum) {#Enum, PSV::SemanticKind::Enum},
6484

6585
static const EnumEntry<PSV::SemanticKind> SemanticKindNames[] = {

llvm/lib/MC/DXContainerRootSignature.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,27 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/MC/DXContainerRootSignature.h"
10+
#include "llvm/BinaryFormat/DXContainer.h"
1011
#include "llvm/Support/EndianStream.h"
11-
#include "llvm/Support/SwapByteOrder.h"
12-
#include <iterator>
12+
#include <cstdint>
1313

1414
using namespace llvm;
1515
using namespace llvm::mcdxbc;
1616

1717
void RootSignatureHeader::write(raw_ostream &OS) {
1818

19-
uint32_t SizeInfo = sizeof(this);
19+
uint32_t SizeInfo = sizeof(RootSignatureHeader);
20+
uint32_t ParamsSize = Parameters.size();
2021
support::endian::write(OS, SizeInfo, llvm::endianness::little);
2122
support::endian::write(OS, Flags, llvm::endianness::little);
23+
support::endian::write(OS, ParamsSize, llvm::endianness::little);
24+
25+
if (Parameters.size() > 0) {
26+
uint32_t BindingSize = sizeof(dxbc::RootParameter);
27+
28+
support::endian::write(OS, BindingSize, llvm::endianness::little);
29+
30+
for (const auto &Param : Parameters)
31+
OS.write(reinterpret_cast<const char *>(&Param), BindingSize);
32+
}
2233
}

llvm/lib/Object/DXContainer.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,26 @@ Error DirectX::RootSignature::parse() {
251251
Flags = support::endian::read<uint32_t, llvm::endianness::little>(Current);
252252
Current += sizeof(uint32_t);
253253

254+
NParameters =
255+
support::endian::read<uint32_t, llvm::endianness::little>(Current);
256+
Current += sizeof(uint32_t);
257+
258+
if (NParameters > 0) {
259+
260+
Parameters.Stride =
261+
support::endian::read<uint32_t, llvm::endianness::little>(Current);
262+
Current += sizeof(uint32_t);
263+
264+
size_t BindingDataSize = Parameters.Stride * NParameters;
265+
Parameters.Data = Data.substr(Current - Data.begin(), BindingDataSize);
266+
267+
if (Parameters.Data.size() < BindingDataSize)
268+
return parseFailed(
269+
"Resource binding data extends beyond the bounds of the part");
270+
271+
Current += BindingDataSize;
272+
}
273+
254274
return Error::success();
255275
}
256276

llvm/lib/ObjectYAML/DXContainerEmitter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
268268

269269
mcdxbc::RootSignatureHeader Header;
270270
Header.Flags = P.RootSignature->getEncodedFlags();
271+
Header.Parameters = P.RootSignature->Parameters;
271272

272273
Header.write(OS);
273274
break;

llvm/lib/ObjectYAML/DXContainerYAML.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ DXContainerYAML::ShaderFeatureFlags::ShaderFeatureFlags(uint64_t FlagData) {
3333
DXContainerYAML::RootSignatureDesc::RootSignatureDesc(
3434
const object::DirectX::RootSignature &Data)
3535
: Size(Data.getSize()) {
36+
: Size(Data.getSize()), Version(Data.getVersion()),
37+
NumParameters(Data.getNParameters()) {
3638
uint32_t Flags = Data.getFlags();
3739
#define ROOT_ELEMENT_FLAG(Num, Val, Str) \
3840
Val = (Flags & (uint32_t)dxbc::RootElementFlag::Val) > 0;
@@ -210,6 +212,9 @@ void MappingTraits<DXContainerYAML::Signature>::mapping(
210212
void MappingTraits<DXContainerYAML::RootSignatureDesc>::mapping(
211213
IO &IO, DXContainerYAML::RootSignatureDesc &S) {
212214
IO.mapRequired("Size", S.Size);
215+
IO.mapRequired("Version", S.Version);
216+
IO.mapRequired("NumParameters", S.NumParameters);
217+
IO.mapRequired("Parameters", S.Parameters);
213218
#define ROOT_ELEMENT_FLAG(Num, Val, Str) IO.mapOptional(#Val, S.Val, false);
214219
#include "llvm/BinaryFormat/DXContainerConstants.def"
215220
}

0 commit comments

Comments
 (0)