Skip to content

Commit e530bcc

Browse files
committed
this was easier than I thought
1 parent 1d0cbd3 commit e530bcc

File tree

4 files changed

+61
-69
lines changed

4 files changed

+61
-69
lines changed

llvm/include/llvm/Object/DXContainer.h

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -223,28 +223,6 @@ static Error parseFailed(const Twine &Msg) {
223223

224224
class RootSignature {
225225
private:
226-
struct samplers_iterator {
227-
const RootSignature *Parent = nullptr;
228-
uint32_t Index = 0;
229-
230-
llvm::Expected<dxbc::RTS0::v3::StaticSampler> operator*() const {
231-
return Parent->getSampler(Index);
232-
}
233-
234-
samplers_iterator &operator++() {
235-
++Index;
236-
return *this;
237-
}
238-
239-
bool operator==(const samplers_iterator &Other) const {
240-
return Parent == Other.Parent && Index == Other.Index;
241-
}
242-
243-
bool operator!=(const samplers_iterator &Other) const {
244-
return !(*this == Other);
245-
}
246-
};
247-
248226
uint32_t Version;
249227
uint32_t NumParameters;
250228
uint32_t RootParametersOffset;
@@ -253,32 +231,11 @@ class RootSignature {
253231
uint32_t Flags;
254232
ViewArray<dxbc::RTS0::v1::RootParameterHeader> ParametersHeaders;
255233
StringRef PartData;
256-
ViewArray<dxbc::RTS0::v1::StaticSampler> StaticSamplers;
257-
258-
llvm::Expected<dxbc::RTS0::v3::StaticSampler> getSampler(uint32_t Loc) const {
259-
if (Loc >= getNumStaticSamplers())
260-
return parseFailed("Static sampler index out of range");
261-
262-
auto SamplerSize = (Version <= 2) ? sizeof(dxbc::RTS0::v1::StaticSampler)
263-
: sizeof(dxbc::RTS0::v3::StaticSampler);
264-
265-
StringRef Buff = PartData.substr(StaticSamplersOffset + (Loc * SamplerSize),
266-
SamplerSize);
267-
if (Version < 3) {
268-
auto Sampler = readParameter<dxbc::RTS0::v1::StaticSampler>(Buff);
269-
if (Error E = Sampler.takeError())
270-
return E;
271-
return dxbc::RTS0::v3::StaticSampler(*Sampler);
272-
}
273-
if (Version != 3)
274-
return make_error<GenericBinaryError>("Invalid Root Signature version: " +
275-
Twine(Version),
276-
object_error::parse_failed);
277-
return readParameter<dxbc::RTS0::v3::StaticSampler>(Buff);
278-
}
234+
ViewArray<dxbc::RTS0::v3::StaticSampler> StaticSamplers;
279235

280236
using param_header_iterator =
281237
ViewArray<dxbc::RTS0::v1::RootParameterHeader>::iterator;
238+
using samplers_iterator = ViewArray<dxbc::RTS0::v3::StaticSampler>::iterator;
282239

283240
public:
284241
RootSignature(StringRef PD) : PartData(PD) {}
@@ -290,21 +247,12 @@ class RootSignature {
290247
uint32_t getNumStaticSamplers() const { return NumStaticSamplers; }
291248
uint32_t getStaticSamplersOffset() const { return StaticSamplersOffset; }
292249
uint32_t getNumRootParameters() const { return ParametersHeaders.size(); }
293-
294-
samplers_iterator samplers_begin() const {
295-
return samplers_iterator{this, 0};
296-
}
297-
298-
samplers_iterator samplers_end() const {
299-
return samplers_iterator{this, getNumStaticSamplers()};
250+
llvm::iterator_range<param_header_iterator> param_headers() const {
251+
return llvm::make_range(ParametersHeaders.begin(), ParametersHeaders.end());
300252
}
301253

302254
llvm::iterator_range<samplers_iterator> samplers() const {
303-
return llvm::make_range(samplers_begin(), samplers_end());
304-
}
305-
306-
llvm::iterator_range<param_header_iterator> param_headers() const {
307-
return llvm::make_range(ParametersHeaders.begin(), ParametersHeaders.end());
255+
return llvm::make_range(StaticSamplers.begin(), StaticSamplers.end());
308256
}
309257

310258
uint32_t getFlags() const { return Flags; }

llvm/lib/Object/DXContainer.cpp

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

9+
#include <cstddef>
10+
911
#include "llvm/Object/DXContainer.h"
1012
#include "llvm/BinaryFormat/DXContainer.h"
1113
#include "llvm/Object/Error.h"
@@ -276,10 +278,13 @@ Error DirectX::RootSignature::parse() {
276278
RootParametersOffset,
277279
NumParameters * sizeof(dxbc::RTS0::v1::RootParameterHeader));
278280

279-
StaticSamplers.Stride = sizeof(dxbc::RTS0::v1::StaticSampler);
280-
StaticSamplers.Data = PartData.substr(
281-
StaticSamplersOffset,
282-
NumStaticSamplers * sizeof(dxbc::RTS0::v1::StaticSampler));
281+
StaticSamplers.Stride = (Version <= 2)
282+
? sizeof(dxbc::RTS0::v1::StaticSampler)
283+
: sizeof(dxbc::RTS0::v3::StaticSampler);
284+
285+
StaticSamplers.Data = PartData.substr(StaticSamplersOffset,
286+
static_cast<size_t>(NumStaticSamplers) *
287+
StaticSamplers.Stride);
283288

284289
return Error::success();
285290
}

llvm/lib/ObjectYAML/DXContainerYAML.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
163163
}
164164
}
165165

166-
for (llvm::Expected<dxbc::RTS0::v3::StaticSampler> MaybeSampler :
167-
Data.samplers()) {
168-
if (Error E = MaybeSampler.takeError())
169-
return std::move(E);
170-
const llvm::dxbc::RTS0::v3::StaticSampler &S = *MaybeSampler;
166+
for (const auto &S : Data.samplers()) {
171167

172168
if (!dxbc::isValidSamplerFilter(S.Filter))
173169
return createStringError(std::errc::invalid_argument,

llvm/unittests/Object/DXContainerTest.cpp

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,9 +1184,7 @@ TEST(RootSignature, ParseStaticSamplers) {
11841184
ASSERT_EQ(RS.getStaticSamplersOffset(), 24u);
11851185
ASSERT_EQ(RS.getFlags(), 17u);
11861186

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

11911189
ASSERT_EQ(Sampler.Filter, 10u);
11921190
ASSERT_EQ(Sampler.AddressU, 1u);
@@ -1202,4 +1200,49 @@ TEST(RootSignature, ParseStaticSamplers) {
12021200
ASSERT_EQ(Sampler.RegisterSpace, 32u);
12031201
ASSERT_EQ(Sampler.ShaderVisibility, 7u);
12041202
}
1203+
{
1204+
uint8_t Buffer[] = {
1205+
0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1206+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1207+
0x90, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
1208+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1209+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1210+
0x52, 0x54, 0x53, 0x30, 0x4c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
1211+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1212+
0x18, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
1213+
0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
1214+
0xa4, 0x70, 0x9d, 0x3f, 0x14, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
1215+
0x00, 0x00, 0x00, 0x00, 0x85, 0xeb, 0x91, 0x40, 0x66, 0x66, 0x0e, 0x41,
1216+
0x1f, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
1217+
0x01, 0x00, 0x00, 0x00};
1218+
DXContainer C =
1219+
llvm::cantFail(DXContainer::create(getMemoryBuffer<148>(Buffer)));
1220+
1221+
auto MaybeRS = C.getRootSignature();
1222+
ASSERT_TRUE(MaybeRS.has_value());
1223+
const auto &RS = MaybeRS.value();
1224+
ASSERT_EQ(RS.getVersion(), 3U);
1225+
ASSERT_EQ(RS.getNumParameters(), 0U);
1226+
ASSERT_EQ(RS.getRootParametersOffset(), 0U);
1227+
ASSERT_EQ(RS.getNumStaticSamplers(), 1U);
1228+
ASSERT_EQ(RS.getStaticSamplersOffset(), 24U);
1229+
ASSERT_EQ(RS.getFlags(), 17U);
1230+
1231+
auto Sampler = *RS.samplers().begin();
1232+
1233+
ASSERT_EQ(Sampler.Filter, 10U);
1234+
ASSERT_EQ(Sampler.AddressU, 1U);
1235+
ASSERT_EQ(Sampler.AddressV, 2U);
1236+
ASSERT_EQ(Sampler.AddressW, 5U);
1237+
ASSERT_FLOAT_EQ(Sampler.MipLODBias, 1.23F);
1238+
ASSERT_EQ(Sampler.MaxAnisotropy, 20U);
1239+
ASSERT_EQ(Sampler.ComparisonFunc, 4U);
1240+
ASSERT_EQ(Sampler.BorderColor, 0U);
1241+
ASSERT_FLOAT_EQ(Sampler.MinLOD, 4.56F);
1242+
ASSERT_FLOAT_EQ(Sampler.MaxLOD, 8.9F);
1243+
ASSERT_EQ(Sampler.ShaderRegister, 31U);
1244+
ASSERT_EQ(Sampler.RegisterSpace, 32U);
1245+
ASSERT_EQ(Sampler.ShaderVisibility, 7U);
1246+
ASSERT_EQ(Sampler.Flags, 1U);
1247+
}
12051248
}

0 commit comments

Comments
 (0)