Skip to content

Commit ca58712

Browse files
author
joaosaffran
committed
adding support for root constants in dxcontainer for ob2jyaml and yaml2obj tools
1 parent d59738d commit ca58712

File tree

11 files changed

+197
-58
lines changed

11 files changed

+197
-58
lines changed

llvm/include/llvm/BinaryFormat/DXContainer.h

Lines changed: 47 additions & 38 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,44 +64,6 @@ struct ShaderHash {
6364
void swapBytes() { sys::swapByteOrder(Flags); }
6465
};
6566

66-
#define ROOT_PARAMETER(RootParameter) RootParameter,
67-
enum class RootParameterType {
68-
#include "DXContainerConstants.def"
69-
};
70-
71-
#define SHADER_VISIBILITY(ShaderVisibility) ShaderVisibility,
72-
enum class ShaderVisibilityFlag {
73-
#include "DXContainerConstants.def"
74-
};
75-
76-
struct RootConstants {
77-
uint32_t ShaderRegister;
78-
uint32_t RegisterSpace;
79-
uint32_t Num32BitValues;
80-
};
81-
82-
struct RootParameter {
83-
RootParameterType ParameterType;
84-
union {
85-
RootConstants Constants;
86-
};
87-
ShaderVisibilityFlag ShaderVisibility;
88-
};
89-
90-
struct RootSignatureDesc {
91-
uint32_t Size;
92-
uint32_t Version;
93-
uint32_t Flags;
94-
uint32_t NumParameters;
95-
RootParameter *Parameters;
96-
97-
void swapBytes() {
98-
sys::swapByteOrder(Size);
99-
sys::swapByteOrder(Version);
100-
sys::swapByteOrder(Flags);
101-
}
102-
};
103-
10467
struct ContainerVersion {
10568
uint16_t Major;
10669
uint16_t Minor;
@@ -195,6 +158,50 @@ enum class RootElementFlag : uint32_t {
195158
#include "DXContainerConstants.def"
196159
};
197160

161+
#define ROOT_PARAMETER(Val, Enum) Enum = Val,
162+
enum class RootParameterType : uint8_t {
163+
#include "DXContainerConstants.def"
164+
};
165+
166+
#define SHADER_VISIBILITY(Val, Enum) Enum = Val,
167+
enum class ShaderVisibilityFlag : uint8_t {
168+
#include "DXContainerConstants.def"
169+
};
170+
171+
struct RootConstants {
172+
uint32_t ShaderRegister;
173+
uint32_t RegisterSpace;
174+
uint32_t Num32BitValues;
175+
176+
void swapBytes() {
177+
sys::swapByteOrder(ShaderRegister);
178+
sys::swapByteOrder(RegisterSpace);
179+
sys::swapByteOrder(Num32BitValues);
180+
}
181+
};
182+
183+
struct RootParameter {
184+
RootParameterType ParameterType;
185+
union {
186+
RootConstants Constants;
187+
};
188+
ShaderVisibilityFlag ShaderVisibility;
189+
190+
void swapBytes() {
191+
switch (ParameterType) {
192+
193+
case RootParameterType::Constants32Bit:
194+
Constants.swapBytes();
195+
break;
196+
case RootParameterType::DescriptorTable:
197+
case RootParameterType::CBV:
198+
case RootParameterType::SRV:
199+
case RootParameterType::UAV:
200+
break;
201+
}
202+
}
203+
};
204+
198205
PartType parsePartType(StringRef S);
199206

200207
struct VertexPSVInfo {
@@ -536,6 +543,8 @@ enum class SigComponentType : uint32_t {
536543
};
537544

538545
ArrayRef<EnumEntry<SigComponentType>> getSigComponentTypes();
546+
ArrayRef<EnumEntry<RootParameterType>> getRootParameterTypes();
547+
ArrayRef<EnumEntry<ShaderVisibilityFlag>> getShaderVisibilityFlags();
539548

540549
struct ProgramSignatureHeader {
541550
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

@@ -17,6 +19,7 @@ namespace mcdxbc {
1719
struct RootSignatureHeader {
1820
uint32_t Version;
1921
uint32_t Flags;
22+
SmallVector<dxbc::RootParameter> Parameters;
2023

2124
void swapBytes();
2225
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,11 +119,16 @@ 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 Version;
125129
uint32_t Flags;
130+
uint32_t NParameters;
131+
ParametersArray Parameters;
126132

127133
public:
128134
RootSignature(StringRef Data) : Data(Data) {}
@@ -134,6 +140,10 @@ class RootSignature {
134140
uint32_t getVersion() const { return Version; }
135141

136142
uint32_t getFlags() const { return Flags; }
143+
144+
uint32_t getNParameters() const { return NParameters; }
145+
146+
ParametersArray getParameters() const { return Parameters; }
137147
};
138148

139149
class PSVRuntimeInfo {

llvm/include/llvm/ObjectYAML/DXContainerYAML.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::ResourceBindInfo)
190190
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::SignatureElement)
191191
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::PSVInfo::MaskVector)
192192
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::SignatureParameter)
193+
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dxbc::RootParameter)
193194
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::SemanticKind)
194195
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::ComponentType)
195196
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::InterpolationMode)
@@ -198,6 +199,8 @@ LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::ResourceKind)
198199
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::D3DSystemValue)
199200
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::SigComponentType)
200201
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::SigMinPrecision)
202+
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::RootParameterType)
203+
LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::ShaderVisibilityFlag)
201204

202205
namespace llvm {
203206

@@ -262,6 +265,14 @@ template <> struct MappingTraits<DXContainerYAML::RootSignatureDesc> {
262265
DXContainerYAML::RootSignatureDesc &RootSignature);
263266
};
264267

268+
template <> struct MappingTraits<dxbc::RootParameter> {
269+
static void mapping(IO &IO, dxbc::RootParameter &RootParameter);
270+
};
271+
272+
template <> struct MappingTraits<dxbc::RootConstants> {
273+
static void mapping(IO &IO, dxbc::RootConstants &RootConstants);
274+
};
275+
265276
} // namespace yaml
266277

267278
} // 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,17 +7,28 @@
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, Version, llvm::endianness::little);
2223
support::endian::write(OS, Flags, llvm::endianness::little);
24+
support::endian::write(OS, ParamsSize, llvm::endianness::little);
25+
26+
if (Parameters.size() > 0) {
27+
uint32_t BindingSize = sizeof(dxbc::RootParameter);
28+
29+
support::endian::write(OS, BindingSize, llvm::endianness::little);
30+
31+
for (const auto &Param : Parameters)
32+
OS.write(reinterpret_cast<const char *>(&Param), BindingSize);
33+
}
2334
}

llvm/lib/Object/DXContainer.cpp

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

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

llvm/lib/ObjectYAML/DXContainerEmitter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
269269
mcdxbc::RootSignatureHeader Header;
270270
Header.Version = P.RootSignature->Version;
271271
Header.Flags = P.RootSignature->getEncodedFlags();
272+
Header.Parameters = P.RootSignature->Parameters;
272273

273274
Header.write(OS);
274275
break;

0 commit comments

Comments
 (0)