Nameless Engine
Loading...
Searching...
No Matches
GpuResourceManager.h
1#pragma once
2
3// Standard.
4#include <variant>
5#include <memory>
6#include <filesystem>
7#include <optional>
8#include <atomic>
9
10// Custom.
11#include "misc/Error.h"
12#include "render/general/resource/UploadBuffer.h"
13#include "material/TextureManager.h"
14#include "render/general/resource/shadow/ShadowMapManager.h"
15
16namespace ne {
17 class Renderer;
18 class GpuResource;
19 class DynamicCpuWriteShaderResourceArrayManager;
20
22 enum class ResourceUsageType {
23 VERTEX_BUFFER,
24 INDEX_BUFFER,
25 ARRAY_BUFFER,
26 OTHER
27 };
28
30 enum class ShaderReadWriteTextureResourceFormat {
31 R32G32_UINT,
32 // ONLY THE FOLLOWING FORMATS CAN BE ADDED HERE:
33 // 1. Formats that are supported as Vulkan storage images on most of the GPUs. Please, make sure you
34 // don't add new formats without checking Vulkan Hardware Database (take Intel(R) UHD
35 // Graphics 600 on Linux (!) for example, if it supports the format as storage image then it's
36 // OK to add it here).
37 // 2. Formats that have the same type in both Vulkan and DirectX.
38
39 SIZE, //< marks the size of this enum
40 };
41
44 // Only renderer should be allowed to create resource manager.
45 friend class Renderer;
46
47 // Creates shadow maps.
48 friend class ShadowMapManager;
49
50 // Modifies counter of alive GPU resources.
51 friend class GpuResource;
52
53 public:
54 GpuResourceManager() = delete;
55
56 virtual ~GpuResourceManager() = default;
57
63 virtual size_t getTotalVideoMemoryInMb() const = 0;
64
70 virtual size_t getUsedVideoMemoryInMb() const = 0;
71
80 virtual std::variant<std::unique_ptr<GpuResource>, Error> loadTextureFromDisk(
81 const std::string& sResourceName, const std::filesystem::path& pathToTextureFile) = 0;
82
111 virtual std::variant<std::unique_ptr<UploadBuffer>, Error> createResourceWithCpuWriteAccess(
112 const std::string& sResourceName,
113 size_t iElementSizeInBytes,
114 size_t iElementCount,
115 std::optional<bool> isUsedInShadersAsArrayResource) = 0;
116
142 virtual std::variant<std::unique_ptr<GpuResource>, Error> createResourceWithData(
143 const std::string& sResourceName,
144 const void* pBufferData,
145 size_t iElementSizeInBytes,
146 size_t iElementCount,
147 ResourceUsageType usage,
148 bool bIsShaderReadWriteResource) = 0;
149
165 virtual std::variant<std::unique_ptr<GpuResource>, Error> createResource(
166 const std::string& sResourceName,
167 size_t iElementSizeInBytes,
168 size_t iElementCount,
169 ResourceUsageType usage,
170 bool bIsShaderReadWriteResource) = 0;
171
182 virtual std::variant<std::unique_ptr<GpuResource>, Error> createShaderReadWriteTextureResource(
183 const std::string& sResourceName,
184 unsigned int iWidth,
185 unsigned int iHeight,
186 ShaderReadWriteTextureResourceFormat format) = 0;
187
193 virtual std::string getCurrentStateInfo() = 0;
194
202 Renderer* getRenderer() const;
203
212
221
230
237
238 protected:
246 static std::variant<std::unique_ptr<GpuResourceManager>, Error> create(Renderer* pRenderer);
247
257 static std::variant<std::unique_ptr<GpuResourceManager>, Error>
259
271 virtual std::variant<std::unique_ptr<GpuResource>, Error> createShadowMapTexture(
272 const std::string& sResourceName, unsigned int iTextureSize, bool bPointLightColorCubemap) = 0;
273
282
284 void resetManagers();
285
286 private:
288 std::unique_ptr<TextureManager> pTextureManager;
289
291 std::unique_ptr<ShadowMapManager> pShadowMapManager;
292
294 std::unique_ptr<DynamicCpuWriteShaderResourceArrayManager> pDynamicCpuWriteShaderResourceArrayManager;
295
297 std::atomic<size_t> iAliveResourceCount{0};
298
300 Renderer* const pRenderer = nullptr;
301 };
302} // namespace ne
Definition: DynamicCpuWriteShaderResourceArrayManager.h:27
Definition: Error.h:27
Definition: GpuResourceManager.h:43
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)=0
static std::variant< std::unique_ptr< GpuResourceManager >, Error > createRendererSpecificManager(Renderer *pRenderer)
Definition: GpuResourceManager.cpp:27
Renderer *const pRenderer
Definition: GpuResourceManager.h:300
virtual size_t getUsedVideoMemoryInMb() const =0
std::unique_ptr< TextureManager > pTextureManager
Definition: GpuResourceManager.h:288
virtual std::variant< std::unique_ptr< UploadBuffer >, Error > createResourceWithCpuWriteAccess(const std::string &sResourceName, size_t iElementSizeInBytes, size_t iElementCount, std::optional< bool > isUsedInShadersAsArrayResource)=0
Renderer * getRenderer() const
Definition: GpuResourceManager.cpp:13
ShadowMapManager * getShadowMapManager() const
Definition: GpuResourceManager.cpp:17
TextureManager * getTextureManager() const
Definition: GpuResourceManager.cpp:15
virtual size_t getTotalVideoMemoryInMb() const =0
std::atomic< size_t > iAliveResourceCount
Definition: GpuResourceManager.h:297
std::unique_ptr< DynamicCpuWriteShaderResourceArrayManager > pDynamicCpuWriteShaderResourceArrayManager
Definition: GpuResourceManager.h:294
static std::variant< std::unique_ptr< GpuResourceManager >, Error > create(Renderer *pRenderer)
Definition: GpuResourceManager.cpp:57
DynamicCpuWriteShaderResourceArrayManager * getDynamicCpuWriteShaderResourceArrayManager() const
Definition: GpuResourceManager.cpp:20
virtual std::variant< std::unique_ptr< GpuResource >, Error > createShadowMapTexture(const std::string &sResourceName, unsigned int iTextureSize, bool bPointLightColorCubemap)=0
virtual std::variant< std::unique_ptr< GpuResource >, Error > loadTextureFromDisk(const std::string &sResourceName, const std::filesystem::path &pathToTextureFile)=0
std::unique_ptr< ShadowMapManager > pShadowMapManager
Definition: GpuResourceManager.h:291
size_t getTotalAliveResourceCount()
Definition: GpuResourceManager.cpp:24
virtual std::variant< std::unique_ptr< GpuResource >, Error > createShaderReadWriteTextureResource(const std::string &sResourceName, unsigned int iWidth, unsigned int iHeight, ShaderReadWriteTextureResourceFormat format)=0
virtual std::string getCurrentStateInfo()=0
virtual std::variant< std::unique_ptr< GpuResource >, Error > createResource(const std::string &sResourceName, size_t iElementSizeInBytes, size_t iElementCount, ResourceUsageType usage, bool bIsShaderReadWriteResource)=0
void resetManagers()
Definition: GpuResourceManager.cpp:95
Definition: GpuResource.h:16
Definition: Renderer.h:43
Definition: ShadowMapManager.h:29
Definition: TextureManager.h:20