Skip to content

Commit 6feb64d

Browse files
committed
DX: Simplify tracking of resources
There's an assumption that the resources are created in the same order as they appear in the descriptor heap, which is the order they appear when traversing Pipeline/Sets/Resources. The root signature is where the mapping from index in the descriptor heap to a shader register happens. Because of this, trying to keep track of the resources via their DX register binding makes things more complicated that necessary, since we can just assume there's a simple mapping of Set/Resource to the resources.
1 parent a8967dd commit 6feb64d

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

lib/API/DX/Device.cpp

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "HLSLTest/API/Pipeline.h"
2828
#include "HLSLTest/WinError.h"
2929

30-
#include "llvm/ADT/DenseMap.h"
3130
#include "llvm/ADT/SmallVector.h"
3231
#include "llvm/Support/Error.h"
3332

@@ -83,11 +82,6 @@ class DXDevice : public hlsltest::Device {
8382
};
8483

8584
struct InvocationState {
86-
// Resource references need to be the last things cleaned up, so put them at
87-
// the top.
88-
using Binding = std::pair<uint32_t, uint32_t>;
89-
llvm::DenseMap<Binding, UAVResourceSet> Outputs;
90-
9185
CComPtr<ID3D12RootSignature> RootSig;
9286
CComPtr<ID3D12DescriptorHeap> DescHeap;
9387
CComPtr<ID3D12PipelineState> PSO;
@@ -96,6 +90,7 @@ class DXDevice : public hlsltest::Device {
9690
CComPtr<ID3D12GraphicsCommandList> CmdList;
9791
CComPtr<ID3D12Fence> Fence;
9892
HANDLE Event;
93+
llvm::SmallVector<UAVResourceSet> Resources;
9994
};
10095

10196
public:
@@ -388,10 +383,9 @@ class DXDevice : public hlsltest::Device {
388383
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
389384
Device->CreateUnorderedAccessView(Buffer, nullptr, &UAVDesc, UAVHandle);
390385

391-
std::pair<uint32_t, uint32_t> Binding = {R.DXBinding.Register,
392-
R.DXBinding.Space};
393386
UAVResourceSet Resources = {UploadBuffer, Buffer, ReadBackBuffer};
394-
IS.Outputs.insert(std::make_pair(Binding, Resources));
387+
IS.Resources.push_back(Resources);
388+
395389
return llvm::Error::success();
396390
}
397391

@@ -526,31 +520,31 @@ class DXDevice : public hlsltest::Device {
526520
IS.CmdList->Dispatch(P.DispatchSize[0], P.DispatchSize[1],
527521
P.DispatchSize[2]);
528522

529-
for (auto &Out : IS.Outputs) {
530-
addReadbackBeginBarrier(IS, Out.second.Buffer);
531-
IS.CmdList->CopyResource(Out.second.Readback, Out.second.Buffer);
532-
addReadbackEndBarrier(IS, Out.second.Buffer);
523+
for (auto &Out : IS.Resources) {
524+
addReadbackBeginBarrier(IS, Out.Buffer);
525+
IS.CmdList->CopyResource(Out.Readback, Out.Buffer);
526+
addReadbackEndBarrier(IS, Out.Buffer);
533527
}
534528
}
535529

536530
llvm::Error readBack(Pipeline &P, InvocationState &IS) {
531+
auto ResourcesIterator = IS.Resources.begin();
537532
for (auto &S : P.Sets) {
538533
for (auto &R : S.Resources) {
539-
if (R.Access != DataAccess::ReadWrite)
540-
continue;
541-
auto ResourcePair = IS.Outputs.find(
542-
std::make_pair(R.DXBinding.Register, R.DXBinding.Space));
543-
if (ResourcePair == IS.Outputs.end())
544-
return llvm::createStringError(std::errc::no_such_device_or_address,
545-
"Failed to find binding.");
546-
547-
void *DataPtr;
548-
if (auto Err = HR::toError(
549-
ResourcePair->second.Readback->Map(0, nullptr, &DataPtr),
550-
"Failed to map result."))
551-
return Err;
552-
memcpy(R.Data.get(), DataPtr, R.Size);
553-
ResourcePair->second.Readback->Unmap(0, nullptr);
534+
if (ResourcesIterator == IS.Resources.end())
535+
return llvm::createStringError(
536+
std::errc::no_such_device_or_address,
537+
"Internal error: created resources doesn't match pipeline");
538+
if (R.Access == DataAccess::ReadWrite) {
539+
void *DataPtr;
540+
if (auto Err = HR::toError(
541+
ResourcesIterator->Readback->Map(0, nullptr, &DataPtr),
542+
"Failed to map result."))
543+
return Err;
544+
memcpy(R.Data.get(), DataPtr, R.Size);
545+
ResourcesIterator->Readback->Unmap(0, nullptr);
546+
}
547+
++ResourcesIterator;
554548
}
555549
}
556550
return llvm::Error::success();

0 commit comments

Comments
 (0)