Nameless Engine
Loading...
Searching...
No Matches
ShaderManager.h
1#pragma once
2
3// Standard.
4#include <string>
5#include <unordered_map>
6#include <memory>
7#include <functional>
8#include <mutex>
9#include <optional>
10#include <atomic>
11#include <variant>
12#include <array>
13
14// Custom.
15#include "misc/Error.h"
16#include "shader/ShaderDescription.h"
17#include "shader/general/cache/ShaderCacheManager.h"
18#include "shader/general/ShaderPack.h"
19
20namespace ne {
21 class Shader;
22 class Renderer;
23
26 // Only ShaderUser should be able to work with shaders.
27 friend class ShaderUser;
28
29 // Renderer sets shaders' configuration.
30 friend class Renderer;
31
32 public:
39
40 ShaderManager() = delete;
41 ShaderManager(const ShaderManager&) = delete;
42 ShaderManager& operator=(const ShaderManager&) = delete;
43
44 virtual ~ShaderManager() = default;
45
84 [[nodiscard]] std::optional<Error> compileShaders(
85 std::vector<ShaderDescription> vShadersToCompile,
86 const std::function<void(size_t iCompiledShaderCount, size_t iTotalShadersToCompile)>& onProgress,
87 const std::function<
88 void(ShaderDescription shaderDescription, std::variant<std::string, Error> error)>& onError,
89 const std::function<void()>& onCompleted);
90
99 bool isShaderNameCanBeUsed(const std::string& sShaderName);
100
127 bool markShaderToBeRemoved(const std::string& sShaderName);
128
134
135 protected:
158 size_t iQueryId,
159 const std::shared_ptr<std::atomic<size_t>>& pCompiledShaderCount,
160 size_t iTotalShaderCount,
161 ShaderDescription shaderDescription,
162 const std::function<void(size_t iCompiledShaderCount, size_t iTotalShadersToCompile)>& onProgress,
163 const std::function<
164 void(ShaderDescription shaderDescription, std::variant<std::string, Error> error)>& onError,
165 const std::function<void()>& onCompleted);
166
177 std::shared_ptr<ShaderPack> findShader(const std::string& sShaderName);
178
185 void releaseShaderBytecodeIfNotUsed(const std::string& sShaderName);
186
193 void removeShaderIfMarkedToBeRemoved(const std::string& sShaderName);
194
204 [[nodiscard]] std::optional<Error> refreshShaderCache();
205
206 private:
208 struct ShaderData {
216 std::unordered_map<std::string, std::shared_ptr<ShaderPack>> compiledShaders;
217
222 std::vector<std::string> vShadersToBeRemoved;
223 };
224
226 std::unique_ptr<ShaderCacheManager> pShaderCacheManager;
227
229 std::pair<std::recursive_mutex, ShaderData> mtxShaderData;
230
235 std::atomic<size_t> iTotalCompileShadersQueries = 0;
236
238 Renderer* const pRenderer = nullptr;
239
245 static inline const std::array<char, 65> vValidCharactersForShaderName = {
246 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
247 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
248 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
249 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_', '-'};
250
252 static inline const size_t iMaximumShaderNameLength = 40;
253 };
254} // namespace ne
Definition: Renderer.h:43
Definition: ShaderManager.h:25
bool markShaderToBeRemoved(const std::string &sShaderName)
Definition: ShaderManager.cpp:106
void performSelfValidation()
Definition: ShaderManager.cpp:136
std::unique_ptr< ShaderCacheManager > pShaderCacheManager
Definition: ShaderManager.h:226
void compileShaderTask(size_t iQueryId, const std::shared_ptr< std::atomic< size_t > > &pCompiledShaderCount, size_t iTotalShaderCount, ShaderDescription shaderDescription, const std::function< void(size_t iCompiledShaderCount, size_t iTotalShadersToCompile)> &onProgress, const std::function< void(ShaderDescription shaderDescription, std::variant< std::string, Error > error)> &onError, const std::function< void()> &onCompleted)
Definition: ShaderManager.cpp:318
static const size_t iMaximumShaderNameLength
Definition: ShaderManager.h:252
static const std::array< char, 65 > vValidCharactersForShaderName
Definition: ShaderManager.h:245
std::atomic< size_t > iTotalCompileShadersQueries
Definition: ShaderManager.h:235
std::shared_ptr< ShaderPack > findShader(const std::string &sShaderName)
Definition: ShaderManager.cpp:24
Renderer *const pRenderer
Definition: ShaderManager.h:238
bool isShaderNameCanBeUsed(const std::string &sShaderName)
Definition: ShaderManager.cpp:98
std::optional< Error > compileShaders(std::vector< ShaderDescription > vShadersToCompile, const std::function< void(size_t iCompiledShaderCount, size_t iTotalShadersToCompile)> &onProgress, const std::function< void(ShaderDescription shaderDescription, std::variant< std::string, Error > error)> &onError, const std::function< void()> &onCompleted)
Definition: ShaderManager.cpp:232
void removeShaderIfMarkedToBeRemoved(const std::string &sShaderName)
Definition: ShaderManager.cpp:56
std::pair< std::recursive_mutex, ShaderData > mtxShaderData
Definition: ShaderManager.h:229
void releaseShaderBytecodeIfNotUsed(const std::string &sShaderName)
Definition: ShaderManager.cpp:36
std::optional< Error > refreshShaderCache()
Definition: ShaderManager.cpp:86
Definition: ShaderUser.h:20
Definition: ShaderDescription.h:74
Definition: ShaderManager.h:208
std::unordered_map< std::string, std::shared_ptr< ShaderPack > > compiledShaders
Definition: ShaderManager.h:216
std::vector< std::string > vShadersToBeRemoved
Definition: ShaderManager.h:222