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>
This commit is contained in:
Bryan Bernhart 2019-10-02 21:45:09 +00:00 committed by Commit Bot service account
parent 867f72058a
commit 0da52f2d92
5 changed files with 143 additions and 55 deletions

View File

@ -274,6 +274,8 @@ source_set("libdawn_native_sources") {
"src/dawn_native/d3d12/RenderPipelineD3D12.h", "src/dawn_native/d3d12/RenderPipelineD3D12.h",
"src/dawn_native/d3d12/ResourceAllocator.cpp", "src/dawn_native/d3d12/ResourceAllocator.cpp",
"src/dawn_native/d3d12/ResourceAllocator.h", "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.cpp",
"src/dawn_native/d3d12/ResourceHeapD3D12.h", "src/dawn_native/d3d12/ResourceHeapD3D12.h",
"src/dawn_native/d3d12/SamplerD3D12.cpp", "src/dawn_native/d3d12/SamplerD3D12.cpp",

View File

@ -31,6 +31,7 @@
#include "dawn_native/d3d12/QueueD3D12.h" #include "dawn_native/d3d12/QueueD3D12.h"
#include "dawn_native/d3d12/RenderPipelineD3D12.h" #include "dawn_native/d3d12/RenderPipelineD3D12.h"
#include "dawn_native/d3d12/ResourceAllocator.h" #include "dawn_native/d3d12/ResourceAllocator.h"
#include "dawn_native/d3d12/ResourceAllocatorManagerD3D12.h"
#include "dawn_native/d3d12/ResourceHeapD3D12.h" #include "dawn_native/d3d12/ResourceHeapD3D12.h"
#include "dawn_native/d3d12/SamplerD3D12.h" #include "dawn_native/d3d12/SamplerD3D12.h"
#include "dawn_native/d3d12/ShaderModuleD3D12.h" #include "dawn_native/d3d12/ShaderModuleD3D12.h"
@ -68,6 +69,7 @@ namespace dawn_native { namespace d3d12 {
mDescriptorHeapAllocator = std::make_unique<DescriptorHeapAllocator>(this); mDescriptorHeapAllocator = std::make_unique<DescriptorHeapAllocator>(this);
mMapRequestTracker = std::make_unique<MapRequestTracker>(this); mMapRequestTracker = std::make_unique<MapRequestTracker>(this);
mResourceAllocator = std::make_unique<ResourceAllocator>(this); mResourceAllocator = std::make_unique<ResourceAllocator>(this);
mResourceAllocatorManager = std::make_unique<ResourceAllocatorManager>(this);
NextSerial(); NextSerial();
@ -334,29 +336,8 @@ namespace dawn_native { namespace d3d12 {
return {}; 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) { void Device::DeallocateMemory(ResourceMemoryAllocation& allocation) {
if (allocation.GetAllocationMethod() == AllocationMethod::kInvalid) { mResourceAllocatorManager->DeallocateMemory(allocation);
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();
} }
ResultOrError<ResourceMemoryAllocation> Device::AllocateMemory( ResultOrError<ResourceMemoryAllocation> Device::AllocateMemory(
@ -364,22 +345,8 @@ namespace dawn_native { namespace d3d12 {
const D3D12_RESOURCE_DESC& resourceDescriptor, const D3D12_RESOURCE_DESC& resourceDescriptor,
D3D12_RESOURCE_STATES initialUsage, D3D12_RESOURCE_STATES initialUsage,
D3D12_HEAP_FLAGS heapFlags) { D3D12_HEAP_FLAGS heapFlags) {
const size_t heapTypeIndex = GetD3D12HeapTypeToIndex(heapType); return mResourceAllocatorManager->AllocateMemory(heapType, resourceDescriptor, initialUsage,
ASSERT(heapTypeIndex < kNumHeapTypes); heapFlags);
// 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;
} }
TextureBase* Device::WrapSharedHandle(const TextureDescriptor* descriptor, TextureBase* Device::WrapSharedHandle(const TextureDescriptor* descriptor,

View File

@ -19,7 +19,7 @@
#include "common/SerialQueue.h" #include "common/SerialQueue.h"
#include "dawn_native/Device.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/Forward.h"
#include "dawn_native/d3d12/d3d12_platform.h" #include "dawn_native/d3d12/d3d12_platform.h"
@ -32,6 +32,7 @@ namespace dawn_native { namespace d3d12 {
class MapRequestTracker; class MapRequestTracker;
class PlatformFunctions; class PlatformFunctions;
class ResourceAllocator; class ResourceAllocator;
class ResourceAllocatorManager;
#define ASSERT_SUCCESS(hr) \ #define ASSERT_SUCCESS(hr) \
{ \ { \
@ -119,8 +120,6 @@ namespace dawn_native { namespace d3d12 {
TextureBase* texture, TextureBase* texture,
const TextureViewDescriptor* descriptor) override; const TextureViewDescriptor* descriptor) override;
size_t GetD3D12HeapTypeToIndex(D3D12_HEAP_TYPE heapType) const;
Serial mCompletedSerial = 0; Serial mCompletedSerial = 0;
Serial mLastSubmittedSerial = 0; Serial mLastSubmittedSerial = 0;
ComPtr<ID3D12Fence> mFence; ComPtr<ID3D12Fence> mFence;
@ -144,20 +143,7 @@ namespace dawn_native { namespace d3d12 {
std::unique_ptr<DescriptorHeapAllocator> mDescriptorHeapAllocator; std::unique_ptr<DescriptorHeapAllocator> mDescriptorHeapAllocator;
std::unique_ptr<MapRequestTracker> mMapRequestTracker; std::unique_ptr<MapRequestTracker> mMapRequestTracker;
std::unique_ptr<ResourceAllocator> mResourceAllocator; std::unique_ptr<ResourceAllocator> mResourceAllocator;
std::unique_ptr<ResourceAllocatorManager> mResourceAllocatorManager;
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;
dawn_native::PCIInfo mPCIInfo; dawn_native::PCIInfo mPCIInfo;
}; };

View File

@ -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

View File

@ -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_