Skip to content

Commit 2f7d28f

Browse files
authored
Merge pull request #13 from damyanp/resources
DX: Simplify tracking of resources
2 parents b2058aa + 6feb64d commit 2f7d28f

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

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

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

10095
public:
@@ -390,10 +385,9 @@ class DXDevice : public hlsltest::Device {
390385
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
391386
Device->CreateUnorderedAccessView(Buffer, nullptr, &UAVDesc, UAVHandle);
392387

393-
std::pair<uint32_t, uint32_t> Binding = {R.DXBinding.Register,
394-
R.DXBinding.Space};
395388
UAVResourceSet Resources = {UploadBuffer, Buffer, ReadBackBuffer};
396-
IS.Outputs.insert(std::make_pair(Binding, Resources));
389+
IS.Resources.push_back(Resources);
390+
397391
return llvm::Error::success();
398392
}
399393

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

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

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

0 commit comments

Comments
 (0)