NAP
Public Member Functions | List of all members
MaterialInstance Class Reference

#include <materialinstance.h>

Public Member Functions

bool init (RenderService &renderService, MaterialInstanceResource &resource, utility::ErrorState &errorState)
 
MaterialgetMaterial ()
 
const MaterialgetMaterial () const
 
EBlendMode getBlendMode () const
 
void setBlendMode (EBlendMode blendMode)
 
void setDepthMode (EDepthMode depthMode)
 
EDepthMode getDepthMode () const
 
- Public Member Functions inherited from BaseMaterialInstance
virtual UniformStructInstancegetOrCreateUniform (const std::string &name)
 
template<class T >
T * getOrCreateBuffer (const std::string &name)
 
BufferBindingInstancegetOrCreateBuffer (const std::string &name)
 
template<class T >
T * getOrCreateSampler (const std::string &name)
 
SamplerInstancegetOrCreateSampler (const std::string &name)
 
template<class T >
T * getOrCreateSampler (const Sampler &resource)
 
SamplerInstancegetOrCreateSampler (const Sampler &resource)
 
BaseMaterialgetMaterial ()
 
const BaseMaterialgetMaterial () const
 
virtual const DescriptorSetupdate ()
 
- Public Member Functions inherited from UniformContainer
 UniformContainer ()=default
 
virtual ~UniformContainer ()=default
 
 UniformContainer (const UniformContainer &)=delete
 
UniformContaineroperator= (const UniformContainer &)=delete
 
UniformStructInstancefindUniform (const std::string &name) const
 
SamplerInstancefindSampler (const std::string &name) const
 
BufferBindingInstancefindBinding (const std::string &name) const
 
ShaderConstantInstancefindConstant (const std::string &name) const
 
const SamplerInstanceListgetSamplers () const
 
const BufferBindingInstanceListgetBufferBindings () const
 
const ShaderConstantInstanceListgetConstants () const
 
const UniformStructInstanceListgetUniformStructs () const
 

Additional Inherited Members

- Public Types inherited from UniformContainer
using UniformStructInstanceList = std::vector< std::unique_ptr< UniformStructInstance > >
 
using BufferBindingInstanceList = std::vector< std::unique_ptr< BufferBindingInstance > >
 
using SamplerInstanceList = std::vector< std::unique_ptr< SamplerInstance > >
 
using ShaderConstantInstanceList = std::vector< std::unique_ptr< ShaderConstantInstance > >
 
- Protected Member Functions inherited from BaseMaterialInstance
bool initInternal (RenderService &renderService, BaseMaterial &material, BaseMaterialInstanceResource &instanceResource, utility::ErrorState &errorState)
 
void rebuildUBO (UniformBufferObject &ubo, UniformStructInstance *overrideStruct)
 
void onUniformCreated ()
 
void onSamplerChanged (int imageStartIndex, SamplerInstance &samplerInstance, int imageArrayIndex)
 
void onBufferChanged (int storageBufferIndex, BufferBindingInstance &bindingInstance)
 
void updateBuffers (const DescriptorSet &descriptorSet)
 
bool initBuffers (BaseMaterialInstanceResource &resource, utility::ErrorState &errorState)
 
void updateSamplers (const DescriptorSet &descriptorSet)
 
bool initSamplers (BaseMaterialInstanceResource &resource, utility::ErrorState &errorState)
 
void addImageInfo (const Texture &texture, VkSampler sampler)
 
bool initConstants (BaseMaterialInstanceResource &resource, utility::ErrorState &errorState)
 
BufferBindingInstancegetOrCreateBufferInternal (const std::string &name)
 
SamplerInstancegetOrCreateSamplerInternal (const std::string &name, const Sampler *sampler)
 
const ShaderStageConstantMapgetShaderStageConstantMap () const
 
ShaderConstantHash getConstantHash () const
 
- Protected Member Functions inherited from UniformContainer
UniformStructInstancecreateUniformRootStruct (const ShaderVariableStructDeclaration &declaration, const UniformCreatedCallback &uniformCreatedCallback)
 
BufferBindingInstanceaddBindingInstance (std::unique_ptr< BufferBindingInstance > instance)
 
SamplerInstanceaddSamplerInstance (std::unique_ptr< SamplerInstance > instance)
 
ShaderConstantInstanceaddConstantInstance (std::unique_ptr< ShaderConstantInstance > instance)
 
- Protected Attributes inherited from BaseMaterialInstance
VkDevice mDevice = nullptr
 
RenderServicemRenderService = nullptr
 
BaseMaterialmMaterial = nullptr
 
BaseMaterialInstanceResourcemResource = nullptr
 
DescriptorSetCachemDescriptorSetCache
 
std::vector< UniformBufferObjectmUniformBufferObjects
 
std::vector< VkWriteDescriptorSet > mStorageWriteDescriptorSets
 
std::vector< VkDescriptorBufferInfo > mStorageDescriptors
 
std::vector< VkWriteDescriptorSet > mSamplerWriteDescriptorSets
 
std::vector< VkDescriptorImageInfo > mSamplerDescriptors
 
ShaderStageConstantMap mShaderStageConstantMap
 
ShaderConstantHash mConstantHash
 
bool mUniformsCreated = false
 

Description

To draw an object with a Material, you must use a MaterialInstance. MaterialInstance contains the runtime data for drawing a Material. MaterialInstance is intended to be used as a property in Components. It must be initialized based on a MaterialInstanceResource object to fill its runtime data. init() must be called from the ComponentInstance's init() function. Before drawing, make sure to call update() to update the uniforms and samplers. A descriptorset will be returned that can be used to issue the Vulkan draw call.

Multiple MaterialInstances can share a single Material and a single MaterialInstance can override Material properties on a per-instance basis. This means that you can set uniform or texture data on Material level, which means that, as long as the property isn't overridden, you will set it for all MaterialInstances in one go. If you set a property on MaterialInstance level, you will set it only for that MaterialInstance.

It is also possible to set uniform or texture state on a single MaterialInstance multiple times per frame. When multiple draws are performed with the frame, the state at the point of draw will be used.

Note that there is no implicit synchronization of access to shader resources bound to storage and regular uniforms between render passes. Therefore, it is currently not recommended to write to storage buffers inside vertex and/or fragment shaders over consecutive render passes within a single frame.

Performance note: changing the Depth mode or Blend mode frequently on a single MaterialInstance is not recommended, as it requires a rebuild of the entire GPU pipeline. If changing it per frame is required, consider using multiple MaterialInstance objects and switch between them instead.

Inheritance diagram for MaterialInstance:
[legend]
Collaboration diagram for MaterialInstance:
[legend]

Member Function Documentation

◆ getBlendMode()

EBlendMode getBlendMode ( ) const
Returns
If blend mode was overridden for this material, returns blend mode, otherwise material's blendmode.

◆ getDepthMode()

EDepthMode getDepthMode ( ) const
Returns
If depth mode was overridden for this material, returns depth mode, otherwise material's depthmode.

◆ getMaterial() [1/2]

Material& getMaterial ( )
Returns
material that this instance is overriding.

◆ getMaterial() [2/2]

const Material& getMaterial ( ) const
Returns
material that this instance is overriding

◆ init()

bool init ( RenderService renderService,
MaterialInstanceResource resource,
utility::ErrorState errorState 
)

Initializes all runtime structures for MaterialInstance.

◆ setBlendMode()

void setBlendMode ( EBlendMode  blendMode)

Sets the blend mode that is used when rendering an object with this material. Note that frequently changing the blend mode on the same MaterialInstance requires a rebuild of the underlying GPU pipeline. When frequent changes are required, it is recommended to use multiple MaterialInstance objects instead.

Parameters
blendModethe new blend mode

◆ setDepthMode()

void setDepthMode ( EDepthMode  depthMode)

Sets the depth mode that is used when rendering an object with this material. Note that frequently changing the depth mode on the same MaterialInstance requires a rebuild of the underlying GPU pipeline. When frequent changes are required, it is recommended to use multiple MaterialInstance objects instead.

Parameters
depthModethe new depth mode