pyglet.graphics.vertexbuffer

OpenGL Buffer Objects.

BufferObject and a BackedBufferObject are provided. The first is a lightweight abstraction over an OpenGL buffer, as created with glGenBuffers. The backed buffer object is similar, but provides a full mirror of the data in CPU memory. This allows for delayed uploading of changes to GPU memory, which can improve performance is some cases.

class AbstractBuffer

Abstract buffer of byte data.

Class Variables:
  • size (int) – Size of buffer, in bytes

  • ptr (int) – Memory offset of the buffer, as used by the glVertexPointer family of functions

abstract bind(target: int = 34962) None

Bind this buffer to an OpenGL target.

Return type:

None

abstract delete() None

Delete this buffer, reducing system resource usage.

Return type:

None

abstract map() CTypesPointer[ctypes.c_ubyte]

Map the entire buffer into system memory.

The mapped region must be subsequently unmapped with unmap before performing any other operations on the buffer.

Returns:

Pointer to the mapped block in memory

resize(size: int) None

Resize the buffer to a new size.

Parameters:

size (int) – New size of the buffer, in bytes

Return type:

None

abstract set_data(data: Sequence[int] | _Pointer) None

Set the entire contents of the buffer.

Parameters:

data (Sequence[int] | _Pointer) – The byte array to set.

Return type:

None

abstract set_data_region(
data: Sequence[int] | _Pointer,
start: int,
length: int,
) None

Set part of the buffer contents.

Parameters:
  • data (Sequence[int] | _Pointer) – The byte array of data to set

  • start (int) – Offset to start replacing data

  • length (int) – Length of region to replace

Return type:

None

abstract unbind() None

Reset the buffer’s OpenGL target.

Return type:

None

abstract unmap() None

Unmap a previously mapped memory block.

Return type:

None

ptr: int = 0
size: int = 0
class AttributeBufferObject

A backed buffer used for Shader Program attributes.

__init__(
size: int,
attribute: Attribute,
) None

Initialize the BufferObject with the given size and draw usage.

Buffer data is cleared on creation.

class BackedBufferObject

A buffer with system-memory backed store.

Updates to the data via set_data and set_data_region will be held in system memory until commit is called. The advantage is that fewer OpenGL calls are needed, which can increase performance at the expense of system memory.

__init__(
size: int,
c_type: Type[_SimpleCData],
stride: int,
count: int,
usage: int = 35048,
) None

Initialize the BufferObject with the given size and draw usage.

Buffer data is cleared on creation.

commit() None

Commits all saved changes to the underlying buffer before drawing.

Allows submitting multiple changes at once, rather than having to call glBufferSubData for every change.

Return type:

None

get_region(
start: int,
count: int,
) Array[Type[_SimpleCData]]
Return type:

Array[Type[_SimpleCData]]

invalidate() None
Return type:

None

invalidate_region(start: int, count: int) None
Return type:

None

resize(size: int) None

Resize the buffer to a new size.

Parameters:

size (int) – New size of the buffer, in bytes

Return type:

None

set_region(
start: int,
count: int,
data: Sequence[float],
) None
Return type:

None

count: int
ctype: Type[_SimpleCData]
data: Type[_SimpleCData]
data_ptr: int
stride: int
class BufferObject

Lightweight representation of an OpenGL Buffer Object.

The data in the buffer is not replicated in any system memory (unless it is done so by the video driver). While this can reduce system memory usage, performing multiple small updates to the buffer can be relatively slow. The target of the buffer is GL_ARRAY_BUFFER internally to avoid accidentally overriding other states when altering the buffer contents. The intended target can be set when binding the buffer.

__init__(size: int, usage: int = 35048) None

Initialize the BufferObject with the given size and draw usage.

Buffer data is cleared on creation.

bind(target: int = 34962) None

Bind this buffer to an OpenGL target.

Return type:

None

bind_to_index_buffer() None

Binds this buffer as an index buffer on the active vertex array.

Return type:

None

delete() None

Delete this buffer, reducing system resource usage.

Return type:

None

invalidate() None
Return type:

None

map() CTypesPointer[ctypes.c_byte]

Map the entire buffer into system memory.

The mapped region must be subsequently unmapped with unmap before performing any other operations on the buffer.

Returns:

Pointer to the mapped block in memory

map_range(
start: int,
size: int,
ptr_type: type[_ctypes._Pointer],
) _Pointer
Return type:

_Pointer

resize(size: int) None

Resize the buffer to a new size.

Parameters:

size (int) – New size of the buffer, in bytes

Return type:

None

set_data(data: Sequence[int] | _Pointer) None

Set the entire contents of the buffer.

Parameters:

data (Sequence[int] | _Pointer) – The byte array to set.

Return type:

None

set_data_region(
data: Sequence[int] | _Pointer,
start: int,
length: int,
) None

Set part of the buffer contents.

Parameters:
  • data (Sequence[int] | _Pointer) – The byte array of data to set

  • start (int) – Offset to start replacing data

  • length (int) – Length of region to replace

Return type:

None

unbind() None

Reset the buffer’s OpenGL target.

Return type:

None

unmap() None

Unmap a previously mapped memory block.

Return type:

None

id: int
usage: int
class IndexedBufferObject

A backed buffer used for indices.

__init__(
size: int,
c_type: Type[_SimpleCData],
stride: int,
count: int,
usage: int = 35048,
) None

Initialize the BufferObject with the given size and draw usage.

Buffer data is cleared on creation.

class PersistentBufferObject

A persistently mapped buffer.

Available in OpenGL 4.3+ contexts. Persistently mapped buffers are mapped one time on creation, and can be updated at any time without the need to map or unmap.

__init__(size, attribute, vao)
bind(target=34962)

Bind this buffer to an OpenGL target.

delete() None

Delete this buffer, reducing system resource usage.

Return type:

None

get_region(start, count)
invalidate()
invalidate_region(start, count)
map() CTypesPointer[ctypes.c_ubyte]

Map the entire buffer into system memory.

The mapped region must be subsequently unmapped with unmap before performing any other operations on the buffer.

Returns:

Pointer to the mapped block in memory

map_range(start, size, ptr_type, flags=2)
resize(size)

Resize the buffer to a new size.

Parameters:

size – New size of the buffer, in bytes

set_data(
data: Sequence[int] | _Pointer,
) None

Set the entire contents of the buffer.

Parameters:

data (Sequence[int] | _Pointer) – The byte array to set.

Return type:

None

set_data_region(
data: Sequence[int] | _Pointer,
start: int,
length: int,
) None

Set part of the buffer contents.

Parameters:
  • data (Sequence[int] | _Pointer) – The byte array of data to set

  • start (int) – Offset to start replacing data

  • length (int) – Length of region to replace

Return type:

None

set_region(start, count, data)
sub_data()
unbind()

Reset the buffer’s OpenGL target.

unmap() None

Unmap a previously mapped memory block.

Return type:

None