Nameless Engine
|
#include <Material.h>
Classes | |
struct | GpuResources |
struct | InternalResources |
struct | MaterialShaderConstants |
Public Member Functions | |
Material () | |
Material (const Material &)=delete | |
Material & | operator= (const Material &)=delete |
void | setEnableTransparency (bool bEnable) |
void | setDiffuseColor (const glm::vec3 &diffuseColor) |
void | setDiffuseTexture (const std::string &sTextureResourcePathRelativeRes) |
void | setSpecularColor (const glm::vec3 &specularColor) |
void | setRoughness (float roughness) |
void | setOpacity (float opacity=1.0F) |
bool | isTransparencyEnabled () |
glm::vec3 | getDiffuseColor () const |
glm::vec3 | getSpecularColor () const |
std::string | getPathToDiffuseTextureResource () |
float | getRoughness () const |
float | getOpacity () const |
std::pair< std::mutex, MeshNodesThatUseThisMaterial > * | getSpawnedMeshNodesThatUseThisMaterial () |
std::string | getMaterialName () const |
bool | isUsingTransparency () const |
Pipeline * | getColorPipeline () const |
Pipeline * | getDepthOnlyPipeline () const |
Pipeline * | getShadowMappingDirectionalSpotPipeline () const |
Pipeline * | getShadowMappingPointPipeline () const |
std::pair< std::recursive_mutex, GpuResources > * | getMaterialGpuResources () |
std::string | getVertexShaderName () const |
std::string | getPixelShaderName () const |
Public Member Functions inherited from ne::Serializable | |
std::optional< Error > | serialize (std::filesystem::path pathToFile, bool bEnableBackup, const std::unordered_map< std::string, std::string > &customAttributes={}) |
std::variant< std::string, Error > | serialize (toml::value &tomlData, const std::string &sEntityId="", const std::unordered_map< std::string, std::string > &customAttributes={}, const std::optional< std::filesystem::path > &optionalPathToFile={}, bool bEnableBackup=false) |
std::variant< std::string, Error > | serialize (toml::value &tomlData, Serializable *pOriginalObject, std::string sEntityId="", const std::unordered_map< std::string, std::string > &customAttributes={}, const std::optional< std::filesystem::path > &optionalPathToFile={}, bool bEnableBackup=false) |
std::optional< std::pair< std::string, std::string > > | getPathDeserializedFromRelativeToRes () const |
Static Public Member Functions | |
static size_t | getCurrentAliveMaterialCount () |
static std::variant< std::unique_ptr< Material >, Error > | create (const std::string &sVertexShaderName, const std::string &sPixelShaderName, bool bUseTransparency, const std::string &sMaterialName="Material") |
Static Public Member Functions inherited from ne::Serializable | |
static std::variant< std::pair< std::set< std::string >, toml::value >, Error > | getIdsFromFile (std::filesystem::path pathToFile) |
static std::optional< Error > | serializeMultiple (std::filesystem::path pathToFile, std::vector< SerializableObjectInformation > vObjects, bool bEnableBackup) |
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type> requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>) | |
static std::variant< SmartPointer, Error > | deserialize (const std::filesystem::path &pathToFile) |
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type> requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>) | |
static std::variant< SmartPointer, Error > | deserialize (const std::filesystem::path &pathToFile, std::unordered_map< std::string, std::string > &customAttributes) |
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type> requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>) | |
static std::variant< SmartPointer, Error > | deserialize (std::filesystem::path pathToFile, std::unordered_map< std::string, std::string > &customAttributes, const std::string &sEntityId) |
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type> requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>) | |
static std::variant< SmartPointer, Error > | deserialize (const std::filesystem::path &pathToFile, const std::string &sEntityId) |
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type> requires std::same_as<SmartPointer, sgc::GcPtr<Serializable>> || std::same_as<SmartPointer, std::unique_ptr<Serializable>> | |
static std::variant< std::vector< DeserializedObjectInformation< SmartPointer > >, Error > | deserializeMultiple (std::filesystem::path pathToFile) |
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type> requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>) | |
static std::variant< SmartPointer, Error > | deserialize (const toml::value &tomlData, std::unordered_map< std::string, std::string > &customAttributes, std::string sEntityId="", const std::optional< std::filesystem::path > &optionalPathToFile={}) |
Protected Member Functions | |
virtual void | onAfterDeserialized () override |
virtual void | onAfterDeserialized () |
Private Member Functions | |
Material (const std::string &sVertexShaderName, const std::string &sPixelShaderName, bool bUseTransparency, PipelineManager *pPipelineManager, const std::string &sMaterialName="Material") | |
void | onMeshNodeSpawning (MeshNode *pMeshNode, const std::pair< GpuResource *, unsigned int > &indexBufferToDisplay) |
void | onSpawnedMeshNodeStartedUsingMaterial (MeshNode *pMeshNode, const std::pair< GpuResource *, unsigned int > &indexBufferToDisplay) |
void | onSpawnedMeshNodeRecreatedIndexBuffer (MeshNode *pMeshNode, const std::pair< GpuResource *, unsigned int > &deletedIndexBuffer, const std::pair< GpuResource *, unsigned int > &newIndexBuffer) |
void | onSpawnedMeshNodeChangedVisibility (MeshNode *pMeshNode, bool bOldVisibility) |
void | onSpawnedMeshNodeStoppedUsingMaterial (MeshNode *pMeshNode, const std::pair< GpuResource *, unsigned int > &indexBufferDisplayed) |
void | onMeshNodeDespawning (MeshNode *pMeshNode, const std::pair< GpuResource *, unsigned int > &indexBufferDisplayed) |
std::optional< Error > | initializePipelines () |
void | resetPipelines () |
void | allocateShaderResources () |
void | deallocateShaderResources () |
void | setShaderCpuWriteResourceBinding (const std::string &sShaderResourceName, size_t iResourceSizeInBytes, const std::function< void *()> &onStartedUpdatingResource, const std::function< void()> &onFinishedUpdatingResource) |
void | setShaderTextureResourceBinding (const std::string &sShaderResourceName, const std::string &sPathToTextureResourceRelativeRes) |
void | markShaderCpuWriteResourceAsNeedsUpdate (const std::string &sShaderResourceName) |
void | updateToNewPipeline () |
void * | onStartUpdatingShaderMeshConstants () |
void | onFinishedUpdatingShaderMeshConstants () |
std::set< ShaderMacro > | getVertexShaderMacrosForCurrentState () |
std::set< ShaderMacro > | getPixelShaderMacrosForCurrentState () |
Static Private Member Functions | |
static std::variant< PipelineManager *, Error > | getPipelineManagerForNewMaterial (const std::string &sVertexShaderName, const std::string &sPixelShaderName) |
Private Attributes | |
std::pair< std::mutex, MeshNodesThatUseThisMaterial > | mtxSpawnedMeshNodesThatUseThisMaterial |
std::pair< std::recursive_mutex, InternalResources > | mtxInternalResources |
std::pair< std::recursive_mutex, GpuResources > | mtxGpuResources |
std::pair< std::recursive_mutex, MaterialShaderConstants > | mtxShaderMaterialDataConstants |
PipelineManager * | pPipelineManager = nullptr |
std::string | sVertexShaderName |
std::string | sPixelShaderName |
std::string | sMaterialName |
std::string | sDiffuseTexturePathRelativeRes |
glm::vec3 | diffuseColor = glm::vec3(1.0F, 1.0F, 1.0F) |
glm::vec3 | specularColor = glm::vec3(1.0F, 1.0F, 1.0F) |
float | roughness = 0.7F |
float | opacity = 0.6F |
bool | bUseTransparency = false |
bool | bIsShaderResourcesAllocated = false |
Static Private Attributes | |
static const auto | sMaterialShaderBufferName = "materialData" |
static const auto | sMaterialShaderDiffuseTextureName = "diffuseTextures" |
Friends | |
class | MeshNode |
Defines visual aspects of a mesh.
ne::Material::Material | ( | ) |
Creates uninitialized material, only used for deserialization, instead use create.
|
private |
Creates a new material with the specified name.
sVertexShaderName | Name of the vertex shader that this material is using. |
sPixelShaderName | Name of the pixel shader that this material is using. |
bUseTransparency | Whether this material will use transparency or not. |
pPipelineManager | Pipeline manager that the renderer owns. |
sMaterialName | Name of this material. |
|
private |
Creates shader resources such as material's constant buffer.
|
static |
Creates a new material that uses the specified shaders.
sVertexShaderName | Name of the compiled vertex shader (see ShaderManager::compileShaders) to use. |
sPixelShaderName | Name of the compiled pixel shader (see ShaderManager::compileShaders) to use. |
bUseTransparency | Whether this material should enable transparency after being created or not (see setEnableTransparency). |
sMaterialName | Name of this material. |
|
private |
Deallocates shader resources after allocateShaderResources was called.
Pipeline * ne::Material::getColorPipeline | ( | ) | const |
Returns pipeline with pixel/fragment shader enabled (this pipeline is considered to be the main pipeline while others might be optional).
nullptr
if pipeline was not initialized yet, otherwise used pipeline.
|
static |
Returns total amount of currently alive material objects.
Pipeline * ne::Material::getDepthOnlyPipeline | ( | ) | const |
Returns pipeline that only has vertex shader (used for depth only passes).
nullptr
if pipeline was not initialized yet or if isUsingTransparency is enabled, otherwise used pipeline. glm::vec3 ne::Material::getDiffuseColor | ( | ) | const |
Returns fill color of this material.
|
inline |
Returns GPU resources that this material uses.
std::string ne::Material::getMaterialName | ( | ) | const |
Returns material name.
float ne::Material::getOpacity | ( | ) | const |
Returns opacity of this material.
std::string ne::Material::getPathToDiffuseTextureResource | ( | ) |
Returns path to a file/directory that stores currently used diffuse texture of this material.
res
directory.
|
staticprivate |
Returns pipeline manager.
sVertexShaderName | Name of the vertex shader that the material is using. |
sPixelShaderName | Name of the pixel shader that the material is using. |
|
private |
Analyzes mtxInternalResources and returns pixel/fragment shader macros that should be enabled to support the material's features.
std::string ne::Material::getPixelShaderName | ( | ) | const |
Returns name of the pixel shader that this material uses.
float ne::Material::getRoughness | ( | ) | const |
Returns roughness of this material.
Pipeline * ne::Material::getShadowMappingDirectionalSpotPipeline | ( | ) | const |
Returns pipeline that only has vertex shader and depth bias enabled, used for shadow passes of directional and spot lights.
nullptr
if pipeline was not initialized yet or if isUsingTransparency is enabled, otherwise used pipeline. Pipeline * ne::Material::getShadowMappingPointPipeline | ( | ) | const |
Returns pipeline that only has vertex shader and depth bias enabled, used for shadow passes of point lights.
nullptr
if pipeline was not initialized yet or if isUsingTransparency is enabled, otherwise used pipeline. std::pair< std::mutex, MeshNodesThatUseThisMaterial > * ne::Material::getSpawnedMeshNodesThatUseThisMaterial | ( | ) |
Returns array of mesh nodes that currently use this material. Must be used with mutex.
glm::vec3 ne::Material::getSpecularColor | ( | ) | const |
Returns reflected color of this material.
|
private |
Analyzes mtxInternalResources and returns vertex shader macros that should be enabled to support the material's features.
std::string ne::Material::getVertexShaderName | ( | ) | const |
Returns name of the vertex shader that this material uses.
|
private |
Initializes pipelines that the material needs.
bool ne::Material::isTransparencyEnabled | ( | ) |
Tells whether transparency on this material is enabled or not.
true
if enabled, false
otherwise. bool ne::Material::isUsingTransparency | ( | ) | const |
Tells whether this material uses transparency or not.
|
private |
Looks for binding created using setShaderCpuWriteResourceBinding and notifies the engine that there is new (updated) data for shader CPU write resource to copy to the GPU to be used by shaders.
sShaderResourceName | Name of the shader CPU write resource (should be exactly the same as the resource name written in the shader file we are referencing). |
|
overrideprotectedvirtual |
Called after the object was successfully deserialized. Used to execute post-deserialization logic.
Reimplemented from ne::Serializable.
|
private |
Called after finished copying data from mtxShaderMaterialDataConstants.
|
private |
Called from MeshNode when a spawned mesh node that uses this material is being despawned.
pMeshNode | Spawned mesh node that is being despawned. |
indexBufferDisplayed | Index buffer that this material was displaying. |
|
private |
Called from MeshNode when a mesh node that uses this material is being spawned.
pMeshNode | Mesh node that is currently being spawned. |
indexBufferToDisplay | Index buffer that this material should display. |
|
private |
Called from MeshNode when a spawned mesh node changed its visibility.
pMeshNode | Spawned mesh node that is using this material. |
bOldVisibility | Old visibility of the mesh. |
|
private |
Called from MeshNode when a spawned mesh node re-created some index buffer and now wants to notify the material about it.
pMeshNode | Spawned mesh node that is using this material. |
deletedIndexBuffer | Index buffer that was deleted. |
newIndexBuffer | Index buffer that this material should display now. |
|
private |
Called from MeshNode when a spawned mesh node changed its material and started using this material now.
pMeshNode | Spawned mesh node that is using this material. |
indexBufferToDisplay | Index buffer that this material should display. |
|
private |
Called from MeshNode when a spawned mesh node changed its material and now no longer using this material.
pMeshNode | Spawned mesh node that stopped using this material. |
indexBufferDisplayed | Index buffer that this material was displaying. |
|
private |
Called to copy data from mtxShaderMaterialDataConstants.
|
private |
Clears (sets to nullptr
) all used pipelines.
void ne::Material::setDiffuseColor | ( | const glm::vec3 & | diffuseColor | ) |
Sets material's fill color.
diffuseColor | Color in the RGB format. |
void ne::Material::setDiffuseTexture | ( | const std::string & | sTextureResourcePathRelativeRes | ) |
Sets material's diffuse texture.
Example:
sTextureResourcePathRelativeRes | Path to a texture resource (file/directory) relative to res directory that this material should now use. Specify empty string to clear the current diffuse texture (if any is set) and don't use diffuse texture at all. |
void ne::Material::setEnableTransparency | ( | bool | bEnable | ) |
Enables/disables transparency.
bEnable | Whether to enable transparency or not. |
void ne::Material::setOpacity | ( | float | opacity = 1.0F | ) |
Sets material's opacity.
opacity | Value in range [0.0F; 1.0F], will be clamped if outside of this range. |
void ne::Material::setRoughness | ( | float | roughness | ) |
Sets factor that defines how much specular light will be reflected (i.e. how rough or smooth the surface is).
roughness | Value in range [0.0F; 1.0F], will be clamped if outside of this range. |
|
private |
Setups callbacks for a shader resource (buffer or a texture from the shader code) with CPU write access to copy the data from the CPU to the GPU to be used by the shaders.
sShaderResourceName | Name of the resource we are referencing (should be exactly the same as the resource name written in the shader file we are referencing). |
iResourceSizeInBytes | Size of the data that this resource will contain. Note that the specified size will most likely be padded (changed) to be a multiple of 256 because of the hardware requirement for shader constant buffers. |
onStartedUpdatingResource | Function that will be called when the engine has started copying data to the GPU. Function returns pointer to new (updated) data of the specified resource that will be copied to the GPU. |
onFinishedUpdatingResource | Function that will be called when the engine has finished copying resource data to the GPU (usually used for unlocking mutexes). |
|
private |
Setups a shader resource binding that references a texture that will be used in shaders when this material is rendered.
sShaderResourceName | Name of the resource we are referencing (should be exactly the same as the resource name written in the shader file we are referencing). |
sPathToTextureResourceRelativeRes | Path to the directory with texture resource to use. |
void ne::Material::setSpecularColor | ( | const glm::vec3 & | specularColor | ) |
Sets material's reflected color.
specularColor | Color in the RGB format. |
|
private |
Releases all shader resources, requests a new pipeline according to currently defined shader macros, allocates all shader resources and notifies all mesh nodes that use this material.
|
private |
Whether allocateShaderResources was called or not.
|
private |
Whether this material will use transparency or not.
|
private |
Fill color.
|
private |
Stores GPU resources used by this material.
|
private |
Internal data.
|
private |
Stores data for constant buffer used by shaders.
|
private |
Array of spawned mesh nodes that use this material. Must be used with mutex.
|
private |
Opacity in range [0.0; 1.0].
|
private |
Do not delete (free) this pointer. Pipeline manager that the renderer owns.
|
private |
Defines how much specular light will be reflected. Value in range [0.0F; 1.0F].
|
private |
Empty if diffuse texture is not used, otherwise path to used diffuse texture relative to res
directory.
|
private |
Name of this material.
|
inlinestaticprivate |
Name of the buffer used to store material data in shaders.
|
inlinestaticprivate |
Name of the resource used to store diffuse textures in shaders.
|
private |
Reflected color.
|
private |
Name of the pixel shader that this material is using.
|
private |
Name of the vertex shader that this material is using.