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
63 [[nodiscard]] std::optional<Error> bindDescriptor(
64 DirectXDescriptorType descriptorType,
65 const std::shared_ptr<ContinuousDirectXDescriptorRange>& pRange = nullptr,
66 bool bBindDescriptorsToCubemapFaces = true);
67
76 std::optional<D3D12_CPU_DESCRIPTOR_HANDLE>
77 getBindedDescriptorCpuHandle(DirectXDescriptorType descriptorType);
78
89 std::optional<D3D12_CPU_DESCRIPTOR_HANDLE> getBindedCubemapFaceDescriptorCpuHandle(
90 DirectXDescriptorType descriptorType, size_t iCubemapFaceIndex);
91
100 std::optional<D3D12_GPU_DESCRIPTOR_HANDLE>
101 getBindedDescriptorGpuHandle(DirectXDescriptorType descriptorType);
102
112 ID3D12Resource* getInternalResource() const { return pInternalResource; }
113
124 DirectXDescriptor* getDescriptor(DirectXDescriptorType descriptorType);
125
126 private:
133 std::unique_ptr<DirectXDescriptor> pResource;
134
139 std::array<std::unique_ptr<DirectXDescriptor>, 6> // NOLINT: 1 per cubemap face (if cubemap)
141 };
142
154 DirectXResourceManager* pResourceManager,
155 const std::string& sResourceName,
157 UINT iElementCount);
158
177 static std::variant<std::unique_ptr<DirectXResource>, Error> create(
178 DirectXResourceManager* pResourceManager,
179 const std::string& sResourceName,
180 D3D12MA::Allocator* pMemoryAllocator,
181 const D3D12MA::ALLOCATION_DESC& allocationDesc,
182 const D3D12_RESOURCE_DESC& resourceDesc,
183 const D3D12_RESOURCE_STATES& initialResourceState,
184 std::optional<D3D12_CLEAR_VALUE> resourceClearValue,
185 size_t iElementSizeInBytes = 0,
186 size_t iElementCount = 0);
187
198 static std::variant<std::unique_ptr<DirectXResource>, Error> createResourceFromSwapChainBuffer(
199 DirectXResourceManager* pResourceManager,
200 DirectXDescriptorHeap* pRtvHeap,
201 const ComPtr<ID3D12Resource>& pSwapChainBuffer);
202
213 std::pair<
214 std::recursive_mutex,
215 std::array<DescriptorsSameType, static_cast<size_t>(DirectXDescriptorType::END)>>
217
219 ComPtr<D3D12MA::Allocation> pAllocatedResource;
220
225 ComPtr<ID3D12Resource> pSwapChainBuffer;
226
231 ID3D12Resource* pInternalResource = nullptr;
232 };
233} // namespace ne
Definition: DirectXDescriptorHeap.h:205
Definition: DirectXDescriptor.h:21
Definition: DirectXResourceManager.h:27
Definition: DirectXResource.h:32
ComPtr< D3D12MA::Allocation > pAllocatedResource
Definition: DirectXResource.h:219
std::optional< D3D12_GPU_DESCRIPTOR_HANDLE > getBindedDescriptorGpuHandle(DirectXDescriptorType descriptorType)
Definition: DirectXResource.cpp:158
ComPtr< ID3D12Resource > pSwapChainBuffer
Definition: DirectXResource.h:225
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
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:231
std::optional< Error > bindDescriptor(DirectXDescriptorType descriptorType, const std::shared_ptr< ContinuousDirectXDescriptorRange > &pRange=nullptr, bool bBindDescriptorsToCubemapFaces=true)
Definition: DirectXResource.cpp:213
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:216
ID3D12Resource * getInternalResource() const
Definition: DirectXResource.h:112
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:131
std::unique_ptr< DirectXDescriptor > pResource
Definition: DirectXResource.h:133
std::array< std::unique_ptr< DirectXDescriptor >, 6 > vCubemapFaces
Definition: DirectXResource.h:140