cgv
Loading...
Searching...
No Matches
cgv::render::vertex_buffer Class Reference

a vertex buffer is an unstructured memory block on the GPU. More...

#include <vertex_buffer.h>

Inheritance diagram for cgv::render::vertex_buffer:
cgv::render::vertex_buffer_base cgv::render::render_component

Public Member Functions

 vertex_buffer (VertexBufferType _type=VBT_VERTICES, VertexBufferUsage _usage=VBU_STATIC_DRAW)
 construct from description of component format, where the default format specifies a color buffer with alpha channel
 
 ~vertex_buffer ()
 calls the destruct method if necessary
 
void bind (context &ctx, VertexBufferType type=VBT_UNDEF) const
 Bind buffer to appropriate target.
 
void unbind (context &ctx, VertexBufferType type=VBT_UNDEF) const
 Unbind buffer from the appropriate target.
 
void unbind (context &ctx, unsigned index) const
 Unbind this buffer from the appropriate indexed binding target.
 
void unbind (context &ctx, VertexBufferType type, unsigned index) const
 Unbind from the choosen binding target.
 
void bind (context &ctx, unsigned index) const
 Bind this buffer to the appropriate indexed buffer target.
 
void bind (context &ctx, VertexBufferType type, unsigned index) const
 Bind this buffer to the appropriate indexed buffer target.
 
bool create (const context &ctx, size_t size_in_bytes)
 create empty vertex buffer of size size given in bytes
 
template<typename T >
bool create (const context &ctx, const T &array)
 create vertex buffer and copy data from CPU array array into buffer memory
 
template<typename T >
bool create (const context &ctx, const T *array_ptr, size_t nr_elements)
 create vertex buffer and copy data from CPU array array_ptr into buffer memory
 
bool is_created () const override
 Check whether the vertex buffer has been created.
 
size_t get_size_in_bytes () const
 Retrieves the current size of the buffer in bytes.
 
bool resize (const context &ctx, size_t size_in_bytes)
 resize vertex buffer to size size given in bytes clearing all data
 
template<typename T , typename = std::enable_if_t<std::is_class<T>::value, bool>>
bool resize (const context &ctx, const T &array)
 resize vertex buffer and copy data from CPU array array into buffer memory
 
template<typename T >
bool resize (const context &ctx, const T *array_ptr, size_t nr_elements)
 resize vertex buffer and copy data from CPU array array_ptr into buffer memory
 
bool create_or_resize (const context &ctx, size_t size_in_bytes)
 Convenience wrapper to either create() or resize() the buffer.
 
template<typename T , typename = std::enable_if_t<std::is_class<T>::value, bool>>
bool create_or_resize (const context &ctx, const T &array)
 Convenience wrapper to either create() or resize() the buffer.
 
template<typename T >
bool create_or_resize (const context &ctx, const T *array_ptr, size_t nr_elements)
 Convenience wrapper to either create() or resize() the buffer.
 
template<typename T >
bool replace (const context &ctx, size_t buffer_offset_in_bytes, const T *array_ptr, size_t nr_elements)
 replace part (starting at byte offset buffer_offset_in_bytes) or whole vertex buffer content from nr_elements of CPU array array_ptr
 
bool copy (const context &ctx, size_t src_offset_in_bytes, size_t size_in_bytes, vertex_buffer &dst, size_t dst_offset_in_bytes) const
 Copy bytes between different vertex_buffer instances.
 
template<typename T >
bool copy (const context &ctx, size_t src_offset_in_bytes, T *array_ptr, size_t nr_elements)
 Copy elements from the buffer into the CPU array.
 
template<typename T , typename = std::enable_if_t<std::is_class<T>::value, bool>>
bool copy (const context &ctx, T &array, size_t src_offset_in_bytes=0)
 Copy elements from the buffer into the CPU array.
 
void destruct (const context &ctx)
 destruct the render buffer
 
- Public Member Functions inherited from cgv::render::vertex_buffer_base
 vertex_buffer_base ()
 initialize members
 
- Public Member Functions inherited from cgv::render::render_component
 render_component ()
 initialize members
 
void put_id_void (void *ptr) const
 copy the rendering api specific id the component to the memory location of the given pointer.
 
template<typename T >
void put_id (T &id) const
 cast the refence to rendering api specific representation of component id to the specified type
 

Protected Attributes

size_t size_in_bytes
 

Additional Inherited Members

- Public Attributes inherited from cgv::render::vertex_buffer_base
VertexBufferType type
 buffer type defaults to VBT_VERTICES
 
VertexBufferUsage usage
 usage defaults to VBU_STATIC_DRAW
 
- Public Attributes inherited from cgv::render::render_component
void * handle
 
void * internal_format
 
void * user_data
 
const contextctx_ptr
 keep pointer to my context
 
std::string last_error
 a string that contains the last error
 

Detailed Description

a vertex buffer is an unstructured memory block on the GPU.

Definition at line 12 of file vertex_buffer.h.

Constructor & Destructor Documentation

◆ vertex_buffer()

cgv::render::vertex_buffer::vertex_buffer ( VertexBufferType  _type = VBT_VERTICES,
VertexBufferUsage  _usage = VBU_STATIC_DRAW 
)

construct from description of component format, where the default format specifies a color buffer with alpha channel

Definition at line 7 of file vertex_buffer.cxx.

References cgv::render::vertex_buffer_base::type, and cgv::render::vertex_buffer_base::usage.

◆ ~vertex_buffer()

cgv::render::vertex_buffer::~vertex_buffer ( )

calls the destruct method if necessary

Definition at line 15 of file vertex_buffer.cxx.

References cgv::render::render_component::ctx_ptr, destruct(), and cgv::render::context::make_current().

Member Function Documentation

◆ bind() [1/3]

void cgv::render::vertex_buffer::bind ( context ctx,
unsigned  index 
) const

Bind this buffer to the appropriate indexed buffer target.

Parameters
ctxThe CGV rendering context.
indexWhich index of the binding target to use.

This function will use the type information given to vertex_buffer::vertex_buffer() to determine the correct binding target.

Definition at line 47 of file vertex_buffer.cxx.

References cgv::render::vertex_buffer_base::type.

◆ bind() [2/3]

void cgv::render::vertex_buffer::bind ( context ctx,
VertexBufferType  type,
unsigned  index 
) const

Bind this buffer to the appropriate indexed buffer target.

Parameters
ctxThe CGV rendering context.
typeWill bind according to the given type.
indexWhich index of the binding target to use.
Warning
Pay special attention to use the same type in the matching unbind() call. Otherwise the internal type will be used again.

Definition at line 52 of file vertex_buffer.cxx.

◆ bind() [3/3]

void cgv::render::vertex_buffer::bind ( context ctx,
VertexBufferType  type = VBT_UNDEF 
) const

Bind buffer to appropriate target.

Parameters
ctxThe CGV rendering context.
typeIf cgv::render::VBT_UNDEF, will use type information as given to vertex_buffer::vertex_buffer(). Otherwise will bind according to the given type.
Warning
Pay special attention to use the same type in the matching unbind() call. Otherwise the internal type will be used again.

The explicit type specification allows interpreting the underlying buffer in different ways. One example would be the manipulation of vertex data in a compute shader. Here you would bind your vertex buffer as a simple shader storage buffer before dispatching the compute call.

Definition at line 30 of file vertex_buffer.cxx.

References cgv::render::vertex_buffer_base::type, and cgv::render::VBT_UNDEF.

◆ copy() [1/3]

bool cgv::render::vertex_buffer::copy ( const context ctx,
size_t  src_offset_in_bytes,
size_t  size_in_bytes,
vertex_buffer dst,
size_t  dst_offset_in_bytes 
) const

Copy bytes between different vertex_buffer instances.

Parameters
ctxThe CGV rendering context.
src_offset_in_bytesOffset in bytes from the beginning of the source buffer.
size_in_bytesThe amount of bytes, which will be copied.
dstThe buffer in which to copy the bytes into.
dst_offset_in_bytesOffset in bytes from the beginning of the destination buffer.
Returns
False if the buffers have no valid handles or if there was a rendering API error, true otherwise.

Definition at line 80 of file vertex_buffer.cxx.

◆ copy() [2/3]

template<typename T >
bool cgv::render::vertex_buffer::copy ( const context ctx,
size_t  src_offset_in_bytes,
T *  array_ptr,
size_t  nr_elements 
)
inline

Copy elements from the buffer into the CPU array.

Template Parameters
Tthe fundamental data type of the CPU array.
Parameters
ctxThe CGV rendering context.
src_offset_in_bytesOffset in bytes from the beginning of the source buffer.
array_ptrThe destination array in which to copy the elements.
nr_elementsHow many of the elements to copy into the destination buffer.
Returns
False if the vertex_buffer has no valid handle or if there was a rendering API error, true otherwise.

Definition at line 182 of file vertex_buffer.h.

◆ copy() [3/3]

template<typename T , typename = std::enable_if_t<std::is_class<T>::value, bool>>
bool cgv::render::vertex_buffer::copy ( const context ctx,
T &  array,
size_t  src_offset_in_bytes = 0 
)
inline

Copy elements from the buffer into the CPU array.

Template Parameters
TA generic array type.
Parameters
ctxThe CGV rendering context.
arrayThe destination array in which to copy the data.
src_offset_in_bytesOffset in bytes from the beginning of the source buffer.
Returns
False if the vertex_buffer has no valid handle or if there was a rendering API error, true otherwise.

This function will implicity infer how many bytes to copy from the dimensions of the generic array. Therefore you need to make sure, that your std::vector or cgv::math::vec is appropriately sized beforehand! Otherwise the underlying copy command might cross the boundaries of the GPU buffer and or read unrelated data.

Definition at line 199 of file vertex_buffer.h.

◆ create() [1/3]

template<typename T >
bool cgv::render::vertex_buffer::create ( const context ctx,
const T &  array 
)
inline

create vertex buffer and copy data from CPU array array into buffer memory

Definition at line 83 of file vertex_buffer.h.

◆ create() [2/3]

template<typename T >
bool cgv::render::vertex_buffer::create ( const context ctx,
const T *  array_ptr,
size_t  nr_elements 
)
inline

create vertex buffer and copy data from CPU array array_ptr into buffer memory

Definition at line 90 of file vertex_buffer.h.

◆ create() [3/3]

bool cgv::render::vertex_buffer::create ( const context ctx,
size_t  size_in_bytes 
)

◆ create_or_resize() [1/3]

template<typename T , typename = std::enable_if_t<std::is_class<T>::value, bool>>
bool cgv::render::vertex_buffer::create_or_resize ( const context ctx,
const T &  array 
)
inline

Convenience wrapper to either create() or resize() the buffer.

Template Parameters
TA generic array type.
Parameters
ctxThe CGV rendering context.
arrayThe array which will be copied into the buffer.
Returns
False if there was a rendering API error, true otherwise.

Definition at line 138 of file vertex_buffer.h.

◆ create_or_resize() [2/3]

template<typename T >
bool cgv::render::vertex_buffer::create_or_resize ( const context ctx,
const T *  array_ptr,
size_t  nr_elements 
)
inline

Convenience wrapper to either create() or resize() the buffer.

Template Parameters
Tthe fundamental data type of the CPU array.
Parameters
ctxThe CGV rendering context.
array_ptrThe array which will be copied into the buffer.
nr_elementsThe count of elements in the array.
Returns
False if there was a rendering API error, true otherwise.

Definition at line 151 of file vertex_buffer.h.

◆ create_or_resize() [3/3]

bool cgv::render::vertex_buffer::create_or_resize ( const context ctx,
size_t  size_in_bytes 
)

Convenience wrapper to either create() or resize() the buffer.

Parameters
ctxThe CGV rendering context.
size_in_bytesThe desired size in bytes of the buffer.
Returns
False if there was a rendering API error, true otherwise.

Definition at line 75 of file vertex_buffer.cxx.

References create(), is_created(), and resize().

◆ destruct()

void cgv::render::vertex_buffer::destruct ( const context ctx)

◆ get_size_in_bytes()

size_t cgv::render::vertex_buffer::get_size_in_bytes ( ) const

Retrieves the current size of the buffer in bytes.

Returns
buffer size of the last allocation command in bytes.

Definition at line 25 of file vertex_buffer.cxx.

◆ is_created()

bool cgv::render::vertex_buffer::is_created ( ) const
overridevirtual

Check whether the vertex buffer has been created.

Returns
true if the buffer has a handle from the graphics context, false otherwise.

Reimplemented from cgv::render::render_component.

Definition at line 64 of file vertex_buffer.cxx.

Referenced by create_or_resize().

◆ replace()

template<typename T >
bool cgv::render::vertex_buffer::replace ( const context ctx,
size_t  buffer_offset_in_bytes,
const T *  array_ptr,
size_t  nr_elements 
)
inline

replace part (starting at byte offset buffer_offset_in_bytes) or whole vertex buffer content from nr_elements of CPU array array_ptr

Definition at line 157 of file vertex_buffer.h.

Referenced by cgv::render::mesh_render_info::construct_element_vbo(), and cgv::render::gl::gl_context::prepare_attributes().

◆ resize() [1/3]

template<typename T , typename = std::enable_if_t<std::is_class<T>::value, bool>>
bool cgv::render::vertex_buffer::resize ( const context ctx,
const T &  array 
)
inline

resize vertex buffer and copy data from CPU array array into buffer memory

Definition at line 110 of file vertex_buffer.h.

◆ resize() [2/3]

template<typename T >
bool cgv::render::vertex_buffer::resize ( const context ctx,
const T *  array_ptr,
size_t  nr_elements 
)
inline

resize vertex buffer and copy data from CPU array array_ptr into buffer memory

Definition at line 117 of file vertex_buffer.h.

◆ resize() [3/3]

bool cgv::render::vertex_buffer::resize ( const context ctx,
size_t  size_in_bytes 
)

resize vertex buffer to size size given in bytes clearing all data

Definition at line 69 of file vertex_buffer.cxx.

Referenced by create_or_resize().

◆ unbind() [1/3]

void cgv::render::vertex_buffer::unbind ( context ctx,
unsigned  index 
) const

Unbind this buffer from the appropriate indexed binding target.

Parameters
ctxThe CGV rendering context.
indexWhich slot of the target to unbind from.

Definition at line 40 of file vertex_buffer.cxx.

References cgv::render::vertex_buffer_base::type.

◆ unbind() [2/3]

void cgv::render::vertex_buffer::unbind ( context ctx,
VertexBufferType  type,
unsigned  index 
) const

Unbind from the choosen binding target.

Parameters
ctxThe CGV rendering context.
typeIf cgv::render::VBT_UNDEF, will use type information as given to vertex_buffer::vertex_buffer(). Otherwise will unbind according to the given type.
indexWhich slot of the target to unbind from.
Warning
Pay special attention to use the same type as the preceeding bind() call. Otherwise the internal type will be used again.

Definition at line 42 of file vertex_buffer.cxx.

References cgv::render::vertex_buffer_base::type.

◆ unbind() [3/3]

void cgv::render::vertex_buffer::unbind ( context ctx,
VertexBufferType  type = VBT_UNDEF 
) const

Unbind buffer from the appropriate target.

Parameters
ctxThe CGV rendering context.
typeIf cgv::render::VBT_UNDEF, will use type information as given to vertex_buffer::vertex_buffer(). Otherwise will unbind according to the given type.
indexThe index to unbind from, on an index binding target.
Warning
Pay special attention to use the same type as the preceeding bind() call. Otherwise the internal type will be used again.

Definition at line 35 of file vertex_buffer.cxx.

References cgv::render::vertex_buffer_base::type, and cgv::render::VBT_UNDEF.

Member Data Documentation

◆ size_in_bytes

size_t cgv::render::vertex_buffer::size_in_bytes
protected

Definition at line 15 of file vertex_buffer.h.


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