StapleGL
Header-only C++20 OpenGL wrapper
Loading...
Searching...
No Matches
staplegl::vertex_buffer_inst Class Reference

A vertex buffer object for instanced rendering. More...

#include <vertex_buffer_inst.hpp>

Inheritance diagram for staplegl::vertex_buffer_inst:
Collaboration diagram for staplegl::vertex_buffer_inst:

Public Member Functions

 vertex_buffer_inst (std::span< const float > instance_data, vertex_buffer_layout &&layout) noexcept
 
 vertex_buffer_inst (std::span< const float > instance_data) noexcept
 
 ~vertex_buffer_inst () noexcept=default
 
 vertex_buffer_inst (const vertex_buffer_inst &)=delete
 
auto operator= (const vertex_buffer_inst &) -> vertex_buffer_inst &=delete
 
 vertex_buffer_inst (vertex_buffer_inst &&) noexcept=default
 
auto operator= (vertex_buffer_inst &&) noexcept -> vertex_buffer_inst &=default
 
void add_instance (std::span< const float > instance_data) noexcept
 Add an instance to the buffer.
 
auto delete_instance (std::int32_t index) noexcept -> int32_t
 Delete an instance from the buffer, does not preserve the order of the instances.
 
void update_instance (std::int32_t index, std::span< const float > instance_data) noexcept
 Update the data of an instance in the buffer.
 
constexpr auto instance_count () const noexcept -> std::int32_t
 
constexpr auto instance_size () const noexcept -> std::size_t
 
constexpr auto capacity () const noexcept -> std::size_t
 
- Public Member Functions inherited from staplegl::vertex_buffer
 vertex_buffer (std::span< const float > vertices) noexcept
 Construct a new vertex buffer object.
 
 vertex_buffer (std::span< const float > vertices, driver_draw_hint hint) noexcept
 
 vertex_buffer (std::span< const float > vertices, vertex_buffer_layout &&layout) noexcept
 
 vertex_buffer (std::span< const float > vertices, vertex_buffer_layout &&layout, driver_draw_hint hint) noexcept
 
 ~vertex_buffer ()
 
 vertex_buffer (const vertex_buffer &)=delete
 
auto operator= (const vertex_buffer &) -> vertex_buffer &=delete
 
 vertex_buffer (vertex_buffer &&other) noexcept
 
auto operator= (vertex_buffer &&other) noexcept -> vertex_buffer &
 
void bind () const
 Bind the vertex buffer object.
 
void set_layout (const vertex_buffer_layout &layout)
 Set the layout object.
 
constexpr auto layout () const -> const vertex_buffer_layout &
 
void set_data (std::span< const float > vertices) const noexcept
 Give new data to the vertex buffer object, overwriting the old one.
 
void set_data (std::span< const float > vertices, driver_draw_hint hint) const noexcept
 Give new data to the vertex buffer object, overwriting the old one. Also re-specify the hint.
 
constexpr auto id () const noexcept -> std::uint32_t
 Get the id of the vertex buffer object.
 
constexpr auto size () const noexcept -> std::size_t
 Get the number of vertices in the vertex buffer object.
 
constexpr auto draw_hint () const noexcept -> driver_draw_hint
 Return the driver draw hint of the vertex buffer object.
 
constexpr auto size_bytes () const noexcept -> std::size_t
 Get the size of the vertex buffer object in bytes.
 
template<plain_old_data T>
void apply (const std::function< void(std::span< T > vertices)> &func, driver_access_specifier access_specifier=staplegl::READ_WRITE) noexcept
 Applies a function to the vertices of the vertex buffer object.
 

Private Member Functions

constexpr auto calc_capacity (std::size_t capacity) const noexcept -> std::size_t
 Compute the new capacity of the buffer, given the current capacity.
 
void resize_buffer (std::size_t old_capacity, std::size_t new_capacity) noexcept
 Resize the buffer to the given capacity.
 

Private Attributes

std::size_t m_capacity {}
 The capacity of the buffer, in bytes.
 
std::int32_t m_count {}
 The number of instances in the buffer.
 

Additional Inherited Members

- Static Public Member Functions inherited from staplegl::vertex_buffer
static void unbind ()
 Unbind the vertex buffer object.
 
- Protected Attributes inherited from staplegl::vertex_buffer
std::uint32_t m_id {}
 
staplegl::driver_draw_hint m_hint {}
 
vertex_buffer_layout m_layout
 
std::size_t m_size {}
 

Detailed Description

A vertex buffer object for instanced rendering.

This class is a specialization of the vertex_buffer class, and it is meant to help with instanced rendering, allowing the user of staplegl to build a more performant renderer.

When using this class, the user should initialize the buffer with an empty span of data and a layout that describes how a single instance of the object is laid out in memory.

Alternatively, the user can also supply a span with the initial data, to avoid calling add_instance() multiple times.

Adding and deleting instances automatically resizes the buffer in a way similar to std::vector, so that the user does not have to worry about the size of the buffer.

When deleting an instance, the user should be aware that the last instance in the buffer will be moved to the position of the deleted instance, so the order of the instances is not preserved, to help the user keep track of the indices of the instances, we return the index of the deleted instance (which is the index of the last instance in the buffer before the deletion).

See also
vertex_buffer.hpp
Attention
None of the base class methods are virtual, so this class is not meant to be used polymorphically.
Examples
batches.cpp.

Definition at line 56 of file vertex_buffer_inst.hpp.

Constructor & Destructor Documentation

◆ vertex_buffer_inst() [1/4]

staplegl::vertex_buffer_inst::vertex_buffer_inst ( std::span< const float >  instance_data,
vertex_buffer_layout &&  layout 
)
inlinenoexcept

Definition at line 136 of file vertex_buffer_inst.hpp.

◆ vertex_buffer_inst() [2/4]

staplegl::vertex_buffer_inst::vertex_buffer_inst ( std::span< const float >  instance_data)
inlinenoexcept

Definition at line 141 of file vertex_buffer_inst.hpp.

◆ ~vertex_buffer_inst()

staplegl::vertex_buffer_inst::~vertex_buffer_inst ( )
defaultnoexcept

◆ vertex_buffer_inst() [3/4]

staplegl::vertex_buffer_inst::vertex_buffer_inst ( const vertex_buffer_inst )
delete

◆ vertex_buffer_inst() [4/4]

staplegl::vertex_buffer_inst::vertex_buffer_inst ( vertex_buffer_inst &&  )
defaultnoexcept

Member Function Documentation

◆ add_instance()

void staplegl::vertex_buffer_inst::add_instance ( std::span< const float >  instance_data)
inlinenoexcept

Add an instance to the buffer.

Parameters
instance_datathe data of the instance to be added.

Definition at line 199 of file vertex_buffer_inst.hpp.

◆ calc_capacity()

constexpr auto staplegl::vertex_buffer_inst::calc_capacity ( std::size_t  capacity) const -> std::size_t
inlineconstexprprivatenoexcept

Compute the new capacity of the buffer, given the current capacity.

Parameters
capacitythe current capacity of the buffer.
Returns
std::size_t the new capacity of the buffer.

Definition at line 78 of file vertex_buffer_inst.hpp.

◆ capacity()

constexpr auto staplegl::vertex_buffer_inst::capacity ( ) const -> std::size_t
inlineconstexprnoexcept

Definition at line 189 of file vertex_buffer_inst.hpp.

◆ delete_instance()

auto staplegl::vertex_buffer_inst::delete_instance ( std::int32_t  index) -> int32_t
inlinenoexcept

Delete an instance from the buffer, does not preserve the order of the instances.

Warning
The order of the instances is not preserved, by deleting an instance, the last instance will be moved to the position of the deleted instance.
Note
For out-of-bounds indices, the function will do nothing and return the index of the last instance in the buffer (as it has not been moved).
Parameters
indexthe index of the instance to be deleted.
Returns
int32_t the new index of the last instance in the buffer (so that the user can keep track of the indices).

Definition at line 225 of file vertex_buffer_inst.hpp.

◆ instance_count()

constexpr auto staplegl::vertex_buffer_inst::instance_count ( ) const -> std::int32_t
inlineconstexprnoexcept

Definition at line 187 of file vertex_buffer_inst.hpp.

◆ instance_size()

constexpr auto staplegl::vertex_buffer_inst::instance_size ( ) const -> std::size_t
inlineconstexprnoexcept

Definition at line 188 of file vertex_buffer_inst.hpp.

◆ operator=() [1/2]

auto staplegl::vertex_buffer_inst::operator= ( const vertex_buffer_inst ) -> vertex_buffer_inst &=delete
delete

◆ operator=() [2/2]

auto staplegl::vertex_buffer_inst::operator= ( vertex_buffer_inst &&  ) -> vertex_buffer_inst &=default
defaultnoexcept

◆ resize_buffer()

void staplegl::vertex_buffer_inst::resize_buffer ( std::size_t  old_capacity,
std::size_t  new_capacity 
)
inlineprivatenoexcept

Resize the buffer to the given capacity.

Note
The old buffer is regrown and a temporary buffer is created to copy the data from the old buffer to the new one. One could argue that this is not the most efficient way, as the data could be copied directly from the old buffer to the new one, however, our main concern is ID stability, so that the buffers do not have to be re-bound after resizing.
Parameters
old_capacitythe old capacity of the buffer.
new_capacitythe new capacity of the buffer.

Definition at line 103 of file vertex_buffer_inst.hpp.

◆ update_instance()

void staplegl::vertex_buffer_inst::update_instance ( std::int32_t  index,
std::span< const float >  instance_data 
)
inlinenoexcept

Update the data of an instance in the buffer.

Note
The entire contents of instance_data will not be written at the index, only the first layout.stride() bytes, preventing buffer overflows. Debug builds will also assert that the size of instance_data is equal to layout.stride().
See also
vertex_buffer_layout.hpp
Parameters
indexthe index of the instance to be updated.
instance_datathe new data of the instance.

Definition at line 211 of file vertex_buffer_inst.hpp.

Member Data Documentation

◆ m_capacity

std::size_t staplegl::vertex_buffer_inst::m_capacity {}
private

The capacity of the buffer, in bytes.

Definition at line 63 of file vertex_buffer_inst.hpp.

◆ m_count

std::int32_t staplegl::vertex_buffer_inst::m_count {}
private

The number of instances in the buffer.

Definition at line 69 of file vertex_buffer_inst.hpp.


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