Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 112 additions & 37 deletions .github/workflows/.patches/dawn.diff
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
From 94b1c6f30ff9d23f3c8734cffc8430765d5b6c39 Mon Sep 17 00:00:00 2001
From 0698833a19cab41fd2f28dceb03fd6db37ba3571 Mon Sep 17 00:00:00 2001
From: Bryan Bernhart <bryan.bernhart@intel.com>
Date: Tue, 15 Feb 2022 17:25:29 -0800
Subject: [PATCH] Use GPGMM for D3D12 backend.
Expand All @@ -22,16 +22,16 @@ Change-Id: I47708462a1d9dd0166120c3a6af93451aae54a07
src/dawn/native/d3d12/D3D12Backend.cpp | 17 ++-
src/dawn/native/d3d12/DeviceD3D12.cpp | 114 +++++++++++++++---
src/dawn/native/d3d12/DeviceD3D12.h | 20 +--
.../ShaderVisibleDescriptorAllocatorD3D12.cpp | 25 +++-
.../ShaderVisibleDescriptorAllocatorD3D12.h | 6 +-
.../ShaderVisibleDescriptorAllocatorD3D12.cpp | 80 +++++++-----
.../ShaderVisibleDescriptorAllocatorD3D12.h | 11 +-
src/dawn/native/d3d12/StagingBufferD3D12.cpp | 22 +---
src/dawn/native/d3d12/StagingBufferD3D12.h | 4 +-
src/dawn/native/d3d12/TextureD3D12.cpp | 41 +++----
src/dawn/native/d3d12/TextureD3D12.h | 4 +-
src/dawn/native/d3d12/UtilsD3D12.cpp | 11 ++
src/dawn/native/d3d12/UtilsD3D12.h | 2 +
.../tests/white_box/D3D12ResidencyTests.cpp | 17 +--
26 files changed, 291 insertions(+), 137 deletions(-)
26 files changed, 326 insertions(+), 162 deletions(-)
create mode 100644 build_overrides/gpgmm.gni

diff --git a/.gitignore b/.gitignore
Expand Down Expand Up @@ -725,7 +725,7 @@ index 99b03e396..b1cf46fd0 100644
static constexpr uint32_t kMaxSamplerDescriptorsPerBindGroup = 3 * kMaxSamplersPerShaderStage;
static constexpr uint32_t kMaxViewDescriptorsPerBindGroup =
diff --git a/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.cpp b/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.cpp
index fe99a63ac..a123dfc7b 100644
index fe99a63ac..40ce7b4a7 100644
--- a/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.cpp
+++ b/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.cpp
@@ -93,7 +93,8 @@ ShaderVisibleDescriptorAllocator::ShaderVisibleDescriptorAllocator(
Expand All @@ -738,65 +738,131 @@ index fe99a63ac..a123dfc7b 100644
ASSERT(heapType == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV ||
heapType == D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
}
@@ -145,7 +146,11 @@ ShaderVisibleDescriptorAllocator::AllocateHeap(uint32_t descriptorCount) const {
// the actual size may vary depending on the driver.
const uint64_t kSize = mSizeIncrement * descriptorCount;
@@ -140,31 +141,41 @@ void ShaderVisibleDescriptorAllocator::Tick(ExecutionSerial completedSerial) {

ResultOrError<std::unique_ptr<ShaderVisibleDescriptorHeap>>
ShaderVisibleDescriptorAllocator::AllocateHeap(uint32_t descriptorCount) const {
+ gpgmm::d3d12::HEAP_DESC heapDesc = {};
+
// The size in bytes of a descriptor heap is best calculated by the increment size
// multiplied by the number of descriptors. In practice, this is only an estimate and
// the actual size may vary depending on the driver.
- const uint64_t kSize = mSizeIncrement * descriptorCount;
-
- DAWN_TRY(mDevice->GetResidencyManager()->EnsureCanAllocate(kSize, MemorySegment::Local));
+ if (mResidencyManagementEnabled) {
+ DAWN_TRY(CheckHRESULT(
+ mDevice->GetResidencyManager()->Evict(kSize, DXGI_MEMORY_SEGMENT_GROUP_LOCAL),
+ "Unable to allocate heap"));
+ }
-
- ComPtr<ID3D12DescriptorHeap> d3d12DescriptorHeap;
- D3D12_DESCRIPTOR_HEAP_DESC heapDescriptor;
- heapDescriptor.Type = mHeapType;
- heapDescriptor.NumDescriptors = descriptorCount;
- heapDescriptor.Flags = GetD3D12HeapFlags(mHeapType);
- heapDescriptor.NodeMask = 0;
- DAWN_TRY(CheckOutOfMemoryHRESULT(mDevice->GetD3D12Device()->CreateDescriptorHeap(
- &heapDescriptor, IID_PPV_ARGS(&d3d12DescriptorHeap)),
- "ID3D12Device::CreateDescriptorHeap"));
+ heapDesc.SizeInBytes = mSizeIncrement * descriptorCount;
+
+ heapDesc.DebugName = "Shader visible heap";
+ heapDesc.MemorySegment = gpgmm::d3d12::RESIDENCY_SEGMENT_LOCAL;
+
+ auto createHeapFn = [&](ID3D12Pageable** ppPageableOut) -> HRESULT {
+ ComPtr<ID3D12DescriptorHeap> d3d12DescriptorHeap;
+ D3D12_DESCRIPTOR_HEAP_DESC heapDescriptor;
+ heapDescriptor.Type = mHeapType;
+ heapDescriptor.NumDescriptors = descriptorCount;
+ heapDescriptor.Flags = GetD3D12HeapFlags(mHeapType);
+ heapDescriptor.NodeMask = 0;
+ HRESULT hr = mDevice->GetD3D12Device()->CreateDescriptorHeap(
+ &heapDescriptor, IID_PPV_ARGS(&d3d12DescriptorHeap));
+ if (FAILED(hr)) {
+ return hr;
+ }

ComPtr<ID3D12DescriptorHeap> d3d12DescriptorHeap;
D3D12_DESCRIPTOR_HEAP_DESC heapDescriptor;
@@ -162,7 +167,10 @@ ShaderVisibleDescriptorAllocator::AllocateHeap(uint32_t descriptorCount) const {
- std::unique_ptr<ShaderVisibleDescriptorHeap> descriptorHeap =
- std::make_unique<ShaderVisibleDescriptorHeap>(std::move(d3d12DescriptorHeap), kSize);
+ *ppPageableOut = d3d12DescriptorHeap.Detach();
+ return S_OK;
+ };

// We must track the allocation in the LRU when it is created, otherwise the residency
// manager will see the allocation as non-resident in the later call to LockAllocation.
- // We must track the allocation in the LRU when it is created, otherwise the residency
- // manager will see the allocation as non-resident in the later call to LockAllocation.
- mDevice->GetResidencyManager()->TrackResidentAllocation(descriptorHeap.get());
+ if (mResidencyManagementEnabled) {
+ DAWN_TRY(CheckHRESULT(mDevice->GetResidencyManager()->InsertHeap(descriptorHeap.get()),
+ "Unable to insert descriptor heap into residency manager"));
+ }
+ gpgmm::d3d12::Heap* descriptorHeapPtr = nullptr;
+ DAWN_TRY(CheckOutOfMemoryHRESULT(
+ gpgmm::d3d12::Heap::CreateHeap(heapDesc, mDevice->GetResidencyManager(), createHeapFn,
+ &descriptorHeapPtr),
+ "Unable to create descriptor heap"));

return std::move(descriptorHeap);
- return std::move(descriptorHeap);
+ return std::make_unique<ShaderVisibleDescriptorHeap>(
+ std::unique_ptr<gpgmm::d3d12::Heap>(descriptorHeapPtr));
}
@@ -174,7 +182,9 @@ MaybeError ShaderVisibleDescriptorAllocator::AllocateAndSwitchShaderVisibleHeap(

// Creates a GPU descriptor heap that manages descriptors in a FIFO queue.
@@ -174,7 +185,9 @@ MaybeError ShaderVisibleDescriptorAllocator::AllocateAndSwitchShaderVisibleHeap(
// The first phase increasingly grows a small heap in binary sizes for light users while the
// second phase pool-allocates largest sized heaps for heavy users.
if (mHeap != nullptr) {
- mDevice->GetResidencyManager()->UnlockAllocation(mHeap.get());
+ if (mResidencyManagementEnabled) {
+ mDevice->GetResidencyManager()->UnlockHeap(mHeap.get());
+ mDevice->GetResidencyManager()->UnlockHeap(mHeap->GetHeap());
+ }

const uint32_t maxDescriptorCount = GetD3D12ShaderVisibleHeapMaxSize(
mHeapType, mDevice->IsToggleEnabled(Toggle::UseD3D12SmallShaderVisibleHeapForTesting));
@@ -200,7 +210,10 @@ MaybeError ShaderVisibleDescriptorAllocator::AllocateAndSwitchShaderVisibleHeap(
@@ -200,7 +213,10 @@ MaybeError ShaderVisibleDescriptorAllocator::AllocateAndSwitchShaderVisibleHeap(
DAWN_TRY_ASSIGN(descriptorHeap, AllocateHeap(mDescriptorCount));
}

- DAWN_TRY(mDevice->GetResidencyManager()->LockAllocation(descriptorHeap.get()));
+ if (mResidencyManagementEnabled) {
+ DAWN_TRY(CheckHRESULT(mDevice->GetResidencyManager()->LockHeap(descriptorHeap.get()),
+ DAWN_TRY(CheckHRESULT(mDevice->GetResidencyManager()->LockHeap(descriptorHeap->GetHeap()),
+ "Unable to lock descriptor heap"));
+ }

// Create a FIFO buffer from the recently created heap.
mHeap = std::move(descriptorHeap);
@@ -246,7 +259,7 @@ bool ShaderVisibleDescriptorAllocator::IsAllocationStillValid(
@@ -227,12 +243,12 @@ uint64_t ShaderVisibleDescriptorAllocator::GetShaderVisiblePoolSizeForTesting()
}

bool ShaderVisibleDescriptorAllocator::IsShaderVisibleHeapLockedResidentForTesting() const {
- return mHeap->IsResidencyLocked();
+ return mHeap->GetHeap()->IsResidencyLocked();
}

bool ShaderVisibleDescriptorAllocator::IsLastShaderVisibleHeapInLRUForTesting() const {
ASSERT(!mPool.empty());
- return mPool.back().heap->IsInResidencyLRUCache();
+ return mPool.back().heap->GetHeap()->IsInResidencyLRUCache();
}

bool ShaderVisibleDescriptorAllocator::IsAllocationStillValid(
@@ -244,12 +260,18 @@ bool ShaderVisibleDescriptorAllocator::IsAllocationStillValid(
}

ShaderVisibleDescriptorHeap::ShaderVisibleDescriptorHeap(
ComPtr<ID3D12DescriptorHeap> d3d12DescriptorHeap,
uint64_t size)
- ComPtr<ID3D12DescriptorHeap> d3d12DescriptorHeap,
- uint64_t size)
- : Pageable(d3d12DescriptorHeap, MemorySegment::Local, size),
+ : gpgmm::d3d12::Heap(d3d12DescriptorHeap, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, size),
mD3d12DescriptorHeap(std::move(d3d12DescriptorHeap)) {}
- mD3d12DescriptorHeap(std::move(d3d12DescriptorHeap)) {}
+ std::unique_ptr<gpgmm::d3d12::Heap> descriptorHeap)
+ : mDescriptorHeap(std::move(descriptorHeap)) {}

ID3D12DescriptorHeap* ShaderVisibleDescriptorHeap::GetD3D12DescriptorHeap() const {
- return mD3d12DescriptorHeap.Get();
+ ComPtr<ID3D12DescriptorHeap> descriptorHeap;
+ HRESULT hr = mDescriptorHeap->As(&descriptorHeap);
+ ASSERT(SUCCEEDED(hr));
+ return descriptorHeap.Get();
+}
+
+gpgmm::d3d12::Heap* ShaderVisibleDescriptorHeap::GetHeap() const {
+ return mDescriptorHeap.get();
}
+
} // namespace dawn::native::d3d12
diff --git a/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.h b/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.h
index cf09f9d50..2b3254f9f 100644
index cf09f9d50..008b54818 100644
--- a/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.h
+++ b/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.h
@@ -24,6 +24,8 @@
Expand All @@ -808,16 +874,25 @@ index cf09f9d50..2b3254f9f 100644
// |ShaderVisibleDescriptorAllocator| allocates a variable-sized block of descriptors from a GPU
// descriptor heap pool.
// Internally, it manages a list of heaps using a ringbuffer block allocator. The heap is in one
@@ -35,7 +37,7 @@ namespace dawn::native::d3d12 {
@@ -35,13 +37,14 @@ namespace dawn::native::d3d12 {
class Device;
class GPUDescriptorHeapAllocation;

-class ShaderVisibleDescriptorHeap : public Pageable {
+class ShaderVisibleDescriptorHeap : public gpgmm::d3d12::Heap {
+class ShaderVisibleDescriptorHeap {
public:
ShaderVisibleDescriptorHeap(ComPtr<ID3D12DescriptorHeap> d3d12DescriptorHeap, uint64_t size);
- ShaderVisibleDescriptorHeap(ComPtr<ID3D12DescriptorHeap> d3d12DescriptorHeap, uint64_t size);
+ ShaderVisibleDescriptorHeap(std::unique_ptr<gpgmm::d3d12::Heap> descriptorHeap);
ID3D12DescriptorHeap* GetD3D12DescriptorHeap() const;
@@ -99,6 +101,8 @@ class ShaderVisibleDescriptorAllocator {
+ gpgmm::d3d12::Heap* GetHeap() const;

private:
- ComPtr<ID3D12DescriptorHeap> mD3d12DescriptorHeap;
+ std::unique_ptr<gpgmm::d3d12::Heap> mDescriptorHeap;
};

class ShaderVisibleDescriptorAllocator {
@@ -99,6 +102,8 @@ class ShaderVisibleDescriptorAllocator {
// The descriptor count is the current size of the heap in number of descriptors.
// This is stored on the allocator to avoid extra conversions.
uint32_t mDescriptorCount = 0;
Expand Down