Nameless Engine
Loading...
Searching...
No Matches
DirectXResource.h
1#pragma once
2
3// Standard.
4#include <variant>
5#include <memory>
6#include <array>
7#include <optional>
8#include <mutex>
9
10// Custom.
11#include "misc/Error.h"
12#include "render/general/resource/GpuResource.h"
13#include "render/directx/descriptors/DirectXDescriptor.h"
14#include "render/directx/descriptors/DirectXDescriptorType.hpp"
15
16// External.
17#include "directx/d3dx12.h"
18#include "D3D12MemoryAllocator/include/D3D12MemAlloc.h"
19
20// OS.
21#include <wrl.h>
22
23namespace ne {
24 using namespace Microsoft::WRL;
25
26 class DirectXRenderer;
27 class DirectXDescriptorHeap;
28 class DirectXResourceManager;
29 class ContinuousDirectXDescriptorRange;
30
33 // If descriptor heap of a used descriptor (see field of this class) was recreated
34 // then descriptor heap will update our descriptors with new descriptor information.
35 friend class DirectXDescriptorHeap;
36
37 // Only resource manager can create this resource
38 // (simply because only manager has a memory allocator object).
39 friend class DirectXResourceManager;
40
41 public:
42 DirectXResource() = delete;
43 DirectXResource(const DirectXResource&) = delete;
44 DirectXResource& operator=(const DirectXResource&) = delete;
45
46 virtual ~DirectXResource() override;
47
61 [[nodiscard]] std::optional<Error> bindDescriptor(
62 DirectXDescriptorType descriptorType,
63 ContinuousDirectXDescriptorRange* pRange = nullptr,
64 bool bBindDescriptorsToCubemapFaces = true);
65
74 std::optional<D3D12_CPU_DESCRIPTOR_HANDLE>
75 getBindedDescriptorCpuHandle(DirectXDescriptorType descriptorType);
76
87 std::optional<D3D12_CPU_DESCRIPTOR_HANDLE> getBindedCubemapFaceDescriptorCpuHandle(
88 DirectXDescriptorType descriptorType, size_t iCubemapFaceIndex);
89
98 std::optional<D3D12_GPU_DESCRIPTOR_HANDLE>
99 getBindedDescriptorGpuHandle(DirectXDescriptorType descriptorType);
100
110 inline ID3D12Resource* getInternalResource() const { return pInternalResource; }
111
122 DirectXDescriptor* getDescriptor(DirectXDescriptorType descriptorType);
123
124 private:
131 std::unique_ptr<DirectXDescriptor> pResource;
132
137 std::array<std::unique_ptr<DirectXDescriptor>, 6> // NOLINT: 1 per cubemap face (if cubemap)
139 };
140
152 DirectXResourceManager* pResourceManager,
153 const std::string& sResourceName,
155 UINT iElementCount);
156
175 static std::variant<std::unique_ptr<DirectXResource>, Error> create(
176 DirectXResourceManager* pResourceManager,
177 const std::string& sResourceName,
178 D3D12MA::Allocator* pMemoryAllocator,
179 const D3D12MA::ALLOCATION_DESC& allocationDesc,
180 const D3D12_RESOURCE_DESC& resourceDesc,
181 const D3D12_RESOURCE_STATES& initialResourceState,
182 std::optional<D3D12_CLEAR_VALUE> resourceClearValue,
183 size_t iElementSizeInBytes = 0,
184 size_t iElementCount = 0);
185
196 static std::variant<std::unique_ptr<DirectXResource>, Error> createResourceFromSwapChainBuffer(
197 DirectXResourceManager* pResourceManager,
198 DirectXDescriptorHeap* pRtvHeap,
199 const ComPtr<ID3D12Resource>& pSwapChainBuffer);
200
211 std::pair<
212 std::recursive_mutex,
213 std::array<DescriptorsSameType, static_cast<size_t>(DirectXDescriptorType::END)>>
215
217 ComPtr<D3D12MA::Allocation> pAllocatedResource;
218
223 ComPtr<ID3D12Resource> pSwapChainBuffer;
224
229 ID3D12Resource* pInternalResource = nullptr;
230 };
231} // namespace ne
Definition: DirectXDescriptorHeap.h:44
Definition: DirectXDescriptorHeap.h:211
Definition: DirectXDescriptor.h:18
Definition: DirectXResourceManager.h:27
Definition: DirectXResource.h:32
ComPtr< D3D12MA::Allocation > pAllocatedResource
Definition: DirectXResource.h:217
std::optional< D3D12_GPU_DESCRIPTOR_HANDLE > getBindedDescriptorGpuHandle(DirectXDescriptorType descriptorType)
Definition: DirectXResource.cpp:158
ComPtr< ID3D12Resource > pSwapChainBuffer
Definition: DirectXResource.h:223
std::optional< D3D12_CPU_DESCRIPTOR_HANDLE > getBindedDescriptorCpuHandle(DirectXDescriptorType descriptorType)
Definition: DirectXResource.cpp:99
std::optional< D3D12_CPU_DESCRIPTOR_HANDLE > getBindedCubemapFaceDescriptorCpuHandle(DirectXDescriptorType descriptorType, size_t iCubemapFaceIndex)
Definition: DirectXResource.cpp:123
std::optional< Error > bindDescriptor(DirectXDescriptorType descriptorType, ContinuousDirectXDescriptorRange *pRange=nullptr, bool bBindDescriptorsToCubemapFaces=true)
Definition: DirectXResource.cpp:213
DirectXDescriptor * getDescriptor(DirectXDescriptorType descriptorType)
Definition: DirectXResource.cpp:182
static std::variant< std::unique_ptr< DirectXResource >, Error > create(DirectXResourceManager *pResourceManager, const std::string &sResourceName, D3D12MA::Allocator *pMemoryAllocator, const D3D12MA::ALLOCATION_DESC &allocationDesc, const D3D12_RESOURCE_DESC &resourceDesc, const D3D12_RESOURCE_STATES &initialResourceState, std::optional< D3D12_CLEAR_VALUE > resourceClearValue, size_t iElementSizeInBytes=0, size_t iElementCount=0)
Definition: DirectXResource.cpp:11
ID3D12Resource * pInternalResource
Definition: DirectXResource.h:229
static std::variant< std::unique_ptr< DirectXResource >, Error > createResourceFromSwapChainBuffer(DirectXResourceManager *pResourceManager, DirectXDescriptorHeap *pRtvHeap, const ComPtr< ID3D12Resource > &pSwapChainBuffer)
Definition: DirectXResource.cpp:76
std::pair< std::recursive_mutex, std::array< DescriptorsSameType, static_cast< size_t >(DirectXDescriptorType::END)> > mtxHeapDescriptors
Definition: DirectXResource.h:214
ID3D12Resource * getInternalResource() const
Definition: DirectXResource.h:110
Definition: Error.h:27
Definition: GpuResource.h:16
const std::string sResourceName
Definition: GpuResource.h:97
const unsigned int iElementCount
Definition: GpuResource.h:94
const unsigned int iElementSizeInBytes
Definition: GpuResource.h:91
Definition: DirectXResource.h:129
std::unique_ptr< DirectXDescriptor > pResource
Definition: DirectXResource.h:131
std::array< std::unique_ptr< DirectXDescriptor >, 6 > vCubemapFaces
Definition: DirectXResource.h:138