Nameless Engine
Loading...
Searching...
No Matches
DirectXDescriptorHeap.h
1#pragma once
2
3// Standard.
4#include <variant>
5#include <memory>
6#include <unordered_set>
7#include <mutex>
8#include <atomic>
9#include <queue>
10#include <functional>
11
12// Custom.
13#include "render/directx/descriptors/DirectXDescriptor.h"
14#include "render/general/resource/GpuResource.h"
15#include "DirectXDescriptorType.hpp"
16
17// External.
18#include "directx/d3dx12.h"
19#include "misc/Error.h"
20
21// OS.
22#include <wrl.h>
23
24namespace ne {
25 using namespace Microsoft::WRL;
26
27 class DirectXRenderer;
28 class DirectXResource;
29 class DirectXDescriptorHeap;
30
32 enum class DescriptorHeapType : int {
33 RTV = 0,
34 DSV,
35 CBV_SRV_UAV,
36 };
37
45 // This class is fully managed by the heap.
46 friend class DirectXDescriptorHeap;
47
48 public:
50
51 /* Notifies the heap. */
53
56
57 // Intentionally disable `move` because heap will store a raw pointer to the range
58 // and we don't want to accidentally `move` the range which will make the heap's raw pointer invalid.
61 operator=(ContinuousDirectXDescriptorRange&& other) noexcept = delete;
62
68 static constexpr INT getRangeGrowSize() { return iRangeGrowSize; }
69
75 size_t getRangeSize();
76
82 size_t getRangeCapacity();
83
90
99 D3D12_GPU_DESCRIPTOR_HANDLE getGpuDescriptorHandleToRangeStart() const;
100
106 std::string getRangeName() const { return sRangeName; }
107
108 private:
119 std::unordered_set<DirectXDescriptor*> allocatedDescriptors;
120
123
131
134
144 };
145
157 const std::function<void()>& onRangeIndicesChanged,
158 const std::string& sRangeName);
159
170 [[nodiscard]] std::optional<Error> markDescriptorAsUnused(DirectXDescriptor* pDescriptor);
171
182 std::variant<std::optional<INT>, Error> tryReserveFreeHeapIndexToCreateDescriptor();
183
185 std::pair<std::recursive_mutex, InternalData> mtxInternalData;
186
188 const std::function<void()> onRangeIndicesChanged;
189
191 const std::string sRangeName;
192
194 DirectXDescriptorHeap* const pHeap = nullptr;
195
201 static constexpr INT iRangeGrowSize = 50; // NOLINT: see static asserts
202 };
203
206 // Notifies the heap about descriptor being destroyed.
207 friend class DirectXDescriptor;
208
209 // Notifies the heap about range being destroyed.
211
212 // Only resource can request descriptors.
213 friend class DirectXResource;
214
215 public:
219 ComPtr<ID3D12DescriptorHeap> pHeap;
220
227 std::unordered_set<ContinuousDirectXDescriptorRange*> continuousDescriptorRanges;
228
231
237 INT iHeapSize = 0;
238
248
256
266 std::unordered_set<DirectXDescriptor*> bindedSingleDescriptors;
267 };
268
269 DirectXDescriptorHeap() = delete;
271 DirectXDescriptorHeap& operator=(const DirectXDescriptorHeap&) = delete;
272
275
281 static constexpr INT getHeapGrowSize() { return iHeapGrowSize; }
282
291 static std::variant<std::unique_ptr<DirectXDescriptorHeap>, Error>
292 create(DirectXRenderer* pRenderer, DescriptorHeapType heapType);
293
304 std::variant<std::shared_ptr<ContinuousDirectXDescriptorRange>, Error>
306 const std::string& sRangeName, const std::function<void()>& onRangeIndicesChanged);
307
316 INT getHeapCapacity();
317
326 INT getHeapSize();
327
334
340 UINT getDescriptorSize() const { return iDescriptorSize; }
341
347 ID3D12DescriptorHeap* getInternalHeap() const { return mtxInternalData.second.pHeap.Get(); }
348
356 std::pair<std::recursive_mutex, InternalData>* getInternalData() { return &mtxInternalData; }
357
358 protected:
366 static std::string convertHeapTypeToString(DescriptorHeapType heapType);
367
375
387 DirectXDescriptor* pDescriptor, ContinuousDirectXDescriptorRange* pRange = nullptr);
388
397
408 void createView(
409 CD3DX12_CPU_DESCRIPTOR_HANDLE heapHandle,
410 const DirectXResource* pResource,
411 DirectXDescriptorType descriptorType,
412 std::optional<size_t> cubemapFaceIndex) const;
413
424 [[nodiscard]] std::optional<Error> expandHeap(ContinuousDirectXDescriptorRange* pChangedRange);
425
439 [[nodiscard]] std::variant<bool, Error>
441
454 [[nodiscard]] std::optional<Error>
455 createHeap(INT iCapacity, ContinuousDirectXDescriptorRange* pChangedRange);
456
464 std::vector<DirectXDescriptorType> getDescriptorTypesHandledByThisHeap() const;
465
474 [[nodiscard]] std::optional<Error> rebindViewsUpdateIndices();
475
476 private:
496 [[nodiscard]] std::optional<Error> assignDescriptor(
497 DirectXResource* pResource,
498 DirectXDescriptorType descriptorType,
499 const std::shared_ptr<ContinuousDirectXDescriptorRange>& pRange = nullptr,
500 bool bBindDescriptorsToCubemapFaces = true);
501
511 static bool isShrinkingPossible(INT iSize, INT iCapacity, INT iGrowSize);
512
520 [[nodiscard]] std::optional<Error> expandRange(ContinuousDirectXDescriptorRange* pRange);
521
524
526 std::pair<std::recursive_mutex, InternalData> mtxInternalData;
527
530
532 DescriptorHeapType heapType;
533
535 std::string sHeapType;
536
538 D3D12_DESCRIPTOR_HEAP_TYPE d3dHeapType;
539
541 static constexpr INT iHeapGrowSize = 300; // NOLINT: don't recreate heap too often
542 };
543} // namespace ne
Definition: DirectXDescriptorHeap.h:44
size_t getRangeSize()
Definition: DirectXDescriptorHeap.cpp:1047
const std::string sRangeName
Definition: DirectXDescriptorHeap.h:191
std::variant< std::optional< INT >, Error > tryReserveFreeHeapIndexToCreateDescriptor()
Definition: DirectXDescriptorHeap.cpp:1109
INT getRangeStartInHeap()
Definition: DirectXDescriptorHeap.cpp:1057
size_t getRangeCapacity()
Definition: DirectXDescriptorHeap.cpp:1052
D3D12_GPU_DESCRIPTOR_HANDLE getGpuDescriptorHandleToRangeStart() const
Definition: DirectXDescriptorHeap.cpp:1062
const std::function< void()> onRangeIndicesChanged
Definition: DirectXDescriptorHeap.h:188
DirectXDescriptorHeap *const pHeap
Definition: DirectXDescriptorHeap.h:194
std::optional< Error > markDescriptorAsUnused(DirectXDescriptor *pDescriptor)
Definition: DirectXDescriptorHeap.cpp:1087
static constexpr INT iRangeGrowSize
Definition: DirectXDescriptorHeap.h:201
static constexpr INT getRangeGrowSize()
Definition: DirectXDescriptorHeap.h:68
std::pair< std::recursive_mutex, InternalData > mtxInternalData
Definition: DirectXDescriptorHeap.h:185
std::string getRangeName() const
Definition: DirectXDescriptorHeap.h:106
Definition: DirectXDescriptorHeap.h:205
INT getHeapSize()
Definition: DirectXDescriptorHeap.cpp:216
static std::string convertHeapTypeToString(DescriptorHeapType heapType)
Definition: DirectXDescriptorHeap.cpp:226
void onDescriptorRangeBeingDestroyed(ContinuousDirectXDescriptorRange *pRange)
Definition: DirectXDescriptorHeap.cpp:426
static std::variant< std::unique_ptr< DirectXDescriptorHeap >, Error > create(DirectXRenderer *pRenderer, DescriptorHeapType heapType)
Definition: DirectXDescriptorHeap.cpp:10
DirectXRenderer * pRenderer
Definition: DirectXDescriptorHeap.h:523
static constexpr INT getHeapGrowSize()
Definition: DirectXDescriptorHeap.h:281
std::variant< std::shared_ptr< ContinuousDirectXDescriptorRange >, Error > allocateContinuousDescriptorRange(const std::string &sRangeName, const std::function< void()> &onRangeIndicesChanged)
Definition: DirectXDescriptorHeap.cpp:24
void onDescriptorBeingDestroyed(DirectXDescriptor *pDescriptor, ContinuousDirectXDescriptorRange *pRange=nullptr)
Definition: DirectXDescriptorHeap.cpp:319
D3D12_DESCRIPTOR_HEAP_TYPE d3dHeapType
Definition: DirectXDescriptorHeap.h:538
std::string sHeapType
Definition: DirectXDescriptorHeap.h:535
std::optional< Error > expandRange(ContinuousDirectXDescriptorRange *pRange)
Definition: DirectXDescriptorHeap.cpp:541
std::variant< bool, Error > shrinkHeapIfPossible(ContinuousDirectXDescriptorRange *pChangedRange)
Definition: DirectXDescriptorHeap.cpp:573
~DirectXDescriptorHeap()
Definition: DirectXDescriptorHeap.cpp:285
std::vector< DirectXDescriptorType > getDescriptorTypesHandledByThisHeap() const
Definition: DirectXDescriptorHeap.cpp:912
UINT getDescriptorSize() const
Definition: DirectXDescriptorHeap.h:340
std::optional< Error > assignDescriptor(DirectXResource *pResource, DirectXDescriptorType descriptorType, const std::shared_ptr< ContinuousDirectXDescriptorRange > &pRange=nullptr, bool bBindDescriptorsToCubemapFaces=true)
Definition: DirectXDescriptorHeap.cpp:46
static bool isShrinkingPossible(INT iSize, INT iCapacity, INT iGrowSize)
Definition: DirectXDescriptorHeap.cpp:519
std::pair< std::recursive_mutex, InternalData > mtxInternalData
Definition: DirectXDescriptorHeap.h:526
DescriptorHeapType heapType
Definition: DirectXDescriptorHeap.h:532
std::pair< std::recursive_mutex, InternalData > * getInternalData()
Definition: DirectXDescriptorHeap.h:356
std::optional< Error > expandHeap(ContinuousDirectXDescriptorRange *pChangedRange)
Definition: DirectXDescriptorHeap.cpp:471
size_t getNoLongerUsedDescriptorCount()
Definition: DirectXDescriptorHeap.cpp:221
std::optional< Error > rebindViewsUpdateIndices()
Definition: DirectXDescriptorHeap.cpp:927
UINT iDescriptorSize
Definition: DirectXDescriptorHeap.h:529
ID3D12DescriptorHeap * getInternalHeap() const
Definition: DirectXDescriptorHeap.h:347
void createView(CD3DX12_CPU_DESCRIPTOR_HANDLE heapHandle, const DirectXResource *pResource, DirectXDescriptorType descriptorType, std::optional< size_t > cubemapFaceIndex) const
Definition: DirectXDescriptorHeap.cpp:595
INT getHeapCapacity()
Definition: DirectXDescriptorHeap.cpp:211
static constexpr INT iHeapGrowSize
Definition: DirectXDescriptorHeap.h:541
std::optional< Error > createHeap(INT iCapacity, ContinuousDirectXDescriptorRange *pChangedRange)
Definition: DirectXDescriptorHeap.cpp:858
Definition: DirectXDescriptor.h:21
Definition: DirectXRenderer.h:36
Definition: DirectXResource.h:32
Definition: Error.h:27
Definition: DirectXDescriptorHeap.h:110
std::unordered_set< DirectXDescriptor * > allocatedDescriptors
Definition: DirectXDescriptorHeap.h:119
INT iRangeStartInHeap
Definition: DirectXDescriptorHeap.h:130
INT iRangeCapacity
Definition: DirectXDescriptorHeap.h:133
std::queue< INT > noLongerUsedDescriptorIndices
Definition: DirectXDescriptorHeap.h:122
INT iNextFreeIndexInRange
Definition: DirectXDescriptorHeap.h:143
Definition: DirectXDescriptorHeap.h:217
std::queue< INT > noLongerUsedSingleDescriptorIndices
Definition: DirectXDescriptorHeap.h:255
std::unordered_set< ContinuousDirectXDescriptorRange * > continuousDescriptorRanges
Definition: DirectXDescriptorHeap.h:227
std::unordered_set< DirectXDescriptor * > bindedSingleDescriptors
Definition: DirectXDescriptorHeap.h:266
INT iNextFreeHeapIndex
Definition: DirectXDescriptorHeap.h:247
INT iHeapCapacity
Definition: DirectXDescriptorHeap.h:230
INT iHeapSize
Definition: DirectXDescriptorHeap.h:237
ComPtr< ID3D12DescriptorHeap > pHeap
Definition: DirectXDescriptorHeap.h:219