Nameless Engine
Loading...
Searching...
No Matches
Shader.h
1#pragma once
2
3// Standard.
4#include <filesystem>
5#include <memory>
6#include <unordered_map>
7#include <variant>
8
9// Custom.
10#include "misc/Error.h"
11#include "shader/ShaderDescription.h"
12#include "shader/general/format/VertexFormat.h"
13
14namespace ne {
15 class Renderer;
16 class ConfigManager;
17
23 class Shader {
24 public:
25 Shader() = delete;
26 Shader(const Shader&) = delete;
27 Shader& operator=(const Shader&) = delete;
28
29 virtual ~Shader() = default;
30
36 static constexpr const char* getFrameConstantsShaderResourceName() {
38 }
39
46
60 static std::variant<std::shared_ptr<Shader>, std::string, Error> compileShader(
62 const std::filesystem::path& shaderCacheDirectory,
63 const std::string& sConfiguration,
64 const ShaderDescription& shaderDescription);
65
84 static std::variant<std::shared_ptr<Shader>, Error> createFromCache(
86 const std::filesystem::path& pathToCompiledShaderBytecode,
87 ShaderDescription& shaderDescription,
88 const std::string& sShaderNameWithoutConfiguration,
89 std::optional<ShaderCacheInvalidationReason>& cacheInvalidationReason);
90
96 std::string getShaderName() const;
97
103 ShaderType getShaderType() const;
104
111 std::optional<VertexFormat> getVertexFormat() const;
112
121
122 protected:
133 Shader(
135 std::filesystem::path pathToCompiledShader,
136 const std::string& sShaderName,
137 ShaderType shaderType,
138 std::optional<VertexFormat> vertexFormat);
139
145
151
162 [[nodiscard]] virtual std::optional<Error>
164 return {};
165 };
166
178 [[nodiscard]] virtual std::optional<Error> checkCachedAdditionalCompilationResultsInfo(
179 ConfigManager& cacheMetadataConfigManager,
180 std::optional<ShaderCacheInvalidationReason>& cacheInvalidationReason) {
181 return {};
182 };
183
189 std::variant<std::filesystem::path, Error> getPathToCompiledShader();
190
196 Renderer* getRenderer() const;
197
198 private:
212 static std::variant<std::shared_ptr<Shader>, std::string, Error> compileRenderDependentShader(
214 const std::filesystem::path& shaderCacheDirectory,
215 const std::string& sConfiguration,
216 const ShaderDescription& shaderDescription);
217
232 static std::variant<std::shared_ptr<Shader>, Error> createRenderDependentShaderFromCache(
234 const std::filesystem::path& pathToSourceShaderFile,
235 const std::filesystem::path& pathToCompiledShaderBytecode,
236 const std::string& sShaderName,
237 ShaderType shaderType,
238 std::optional<VertexFormat> vertexFormat);
239
241 Renderer* const pRenderer = nullptr;
242
248 const std::optional<VertexFormat> optionalVertexFormat;
249
251 const std::string sShaderName;
252
254 const ShaderType shaderType;
255
257 const std::filesystem::path pathToCompiledShader;
258
260 static inline const auto sCompiledBytecodeHashKeyName = "compiled_bytecode_hash";
261
263 static constexpr auto pFrameConstantBufferName = "frameData";
264 };
265} // namespace ne
Definition: ConfigManager.h:27
Definition: Error.h:27
Definition: Renderer.h:43
Definition: Shader.h:23
std::optional< VertexFormat > getVertexFormat() const
Definition: Shader.cpp:311
virtual std::optional< Error > checkCachedAdditionalCompilationResultsInfo(ConfigManager &cacheMetadataConfigManager, std::optional< ShaderCacheInvalidationReason > &cacheInvalidationReason)
Definition: Shader.h:178
virtual bool releaseShaderDataFromMemoryIfLoaded()=0
const std::optional< VertexFormat > optionalVertexFormat
Definition: Shader.h:248
static std::variant< std::shared_ptr< Shader >, std::string, Error > compileShader(Renderer *pRenderer, const std::filesystem::path &shaderCacheDirectory, const std::string &sConfiguration, const ShaderDescription &shaderDescription)
Definition: Shader.cpp:81
Renderer * getRenderer() const
Definition: Shader.cpp:313
const ShaderType shaderType
Definition: Shader.h:254
ShaderType getShaderType() const
Definition: Shader.cpp:315
std::string getShaderName() const
Definition: Shader.cpp:300
static constexpr auto pFrameConstantBufferName
Definition: Shader.h:263
virtual std::optional< Error > saveAdditionalCompilationResultsInfo(ConfigManager &cacheMetadataConfigManager)
Definition: Shader.h:163
const std::filesystem::path pathToCompiledShader
Definition: Shader.h:257
static void notifyShaderBytecodeReleasedFromMemory()
Definition: Shader.cpp:34
static const auto sCompiledBytecodeHashKeyName
Definition: Shader.h:260
static constexpr const char * getFrameConstantsShaderResourceName()
Definition: Shader.h:36
static size_t getCurrentAmountOfShadersInMemory()
Definition: Shader.cpp:30
const std::string sShaderName
Definition: Shader.h:251
static std::variant< std::shared_ptr< Shader >, std::string, Error > compileRenderDependentShader(Renderer *pRenderer, const std::filesystem::path &shaderCacheDirectory, const std::string &sConfiguration, const ShaderDescription &shaderDescription)
Definition: Shader.cpp:45
std::variant< std::filesystem::path, Error > getPathToCompiledShader()
Definition: Shader.cpp:302
static std::variant< std::shared_ptr< Shader >, Error > createFromCache(Renderer *pRenderer, const std::filesystem::path &pathToCompiledShaderBytecode, ShaderDescription &shaderDescription, const std::string &sShaderNameWithoutConfiguration, std::optional< ShaderCacheInvalidationReason > &cacheInvalidationReason)
Definition: Shader.cpp:153
static void notifyShaderBytecodeLoadedIntoMemory()
Definition: Shader.cpp:32
Renderer *const pRenderer
Definition: Shader.h:241
static std::variant< std::shared_ptr< Shader >, Error > createRenderDependentShaderFromCache(Renderer *pRenderer, const std::filesystem::path &pathToSourceShaderFile, const std::filesystem::path &pathToCompiledShaderBytecode, const std::string &sShaderName, ShaderType shaderType, std::optional< VertexFormat > vertexFormat)
Definition: Shader.cpp:265
Definition: ShaderDescription.h:74