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

#include <DynamicCpuWriteShaderResourceArray.h>

Classes

struct  InternalResources
 

Public Member Functions

 DynamicCpuWriteShaderResourceArray (const DynamicCpuWriteShaderResourceArray &)=delete
 
DynamicCpuWriteShaderResourceArrayoperator= (const DynamicCpuWriteShaderResourceArray &)=delete
 
size_t getSize ()
 
size_t getCapacity ()
 
size_t getSizeInBytes ()
 
size_t getElementSize () const
 
size_t getCapacityStepSize () const
 
std::string_view getHandledShaderResourceName () const
 
std::pair< std::recursive_mutex, InternalResources > * getInternalResources ()
 

Private Member Functions

 DynamicCpuWriteShaderResourceArray (GpuResourceManager *pResourceManager, const std::string &sHandledShaderResourceName, size_t iElementSizeInBytes, size_t iCapacityStepSize)
 
std::variant< std::unique_ptr< DynamicCpuWriteShaderResourceArraySlot >, Errorinsert (ShaderCpuWriteResourceBinding *pShaderResource)
 
void markSlotAsNoLongerBeingUsed (DynamicCpuWriteShaderResourceArraySlot *pSlot)
 
void updateSlotData (DynamicCpuWriteShaderResourceArraySlot *pSlot, void *pData)
 
std::optional< ErrorcreateArray (size_t iCapacity)
 
std::optional< ErrorexpandArray ()
 
std::optional< ErrorshrinkArray ()
 

Static Private Member Functions

static std::variant< std::unique_ptr< DynamicCpuWriteShaderResourceArray >, Errorcreate (GpuResourceManager *pResourceManager, const std::string &sHandledShaderResourceName, size_t iElementSizeInBytes)
 
static std::string formatBytesToKilobytes (size_t iSizeInBytes)
 
static std::variant< size_t, ErrorcalculateCapacityStepSize (size_t iElementSizeInBytes)
 

Private Attributes

std::pair< std::recursive_mutex, InternalResourcesmtxInternalResources
 
GpuResourceManager *const pResourceManager = nullptr
 
const size_t iCapacityStepSize = 0
 
const std::string sHandledShaderResourceName
 
const size_t iElementSizeInBytes = 0
 

Friends

class DynamicCpuWriteShaderResourceArraySlot
 
class DynamicCpuWriteShaderResourceArrayManager
 

Detailed Description

Allows storing elements of the same size in one GPU buffer.

Remarks
Automatically handles binding the array to the specified shader resource.
Dynamically grows and shrinks when adding/removing elements from the array.

Constructor & Destructor Documentation

◆ DynamicCpuWriteShaderResourceArray()

ne::DynamicCpuWriteShaderResourceArray::DynamicCpuWriteShaderResourceArray ( GpuResourceManager pResourceManager,
const std::string &  sHandledShaderResourceName,
size_t  iElementSizeInBytes,
size_t  iCapacityStepSize 
)
private

Creates initialized array.

Remarks
Only used internally, for creating new arrays use create.
Parameters
pResourceManagerResource manager that will allocate GPU buffers.
sHandledShaderResourceNameName of the shader resource this array handles. It will be used to bind the array to pipelines.
iElementSizeInBytesSize (in bytes) of one element in the array.
iCapacityStepSizeCapacity step size to use. Expecting it to be even and not zero.

Member Function Documentation

◆ calculateCapacityStepSize()

std::variant< size_t, Error > ne::DynamicCpuWriteShaderResourceArray::calculateCapacityStepSize ( size_t  iElementSizeInBytes)
staticprivate

Calculates array capacity step size depending on the size of the elements in the array.

Parameters
iElementSizeInBytesSize (in bytes) of one element in the array.
Returns
Error if something went wrong, otherwise capacity step size to add/remove this value when expanding/shrinking the array.

◆ create()

std::variant< std::unique_ptr< DynamicCpuWriteShaderResourceArray >, Error > ne::DynamicCpuWriteShaderResourceArray::create ( GpuResourceManager pResourceManager,
const std::string &  sHandledShaderResourceName,
size_t  iElementSizeInBytes 
)
staticprivate

Creates a new initialized array.

Parameters
pResourceManagerResource manager that will be used to allocate GPU buffers.
sHandledShaderResourceNameName of the shader resource this array handles. It will be used to bind the array to pipelines.
iElementSizeInBytesSize (in bytes) of one element in the array.
Returns
Error if something went wrong, otherwise created array.

◆ createArray()

std::optional< Error > ne::DynamicCpuWriteShaderResourceArray::createArray ( size_t  iCapacity)
private

(Re)creates the internal GPU buffer with the specified capacity.

Remarks
Indices in the currently active slots (created with insert) will be updated to reference new indices in the array.
Only updates the GPU buffer, internal capacity and all active slots (if any), other internal values are not changed and should be corrected afterwards if needed.
Parameters
iCapacitySize of the array (in elements, not bytes).
Returns
Error if something went wrong.

◆ expandArray()

std::optional< Error > ne::DynamicCpuWriteShaderResourceArray::expandArray ( )
private

Recreates the array to expand its capacity to support iCapacityStepSize more elements.

Remarks
All internal values and active slots will be updated.
Returns
Error if something went wrong.

◆ formatBytesToKilobytes()

std::string ne::DynamicCpuWriteShaderResourceArray::formatBytesToKilobytes ( size_t  iSizeInBytes)
staticprivate

Formats the specified size in bytes to the following format: "<number> KB", for example the number 1512 will be formatted to the following text: "1.47 KB".

Parameters
iSizeInBytesSize in bytes to format.
Returns
Formatted text.

◆ getCapacity()

size_t ne::DynamicCpuWriteShaderResourceArray::getCapacity ( )

Returns the maximum number of elements that could be added to the array without expanding (recreating with a bigger size) the buffer.

Returns
The maximum possible number of elements without expanding.

◆ getCapacityStepSize()

size_t ne::DynamicCpuWriteShaderResourceArray::getCapacityStepSize ( ) const

Returns the capacity to add to the new (expanded) array when there is no more free space left in the current array or capacity to remove from the new (shrinked) array when shrinking.

Returns
Capacity step size.

◆ getElementSize()

size_t ne::DynamicCpuWriteShaderResourceArray::getElementSize ( ) const

Returns size (in bytes) of one element in the array.

Returns
Size in bytes of one element.

◆ getHandledShaderResourceName()

std::string_view ne::DynamicCpuWriteShaderResourceArray::getHandledShaderResourceName ( ) const

Returns name of the shader resource (from shader code) this array handles.

Returns
Shader resource name.

◆ getInternalResources()

std::pair< std::recursive_mutex, DynamicCpuWriteShaderResourceArray::InternalResources > * ne::DynamicCpuWriteShaderResourceArray::getInternalResources ( )

Returns internal resources.

Remarks
Generally used by automated tests.
Do not delete (free) returned pointer.
Returns
Internal resources.

◆ getSize()

size_t ne::DynamicCpuWriteShaderResourceArray::getSize ( )

Returns the actual number of elements in the array (smaller or equal to getCapacity).

Returns
The actual number of elements in the array.

◆ getSizeInBytes()

size_t ne::DynamicCpuWriteShaderResourceArray::getSizeInBytes ( )

Returns size in bytes that this array takes up.

Returns
Size of array in bytes.

◆ insert()

std::variant< std::unique_ptr< DynamicCpuWriteShaderResourceArraySlot >, Error > ne::DynamicCpuWriteShaderResourceArray::insert ( ShaderCpuWriteResourceBinding pShaderResource)
private

Inserts a new element in the array.

Remarks
There is no erase function because slot destruction automatically uses internal erase, see documentation on the returned slot object.
Parameters
pShaderResourceShader resource that requires a slot in the array. If the array will resize the specified resource (if it has an active slot in the array) will be marked as "needs update" through the shader resource manager.
Returns
Error if something went wrong, otherwise slot of the newly added element in the array.

◆ markSlotAsNoLongerBeingUsed()

void ne::DynamicCpuWriteShaderResourceArray::markSlotAsNoLongerBeingUsed ( DynamicCpuWriteShaderResourceArraySlot pSlot)
private

Called by slots in their destructors to notify the array that the slot can be reused.

Parameters
pSlotSlot that can be reused now.

◆ shrinkArray()

std::optional< Error > ne::DynamicCpuWriteShaderResourceArray::shrinkArray ( )
private

Recreates the array to shrink its capacity to support iCapacityStepSize less elements.

Remarks
All internal values and active slots will be updated.
Returns
Error if something went wrong.

◆ updateSlotData()

void ne::DynamicCpuWriteShaderResourceArray::updateSlotData ( DynamicCpuWriteShaderResourceArraySlot pSlot,
void *  pData 
)
private

Called by slots to update their data.

Parameters
pSlotSlot that needs to be updated.
pDataData to copy to slot.

Member Data Documentation

◆ iCapacityStepSize

const size_t ne::DynamicCpuWriteShaderResourceArray::iCapacityStepSize = 0
private

Used for 2 things:

  1. Capacity to add for the new (expanded) GPU buffer when there is no more free space left in the current GPU buffer
  2. Capacity to remove for the new (shrinked) GPU buffer when shrinking.

◆ iElementSizeInBytes

const size_t ne::DynamicCpuWriteShaderResourceArray::iElementSizeInBytes = 0
private

Size in bytes of one element in the array.

◆ mtxInternalResources

std::pair<std::recursive_mutex, InternalResources> ne::DynamicCpuWriteShaderResourceArray::mtxInternalResources
private

Internal resources of the array.

◆ pResourceManager

GpuResourceManager* const ne::DynamicCpuWriteShaderResourceArray::pResourceManager = nullptr
private

Used to allocate GPU buffers.

◆ sHandledShaderResourceName

const std::string ne::DynamicCpuWriteShaderResourceArray::sHandledShaderResourceName
private

Name of the shader resource (from shader code) this array handles.


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