SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
ResourceManager.hpp
Go to the documentation of this file.
1#ifndef _RESOURCEMANAGER_HPP_
2#define _RESOURCEMANAGER_HPP_
3
4#include <functional>
5#include <mutex>
6#include <any>
7#include <memory>
8#include <array>
9
10#include "BufferBase.hpp"
11#include "Shader.hpp"
12#include <Runtime/Texture.hpp>
13
14#define IMPLEMENT_RESOURCE_MANAGER \
15 std::function<std::any(BufferType, uint32_t, void*)> ResourceManager::BufferCreationFunc; \
16 std::function<std::any(const std::string&)> ResourceManager::ShaderCreateFunc;\
17 std::function<std::any(const std::string&)> ResourceManager::TextureCreateFunc; \
18 std::function<std::any(const void*, uint32_t, uint32_t, TextureFormat, uint32_t)> ResourceManager::TextureFromMemoryCreateFunc; \
19 std::function<std::any(const std::array<std::string, 6>&)> ResourceManager::CubemapTextureCreateFunc; \
20 std::function<std::any(const std::string&)> ResourceManager::CubemapPanoramaCreateFunc; \
21 std::mutex ResourceManager::threadManager;
22
23namespace Sleak {
24 namespace RenderEngine {
25
26 /// Coarse kind tag for a graphics resource.
27 enum class ResourceType : uint8_t {
28 Buffer = 0,
29 Shader = 1,
31 };
32
33 /// Backend-agnostic factory: routes buffer/shader/texture creation to whichever
34 /// backend registered its creation functions at startup.
36 public:
37 // Register a function for creating buffers
38 template <typename T>
39 static void RegisterCreateBuffer(T* instance, BufferBase* (T::*method)(BufferType, uint32_t, void*)) {
40 std::lock_guard<std::mutex> lock(threadManager);
41 BufferCreationFunc = [instance, method](BufferType type, uint32_t size, void* data) -> std::any {
42 return (instance->*method)(type, size, data);
43 };
44 }
45
46 // Register a function for creating shaders
47 template <typename T>
48 static void RegisterCreateShader(T* instance, Shader* (T::*method)(const std::string&)) {
49 std::lock_guard<std::mutex> lock(threadManager);
50 ShaderCreateFunc = [instance, method](const std::string& path) -> std::any {
51 return (instance->*method)(path);
52 };
53 }
54
55 // Register a function for creating textures
56 template <typename T>
57 static void RegisterCreateTexture(T* instance, Texture* (T::*method)(const std::string&)) {
58 std::lock_guard<std::mutex> lock(threadManager);
59 TextureCreateFunc = [instance, method](const std::string& path) -> std::any {
60 return (instance->*method)(path);
61 };
62 }
63
64 // Register a function for creating textures from memory
65 static void RegisterCreateTextureFromMemory(std::function<Texture*(const void*, uint32_t, uint32_t, TextureFormat, uint32_t)> func) {
66 std::lock_guard<std::mutex> lock(threadManager);
67 TextureFromMemoryCreateFunc = [func](const void* data, uint32_t w, uint32_t h, TextureFormat fmt, uint32_t maxMip) -> std::any {
68 return func(data, w, h, fmt, maxMip);
69 };
70 }
71
72 // Register a function for creating cubemap textures
73 template <typename T>
74 static void RegisterCreateCubemapTexture(T* instance, Texture* (T::*method)(const std::array<std::string, 6>&)) {
75 std::lock_guard<std::mutex> lock(threadManager);
76 CubemapTextureCreateFunc = [instance, method](const std::array<std::string, 6>& paths) -> std::any {
77 return (instance->*method)(paths);
78 };
79 }
80
81 // Register a function for creating cubemap textures from a single panorama
82 template <typename T>
83 static void RegisterCreateCubemapTextureFromPanorama(T* instance, Texture* (T::*method)(const std::string&)) {
84 std::lock_guard<std::mutex> lock(threadManager);
85 CubemapPanoramaCreateFunc = [instance, method](const std::string& path) -> std::any {
86 return (instance->*method)(path);
87 };
88 }
89
90 /// Creates a buffer via the currently registered backend factory.
91 static BufferBase* CreateBuffer(BufferType Type, uint32_t Size, void* Data);
92 /// Compiles a shader via the currently registered backend factory.
93 static Shader* CreateShader(const std::string& ShaderPath);
94 /// Loads a texture via the currently registered backend factory.
95 static Sleak::Texture* CreateTexture(const std::string& TexturePath);
96 /// Creates a texture from raw pixel data via the currently registered backend factory.
97 static Sleak::Texture* CreateTextureFromMemory(const void* data, uint32_t width, uint32_t height, TextureFormat format, uint32_t maxMipLevels = 0);
98 /// Loads a cubemap from six face image paths via the currently registered backend factory.
99 static Sleak::Texture* CreateCubemapTexture(const std::array<std::string, 6>& FacePaths);
100 /// Loads a cubemap from a single equirectangular panorama via the currently registered backend factory.
101 static Sleak::Texture* CreateCubemapTextureFromPanorama(const std::string& PanoramaPath);
102
103
104 private:
105 static std::function<std::any(BufferType, uint32_t, void*)> BufferCreationFunc;
106 static std::function<std::any(const std::string&)> ShaderCreateFunc;
107 static std::function<std::any(const std::string&)> TextureCreateFunc;
108 static std::function<std::any(const void*, uint32_t, uint32_t, TextureFormat, uint32_t)> TextureFromMemoryCreateFunc;
109 static std::function<std::any(const std::array<std::string, 6>&)> CubemapTextureCreateFunc;
110 static std::function<std::any(const std::string&)> CubemapPanoramaCreateFunc;
111
112 // Mutex for thread safety
113 static std::mutex threadManager;
114 };
115 };
116};
117
118#endif
int width
int height
Backend-agnostic GPU buffer: vertex, index, constant, or resource view target.
static void RegisterCreateCubemapTextureFromPanorama(T *instance, Texture *(T::*method)(const std::string &))
static Sleak::Texture * CreateCubemapTextureFromPanorama(const std::string &PanoramaPath)
Loads a cubemap from a single equirectangular panorama via the currently registered backend factory.
static void RegisterCreateCubemapTexture(T *instance, Texture *(T::*method)(const std::array< std::string, 6 > &))
static void RegisterCreateBuffer(T *instance, BufferBase *(T::*method)(BufferType, uint32_t, void *))
static Shader * CreateShader(const std::string &ShaderPath)
Compiles a shader via the currently registered backend factory.
static Sleak::Texture * CreateTexture(const std::string &TexturePath)
Loads a texture via the currently registered backend factory.
static void RegisterCreateShader(T *instance, Shader *(T::*method)(const std::string &))
static BufferBase * CreateBuffer(BufferType Type, uint32_t Size, void *Data)
Creates a buffer via the currently registered backend factory.
static void RegisterCreateTextureFromMemory(std::function< Texture *(const void *, uint32_t, uint32_t, TextureFormat, uint32_t)> func)
static Sleak::Texture * CreateTextureFromMemory(const void *data, uint32_t width, uint32_t height, TextureFormat format, uint32_t maxMipLevels=0)
Creates a texture from raw pixel data via the currently registered backend factory.
static void RegisterCreateTexture(T *instance, Texture *(T::*method)(const std::string &))
static Sleak::Texture * CreateCubemapTexture(const std::array< std::string, 6 > &FacePaths)
Loads a cubemap from six face image paths via the currently registered backend factory.
Backend-agnostic compiled shader program.
Definition Shader.hpp:11
TextureFormat
Definition Texture.hpp:10
BufferType
GPU buffer usage kind, drives backend binding flags and layout.
ResourceType
Coarse kind tag for a graphics resource.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10