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

#include <HlslShader.h>

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

Public Member Functions

 HlslShader (Renderer *pRenderer, std::filesystem::path pathToCompiledShader, const std::string &sShaderName, ShaderType shaderType, const std::optional< VertexFormat > &vertexFormat, const std::string &sSourceFileHash)
 
 HlslShader (const HlslShader &)=delete
 
HlslShaderoperator= (const HlslShader &)=delete
 
std::variant< ComPtr< IDxcBlob >, ErrorgetCompiledBlob ()
 
std::pair< std::mutex, std::optional< RootSignatureGenerator::CollectedInfo > > * getRootSignatureInfo ()
 
std::string getShaderSourceFileHash () const
 
virtual bool releaseShaderDataFromMemoryIfLoaded () override
 
- Public Member Functions inherited from ne::Shader
 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 std::string_view getVertexShaderModel ()
 
static constexpr std::string_view getPixelShaderModel ()
 
static constexpr std::string_view getComputeShaderModel ()
 
static std::variant< std::string, ErrorgetShaderCompilerVersion ()
 
static CD3DX12_STATIC_SAMPLER_DESC getStaticSamplerDescription (TextureFilteringQuality textureFilteringQuality)
 
static CD3DX12_STATIC_SAMPLER_DESC getStaticComparisonSamplerDescription ()
 
static UINT getStaticSamplerShaderRegisterSpace ()
 
static std::variant< ComPtr< IDxcResult >, std::string, ErrorcompileShaderToBytecode (const ShaderDescription &shaderDescription, const std::optional< std::filesystem::path > &pathToPdb)
 
static std::variant< std::shared_ptr< Shader >, std::string, ErrorcompileShader (Renderer *pRenderer, const std::filesystem::path &cacheDirectory, const std::string &sConfiguration, const ShaderDescription &shaderDescription)
 
- Static Public Member Functions inherited from ne::Shader
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

virtual std::optional< ErrorsaveAdditionalCompilationResultsInfo (ConfigManager &cacheMetadataConfigManager) override
 
virtual std::optional< ErrorcheckCachedAdditionalCompilationResultsInfo (ConfigManager &cacheMetadataConfigManager, std::optional< ShaderCacheInvalidationReason > &cacheInvalidationReason) override
 
- Protected Member Functions inherited from ne::Shader
 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
 

Private Member Functions

std::variant< std::string, ErrorcalculateReflectionFileHash ()
 
std::optional< ErrorloadShaderDataFromDiskIfNotLoaded ()
 

Static Private Member Functions

static std::variant< ComPtr< IDxcBlob >, ErrorreadBlobFromDisk (const std::filesystem::path &pathToFile)
 

Private Attributes

std::pair< std::recursive_mutex, ComPtr< IDxcBlob > > mtxCompiledBytecode
 
std::pair< std::mutex, std::optional< RootSignatureGenerator::CollectedInfo > > mtxRootSignatureInfo
 
std::string sSourceFileHash
 

Static Private Attributes

static UINT iShaderFileCodepage = DXC_CP_ACP
 
static auto sShaderReflectionFileExtension = ".reflection"
 
static const auto sReflectionFileHashKeyName = "reflection_file_hash"
 
static const auto sHlslSectionName = "HLSL"
 
static const UINT iStaticSamplerShaderRegisterSpace = 5
 
static constexpr std::string_view sVertexShaderModel = "vs_6_0"
 
static constexpr std::string_view sPixelShaderModel = "ps_6_0"
 
static constexpr std::string_view sComputeShaderModel = "cs_6_0"
 

Additional Inherited Members

- Static Protected Member Functions inherited from ne::Shader
static void notifyShaderBytecodeLoadedIntoMemory ()
 
static void notifyShaderBytecodeReleasedFromMemory ()
 

Detailed Description

Represents a compiled HLSL shader.

Constructor & Destructor Documentation

◆ HlslShader()

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

Constructor. Used to create shader using cache.

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).
sSourceFileHashShader source file hash, used to tell what shaders were compiled from the same file.

Member Function Documentation

◆ calculateReflectionFileHash()

std::variant< std::string, Error > ne::HlslShader::calculateReflectionFileHash ( )
private

Looks for reflection file next to the compiled shader bytecode file and calculates its hash.

Returns
Error if something went wrong, otherwise hash of the reflection file.

◆ checkCachedAdditionalCompilationResultsInfo()

std::optional< Error > ne::HlslShader::checkCachedAdditionalCompilationResultsInfo ( ConfigManager cacheMetadataConfigManager,
std::optional< ShaderCacheInvalidationReason > &  cacheInvalidationReason 
)
overrideprotectedvirtual

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 from ne::Shader.

◆ compileShader()

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

Compiles a shader.

Parameters
pRendererDirectX renderer.
cacheDirectoryDirectory 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.

◆ compileShaderToBytecode()

std::variant< ComPtr< IDxcResult >, std::string, Error > ne::HlslShader::compileShaderToBytecode ( const ShaderDescription shaderDescription,
const std::optional< std::filesystem::path > &  pathToPdb 
)
static

Compiles the specified shader file to bytecode.

Parameters
shaderDescriptionDescribes the shader and how the shader should be compiled.
pathToPdbPath where the PDB will be saved after compilation (outside of this function) in debug builds.
Returns
One of the three values: results of the compilation, string containing shader compilation error/warning or an internal error.

◆ getCompiledBlob()

std::variant< ComPtr< IDxcBlob >, Error > ne::HlslShader::getCompiledBlob ( )

Loads compiled bytecode from disk and stores it in memory. Subsequent calls to this function will just copy the bytecode pointer (no disk loading will happen).

Returns
Compiled shader blob.

◆ getComputeShaderModel()

static constexpr std::string_view ne::HlslShader::getComputeShaderModel ( )
inlinestaticconstexpr

Returns used compute shader model version.

Returns
Compute shader model version.

◆ getPixelShaderModel()

static constexpr std::string_view ne::HlslShader::getPixelShaderModel ( )
inlinestaticconstexpr

Returns used pixel shader model version.

Returns
Pixel shader model version.

◆ getRootSignatureInfo()

std::pair< std::mutex, std::optional< RootSignatureGenerator::CollectedInfo > > * ne::HlslShader::getRootSignatureInfo ( )

Returns information about root signature that can be used for this shader.

Returns
Empty if root signature information was not collected yet, use getCompiledBlob to collect and load everything, otherwise root signature info.

◆ getShaderCompilerVersion()

std::variant< std::string, Error > ne::HlslShader::getShaderCompilerVersion ( )
static

Returns current version of the HLSL shader compiler.

Returns
Compiler version.

◆ getShaderSourceFileHash()

std::string ne::HlslShader::getShaderSourceFileHash ( ) const

Returns hash of the shader source file that was used to compile the shader.

Returns
Hash of the shader source file.

◆ getStaticComparisonSamplerDescription()

CD3DX12_STATIC_SAMPLER_DESC ne::HlslShader::getStaticComparisonSamplerDescription ( )
static

Returns description of a static comparison sampler.

Returns
Static sampler description.

◆ getStaticSamplerDescription()

CD3DX12_STATIC_SAMPLER_DESC ne::HlslShader::getStaticSamplerDescription ( TextureFilteringQuality  textureFilteringQuality)
static

Returns a static sampler description depending on the specified texture filtering mode.

Parameters
textureFilteringQualityReturned sampler description will use the specified texture filtering.
Returns
Static sampler description.

◆ getStaticSamplerShaderRegisterSpace()

UINT ne::HlslShader::getStaticSamplerShaderRegisterSpace ( )
static

Returns shader register space (in HLSL) that should be used by different sampler types.

Returns
Shader register space.

◆ getVertexShaderModel()

static constexpr std::string_view ne::HlslShader::getVertexShaderModel ( )
inlinestaticconstexpr

Returns used vertex shader model version.

Returns
Vertex shader model version.

◆ loadShaderDataFromDiskIfNotLoaded()

std::optional< Error > ne::HlslShader::loadShaderDataFromDiskIfNotLoaded ( )
private

Loads shader data (bytecode, root signature, etc.) from disk cache if it's not loaded yet.

Returns
Error if something went wrong.

◆ readBlobFromDisk()

std::variant< ComPtr< IDxcBlob >, Error > ne::HlslShader::readBlobFromDisk ( const std::filesystem::path &  pathToFile)
staticprivate

Reads file and creates a new DXC blob using file's content.

Parameters
pathToFilePath to the file to create blob from.
Returns
Error if something went wrong, otherwise created blob.

◆ releaseShaderDataFromMemoryIfLoaded()

bool ne::HlslShader::releaseShaderDataFromMemoryIfLoaded ( )
overridevirtual

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.

Implements ne::Shader.

◆ saveAdditionalCompilationResultsInfo()

std::optional< Error > ne::HlslShader::saveAdditionalCompilationResultsInfo ( ConfigManager cacheMetadataConfigManager)
overrideprotectedvirtual

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 from ne::Shader.

Member Data Documentation

◆ iShaderFileCodepage

UINT ne::HlslShader::iShaderFileCodepage = DXC_CP_ACP
inlinestaticprivate

Shader file encoding.

◆ iStaticSamplerShaderRegisterSpace

const UINT ne::HlslShader::iStaticSamplerShaderRegisterSpace = 5
inlinestaticprivate

Determines which shader register space (in HLSL) should be used by different sampler types.

◆ mtxCompiledBytecode

std::pair<std::recursive_mutex, ComPtr<IDxcBlob> > ne::HlslShader::mtxCompiledBytecode
private

Mutex for read/write operations on compiled bytecode (may be empty if not stored in memory right now).

◆ mtxRootSignatureInfo

std::pair<std::mutex, std::optional<RootSignatureGenerator::CollectedInfo> > ne::HlslShader::mtxRootSignatureInfo
private

Contains information used to create root signature.

Remarks
Might not be calculated yet, see loadShaderDataFromDiskIfNotLoaded for collecting root signature information.

◆ sComputeShaderModel

constexpr std::string_view ne::HlslShader::sComputeShaderModel = "cs_6_0"
staticconstexprprivate

Used compute shader model.

◆ sHlslSectionName

const auto ne::HlslShader::sHlslSectionName = "HLSL"
inlinestaticprivate

Name of the section used to store HLSL specific metadata.

◆ sPixelShaderModel

constexpr std::string_view ne::HlslShader::sPixelShaderModel = "ps_6_0"
staticconstexprprivate

Used pixel shader model.

◆ sReflectionFileHashKeyName

const auto ne::HlslShader::sReflectionFileHashKeyName = "reflection_file_hash"
inlinestaticprivate

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

◆ sShaderReflectionFileExtension

auto ne::HlslShader::sShaderReflectionFileExtension = ".reflection"
inlinestaticprivate

File extension for saving shader reflection data.

◆ sSourceFileHash

std::string ne::HlslShader::sSourceFileHash
private

Shader source file hash, used to tell what shaders were compiled from the same file.

◆ sVertexShaderModel

constexpr std::string_view ne::HlslShader::sVertexShaderModel = "vs_6_0"
staticconstexprprivate

Used vertex shader model.


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