Nameless Engine
Loading...
Searching...
No Matches
ne::Shader Class Referenceabstract

#include <Shader.h>

Inheritance diagram for ne::Shader:
ne::GlslShader ne::HlslShader

Public Member Functions

 Shader (const Shader &)=delete
 
Shaderoperator= (const Shader &)=delete
 
std::string getShaderName () const
 
ShaderType getShaderType () const
 
std::optional< VertexFormat > getVertexFormat () const
 
virtual bool releaseShaderDataFromMemoryIfLoaded ()=0
 

Static Public Member Functions

static constexpr const char * getFrameConstantsShaderResourceName ()
 
static size_t getCurrentAmountOfShadersInMemory ()
 
static std::variant< std::shared_ptr< Shader >, std::string, ErrorcompileShader (Renderer *pRenderer, const std::filesystem::path &shaderCacheDirectory, const std::string &sConfiguration, const ShaderDescription &shaderDescription)
 
static std::variant< std::shared_ptr< Shader >, ErrorcreateFromCache (Renderer *pRenderer, const std::filesystem::path &pathToCompiledShaderBytecode, ShaderDescription &shaderDescription, const std::string &sShaderNameWithoutConfiguration, std::optional< ShaderCacheInvalidationReason > &cacheInvalidationReason)
 

Protected Member Functions

 Shader (Renderer *pRenderer, std::filesystem::path pathToCompiledShader, const std::string &sShaderName, ShaderType shaderType, std::optional< VertexFormat > vertexFormat)
 
virtual std::optional< ErrorsaveAdditionalCompilationResultsInfo (ConfigManager &cacheMetadataConfigManager)
 
virtual std::optional< ErrorcheckCachedAdditionalCompilationResultsInfo (ConfigManager &cacheMetadataConfigManager, std::optional< ShaderCacheInvalidationReason > &cacheInvalidationReason)
 
std::variant< std::filesystem::path, ErrorgetPathToCompiledShader ()
 
RenderergetRenderer () const
 

Static Protected Member Functions

static void notifyShaderBytecodeLoadedIntoMemory ()
 
static void notifyShaderBytecodeReleasedFromMemory ()
 

Static Private Member Functions

static std::variant< std::shared_ptr< Shader >, std::string, ErrorcompileRenderDependentShader (Renderer *pRenderer, const std::filesystem::path &shaderCacheDirectory, const std::string &sConfiguration, const ShaderDescription &shaderDescription)
 
static std::variant< std::shared_ptr< Shader >, ErrorcreateRenderDependentShaderFromCache (Renderer *pRenderer, const std::filesystem::path &pathToSourceShaderFile, const std::filesystem::path &pathToCompiledShaderBytecode, const std::string &sShaderName, ShaderType shaderType, std::optional< VertexFormat > vertexFormat)
 

Private Attributes

Renderer *const pRenderer = nullptr
 
const std::optional< VertexFormat > optionalVertexFormat
 
const std::string sShaderName
 
const ShaderType shaderType
 
const std::filesystem::path pathToCompiledShader
 

Static Private Attributes

static const auto sCompiledBytecodeHashKeyName = "compiled_bytecode_hash"
 
static constexpr auto pFrameConstantBufferName = "frameData"
 

Detailed Description

Base class for different types/formats of shaders to implement.

Represents a single compiled shader variant from the ShaderPack.

Constructor & Destructor Documentation

◆ Shader()

ne::Shader::Shader ( Renderer pRenderer,
std::filesystem::path  pathToCompiledShader,
const std::string &  sShaderName,
ShaderType  shaderType,
std::optional< VertexFormat >  vertexFormat 
)
protected

Constructor.

Parameters
pRendererUsed renderer.
pathToCompiledShaderPath to compiled shader bytecode on disk.
sShaderNameUnique name of this shader.
shaderTypeType of this shader.
vertexFormatVertex format that this shader uses (can be empty if not applicable, for example if this is a compute shader).

Member Function Documentation

◆ checkCachedAdditionalCompilationResultsInfo()

virtual std::optional< Error > ne::Shader::checkCachedAdditionalCompilationResultsInfo ( ConfigManager cacheMetadataConfigManager,
std::optional< ShaderCacheInvalidationReason > &  cacheInvalidationReason 
)
inlineprotectedvirtual

Used to check cached data of shader language specific (additional) shader compilation results (such as reflection data, i.e. if there are some other compilation results besides compiled shader bytecode which is automatically hashed and checked) whether its valid or not.

Parameters
cacheMetadataConfigManagerConfig manager to write the data to.
cacheInvalidationReasonWill be not empty if cache was invalidated (i.e. cache can't be used).
Returns
Error if some internal error occurred.

Reimplemented in ne::GlslShader, and ne::HlslShader.

◆ compileRenderDependentShader()

std::variant< std::shared_ptr< Shader >, std::string, Error > ne::Shader::compileRenderDependentShader ( Renderer pRenderer,
const std::filesystem::path &  shaderCacheDirectory,
const std::string &  sConfiguration,
const ShaderDescription shaderDescription 
)
staticprivate

Compiles a HLSL/GLSL shader depending on the used renderer.

Parameters
pRendererCurrent renderer.
shaderCacheDirectoryDirectory to store this shader's cache, for example: ".../shader_cache/engine.default".
sConfigurationShader configuration text that will be added to the name.
shaderDescriptionDescription that describes the shader and how the shader should be compiled.
Returns
One of the three values: compiled shader, string containing shader compilation error/warning or an internal error.

◆ compileShader()

std::variant< std::shared_ptr< Shader >, std::string, Error > ne::Shader::compileShader ( Renderer pRenderer,
const std::filesystem::path &  shaderCacheDirectory,
const std::string &  sConfiguration,
const ShaderDescription shaderDescription 
)
static

Compiles a shader.

Parameters
pRendererCurrent renderer.
shaderCacheDirectoryDirectory to store this shader's cache, for example: ".../shader_cache/engine.default".
sConfigurationShader configuration text that will be added to the name.
shaderDescriptionDescription that describes the shader and how the shader should be compiled.
Returns
One of the three values: compiled shader, string containing shader compilation error/warning or an internal error.

◆ createFromCache()

std::variant< std::shared_ptr< Shader >, Error > ne::Shader::createFromCache ( Renderer pRenderer,
const std::filesystem::path &  pathToCompiledShaderBytecode,
ShaderDescription shaderDescription,
const std::string &  sShaderNameWithoutConfiguration,
std::optional< ShaderCacheInvalidationReason > &  cacheInvalidationReason 
)
static

Creates a new shader using shader cache.

Parameters
pRendererUsed renderer.
pathToCompiledShaderBytecodePath to compiled shader bytecode on disk (with configuration), for example: ".../shader_cache/engine.default.vs/shader16604691462768904089".
shaderDescriptionDescription that describes the shader and how the shader should be compiled. Used for cache invalidation.
sShaderNameWithoutConfigurationInitial shader name without configuration hash, this name is used for logging.
cacheInvalidationReasonWill be not empty if cache was invalidated (i.e. cache can't be used).
Returns
Error if the shader cache is corrupted/invalidated (cache invalidation reason should have a value and returned Error will contain a full description of this invalidation reason) or there was something wrong while attempting to load the cache, otherwise a shader created using shader cache.

◆ createRenderDependentShaderFromCache()

std::variant< std::shared_ptr< Shader >, Error > ne::Shader::createRenderDependentShaderFromCache ( Renderer pRenderer,
const std::filesystem::path &  pathToSourceShaderFile,
const std::filesystem::path &  pathToCompiledShaderBytecode,
const std::string &  sShaderName,
ShaderType  shaderType,
std::optional< VertexFormat >  vertexFormat 
)
staticprivate

Creates a new HLSL/GLSL shader depending on the used renderer, expects that all cached shader data is valid.

Parameters
pRendererUsed renderer.
pathToSourceShaderFilePath to shader source code file.
pathToCompiledShaderBytecodePath to compiled shader bytecode on disk.
sShaderNameUnique name of this shader.
shaderTypeType of this shader.
vertexFormatCan be empty if non applicable (for example if this shader is a compute shader).
Returns
Error if something went wrong, otherwise created shader.

◆ getCurrentAmountOfShadersInMemory()

size_t ne::Shader::getCurrentAmountOfShadersInMemory ( )
static

Returns the current amount shaders (vertex, pixel, etc.) loaded into the memory (RAM/VRAM).

Returns
Current amount of shaders loaded into the memory (RAM/VRAM).

◆ getFrameConstantsShaderResourceName()

static constexpr const char * ne::Shader::getFrameConstantsShaderResourceName ( )
inlinestaticconstexpr

Returns name of the shader resource that stores frame-global constants.

Returns
Shader resource name.

◆ getPathToCompiledShader()

std::variant< std::filesystem::path, Error > ne::Shader::getPathToCompiledShader ( )
protected

Returns path to compiled shader blob on disk.

Returns
Error if the compiled shader does not exist, otherwise path to compiled shader.

◆ getRenderer()

Renderer * ne::Shader::getRenderer ( ) const
protected

Returns renderer.

Returns
renderer.

◆ getShaderName()

std::string ne::Shader::getShaderName ( ) const

Returns unique name of this shader.

Returns
Unique name of this shader.

◆ getShaderType()

ShaderType ne::Shader::getShaderType ( ) const

Returns type of this shader.

Returns
Shader type.

◆ getVertexFormat()

std::optional< VertexFormat > ne::Shader::getVertexFormat ( ) const

Returns vertex format used by this shader.

Returns
Empty if not applicable (for example if this shader is a compute shader), otherwise used vertex format.

◆ notifyShaderBytecodeLoadedIntoMemory()

void ne::Shader::notifyShaderBytecodeLoadedIntoMemory ( )
staticprotected

Derived shader classes should call this function once they load shader bytecode into the memory from the disk.

◆ notifyShaderBytecodeReleasedFromMemory()

void ne::Shader::notifyShaderBytecodeReleasedFromMemory ( )
staticprotected

Derived shader classes should call this function once they release shader bytecode from the memory.

◆ releaseShaderDataFromMemoryIfLoaded()

virtual bool ne::Shader::releaseShaderDataFromMemoryIfLoaded ( )
pure virtual

Releases underlying shader bytecode for each shader from memory (this object will not be deleted) if the shader bytecode was loaded into memory. Next time this shader will be needed it will be loaded from disk.

Returns
false if was released from memory, true if was not loaded in memory previously.

Implemented in ne::GlslShader, and ne::HlslShader.

◆ saveAdditionalCompilationResultsInfo()

virtual std::optional< Error > ne::Shader::saveAdditionalCompilationResultsInfo ( ConfigManager cacheMetadataConfigManager)
inlineprotectedvirtual

Used to save data of shader language specific (additional) shader compilation results (such as reflection data, i.e. if there are some other compilation results besides compiled shader bytecode which is automatically hashed and checked) to later check them in checkCachedAdditionalCompilationResultsInfo.

Parameters
cacheMetadataConfigManagerConfig manager to write the data to.
Returns
Error if something went wrong.

Reimplemented in ne::GlslShader, and ne::HlslShader.

Member Data Documentation

◆ optionalVertexFormat

const std::optional<VertexFormat> ne::Shader::optionalVertexFormat
private

Vertex format that this shader uses.

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

◆ pathToCompiledShader

const std::filesystem::path ne::Shader::pathToCompiledShader
private

Path to compiled shader.

◆ pFrameConstantBufferName

constexpr auto ne::Shader::pFrameConstantBufferName = "frameData"
staticconstexprprivate

Name of the cbuffer/uniform resource used to store frame data in shaders.

◆ pRenderer

Renderer* const ne::Shader::pRenderer = nullptr
private

Do not delete. Used renderer.

◆ sCompiledBytecodeHashKeyName

const auto ne::Shader::sCompiledBytecodeHashKeyName = "compiled_bytecode_hash"
inlinestaticprivate

Name of the key used to store compiled bytecode hash in the metadata file.

◆ shaderType

const ShaderType ne::Shader::shaderType
private

Type of this shader.

◆ sShaderName

const std::string ne::Shader::sShaderName
private

Unique shader name received from ShaderManager.


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