• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DAWNNATIVE_D3D12_RESOURCEALLOCATORMANAGERD3D12_H_
16 #define DAWNNATIVE_D3D12_RESOURCEALLOCATORMANAGERD3D12_H_
17 
18 #include "common/SerialQueue.h"
19 #include "dawn_native/BuddyMemoryAllocator.h"
20 #include "dawn_native/IntegerTypes.h"
21 #include "dawn_native/PooledResourceMemoryAllocator.h"
22 #include "dawn_native/d3d12/HeapAllocatorD3D12.h"
23 #include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h"
24 
25 #include <array>
26 
27 namespace dawn_native { namespace d3d12 {
28 
29     class Device;
30 
31     // Resource heap types + flags combinations are named after the D3D constants.
32     // https://docs.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_heap_flags
33     // https://docs.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_heap_type
34     enum ResourceHeapKind {
35 
36         // Resource heap tier 2
37         // Allows resource heaps to contain all buffer and textures types.
38         // This enables better heap re-use by avoiding the need for separate heaps and
39         // also reduces fragmentation.
40         Readback_AllBuffersAndTextures,
41         Upload_AllBuffersAndTextures,
42         Default_AllBuffersAndTextures,
43 
44         // Resource heap tier 1
45         // Resource heaps only support types from a single resource category.
46         Readback_OnlyBuffers,
47         Upload_OnlyBuffers,
48         Default_OnlyBuffers,
49 
50         Default_OnlyNonRenderableOrDepthTextures,
51         Default_OnlyRenderableOrDepthTextures,
52 
53         EnumCount,
54         InvalidEnum = EnumCount,
55     };
56 
57     // Manages a list of resource allocators used by the device to create resources using
58     // multiple allocation methods.
59     class ResourceAllocatorManager {
60       public:
61         ResourceAllocatorManager(Device* device);
62 
63         ResultOrError<ResourceHeapAllocation> AllocateMemory(
64             D3D12_HEAP_TYPE heapType,
65             const D3D12_RESOURCE_DESC& resourceDescriptor,
66             D3D12_RESOURCE_STATES initialUsage);
67 
68         void DeallocateMemory(ResourceHeapAllocation& allocation);
69 
70         void Tick(ExecutionSerial lastCompletedSerial);
71 
72         void DestroyPool();
73 
74       private:
75         void FreeMemory(ResourceHeapAllocation& allocation);
76 
77         ResultOrError<ResourceHeapAllocation> CreatePlacedResource(
78             D3D12_HEAP_TYPE heapType,
79             const D3D12_RESOURCE_DESC& requestedResourceDescriptor,
80             const D3D12_CLEAR_VALUE* optimizedClearValue,
81             D3D12_RESOURCE_STATES initialUsage);
82 
83         ResultOrError<ResourceHeapAllocation> CreateCommittedResource(
84             D3D12_HEAP_TYPE heapType,
85             const D3D12_RESOURCE_DESC& resourceDescriptor,
86             const D3D12_CLEAR_VALUE* optimizedClearValue,
87             D3D12_RESOURCE_STATES initialUsage);
88 
89         Device* mDevice;
90         uint32_t mResourceHeapTier;
91 
92         static constexpr uint64_t kMaxHeapSize = 32ll * 1024ll * 1024ll * 1024ll;  // 32GB
93         static constexpr uint64_t kMinHeapSize = 4ll * 1024ll * 1024ll;            // 4MB
94 
95         std::array<std::unique_ptr<BuddyMemoryAllocator>, ResourceHeapKind::EnumCount>
96             mSubAllocatedResourceAllocators;
97         std::array<std::unique_ptr<HeapAllocator>, ResourceHeapKind::EnumCount> mHeapAllocators;
98 
99         std::array<std::unique_ptr<PooledResourceMemoryAllocator>, ResourceHeapKind::EnumCount>
100             mPooledHeapAllocators;
101 
102         SerialQueue<ExecutionSerial, ResourceHeapAllocation> mAllocationsToDelete;
103     };
104 
105 }}  // namespace dawn_native::d3d12
106 
107 #endif  // DAWNNATIVE_D3D12_RESOURCEALLOCATORMANAGERD3D12_H_
108