Skip to content

Commit 4ff986a

Browse files
authored
[DirectX] Updating DXContainer logic to read version 1.2 of static samplers (#160184)
This PR is updating `Object/DXContainer.h` so that we can read data from root signature version 1.2, which adds flags into static samplers.
1 parent 65d730b commit 4ff986a

File tree

5 files changed

+127
-6
lines changed

5 files changed

+127
-6
lines changed

llvm/include/llvm/Object/DXContainer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ class RootSignature {
228228
uint32_t Flags;
229229
ViewArray<dxbc::RTS0::v1::RootParameterHeader> ParametersHeaders;
230230
StringRef PartData;
231-
ViewArray<dxbc::RTS0::v1::StaticSampler> StaticSamplers;
231+
ViewArray<dxbc::RTS0::v3::StaticSampler> StaticSamplers;
232232

233233
using param_header_iterator =
234234
ViewArray<dxbc::RTS0::v1::RootParameterHeader>::iterator;
235-
using samplers_iterator = ViewArray<dxbc::RTS0::v1::StaticSampler>::iterator;
235+
using samplers_iterator = ViewArray<dxbc::RTS0::v3::StaticSampler>::iterator;
236236

237237
public:
238238
RootSignature(StringRef PD) : PartData(PD) {}

llvm/lib/Object/DXContainer.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,13 @@ 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));
279+
StaticSamplers.Stride = (Version <= 2)
280+
? sizeof(dxbc::RTS0::v1::StaticSampler)
281+
: sizeof(dxbc::RTS0::v3::StaticSampler);
282+
283+
StaticSamplers.Data = PartData.substr(StaticSamplersOffset,
284+
static_cast<size_t>(NumStaticSamplers) *
285+
StaticSamplers.Stride);
283286

284287
return Error::success();
285288
}

llvm/lib/ObjectYAML/DXContainerYAML.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ DXContainerYAML::RootSignatureYamlDesc::create(
209209
NewS.RegisterSpace = S.RegisterSpace;
210210
NewS.ShaderVisibility = dxbc::ShaderVisibility(S.ShaderVisibility);
211211

212+
if (Version > 2) {
213+
#define STATIC_SAMPLER_FLAG(Num, Enum, Flag) \
214+
NewS.Enum = (S.Flags & llvm::to_underlying(dxbc::StaticSamplerFlags::Enum));
215+
#include "llvm/BinaryFormat/DXContainerConstants.def"
216+
}
212217
RootSigDesc.StaticSamplers.push_back(NewS);
213218
}
214219

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# RUN: yaml2obj %s | obj2yaml | FileCheck %s
2+
3+
--- !dxcontainer
4+
Header:
5+
Hash: [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
6+
0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
7+
Version:
8+
Major: 1
9+
Minor: 0
10+
PartCount: 1
11+
PartOffsets: [ 60 ]
12+
Parts:
13+
- Name: RTS0
14+
Size: 80
15+
RootSignature:
16+
Version: 3
17+
NumRootParameters: 0
18+
RootParametersOffset: 24
19+
NumStaticSamplers: 1
20+
StaticSamplersOffset: 24
21+
Parameters: []
22+
Samplers:
23+
- Filter: MinLinearMagMipPoint
24+
AddressU: Wrap
25+
AddressV: Mirror
26+
AddressW: MirrorOnce
27+
MipLODBias: 1.23
28+
MaxAnisotropy: 20
29+
ComparisonFunc: LessEqual
30+
BorderColor: TransparentBlack
31+
MinLOD: 4.56
32+
MaxLOD: 8.90
33+
ShaderRegister: 31
34+
RegisterSpace: 32
35+
ShaderVisibility: Mesh
36+
SAMPLER_FLAG_UINT_BORDER_COLOR: true
37+
AllowInputAssemblerInputLayout: true
38+
DenyGeometryShaderRootAccess: true
39+
40+
#CHECK: - Name: RTS0
41+
#CHECK-NEXT: Size: 80
42+
#CHECK-NEXT: RootSignature:
43+
#CHECK-NEXT: Version: 3
44+
#CHECK-NEXT: NumRootParameters: 0
45+
#CHECK-NEXT: RootParametersOffset: 24
46+
#CHECK-NEXT: NumStaticSamplers: 1
47+
#CHECK-NEXT: StaticSamplersOffset: 24
48+
#CHECK-NEXT: Parameters: []
49+
#CHECK-NEXT: Samplers:
50+
#CHECK-NEXT: - Filter: MinLinearMagMipPoint
51+
#CHECK-NEXT: AddressU: Wrap
52+
#CHECK-NEXT: AddressV: Mirror
53+
#CHECK-NEXT: AddressW: MirrorOnce
54+
#CHECK-NEXT: MipLODBias: 1.23
55+
#CHECK-NEXT: MaxAnisotropy: 20
56+
#CHECK-NEXT: ComparisonFunc: LessEqual
57+
#CHECK-NEXT: BorderColor: TransparentBlack
58+
#CHECK-NEXT: MinLOD: 4.56
59+
#CHECK-NEXT: MaxLOD: 8.9
60+
#CHECK-NEXT: ShaderRegister: 31
61+
#CHECK-NEXT: RegisterSpace: 32
62+
#CHECK-NEXT: ShaderVisibility: Mesh
63+
#CHECK-NEXT: SAMPLER_FLAG_UINT_BORDER_COLOR: true
64+
#CHECK-NEXT: AllowInputAssemblerInputLayout: true
65+
#CHECK-NEXT: DenyGeometryShaderRootAccess: true

llvm/unittests/Object/DXContainerTest.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,4 +1200,52 @@ TEST(RootSignature, ParseStaticSamplers) {
12001200
ASSERT_EQ(Sampler.RegisterSpace, 32u);
12011201
ASSERT_EQ(Sampler.ShaderVisibility, 7u);
12021202
}
1203+
{
1204+
// this is testing static sampler parsing for root signature version 1.2,
1205+
// it changes: the version number, the size of root signature being emitted
1206+
// and the values for flag fields.
1207+
uint8_t Buffer[] = {
1208+
0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1209+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1210+
0x90, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
1211+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1212+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1213+
0x52, 0x54, 0x53, 0x30, 0x4c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
1214+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1215+
0x18, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
1216+
0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
1217+
0xa4, 0x70, 0x9d, 0x3f, 0x14, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
1218+
0x00, 0x00, 0x00, 0x00, 0x85, 0xeb, 0x91, 0x40, 0x66, 0x66, 0x0e, 0x41,
1219+
0x1f, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
1220+
0x01, 0x00, 0x00, 0x00};
1221+
DXContainer C =
1222+
llvm::cantFail(DXContainer::create(getMemoryBuffer<148>(Buffer)));
1223+
1224+
auto MaybeRS = C.getRootSignature();
1225+
ASSERT_TRUE(MaybeRS.has_value());
1226+
const auto &RS = MaybeRS.value();
1227+
ASSERT_EQ(RS.getVersion(), 3U);
1228+
ASSERT_EQ(RS.getNumParameters(), 0U);
1229+
ASSERT_EQ(RS.getRootParametersOffset(), 0U);
1230+
ASSERT_EQ(RS.getNumStaticSamplers(), 1U);
1231+
ASSERT_EQ(RS.getStaticSamplersOffset(), 24U);
1232+
ASSERT_EQ(RS.getFlags(), 17U);
1233+
1234+
auto Sampler = *RS.samplers().begin();
1235+
1236+
ASSERT_EQ(Sampler.Filter, 10U);
1237+
ASSERT_EQ(Sampler.AddressU, 1U);
1238+
ASSERT_EQ(Sampler.AddressV, 2U);
1239+
ASSERT_EQ(Sampler.AddressW, 5U);
1240+
ASSERT_FLOAT_EQ(Sampler.MipLODBias, 1.23F);
1241+
ASSERT_EQ(Sampler.MaxAnisotropy, 20U);
1242+
ASSERT_EQ(Sampler.ComparisonFunc, 4U);
1243+
ASSERT_EQ(Sampler.BorderColor, 0U);
1244+
ASSERT_FLOAT_EQ(Sampler.MinLOD, 4.56F);
1245+
ASSERT_FLOAT_EQ(Sampler.MaxLOD, 8.9F);
1246+
ASSERT_EQ(Sampler.ShaderRegister, 31U);
1247+
ASSERT_EQ(Sampler.RegisterSpace, 32U);
1248+
ASSERT_EQ(Sampler.ShaderVisibility, 7U);
1249+
ASSERT_EQ(Sampler.Flags, 1U);
1250+
}
12031251
}

0 commit comments

Comments
 (0)