From ff83dd0037eb2a0beb86c7cf4a52f78d9bd05a69 Mon Sep 17 00:00:00 2001 From: Bryan Bernhart Date: Tue, 13 Sep 2022 15:18:13 -0700 Subject: [PATCH] Move Heap parameters into HEAP_DESC. Adds HEAPS_FLAGS to HEAP_DESC to configure Heap creation. --- src/gpgmm/d3d12/HeapD3D12.cpp | 19 +++------- src/gpgmm/d3d12/HeapD3D12.h | 38 ++++++++++++------- src/gpgmm/d3d12/JSONSerializerD3D12.cpp | 3 +- src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp | 5 ++- src/gpgmm/d3d12/ResourceAllocatorD3D12.h | 5 ++- .../d3d12/ResourceHeapAllocatorD3D12.cpp | 2 +- src/include/min/gpgmm_d3d12.cpp | 1 - src/include/min/gpgmm_d3d12.h | 12 ++++-- .../D3D12EventTraceReplay.cpp | 1 - 9 files changed, 48 insertions(+), 38 deletions(-) diff --git a/src/gpgmm/d3d12/HeapD3D12.cpp b/src/gpgmm/d3d12/HeapD3D12.cpp index b6fda5dd4..c1b86fd3d 100644 --- a/src/gpgmm/d3d12/HeapD3D12.cpp +++ b/src/gpgmm/d3d12/HeapD3D12.cpp @@ -28,10 +28,9 @@ namespace gpgmm::d3d12 { ResidencyManager* const pResidencyManager, CreateHeapFn&& createHeapFn, Heap** ppHeapOut) { - // Ensure enough free memory exists before allocating to avoid an out-of-memory error // when over budget. - if (pResidencyManager != nullptr && descriptor.AlwaysInBudget) { + if (pResidencyManager != nullptr && (descriptor.Flags & HEAP_FLAG_ALWAYS_IN_BUDGET)) { ReturnIfFailed(pResidencyManager->EnsureInBudget(descriptor.SizeInBytes, descriptor.MemorySegmentGroup)); } @@ -42,9 +41,7 @@ namespace gpgmm::d3d12 { GPGMM_TRACE_EVENT_OBJECT_CALL("Heap.CreateHeap", (CREATE_HEAP_DESC{descriptor, pageable.Get()})); - std::unique_ptr heap(new Heap(std::move(pageable), descriptor.MemorySegmentGroup, - descriptor.SizeInBytes, descriptor.Alignment, - descriptor.IsExternal)); + std::unique_ptr heap(new Heap(std::move(pageable), descriptor)); if (pResidencyManager != nullptr) { ReturnIfFailed(pResidencyManager->InsertHeap(heap.get())); @@ -60,16 +57,12 @@ namespace gpgmm::d3d12 { return S_OK; } - Heap::Heap(ComPtr pageable, - const DXGI_MEMORY_SEGMENT_GROUP& memorySegmentGroup, - uint64_t size, - uint64_t alignment, - bool isExternal) - : MemoryBase(size, alignment), + Heap::Heap(ComPtr pageable, const HEAP_DESC& descriptor) + : MemoryBase(descriptor.SizeInBytes, descriptor.Alignment), mPageable(std::move(pageable)), - mMemorySegmentGroup(memorySegmentGroup), + mMemorySegmentGroup(descriptor.MemorySegmentGroup), mResidencyLock(0), - mIsExternal(isExternal) { + mIsExternal(descriptor.Flags & HEAP_FLAG_NEVER_USE_RESIDENCY) { ASSERT(mPageable != nullptr); if (!mIsExternal) { GPGMM_TRACE_EVENT_OBJECT_NEW(this); diff --git a/src/gpgmm/d3d12/HeapD3D12.h b/src/gpgmm/d3d12/HeapD3D12.h index b26c8de0f..75bc1116e 100644 --- a/src/gpgmm/d3d12/HeapD3D12.h +++ b/src/gpgmm/d3d12/HeapD3D12.h @@ -40,8 +40,28 @@ namespace gpgmm::d3d12 { bool IsResident; }; + /** \enum HEAPS_FLAGS + Specify creation options to configure the heap. + */ + enum HEAPS_FLAGS { + + /** \brief Disables all option flags. + */ + HEAPS_FLAG_NONE = 0x0, + + /** \brief Requires the heap to be created in budget. + */ + HEAP_FLAG_ALWAYS_IN_BUDGET = 0x1, + + /** \brief Specifies to leave the heap unmanaged for residency. + */ + HEAP_FLAG_NEVER_USE_RESIDENCY = 0x2, + }; + + DEFINE_ENUM_FLAG_OPERATORS(HEAPS_FLAGS) + /** \struct HEAP_DESC - Specifies properties of a managed heap. + Specifies creation options for a residency managed heap. */ struct HEAP_DESC { /** \brief Created size of the heap, in bytes. @@ -56,15 +76,9 @@ namespace gpgmm::d3d12 { */ uint64_t Alignment; - /** \brief Requires the heap to be created in budget. + /** \brief Specifies heaps options. */ - bool AlwaysInBudget; - - /** \brief Specifies to leave the heap unmanaged by GPGMM. - - External heaps are not supported for residency. - */ - bool IsExternal; + HEAPS_FLAGS Flags; /** \brief Specifies the memory segment to use for residency. @@ -157,11 +171,7 @@ namespace gpgmm::d3d12 { friend ResidencyManager; friend ResourceAllocator; - Heap(ComPtr pageable, - const DXGI_MEMORY_SEGMENT_GROUP& memorySegmentGroup, - uint64_t size, - uint64_t alignment, - bool isExternal); + Heap(ComPtr pageable, const HEAP_DESC& descriptor); HRESULT SetDebugNameImpl(const std::string& name) override; const char* GetTypename() const override; diff --git a/src/gpgmm/d3d12/JSONSerializerD3D12.cpp b/src/gpgmm/d3d12/JSONSerializerD3D12.cpp index 8f85a557e..5c69e3ac4 100644 --- a/src/gpgmm/d3d12/JSONSerializerD3D12.cpp +++ b/src/gpgmm/d3d12/JSONSerializerD3D12.cpp @@ -164,8 +164,7 @@ namespace gpgmm::d3d12 { JSONDict dict; dict.AddItem("SizeInBytes", desc.SizeInBytes); dict.AddItem("Alignment", desc.Alignment); - dict.AddItem("AlwaysInBudget", desc.AlwaysInBudget); - dict.AddItem("IsExternal", desc.IsExternal); + dict.AddItem("Flags", desc.Flags); dict.AddItem("MemorySegmentGroup", desc.MemorySegmentGroup); dict.AddItem("DebugName", desc.DebugName); return dict; diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp index 9217f3a4c..57362489a 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp @@ -1149,7 +1149,7 @@ namespace gpgmm::d3d12 { HEAP_DESC resourceHeapDesc = {}; resourceHeapDesc.SizeInBytes = resourceInfo.SizeInBytes; resourceHeapDesc.Alignment = resourceInfo.Alignment; - resourceHeapDesc.IsExternal = true; + resourceHeapDesc.Flags |= HEAP_FLAG_NEVER_USE_RESIDENCY; Heap* resourceHeap = nullptr; ReturnIfFailed(Heap::CreateHeap( @@ -1222,7 +1222,8 @@ namespace gpgmm::d3d12 { resourceHeapDesc.SizeInBytes = info.SizeInBytes; resourceHeapDesc.DebugName = "Resource heap (committed)"; resourceHeapDesc.Alignment = info.Alignment; - resourceHeapDesc.AlwaysInBudget = mIsAlwaysInBudget; + resourceHeapDesc.Flags |= + (mIsAlwaysInBudget) ? HEAP_FLAG_ALWAYS_IN_BUDGET : HEAPS_FLAG_NONE; if (IsResidencyEnabled()) { resourceHeapDesc.MemorySegmentGroup = GetMemorySegmentGroup( diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.h b/src/gpgmm/d3d12/ResourceAllocatorD3D12.h index 0c9d4dfda..74eb1ac39 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.h +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.h @@ -38,9 +38,12 @@ namespace gpgmm::d3d12 { class ResidencyManager; class ResourceAllocation; + /** \enum ALLOCATOR_FLAGS + Specify creation options for allocator. + */ enum ALLOCATOR_FLAGS { - /** \brief Disables all allocator flags. + /** \brief Disables all option flags. */ ALLOCATOR_FLAG_NONE = 0x0, diff --git a/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.cpp index 0965673e3..f5cc3c4f1 100644 --- a/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.cpp @@ -63,7 +63,7 @@ namespace gpgmm::d3d12 { resourceHeapDesc.SizeInBytes = heapSize; resourceHeapDesc.DebugName = "Resource heap"; resourceHeapDesc.Alignment = request.Alignment; - resourceHeapDesc.AlwaysInBudget = mAlwaysInBudget; + resourceHeapDesc.Flags |= (mAlwaysInBudget) ? HEAP_FLAG_ALWAYS_IN_BUDGET : HEAPS_FLAG_NONE; if (mResidencyManager != nullptr) { resourceHeapDesc.MemorySegmentGroup = GetMemorySegmentGroup( diff --git a/src/include/min/gpgmm_d3d12.cpp b/src/include/min/gpgmm_d3d12.cpp index f486fd2f1..f50d69be3 100644 --- a/src/include/min/gpgmm_d3d12.cpp +++ b/src/include/min/gpgmm_d3d12.cpp @@ -280,7 +280,6 @@ namespace gpgmm::d3d12 { HEAP_DESC resourceHeapDesc = {}; resourceHeapDesc.SizeInBytes = resourceInfo.SizeInBytes; resourceHeapDesc.Alignment = resourceInfo.Alignment; - resourceHeapDesc.HeapType = allocationDescriptor.HeapType; ReturnIfFailed(Heap::CreateHeap( resourceHeapDesc, mResidencyManager.Get(), diff --git a/src/include/min/gpgmm_d3d12.h b/src/include/min/gpgmm_d3d12.h index 160252828..6a88ef404 100644 --- a/src/include/min/gpgmm_d3d12.h +++ b/src/include/min/gpgmm_d3d12.h @@ -69,12 +69,18 @@ namespace gpgmm::d3d12 { bool IsResident; }; + enum HEAPS_FLAGS { + HEAPS_FLAG_NONE = 0x0, + HEAP_FLAG_ALWAYS_IN_BUDGET = 0x1, + HEAP_FLAG_NEVER_USE_RESIDENCY = 0x2, + }; + + DEFINE_ENUM_FLAG_OPERATORS(HEAPS_FLAGS) + struct HEAP_DESC { uint64_t SizeInBytes; uint64_t Alignment; - D3D12_HEAP_TYPE HeapType; - bool AlwaysInBudget; - bool IsExternal; + HEAPS_FLAGS Flags; DXGI_MEMORY_SEGMENT_GROUP MemorySegmentGroup; std::string DebugName; }; diff --git a/src/tests/capture_replay_tests/D3D12EventTraceReplay.cpp b/src/tests/capture_replay_tests/D3D12EventTraceReplay.cpp index 03ffd6433..285ad707e 100644 --- a/src/tests/capture_replay_tests/D3D12EventTraceReplay.cpp +++ b/src/tests/capture_replay_tests/D3D12EventTraceReplay.cpp @@ -487,7 +487,6 @@ class D3D12EventTraceReplay : public D3D12TestBase, public CaptureReplayTestWith HEAP_DESC resourceHeapDesc = {}; resourceHeapDesc.SizeInBytes = args["Heap"]["SizeInBytes"].asUInt64(); resourceHeapDesc.Alignment = args["Heap"]["Alignment"].asUInt64(); - resourceHeapDesc.IsExternal = args["IsExternal"].asBool(); resourceHeapDesc.MemorySegmentGroup = GetMemorySegmentGroup(heapProperties.MemoryPoolPreference, mIsUMA);