Sparkle 0.0.1
Loading...
Searching...
No Matches
spk::OpenGL::BufferObject Class Reference

Owns an OpenGL buffer with CPU-side staging and synchronization to GPU. More...

#include <spk_buffer_object.hpp>

Inheritance diagram for spk::OpenGL::BufferObject:
Inheritance graph
Collaboration diagram for spk::OpenGL::BufferObject:
Collaboration graph

Public Types

enum class  Type : GLenum {
  Unknow = GL_INVALID_ENUM , Storage = GL_ARRAY_BUFFER , Element = GL_ELEMENT_ARRAY_BUFFER , Uniform = GL_UNIFORM_BUFFER ,
  Texture = GL_TEXTURE_BUFFER , TransformFeedback = GL_TRANSFORM_FEEDBACK_BUFFER , ShaderStorage = GL_SHADER_STORAGE_BUFFER , PixelPack = GL_PIXEL_PACK_BUFFER ,
  PixelUnpack = GL_PIXEL_UNPACK_BUFFER , DrawIndirect = GL_DRAW_INDIRECT_BUFFER , AtomicCounter = GL_ATOMIC_COUNTER_BUFFER
}
 Supported OpenGL binding targets for buffer uploads.
enum class  Usage : GLenum {
  Unknow = GL_INVALID_ENUM , Static = GL_STATIC_DRAW , Dynamic = GL_DYNAMIC_DRAW , Stream = GL_STREAM_DRAW ,
  StaticRead = GL_STATIC_READ , DynamicRead = GL_DYNAMIC_READ , StreamRead = GL_STREAM_READ , StaticCopy = GL_STATIC_COPY ,
  DynamicCopy = GL_DYNAMIC_COPY , StreamCopy = GL_STREAM_COPY
}
 Usage hints forwarded to the OpenGL driver.
using Buffer = std::vector<uint8_t>
 CPU-side byte buffer used as staging before GPU upload.
Public Types inherited from spk::ActivableObject
using Contract = StatefulObject<ActivationStatus>::Contract
 Subscription handle controlling callback lifetime.
using Job = StatefulObject<ActivationStatus>::Job
 Callback signature executed on activation state changes.
Public Types inherited from spk::StatefulObject< ActivationStatus >
using Contract
 Handle allowing subscribers to manage callback lifetimes.
using Job
 Callable invoked when the object transitions to a state.

Public Member Functions

 BufferObject (const Type &p_type, const Usage &p_usage)
 Constructs a buffer with target type and usage hint.
 BufferObject (const BufferObject &p_other)
 Copies buffer configuration and staged content from another buffer.
BufferObjectoperator= (const BufferObject &p_other)
 Copies buffer configuration and staged content from another buffer.
void reserve (size_t p_size)
 Reserves CPU-side storage without changing size.
void resize (size_t p_size)
 Resizes CPU-side buffer and marks for sync.
void swapData (std::vector< uint8_t > &p_data)
 Swaps CPU staging buffer with external vector.
template<typename TType>
void setData (const std::vector< TType > &p_data)
 Replaces buffer contents with a vector of trivially copyable values.
template<typename TType>
void setData (std::initializer_list< TType > p_data)
 Replaces buffer contents with an initializer list of values.
void setData (const void *p_data, size_t p_size)
 Replaces CPU buffer content with raw data.
void pushData (const void *p_data, size_t p_size)
 Appends raw data to CPU buffer.
template<typename TType>
void pushData (const TType &p_value)
 Appends a single trivially copyable value to the buffer.
template<typename TType>
void pushData (const std::vector< TType > &p_values)
 Appends a vector of trivially copyable values to the buffer.
template<typename TType>
void pushData (std::initializer_list< TType > p_values)
 Appends an initializer list of trivially copyable values to the buffer.
void editData (const void *p_data, size_t p_offset, size_t p_size)
 Overwrites a range of the CPU buffer.
template<typename TType>
void editData (const TType &p_value, size_t p_offset)
 Overwrites part of the buffer with a single value.
size_t size () const
 Returns the current CPU buffer size.
Bufferbuffer ()
 Mutable access to the CPU staging buffer.
const Bufferbuffer () const
 Const access to the CPU staging buffer.
Buffer::value_type * data ()
 Mutable pointer to underlying bytes.
const Buffer::value_type * data () const
 Const pointer to underlying bytes.
Public Member Functions inherited from spk::ActivableObject
 ActivableObject ()
 Builds a deactivated object.
 ActivableObject (ActivationStatus p_initial)
 Builds with a custom initial activation state.
void activate ()
 Sets the state to Activated.
void deactivate ()
 Sets the state to Deactivated.
void toggle ()
 Toggles between activated and deactivated states.
bool isActive () const
 Checks whether the object is currently activated.
Contract addActivationCallback (const Job &p_callback)
 Registers a callback executed when the object activates.
Contract addDeactivationCallback (const Job &p_callback)
 Registers a callback executed when the object deactivates.
Public Member Functions inherited from spk::StatefulObject< ActivationStatus >
 StatefulObject (const ActivationStatus &p_initialState)
 Builds with an initial state.
StatefulObjectoperator= (const StatefulObject &p_other)=delete
void setState (const ActivationStatus &p_newState)
 Sets a new state and triggers callbacks registered for it.
ActivationStatus state () const
 Returns the current state.
Contract addCallback (const ActivationStatus &p_state, const Job &p_callback)
 Subscribes a callback for a specific state.
Public Member Functions inherited from spk::SynchronizableObject
void requestSynchronization () noexcept
 Marks the object as needing synchronization.
bool needsSynchronization () const noexcept
 Checks if synchronization is pending.
void synchronize ()
 Performs synchronization if requested.
void forceSynchronization ()
 Forces synchronization regardless of current flag.

Protected Member Functions

void _registerCallbacks ()
 Registers GPU lifecycle callbacks with the cached buffer id.
void _onSynchronize () override
 Uploads staged data to the GPU when synchronization is requested.

Protected Attributes

Type _type
 Buffer target selected at construction.
Usage _usage
 Driver usage hint selected at construction.
spk::CachedData< GLuint > _id
 Lazily created GPU buffer identifier.
size_t _gpuCapacity = 0
 Capacity reserved on the GPU for the current buffer.
Buffer _content
 CPU staging area mirrored to GPU on synchronization.

Detailed Description

Owns an OpenGL buffer with CPU-side staging and synchronization to GPU.

See also
BufferSetObject, UniformBufferObject, ShaderStorageBufferObject
spk::OpenGL::BufferObject buf(spk::OpenGL::BufferObject::Type::Storage,
spk::OpenGL::BufferObject::Usage::Dynamic);
buf.setData(std::vector<float>{1.f, 2.f, 3.f});
buf.activate();
Owns an OpenGL buffer with CPU-side staging and synchronization to GPU.
Definition spk_buffer_object.hpp:33

Constructor & Destructor Documentation

◆ BufferObject() [1/2]

spk::OpenGL::BufferObject::BufferObject ( const Type & p_type,
const Usage & p_usage )

Constructs a buffer with target type and usage hint.

Parameters
p_typeBuffer binding target.
p_usageUsage hint.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BufferObject() [2/2]

spk::OpenGL::BufferObject::BufferObject ( const BufferObject & p_other)

Copies buffer configuration and staged content from another buffer.

Parameters
p_otherSource buffer.
Here is the call graph for this function:

Member Function Documentation

◆ _onSynchronize()

void spk::OpenGL::BufferObject::_onSynchronize ( )
overrideprotectedvirtual

Uploads staged data to the GPU when synchronization is requested.

Implements spk::SynchronizableObject.

Reimplemented in spk::OpenGL::VertexBufferObject.

Here is the caller graph for this function:

◆ buffer() [1/2]

BufferObject::Buffer & spk::OpenGL::BufferObject::buffer ( )

Mutable access to the CPU staging buffer.

Returns
Reference to the buffer vector.

◆ buffer() [2/2]

const BufferObject::Buffer & spk::OpenGL::BufferObject::buffer ( ) const

Const access to the CPU staging buffer.

Returns
Const reference to the buffer vector.

◆ data() [1/2]

BufferObject::Buffer::value_type * spk::OpenGL::BufferObject::data ( )

Mutable pointer to underlying bytes.

Returns
Pointer or nullptr if empty.
Here is the caller graph for this function:

◆ data() [2/2]

const BufferObject::Buffer::value_type * spk::OpenGL::BufferObject::data ( ) const

Const pointer to underlying bytes.

Returns
Pointer or nullptr if empty.

◆ editData() [1/2]

template<typename TType>
void spk::OpenGL::BufferObject::editData ( const TType & p_value,
size_t p_offset )
inline

Overwrites part of the buffer with a single value.

Template Parameters
TTypeTrivially copyable type written into the buffer.
Parameters
p_valueValue to copy.
p_offsetByte offset where the value should be written.
Here is the call graph for this function:

◆ editData() [2/2]

void spk::OpenGL::BufferObject::editData ( const void * p_data,
size_t p_offset,
size_t p_size )

Overwrites a range of the CPU buffer.

Parameters
p_dataSource pointer.
p_offsetByte offset to start writing.
p_sizeNumber of bytes to write.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=()

BufferObject & spk::OpenGL::BufferObject::operator= ( const BufferObject & p_other)

Copies buffer configuration and staged content from another buffer.

Parameters
p_otherSource buffer.
Returns
Reference to this buffer.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pushData() [1/4]

template<typename TType>
void spk::OpenGL::BufferObject::pushData ( const std::vector< TType > & p_values)
inline

Appends a vector of trivially copyable values to the buffer.

Template Parameters
TTypeTrivially copyable type to append.
Parameters
p_valuesValues to append.
Here is the call graph for this function:

◆ pushData() [2/4]

template<typename TType>
void spk::OpenGL::BufferObject::pushData ( const TType & p_value)
inline

Appends a single trivially copyable value to the buffer.

Template Parameters
TTypeTrivially copyable type to append.
Parameters
p_valueValue to append.
Here is the call graph for this function:

◆ pushData() [3/4]

void spk::OpenGL::BufferObject::pushData ( const void * p_data,
size_t p_size )

Appends raw data to CPU buffer.

Parameters
p_dataSource pointer.
p_sizeByte size.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pushData() [4/4]

template<typename TType>
void spk::OpenGL::BufferObject::pushData ( std::initializer_list< TType > p_values)
inline

Appends an initializer list of trivially copyable values to the buffer.

Template Parameters
TTypeTrivially copyable type to append.
Parameters
p_valuesValues to append.
Here is the call graph for this function:

◆ reserve()

void spk::OpenGL::BufferObject::reserve ( size_t p_size)

Reserves CPU-side storage without changing size.

Parameters
p_sizeBytes to reserve.
Here is the caller graph for this function:

◆ resize()

void spk::OpenGL::BufferObject::resize ( size_t p_size)

Resizes CPU-side buffer and marks for sync.

Parameters
p_sizeNew size in bytes.
Here is the caller graph for this function:

◆ setData() [1/3]

template<typename TType>
void spk::OpenGL::BufferObject::setData ( const std::vector< TType > & p_data)
inline

Replaces buffer contents with a vector of trivially copyable values.

Template Parameters
TTypeTrivially copyable element type stored in the buffer.
Parameters
p_dataData vector to copy from.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setData() [2/3]

void spk::OpenGL::BufferObject::setData ( const void * p_data,
size_t p_size )

Replaces CPU buffer content with raw data.

Parameters
p_dataSource pointer.
p_sizeByte size.
Here is the call graph for this function:

◆ setData() [3/3]

template<typename TType>
void spk::OpenGL::BufferObject::setData ( std::initializer_list< TType > p_data)
inline

Replaces buffer contents with an initializer list of values.

Template Parameters
TTypeTrivially copyable element type stored in the buffer.
Parameters
p_dataData list to copy from.
Here is the call graph for this function:

◆ size()

size_t spk::OpenGL::BufferObject::size ( ) const

Returns the current CPU buffer size.

Returns
Byte size.
Here is the caller graph for this function:

◆ swapData()

void spk::OpenGL::BufferObject::swapData ( std::vector< uint8_t > & p_data)

Swaps CPU staging buffer with external vector.

Parameters
p_dataVector to swap with.
Here is the call graph for this function:

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