Overte C++ Documentation
gpu::Buffer Class Reference

#include <Buffer.h>

Inheritance diagram for gpu::Buffer:
Collaboration diagram for gpu::Buffer:

Classes

class  Update
 

Public Member Functions

 Buffer (uint32_t usage, Size pageSize=PageManager::DEFAULT_PAGE_SIZE)
 
 Buffer (uint32_t usage, Size size, const Byte *bytes, Size pageSize=PageManager::DEFAULT_PAGE_SIZE)
 Creates buffer with a given content. More...
 
 Buffer (const Buffer &buf)
 Copy constructor. More...
 
Buffer & operator= (const Buffer &buf)
 Assignment constructor. More...
 
 ~Buffer ()
 
Size getSize () const override
 
template<typename T >
Size getNumTypedElements () const
 
const Byte * getData () const
 
uint32_t getUsage () const
 
Size resize (Size pSize)
 Resizes buffer. More...
 
Size setData (Size size, const Byte *data)
 Assign data bytes and size (allocate for size, then copy bytes if exists). More...
 
Size setSubData (Size offset, Size size, const Byte *data)
 Set a subset of buffer's data. More...
 
template<typename T >
Size setSubData (Size index, const T &t)
 Set a subset of buffer's data using data of a given object. More...
 
template<typename T >
Size setSubData (Size index, const std::vector< T > &t)
 Set a subset of buffer's data using a vector of objects of a given type. More...
 
Size append (Size size, const Byte *data)
 Append new data at the end of the current buffer. More...
 
template<typename T >
Size append (const T &t)
 Append new data at the end of the current buffer from an object of a given type. More...
 
template<typename T >
Size append (const std::vector< T > &t)
 Append new data at the end of the current buffer using a vector of objects of a given type. More...
 
const Sysmem & getSysmem () const
 Access the sysmem object. More...
 
bool isDirty () const
 
void applyUpdate (const Update &update)
 Applies update to a buffer. More...
 
Update getUpdate () const
 Generate Update object from the current changes to the buffer. More...
 

Static Public Member Functions

static uint32_t getBufferCPUCount ()
 
static Size getBufferCPUMemSize ()
 
template<typename T >
static Buffer * createBuffer (uint32_t usage, const std::vector< T > &v)
 Creates a buffer from std::vector. More...
 

Public Attributes

const GPUObjectPointer gpuObject {}
 

Protected Member Functions

void incrementCreatedBufferCount ()
 Counter used for debugging excessive creation of buffers.
 
void flush () const
 Instantly applies current changes to a buffer without creating an Update object. More...
 
void markDirty (Size offset, Size bytes)
 Marks pages on which specified data range is stored as dirty. More...
 
template<typename T >
void markDirty (Size index, Size count=1)
 Marks pages on which data from a number of objects of a specified type range is stored as dirty. More...
 
Sysmem & editSysmem ()
 
Byte * editData ()
 Returns pointer to data to be edited, but data still needs to be flagged as dirty with a separate makeDirty call. More...
 

Protected Attributes

PageManager _renderPages
 
Sysmem _renderSysmem
 
std::atomic< size_t > _getUpdateCount { 0 }
 
std::atomic< size_t > _applyUpdateCount { 0 }
 
PageManager _pages
 
Size _end { 0 }
 
Sysmem _sysmem
 
const uint32_t _usage { 0 }
 

Detailed Description

Represents various kinds of buffers (index, vertex, uniform, resource, etc.) on the CPU side.

Constructor & Destructor Documentation

◆ Buffer() [1/3]

Buffer::Buffer ( uint32_t  usage,
Size  pageSize = PageManager::DEFAULT_PAGE_SIZE 
)
Parameters
usageA bit sum of Buffer::Usage flags describing this buffer.
pageSizeSize of the page in bytes. Pages are the smallest parts of the buffer that can be updated.

◆ Buffer() [2/3]

Buffer::Buffer ( uint32_t  usage,
Size  size,
const Byte *  bytes,
Size  pageSize = PageManager::DEFAULT_PAGE_SIZE 
)

Creates buffer with a given content.

Parameters
usagePlanned usage of this buffer, bit sum of Buffer::Usage flags.
sizeSize in bytes.
bytesPointer to the data.
pageSizeSize of the page in bytes. Pages are the smallest parts of the buffer that can be updated.

◆ Buffer() [3/3]

Buffer::Buffer ( const Buffer &  buf)

Copy constructor.

Performs deep copy of the sysmem buffer.

Parameters
bufBuffer to be copied.

◆ ~Buffer()

Buffer::~Buffer ( )

Buffer destructor updated statistics.

Member Function Documentation

◆ append() [1/3]

template<typename T >
Size gpu::Buffer::append ( const std::vector< T > &  t)
inline

Append new data at the end of the current buffer using a vector of objects of a given type.

Template Parameters
TObject type.
Parameters
tVector of objects that will be the source of data.
Returns
Number of bytes copied.

◆ append() [2/3]

template<typename T >
Size gpu::Buffer::append ( const T &  t)
inline

Append new data at the end of the current buffer from an object of a given type.

Template Parameters
TObject type.
Parameters
tInstance of the object that will be the source of data.
Returns
Number of bytes copied.

◆ append() [3/3]

Buffer::Size Buffer::append ( Size  size,
const Byte *  data 
)

Append new data at the end of the current buffer.

Resizes buffer to (size + getSize) and copies the new data.

Parameters
sizeSize of the data to append in bytes.
dataPointer to the data.
Returns
Number of bytes copied.

◆ applyUpdate()

void Buffer::applyUpdate ( const Update &  update)

Applies update to a buffer.

After the batches are generated changes to the buffer contents need to be applied. The reason for this is that a frame is executed at the same time as another frame object is being generated.

Parameters
updateUpdate object to apply.

◆ createBuffer()

template<typename T >
static Buffer* gpu::Buffer::createBuffer ( uint32_t  usage,
const std::vector< T > &  v 
)
inlinestatic

Creates a buffer from std::vector.

Template Parameters
TVector type.
Parameters
usagePlanned usage of this buffer, bit sum of Buffer::Usage flags.
vVector, which contents will be copied to the buffer.
Returns
Pointer to the newly created buffer.

◆ editData()

Byte* gpu::Buffer::editData ( )
inlineprotected

Returns pointer to data to be edited, but data still needs to be flagged as dirty with a separate makeDirty call.

Returns
Pointer to buffer data.

◆ editSysmem()

Sysmem& gpu::Buffer::editSysmem ( )
inlineprotected
Returns
Reference to sysmem object.

◆ flush()

void Buffer::flush ( ) const
protected

Instantly applies current changes to a buffer without creating an Update object.

For use by the render thread to avoid the intermediate step of getUpdate/applyUpdate.

◆ getBufferCPUCount()

uint32_t Buffer::getBufferCPUCount ( )
static
Returns
Total count of currently existing buffers.

◆ getBufferCPUMemSize()

Buffer::Size Buffer::getBufferCPUMemSize ( )
static
Returns
Total size in bytes of currently existing buffers.

◆ getData()

const Byte* gpu::Buffer::getData ( ) const
inline
Returns
Constant pointer to buffer data.

◆ getNumTypedElements()

template<typename T >
Size gpu::Buffer::getNumTypedElements ( ) const
inline
Template Parameters
TElement type.
Returns
Number of elements of a given type in the buffer.

◆ getSize()

Buffer::Size Buffer::getSize ( ) const
overridevirtual
Returns
The size in bytes of data stored in the buffer

Implements gpu::Resource.

◆ getSysmem()

const Sysmem& gpu::Buffer::getSysmem ( ) const
inline

Access the sysmem object.

Symsmem object contains buffer data on the CPU side. Rendering backends upload it to the GPU. Intended only for Buffer and GPUObject derived classes.

Returns
Constant reference to the sysmem object.

◆ getUpdate()

Buffer::Update Buffer::getUpdate ( ) const

Generate Update object from the current changes to the buffer.

Should be called only if isDirty returns true.

Returns
Update object.

◆ getUsage()

uint32_t gpu::Buffer::getUsage ( ) const
inline
Returns
Returns usage, which is a bit sum of Buffer::Usage flags.

◆ isDirty()

bool gpu::Buffer::isDirty ( ) const
inline
Returns
true if there are updates for this buffer that were not applied yet.

◆ markDirty() [1/2]

template<typename T >
void gpu::Buffer::markDirty ( Size  index,
Size  count = 1 
)
inlineprotected

Marks pages on which data from a number of objects of a specified type range is stored as dirty.

Template Parameters
TType of the object from which data is stored.
Parameters
indexStart position. Offset in bytes is index * sizeof(T).
countNumber of objects to mark as dirty. Range in bytes is count * sizeof(T).

◆ markDirty() [2/2]

void Buffer::markDirty ( Size  offset,
Size  bytes 
)
protected

Marks pages on which specified data range is stored as dirty.

Used to notify buffer update system that data was edited.

Parameters
offset
bytes

◆ operator=()

Buffer & Buffer::operator= ( const Buffer &  buf)

Assignment constructor.

Performs deep copy of the sysmem buffer.

Parameters
bufBuffer to be copied.
Returns
Reference to the destination buffer.

◆ resize()

Buffer::Size Buffer::resize ( Size  pSize)

Resizes buffer.

Keeps previous data [0 to min(pSize, mSize)].

Parameters
pSizeNew size in bytes.
Returns
New size, same as input parameter.

◆ setData()

Buffer::Size Buffer::setData ( Size  size,
const Byte *  data 
)

Assign data bytes and size (allocate for size, then copy bytes if exists).

Parameters
sizeNew size in bytes.
dataPointer to data of size specified in the previous parameter.
Returns
New size, same as input parameter.

◆ setSubData() [1/3]

template<typename T >
Size gpu::Buffer::setSubData ( Size  index,
const std::vector< T > &  t 
)
inline

Set a subset of buffer's data using a vector of objects of a given type.

Template Parameters
TObject type.
Parameters
indexPosition in the buffer to which to write to. Offset in bytes is (index * sizeof(T)).
tVector of objects that will be the source of data.
Returns
Number of bytes written.

◆ setSubData() [2/3]

template<typename T >
Size gpu::Buffer::setSubData ( Size  index,
const T &  t 
)
inline

Set a subset of buffer's data using data of a given object.

Template Parameters
TType of the object.
Parameters
indexPosition in the buffer to which to write to. Offset in bytes is (index * sizeof(T)).
tInstance of the object that will be the source of data.
Returns
Number of bytes written.

◆ setSubData() [3/3]

Buffer::Size Buffer::setSubData ( Size  offset,
Size  size,
const Byte *  data 
)

Set a subset of buffer's data.

Fails and returns 0 if buffer is not big enough.

Parameters
offsetOffset at which to start writing data.
sizeSize in bytes of data to write.
dataPointer to the data.
Returns
The number of bytes copied or zero if it was not possible to copy them.

Member Data Documentation

◆ _applyUpdateCount

std::atomic<size_t> gpu::Buffer::_applyUpdateCount { 0 }
mutableprotected

Number of times a buffer update object was applied.

◆ _end

Size gpu::Buffer::_end { 0 }
protected

Size of the buffer.

◆ _getUpdateCount

std::atomic<size_t> gpu::Buffer::_getUpdateCount { 0 }
mutableprotected

Number of times a buffer update object was created.

◆ _pages

PageManager gpu::Buffer::_pages
mutableprotected

PageManager for _sysmem. When buffer memory is edited, pages that were changed are flagged here and then Update object can be generated. Later applying the update copies changes to _renderSysmem object.

◆ _renderPages

PageManager gpu::Buffer::_renderPages
mutableprotected

PageManager for second copy of the buffer (one ot which updates are applied).

◆ _renderSysmem

Sysmem gpu::Buffer::_renderSysmem
mutableprotected

Second copy of the buffer. Update objects need to be generated from _sysmem and applying them copies them to _renderSysmem.

◆ _sysmem

Sysmem gpu::Buffer::_sysmem
protected

Changes to the buffer are written here, and then pages corresponding to them in _pages are marked as dirty. This is later used to create Update object that can be applied to _renderSysmem.

◆ _usage

const uint32_t gpu::Buffer::_usage { 0 }
protected

Usage of this buffer. It's a bit sum of Buffer::Usage.

◆ gpuObject

const GPUObjectPointer gpu::Buffer::gpuObject {}

Pointer to the renderer backend object associated with this object. The renderer backend object will have different type depending on currently running backend, for example OpenGL or Vulkan.


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