Nameless Engine
Loading...
Searching...
No Matches
DirectXResourceManager.h
1#pragma once
2
3// Standard.
4#include <memory>
5#include <variant>
6
7// Custom.
8#include "misc/Error.h"
9#include "render/directx/descriptors/DirectXDescriptorHeap.h"
10#include "render/general/resource/GpuResourceManager.h"
11
12// External.
13#include "directx/d3dx12.h"
14#include "D3D12MemoryAllocator/include/D3D12MemAlloc.h"
15
16// OS.
17#include <wrl.h>
18
19namespace ne {
20 using namespace Microsoft::WRL;
21
22 class DirectXResource;
23 class DirectXRenderer;
24 class UploadBuffer;
25
28 public:
29 DirectXResourceManager() = delete;
31 DirectXResourceManager& operator=(const DirectXResourceManager&) = delete;
32
33 virtual ~DirectXResourceManager() override;
34
42 static DXGI_FORMAT
43 convertTextureResourceFormatToDxFormat(ShaderReadWriteTextureResourceFormat format);
44
52 static std::variant<std::unique_ptr<DirectXResourceManager>, Error>
54
63 virtual std::variant<std::unique_ptr<GpuResource>, Error> loadTextureFromDisk(
64 const std::string& sResourceName, const std::filesystem::path& pathToTextureFile) override;
65
97 virtual std::variant<std::unique_ptr<UploadBuffer>, Error> createResourceWithCpuWriteAccess(
98 const std::string& sResourceName,
99 size_t iElementSizeInBytes,
100 size_t iElementCount,
101 std::optional<bool> isUsedInShadersAsArrayResource) override;
102
128 virtual std::variant<std::unique_ptr<GpuResource>, Error> createResourceWithData(
129 const std::string& sResourceName,
130 const void* pBufferData,
131 size_t iElementSizeInBytes,
132 size_t iElementCount,
133 ResourceUsageType usage,
134 bool bIsShaderReadWriteResource) override;
135
151 virtual std::variant<std::unique_ptr<GpuResource>, Error> createResource(
152 const std::string& sResourceName,
153 size_t iElementSizeInBytes,
154 size_t iElementCount,
155 ResourceUsageType usage,
156 bool bIsShaderReadWriteResource) override;
157
168 virtual std::variant<std::unique_ptr<GpuResource>, Error> createShaderReadWriteTextureResource(
169 const std::string& sResourceName,
170 unsigned int iWidth,
171 unsigned int iHeight,
172 ShaderReadWriteTextureResourceFormat format) override;
173
179 virtual size_t getTotalVideoMemoryInMb() const override;
180
186 virtual size_t getUsedVideoMemoryInMb() const override;
187
199 std::variant<std::unique_ptr<DirectXResource>, Error> createResource(
200 const std::string& sResourceName,
201 const D3D12MA::ALLOCATION_DESC& allocationDesc,
202 const D3D12_RESOURCE_DESC& resourceDesc,
203 const D3D12_RESOURCE_STATES& initialResourceState,
204 const std::optional<D3D12_CLEAR_VALUE>& resourceClearValue);
205
215 std::variant<std::vector<std::unique_ptr<DirectXResource>>, Error>
216 makeRtvResourcesFromSwapChainBuffer(IDXGISwapChain3* pSwapChain, unsigned int iSwapChainBufferCount);
217
224
231
238
244 virtual std::string getCurrentStateInfo() override;
245
246 private:
258 ComPtr<D3D12MA::Allocator>&& pMemoryAllocator,
259 std::unique_ptr<DirectXDescriptorHeap>&& pRtvHeap,
260 std::unique_ptr<DirectXDescriptorHeap>&& pDsvHeap,
261 std::unique_ptr<DirectXDescriptorHeap>&& pCbvSrvUavHeap);
262
276 static inline size_t makeMultipleOf256(size_t iNumber) {
277 // We are adding 255 and then masking off
278 // the lower 2 bytes which store all bits < 256.
279
280 // Example: Suppose iNumber = 300.
281 // (300 + 255) & ~255
282 // 555 & ~255
283 // 0x022B & ~0x00ff
284 // 0x022B & 0xff00
285 // 0x0200
286 // 512
287 return (iNumber + 255) & ~255; // NOLINT
288 }
289
301 virtual std::variant<std::unique_ptr<GpuResource>, Error> createShadowMapTexture(
302 const std::string& sResourceName,
303 unsigned int iTextureSize,
304 bool bPointLightColorCubemap) override;
305
320 std::variant<std::unique_ptr<GpuResource>, Error> createResourceWithData(
321 const std::string& sResourceName,
322 const D3D12_RESOURCE_DESC& finalResourceDescription,
323 const std::vector<D3D12_SUBRESOURCE_DATA>& vSubresourcesToCopy,
324 const D3D12_RESOURCE_DESC& uploadResourceDescription,
325 bool bIsTextureResource,
326 size_t iElementSizeInBytes = 0,
327 size_t iElementCount = 0);
328
330 ComPtr<D3D12MA::Allocator> pMemoryAllocator;
331
333 std::unique_ptr<DirectXDescriptorHeap> pRtvHeap;
334
336 std::unique_ptr<DirectXDescriptorHeap> pDsvHeap;
337
339 std::unique_ptr<DirectXDescriptorHeap> pCbvSrvUavHeap;
340 };
341} // namespace ne
Definition: DirectXDescriptorHeap.h:205
Definition: DirectXRenderer.h:36
Definition: DirectXResourceManager.h:27
static std::variant< std::unique_ptr< DirectXResourceManager >, Error > create(DirectXRenderer *pRenderer)
Definition: DirectXResourceManager.cpp:16
virtual std::variant< std::unique_ptr< UploadBuffer >, Error > createResourceWithCpuWriteAccess(const std::string &sResourceName, size_t iElementSizeInBytes, size_t iElementCount, std::optional< bool > isUsedInShadersAsArrayResource) override
Definition: DirectXResourceManager.cpp:182
ComPtr< D3D12MA::Allocator > pMemoryAllocator
Definition: DirectXResourceManager.h:330
virtual std::variant< std::unique_ptr< GpuResource >, Error > createShaderReadWriteTextureResource(const std::string &sResourceName, unsigned int iWidth, unsigned int iHeight, ShaderReadWriteTextureResourceFormat format) override
Definition: DirectXResourceManager.cpp:296
DirectXDescriptorHeap * getRtvHeap() const
Definition: DirectXResourceManager.cpp:466
static DXGI_FORMAT convertTextureResourceFormatToDxFormat(ShaderReadWriteTextureResourceFormat format)
Definition: DirectXResourceManager.cpp:531
DirectXDescriptorHeap * getDsvHeap() const
Definition: DirectXResourceManager.cpp:468
DirectXDescriptorHeap * getCbvSrvUavHeap() const
Definition: DirectXResourceManager.cpp:470
std::unique_ptr< DirectXDescriptorHeap > pRtvHeap
Definition: DirectXResourceManager.h:333
std::unique_ptr< DirectXDescriptorHeap > pCbvSrvUavHeap
Definition: DirectXResourceManager.h:339
std::unique_ptr< DirectXDescriptorHeap > pDsvHeap
Definition: DirectXResourceManager.h:336
virtual std::variant< std::unique_ptr< GpuResource >, Error > loadTextureFromDisk(const std::string &sResourceName, const std::filesystem::path &pathToTextureFile) override
Definition: DirectXResourceManager.cpp:66
virtual size_t getUsedVideoMemoryInMb() const override
Definition: DirectXResourceManager.cpp:412
virtual std::string getCurrentStateInfo() override
Definition: DirectXResourceManager.cpp:472
virtual std::variant< std::unique_ptr< GpuResource >, Error > createResource(const std::string &sResourceName, size_t iElementSizeInBytes, size_t iElementCount, ResourceUsageType usage, bool bIsShaderReadWriteResource) override
Definition: DirectXResourceManager.cpp:257
virtual std::variant< std::unique_ptr< GpuResource >, Error > createResourceWithData(const std::string &sResourceName, const void *pBufferData, size_t iElementSizeInBytes, size_t iElementCount, ResourceUsageType usage, bool bIsShaderReadWriteResource) override
Definition: DirectXResourceManager.cpp:220
virtual std::variant< std::unique_ptr< GpuResource >, Error > createShadowMapTexture(const std::string &sResourceName, unsigned int iTextureSize, bool bPointLightColorCubemap) override
Definition: DirectXResourceManager.cpp:335
virtual size_t getTotalVideoMemoryInMb() const override
Definition: DirectXResourceManager.cpp:405
static size_t makeMultipleOf256(size_t iNumber)
Definition: DirectXResourceManager.h:276
std::variant< std::vector< std::unique_ptr< DirectXResource > >, Error > makeRtvResourcesFromSwapChainBuffer(IDXGISwapChain3 *pSwapChain, unsigned int iSwapChainBufferCount)
Definition: DirectXResourceManager.cpp:443
Definition: Error.h:27
Definition: GpuResourceManager.h:43
Renderer *const pRenderer
Definition: GpuResourceManager.h:300