Nameless Engine
Loading...
Searching...
No Matches
ne::ShaderPack Class Reference

#include <ShaderPack.h>

Classes

struct  InternalResources
 

Public Member Functions

 ShaderPack (const ShaderPack &)=delete
 
ShaderPackoperator= (const ShaderPack &)=delete
 
bool releaseShaderPackDataFromMemoryIfLoaded ()
 
std::shared_ptr< ShadergetShader (const std::set< ShaderMacro > &additionalConfiguration, std::set< ShaderMacro > &fullShaderConfiguration)
 
std::string getShaderName () const
 
ShaderType getShaderType ()
 
std::pair< std::mutex, InternalResources > * getInternalResources ()
 

Static Public Member Functions

static std::variant< std::shared_ptr< ShaderPack >, std::string, ErrorcompileShaderPack (Renderer *pRenderer, const ShaderDescription &shaderDescription)
 
static std::variant< std::shared_ptr< ShaderPack >, ErrorcreateFromCache (Renderer *pRenderer, const ShaderDescription &shaderDescription, std::optional< ShaderCacheInvalidationReason > &cacheInvalidationReason)
 

Private Member Functions

 ShaderPack (const std::string &sShaderName, ShaderType shaderType)
 
void setRendererConfiguration (const std::set< ShaderMacro > &renderConfiguration)
 

Static Private Member Functions

static void addEngineMacrosToShaderDescription (ShaderDescription &description, const std::set< ShaderMacro > &shaderConfigurationMacros, Renderer *pRenderer)
 

Private Attributes

std::pair< std::mutex, InternalResourcesmtxInternalResources
 
std::string sShaderName
 
ShaderType shaderType
 

Friends

class ShaderManager
 

Detailed Description

Represents a group of different variants of one shader (typically this means one shader compiled with different combinations of predefined macros).

Constructor & Destructor Documentation

◆ ShaderPack()

ne::ShaderPack::ShaderPack ( const std::string &  sShaderName,
ShaderType  shaderType 
)
private

Constructor to create an empty shader pack.

Parameters
sShaderNameInitial name of the shader.
shaderTypeType of shaders this pack stores.

Member Function Documentation

◆ addEngineMacrosToShaderDescription()

void ne::ShaderPack::addEngineMacrosToShaderDescription ( ShaderDescription description,
const std::set< ShaderMacro > &  shaderConfigurationMacros,
Renderer pRenderer 
)
staticprivate

Adds additional defined macros to shader description that engine shaders expect.

Parameters
descriptionShader description to modify.
shaderConfigurationMacrosMacros of the current shader configuration to add.
pRendererUsed renderer.

◆ compileShaderPack()

std::variant< std::shared_ptr< ShaderPack >, std::string, Error > ne::ShaderPack::compileShaderPack ( Renderer pRenderer,
const ShaderDescription shaderDescription 
)
static

Compiles a shader pack.

Parameters
pRendererUsed renderer.
shaderDescriptionDescription that describes the shader and how the shader should be compiled.
Returns
One of the three values: compiled shader pack, string containing shader compilation error/warning, internal error

◆ createFromCache()

std::variant< std::shared_ptr< ShaderPack >, Error > ne::ShaderPack::createFromCache ( Renderer pRenderer,
const ShaderDescription shaderDescription,
std::optional< ShaderCacheInvalidationReason > &  cacheInvalidationReason 
)
static

Creates a new shader pack using shader cache on the disk.

Parameters
pRendererUsed renderer.
shaderDescriptionDescription that describes the shader and how the shader should be compiled. Used for cache invalidation.
cacheInvalidationReasonWill be not empty if cache was invalidated. Used for testing.
Returns
Error if shader cache is corrupted or invalid, this also means that corrupted/invalid shader cache directory was deleted and cache for this shader no longer exists, otherwise a shader pack created using cache.

◆ getInternalResources()

std::pair< std::mutex, ShaderPack::InternalResources > * ne::ShaderPack::getInternalResources ( )

Returns internal resources that this shader pack uses.

Returns
Internal resources.

◆ getShader()

std::shared_ptr< Shader > ne::ShaderPack::getShader ( const std::set< ShaderMacro > &  additionalConfiguration,
std::set< ShaderMacro > &  fullShaderConfiguration 
)

Returns a shader that matches the current renderer's shader configuration and the specified additional configuration.

Remarks
Since renderer's shader configuration usually does not contain all needed macros for a shader, you should specify an additional configuration that will be considered together with the renderer's configuration to find a matching shader that has uses this configuration (macros).
Some macros that the renderer defines in the current renderer shader configuration will not be appended to the specified configuration if they are not applicable (i.e. if the renderer defines texture filtering macro but your configuration does not have a diffuse texture macro - adding texture filtering macro is useless) see ShaderMacro::isMacroShouldBeConsideredInConfiguration.
If the a shader that matches the target configuration is not found an error will be shown and an exception will be thrown.
Parameters
additionalConfigurationMacros that the renderer does not define but that are needed by the current configuration for the shader. If this array contains a macro that the renderer already defines an error will be shown and an exception will be thrown.
fullShaderConfigurationOutput. Full shader configuration (might include renderer's configuration) of the found shader.
Returns
Found shader.

◆ getShaderName()

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

Returns unique name of this shader.

Returns
Unique name of this shader.

◆ getShaderType()

ShaderType ne::ShaderPack::getShaderType ( )

Returns type of this shader.

Returns
Shader type.

◆ releaseShaderPackDataFromMemoryIfLoaded()

bool ne::ShaderPack::releaseShaderPackDataFromMemoryIfLoaded ( )

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 at least one shader variant was released from memory, true if all variants were not loaded into memory.

◆ setRendererConfiguration()

void ne::ShaderPack::setRendererConfiguration ( const std::set< ShaderMacro > &  renderConfiguration)
private

Sets renderer's shader configuration, it will be considered in the further calls to getShader.

Warning
If the configuration is changed we will try to release old shader's resources from the memory. Make sure no object is holding shared pointers to old shader (old configuration), otherwise there would be an error printed in the logs.
Parameters
renderConfigurationNew renderer configuration.

Member Data Documentation

◆ mtxInternalResources

std::pair<std::mutex, InternalResources> ne::ShaderPack::mtxInternalResources
private

Used data.

◆ shaderType

ShaderType ne::ShaderPack::shaderType
private

Type of shaders this pack stores.

◆ sShaderName

std::string ne::ShaderPack::sShaderName
private

Initial shader name (without configuration text).


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