OpenGL::Sandbox::Buffer - Wrapper object for OpenGL Buffer Object


version 0.120


OpenGL Buffers represent a named block of memory on the graphics hardware which can be used for various purposes, mostly serving to deliver large quantities of data from the application to be used repeatedly by the shaders.

Creating a buffer simply reserves an ID, which can then later be bound to a buffer target and loaded with data.

Loading this module requires OpenGL 2.0 or higher.



Human-readable name of object (not GL's integer "name")


OpenGL buffer target which this buffer object should be bound to, when necessary. This attribute will be updated by "bind" if you bind to a different target.


Default data usage hint for OpenGL, when loading data into this buffer. If this is undef, the default when loading data will be GL_STATIC_DRAW. If you call load with a different usage value, it will update this attribute.


The OpenGL integer "name" of this buffer. This is a lazy-built attribute, and will call glGenBuffers the first time you access it. Use has_id to find out whether this has happened yet.


True if the id attribute is allocated.


If this field is defined at the time of the next call to "bind", this data will be immediately be loaded into the buffer with glBufferData. The field will then be cleared, to avoid holding onto large blobs of data.


If passed to the constructor, this implies an "autoload" from the file. Else it is just for informational purposes.



This is a standard Moo constructor, accepting any of the attributes, however it also accepts an alias 'data' for the "autoload" attribute.



Bind this buffer to a target, using "target" as the default. If autoload is set, this will also load that data into the buffer.

Returns $self for convenient chaining.


  $buffer->load( $data, $usage_hint );

Load data into this buffer object. You may pass a scalar, scalar ref, memory map (which is just a special scalar ref) or an OpenGL::Array. This performs an automatic glBindBuffer to the value of "target". If "target" is not defined, this dies.


  $buffer->load_at( $offset, $data );
  $buffer->load_at( $offset, $data, $src_offset, $src_length );

Load some data into the buffer at an offset. If the $src_offset and/or $src_length values are given, this will use a substring of $data. The buffer will be bound to "target" first, if it wasn't already. This performs an automatic glBindBuffer to the value of "target". If "target" is not defined, this dies. Additionally, if "load" has not been called before this dies.

Returns $self for convenient chaining.


  my $mmap= $buffer->mmap($mode);
  my $mmap= $buffer->mmap($mode, $offset, $length);

Call glMapBuffer (or glMapNamedBuffer) to memory-map the buffer into the current process. The $mode can be a GL constant of GL_READ_ONLY, GL_WRITE_ONLY, GL_READ_WRITE or a perl style mode of r, w, r+.

On OpenGL < 4.5, the buffer gets automatically mapped to its "target" to perform the mapping. OpenGL 4.5 introduced glMapNamedBuffer which avoids the need for binding.

The memory-map object persists for the live of this buffer object or until you call unmap. Until then, mmap acts as an attribute to return the previous value to you.


Release a memory-mapping of this buffer, if any. If OpenGL < 4.5, this forces the buffer to get mapped first. This happens automatically on object destruction. If automatically destroyed on OpenGL < 4.5, it genertes a warning, since messing with global state during object destruction is a bad thing.


Michael Conrad <>


This software is copyright (c) 2019 by Michael Conrad.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.