class
BufferBuffer.
Contents
Encapsulates one OpenGL buffer object and provides functions for convenient data updates.
Data updating
Default way to set or update buffer data with setData() or setSubData() is to use Corrade::
Containers::ArrayView<Vector3> data; buffer.setData(data, GL::BufferUsage::StaticDraw);
There is also overload for array-like containers from STL, such as std::
std::vector<Vector3> data; buffer.setData(data, GL::BufferUsage::StaticDraw);
Memory mapping
Buffer data can be also updated asynchronously. First you need to allocate the buffer to desired size by passing nullptr
to setData(), e.g.:
buffer.setData({nullptr, 200*sizeof(Vector3)}, GL::BufferUsage::StaticDraw);
Then you can map the buffer to client memory and operate with the memory directly. After you are done with the operation, call unmap() to unmap the buffer again. The map() functions return a view on char
array and you may want to cast it to some useful type first using Containers::
Containers::ArrayView<Vector3> data = Containers::arrayCast<Vector3>(buffer.map(0, 200*sizeof(Vector3), GL::Buffer::MapFlag::Write|GL::Buffer::MapFlag::InvalidateBuffer)); CORRADE_INTERNAL_ASSERT(data); for(Vector3& d: data) d = {/*...*/}; CORRADE_INTERNAL_ASSERT_OUTPUT(buffer.unmap());
If you are updating only a few discrete portions of the buffer, you can use MapFlag::
Containers::ArrayView<Vector3> data = Containers::arrayCast<Vector3>(buffer.map(0, 200*sizeof(Vector3), GL::Buffer::MapFlag::Write|GL::Buffer::MapFlag::FlushExplicit)); CORRADE_INTERNAL_ASSERT(data); for(std::size_t i: {7, 27, 56, 128}) { data[i] = {/*...*/}; buffer.flushMappedRange(i*sizeof(Vector3), sizeof(Vector3)); } CORRADE_INTERNAL_ASSERT_OUTPUT(buffer.unmap());
WebGL restrictions
Buffers in WebGL need to be bound only to one unique target, i.e., Buffer bound to Buffer::
GL::Buffer vertices, indices;
You have to set target hint to desired target, either in constructor or using Buffer::
GL::Buffer vertices{GL::Buffer::TargetHint::Array}, indices{GL::Buffer::TargetHint::ElementArray};
To simplify debugging, Mesh checks proper target hint when adding vertex and index buffers in WebGL.
Performance optimizations
The engine tracks currently bound buffers to avoid unnecessary calls to glBindBuffer(). If the buffer is already bound to some target, functions copy(), setData(), setSubData(), map(), mapRead(), flushMappedRange() and unmap() use that target instead of binding the buffer to some specific target. You can also use setTargetHint() to possibly reduce unnecessary rebinding. Buffer limits and implementation-defined values (such as maxUniformBindings()) are cached, so repeated queries don't result in repeated glGet() calls. See also Context::
If either ARB_
You can use functions invalidateData() and invalidateSubData() if you don't need buffer data anymore to avoid unnecessary memory operations performed by OpenGL in order to preserve the data. If running on OpenGL ES or extension ARB_
Base classes
- class AbstractObject
- Base for all OpenGL objects.
Public types
- enum class TargetHint: GLenum { Array = GL_ARRAY_BUFFER, AtomicCounter = GL_ATOMIC_COUNTER_BUFFER, CopyRead = GL_COPY_READ_BUFFER, CopyWrite = GL_COPY_WRITE_BUFFER, DispatchIndirect = GL_DISPATCH_INDIRECT_BUFFER, DrawIndirect = GL_DRAW_INDIRECT_BUFFER, ElementArray = GL_ELEMENT_ARRAY_BUFFER, PixelPack = GL_PIXEL_PACK_BUFFER, PixelUnpack = GL_PIXEL_UNPACK_BUFFER, ShaderStorage = GL_SHADER_STORAGE_BUFFER, Texture = GL_TEXTURE_BUFFER, TransformFeedback = GL_TRANSFORM_FEEDBACK_BUFFER, Uniform = GL_UNIFORM_BUFFER }
- Buffer target.
- enum class Target: GLenum { AtomicCounter = GL_ATOMIC_COUNTER_BUFFER, ShaderStorage = GL_SHADER_STORAGE_BUFFER, Uniform = GL_UNIFORM_BUFFER }
- Buffer binding target.
- enum class MapAccess: GLenum { ReadOnly = GL_READ_ONLY, WriteOnly = GL_WRITE_ONLY, ReadWrite = GL_READ_WRITE }
- Memory mapping access.
- enum class MapFlag: GLbitfield { Read = GL_MAP_READ_BIT, Write = GL_MAP_WRITE_BIT, InvalidateBuffer = GL_MAP_INVALIDATE_BUFFER_BIT, InvalidateRange = GL_MAP_INVALIDATE_RANGE_BIT, FlushExplicit = GL_MAP_FLUSH_EXPLICIT_BIT, Unsynchronized = GL_MAP_UNSYNCHRONIZED_BIT }
- Memory mapping flag.
-
using MapFlags = Containers::
EnumSet<MapFlag> - Memory mapping flags.
Public static functions
- static auto minMapAlignment() -> Int
- Minimal supported mapping alignment.
- static auto maxAtomicCounterBindings() -> Int
- Max supported atomic counter buffer binding count.
- static auto maxShaderStorageBindings() -> Int
- Max supported shader storage buffer binding count.
- static auto uniformOffsetAlignment() -> Int
- Alignment of uniform buffer binding offset.
- static auto shaderStorageOffsetAlignment() -> Int
- Alignment of shader storage buffer binding offset.
- static auto maxUniformBindings() -> Int
- Max supported uniform buffer binding count.
- static void unbind(Target target, UnsignedInt index)
- Unbind any buffer from given indexed target.
-
static void unbind(Target target,
UnsignedInt firstIndex,
std::
size_t count) - Unbind given range of indexed targets.
-
static void bind(Target target,
UnsignedInt firstIndex,
std::
initializer_list<std:: tuple<Buffer*, GLintptr, GLsizeiptr>> buffers) - Bind ranges of buffers to given range of indexed targets.
-
static void bind(Target target,
UnsignedInt firstIndex,
std::
initializer_list<Buffer*> buffers) - Bind buffers to given range of indexed targets.
- static void copy(Buffer& read, Buffer& write, GLintptr readOffset, GLintptr writeOffset, GLsizeiptr size)
- Copy one buffer to another.
-
static auto wrap(GLuint id,
TargetHint targetHint = TargetHint::
Array, ObjectFlags flags = {}) -> Buffer - Wrap existing OpenGL buffer object.
- static auto wrap(GLuint id, ObjectFlags flags) -> Buffer
Constructors, destructors, conversion operators
-
Buffer(TargetHint targetHint = TargetHint::
Array) explicit - Constructor.
- Buffer(NoCreateT) explicit noexcept
- Construct without creating the underlying OpenGL object.
- Buffer(const Buffer&) deleted
- Copying is not allowed.
- Buffer(Buffer&& other) noexcept
- Move constructor.
- ~Buffer()
- Destructor.
Public functions
- auto operator=(const Buffer&) -> Buffer& deleted
- Copying is not allowed.
- auto operator=(Buffer&& other) -> Buffer& noexcept
- Move assignment.
- auto id() const -> GLuint
- OpenGL buffer ID.
- auto release() -> GLuint
- Release OpenGL object.
-
auto label() -> std::
string - Buffer label.
-
auto setLabel(const std::
string& label) -> Buffer& - Set buffer label.
-
template<std::auto setLabel(const char(&label)[size]) -> Buffer&
size_t size> - auto targetHint() const -> TargetHint
- Target hint.
- auto setTargetHint(TargetHint hint) -> Buffer&
- Set target hint.
- auto bind(Target target, UnsignedInt index, GLintptr offset, GLsizeiptr size) -> Buffer&
- Bind buffer range to given binding index.
- auto bind(Target target, UnsignedInt index) -> Buffer&
- Bind buffer to given binding index.
- auto size() -> Int
- Buffer size.
-
auto data() -> Containers::
Array<char> - Buffer data.
-
template<class T>auto data() -> Containers::
Array<T> deprecated - Buffer data.
-
auto subData(GLintptr offset,
GLsizeiptr size) -> Containers::
Array<char> - Buffer subdata.
-
template<class T>auto subData(GLintptr offset, GLsizeiptr size) -> Containers::
Array<T> deprecated - Buffer subdata.
-
auto setData(Containers::
ArrayView<const void> data, BufferUsage usage) -> Buffer& - Set buffer data.
-
template<class T>auto setData(const std::
vector<T>& data, BufferUsage usage) -> Buffer& -
template<std::auto setData(const std::
size_t size, class T> array<T, size>& data, BufferUsage usage) -> Buffer& -
auto setSubData(GLintptr offset,
Containers::
ArrayView<const void> data) -> Buffer& - Set buffer subdata.
-
template<class T>auto setSubData(GLintptr offset, const std::
vector<T>& data) -> Buffer& -
template<std::auto setSubData(GLintptr offset, const std::
size_t size, class T> array<T, size>& data) -> Buffer& - auto invalidateData() -> Buffer&
- Invalidate buffer data.
- auto invalidateSubData(GLintptr offset, GLsizeiptr length) -> Buffer&
- Invalidate buffer subdata.
- auto map(MapAccess access) -> char*
- Map buffer to client memory.
- auto mapRead() -> const char*
- Map buffer read-only to client memory.
-
template<class T>auto map(MapAccess access) -> T* deprecated
-
auto map(GLintptr offset,
GLsizeiptr length,
MapFlags flags) -> Containers::
ArrayView<char> - Map buffer to client memory.
-
auto mapRead(GLintptr offset,
GLsizeiptr length,
MapFlags flags = {}) -> Containers::
ArrayView<const char> - Map buffer read-only to client memory.
-
template<class T>auto map(GLintptr offset, GLsizeiptr length, MapFlags flags) -> T* deprecated
- auto flushMappedRange(GLintptr offset, GLsizeiptr length) -> Buffer&
- Flush mapped range.
- auto unmap() -> bool
- Unmap buffer.
Enum documentation
enum class Magnum:: GL:: Buffer:: TargetHint: GLenum
Buffer target.
Enumerators | |
---|---|
Array |
Used for storing vertex attributes. |
AtomicCounter |
Used for storing atomic counters. |
CopyRead |
Source for copies. See copy(). |
CopyWrite |
Target for copies. See copy(). |
DispatchIndirect |
Indirect compute dispatch commands. |
DrawIndirect |
Used for supplying arguments for indirect drawing. |
ElementArray |
Used for storing vertex indices. |
PixelPack |
Target for pixel pack operations. |
PixelUnpack |
Source for texture update operations. |
ShaderStorage |
Used for shader storage. |
Texture |
Source for texel fetches. See BufferTexture. |
TransformFeedback |
Target for transform feedback. |
Uniform |
Used for storing uniforms. |
enum class Magnum:: GL:: Buffer:: Target: GLenum
Buffer binding target.
Enumerators | |
---|---|
AtomicCounter |
Atomic counter binding |
ShaderStorage |
Shader storage binding |
Uniform |
Uniform binding |
enum class Magnum:: GL:: Buffer:: MapFlag: GLbitfield
Memory mapping flag.
Enumerators | |
---|---|
Read |
Map buffer for reading. |
Write |
Map buffer for writing. |
InvalidateBuffer |
Previous contents of the entire buffer may be discarded. May not be used in combination with MapFlag:: |
InvalidateRange |
Previous contents of mapped range may be discarded. May not be used in combination with MapFlag:: |
FlushExplicit |
Only one or more discrete subranges of the mapping will be modified. See flushMappedRange() for more information. May only be used in conjuction with MapFlag:: |
Unsynchronized |
No pending operations on the buffer should be synchronized before mapping. |
Typedef documentation
typedef Containers:: EnumSet<MapFlag> Magnum:: GL:: Buffer:: MapFlags
Memory mapping flags.
Function documentation
static Int Magnum:: GL:: Buffer:: minMapAlignment()
Minimal supported mapping alignment.
The result is cached, repeated queries don't result in repeated OpenGL calls. If extension ARB_1
.
static Int Magnum:: GL:: Buffer:: maxAtomicCounterBindings()
Max supported atomic counter buffer binding count.
The result is cached, repeated queries don't result in repeated OpenGL calls. If neither extension ARB_0
.
static Int Magnum:: GL:: Buffer:: maxShaderStorageBindings()
Max supported shader storage buffer binding count.
The result is cached, repeated queries don't result in repeated OpenGL calls. If neither extension ARB_0
.
static Int Magnum:: GL:: Buffer:: uniformOffsetAlignment()
Alignment of uniform buffer binding offset.
The result is cached, repeated queries don't result in repeated OpenGL calls. If extension ARB_1
.
static Int Magnum:: GL:: Buffer:: shaderStorageOffsetAlignment()
Alignment of shader storage buffer binding offset.
The result is cached, repeated queries don't result in repeated OpenGL calls. If neither extension ARB_1
.
static Int Magnum:: GL:: Buffer:: maxUniformBindings()
Max supported uniform buffer binding count.
The result is cached, repeated queries don't result in repeated OpenGL calls. If extension ARB_0
.
static void Magnum:: GL:: Buffer:: unbind(Target target,
UnsignedInt index)
Unbind any buffer from given indexed target.
The index
parameter must respect limits for given target
.
static void Magnum:: GL:: Buffer:: unbind(Target target,
UnsignedInt firstIndex,
std:: size_t count)
Unbind given range of indexed targets.
Unbinds all buffers in given target in range . The range of indices must respect limits for given target
. If ARB_
static void Magnum:: GL:: Buffer:: bind(Target target,
UnsignedInt firstIndex,
std:: initializer_list<std:: tuple<Buffer*, GLintptr, GLsizeiptr>> buffers)
Bind ranges of buffers to given range of indexed targets.
Binds first buffer in the list to firstIndex
, second to firstIndex + 1
etc. Second parameter is offset, third is size. If any buffer is nullptr
, given indexed target is unbound. The range of indices must respect limits for given target
. The offsets must respect alignment, which is 4 bytes for Target::
static void Magnum:: GL:: Buffer:: bind(Target target,
UnsignedInt firstIndex,
std:: initializer_list<Buffer*> buffers)
Bind buffers to given range of indexed targets.
Binds first buffer in the list to firstIndex
, second to firstIndex + 1
etc. If any buffer is nullptr
, given indexed target is unbound. The range of indices must respect limits for given target
. All the buffers must have allocated data store. If ARB_
static void Magnum:: GL:: Buffer:: copy(Buffer& read,
Buffer& write,
GLintptr readOffset,
GLintptr writeOffset,
GLsizeiptr size)
Copy one buffer to another.
Parameters | |
---|---|
read | Buffer from which to read |
write | Buffer to which to copy |
readOffset | Offset in the read buffer |
writeOffset | Offset in the write buffer |
size | Data size |
If neither ARB_read
buffer is bound for reading and write
buffer is bound for writing before the copy is performed (if not already).
static Buffer Magnum:: GL:: Buffer:: wrap(GLuint id,
TargetHint targetHint = TargetHint:: Array,
ObjectFlags flags = {})
Wrap existing OpenGL buffer object.
Parameters | |
---|---|
id | OpenGL buffer ID |
targetHint | Target hint, see setTargetHint() for more information |
flags | Object creation flags |
The id
is expected to be of an existing OpenGL buffer object. Unlike buffer created using constructor, the OpenGL object is by default not deleted on destruction, use flags
for different behavior.
static Buffer Magnum:: GL:: Buffer:: wrap(GLuint id,
ObjectFlags flags)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Magnum:: GL:: Buffer:: Buffer(TargetHint targetHint = TargetHint:: Array) explicit
Constructor.
Parameters | |
---|---|
targetHint | Target hint, see setTargetHint() for more information |
Creates new OpenGL buffer object. If ARB_
Magnum:: GL:: Buffer:: Buffer(NoCreateT) explicit noexcept
Construct without creating the underlying OpenGL object.
The constructed instance is equivalent to moved-from state. Useful in cases where you will overwrite the instance later anyway. Move another object over it to make it useful.
This function can be safely used for constructing (and later destructing) objects even without any OpenGL context being active.
GLuint Magnum:: GL:: Buffer:: release()
Release OpenGL object.
Releases ownership of OpenGL buffer object and returns its ID so it is not deleted on destruction. The internal state is then equivalent to moved-from state.
std:: string Magnum:: GL:: Buffer:: label()
Buffer label.
The result is not cached, repeated queries will result in repeated OpenGL calls. If OpenGL 4.3 / OpenGL ES 3.2 is not supported and neither KHR_
Buffer& Magnum:: GL:: Buffer:: setLabel(const std:: string& label)
Set buffer label.
Returns | Reference to self (for method chaining) |
---|
Default is empty string. If OpenGL 4.3 / OpenGL ES 3.2 is not supported and neither KHR_
template<std:: size_t size>
Buffer& Magnum:: GL:: Buffer:: setLabel(const char(&label)[size])
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Buffer& Magnum:: GL:: Buffer:: setTargetHint(TargetHint hint)
Set target hint.
Returns | Reference to self (for method chaining) |
---|
If neither ARB_
Buffer& Magnum:: GL:: Buffer:: bind(Target target,
UnsignedInt index,
GLintptr offset,
GLsizeiptr size)
Bind buffer range to given binding index.
The index
parameter must respect limits for given target
. The offset
parameter must respect alignment, which is 4 bytes for Target::
Buffer& Magnum:: GL:: Buffer:: bind(Target target,
UnsignedInt index)
Bind buffer to given binding index.
The index
parameter must respect limits for given target
. The buffer must have allocated data store.
Int Magnum:: GL:: Buffer:: size()
Buffer size.
If neither ARB_
Containers:: Array<char> Magnum:: GL:: Buffer:: data()
Buffer data.
Returns data of whole buffer. If neither ARB_
template<class T>
Containers:: Array<T> Magnum:: GL:: Buffer:: data()
Buffer data.
Containers:: Array<char> Magnum:: GL:: Buffer:: subData(GLintptr offset,
GLsizeiptr size)
Buffer subdata.
Parameters | |
---|---|
offset | Byte offset in the buffer |
size | Data size in bytes |
Returns data of given buffer portion. If neither ARB_
template<class T>
Containers:: Array<T> Magnum:: GL:: Buffer:: subData(GLintptr offset,
GLsizeiptr size)
Buffer subdata.
Buffer& Magnum:: GL:: Buffer:: setData(Containers:: ArrayView<const void> data,
BufferUsage usage)
Set buffer data.
Parameters | |
---|---|
data | Data |
usage | Buffer usage |
Returns | Reference to self (for method chaining) |
If neither ARB_
template<class T>
Buffer& Magnum:: GL:: Buffer:: setData(const std:: vector<T>& data,
BufferUsage usage)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<std:: size_t size, class T>
Buffer& Magnum:: GL:: Buffer:: setData(const std:: array<T, size>& data,
BufferUsage usage)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Buffer& Magnum:: GL:: Buffer:: setSubData(GLintptr offset,
Containers:: ArrayView<const void> data)
Set buffer subdata.
Parameters | |
---|---|
offset | Byte offset in the buffer |
data | Data |
Returns | Reference to self (for method chaining) |
If neither ARB_
template<class T>
Buffer& Magnum:: GL:: Buffer:: setSubData(GLintptr offset,
const std:: vector<T>& data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<std:: size_t size, class T>
Buffer& Magnum:: GL:: Buffer:: setSubData(GLintptr offset,
const std:: array<T, size>& data)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Buffer& Magnum:: GL:: Buffer:: invalidateData()
Invalidate buffer data.
Returns | Reference to self (for method chaining) |
---|
If running on OpenGL ES or extension ARB_
Buffer& Magnum:: GL:: Buffer:: invalidateSubData(GLintptr offset,
GLsizeiptr length)
Invalidate buffer subdata.
Parameters | |
---|---|
offset | Byte offset into the buffer |
length | Length of the invalidated range |
Returns | Reference to self (for method chaining) |
If running on OpenGL ES or extension ARB_
char* Magnum:: GL:: Buffer:: map(MapAccess access)
Map buffer to client memory.
Parameters | |
---|---|
access | Access |
Returns | Pointer to mapped buffer data or nullptr on error |
If neither ARB_
const char* Magnum:: GL:: Buffer:: mapRead()
Map buffer read-only to client memory.
Equivalent to map() with MapAccess::
Containers:: ArrayView<char> Magnum:: GL:: Buffer:: map(GLintptr offset,
GLsizeiptr length,
MapFlags flags)
Map buffer to client memory.
Parameters | |
---|---|
offset | Byte offset into the buffer |
length | Length of the mapped memory in bytes |
flags | Flags. At least MapFlag:: |
Returns | Sized view to buffer data or nullptr on error |
If neither ARB_
Containers:: ArrayView<const char> Magnum:: GL:: Buffer:: mapRead(GLintptr offset,
GLsizeiptr length,
MapFlags flags = {})
Map buffer read-only to client memory.
Equivalent to map() with MapFlag::
Buffer& Magnum:: GL:: Buffer:: flushMappedRange(GLintptr offset,
GLsizeiptr length)
Flush mapped range.
Parameters | |
---|---|
offset | Byte offset relative to start of mapped range |
length | Length of the flushed memory in bytes |
Returns | Reference to self (for method chaining) |
Flushes specified subsection of mapped range. Use only if you called map() with MapFlag::
If neither ARB_
bool Magnum:: GL:: Buffer:: unmap()
Unmap buffer.
Returns | False if the data have become corrupt during the time the buffer was mapped (e.g. after screen was resized), true otherwise. |
---|
Unmaps buffer previously mapped with map() / mapRead(), invalidating the pointer returned by these functions. If neither ARB_
Debug& operator<<(Debug& debug,
Buffer:: TargetHint value)
Debug output operator.
Debug& operator<<(Debug& debug,
Buffer:: Target value)
Debug output operator.