Skip to content

Class UniformBuffer

ClassList > UniformBuffer

OpenGL uniform buffer object wrapper with template-based updates. More...

  • #include <UniformBuffer.h>

Inherits the following classes: Buffers

Public Functions

Type Name
void Bind (GLuint _bind=static_cast< GLuint >(-1)) const
Binds UBO to its binding point.
void Unbind () const
Unbinds the current UBO from its binding point.
UniformBuffer () = default
Default constructor (empty buffer).
UniformBuffer (GLuint _bind)
Constructs UBO with a binding point (no initial data).
UniformBuffer (GLuint _bind, _S _tar)
Constructs UBO with binding point and initial data.
UniformBuffer (const UniformBuffer & ubo)
Copy constructor. Deep-copies GPU buffer content and binding point.
UniformBuffer (UniformBuffer && ubo) noexcept
Move constructor. Transfers ownership; ubo is left empty.
void Update (_S _tar)
Uploads new data to the UBO and records the buffer size.
UniformBuffer & operator= (const UniformBuffer & ubo)
Copy assignment. Deep-copies GPU buffer content and binding point.
UniformBuffer & operator= (UniformBuffer && ubo) noexcept
Move assignment. Transfers ownership; ubo is left empty.
~UniformBuffer () = default
Destructor. Releases OpenGL UBO (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

UniformBuffer encapsulates a UBO for sharing uniform blocks across shaders. It inherits RAII resource management from Buffers, including GPU-side deep copy via glCopyBufferSubData. buf_size is updated whenever Update() or the initialising constructor is called.

More efficient than individual uniforms when sharing large data structures (e.g., camera matrices, lighting data) across multiple shader programs.

Usage:

struct CameraData {
    glm::mat4 view;
    glm::mat4 projection;
};

UniformBuffer ubo(0, CameraData{viewMat, projMat});
ubo.Bind();
// Update on camera change
ubo.Update(CameraData{newView, newProj});

// Deep copy (new GPU buffer allocated):
UniformBuffer copy = ubo;

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:

Binding point: UBOs use binding points to link with shader uniform blocks.

Public Functions Documentation

function Bind

Binds UBO to its binding point.

void UniformBuffer::Bind (
    GLuint _bind=static_cast< GLuint >(-1)
) const

Parameters:

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

function Unbind

Unbinds the current UBO from its binding point.

void UniformBuffer::Unbind () const


function UniformBuffer [1/5]

Default constructor (empty buffer).

UniformBuffer::UniformBuffer () = default


function UniformBuffer [2/5]

Constructs UBO with a binding point (no initial data).

UniformBuffer::UniformBuffer (
    GLuint _bind
) 

Parameters:

  • _bind Binding point index (must match shader layout).

function UniformBuffer [3/5]

Constructs UBO with binding point and initial data.

template<class _S>
UniformBuffer::UniformBuffer (
    GLuint _bind,
    _S _tar
) 

Template parameters:

  • _S Data structure type

Parameters:

  • _bind Binding point index
  • _tar Initial data to upload

Note:

Uses GL_DYNAMIC_DRAW. Sets buf_size = sizeof(_S).


function UniformBuffer [4/5]

Copy constructor. Deep-copies GPU buffer content and binding point.

UniformBuffer::UniformBuffer (
    const UniformBuffer & ubo
) 

Parameters:

  • ubo Source uniform buffer.

function UniformBuffer [5/5]

Move constructor. Transfers ownership; ubo is left empty.

UniformBuffer::UniformBuffer (
    UniformBuffer && ubo
) noexcept

Parameters:

  • ubo Source uniform buffer (invalidated after move).

function Update

Uploads new data to the UBO and records the buffer size.

template<class _S>
void UniformBuffer::Update (
    _S _tar
) 

Template parameters:

  • _S Data structure type (must match UBO layout)

Parameters:

  • _tar New data to upload

Note:

Uses GL_DYNAMIC_DRAW for efficient updates. Sets buf_size = sizeof(_S).


function operator=

Copy assignment. Deep-copies GPU buffer content and binding point.

UniformBuffer & UniformBuffer::operator= (
    const UniformBuffer & ubo
) 

Parameters:

  • ubo Source uniform buffer.

Returns:

Reference to this.


function operator=

Move assignment. Transfers ownership; ubo is left empty.

UniformBuffer & UniformBuffer::operator= (
    UniformBuffer && ubo
) noexcept

Parameters:

  • ubo Source uniform buffer (invalidated after move).

Returns:

Reference to this.


function ~UniformBuffer

Destructor. Releases OpenGL UBO (via Buffers ).

UniformBuffer::~UniformBuffer () = default



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