NAP
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
GPUBuffer Class Referenceabstract

#include <gpubuffer.h>

Public Member Functions

 GPUBuffer (Core &core)
 
 GPUBuffer (Core &core, EMemoryUsage usage)
 
virtual ~GPUBuffer ()
 
virtual VkBuffer getBuffer () const
 
virtual const BufferDatagetBufferData () const
 
virtual bool init (utility::ErrorState &errorState) override
 
virtual uint getCount () const =0
 
virtual size_t getSize () const =0
 
virtual uint32 getElementSize () const =0
 
virtual VkBufferUsageFlags getBufferUsageFlags () const
 
void ensureUsage (VkBufferUsageFlags usage)
 
virtual bool isInitialized () const =0
 
void asyncGetData (std::function< void(const void *, size_t)> copyFunction)
 
- Public Member Functions inherited from Resource
 Resource ()
 
- Public Member Functions inherited from Object
 Object ()
 
virtual ~Object ()
 
virtual void onDestroy ()
 
 Object (Object &)=delete
 
Objectoperator= (const Object &)=delete
 
 Object (Object &&)=delete
 
Objectoperator= (Object &&)=delete
 

Public Attributes

nap::Signal bufferChanged
 
EMemoryUsage mMemoryUsage = EMemoryUsage::Static
 Property 'Usage' How the buffer is used: initialized once (Static), updated frequently from CPU to GPU (DynamicWrite) or read from GPU to CPU (DynamicRead). More...
 
- Public Attributes inherited from Object
std::string mID
 Property: 'mID' unique name of the object. Used as an identifier by the system. More...
 

Protected Member Functions

bool allocateInternal (size_t size, utility::ErrorState &errorState)
 
bool setDataInternal (const void *data, size_t size, size_t reservedSize, utility::ErrorState &errorState)
 
void requestClear ()
 

Protected Attributes

RenderServicemRenderService = nullptr
 Handle to the render service. More...
 
std::vector< BufferDatamRenderBuffers
 Render accessible buffers. More...
 
std::vector< BufferDatamStagingBuffers
 Staging buffers, used when uploading or downloading data. More...
 
uint32 mSize = 0
 Current used buffer size in bytes. More...
 
int mCurrentRenderBufferIndex = 0
 Current render buffer index. More...
 
int mCurrentStagingBufferIndex = 0
 Current staging buffer index. More...
 
std::vector< int > mDownloadStagingBufferIndices
 Staging buffer indices associated with a frameindex. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Object
static bool isIDProperty (rtti::Instance &object, const rtti::Property &property)
 

Description

Defines a Vulkan buffer object on the GPU.

A static buffer is updated (uploaded to) only once, a dynamic buffer can be updated more frequently but requires more resources and is 'generally' slower to draw, depending of the memory layout of the underlying hardware. It is not allowed to update a static buffer after the initial upload!

Note that static data is often placed in a different cache on the GPU, not accessible by the CPU, which allows for faster drawing times. 'DynamicWrite' buffers are uploaded into shared CPU / GPU memory and are therefore slower to access. Keep this in mind when selecting the appropriate memory usage.

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

Constructor & Destructor Documentation

◆ GPUBuffer() [1/2]

GPUBuffer ( Core core)

Every buffer needs to have access to the render engine. The given 'usage' controls if a buffer can be updated more than once and in which memory space it is placed.

Parameters
corethe nap core

◆ GPUBuffer() [2/2]

GPUBuffer ( Core core,
EMemoryUsage  usage 
)

Every buffer needs to have access to the render engine. The given 'usage' controls if a buffer can be updated more than once and in which memory space it is placed.

Parameters
corethe nap core
usagehow the buffer is used at runtime.

◆ ~GPUBuffer()

virtual ~GPUBuffer ( )
virtual

Member Function Documentation

◆ allocateInternal()

bool allocateInternal ( size_t  size,
utility::ErrorState errorState 
)
protected

Allocates buffers, called by derived classes

Parameters
sizesize in bytes of the buffer to allocate
errorStatecontains error when data could not be set.
Returns
if the data was set

◆ asyncGetData()

void asyncGetData ( std::function< void(const void *, size_t)>  copyFunction)

Starts a transfer of buffer data from GPU to CPU. Use this overload to pass your own copy function. This is a non blocking call.

Parameters
copyFunctionthe copy function to call when the buffer data is available for download.

◆ ensureUsage()

void ensureUsage ( VkBufferUsageFlags  usage)

Ensures the given buffer usage flags are applied when allocating and creating the buffer, next to the flags derived from the 'Usage' property. Call this function before allocation.

Parameters
usagebuffer usage flags required on allocation

◆ getBuffer()

virtual VkBuffer getBuffer ( ) const
virtual
Returns
handle to the Vulkan buffer.

◆ getBufferData()

virtual const BufferData& getBufferData ( ) const
virtual
Returns
handle to the buffer data.

◆ getBufferUsageFlags()

virtual VkBufferUsageFlags getBufferUsageFlags ( ) const
virtual
Returns
the buffer usage flags.

◆ getCount()

virtual uint getCount ( ) const
pure virtual
Returns
the number of buffer elements

Implemented in GPUBufferNumeric, and StructBuffer.

◆ getElementSize()

virtual uint32 getElementSize ( ) const
pure virtual
Returns
the element size in bytes

Implemented in GPUBufferNumeric, and StructBuffer.

◆ getSize()

virtual size_t getSize ( ) const
pure virtual
Returns
the size of the buffer in bytes

Implemented in GPUBufferNumeric, and StructBuffer.

◆ init()

virtual bool init ( utility::ErrorState errorState)
overridevirtual

Initialize the buffer

Reimplemented from Object.

Reimplemented in IndexBuffer, VertexBuffer< T >, TypedGPUBufferNumeric< T >, TypedGPUBufferNumeric< uint >, and StructBuffer.

◆ isInitialized()

virtual bool isInitialized ( ) const
pure virtual

Implemented by derived classes

Returns
whether this buffer is initialized

Implemented in TypedGPUBufferNumeric< T >, and StructBuffer.

◆ requestClear()

void requestClear ( )
protected

Helper function that calls requestBufferClear() in RenderService for derived classes

◆ setDataInternal()

bool setDataInternal ( const void *  data,
size_t  size,
size_t  reservedSize,
utility::ErrorState errorState 
)
protected

Allocates and updates GPU buffer content, called by derived classes.

Parameters
datapointer to the data to upload.
sizesize in bytes of the data to upload
reservedSizeallows the buffer to allocate more memory than required, needs to be >= size
errorStatecontains error when data could not be set.
Returns
if the data was set

Member Data Documentation

◆ bufferChanged

nap::Signal bufferChanged

Called right after the buffer on the GPU has been updated.

◆ mCurrentRenderBufferIndex

int mCurrentRenderBufferIndex = 0
protected

Current render buffer index.

◆ mCurrentStagingBufferIndex

int mCurrentStagingBufferIndex = 0
protected

Current staging buffer index.

◆ mDownloadStagingBufferIndices

std::vector<int> mDownloadStagingBufferIndices
protected

Staging buffer indices associated with a frameindex.

◆ mMemoryUsage

Property 'Usage' How the buffer is used: initialized once (Static), updated frequently from CPU to GPU (DynamicWrite) or read from GPU to CPU (DynamicRead).

◆ mRenderBuffers

std::vector<BufferData> mRenderBuffers
protected

Render accessible buffers.

◆ mRenderService

RenderService* mRenderService = nullptr
protected

Handle to the render service.

◆ mSize

uint32 mSize = 0
protected

Current used buffer size in bytes.

◆ mStagingBuffers

std::vector<BufferData> mStagingBuffers
protected

Staging buffers, used when uploading or downloading data.