Nameless Engine
Loading...
Searching...
No Matches
ne::ShaderDescription Struct Reference

#include <ShaderDescription.h>

Public Member Functions

 ShaderDescription (const std::string &sShaderName, const std::filesystem::path &pathToShaderFile, ShaderType shaderType, std::optional< VertexFormat > vertexFormat, const std::string &sShaderEntryFunctionName, const std::unordered_map< std::string, std::string > &definedShaderMacros)
 
 ShaderDescription (const ShaderDescription &other) noexcept=default
 
ShaderDescriptionoperator= (const ShaderDescription &other) noexcept=default
 
 ShaderDescription (ShaderDescription &&other) noexcept=default
 
ShaderDescriptionoperator= (ShaderDescription &&other) noexcept=default
 
std::optional< ShaderCacheInvalidationReason > isSerializableDataEqual (ShaderDescription &other)
 
void from_toml (const toml::value &data)
 
toml::value into_toml () const
 

Static Public Member Functions

static const char * getConfigurationFileSectionName ()
 
static std::string getFileHash (const std::filesystem::path &pathToFile, const std::string &sShaderName)
 

Public Attributes

std::unordered_map< std::string, std::string > definedShaderMacros
 
std::string sShaderName
 
std::filesystem::path pathToShaderFile
 
ShaderType shaderType = ShaderType::VERTEX_SHADER
 
std::optional< VertexFormat > vertexFormat
 
std::string sShaderEntryFunctionName
 

Private Member Functions

void calculateShaderIncludeTreeHashes ()
 

Static Private Member Functions

static std::unordered_map< std::string, std::unordered_map< std::string, std::string > > deserializeShaderIncludeTreeHashes (const toml::value &data)
 
static void serializeShaderIncludeTree (const std::filesystem::path &pathToShaderFile, std::string &sCurrentIncludeChain, toml::value &data)
 

Private Attributes

std::string sSourceFileHash
 
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > shaderIncludeTreeHashes
 

Static Private Attributes

static std::string_view sInitialIncludeChainText = "includes"
 
static auto sConfigurationFileSectionName = "shader_description"
 

Detailed Description

Describes a shader.

Constructor & Destructor Documentation

◆ ShaderDescription() [1/3]

ne::ShaderDescription::ShaderDescription ( const std::string &  sShaderName,
const std::filesystem::path &  pathToShaderFile,
ShaderType  shaderType,
std::optional< VertexFormat >  vertexFormat,
const std::string &  sShaderEntryFunctionName,
const std::unordered_map< std::string, std::string > &  definedShaderMacros 
)

Constructor.

Parameters
sShaderNameGlobally unique shader name.
pathToShaderFilePath to the shader file.
shaderTypeType of the shader.
vertexFormatVertex format that the shader uses (can be empty if not applicable, for example if this shader is a compute shader).
sShaderEntryFunctionNameName of the shader's entry function name. For example: if the shader type is vertex shader, then this value should contain name of the function used for vertex processing (from shader's file, "VS" for example).
definedShaderMacrosMap of defined macros for shader, stores pairs of "macro name" - "value" (no value if empty).

◆ ShaderDescription() [2/3]

ne::ShaderDescription::ShaderDescription ( const ShaderDescription other)
defaultnoexcept

Copy constructor.

Parameters
otherOther object.

◆ ShaderDescription() [3/3]

ne::ShaderDescription::ShaderDescription ( ShaderDescription &&  other)
defaultnoexcept

Move constructor.

Parameters
otherOther object.

Member Function Documentation

◆ calculateShaderIncludeTreeHashes()

void ne::ShaderDescription::calculateShaderIncludeTreeHashes ( )
private

Uses pathToShaderFile to recursively calculate hashes of all include files to fill shaderIncludeTreeHashes.

◆ deserializeShaderIncludeTreeHashes()

std::unordered_map< std::string, std::unordered_map< std::string, std::string > > ne::ShaderDescription::deserializeShaderIncludeTreeHashes ( const toml::value &  data)
staticprivate

Converts TOML table into shader include tree hash map.

Parameters
dataTOML data formatted as table.
Returns
Map [include chain (i.e. current shader), [relative include path, include file hash]].

◆ from_toml()

void ne::ShaderDescription::from_toml ( const toml::value &  data)

Used to deserialize struct from .toml file.

Parameters
dataToml value.

◆ getConfigurationFileSectionName()

static const char * ne::ShaderDescription::getConfigurationFileSectionName ( )
inlinestatic

Returns name of the section that is used to store ShaderDescription in configuration (on disk).

Returns
Section name.

◆ getFileHash()

std::string ne::ShaderDescription::getFileHash ( const std::filesystem::path &  pathToFile,
const std::string &  sShaderName 
)
static

Calculates hash of a file and returns it.

Parameters
pathToFilePath to a file. Assumes that this path exists.
sShaderNameUnique name of this shader (used for logging).
Returns
Empty string if something went wrong, source file hash otherwise.

◆ into_toml()

toml::value ne::ShaderDescription::into_toml ( ) const

Used to serialize struct to .toml file.

Returns
Toml value.

◆ isSerializableDataEqual()

std::optional< ShaderCacheInvalidationReason > ne::ShaderDescription::isSerializableDataEqual ( ShaderDescription other)

Compares this shader description with other to see if the serializable fields are equal. This is usually done to check if shader cache is valid or not (needs to be recompiled or not).

Parameters
otherOther shader description to compare with. If this "other" shader description was retrieved from the cache it will not be changed, otherwise if it has path to the shader source file specified it will be updated with hashes of the source shader file and its includes.
Returns
Whether the data is equal or not. If the data is not equal, has a value that contains a reason.

◆ operator=() [1/2]

ShaderDescription & ne::ShaderDescription::operator= ( const ShaderDescription other)
defaultnoexcept

Copy assignment.

Parameters
otherother object.
Returns
Result of copy assignment.

◆ operator=() [2/2]

ShaderDescription & ne::ShaderDescription::operator= ( ShaderDescription &&  other)
defaultnoexcept

Move assignment.

Parameters
otherOther object.
Returns
Result of move assignment.

◆ serializeShaderIncludeTree()

void ne::ShaderDescription::serializeShaderIncludeTree ( const std::filesystem::path &  pathToShaderFile,
std::string &  sCurrentIncludeChain,
toml::value &  data 
)
staticprivate

Scans shader file for "#include" entries and recursively adds included files hashes.

Parameters
pathToShaderFilePath to shader source file.
sCurrentIncludeChainCurrent section (in TOML) text.
dataTOML structure to write to.

Member Data Documentation

◆ definedShaderMacros

std::unordered_map<std::string, std::string> ne::ShaderDescription::definedShaderMacros

Map of defined macros for shader, stores pairs of "macro name" - "value" (no value if empty).

◆ pathToShaderFile

std::filesystem::path ne::ShaderDescription::pathToShaderFile

Path to the shader file.

◆ sConfigurationFileSectionName

auto ne::ShaderDescription::sConfigurationFileSectionName = "shader_description"
inlinestaticprivate

Name of the section that is used to store ShaderDescription in configuration (on disk).

◆ shaderIncludeTreeHashes

std::unordered_map< std::string , std:: unordered_map<std::string , std::string > > ne::ShaderDescription::shaderIncludeTreeHashes
private

Shader include tree hashes, contains relative include paths for each "#include" entry in shader with included source file hash. May be empty (not calculated yet).

◆ shaderType

ShaderType ne::ShaderDescription::shaderType = ShaderType::VERTEX_SHADER

Type of the shader.

◆ sInitialIncludeChainText

std::string_view ne::ShaderDescription::sInitialIncludeChainText = "includes"
inlinestaticprivate

Used as the initial text for "include chain" string (starting text in serialized form). After serializing data in the TOML file, it might look like this:

"includes.default.post_process"
^------ [includes] initial text (this variable)
^------ [default] shader file name from ShaderDescription (say shader A)
^------ [post_process] shader file name B that shader A includes
Definition: ShaderDescription.h:74

◆ sShaderEntryFunctionName

std::string ne::ShaderDescription::sShaderEntryFunctionName

Name of the shader's entry function name.

◆ sShaderName

std::string ne::ShaderDescription::sShaderName

Globally unique shader name.

◆ sSourceFileHash

std::string ne::ShaderDescription::sSourceFileHash
private

Shader source file hash, may be empty (not calculated yet).

◆ vertexFormat

std::optional<VertexFormat> ne::ShaderDescription::vertexFormat

Used vertex format.

Remarks
Can be empty if not applicable (for example if this is a compute shader).

The documentation for this struct was generated from the following files: