UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
D3D12PoolAllocator.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "RHIPoolAllocator.h"
5#include "D3D12Resources.h"
6
8{
9 // This strategy uses Placed Resources to sub-allocate a buffer out of an underlying ID3D12Heap.
10 // The benefit of this is that each buffer can have it's own resource state and can be treated
11 // as any other buffer. The downside of this strategy is the API limitation which enforces
12 // the minimum buffer size to 64k leading to large internal fragmentation in the allocator
14 // The alternative is to manually sub-allocate out of a single large buffer which allows block
15 // allocation granularity down to 1 byte. However, this strategy is only really valid for buffers which
16 // will be treated as read-only after their creation (i.e. most Index and Vertex buffers). This
17 // is because the underlying resource can only have one state at a time.
19};
20
21
22// All data required to create a pool
24{
26 {
28 Config.HeapType = D3D12_HEAP_TYPE_UPLOAD;
29 Config.HeapFlags = D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS;
30 Config.ResourceFlags = D3D12_RESOURCE_FLAG_NONE;
31 Config.InitialD3D12Access = ED3D12Access::GenericRead;
32 return Config;
33 }
34
36 {
37 return HeapType == InOther.HeapType &&
38 HeapFlags == InOther.HeapFlags &&
39 ResourceFlags == InOther.ResourceFlags &&
40 InitialD3D12Access == InOther.InitialD3D12Access;
41 }
42
47};
48
49
70
71// Heap and offset combined to specific location of a resource
77
78
80// FD3D12Pool
82
87{
88public:
89
90 // Constructor
93 virtual ~FD3D12MemoryPool();
94
95 // Setup/Shutdown
96 virtual void Init() override;
97 virtual void Destroy() override;
98
101
104
105protected:
106
108 const FString Name;
110
111 // Actual D3D resource - either resource or heap (see EResourceAllocationStrategy)
114
116
117private:
118 HeapId TraceHeapId;
119};
120
121
123// FD3D12PoolAllocator
125
130{
131public:
132
133 // Constructor
137
138 // Function names currently have to match with FD3D12DefaultBufferPool until we can make full replacement of the allocator
147 const TCHAR* InName,
148 FD3D12ResourceLocation& ResourceLocation);
149 virtual void DeallocateResource(FD3D12ResourceLocation& ResourceLocation, bool bDefragFree = false);
150
151 // Implementation of ID3D12ResourceAllocator
152 virtual void AllocateResource(
153 uint32 GPUIndex,
162 const TCHAR* InName,
163 FD3D12ResourceLocation& ResourceLocation) override;
164
167
169 bool IsOwner(FD3D12ResourceLocation& ResourceLocation) const { return ResourceLocation.GetPoolAllocator() == this; }
170
176
179
180protected:
181
182 // Implementation of FRHIPoolAllocator pure virtuals
185
186 // Placed resource allocation helper function which can be overriden
194 const TCHAR* InName);
195
196 // Track the allocation
198 {
199 Allocate, // Update tracking data on allocation
200 DefragFree, // Update tracking data on free during defrag op
201 Free // Update tracking data when resource is actually freed
202 };
204
205 // Locked allocation data which needs to do something specific at certain frame fence value
221
223 const FString Name;
225
226 // All operations which need to happen on specific frame fences
228
229 // Size of all pending delete requests in the frame fence operation array
231
232 // Pending copy ops which need to be flush this frame
234
235 // Allocation data pool used to reduce allocation overhead
237
238private:
239 HeapId TraceHeapId;
240};
241
#define check(expr)
Definition AssertionMacros.h:314
FPlatformTypes::int16 int16
A 16-bit signed integer.
Definition Platform.h:1123
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
ED3D12Access
Definition D3D12Access.h:15
EResourceAllocationStrategy
Definition D3D12PoolAllocator.h:8
ED3D12ResourceStateMode
Definition D3D12Resources.h:52
uint32 HeapId
Definition MemoryTrace.h:30
EBufferUsageFlags
Definition RHIDefinitions.h:892
ERHIPoolResourceTypes
Resource type supported by the pools (can be any or all)
Definition RHIPoolAllocator.h:27
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition D3D12CommandContext.h:513
Definition D3D12RHICommon.h:78
Definition D3D12Device.h:176
Definition D3D12Resources.h:73
D3D12 specific implementation of the FRHIMemoryPool.
Definition D3D12PoolAllocator.h:87
uint64 GetLastUsedFrameFence() const
Definition D3D12PoolAllocator.h:102
const FString Name
Definition D3D12PoolAllocator.h:108
FD3D12Resource * GetBackingResource()
Definition D3D12PoolAllocator.h:99
const FD3D12ResourceInitConfig InitConfig
Definition D3D12PoolAllocator.h:107
uint64 LastUsedFrameFence
Definition D3D12PoolAllocator.h:115
EResourceAllocationStrategy AllocationStrategy
Definition D3D12PoolAllocator.h:109
TRefCountPtr< FD3D12Resource > BackingResource
Definition D3D12PoolAllocator.h:112
FD3D12Heap * GetBackingHeap()
Definition D3D12PoolAllocator.h:100
virtual ~FD3D12MemoryPool()
Definition D3D12PoolAllocator.cpp:40
void UpdateLastUsedFrameFence(uint64 InFrameFence)
Definition D3D12PoolAllocator.h:103
virtual void Destroy() override
Definition D3D12PoolAllocator.cpp:159
TRefCountPtr< FD3D12Heap > BackingHeap
Definition D3D12PoolAllocator.h:113
virtual void Init() override
Definition D3D12PoolAllocator.cpp:45
Definition D3D12RHICommon.h:153
D3D12 specific implementation of the FRHIPoolAllocator.
Definition D3D12PoolAllocator.h:130
virtual void DeallocateResource(FD3D12ResourceLocation &ResourceLocation, bool bDefragFree=false)
Definition D3D12PoolAllocator.cpp:571
FD3D12Resource * GetBackingResource(FD3D12ResourceLocation &InResourceLocation) const
Definition D3D12PoolAllocator.cpp:875
~FD3D12PoolAllocator()
Definition D3D12PoolAllocator.cpp:257
void CleanUpAllocations(uint64 InFrameLag, bool bForceFree=false)
Definition D3D12PoolAllocator.cpp:756
TArray< FrameFencedAllocationData > FrameFencedOperations
Definition D3D12PoolAllocator.h:227
uint64 PendingDeleteRequestSize
Definition D3D12PoolAllocator.h:230
FD3D12HeapAndOffset GetBackingHeapAndAllocationOffsetInBytes(FD3D12ResourceLocation &InResourceLocation) const
Definition D3D12PoolAllocator.cpp:883
virtual FD3D12Resource * CreatePlacedResource(const FRHIPoolAllocationData &InAllocationData, const FD3D12ResourceDesc &InDesc, ED3D12Access InInitialD3D12Access, ED3D12ResourceStateMode InResourceStateMode, ED3D12Access InDefaultD3D12Access, const D3D12_CLEAR_VALUE *InClearValue, const TCHAR *InName)
Definition D3D12PoolAllocator.cpp:543
EResourceAllocationStrategy AllocationStrategy
Definition D3D12PoolAllocator.h:224
EAllocationType
Definition D3D12PoolAllocator.h:198
TArray< FD3D12VRAMCopyOperation > PendingCopyOps
Definition D3D12PoolAllocator.h:233
const FString Name
Definition D3D12PoolAllocator.h:223
virtual FRHIMemoryPool * CreateNewPool(int16 InPoolIndex, uint32 InMinimumAllocationSize, ERHIPoolResourceTypes InAllocationResourceType) override
Definition D3D12PoolAllocator.cpp:646
uint64 GetPendingDeleteRequestSize() const
Definition D3D12PoolAllocator.h:175
bool SupportsAllocation(D3D12_HEAP_TYPE InHeapType, D3D12_RESOURCE_FLAGS InResourceFlags, EBufferUsageFlags InBufferUsage, ED3D12ResourceStateMode InResourceStateMode, uint32 Alignment) const
Definition D3D12PoolAllocator.cpp:263
virtual bool HandleDefragRequest(FRHIContextArray const &Contexts, FRHIPoolAllocationData *InSourceBlock, FRHIPoolAllocationData &InTmpTargetBlock) override
Definition D3D12PoolAllocator.cpp:666
bool IsOwner(FD3D12ResourceLocation &ResourceLocation) const
Definition D3D12PoolAllocator.h:169
virtual void UpdateAllocationTracking(FD3D12ResourceLocation &InAllocation, EAllocationType InAllocationType)
Definition D3D12PoolAllocator.cpp:900
EResourceAllocationStrategy GetAllocationStrategy() const
Definition D3D12PoolAllocator.h:171
void AllocDefaultResource(D3D12_HEAP_TYPE InHeapType, const FD3D12ResourceDesc &InDesc, EBufferUsageFlags InBufferUsage, ED3D12ResourceStateMode InResourceStateMode, ED3D12Access InCreateD3D12Access, uint32 InAlignment, const TCHAR *InName, FD3D12ResourceLocation &ResourceLocation)
Definition D3D12PoolAllocator.cpp:288
void FlushPendingCopyOps(FD3D12CommandContext &InCommandContext)
Definition D3D12PoolAllocator.cpp:952
void TransferOwnership(FD3D12ResourceLocation &InSource, FD3D12ResourceLocation &InDest)
Definition D3D12PoolAllocator.cpp:842
static EResourceAllocationStrategy GetResourceAllocationStrategy(D3D12_RESOURCE_FLAGS InResourceFlags, ED3D12ResourceStateMode InResourceStateMode, uint32 Alignment)
Definition D3D12PoolAllocator.cpp:223
static FD3D12ResourceInitConfig GetResourceAllocatorInitConfig(D3D12_HEAP_TYPE InHeapType, D3D12_RESOURCE_FLAGS InResourceFlags, EBufferUsageFlags InBufferUsage)
Definition D3D12PoolAllocator.cpp:184
TArray< FRHIPoolAllocationData * > AllocationDataPool
Definition D3D12PoolAllocator.h:236
virtual void AllocateResource(uint32 GPUIndex, D3D12_HEAP_TYPE InHeapType, const FD3D12ResourceDesc &InDesc, uint64 InSize, uint32 InAllocationAlignment, ED3D12Access InInitialD3D12Access, ED3D12ResourceStateMode InResourceStateMode, ED3D12Access InDefaultD3D12Access, const D3D12_CLEAR_VALUE *InClearValue, const TCHAR *InName, FD3D12ResourceLocation &ResourceLocation) override
Definition D3D12PoolAllocator.cpp:337
const FD3D12ResourceInitConfig InitConfig
Definition D3D12PoolAllocator.h:222
Definition D3D12Resources.h:641
FD3D12PoolAllocator * GetPoolAllocator()
Definition D3D12Resources.h:699
Definition D3D12Resources.h:181
Definition RHIResources.h:5541
Pool which stores for each allocation the previous and next allocation. Each block is either free or ...
Definition RHIPoolAllocator.h:135
EFreeListOrder
Definition RHIPoolAllocator.h:139
Manages an array of FRHIMemoryPool and supports defragmentation between multiple pools.
Definition RHIPoolAllocator.h:220
Definition Array.h:670
Definition RefCounting.h:454
UE_FORCEINLINE_HINT ReferencedType * GetReference() const
Definition RefCounting.h:584
Definition D3D12PoolAllocator.h:73
FD3D12Heap * Heap
Definition D3D12PoolAllocator.h:74
uint64 Offset
Definition D3D12PoolAllocator.h:75
Definition D3D12PoolAllocator.h:207
EOperation Operation
Definition D3D12PoolAllocator.h:216
FRHIPoolAllocationData * AllocationData
Definition D3D12PoolAllocator.h:219
FD3D12Resource * PlacedResource
Definition D3D12PoolAllocator.h:217
uint64 FrameFence
Definition D3D12PoolAllocator.h:218
EOperation
Definition D3D12PoolAllocator.h:209
Definition D3D12Resources.h:134
Definition D3D12PoolAllocator.h:24
D3D12_RESOURCE_FLAGS ResourceFlags
Definition D3D12PoolAllocator.h:45
D3D12_HEAP_TYPE HeapType
Definition D3D12PoolAllocator.h:43
ED3D12Access InitialD3D12Access
Definition D3D12PoolAllocator.h:46
bool operator==(const FD3D12ResourceInitConfig &InOther) const
Definition D3D12PoolAllocator.h:35
D3D12_HEAP_FLAGS HeapFlags
Definition D3D12PoolAllocator.h:44
static FD3D12ResourceInitConfig CreateUpload()
Definition D3D12PoolAllocator.h:25
Stores all required data for a VRAM copy operation - doesn't own the resources.
Definition D3D12PoolAllocator.h:54
uint32 Size
Definition D3D12PoolAllocator.h:67
ED3D12Access SrcResourcED3D12Access
Definition D3D12PoolAllocator.h:62
ECopyType CopyType
Definition D3D12PoolAllocator.h:68
FD3D12Resource * SrcResource
Definition D3D12PoolAllocator.h:61
uint32 DstOffset
Definition D3D12PoolAllocator.h:66
ECopyType
Definition D3D12PoolAllocator.h:56
@ BufferRegion
Definition D3D12PoolAllocator.h:57
@ Resource
Definition D3D12PoolAllocator.h:58
FD3D12Resource * DstResource
Definition D3D12PoolAllocator.h:64
ED3D12Access DstResourcED3D12Access
Definition D3D12PoolAllocator.h:65
uint32 SrcOffset
Definition D3D12PoolAllocator.h:63
Definition MultiGPU.h:33
Pool allocator internal data.
Definition RHIPoolAllocator.h:39
Definition D3D12Resources.h:827