34 #include "ObjectBase.h" 35 #include "VariableSizeGPUAllocationsManager.h" 40 class DescriptorHeapAllocation;
41 class DescriptorHeapAllocationManager;
42 class RenderDeviceD3D12Impl;
44 class IDescriptorAllocator
48 virtual DescriptorHeapAllocation Allocate( uint32_t Count ) = 0;
49 virtual void Free(DescriptorHeapAllocation&& Allocation) = 0;
50 virtual Uint32 GetDescriptorSize()
const = 0;
62 class DescriptorHeapAllocation
66 DescriptorHeapAllocation() :
68 m_pDescriptorHeap(nullptr),
71 m_FirstCpuHandle.ptr = 0;
72 m_FirstGpuHandle.ptr = 0;
76 DescriptorHeapAllocation( IDescriptorAllocator *pAllocator,
77 ID3D12DescriptorHeap *pHeap,
78 D3D12_CPU_DESCRIPTOR_HANDLE CpuHandle,
79 D3D12_GPU_DESCRIPTOR_HANDLE GpuHandle,
81 Uint16 AllocationManagerId = static_cast<Uint16>(-1) ) :
82 m_FirstCpuHandle(CpuHandle),
83 m_FirstGpuHandle(GpuHandle),
84 m_pAllocator(pAllocator),
85 m_NumHandles(NHandles),
86 m_pDescriptorHeap(pHeap),
87 m_AllocationManagerId(AllocationManagerId)
89 VERIFY_EXPR(m_pAllocator !=
nullptr && m_pDescriptorHeap !=
nullptr);
90 auto DescriptorSize = m_pAllocator->GetDescriptorSize();
91 VERIFY(DescriptorSize < std::numeric_limits<Uint16>::max(),
"DescriptorSize exceeds allowed limit");
92 m_DescriptorSize =
static_cast<Uint16
>( DescriptorSize );
96 DescriptorHeapAllocation(DescriptorHeapAllocation &&Allocation) :
97 m_FirstCpuHandle(Allocation.m_FirstCpuHandle),
98 m_FirstGpuHandle(Allocation.m_FirstGpuHandle),
99 m_NumHandles(Allocation.m_NumHandles),
100 m_pAllocator(
std::move(Allocation.m_pAllocator)),
101 m_AllocationManagerId(
std::move(Allocation.m_AllocationManagerId)),
102 m_pDescriptorHeap(
std::move(Allocation.m_pDescriptorHeap) ),
103 m_DescriptorSize(
std::move(Allocation.m_DescriptorSize) )
105 Allocation.m_pAllocator =
nullptr;
106 Allocation.m_FirstCpuHandle.ptr = 0;
107 Allocation.m_FirstGpuHandle.ptr = 0;
108 Allocation.m_NumHandles = 0;
109 Allocation.m_pDescriptorHeap =
nullptr;
110 Allocation.m_DescriptorSize = 0;
111 Allocation.m_AllocationManagerId =
static_cast<Uint16
>(-1);
115 DescriptorHeapAllocation& operator = (DescriptorHeapAllocation &&Allocation)
117 m_FirstCpuHandle = Allocation.m_FirstCpuHandle;
118 m_FirstGpuHandle = Allocation.m_FirstGpuHandle;
119 m_NumHandles = Allocation.m_NumHandles;
120 m_pAllocator = std::move(Allocation.m_pAllocator);
121 m_AllocationManagerId = std::move(Allocation.m_AllocationManagerId);
122 m_pDescriptorHeap = std::move(Allocation.m_pDescriptorHeap);
123 m_DescriptorSize = std::move(Allocation.m_DescriptorSize);
125 Allocation.m_FirstCpuHandle.ptr = 0;
126 Allocation.m_FirstGpuHandle.ptr = 0;
127 Allocation.m_NumHandles = 0;
128 Allocation.m_pAllocator =
nullptr;
129 Allocation.m_pDescriptorHeap =
nullptr;
130 Allocation.m_DescriptorSize = 0;
131 Allocation.m_AllocationManagerId =
static_cast<Uint16
>(-1);
137 ~DescriptorHeapAllocation()
139 if(!IsNull() && m_pAllocator)
140 m_pAllocator->Free(std::move(*
this));
142 VERIFY(IsNull(),
"Non-null descriptor is being destroyed");
146 D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandle(Uint32 Offset = 0)
const 148 VERIFY_EXPR(Offset >= 0 && Offset < m_NumHandles);
150 D3D12_CPU_DESCRIPTOR_HANDLE CPUHandle = m_FirstCpuHandle;
153 CPUHandle.ptr += m_DescriptorSize * Offset;
159 D3D12_GPU_DESCRIPTOR_HANDLE GetGpuHandle(Uint32 Offset = 0)
const 161 VERIFY_EXPR(Offset >= 0 && Offset < m_NumHandles);
162 D3D12_GPU_DESCRIPTOR_HANDLE GPUHandle = m_FirstGpuHandle;
165 GPUHandle.ptr += m_DescriptorSize * Offset;
171 ID3D12DescriptorHeap *GetDescriptorHeap(){
return m_pDescriptorHeap;}
173 size_t GetNumHandles()
const{
return m_NumHandles;}
175 bool IsNull()
const {
return m_FirstCpuHandle.ptr == 0; }
176 bool IsShaderVisible()
const {
return m_FirstGpuHandle.ptr != 0; }
177 size_t GetAllocationManagerId(){
return m_AllocationManagerId;}
178 UINT GetDescriptorSize()
const{
return m_DescriptorSize;}
182 DescriptorHeapAllocation(
const DescriptorHeapAllocation&) =
delete;
183 DescriptorHeapAllocation& operator= (
const DescriptorHeapAllocation&) =
delete;
186 D3D12_CPU_DESCRIPTOR_HANDLE m_FirstCpuHandle = {0};
189 D3D12_GPU_DESCRIPTOR_HANDLE m_FirstGpuHandle = {0};
195 IDescriptorAllocator* m_pAllocator =
nullptr;
198 ID3D12DescriptorHeap* m_pDescriptorHeap =
nullptr;
201 Uint32 m_NumHandles = 0;
207 Uint16 m_AllocationManagerId =
static_cast<Uint16
>(-1);
210 Uint16 m_DescriptorSize = 0;
222 class DescriptorHeapAllocationManager
226 DescriptorHeapAllocationManager(IMemoryAllocator &Allocator,
227 RenderDeviceD3D12Impl *pDeviceD3D12Impl,
228 IDescriptorAllocator *pParentAllocator,
229 size_t ThisManagerId,
230 const D3D12_DESCRIPTOR_HEAP_DESC &HeapDesc);
234 DescriptorHeapAllocationManager(IMemoryAllocator &Allocator,
235 RenderDeviceD3D12Impl *pDeviceD3D12Impl,
236 IDescriptorAllocator *pParentAllocator,
237 size_t ThisManagerId,
238 ID3D12DescriptorHeap *pd3d12DescriptorHeap,
239 Uint32 FirstDescriptor,
240 Uint32 NumDescriptors);
244 DescriptorHeapAllocationManager(DescriptorHeapAllocationManager&& rhs) :
245 m_FreeBlockManager(
std::move(rhs.m_FreeBlockManager)),
246 m_HeapDesc(rhs.m_HeapDesc),
247 m_pd3d12DescriptorHeap(
std::move(rhs.m_pd3d12DescriptorHeap)),
248 m_FirstCPUHandle(rhs.m_FirstCPUHandle),
249 m_FirstGPUHandle(rhs.m_FirstGPUHandle),
250 m_DescriptorSize(rhs.m_DescriptorSize),
251 m_NumDescriptorsInAllocation(rhs.m_NumDescriptorsInAllocation),
254 m_pDeviceD3D12Impl(rhs.m_pDeviceD3D12Impl),
255 m_pParentAllocator(rhs.m_pParentAllocator),
256 m_ThisManagerId(rhs.m_ThisManagerId)
258 rhs.m_FirstCPUHandle.ptr = 0;
259 rhs.m_FirstGPUHandle.ptr = 0;
260 rhs.m_DescriptorSize = 0;
261 rhs.m_NumDescriptorsInAllocation = 0;
262 rhs.m_HeapDesc.NumDescriptors = 0;
263 rhs.m_pDeviceD3D12Impl =
nullptr;
264 rhs.m_pParentAllocator =
nullptr;
265 rhs.m_ThisManagerId =
static_cast<size_t>(-1);
269 DescriptorHeapAllocationManager& operator = (DescriptorHeapAllocationManager&& rhs) =
delete;
270 DescriptorHeapAllocationManager(
const DescriptorHeapAllocationManager&) =
delete;
271 DescriptorHeapAllocationManager& operator = (
const DescriptorHeapAllocationManager&) =
delete;
273 ~DescriptorHeapAllocationManager();
276 DescriptorHeapAllocation Allocate( uint32_t Count );
281 void Free(DescriptorHeapAllocation&& Allocation);
286 void ReleaseStaleAllocations(Uint64 LastCompletedFenceValue);
288 size_t GetNumAvailableDescriptors()
const{
return m_FreeBlockManager.GetFreeSize();}
289 size_t GetNumStaleDescriptors()
const {
return m_FreeBlockManager.GetStaleAllocationsSize(); }
290 Uint32 GetMaxDescriptors()
const {
return m_NumDescriptorsInAllocation; }
294 VariableSizeGPUAllocationsManager m_FreeBlockManager;
297 D3D12_DESCRIPTOR_HEAP_DESC m_HeapDesc;
300 CComPtr<ID3D12DescriptorHeap> m_pd3d12DescriptorHeap;
303 D3D12_CPU_DESCRIPTOR_HANDLE m_FirstCPUHandle = {0};
306 D3D12_GPU_DESCRIPTOR_HANDLE m_FirstGPUHandle = {0};
308 UINT m_DescriptorSize = 0;
313 Uint32 m_NumDescriptorsInAllocation = 0;
315 std::mutex m_AllocationMutex;
316 RenderDeviceD3D12Impl *m_pDeviceD3D12Impl =
nullptr;
317 IDescriptorAllocator *m_pParentAllocator =
nullptr;
320 size_t m_ThisManagerId =
static_cast<size_t>(-1);
340 class CPUDescriptorHeap :
public IDescriptorAllocator
344 CPUDescriptorHeap(IMemoryAllocator &Allocator,
345 RenderDeviceD3D12Impl *pDeviceD3D12Impl,
346 Uint32 NumDescriptorsInHeap,
347 D3D12_DESCRIPTOR_HEAP_TYPE Type,
348 D3D12_DESCRIPTOR_HEAP_FLAGS Flags);
350 CPUDescriptorHeap(
const CPUDescriptorHeap&) =
delete;
351 CPUDescriptorHeap(CPUDescriptorHeap&&) =
delete;
352 CPUDescriptorHeap& operator = (
const CPUDescriptorHeap&) =
delete;
353 CPUDescriptorHeap& operator = (CPUDescriptorHeap&&) =
delete;
355 ~CPUDescriptorHeap();
357 virtual DescriptorHeapAllocation Allocate( uint32_t Count )
override;
358 virtual void Free(DescriptorHeapAllocation&& Allocation)
override;
359 virtual Uint32 GetDescriptorSize()
const override{
return m_DescriptorSize;}
364 void ReleaseStaleAllocations(Uint64 LastCompletedFenceValue);
369 std::vector<DescriptorHeapAllocationManager, STDAllocatorRawMem<DescriptorHeapAllocationManager> > m_HeapPool;
371 std::set<size_t, std::less<size_t>, STDAllocatorRawMem<size_t> > m_AvailableHeaps;
372 IMemoryAllocator &m_MemAllocator;
374 std::mutex m_AllocationMutex;
376 D3D12_DESCRIPTOR_HEAP_DESC m_HeapDesc;
377 RenderDeviceD3D12Impl *m_pDeviceD3D12Impl;
378 UINT m_DescriptorSize;
381 Uint32 m_MaxHeapSize = 0;
382 Uint32 m_MaxStaleSize = 0;
383 Uint32 m_CurrentSize = 0;
425 class GPUDescriptorHeap :
public IDescriptorAllocator
428 GPUDescriptorHeap(IMemoryAllocator &Allocator,
429 RenderDeviceD3D12Impl *pDevice,
430 Uint32 NumDescriptorsInHeap,
431 Uint32 NumDynamicDescriptors,
432 D3D12_DESCRIPTOR_HEAP_TYPE Type,
433 D3D12_DESCRIPTOR_HEAP_FLAGS Flags);
435 GPUDescriptorHeap(
const GPUDescriptorHeap&) =
delete;
436 GPUDescriptorHeap(GPUDescriptorHeap&&) =
delete;
437 GPUDescriptorHeap& operator = (
const GPUDescriptorHeap&) =
delete;
438 GPUDescriptorHeap& operator = (GPUDescriptorHeap&&) =
delete;
440 ~GPUDescriptorHeap();
442 virtual DescriptorHeapAllocation Allocate( uint32_t Count )
override;
443 virtual void Free(DescriptorHeapAllocation&& Allocation)
override;
444 virtual Uint32 GetDescriptorSize()
const override{
return m_DescriptorSize;}
446 DescriptorHeapAllocation AllocateDynamic( uint32_t Count );
451 void ReleaseStaleAllocations(Uint64 LastCompletedFenceValue);
453 const D3D12_DESCRIPTOR_HEAP_DESC &GetHeapDesc()
const{
return m_HeapDesc;}
454 Uint32 GetMaxStaticDescriptors()
const {
return m_HeapAllocationManager.GetMaxDescriptors(); }
455 Uint32 GetMaxDynamicDescriptors()
const {
return m_DynamicAllocationsManager.GetMaxDescriptors(); }
459 D3D12_DESCRIPTOR_HEAP_DESC m_HeapDesc;
460 CComPtr<ID3D12DescriptorHeap> m_pd3d12DescriptorHeap;
462 UINT m_DescriptorSize = 0;
464 std::mutex m_AllocMutex, m_DynAllocMutex;
466 DescriptorHeapAllocationManager m_HeapAllocationManager;
469 DescriptorHeapAllocationManager m_DynamicAllocationsManager;
471 RenderDeviceD3D12Impl *m_pDeviceD3D12;
472 Uint32 m_CurrentSize = 0;
474 Uint32 m_MaxHeapSize = 0;
475 Uint32 m_MaxStaleSize = 0;
476 Uint32 m_CurrentDynamicSize = 0;
478 Uint32 m_MaxDynamicSize = 0;
479 Uint32 m_MaxDynamicStaleSize = 0;
493 class DynamicSuballocationsManager :
public IDescriptorAllocator
496 DynamicSuballocationsManager(IMemoryAllocator &Allocator, GPUDescriptorHeap& ParentGPUHeap, Uint32 DynamicChunkSize);
498 DynamicSuballocationsManager(
const DynamicSuballocationsManager&) =
delete;
499 DynamicSuballocationsManager(DynamicSuballocationsManager&&) =
delete;
500 DynamicSuballocationsManager& operator = (
const DynamicSuballocationsManager&) =
delete;
501 DynamicSuballocationsManager& operator = (DynamicSuballocationsManager&&) =
delete;
503 void DiscardAllocations(Uint64 );
505 virtual DescriptorHeapAllocation Allocate( Uint32 Count )
override;
506 virtual void Free(DescriptorHeapAllocation&& Allocation)
override;
508 virtual Uint32 GetDescriptorSize()
const override{
return m_ParentGPUHeap.GetDescriptorSize();}
513 std::vector<DescriptorHeapAllocation, STDAllocatorRawMem<DescriptorHeapAllocation> > m_Suballocations;
515 Uint32 m_CurrentSuballocationOffset = 0;
516 Uint32 m_DynamicChunkSize = 0;
519 GPUDescriptorHeap &m_ParentGPUHeap;
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
Definition: AdvancedMath.h:316