Skip to content

Commit 0a1c7f9

Browse files
inbelicsvkeerthy
authored andcommitted
[HLSL][RootSignature] Implement serialization of RootConstants and RootFlags (#141130)
- Implements serialization of the currently completely defined `RootElement`s, namely `RootConstants` and `RootFlags` - Adds unit testing for the serialization methods Resolves: #138190 and #138192
1 parent b4e8e4a commit 0a1c7f9

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ enum class RootFlags : uint32_t {
4646
ValidFlags = 0x00000fff
4747
};
4848

49+
raw_ostream &operator<<(raw_ostream &OS, const RootFlags &Flags);
50+
4951
enum class RootDescriptorFlags : unsigned {
5052
None = 0,
5153
DataVolatile = 0x2,
@@ -93,6 +95,8 @@ struct RootConstants {
9395
ShaderVisibility Visibility = ShaderVisibility::All;
9496
};
9597

98+
raw_ostream &operator<<(raw_ostream &OS, const RootConstants &Constants);
99+
96100
enum class DescriptorType : uint8_t { SRV = 0, UAV, CBuffer };
97101
// Models RootDescriptor : CBV | SRV | UAV, by collecting like parameters
98102
struct RootDescriptor {

llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,79 @@ static raw_ostream &operator<<(raw_ostream &OS,
132132
return OS;
133133
}
134134

135+
raw_ostream &operator<<(raw_ostream &OS, const RootFlags &Flags) {
136+
OS << "RootFlags(";
137+
bool FlagSet = false;
138+
unsigned Remaining = llvm::to_underlying(Flags);
139+
while (Remaining) {
140+
unsigned Bit = 1u << llvm::countr_zero(Remaining);
141+
if (Remaining & Bit) {
142+
if (FlagSet)
143+
OS << " | ";
144+
145+
switch (static_cast<RootFlags>(Bit)) {
146+
case RootFlags::AllowInputAssemblerInputLayout:
147+
OS << "AllowInputAssemblerInputLayout";
148+
break;
149+
case RootFlags::DenyVertexShaderRootAccess:
150+
OS << "DenyVertexShaderRootAccess";
151+
break;
152+
case RootFlags::DenyHullShaderRootAccess:
153+
OS << "DenyHullShaderRootAccess";
154+
break;
155+
case RootFlags::DenyDomainShaderRootAccess:
156+
OS << "DenyDomainShaderRootAccess";
157+
break;
158+
case RootFlags::DenyGeometryShaderRootAccess:
159+
OS << "DenyGeometryShaderRootAccess";
160+
break;
161+
case RootFlags::DenyPixelShaderRootAccess:
162+
OS << "DenyPixelShaderRootAccess";
163+
break;
164+
case RootFlags::AllowStreamOutput:
165+
OS << "AllowStreamOutput";
166+
break;
167+
case RootFlags::LocalRootSignature:
168+
OS << "LocalRootSignature";
169+
break;
170+
case RootFlags::DenyAmplificationShaderRootAccess:
171+
OS << "DenyAmplificationShaderRootAccess";
172+
break;
173+
case RootFlags::DenyMeshShaderRootAccess:
174+
OS << "DenyMeshShaderRootAccess";
175+
break;
176+
case RootFlags::CBVSRVUAVHeapDirectlyIndexed:
177+
OS << "CBVSRVUAVHeapDirectlyIndexed";
178+
break;
179+
case RootFlags::SamplerHeapDirectlyIndexed:
180+
OS << "SamplerHeapDirectlyIndexed";
181+
break;
182+
default:
183+
OS << "invalid: " << Bit;
184+
break;
185+
}
186+
187+
FlagSet = true;
188+
}
189+
Remaining &= ~Bit;
190+
}
191+
192+
if (!FlagSet)
193+
OS << "None";
194+
195+
OS << ")";
196+
197+
return OS;
198+
}
199+
200+
raw_ostream &operator<<(raw_ostream &OS, const RootConstants &Constants) {
201+
OS << "RootConstants(num32BitConstants = " << Constants.Num32BitConstants
202+
<< ", " << Constants.Reg << ", space = " << Constants.Space
203+
<< ", visibility = " << Constants.Visibility << ")";
204+
205+
return OS;
206+
}
207+
135208
raw_ostream &operator<<(raw_ostream &OS, const DescriptorTable &Table) {
136209
OS << "DescriptorTable(numClauses = " << Table.NumClauses
137210
<< ", visibility = " << Table.Visibility << ")";

llvm/unittests/Frontend/HLSLRootSignatureDumpTest.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,73 @@ TEST(HLSLRootSignatureTest, DescriptorTableDump) {
108108
EXPECT_EQ(Out, Expected);
109109
}
110110

111+
TEST(HLSLRootSignatureTest, DefaultRootConstantsDump) {
112+
RootConstants Constants;
113+
Constants.Num32BitConstants = 1;
114+
Constants.Reg = {RegisterType::BReg, 3};
115+
116+
std::string Out;
117+
llvm::raw_string_ostream OS(Out);
118+
OS << Constants;
119+
OS.flush();
120+
121+
std::string Expected = "RootConstants(num32BitConstants = 1, b3, space = 0, "
122+
"visibility = All)";
123+
EXPECT_EQ(Out, Expected);
124+
}
125+
126+
TEST(HLSLRootSignatureTest, SetRootConstantsDump) {
127+
RootConstants Constants;
128+
Constants.Num32BitConstants = 983;
129+
Constants.Reg = {RegisterType::BReg, 34593};
130+
Constants.Space = 7;
131+
Constants.Visibility = ShaderVisibility::Pixel;
132+
133+
std::string Out;
134+
llvm::raw_string_ostream OS(Out);
135+
OS << Constants;
136+
OS.flush();
137+
138+
std::string Expected = "RootConstants(num32BitConstants = 983, b34593, "
139+
"space = 7, visibility = Pixel)";
140+
EXPECT_EQ(Out, Expected);
141+
}
142+
143+
TEST(HLSLRootSignatureTest, NoneRootFlagsDump) {
144+
RootFlags Flags = RootFlags::None;
145+
146+
std::string Out;
147+
llvm::raw_string_ostream OS(Out);
148+
OS << Flags;
149+
OS.flush();
150+
151+
std::string Expected = "RootFlags(None)";
152+
EXPECT_EQ(Out, Expected);
153+
}
154+
155+
TEST(HLSLRootSignatureTest, AllRootFlagsDump) {
156+
RootFlags Flags = RootFlags::ValidFlags;
157+
158+
std::string Out;
159+
llvm::raw_string_ostream OS(Out);
160+
OS << Flags;
161+
OS.flush();
162+
163+
std::string Expected = "RootFlags("
164+
"AllowInputAssemblerInputLayout | "
165+
"DenyVertexShaderRootAccess | "
166+
"DenyHullShaderRootAccess | "
167+
"DenyDomainShaderRootAccess | "
168+
"DenyGeometryShaderRootAccess | "
169+
"DenyPixelShaderRootAccess | "
170+
"AllowStreamOutput | "
171+
"LocalRootSignature | "
172+
"DenyAmplificationShaderRootAccess | "
173+
"DenyMeshShaderRootAccess | "
174+
"CBVSRVUAVHeapDirectlyIndexed | "
175+
"SamplerHeapDirectlyIndexed)";
176+
177+
EXPECT_EQ(Out, Expected);
178+
}
179+
111180
} // namespace

0 commit comments

Comments
 (0)