Skip to content

Commit fefd58c

Browse files
committed
fix test
1 parent d72adfe commit fefd58c

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

llvm/include/llvm/Object/DXContainer.h

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,25 +123,26 @@ template <typename T> struct ViewArray {
123123
};
124124

125125
namespace DirectX {
126+
127+
template <typename T> Expected<T> readParameter(StringRef Data) {
128+
T Struct;
129+
if (sizeof(T) != Data.size())
130+
return make_error<GenericBinaryError>(
131+
"Reading structure out of file bounds", object_error::parse_failed);
132+
133+
memcpy(&Struct, Data.data(), sizeof(T));
134+
// DXContainer is always little endian
135+
if (sys::IsBigEndianHost)
136+
Struct.swapBytes();
137+
return Struct;
138+
}
139+
126140
struct RootParameterView {
127141
const dxbc::RTS0::v1::RootParameterHeader &Header;
128142
StringRef ParamData;
129143

130144
RootParameterView(const dxbc::RTS0::v1::RootParameterHeader &H, StringRef P)
131145
: Header(H), ParamData(P) {}
132-
133-
template <typename T> Expected<T> readParameter() {
134-
T Struct;
135-
if (sizeof(T) != ParamData.size())
136-
return make_error<GenericBinaryError>(
137-
"Reading structure out of file bounds", object_error::parse_failed);
138-
139-
memcpy(&Struct, ParamData.data(), sizeof(T));
140-
// DXContainer is always little endian
141-
if (sys::IsBigEndianHost)
142-
Struct.swapBytes();
143-
return Struct;
144-
}
145146
};
146147

147148
struct RootConstantView : RootParameterView {
@@ -151,7 +152,7 @@ struct RootConstantView : RootParameterView {
151152
}
152153

153154
llvm::Expected<dxbc::RTS0::v1::RootConstants> read() {
154-
return readParameter<dxbc::RTS0::v1::RootConstants>();
155+
return readParameter<dxbc::RTS0::v1::RootConstants>(ParamData);
155156
}
156157
};
157158

@@ -167,7 +168,8 @@ struct RootDescriptorView : RootParameterView {
167168

168169
llvm::Expected<dxbc::RTS0::v2::RootDescriptor> read(uint32_t Version) {
169170
if (Version == 1) {
170-
auto Descriptor = readParameter<dxbc::RTS0::v1::RootDescriptor>();
171+
auto Descriptor =
172+
readParameter<dxbc::RTS0::v1::RootDescriptor>(ParamData);
171173
if (Error E = Descriptor.takeError())
172174
return E;
173175
return dxbc::RTS0::v2::RootDescriptor(*Descriptor);
@@ -176,9 +178,10 @@ struct RootDescriptorView : RootParameterView {
176178
return make_error<GenericBinaryError>("Invalid Root Signature version: " +
177179
Twine(Version),
178180
object_error::parse_failed);
179-
return readParameter<dxbc::RTS0::v2::RootDescriptor>();
181+
return readParameter<dxbc::RTS0::v2::RootDescriptor>(ParamData);
180182
}
181183
};
184+
182185
template <typename T> struct DescriptorTable {
183186
uint32_t NumRanges;
184187
uint32_t RangesOffset;
@@ -247,8 +250,26 @@ class RootSignature {
247250
llvm::iterator_range<param_header_iterator> param_headers() const {
248251
return llvm::make_range(ParametersHeaders.begin(), ParametersHeaders.end());
249252
}
250-
llvm::iterator_range<samplers_iterator> samplers() const {
251-
return llvm::make_range(StaticSamplers.begin(), StaticSamplers.end());
253+
llvm::Expected<dxbc::RTS0::v3::StaticSampler> getSampler(uint32_t Loc) const {
254+
if (Loc >= getNumStaticSamplers())
255+
return parseFailed("Static sampler index out of range");
256+
257+
auto SamplerSize = (Version <= 2) ? sizeof(dxbc::RTS0::v1::StaticSampler)
258+
: sizeof(dxbc::RTS0::v3::StaticSampler);
259+
260+
StringRef Buff = PartData.substr(StaticSamplersOffset + (Loc * SamplerSize),
261+
SamplerSize);
262+
if (Version < 3) {
263+
auto Sampler = readParameter<dxbc::RTS0::v1::StaticSampler>(Buff);
264+
if (Error E = Sampler.takeError())
265+
return E;
266+
return dxbc::RTS0::v3::StaticSampler(*Sampler);
267+
}
268+
if (Version != 3)
269+
return make_error<GenericBinaryError>("Invalid Root Signature version: " +
270+
Twine(Version),
271+
object_error::parse_failed);
272+
return readParameter<dxbc::RTS0::v3::StaticSampler>(Buff);
252273
}
253274
uint32_t getFlags() const { return Flags; }
254275

llvm/lib/ObjectYAML/DXContainerYAML.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,13 @@ DXContainerYAML::RootSignatureYamlDesc::create(
163163
}
164164
}
165165

166-
for (const auto &S : Data.samplers()) {
166+
for (uint32_t Loc = 0; Loc < Data.getNumStaticSamplers(); ++Loc) {
167+
llvm::Expected<dxbc::RTS0::v3::StaticSampler> MaybeSampler =
168+
Data.getSampler(Loc);
169+
if (Error E = MaybeSampler.takeError())
170+
return std::move(E);
171+
const llvm::dxbc::RTS0::v3::StaticSampler &S = *MaybeSampler;
172+
167173
if (!dxbc::isValidSamplerFilter(S.Filter))
168174
return createStringError(std::errc::invalid_argument,
169175
"Invalid value for static sampler filter");
@@ -209,6 +215,14 @@ DXContainerYAML::RootSignatureYamlDesc::create(
209215
NewS.RegisterSpace = S.RegisterSpace;
210216
NewS.ShaderVisibility = dxbc::ShaderVisibility(S.ShaderVisibility);
211217

218+
if (Version > 2) {
219+
if (Version > 1) {
220+
#define STATIC_SAMPLER_FLAG(Num, Enum, Flag) \
221+
NewS.Enum = \
222+
(S.Flags & llvm::to_underlying(dxbc::StaticSamplerFlags::Enum)) > 0;
223+
#include "llvm/BinaryFormat/DXContainerConstants.def"
224+
}
225+
}
212226
RootSigDesc.StaticSamplers.push_back(NewS);
213227
}
214228

llvm/unittests/Object/DXContainerTest.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,11 +1165,11 @@ TEST(RootSignature, ParseStaticSamplers) {
11651165
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11661166
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11671167
0x52, 0x54, 0x53, 0x30, 0x4c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
1168-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1169-
0x18, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
1170-
0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
1171-
0xa4, 0x70, 0x9d, 0x3f, 0x14, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
1172-
0x00, 0x00, 0x00, 0x00, 0x85, 0xeb, 0x91, 0x40, 0x66, 0x66, 0x0e, 0x41,
1168+
0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1169+
0x18, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00,
1170+
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1171+
0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
1172+
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7f, 0x7f,
11731173
0x1f, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00};
11741174
DXContainer C =
11751175
llvm::cantFail(DXContainer::create(getMemoryBuffer<133>(Buffer)));
@@ -1179,12 +1179,14 @@ TEST(RootSignature, ParseStaticSamplers) {
11791179
const auto &RS = MaybeRS.value();
11801180
ASSERT_EQ(RS.getVersion(), 2u);
11811181
ASSERT_EQ(RS.getNumParameters(), 0u);
1182-
ASSERT_EQ(RS.getRootParametersOffset(), 0u);
1182+
ASSERT_EQ(RS.getRootParametersOffset(), 24u);
11831183
ASSERT_EQ(RS.getNumStaticSamplers(), 1u);
11841184
ASSERT_EQ(RS.getStaticSamplersOffset(), 24u);
11851185
ASSERT_EQ(RS.getFlags(), 17u);
11861186

1187-
auto Sampler = *RS.samplers().begin();
1187+
auto MaybeSamplerView = RS.getSampler(0);
1188+
ASSERT_THAT_ERROR(MaybeSamplerView.takeError(), Succeeded());
1189+
const auto &Sampler = *MaybeSamplerView;
11881190

11891191
ASSERT_EQ(Sampler.Filter, 10u);
11901192
ASSERT_EQ(Sampler.AddressU, 1u);

0 commit comments

Comments
 (0)