D3D12: Move resource allocators from device into manager. Encapsulates resource allocation using a manager class. BUG=dawn:27 Change-Id: I7a496261dcede647d32e44d96ed27237bf418fb2 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/11742 Reviewed-by: Austin Eng <enga@chromium.org> Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Bryan Bernhart <bryan.bernhart@intel.com>
diff --git a/BUILD.gn b/BUILD.gn index ca4f82a..91d8108 100644 --- a/BUILD.gn +++ b/BUILD.gn
@@ -274,6 +274,8 @@ "src/dawn_native/d3d12/RenderPipelineD3D12.h", "src/dawn_native/d3d12/ResourceAllocator.cpp", "src/dawn_native/d3d12/ResourceAllocator.h", + "src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp", + "src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h", "src/dawn_native/d3d12/ResourceHeapD3D12.cpp", "src/dawn_native/d3d12/ResourceHeapD3D12.h", "src/dawn_native/d3d12/SamplerD3D12.cpp",
diff --git a/src/dawn_native/d3d12/DeviceD3D12.cpp b/src/dawn_native/d3d12/DeviceD3D12.cpp index 4c6eb3b..c45f8fe 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.cpp +++ b/src/dawn_native/d3d12/DeviceD3D12.cpp
@@ -31,6 +31,7 @@ #include "dawn_native/d3d12/QueueD3D12.h" #include "dawn_native/d3d12/RenderPipelineD3D12.h" #include "dawn_native/d3d12/ResourceAllocator.h" +#include "dawn_native/d3d12/ResourceAllocatorManagerD3D12.h" #include "dawn_native/d3d12/ResourceHeapD3D12.h" #include "dawn_native/d3d12/SamplerD3D12.h" #include "dawn_native/d3d12/ShaderModuleD3D12.h" @@ -68,6 +69,7 @@ mDescriptorHeapAllocator = std::make_unique<DescriptorHeapAllocator>(this); mMapRequestTracker = std::make_unique<MapRequestTracker>(this); mResourceAllocator = std::make_unique<ResourceAllocator>(this); + mResourceAllocatorManager = std::make_unique<ResourceAllocatorManager>(this); NextSerial(); @@ -334,29 +336,8 @@ return {}; } - size_t Device::GetD3D12HeapTypeToIndex(D3D12_HEAP_TYPE heapType) const { - ASSERT(heapType > 0); - ASSERT(static_cast<uint32_t>(heapType) <= kNumHeapTypes); - return heapType - 1; - } - void Device::DeallocateMemory(ResourceMemoryAllocation& allocation) { - if (allocation.GetAllocationMethod() == AllocationMethod::kInvalid) { - return; - } - CommittedResourceAllocator* allocator = nullptr; - D3D12_HEAP_PROPERTIES heapProp; - ToBackend(allocation.GetResourceHeap()) - ->GetD3D12Resource() - ->GetHeapProperties(&heapProp, nullptr); - const size_t heapTypeIndex = GetD3D12HeapTypeToIndex(heapProp.Type); - ASSERT(heapTypeIndex < kNumHeapTypes); - allocator = mDirectResourceAllocators[heapTypeIndex].get(); - allocator->Deallocate(allocation); - - // Invalidate the underlying resource heap in case the client accidentally - // calls DeallocateMemory again using the same allocation. - allocation.Invalidate(); + mResourceAllocatorManager->DeallocateMemory(allocation); } ResultOrError<ResourceMemoryAllocation> Device::AllocateMemory( @@ -364,22 +345,8 @@ const D3D12_RESOURCE_DESC& resourceDescriptor, D3D12_RESOURCE_STATES initialUsage, D3D12_HEAP_FLAGS heapFlags) { - const size_t heapTypeIndex = GetD3D12HeapTypeToIndex(heapType); - ASSERT(heapTypeIndex < kNumHeapTypes); - - // Get the direct allocator using a tightly sized heap (aka CreateCommittedResource). - CommittedResourceAllocator* allocator = mDirectResourceAllocators[heapTypeIndex].get(); - if (allocator == nullptr) { - mDirectResourceAllocators[heapTypeIndex] = - std::make_unique<CommittedResourceAllocator>(this, heapType); - allocator = mDirectResourceAllocators[heapTypeIndex].get(); - } - - ResourceMemoryAllocation allocation; - DAWN_TRY_ASSIGN(allocation, - allocator->Allocate(resourceDescriptor, initialUsage, heapFlags)); - - return allocation; + return mResourceAllocatorManager->AllocateMemory(heapType, resourceDescriptor, initialUsage, + heapFlags); } TextureBase* Device::WrapSharedHandle(const TextureDescriptor* descriptor,
diff --git a/src/dawn_native/d3d12/DeviceD3D12.h b/src/dawn_native/d3d12/DeviceD3D12.h index a856d10..a250fad 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.h +++ b/src/dawn_native/d3d12/DeviceD3D12.h
@@ -19,7 +19,7 @@ #include "common/SerialQueue.h" #include "dawn_native/Device.h" -#include "dawn_native/d3d12/CommittedResourceAllocatorD3D12.h" +#include "dawn_native/ResourceMemoryAllocation.h" #include "dawn_native/d3d12/Forward.h" #include "dawn_native/d3d12/d3d12_platform.h" @@ -32,6 +32,7 @@ class MapRequestTracker; class PlatformFunctions; class ResourceAllocator; + class ResourceAllocatorManager; #define ASSERT_SUCCESS(hr) \ { \ @@ -119,8 +120,6 @@ TextureBase* texture, const TextureViewDescriptor* descriptor) override; - size_t GetD3D12HeapTypeToIndex(D3D12_HEAP_TYPE heapType) const; - Serial mCompletedSerial = 0; Serial mLastSubmittedSerial = 0; ComPtr<ID3D12Fence> mFence; @@ -144,20 +143,7 @@ std::unique_ptr<DescriptorHeapAllocator> mDescriptorHeapAllocator; std::unique_ptr<MapRequestTracker> mMapRequestTracker; std::unique_ptr<ResourceAllocator> mResourceAllocator; - - static constexpr uint32_t kNumHeapTypes = 4u; // Number of D3D12_HEAP_TYPE - - static_assert(D3D12_HEAP_TYPE_READBACK <= kNumHeapTypes, - "Readback heap type enum exceeds max heap types"); - static_assert(D3D12_HEAP_TYPE_UPLOAD <= kNumHeapTypes, - "Upload heap type enum exceeds max heap types"); - static_assert(D3D12_HEAP_TYPE_DEFAULT <= kNumHeapTypes, - "Default heap type enum exceeds max heap types"); - static_assert(D3D12_HEAP_TYPE_CUSTOM <= kNumHeapTypes, - "Custom heap type enum exceeds max heap types"); - - std::array<std::unique_ptr<CommittedResourceAllocator>, kNumHeapTypes> - mDirectResourceAllocators; + std::unique_ptr<ResourceAllocatorManager> mResourceAllocatorManager; dawn_native::PCIInfo mPCIInfo; };
diff --git a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp new file mode 100644 index 0000000..e497388 --- /dev/null +++ b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp
@@ -0,0 +1,71 @@ +// Copyright 2019 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "dawn_native/d3d12/ResourceAllocatorManagerD3D12.h" +#include "dawn_native/d3d12/Forward.h" +#include "dawn_native/d3d12/ResourceHeapD3D12.h" + +namespace dawn_native { namespace d3d12 { + + ResourceAllocatorManager::ResourceAllocatorManager(Device* device) : mDevice(device) { + } + + ResultOrError<ResourceMemoryAllocation> ResourceAllocatorManager::AllocateMemory( + D3D12_HEAP_TYPE heapType, + const D3D12_RESOURCE_DESC& resourceDescriptor, + D3D12_RESOURCE_STATES initialUsage, + D3D12_HEAP_FLAGS heapFlags) { + const size_t heapTypeIndex = GetD3D12HeapTypeToIndex(heapType); + ASSERT(heapTypeIndex < kNumHeapTypes); + + // Get the direct allocator using a tightly sized heap (aka CreateCommittedResource). + CommittedResourceAllocator* allocator = mDirectResourceAllocators[heapTypeIndex].get(); + if (allocator == nullptr) { + mDirectResourceAllocators[heapTypeIndex] = + std::make_unique<CommittedResourceAllocator>(mDevice, heapType); + allocator = mDirectResourceAllocators[heapTypeIndex].get(); + } + + ResourceMemoryAllocation allocation; + DAWN_TRY_ASSIGN(allocation, + allocator->Allocate(resourceDescriptor, initialUsage, heapFlags)); + + return allocation; + } + + size_t ResourceAllocatorManager::GetD3D12HeapTypeToIndex(D3D12_HEAP_TYPE heapType) const { + ASSERT(heapType > 0); + ASSERT(static_cast<uint32_t>(heapType) <= kNumHeapTypes); + return heapType - 1; + } + + void ResourceAllocatorManager::DeallocateMemory(ResourceMemoryAllocation& allocation) { + if (allocation.GetAllocationMethod() == AllocationMethod::kInvalid) { + return; + } + CommittedResourceAllocator* allocator = nullptr; + D3D12_HEAP_PROPERTIES heapProp; + ToBackend(allocation.GetResourceHeap()) + ->GetD3D12Resource() + ->GetHeapProperties(&heapProp, nullptr); + const size_t heapTypeIndex = GetD3D12HeapTypeToIndex(heapProp.Type); + ASSERT(heapTypeIndex < kNumHeapTypes); + allocator = mDirectResourceAllocators[heapTypeIndex].get(); + allocator->Deallocate(allocation); + + // Invalidate the underlying resource heap in case the client accidentally + // calls DeallocateMemory again using the same allocation. + allocation.Invalidate(); + } +}} // namespace dawn_native::d3d12
diff --git a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h new file mode 100644 index 0000000..7de6c58 --- /dev/null +++ b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h
@@ -0,0 +1,62 @@ +// Copyright 2019 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef DAWNNATIVE_D3D12_RESOURCEALLOCATORMANAGERD3D12_H_ +#define DAWNNATIVE_D3D12_RESOURCEALLOCATORMANAGERD3D12_H_ + +#include "dawn_native/d3d12/CommittedResourceAllocatorD3D12.h" + +#include <array> + +namespace dawn_native { namespace d3d12 { + + class Device; + + // Manages a list of resource allocators used by the device to create resources using multiple + // allocation methods. + class ResourceAllocatorManager { + public: + ResourceAllocatorManager(Device* device); + + ResultOrError<ResourceMemoryAllocation> AllocateMemory( + D3D12_HEAP_TYPE heapType, + const D3D12_RESOURCE_DESC& resourceDescriptor, + D3D12_RESOURCE_STATES initialUsage, + D3D12_HEAP_FLAGS heapFlags); + + void DeallocateMemory(ResourceMemoryAllocation& allocation); + + private: + size_t GetD3D12HeapTypeToIndex(D3D12_HEAP_TYPE heapType) const; + + Device* mDevice; + + static constexpr uint32_t kNumHeapTypes = 4u; // Number of D3D12_HEAP_TYPE + + static_assert(D3D12_HEAP_TYPE_READBACK <= kNumHeapTypes, + "Readback heap type enum exceeds max heap types"); + static_assert(D3D12_HEAP_TYPE_UPLOAD <= kNumHeapTypes, + "Upload heap type enum exceeds max heap types"); + static_assert(D3D12_HEAP_TYPE_DEFAULT <= kNumHeapTypes, + "Default heap type enum exceeds max heap types"); + static_assert(D3D12_HEAP_TYPE_CUSTOM <= kNumHeapTypes, + "Custom heap type enum exceeds max heap types"); + + std::array<std::unique_ptr<CommittedResourceAllocator>, kNumHeapTypes> + mDirectResourceAllocators; + }; + +}} // namespace dawn_native::d3d12 + +#endif // DAWNNATIVE_D3D12_RESOURCEALLOCATORMANAGERD3D12_H_ \ No newline at end of file