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 SAMPLER,
37 };
38
46 // This class is fully managed by the heap.
47 friend class DirectXDescriptorHeap;
48
49 public:
51
52 /* Notifies the heap. */
54
57
58 // Intentionally disable `move` because heap will store a raw pointer to the range
59 // and we don't want to accidentally `move` the range which will make the heap's raw pointer invalid.
62 operator=(ContinuousDirectXDescriptorRange&& other) noexcept = delete;
63
69 static constexpr INT getRangeGrowSize() { return iRangeGrowSize; }
70
76 size_t getRangeSize();
77
83 size_t getRangeCapacity();
84
91
100 D3D12_GPU_DESCRIPTOR_HANDLE getGpuDescriptorHandleToRangeStart() const;
101
107 std::string getRangeName() const { return sRangeName; }
108
109 private:
120 std::unordered_set<DirectXDescriptor*> allocatedDescriptors;
121
124
132
135
145 };
146
158 const std::function<void()>& onRangeIndicesChanged,
159 const std::string& sRangeName);
160
171 [[nodiscard]] std::optional<Error> markDescriptorAsUnused(DirectXDescriptor* pDescriptor);
172
183 std::variant<std::optional<INT>, Error> tryReserveFreeHeapIndexToCreateDescriptor();
184
186 std::pair<std::recursive_mutex, InternalData> mtxInternalData;
187
189 const std::function<void()> onRangeIndicesChanged;
190
192 const std::string sRangeName;
193
195 DirectXDescriptorHeap* const pHeap = nullptr;
196
202 static constexpr INT iRangeGrowSize = 50; // NOLINT: see static asserts
203 };
204
207 // Notifies the heap about descriptor being destroyed.
208 friend class DirectXDescriptor;
209
210 // Notifies the heap about range being destroyed.
212
213 // Only resource can request descriptors.
214 friend class DirectXResource;
215
216 public:
220 ComPtr<ID3D12DescriptorHeap> pHeap;
221
228 std::unordered_set<ContinuousDirectXDescriptorRange*> continuousDescriptorRanges;
229
232
238 INT iHeapSize = 0;
239
249
257
267 std::unordered_set<DirectXDescriptor*> bindedSingleDescriptors;
268 };
269
270 DirectXDescriptorHeap() = delete;
272 DirectXDescriptorHeap& operator=(const DirectXDescriptorHeap&) = delete;
273
276
282 static constexpr INT getHeapGrowSize() { return iHeapGrowSize; }
283
292 static std::variant<std::unique_ptr<DirectXDescriptorHeap>, Error>
293 create(DirectXRenderer* pRenderer, DescriptorHeapType heapType);
294
305 std::variant<std::shared_ptr<ContinuousDirectXDescriptorRange>, Error>
307 const std::string& sRangeName, const std::function<void()>& onRangeIndicesChanged);
308
317 INT getHeapCapacity();
318
327 INT getHeapSize();
328
335
341 UINT getDescriptorSize() const { return iDescriptorSize; }
342
348 ID3D12DescriptorHeap* getInternalHeap() const { return mtxInternalData.second.pHeap.Get(); }
349
357 std::pair<std::recursive_mutex, InternalData>* getInternalData() { return &mtxInternalData; }
358
359 protected:
367 static std::string convertHeapTypeToString(DescriptorHeapType heapType);
368
376
388 DirectXDescriptor* pDescriptor, ContinuousDirectXDescriptorRange* pRange = nullptr);
389
398
409 void createView(
410 CD3DX12_CPU_DESCRIPTOR_HANDLE heapHandle,
411 const DirectXResource* pResource,
412 DirectXDescriptorType descriptorType,
413 std::optional<size_t> cubemapFaceIndex) const;
414
425 [[nodiscard]] std::optional<Error> expandHeap(ContinuousDirectXDescriptorRange* pChangedRange);
426
440 [[nodiscard]] std::variant<bool, Error>
442
455 [[nodiscard]] std::optional<Error>
456 createHeap(INT iCapacity, ContinuousDirectXDescriptorRange* pChangedRange);
457
465 std::vector<DirectXDescriptorType> getDescriptorTypesHandledByThisHeap() const;
466
475 [[nodiscard]] std::optional<Error> rebindViewsUpdateIndices();
476
477 private:
497 [[nodiscard]] std::optional<Error> assignDescriptor(
498 DirectXResource* pResource,
499 DirectXDescriptorType descriptorType,
500 const std::shared_ptr<ContinuousDirectXDescriptorRange>& pRange = nullptr,
501 bool bBindDescriptorsToCubemapFaces = true);
502
512 static bool isShrinkingPossible(INT iSize, INT iCapacity, INT iGrowSize);
513
521 [[nodiscard]] std::optional<Error> expandRange(ContinuousDirectXDescriptorRange* pRange);
522
525
527 std::pair<std::recursive_mutex, InternalData> mtxInternalData;
528
531
533 DescriptorHeapType heapType;
534
536 std::string sHeapType;
537
539 D3D12_DESCRIPTOR_HEAP_TYPE d3dHeapType;
540
542 static constexpr INT iHeapGrowSize = 300; // NOLINT: don't recreate heap too often
543 };
544} // namespace ne
Definition: DirectXDescriptorHeap.h:45
size_t getRangeSize()
Definition: DirectXDescriptorHeap.cpp:1107
const std::string sRangeName
Definition: DirectXDescriptorHeap.h:192
std::variant< std::optional< INT >, Error > tryReserveFreeHeapIndexToCreateDescriptor()
Definition: DirectXDescriptorHeap.cpp:1169
INT getRangeStartInHeap()
Definition: DirectXDescriptorHeap.cpp:1117
size_t getRangeCapacity()
Definition: DirectXDescriptorHeap.cpp:1112
D3D12_GPU_DESCRIPTOR_HANDLE getGpuDescriptorHandleToRangeStart() const
Definition: DirectXDescriptorHeap.cpp:1122
const std::function< void()> onRangeIndicesChanged
Definition: DirectXDescriptorHeap.h:189
DirectXDescriptorHeap *const pHeap
Definition: DirectXDescriptorHeap.h:195
std::optional< Error > markDescriptorAsUnused(DirectXDescriptor *pDescriptor)
Definition: DirectXDescriptorHeap.cpp:1147
static constexpr INT iRangeGrowSize
Definition: DirectXDescriptorHeap.h:202
static constexpr INT getRangeGrowSize()
Definition: DirectXDescriptorHeap.h:69
std::pair< std::recursive_mutex, InternalData > mtxInternalData
Definition: DirectXDescriptorHeap.h:186
std::string getRangeName() const
Definition: DirectXDescriptorHeap.h:107
Definition: DirectXDescriptorHeap.h:206
INT getHeapSize()
Definition: DirectXDescriptorHeap.cpp:254
static std::string convertHeapTypeToString(DescriptorHeapType heapType)
Definition: DirectXDescriptorHeap.cpp:264
void onDescriptorRangeBeingDestroyed(ContinuousDirectXDescriptorRange *pRange)
Definition: DirectXDescriptorHeap.cpp:476
static std::variant< std::unique_ptr< DirectXDescriptorHeap >, Error > create(DirectXRenderer *pRenderer, DescriptorHeapType heapType)
Definition: DirectXDescriptorHeap.cpp:11
DirectXRenderer * pRenderer
Definition: DirectXDescriptorHeap.h:524
static constexpr INT getHeapGrowSize()
Definition: DirectXDescriptorHeap.h:282
std::variant< std::shared_ptr< ContinuousDirectXDescriptorRange >, Error > allocateContinuousDescriptorRange(const std::string &sRangeName, const std::function< void()> &onRangeIndicesChanged)
Definition: DirectXDescriptorHeap.cpp:62
void onDescriptorBeingDestroyed(DirectXDescriptor *pDescriptor, ContinuousDirectXDescriptorRange *pRange=nullptr)
Definition: DirectXDescriptorHeap.cpp:369
D3D12_DESCRIPTOR_HEAP_TYPE d3dHeapType
Definition: DirectXDescriptorHeap.h:539
std::string sHeapType
Definition: DirectXDescriptorHeap.h:536
std::optional< Error > expandRange(ContinuousDirectXDescriptorRange *pRange)
Definition: DirectXDescriptorHeap.cpp:591
std::variant< bool, Error > shrinkHeapIfPossible(ContinuousDirectXDescriptorRange *pChangedRange)
Definition: DirectXDescriptorHeap.cpp:623
~DirectXDescriptorHeap()
Definition: DirectXDescriptorHeap.cpp:335
std::vector< DirectXDescriptorType > getDescriptorTypesHandledByThisHeap() const
Definition: DirectXDescriptorHeap.cpp:970
UINT getDescriptorSize() const
Definition: DirectXDescriptorHeap.h:341
std::optional< Error > assignDescriptor(DirectXResource *pResource, DirectXDescriptorType descriptorType, const std::shared_ptr< ContinuousDirectXDescriptorRange > &pRange=nullptr, bool bBindDescriptorsToCubemapFaces=true)
Definition: DirectXDescriptorHeap.cpp:84
static bool isShrinkingPossible(INT iSize, INT iCapacity, INT iGrowSize)
Definition: DirectXDescriptorHeap.cpp:569
std::pair< std::recursive_mutex, InternalData > mtxInternalData
Definition: DirectXDescriptorHeap.h:527
DescriptorHeapType heapType
Definition: DirectXDescriptorHeap.h:533
std::pair< std::recursive_mutex, InternalData > * getInternalData()
Definition: DirectXDescriptorHeap.h:357
std::optional< Error > expandHeap(ContinuousDirectXDescriptorRange *pChangedRange)
Definition: DirectXDescriptorHeap.cpp:521
size_t getNoLongerUsedDescriptorCount()
Definition: DirectXDescriptorHeap.cpp:259
std::optional< Error > rebindViewsUpdateIndices()
Definition: DirectXDescriptorHeap.cpp:987
UINT iDescriptorSize
Definition: DirectXDescriptorHeap.h:530
ID3D12DescriptorHeap * getInternalHeap() const
Definition: DirectXDescriptorHeap.h:348
void createView(CD3DX12_CPU_DESCRIPTOR_HANDLE heapHandle, const DirectXResource *pResource, DirectXDescriptorType descriptorType, std::optional< size_t > cubemapFaceIndex) const
Definition: DirectXDescriptorHeap.cpp:645
INT getHeapCapacity()
Definition: DirectXDescriptorHeap.cpp:249
static constexpr INT iHeapGrowSize
Definition: DirectXDescriptorHeap.h:542
std::optional< Error > createHeap(INT iCapacity, ContinuousDirectXDescriptorRange *pChangedRange)
Definition: DirectXDescriptorHeap.cpp:914
Definition: DirectXDescriptor.h:21
Definition: DirectXRenderer.h:36
Definition: DirectXResource.h:33
Definition: Error.h:27
Definition: DirectXDescriptorHeap.h:111
std::unordered_set< DirectXDescriptor * > allocatedDescriptors
Definition: DirectXDescriptorHeap.h:120
INT iRangeStartInHeap
Definition: DirectXDescriptorHeap.h:131
INT iRangeCapacity
Definition: DirectXDescriptorHeap.h:134
std::queue< INT > noLongerUsedDescriptorIndices
Definition: DirectXDescriptorHeap.h:123
INT iNextFreeIndexInRange
Definition: DirectXDescriptorHeap.h:144
Definition: DirectXDescriptorHeap.h:218
std::queue< INT > noLongerUsedSingleDescriptorIndices
Definition: DirectXDescriptorHeap.h:256
std::unordered_set< ContinuousDirectXDescriptorRange * > continuousDescriptorRanges
Definition: DirectXDescriptorHeap.h:228
std::unordered_set< DirectXDescriptor * > bindedSingleDescriptors
Definition: DirectXDescriptorHeap.h:267
INT iNextFreeHeapIndex
Definition: DirectXDescriptorHeap.h:248
INT iHeapCapacity
Definition: DirectXDescriptorHeap.h:231
INT iHeapSize
Definition: DirectXDescriptorHeap.h:238
ComPtr< ID3D12DescriptorHeap > pHeap
Definition: DirectXDescriptorHeap.h:220