Skip to content

Class StorageBuffer

ClassList > StorageBuffer

OpenGL shader storage buffer object wrapper. More...

  • #include <StorageBuffer.h>

Inherits the following classes: Buffers

Public Functions

Type Name
void BindBuffer () const
Binds this SSBO to GL_SHADER_STORAGE_BUFFER.
void BindBufferBase (GLuint _base=static_cast< GLuint >(-1)) const
Binds SSBO to its binding point for shader access.
void GenStorageBuffer (const std::vector< T > & src)
Uploads vector data to SSBO and records the buffer size.
::value void GenStorageBuffers (const _Info & _info, const std::vector< _Ele > & _data)
Uploads structured data (header + array) to SSBO and records the buffer size.
::value void GenStorageBuffers (const _Info & _info, const std::vector< _Ele > & _data)
GLuint GetBase () const
Returns the binding point index.
SSBType GetType () const
Returns the buffer data type classification.
void ReadStorageBuffer (std::vector< T > & tar, GLuint _offset=0)
Reads data from SSBO back to CPU.
void SetBufferBase (GLuint base)
Sets the binding point index.
StorageBuffer () = default
Default constructor (empty buffer).
StorageBuffer (SSBType type)
Constructs SSBO with type classification (binding point defaults to 0).
StorageBuffer (SSBType type, GLuint base)
Constructs SSBO with type and binding point.
StorageBuffer (const StorageBuffer & ssbo)
Copy constructor. Deep-copies GPU buffer content and metadata.
StorageBuffer (StorageBuffer && ssbo) noexcept
Move constructor. Transfers ownership; ssbo is left empty.
void UnbindBuffer () const
Unbinds the current SSBO.
StorageBuffer & operator= (const StorageBuffer & ssbo)
Copy assignment. Deep-copies GPU buffer content and metadata.
StorageBuffer & operator= (StorageBuffer && ssbo) noexcept
Move assignment. Transfers ownership; ssbo is left empty.
~StorageBuffer () = default
Destructor. Releases OpenGL SSBO (via Buffers ).

Public Functions inherited from Buffers

See Buffers

Type Name
Buffers () = default
Buffers (const Buffers & buf)
Copy constructor. Performs a GPU-side deep copy of buf .
Buffers (Buffers && buf) noexcept
Move constructor. Transfers ownership from buf .
GLuint GetID () const
Returns the OpenGL buffer object ID.
GLuint GetSize () const
Returns the allocated buffer size in bytes.
Buffers & operator= (const Buffers & buf)
Copy assignment. Performs a GPU-side deep copy of buf .
Buffers & operator= (Buffers && buf) noexcept
Move assignment. Transfers ownership from buf .
~Buffers ()
Destructor. Releases the OpenGL buffer object if valid.

Protected Attributes inherited from Buffers

See Buffers

Type Name
GLuint buf_ID = 0
OpenGL buffer object ID (0 = invalid/empty)
GLuint buf_size = 0
Allocated buffer size in bytes.

Protected Functions inherited from Buffers

See Buffers

Type Name
void _deepCopyFrom (const Buffers & src)
GPU-side deep copy from src .
void _delBuffer ()
Deletes the OpenGL buffer object and resets buf_ID to 0.

Detailed Description

StorageBuffer (SSBO) provides read/write access to large buffers from shaders. It inherits RAII resource management from Buffers, including GPU-side deep copy via glCopyBufferSubData. buf_size is updated by GenStorageBuffer/GenStorageBuffers so deep copy always reflects the current allocation.

Unlike UBO, SSBO supports: * Much larger buffers (up to GPU memory limit) * Write access from shaders * Dynamic sizing * Atomic operations

Common use cases: * Light arrays for deferred rendering * Particle systems * Compute shader outputs * GPU sorting and reduction

Usage:

StorageBuffer ssbo(FLOAT_LIST, 3);
std::vector<float> data = {...};
ssbo.GenStorageBuffer(data);
ssbo.BindBufferBase();

// After compute shader
std::vector<float> result;
ssbo.ReadStorageBuffer(result);

// Deep copy (new GPU buffer allocated):
StorageBuffer copy = ssbo;

Note:

Thread-safety: Not thread-safe. Must be used from the OpenGL context thread.

Note:

Ownership: Inherits Buffers ownership; releases via glDeleteBuffers in destructor.

Note:

Performance: Reading back from GPU is slow. Minimise CPU readbacks.

Public Functions Documentation

function BindBuffer

Binds this SSBO to GL_SHADER_STORAGE_BUFFER.

void StorageBuffer::BindBuffer () const


function BindBufferBase

Binds SSBO to its binding point for shader access.

void StorageBuffer::BindBufferBase (
    GLuint _base=static_cast< GLuint >(-1)
) const

Parameters:

  • _base Binding point override (-1 to use stored binding).

function GenStorageBuffer

Uploads vector data to SSBO and records the buffer size.

template<typename T>
void StorageBuffer::GenStorageBuffer (
    const std::vector< T > & src
) 

Template parameters:

  • T Element type

Parameters:

  • src Source vector (copied to GPU)

Note:

Uses GL_STATIC_DRAW. For frequently updated data, consider dynamic approach.


function GenStorageBuffers [1/2]

Uploads structured data (header + array) to SSBO and records the buffer size.

template<typename _Info, typename _Ele>
::value void StorageBuffer::GenStorageBuffers (
    const _Info & _info,
    const std::vector< _Ele > & _data
) 

Template parameters:

  • _Info Header structure type
  • _Ele Array element type

Parameters:

  • _info Header data
  • _data Array data

Note:

Useful for arrays with metadata (e.g., count, bounds).


function GenStorageBuffers [1/2]

template<typename _Info, typename _Ele>
::value void StorageBuffer::GenStorageBuffers (
    const _Info & _info,
    const std::vector< _Ele > & _data
) 

function GetBase

Returns the binding point index.

inline GLuint StorageBuffer::GetBase () const

Returns:

Binding point.


function GetType

Returns the buffer data type classification.

inline SSBType StorageBuffer::GetType () const

Returns:

SSBType.


function ReadStorageBuffer

Reads data from SSBO back to CPU.

template<typename T>
void StorageBuffer::ReadStorageBuffer (
    std::vector< T > & tar,
    GLuint _offset=0
) 

Template parameters:

  • T Element type

Parameters:

  • tar Target vector (resized to buffer size if empty)
  • _offset Byte offset into buffer

Note:

Performance: GPU readback is slow. Minimise usage.


function SetBufferBase

Sets the binding point index.

void StorageBuffer::SetBufferBase (
    GLuint base
) 

Parameters:

  • base New binding point.

function StorageBuffer [1/5]

Default constructor (empty buffer).

StorageBuffer::StorageBuffer () = default


function StorageBuffer [2/5]

Constructs SSBO with type classification (binding point defaults to 0).

StorageBuffer::StorageBuffer (
    SSBType type
) 

Parameters:

  • type Data type classification

function StorageBuffer [3/5]

Constructs SSBO with type and binding point.

StorageBuffer::StorageBuffer (
    SSBType type,
    GLuint base
) 

Parameters:

  • type Data type classification
  • base Binding point index

function StorageBuffer [4/5]

Copy constructor. Deep-copies GPU buffer content and metadata.

StorageBuffer::StorageBuffer (
    const StorageBuffer & ssbo
) 

Parameters:

  • ssbo Source storage buffer.

function StorageBuffer [5/5]

Move constructor. Transfers ownership; ssbo is left empty.

StorageBuffer::StorageBuffer (
    StorageBuffer && ssbo
) noexcept

Parameters:

  • ssbo Source storage buffer (invalidated after move).

function UnbindBuffer

Unbinds the current SSBO.

void StorageBuffer::UnbindBuffer () const


function operator=

Copy assignment. Deep-copies GPU buffer content and metadata.

StorageBuffer & StorageBuffer::operator= (
    const StorageBuffer & ssbo
) 

Parameters:

  • ssbo Source storage buffer.

Returns:

Reference to this.


function operator=

Move assignment. Transfers ownership; ssbo is left empty.

StorageBuffer & StorageBuffer::operator= (
    StorageBuffer && ssbo
) noexcept

Parameters:

  • ssbo Source storage buffer (invalidated after move).

Returns:

Reference to this.


function ~StorageBuffer

Destructor. Releases OpenGL SSBO (via Buffers ).

StorageBuffer::~StorageBuffer () = default



The documentation for this class was generated from the following file src/render/buffers/StorageBuffer.h