NAP
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
BaseMaterialInstance Class Reference

#include <materialinstance.h>

Public Member Functions

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
 

Protected Member Functions

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

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
 

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 > >
 

Description

Base class of MaterialInstance and ComputeMaterialInstance

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

Member Function Documentation

◆ addImageInfo()

void addImageInfo ( const Texture texture,
VkSampler  sampler 
)
protected

◆ getConstantHash()

ShaderConstantHash getConstantHash ( ) const
protected
Returns
the shader constant hash for quick distinction of constant data in material instances

◆ getMaterial() [1/2]

BaseMaterial* getMaterial ( )
Returns
base material that this instance is overriding

◆ getMaterial() [2/2]

const BaseMaterial* getMaterial ( ) const
Returns
base material that this instance is overriding

◆ getOrCreateBuffer() [1/2]

T * getOrCreateBuffer ( const std::string &  name)

Gets or creates a nap::BufferBindingInstance of type T for this material. The binding can be used to set the buffer of type T at runtime. The returned binding is only applicable to this instance. This function will assert if the name of the binding does not match the type that you are trying to create.

material_instance->getOrCreateBinding<nap::BufferBindingVec4Instance>("inBinding");
Parameters
namethe name of the buffer binding as declared in the shader.
Returns
buffer binding that was found or created, nullptr if not available.

◆ getOrCreateBuffer() [2/2]

BufferBindingInstance* getOrCreateBuffer ( const std::string &  name)

Gets or creates a buffer binding instance for this material. The binding can be used to set any buffer of type 'BufferBindingInstance' at runtime. The returned buffer binding is only applicable to this instance.

Parameters
namethe name of the buffer binding as declared in the shader.
Returns
buffer binding that was found or created, nullptr if not available.

◆ getOrCreateBufferInternal()

BufferBindingInstance* getOrCreateBufferInternal ( const std::string &  name)
protected

◆ getOrCreateSampler() [1/4]

T * getOrCreateSampler ( const Sampler resource)

Get or creates a nap::SamplerInstance of type T for this material, which can be set at runtime. The instance is initialized against the provided resource and only applicable to this instance.

Note that the resource type must match the instance type! The function asserts otherwise. In order to change a sampler so that its value is shared among MaterialInstances, use getMaterial().findSampler().

material_instance->getOrCreateSampler<nap::Sampler2DInstance>(samplerResource);
Parameters
resourcethe resource to get the instance for
Returns
the sampler instance of type T, nullptr when sampler declaration doesn't exist

◆ getOrCreateSampler() [2/4]

SamplerInstance* getOrCreateSampler ( const Sampler resource)

Get or creates a nap::SamplerInstance for this material, which can be set at runtime. The instance is initialized against the provided resource and only applicable to this instance.

Note that the resource type must match the instance type! The function asserts otherwise. In order to change a sampler so that its value is shared among MaterialInstances, use getMaterial().findSampler().

Parameters
resourcethe resource to get the instance for
Returns
the sampler instance, nullptr when sampler declaration doesn't exist or of incorrect type

◆ getOrCreateSampler() [3/4]

T * getOrCreateSampler ( const std::string &  name)

Gets or creates a nap::SamplerInstance of type T for this material instance. This means that the sampler returned is only applicable to this instance. In order to change a sampler so that its value is shared among MaterialInstances, use getMaterial().findSampler().

material_instance->getOrCreateSampler<nap::Sampler2DInstance>("inTexture");
Parameters
namethe name of the sampler declared in the shader.
Returns
nap::SamplerInstance of type T, nullptr when sampler declaration doesn't exist or of incorrect type

◆ getOrCreateSampler() [4/4]

SamplerInstance* getOrCreateSampler ( const std::string &  name)

Gets or creates a nap::SamplerInstance for this material, which can be set at runtime. The returned sampler is only applicable to this instance. In order to change a sampler so that its value is shared among MaterialInstances, use getMaterial().findSampler().

Parameters
namethe name of the sampler declared in the shader.
Returns
the sampler instance, nullptr when sampler declaration doesn't exist

◆ getOrCreateSamplerInternal()

SamplerInstance* getOrCreateSamplerInternal ( const std::string &  name,
const Sampler sampler 
)
protected

◆ getOrCreateUniform()

virtual UniformStructInstance* getOrCreateUniform ( const std::string &  name)
virtual

Gets or creates a uniform struct (ubo) for this material instance. This means that the uniform returned is only applicable to this instance. In order to change a uniform so that its value is shared among MaterialInstances, use getMaterial().getUniform().

Parameters
namethe name of the uniform struct (ubo) as declared in the shader.
Returns
uniform that was found or created, nullptr if not available.

◆ getShaderStageConstantMap()

const ShaderStageConstantMap& getShaderStageConstantMap ( ) const
protected
Returns
a map that groups shader constant ids by shader stage. Used for creating vulkan pipelines.

◆ initBuffers()

bool initBuffers ( BaseMaterialInstanceResource resource,
utility::ErrorState errorState 
)
protected

◆ initConstants()

bool initConstants ( BaseMaterialInstanceResource resource,
utility::ErrorState errorState 
)
protected

◆ initInternal()

bool initInternal ( RenderService renderService,
BaseMaterial material,
BaseMaterialInstanceResource instanceResource,
utility::ErrorState errorState 
)
protected

◆ initSamplers()

bool initSamplers ( BaseMaterialInstanceResource resource,
utility::ErrorState errorState 
)
protected

◆ onBufferChanged()

void onBufferChanged ( int  storageBufferIndex,
BufferBindingInstance bindingInstance 
)
protected

◆ onSamplerChanged()

void onSamplerChanged ( int  imageStartIndex,
SamplerInstance samplerInstance,
int  imageArrayIndex 
)
protected

◆ onUniformCreated()

void onUniformCreated ( )
protected

◆ rebuildUBO()

void rebuildUBO ( UniformBufferObject ubo,
UniformStructInstance overrideStruct 
)
protected

◆ update()

virtual const DescriptorSet& update ( )
virtual

This must be called before each draw. It will push the current uniform and sampler data into memory that is accessible for the GPU. A descriptor set will be returned that must be used in VkCmdBindDescriptorSets before the Vulkan draw call is issued.

VkDescriptorSet descriptor_set = mat_instance.update();
vkCmdBindDescriptorSets(cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.mLayout, 0, 1, &descriptor_set, 0, nullptr);
Returns
Descriptor to be used in vkCmdBindDescriptorSets.

◆ updateBuffers()

void updateBuffers ( const DescriptorSet descriptorSet)
protected

◆ updateSamplers()

void updateSamplers ( const DescriptorSet descriptorSet)
protected

Member Data Documentation

◆ mConstantHash

ShaderConstantHash mConstantHash
protected

◆ mDescriptorSetCache

DescriptorSetCache* mDescriptorSetCache
protected

◆ mDevice

VkDevice mDevice = nullptr
protected

◆ mMaterial

BaseMaterial* mMaterial = nullptr
protected

◆ mRenderService

RenderService* mRenderService = nullptr
protected

◆ mResource

BaseMaterialInstanceResource* mResource = nullptr
protected

◆ mSamplerDescriptors

std::vector<VkDescriptorImageInfo> mSamplerDescriptors
protected

◆ mSamplerWriteDescriptorSets

std::vector<VkWriteDescriptorSet> mSamplerWriteDescriptorSets
protected

◆ mShaderStageConstantMap

ShaderStageConstantMap mShaderStageConstantMap
protected

◆ mStorageDescriptors

std::vector<VkDescriptorBufferInfo> mStorageDescriptors
protected

◆ mStorageWriteDescriptorSets

std::vector<VkWriteDescriptorSet> mStorageWriteDescriptorSets
protected

◆ mUniformBufferObjects

std::vector<UniformBufferObject> mUniformBufferObjects
protected

◆ mUniformsCreated

bool mUniformsCreated = false
protected
nap::Sampler2DInstance
Definition: samplerinstance.h:93
nap::TypedBufferBindingNumericInstance
Definition: bufferbindinginstance.h:272