globjects::Buffer Class Reference

Wrapper for OpenGL buffer objects. More...

`#include <globjects/include/globjects/Buffer.h>`

Inheritance diagram for globjects::Buffer:

## Public Types

enum | BindlessImplementation { BindlessImplementation::DirectStateAccessARB, BindlessImplementation::DirectStateAccessEXT, BindlessImplementation::Legacy }
Public Types inherited from globjects::Object
enum | NameImplementation { NameImplementation::DebugKHR, NameImplementation::Legacy }

## Public Member Functions

| Buffer ()
| Creates a new OpenGL buffer object. More...
virtual void | accept (ObjectVisitor &visitor) override
| Implements the visitor pattern. More...
void | bind (gl::GLenum target) const
| Binds the buffer to target. More...
void | setData (gl::GLsizeiptr size, const gl::GLvoid *data, gl::GLenum usage)
| Wraps the OpenGL function glBufferData. Creates video memory for the buffer. More...
template<typename T >
void | setData (const T &data, gl::GLenum usage)
| Convenience method to simplify passing of data in form of a POD struct. More...
template<typename T >
void | setData (const std::vector< T > &data, gl::GLenum usage)
| Convenience method to simplify passing of data in form of an std::vector. More...
template<typename T , std::size_t Count>
void | setData (const std::array< T, Count > &data, gl::GLenum usage)
| Convenience method to simplify passing of data in form of an std::array. More...
void | setSubData (gl::GLintptr offset, gl::GLsizeiptr size, const gl::GLvoid *data=nullptr)
| Wraps the OpenGL function glBufferSubData. Writes data only to a defined area of the memory. More...
template<typename T >
void | setSubData (const T &data, gl::GLintptr offset=0)
| Convenience method to simplify passing of data in form of a POD struct. More...
template<typename T >
void | setSubData (const std::vector< T > &data, gl::GLintptr offset=0)
| Convenience method to simplify passing of data in form of an std::vector. More...
template<typename T , std::size_t Count>
void | setSubData (const std::array< T, Count > &data, gl::GLintptr offset=0)
| Convenience method to simplify passing of data in form of an std::array. More...
void | setStorage (gl::GLsizeiptr size, const gl::GLvoid *data, gl::BufferStorageMask flags)
| Wraps the OpenGL function glBufferStorage. More...
template<typename T >
void | setStorage (const T &data, gl::BufferStorageMask flags)
| Convenience method to simplify passing of data in form of a POD struct. More...
template<typename T >
void | setStorage (const std::vector< T > &data, gl::BufferStorageMask flags)
| Convenience method to simplify passing of data in form of an std::vector. More...
template<typename T , std::size_t Count>
void | setStorage (const std::array< T, Count > &data, gl::BufferStorageMask flags)
| Convenience method to simplify passing of data in form of an std::array. More...
gl::GLint | getParameter (gl::GLenum pname) const
| Wraps the OpenGL function gl::glGetBufferParameter. Queries OpenGL for internal state of the buffer. More...
gl::GLint64 | getParameter64 (gl::GLenum pname) const
| Wraps the OpenGL function gl::glGetBufferParameter for 64 bit data types. Queries OpenGL for internal state of the buffer. More...
const void * | map () const
| Maps the Buffer's memory read only. More...
void * | map (gl::GLenum access)
| Maps the Buffer's memory using the internal target. More...
void * | mapRange (gl::GLintptr offset, gl::GLsizeiptr length, gl::BufferAccessMask access)
| Wraps the OpenGL function glMapBufferRange. Maps only a range of the buffers memory. More...
bool | unmap () const
| Wraps the OpenGL function glUnmapBuffer. More...
void | flushMappedRange (gl::GLintptr offset, gl::GLsizeiptr length)
| Wraps the OpenGL function glFlushMappedBufferRange. More...
void | bindBase (gl::GLenum target, gl::GLuint index) const
| Wraps the OpenGL function gl::glBindBufferBase. More...
void | bindRange (gl::GLenum target, gl::GLuint index, gl::GLintptr offset, gl::GLsizeiptr size) const
| Wraps the OpenGL function gl::glBindBufferRange. More...
void | copySubData (Buffer *buffer, gl::GLintptr readOffset, gl::GLintptr writeOffset, gl::GLsizeiptr size) const
| Wraps the OpenGL function glCopyBufferSubData. More...
void | copySubData (Buffer *buffer, gl::GLsizeiptr size) const
| Convenience method. Both readOffset and writeOffset are 0. More...
void | copyData (Buffer *buffer, gl::GLsizeiptr size, gl::GLenum usage) const
| Creates new uninitialized memory to fit size (using usage), then copies the contents of buffer to this buffer's new memory. More...
void | clearData (gl::GLenum internalformat, gl::GLenum format, gl::GLenum type, const void *data=nullptr)
| Wraps the OpenGL function gl::glClearBufferData. Clears the Buffer's data by filling it with the value in data, which has to be long enough to match format. More...
void | clearSubData (gl::GLenum internalformat, gl::GLintptr offset, gl::GLsizeiptr size, gl::GLenum format, gl::GLenum type, const void *data=nullptr)
| Wraps the OpenGL function gl::glClearBufferSubData. More...
const void * | getPointer () const
void * | getPointer ()
const void * | getPointer (gl::GLenum pname) const
void * | getPointer (gl::GLenum pname)
void | getSubData (gl::GLintptr offset, gl::GLsizeiptr size, void *data) const
| Wraps the OpenGL function glGetBufferSubData. Retrieves the contents of the buffers data store. More...
template<typename T >
std::vector< T > | getSubData (gl::GLsizeiptr count, gl::GLintptr offset=0) const
| Convenience method to simplify getting of data in form of an std::vector. More...
template<typename T , std::size_t Count>
std::array< T, Count > | getSubData (gl::GLintptr offset=0) const
| Convenience method to simplify getting of data in form of an std::array. More...
void | invalidateData () const
| Wraps the OpenGL function gl::glInvalidateBufferData. More...
void | invalidateSubData (gl::GLintptr offset, gl::GLsizeiptr length) const
| Wraps the OpenGL function gl::glInvalidateBufferSubData. More...
virtual gl::GLenum | objectType () const override
Public Member Functions inherited from globjects::Object
gl::GLuint | id () const
std::string | name () const
void | setName (const std::string &name)
bool | hasName () const
bool | isDefault () const
void | detach ()
Public Member Functions inherited from globjects::Referenced
| Referenced ()
void | ref () const
void | unref () const
int | refCounter () const
Public Member Functions inherited from globjects::HeapOnly
| HeapOnly ()
void | destroy () const

## Static Public Member Functions

static void | hintBindlessImplementation (BindlessImplementation impl)
static void | setWorkingTarget (gl::GLenum target)
| Sets the target that is used for binding buffers to call state changing OpenGL functions. This has an effect only when GL_EXT_direct_state_access is not available. Usually this target never has to be changed unless you want to ensure that a certain binding target will not be used. More...
static Buffer * | fromId (gl::GLuint id)
| Creates a buffer with an external id. This object does not own the associated OpenGL object and will not delete it in the destructor. More...
static void | unbind (gl::GLenum target)
| Unbinds a specific target, i.e. binds a 0 id to the target. More...
static void | unbind (gl::GLenum target, gl::GLuint index)
| Unbinds the buffer bound to the target and index. More...
Static Public Member Functions inherited from globjects::Object
static void | hintNameImplementation (NameImplementation impl)

## Protected Member Functions

| Buffer (IDResource *resource)
| Creates a buffer with an external id. More...
virtual | ~Buffer ()
Protected Member Functions inherited from globjects::Object
| Object (IDResource *resource)
virtual | ~Object ()
Protected Member Functions inherited from globjects::Referenced
virtual | ~Referenced ()
Protected Member Functions inherited from globjects::HeapOnly
virtual | ~HeapOnly ()

## Additional Inherited Members

Protected Attributes inherited from globjects::Object
IDResource * | m_resource
void * | m_objectLabelState

## Detailed Description

Wrapper for OpenGL buffer objects.

The Buffer class encapsulates OpenGL buffer objects. Each buffer can be bound and unbound (bind(), unbind()). To fill the buffer use setData(). To access the data of a buffer directly, you can use map(). Buffers can be used for OpenGL draw calls, which are encapsulated within drawArrays() and drawElements() on VertexArrayObject, but that doesn't guarantee that OpenGL will use this buffer for drawing. The current bound VertexArrayObject and Program will specify the render pipeline and data.

Buffer * buffer = new Buffer(gl::GL_SHADER_STORAGE_BUFFER);
buffer->setData(sizeof(glm::vec4) * 100, nullptr, gl::GL_DYNAMIC_DRAW); // allocate 100 vec4

See also
  http://www.opengl.org/wiki/Buffer_Object

## Member Enumeration Documentation

## BindlessImplementation

enum globjects::Buffer::BindlessImplementation

strong

Enumerator
DirectStateAccessARB |
DirectStateAccessEXT |
Legacy |

## Constructor & Destructor Documentation

## Buffer() [1/2]

globjects::Buffer::Buffer | ( |  | ) |

Creates a new OpenGL buffer object.

## Buffer() [2/2]

globjects::Buffer::Buffer | ( | IDResource * | resource | ) |

protected

Creates a buffer with an external id.

Parameters

  id | an external OpenGL buffer id

## ~Buffer()

virtual globjects::Buffer::~Buffer | ( |  | ) |

protected virtual

Automatically deletes the associated OpenGL buffer unless the object was created with an external id.

See also
  https://www.opengl.org/sdk/docs/man4/xhtml/gl::glDeleteBuffers.xml

## Member Function Documentation

## hintBindlessImplementation()

static void globjects::Buffer::hintBindlessImplementation | ( | BindlessImplementation | impl | ) |

static

## setWorkingTarget()

static void globjects::Buffer::setWorkingTarget | ( | gl::GLenum | target | ) |

static

Sets the target that is used for binding buffers to call state changing OpenGL functions. This has an effect only when GL_EXT_direct_state_access is not available. Usually this target never has to be changed unless you want to ensure that a certain binding target will not be used.

Parameters

  target |

## fromId()

static Buffer* globjects::Buffer::fromId | ( | gl::GLuint | id | ) |

static

Creates a buffer with an external id. This object does not own the associated OpenGL object and will not delete it in the destructor.

Parameters

  id | an external OpenGL buffer id

## accept()

virtual void globjects::Buffer::accept | ( | ObjectVisitor & | visitor | ) |

override virtual

Implements the visitor pattern.

Parameters

  visitor | The visitor on which visitBuffer will be called.

Implements globjects::Object.

## bind()

void globjects::Buffer::bind | ( | gl::GLenum | target | ) | const

Binds the buffer to target.

Parameters

  target | the target for binding

See also
  https://www.opengl.org/sdk/docs/man4/xhtml/gl::glBindBuffer.xml

## unbind() [1/2]

static void globjects::Buffer::unbind | ( | gl::GLenum | target | ) |

static

Unbinds a specific target, i.e. binds a 0 id to the target.

Parameters

  target | the target for unbinding

## unbind() [2/2]

static void globjects::Buffer::unbind | ( | gl::GLenum | target,
|  | gl::GLuint | index
| ) |  |

static

Unbinds the buffer bound to the target and index.

Parameters

  target | the target for unbinding
  index | the index for unbinding

## setData() [1/4]

void globjects::Buffer::setData | ( | gl::GLsizeiptr | size,
|  | const gl::GLvoid * | data,
|  | gl::GLenum | usage
| ) |  |

Wraps the OpenGL function glBufferData. Creates video memory for the buffer.

Parameters

  size | size of the new memory in bytes
  data | memory location containing the data. If data is nullptr, uninitalized memory will be created.
  usage | used as a performance hint on how the buffer is used

See also
  https://www.opengl.org/sdk/docs/man4/xhtml/glBufferData.xml

## setData() [2/4]

template<typename T >

void globjects::Buffer::setData | ( | const T & | data,
|  | gl::GLenum | usage
| ) |  |

Convenience method to simplify passing of data in form of a POD struct.

## setData() [3/4]

template<typename T >

void globjects::Buffer::setData | ( | const std::vector< T > & | data,
|  | gl::GLenum | usage
| ) |  |

Convenience method to simplify passing of data in form of an std::vector.

## setData() [4/4]

template<typename T , std::size_t Count>

void globjects::Buffer::setData | ( | const std::array< T, Count > & | data,
|  | gl::GLenum | usage
| ) |  |

Convenience method to simplify passing of data in form of an std::array.

## setSubData() [1/4]

void globjects::Buffer::setSubData | ( | gl::GLintptr | offset,
|  | gl::GLsizeiptr | size,
|  | const gl::GLvoid * | data = `nullptr`
| ) |  |

Wraps the OpenGL function glBufferSubData. Writes data only to a defined area of the memory.

Parameters

  size | size of memory in bytes
  offset | offset from the beginning of the buffer in bytes
  data | memory location containing the data

See also
  http://www.opengl.org/sdk/docs/man/xhtml/glBufferSubData.xml

## setSubData() [2/4]

template<typename T >

void globjects::Buffer::setSubData | ( | const T & | data,
|  | gl::GLintptr | offset = `0`
| ) |  |

Convenience method to simplify passing of data in form of a POD struct.

## setSubData() [3/4]

template<typename T >

void globjects::Buffer::setSubData | ( | const std::vector< T > & | data,
|  | gl::GLintptr | offset = `0`
| ) |  |

Convenience method to simplify passing of data in form of an std::vector.

## setSubData() [4/4]

template<typename T , std::size_t Count>

void globjects::Buffer::setSubData | ( | const std::array< T, Count > & | data,
|  | gl::GLintptr | offset = `0`
| ) |  |

Convenience method to simplify passing of data in form of an std::array.

## setStorage() [1/4]

void globjects::Buffer::setStorage | ( | gl::GLsizeiptr | size,
|  | const gl::GLvoid * | data,
|  | gl::BufferStorageMask | flags
| ) |  |

Wraps the OpenGL function glBufferStorage.

Parameters

  size | size of the new memory in bytes
  data | data memory location containing the data.
  flags | flags indicating usage

See also
  www.opengl.org/sdk/docs/man/xhtml/glBufferStorage.xml

## setStorage() [2/4]

template<typename T >

void globjects::Buffer::setStorage | ( | const T & | data,
|  | gl::BufferStorageMask | flags
| ) |  |

Convenience method to simplify passing of data in form of a POD struct.

## setStorage() [3/4]

template<typename T >

void globjects::Buffer::setStorage | ( | const std::vector< T > & | data,
|  | gl::BufferStorageMask | flags
| ) |  |

Convenience method to simplify passing of data in form of an std::vector.

## setStorage() [4/4]

template<typename T , std::size_t Count>

void globjects::Buffer::setStorage | ( | const std::array< T, Count > & | data,
|  | gl::BufferStorageMask | flags
| ) |  |

Convenience method to simplify passing of data in form of an std::array.

## getParameter()

gl::GLint globjects::Buffer::getParameter | ( | gl::GLenum | pname | ) | const

Wraps the OpenGL function gl::glGetBufferParameter. Queries OpenGL for internal state of the buffer.

Parameters

  pname | name of the parameter, e.g. gl::GL_BUFFER_SIZE

Returns
  integer value for the parameter

See also
  http://www.opengl.org/sdk/docs/man/xhtml/gl::glGetBufferParameter.xml

## getParameter64()

gl::GLint64 globjects::Buffer::getParameter64 | ( | gl::GLenum | pname | ) | const

Wraps the OpenGL function gl::glGetBufferParameter for 64 bit data types. Queries OpenGL for internal state of the buffer.

Parameters

  pname | name of the parameter, e.g. gl::GL_BUFFER_SIZE

Returns
  integer value for the parameter

See also
  http://www.opengl.org/sdk/docs/man/xhtml/gl::glGetBufferParameter.xml

## map() [1/2]

const void* globjects::Buffer::map | ( |  | ) | const

Maps the Buffer's memory read only.

Returns
  a pointer to the mapped memory

## map() [2/2]

void* globjects::Buffer::map | ( | gl::GLenum | access | ) |

Maps the Buffer's memory using the internal target.

Parameters

  access | specifies reading/writing access

Returns
  a pointer to the mapped memory

## mapRange()

void* globjects::Buffer::mapRange | ( | gl::GLintptr | offset,
|  | gl::GLsizeiptr | length,
|  | gl::BufferAccessMask | access
| ) |  |

Wraps the OpenGL function glMapBufferRange. Maps only a range of the buffers memory.

Parameters

  offset | offset from the beginning of the buffer data in bytes.
  length | length of the range in bytes.
  access | bitfield of desired access flags

Returns
  pointer to the mapped memory

See also
  http://www.opengl.org/sdk/docs/man/xhtml/glMapBufferRange.xml

## unmap()

bool globjects::Buffer::unmap | ( |  | ) | const

Wraps the OpenGL function glUnmapBuffer.

See also
  http://www.opengl.org/sdk/docs/man3/xhtml/glMapBuffer.xml

## flushMappedRange()

void globjects::Buffer::flushMappedRange | ( | gl::GLintptr | offset,
|  | gl::GLsizeiptr | length
| ) |  |

Wraps the OpenGL function glFlushMappedBufferRange.

Parameters

  offset | offset from the beginning of the buffer data in bytes.
  length | length of the range in bytes

See also
  http://www.opengl.org/sdk/docs/man/html/glFlushMappedBufferRange.xhtml

## bindBase()

void globjects::Buffer::bindBase | ( | gl::GLenum | target,
|  | gl::GLuint | index
| ) |  | const

Wraps the OpenGL function gl::glBindBufferBase.

See also
  http://www.opengl.org/sdk/docs/man/xhtml/gl::glBindBufferBase.xml

## bindRange()

void globjects::Buffer::bindRange | ( | gl::GLenum | target,
|  | gl::GLuint | index,
|  | gl::GLintptr | offset,
|  | gl::GLsizeiptr | size
| ) |  | const

Wraps the OpenGL function gl::glBindBufferRange.

See also
  http://www.opengl.org/sdk/docs/man3/xhtml/gl::glBindBufferRange.xml

## copySubData() [1/2]

void globjects::Buffer::copySubData | ( | Buffer * | buffer,
|  | gl::GLintptr | readOffset,
|  | gl::GLintptr | writeOffset,
|  | gl::GLsizeiptr | size
| ) |  | const

Wraps the OpenGL function glCopyBufferSubData.

Parameters

  readOffset | offset in bytes in read buffer
  writeOffset | offset in bytes in write buffer
  size | size of the data to be copies in bytes

See also
  http://www.opengl.org/sdk/docs/man3/xhtml/glCopyBufferSubData.xml

## copySubData() [2/2]

void globjects::Buffer::copySubData | ( | Buffer * | buffer,
|  | gl::GLsizeiptr | size
| ) |  | const

Convenience method. Both readOffset and writeOffset are 0.

## copyData()

void globjects::Buffer::copyData | ( | Buffer * | buffer,
|  | gl::GLsizeiptr | size,
|  | gl::GLenum | usage
| ) |  | const

Creates new uninitialized memory to fit size (using usage), then copies the contents of buffer to this buffer's new memory.

Parameters

  buffer | buffer from which content is copied
  size | size of the data to be copied
  usage | buffer usage

## clearData()

void globjects::Buffer::clearData | ( | gl::GLenum | internalformat,
|  | gl::GLenum | format,
|  | gl::GLenum | type,
|  | const void * | data = `nullptr`
| ) |  |

Wraps the OpenGL function gl::glClearBufferData. Clears the Buffer's data by filling it with the value in data, which has to be long enough to match format.

Parameters

  data | up to 4 components of the vector value to fill the buffer with

See also
  http://www.opengl.org/sdk/docs/man/xhtml/gl::glClearBufferData.xml

## clearSubData()

void globjects::Buffer::clearSubData | ( | gl::GLenum | internalformat,
|  | gl::GLintptr | offset,
|  | gl::GLsizeiptr | size,
|  | gl::GLenum | format,
|  | gl::GLenum | type,
|  | const void * | data = `nullptr`
| ) |  |

Wraps the OpenGL function gl::glClearBufferSubData.

Parameters

  offset | offset in bytes
  size | size in bytes

See also
  https://www.opengl.org/sdk/docs/man4/xhtml/gl::glClearBufferSubData.xml

## getPointer() [1/4]

const void* globjects::Buffer::getPointer | ( |  | ) | const

## getPointer() [2/4]

void* globjects::Buffer::getPointer | ( |  | ) |

## getPointer() [3/4]

const void* globjects::Buffer::getPointer | ( | gl::GLenum | pname | ) | const

## getPointer() [4/4]

void* globjects::Buffer::getPointer | ( | gl::GLenum | pname | ) |

## getSubData() [1/3]

void globjects::Buffer::getSubData | ( | gl::GLintptr | offset,
|  | gl::GLsizeiptr | size,
|  | void * | data
| ) |  | const

Wraps the OpenGL function glGetBufferSubData. Retrieves the contents of the buffers data store.

Parameters

  offset | offset from the beginning of the buffer in bytes
  size | size of memory in bytes
  data | memory location to store the data

See also
  http://www.opengl.org/sdk/docs/man/xhtml/glGetBufferSubData.xml

## getSubData() [2/3]

template<typename T >

std::vector<T> globjects::Buffer::getSubData | ( | gl::GLsizeiptr | count,
|  | gl::GLintptr | offset = `0`
| ) |  | const

Convenience method to simplify getting of data in form of an std::vector.

## getSubData() [3/3]

template<typename T , std::size_t Count>

std::array<T, Count> globjects::Buffer::getSubData | ( | gl::GLintptr | offset = `0` | ) | const

Convenience method to simplify getting of data in form of an std::array.

## invalidateData()

void globjects::Buffer::invalidateData | ( |  | ) | const

Wraps the OpenGL function gl::glInvalidateBufferData.

See also
  https://www.opengl.org/sdk/docs/man/html/glInvalidateBufferData.xhtml

## invalidateSubData()

void globjects::Buffer::invalidateSubData | ( | gl::GLintptr | offset,
|  | gl::GLsizeiptr | length
| ) |  | const

Wraps the OpenGL function gl::glInvalidateBufferSubData.

Parameters

  offset | offset in bytes
  size | size in bytes

See also
  https://www.opengl.org/sdk/docs/man/html/glInvalidateBufferSubData.xhtml

## objectType()

virtual gl::GLenum globjects::Buffer::objectType | ( |  | ) | const

override virtual

Implements globjects::Object.

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

- Buffer.h