Nameless Engine
Loading...
Searching...
No Matches
ShaderDescription.h
1#pragma once
2
3// Standard.
4#include <filesystem>
5
6// Custom.
7#include "io/ConfigManager.h"
8#include "shader/general/formats/VertexFormat.h"
9
10namespace ne {
17 enum class ShaderType : int {
18 VERTEX_SHADER = 0, //< vertex shader
19 FRAGMENT_SHADER = 1, //< pixel/fragment shader
20 COMPUTE_SHADER = 2, //< compute shader
21 // new types go here...
22 // add a test for new type...
23 };
24
28 enum class ShaderCacheInvalidationReason {
29 ENTRY_FUNCTION_NAME_CHANGED,
30 SHADER_TYPE_CHANGED,
31 DEFINED_SHADER_MACROS_CHANGED,
32 SHADER_SOURCE_FILE_CHANGED,
33 SHADER_INCLUDE_TREE_CONTENT_CHANGED,
34 COMPILED_BINARY_CHANGED, //< some binary file was changed or missing
35 // add new entry to ShaderCacheInvalidationReasonDescription...
36 // add test for new reason...
37 };
38
42 inline static const std::unordered_map<ShaderCacheInvalidationReason, const char*>
44 {ShaderCacheInvalidationReason::ENTRY_FUNCTION_NAME_CHANGED,
45 "shader entry function name changed"},
46 {ShaderCacheInvalidationReason::SHADER_TYPE_CHANGED, "shader type changed"},
47 {ShaderCacheInvalidationReason::DEFINED_SHADER_MACROS_CHANGED,
48 "defined shader macros changed"},
49 {ShaderCacheInvalidationReason::SHADER_SOURCE_FILE_CHANGED, "shader source file changed"},
50 {ShaderCacheInvalidationReason::SHADER_INCLUDE_TREE_CONTENT_CHANGED,
51 "shader include tree content changed"},
52 {ShaderCacheInvalidationReason::COMPILED_BINARY_CHANGED,
53 "previously compiled binary file(s) changed"}};
54
62 static const char* getDescription(ShaderCacheInvalidationReason reason) {
63 const auto it = cacheInvalidationReasons.find(reason);
64 if (it == cacheInvalidationReasons.end()) [[unlikely]] {
65 throw std::runtime_error("no description is provided for this reason");
66 }
67 return it->second;
68 }
69 };
70
81
82 ShaderDescription() = default;
83
100 const std::string& sShaderName,
101 const std::filesystem::path& pathToShaderFile,
102 ShaderType shaderType,
103 std::optional<VertexFormat> vertexFormat,
104 const std::string& sShaderEntryFunctionName,
105 const std::unordered_map<std::string, std::string>& definedShaderMacros);
106
112 ShaderDescription(const ShaderDescription& other) noexcept = default;
113
121 ShaderDescription& operator=(const ShaderDescription& other) noexcept = default;
122
128 ShaderDescription(ShaderDescription&& other) noexcept = default;
129
137 ShaderDescription& operator=(ShaderDescription&& other) noexcept = default;
138
147 static std::string
148 getFileHash(const std::filesystem::path& pathToFile, const std::string& sShaderName);
149
164 std::optional<ShaderCacheInvalidationReason> isSerializableDataEqual(ShaderDescription& other);
165
171 void from_toml(const toml::value& data); // NOLINT
172
178 toml::value into_toml() const; // NOLINT
179
180 // ----------------------------------------------------------------------------------------
181
182 // ------------------------------- ! if adding new fields ! ------------------------------------
183 // if adding new fields:
184 // - add to constructor,
185 // - if fields should be considered when validating cache,
186 // add fields to @ref from_toml, @ref into_toml and @ref isSerializableDataEqual.
187 // ----------------------------------------
188
190 std::unordered_map<std::string, std::string> definedShaderMacros;
191
193 std::string sShaderName;
194
196 std::filesystem::path pathToShaderFile;
197
199 ShaderType shaderType = ShaderType::VERTEX_SHADER;
200
206 std::optional<VertexFormat> vertexFormat;
207
210
211 // ------------------------------- ! if adding new fields ! ------------------------------------
212 // if adding new fields:
213 // - add to constructor,
214 // - if fields should be considered when validating cache,
215 // add fields to @ref from_toml, @ref into_toml and @ref isSerializableDataEqual.
216 // ----------------------------------------
217
218 private:
224
232 static std::unordered_map<
233 std::string ,
234 std::
235 unordered_map<std::string , std::string >>
236 deserializeShaderIncludeTreeHashes(const toml::value& data);
237
246 static void serializeShaderIncludeTree(
247 const std::filesystem::path& pathToShaderFile,
248 std::string& sCurrentIncludeChain,
249 toml::value& data);
250
251 // ----------------------------------------------------------------------------------------
252
254 std::string sSourceFileHash;
255
261 std::unordered_map<
262 std::string ,
263 std::
264 unordered_map<std::string , std::string >>
266
267 // ------------------------------- ! if adding new fields ! ------------------------------------
268 // if adding new fields:
269 // - if fields should be considered when validating cache,
270 // add fields to @ref from_toml, @ref into_toml and @ref isSerializableDataEqual.
271 // ----------------------------------------
272
284 static inline std::string_view sInitialIncludeChainText = "includes";
285
287 inline static auto sConfigurationFileSectionName = "shader_description";
288 };
289} // namespace ne
Definition: ShaderDescription.h:40
static const std::unordered_map< ShaderCacheInvalidationReason, const char * > cacheInvalidationReasons
Definition: ShaderDescription.h:43
static const char * getDescription(ShaderCacheInvalidationReason reason)
Definition: ShaderDescription.h:62
Definition: ShaderDescription.h:74
static std::unordered_map< std::string, std::unordered_map< std::string, std::string > > deserializeShaderIncludeTreeHashes(const toml::value &data)
Definition: ShaderDescription.cpp:106
ShaderDescription(ShaderDescription &&other) noexcept=default
std::string sSourceFileHash
Definition: ShaderDescription.h:254
static std::string getFileHash(const std::filesystem::path &pathToFile, const std::string &sShaderName)
Definition: ShaderDescription.cpp:60
void calculateShaderIncludeTreeHashes()
Definition: ShaderDescription.cpp:85
ShaderDescription & operator=(const ShaderDescription &other) noexcept=default
std::string sShaderEntryFunctionName
Definition: ShaderDescription.h:209
toml::value into_toml() const
Definition: ShaderDescription.cpp:41
ShaderDescription & operator=(ShaderDescription &&other) noexcept=default
std::unordered_map< std::string, std::string > definedShaderMacros
Definition: ShaderDescription.h:190
static void serializeShaderIncludeTree(const std::filesystem::path &pathToShaderFile, std::string &sCurrentIncludeChain, toml::value &data)
Definition: ShaderDescription.cpp:202
std::optional< ShaderCacheInvalidationReason > isSerializableDataEqual(ShaderDescription &other)
Definition: ShaderDescription.cpp:140
static std::string_view sInitialIncludeChainText
Definition: ShaderDescription.h:284
ShaderType shaderType
Definition: ShaderDescription.h:199
static const char * getConfigurationFileSectionName()
Definition: ShaderDescription.h:80
ShaderDescription(const ShaderDescription &other) noexcept=default
void from_toml(const toml::value &data)
Definition: ShaderDescription.cpp:28
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > shaderIncludeTreeHashes
Definition: ShaderDescription.h:265
static auto sConfigurationFileSectionName
Definition: ShaderDescription.h:287
std::filesystem::path pathToShaderFile
Definition: ShaderDescription.h:196
std::optional< VertexFormat > vertexFormat
Definition: ShaderDescription.h:206
std::string sShaderName
Definition: ShaderDescription.h:193