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

#include <ShaderCpuWriteResourceBinding.h>

Inheritance diagram for ne::ShaderCpuWriteResourceBinding:
ne::ShaderResourceBindingBase

Public Member Functions

void copyResourceIndexToShaderConstants (PipelineShaderConstantsManager *pShaderConstantsManager, Pipeline *pUsedPipeline, size_t iCurrentFrameResourceIndex)
 
size_t getResourceDataSizeInBytes () const
 
virtual std::optional< ErrorchangeUsedPipelines (const std::unordered_set< Pipeline * > &pipelinesToUse) override
 
- Public Member Functions inherited from ne::ShaderResourceBindingBase
virtual std::optional< ErrorchangeUsedPipelines (const std::unordered_set< Pipeline * > &pipelinesToUse)=0
 
std::string getShaderResourceName () const
 

Protected Member Functions

 ShaderCpuWriteResourceBinding (const std::string &sShaderResourceName, size_t iResourceDataSizeInBytes, const std::function< void *()> &onStartedUpdatingResource, const std::function< void()> &onFinishedUpdatingResource, const std::unordered_map< Pipeline *, size_t > &uintShaderConstantOffsets)
 
virtual std::optional< ErroronAfterAllPipelinesRefreshedResources () override
 
- Protected Member Functions inherited from ne::ShaderResourceBindingBase
 ShaderResourceBindingBase (const std::string &sShaderResourceName)
 
virtual std::optional< ErroronAfterAllPipelinesRefreshedResources ()=0
 

Static Protected Member Functions

static std::variant< std::unique_ptr< ShaderCpuWriteResourceBinding >, Errorcreate (const std::string &sShaderResourceName, const std::string &sResourceAdditionalInfo, size_t iResourceSizeInBytes, const std::unordered_set< Pipeline * > &pipelinesToUse, const std::function< void *()> &onStartedUpdatingResource, const std::function< void()> &onFinishedUpdatingResource)
 
static std::variant< std::unordered_map< Pipeline *, size_t >, ErrorgetUintShaderConstantOffsetsFromPipelines (const std::unordered_set< Pipeline * > &pipelines, const std::string &sFieldName)
 

Protected Attributes

std::pair< std::recursive_mutex, std::unordered_map< Pipeline *, size_t > > mtxUintShaderConstantOffsets
 
std::array< std::unique_ptr< DynamicCpuWriteShaderResourceArraySlot >, FrameResourceManager::getFrameResourceCount()> vResourceData
 

Private Member Functions

void updateResource (size_t iCurrentFrameResourceIndex)
 

Private Attributes

const std::function< void *()> onStartedUpdatingResource
 
const std::function< void()> onFinishedUpdatingResource
 
const size_t iResourceDataSizeInBytes = 0
 

Friends

class ShaderCpuWriteResourceBindingManager
 

Detailed Description

References a single (non-array) shader resource (that is written in a shader file) that has CPU write access available (can be updated from the CPU side).

Constructor & Destructor Documentation

◆ ShaderCpuWriteResourceBinding()

ne::ShaderCpuWriteResourceBinding::ShaderCpuWriteResourceBinding ( const std::string &  sShaderResourceName,
size_t  iResourceDataSizeInBytes,
const std::function< void *()> &  onStartedUpdatingResource,
const std::function< void()> &  onFinishedUpdatingResource,
const std::unordered_map< Pipeline *, size_t > &  uintShaderConstantOffsets 
)
protected

Constructs a partially initialized object.

Remarks
Only used internally, instead use create.
Parameters
sShaderResourceNameName of the resource we are referencing (should be exactly the same as the resource name written in the shader file we are referencing).
iResourceDataSizeInBytesSize (in bytes) of the data that this resource will contain.
onStartedUpdatingResourceFunction that will be called when started updating resource data. Function returns pointer to data of the specified resource data size that needs to be copied into the resource.
onFinishedUpdatingResourceFunction that will be called when finished updating (usually used for unlocking resource data mutex).
uintShaderConstantOffsetsOffsets of root/push constants (per-pipeline) to copy an index into array to.

Member Function Documentation

◆ changeUsedPipelines()

std::optional< Error > ne::ShaderCpuWriteResourceBinding::changeUsedPipelines ( const std::unordered_set< Pipeline * > &  pipelinesToUse)
overridevirtual

Called to make the resource to discard currently used pipelines and bind/reference other pipelines.

Warning
Expects that the caller is using some mutex to protect this shader resource from being used in the draw function while this function is not finished (i.e. make sure the CPU will not queue a new frame while this function is not finished).
Remarks
For example, for this function can be called from a mesh node that changed its geometry and thus added/removed some material slots, or if some material that mesh node is using changed its pipeline.
Parameters
pipelinesToUsePipelines to use instead of the current ones.
Returns
Error if something went wrong.

Implements ne::ShaderResourceBindingBase.

◆ copyResourceIndexToShaderConstants()

void ne::ShaderCpuWriteResourceBinding::copyResourceIndexToShaderConstants ( PipelineShaderConstantsManager pShaderConstantsManager,
Pipeline pUsedPipeline,
size_t  iCurrentFrameResourceIndex 
)
inline

Copies resource index (into shader arrays) to a root/push constant.

Parameters
pShaderConstantsManagerShader constants manager.
pUsedPipelineCurrent pipeline.
iCurrentFrameResourceIndexIndex of the current frame resource.

◆ create()

std::variant< std::unique_ptr< ShaderCpuWriteResourceBinding >, Error > ne::ShaderCpuWriteResourceBinding::create ( const std::string &  sShaderResourceName,
const std::string &  sResourceAdditionalInfo,
size_t  iResourceSizeInBytes,
const std::unordered_set< Pipeline * > &  pipelinesToUse,
const std::function< void *()> &  onStartedUpdatingResource,
const std::function< void()> &  onFinishedUpdatingResource 
)
staticprotected

Creates a new shader CPU-write resource.

Parameters
sShaderResourceNameName of the resource we are referencing (should be exactly the same as the resource name written in the shader file we are referencing).
sResourceAdditionalInfoAdditional text that we will append to created resource name (used for logging).
iResourceSizeInBytesSize of the data that this resource will contain.
pipelinesToUsePipelines that use shader/parameters we are referencing.
onStartedUpdatingResourceFunction that will be called when started updating resource data. Function returns pointer to data of the specified resource data size that needs to be copied into the resource.
onFinishedUpdatingResourceFunction that will be called when finished updating (usually used for unlocking resource data mutex).
Returns
Error if something went wrong, otherwise created shader resource.

◆ getResourceDataSizeInBytes()

size_t ne::ShaderCpuWriteResourceBinding::getResourceDataSizeInBytes ( ) const
inline

Returns size of the resource data.

Returns
Size in bytes.

◆ getUintShaderConstantOffsetsFromPipelines()

std::variant< std::unordered_map< Pipeline *, size_t >, Error > ne::ShaderCpuWriteResourceBinding::getUintShaderConstantOffsetsFromPipelines ( const std::unordered_set< Pipeline * > &  pipelines,
const std::string &  sFieldName 
)
staticprotected

Looks for root/push constants (field) named after the shader resource in the specified pipelines and returns offsets of such fields.

Parameters
pipelinesPipelines to scan.
sFieldNameName of the root/push constant field to look for.
Returns
Error if something went wrong, otherwise pairs of "pipeline" - "offset of field".

◆ onAfterAllPipelinesRefreshedResources()

std::optional< Error > ne::ShaderCpuWriteResourceBinding::onAfterAllPipelinesRefreshedResources ( )
overrideprotectedvirtual

Called from pipeline manager to notify that all pipelines released their internal resources and now restored them so their internal resources (for example push constants) might be different now and shader resource now needs to check that everything that it needs is still there and possibly re-bind to pipeline's descriptors since these might have been also re-created.

Returns
Error if something went wrong.

Implements ne::ShaderResourceBindingBase.

◆ updateResource()

void ne::ShaderCpuWriteResourceBinding::updateResource ( size_t  iCurrentFrameResourceIndex)
inlineprivate

Copies up to date data to the GPU resource of the specified frame resource.

Remarks
Called by shader resource manager.
Should only be called when resource actually needs an update, otherwise you would cause a useless copy operations.
Parameters
iCurrentFrameResourceIndexIndex of currently used frame resource.

Member Data Documentation

◆ iResourceDataSizeInBytes

const size_t ne::ShaderCpuWriteResourceBinding::iResourceDataSizeInBytes = 0
private

Size (in bytes) of the data that the resource contains.

◆ mtxUintShaderConstantOffsets

std::pair<std::recursive_mutex, std::unordered_map<Pipeline*, size_t> > ne::ShaderCpuWriteResourceBinding::mtxUintShaderConstantOffsets
protected

Offsets of root/push constants (per-pipeline) to copy an index into array to.

◆ onFinishedUpdatingResource

const std::function<void()> ne::ShaderCpuWriteResourceBinding::onFinishedUpdatingResource
private

Function to call when finished updating (usually used for unlocking resource data mutex).

◆ onStartedUpdatingResource

const std::function<void*()> ne::ShaderCpuWriteResourceBinding::onStartedUpdatingResource
private

Function used to update resource data. Returns pointer to data of size iResourceDataSizeInBytes that needs to be copied into resource data storage (GPU resource).

◆ vResourceData

std::array< std::unique_ptr<DynamicCpuWriteShaderResourceArraySlot>, FrameResourceManager::getFrameResourceCount()> ne::ShaderCpuWriteResourceBinding::vResourceData
protected

Stores data for shaders to use (per frame resource). Index (from array start) will be copied to push constant so that shaders can index into the array and access the data.


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