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

#include <ShaderLightArray.h>

Classes

struct  Resources
 

Public Member Functions

 ~ShaderLightArray ()
 
std::variant< std::unique_ptr< ShaderLightArraySlot >, ErrorreserveNewSlot (Node *pSpawnedOwnerLightNode, size_t iDataSizeInBytes, const std::function< void *()> &startUpdateCallback, const std::function< void()> &finishUpdateCallback)
 
std::pair< std::recursive_mutex, Resources > * getInternalResources ()
 
std::string getShaderResourceName () const
 

Static Public Member Functions

static std::unique_ptr< ShaderLightArraycreate (Renderer *pRenderer, const std::string &sShaderLightResourceName, const std::function< void(size_t)> &onSizeChanged, const std::optional< std::pair< std::function< void(size_t)>, std::string > > &optionalOnLightsInCameraFrustumCulled)
 

Private Member Functions

 ShaderLightArray (Renderer *pRenderer, const std::string &sShaderLightResourceName, const std::function< void(size_t)> &onSizeChanged, const std::optional< std::function< void(size_t)> > &optionalCallbackOnLightsInCameraFrustumCulled, const std::string &sIndicesLightsInFrustumShaderResourceName="")
 
void onLightsInCameraFrustumCulled (size_t iCurrentFrameResourceIndex)
 
std::optional< ErrorrecreateArray (bool bIsInitialization=false)
 
void updateSlotsMarkedAsNeedsUpdate (size_t iCurrentFrameResourceIndex)
 
std::optional< ErrorupdateBindingsInAllPipelines ()
 
std::optional< ErrorupdatePipelineBinding (Pipeline *pPipeline)
 
void freeSlot (ShaderLightArraySlot *pSlot)
 
void markSlotAsNeedsUpdate (ShaderLightArraySlot *pSlot)
 

Private Attributes

std::pair< std::recursive_mutex, ResourcesmtxResources
 
RendererpRenderer = nullptr
 
size_t iElementSizeInBytes = 0
 
const std::optional< std::function< void(size_t)> > optionalCallbackOnLightsInCameraFrustumCulled
 
const std::function< void(size_t)> onSizeChanged
 
const std::string sShaderLightResourceName
 

Friends

class ShaderLightArraySlot
 
class LightingShaderResourceManager
 
class Renderer
 

Detailed Description

Manages arrays (defined in shaders) related to lighting and allows modifying the array data from the CPU side.

Constructor & Destructor Documentation

◆ ~ShaderLightArray()

ne::ShaderLightArray::~ShaderLightArray ( )

Makes sure there are no active slots.

◆ ShaderLightArray()

ne::ShaderLightArray::ShaderLightArray ( Renderer pRenderer,
const std::string &  sShaderLightResourceName,
const std::function< void(size_t)> &  onSizeChanged,
const std::optional< std::function< void(size_t)> > &  optionalCallbackOnLightsInCameraFrustumCulled,
const std::string &  sIndicesLightsInFrustumShaderResourceName = "" 
)
private

Creates a new array.

Warning
Only used internally, prefer to use create.
Parameters
pRendererUsed renderer.
sShaderLightResourceNameName of the resource (specified in shader code) that this array should bind to.
onSizeChangedCallback that will be called after array's size changed with the current array size passed as the only argument.
optionalCallbackOnLightsInCameraFrustumCulledIf specified will be called after array of indices to lights in camera frustum changed (indices changed) with the current frame resource index as the only argument, otherwise (if empty) GPU resources for such array will not be created and this callback will never be called.
sIndicesLightsInFrustumShaderResourceNameIf callback for culled lights in camera frustum is specified stores name of the shader resource used for the array of indices of non-culled lights.

Member Function Documentation

◆ create()

std::unique_ptr< ShaderLightArray > ne::ShaderLightArray::create ( Renderer pRenderer,
const std::string &  sShaderLightResourceName,
const std::function< void(size_t)> &  onSizeChanged,
const std::optional< std::pair< std::function< void(size_t)>, std::string > > &  optionalOnLightsInCameraFrustumCulled 
)
static

Creates a new array.

Parameters
pRendererUsed renderer.
sShaderLightResourceNameName of the resource (specified in shader code) that this array should bind to.
onSizeChangedCallback that will be called after array's size changed with the current array size passed as the only argument.
optionalOnLightsInCameraFrustumCulledA pair of callback and shader resource name that are used for array that stores indices of light sources in camera's frustum. If specified the callback will be called after array of indices to lights in camera frustum changed (indices changed) with the current frame resource index as the only argument, otherwise (if empty) GPU resources for such array will not be created and this callback will never be called.
Returns
Created array.

◆ freeSlot()

void ne::ShaderLightArray::freeSlot ( ShaderLightArraySlot pSlot)
private

Called by slot-objects to notify the array that a slot is no longer used.

Parameters
pSlotSlot that is being destroyed.

◆ getInternalResources()

std::pair< std::recursive_mutex, ShaderLightArray::Resources > * ne::ShaderLightArray::getInternalResources ( )

Returns internal resources of this array.

Remarks
Generally used for tests (read-only), you should not modify them.
Returns
Internal resources.

◆ getShaderResourceName()

std::string ne::ShaderLightArray::getShaderResourceName ( ) const

Returns name of the resource (specified in shader code) that this array is binded to.

Returns
Shader resource name.

◆ markSlotAsNeedsUpdate()

void ne::ShaderLightArray::markSlotAsNeedsUpdate ( ShaderLightArraySlot pSlot)
private

Queues the specified slot's data to be updated later.

Parameters
pSlotSlot to update later.

◆ onLightsInCameraFrustumCulled()

void ne::ShaderLightArray::onLightsInCameraFrustumCulled ( size_t  iCurrentFrameResourceIndex)
private

Called after the renderer culls lights (so that indices of lights sources in camera's frustum change) to copy the new (modified) data to the GPU.

Parameters
iCurrentFrameResourceIndexIndex of the frame resource that will be used to submit the next frame.

◆ recreateArray()

std::optional< Error > ne::ShaderLightArray::recreateArray ( bool  bIsInitialization = false)
private

(Re)creates GPU resources to hold the current number of active slots and updates all previously existing slots.

Parameters
bIsInitializationtrue if we are creating resources for the first time and there are no active slots at the moment but since we need valid (non nullptr resources) we will create resources that will hold 1 slot, false otherwise.
Returns
Error if something went wrong.

◆ reserveNewSlot()

std::variant< std::unique_ptr< ShaderLightArraySlot >, Error > ne::ShaderLightArray::reserveNewSlot ( Node pSpawnedOwnerLightNode,
size_t  iDataSizeInBytes,
const std::function< void *()> &  startUpdateCallback,
const std::function< void()> &  finishUpdateCallback 
)

Reserves a new slot in the array to store some data.

Remarks
While you hold the returned slot-object (and while it's not destroyed), it can call update callbacks at any time.
Update callbacks will be called inside of this function to copy the initial data.
If you mark your slot as "needs update" callbacks may be called multiple times (this is perfectly fine, just don't rely on your callbacks being called only once).
Parameters
pSpawnedOwnerLightNodeSpawned light node that requests the slot. Used for light culling.
iDataSizeInBytesSize of the data that you want to store in the slot in bytes.
startUpdateCallbackCallback that will be called after you mark your slot as "needs update" when the engine is ready to copy the data to the GPU. You must return a pointer which data will be copied.
finishUpdateCallbackCallback that will be called after the copying of your new data is finished.
Returns
Error if something went wrong, otherwise reserved slot.

◆ updateBindingsInAllPipelines()

std::optional< Error > ne::ShaderLightArray::updateBindingsInAllPipelines ( )
private

Binds the underlying GPU resource to descriptors of pipelines that use this array in shaders.

Remarks
Does nothing if DirectX renderer is used.
Returns
Error if something went wrong.

◆ updatePipelineBinding()

std::optional< Error > ne::ShaderLightArray::updatePipelineBinding ( Pipeline pPipeline)
private

Binds the underlying GPU resource to the specified pipeline's descriptor (if this pipeline's shaders use this array, otherwise return empty).

Remarks
Does nothing if DirectX renderer is used.
Parameters
pPipelinePipeline to bind to / rebind to.
Returns
Error if something went wrong.

◆ updateSlotsMarkedAsNeedsUpdate()

void ne::ShaderLightArray::updateSlotsMarkedAsNeedsUpdate ( size_t  iCurrentFrameResourceIndex)
private

Goes through all slots that are marked as "needs update" and copies their new data to the GPU resource.

Parameters
iCurrentFrameResourceIndexIndex of the frame resource that will be used to submit the next frame.

Member Data Documentation

◆ iElementSizeInBytes

size_t ne::ShaderLightArray::iElementSizeInBytes = 0
private

Size of one array element in bytes.

◆ mtxResources

std::pair<std::recursive_mutex, Resources> ne::ShaderLightArray::mtxResources
private

Internal data.

◆ onSizeChanged

const std::function<void(size_t)> ne::ShaderLightArray::onSizeChanged
private

Callback that will be called after array's size changed with the current array size passed as the only argument.

◆ optionalCallbackOnLightsInCameraFrustumCulled

const std::optional<std::function<void(size_t)> > ne::ShaderLightArray::optionalCallbackOnLightsInCameraFrustumCulled
private

If specified will be called after array of indices to lights in camera frustum changed (indices changed) with the current frame resource index as the only argument, otherwise (if empty) GPU resources for such array will not be created and this callback will never be called.

◆ pRenderer

Renderer* ne::ShaderLightArray::pRenderer = nullptr
private

Used renderer.

◆ sShaderLightResourceName

const std::string ne::ShaderLightArray::sShaderLightResourceName
private

Name of the resource (specified in shader code) that this array should bind to.


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