pyglet.graphics.vertexbuffer
Shared GPU buffer abstractions.
AbstractBuffer models a GPU byte buffer. It intentionally has no knowledge of ctypes element formats or shader attribute data types.
Typed host-side data is provided by BufferDataStore implementations. The default store is CTypeDataStore, but other stores can be created later such as JS for WebGL or even Numpy.
- class AbstractBuffer(size: int) None
A backend GPU buffer represented as bytes.
Create a resource identity.
- key_type
alias of
BufferKey
- abstractmethod get_bytes_region(offset: int, length: int) bytes
Read a byte-range from the buffer.
- Return type:
- get_data_region(start: int, length: int) bytes
Compatibility helper for callers that expect get_data_region.
- Return type:
- abstractmethod set_bytes(data: bytes | bytearray | memoryview) None
Replace the entire buffer contents from bytes.
- Return type:
- abstractmethod set_bytes_region(
- offset: int,
- data: bytes | bytearray | memoryview,
Write bytes into a sub-range of the buffer.
- Return type:
- set_data(data: Any) None
Compatibility upload with buffer-specific input types.
The default implementation converts raw inputs to bytes. Backed buffers override this method to assign typed scalar data directly to their store. Use
set_bytesfor a common, strictly typed upload interface.- Return type:
- set_data_ptr(offset: int, length: int, ptr: _Pointer) None
Compatibility helper for callers that pass a ctypes pointer.
- Return type:
- class AttributeBufferObject(
- size: int,
- store: BufferDataStore,
A backed buffer used for shader attributes.
Create a resource identity.
- class BackedBufferObject(size: int, store: BufferDataStore) None
A GPU buffer that keeps data on the host.
Create a resource identity.
- abstractmethod commit() None
Commits all saved changes to the underlying GPU buffer before drawing.
Allows submitting multiple changes at once, rather than having to call glBufferSubData for every change.
- Return type:
- set_bytes(data: bytes | bytearray | memoryview) None
Replace the entire buffer contents from bytes.
- Return type:
- set_bytes_region(
- offset: int,
- data: bytes | bytearray | memoryview,
Write bytes into a sub-range of the buffer.
- Return type:
- set_data( ) None
Compatibility upload with buffer-specific input types.
The default implementation converts raw inputs to bytes. Backed buffers override this method to assign typed scalar data directly to their store. Use
set_bytesfor a common, strictly typed upload interface.- Return type:
- set_data_region( ) None
Compatibility region upload with the input types of
set_data.Use
set_bytes_regionfor a common, strictly typed upload interface.- Return type:
- store: BufferDataStore
- class BufferDataStore
Host-side data store used by BackedBufferObject.
- abstractmethod copy_region(dst: int, src: int, count: int) None
Copy a typed region inside the store.
- Return type:
- abstractmethod get_bytes(offset: int = 0, length: int | None = None) bytes
Get raw bytes from the store.
- Return type:
- abstractmethod set_bytes(offset: int, data: bytes | bytearray | memoryview) None
Write raw bytes into the store.
- Return type:
- class CTypeDataStore(
- size: int,
- data_type: Literal['f', 'i', 'I', 'h', 'H', 'b', 'B', 'q', 'Q', '?', 'd'],
- stride: int,
- element_count: int,
- data_ptr: int | None = None,
ctypes-based BufferDataStore.
This is the default host-side data store and keeps data in a ctypes array.
Create a ctypes-backed typed data store.
- Parameters:
size (
int) – Total byte size for the store.data_type (
Literal['f','i','I','h','H','b','B','q','Q','?','d']) – Struct-format data type code (for example"f"or"I") used to determine the underlying ctypes type and element size.stride (
int) – Byte stride for one logical element in this buffer layout.element_count (
int) – Number of scalar values per logical element.data_ptr (
int|None) – Optional pointer to externally owned memory. When provided, this store binds to that memory instead of allocating its own ctypes array.
Note
The
_owns_memoryflag indicates whether this store allocated its own backing array (True) or is bound to external memory (False). This is used to protectresize: externally backed stores cannot reallocate memory and must be rebound withrebind_external.- copy_region(dst: int, src: int, count: int) None
Copy a typed region inside the store.
- Return type:
- get_bytes(offset: int = 0, length: int | None = None) bytes
Get raw bytes from the store.
- Return type:
- get_memoryview( ) memoryview
Return a byte view of the store without copying its ctypes memory.
- Return type:
- rebind_external(size: int, data_ptr: int) None
Rebind this store to a new externally-owned memory region.
- Return type:
- set_bytes(offset: int, data: bytes | bytearray | memoryview) None
Write raw bytes into the store.
- Return type:
- class DrawIndirectBuffer(size: int) None
Backend-agnostic draw indirect buffer.
Create a resource identity.
- class IndexedBindingBuffer(size: int) None
Backend-agnostic role for buffers that support indexed binding points.
Create a resource identity.
- class IndexedBufferObject(
- size: int,
- store: BufferDataStore,
A backed buffer used for indices.
Create a resource identity.
- class MappedBufferObject(size: int) None
A GPU buffer that supports map/unmap and range mapping.
Create a resource identity.
- class PersistentBufferObject(size: int) None
A persistently mapped GPU 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.
Create a resource identity.
- class PixelBuffer(size: int) None
Backend-agnostic pixel transfer buffer.
Create a resource identity.
- class PixelPackBuffer(size: int) None
Backend-agnostic pixel pack buffer.
Create a resource identity.
- class PixelUnpackBuffer(size: int) None
Backend-agnostic pixel unpack buffer.
Create a resource identity.
- class RingBuffer(
- *,
- range_size: int,
- alignment: int = 1,
- copies_per_resource: int = 3,
- strict: bool = False,
- start_offset: int = 0,
Generic ring allocator.
Range lifetime is tracked by owners such as
FrameResourceManagerby settingBufferRange.in_use. This allocator only selects writable ranges.- acquire_writable_slice() tuple[BufferBindingSlice, BufferRange]
- Return type:
- acquire_writable_slice_from_ranges(
- ranges: list[BufferRange],
- next_range_index: int,
- Return type:
- reserve_resource_range( ) list[BufferRange]
- Return type:
- class ShaderStorageBuffer(size: int) None
Backend-agnostic shader storage buffer.
Create a resource identity.
- class TransformFeedbackBuffer( ) None
Backend-agnostic transform feedback buffer.
Create a transform feedback buffer view.
- Parameters:
size (
int) – The buffer size in bytes.data_type (
Literal['f','i','I','h','H','b','B','q','Q','?','d']) – Preferred format used by helper methodget_data(), defaults to"b"for bytes.
- class UniformBufferObject(
- context: Any,
- view_class: type[Structure],
- buffer_size: int,
- binding: int,
- *,
- alignment: int = 1,
- copies_per_resource: int = 3,
- strict: bool = False,
Backend-agnostic Uniform Buffer Object wrapper.
- bind_slice(
- binding: BufferBindingSlice,
- *,
- binding_index: int | None = None,
- Return type:
- reserve_resource_range( ) list[BufferRange]
Reserve ranges and storage capacity.
- Return type:
- upload_to_available_binding(
- resource_data: Structure,
- Return type:
- upload_to_available_binding_from_ranges(
- resource_data: Structure,
- ranges: list[BufferRange],
- next_range_index: int,
- Return type:
- use_range(range_: BufferRange) None
- Return type:
- buffer: AbstractBuffer
- property key: BufferKey
Stable pyglet identity for the wrapped buffer.
- class UniformBufferRegion(
- ubo: UniformBufferObject,
- *,
- copies_per_resource: int | None = None,
UBO region that uploads through a ring-buffered
UniformBufferObject.The region owns one CPU-side data structure and a set of GPU ranges.
General practice is to update the data structure, mark it dirty, and then commit when you are done updating for that frame.
Warning
Do not make the CPU-side data dirty after binding/committing during the same frame (for example, during the
on_drawcall), or GPU stalls may occur.Added in version 3.0.
- bind(*, binding_index: int | None = None) None
Upload dirty data if needed, then bind the current GPU range.
- Return type:
- data
- property ubo: UniformBufferObject
The underlying
UniformBufferObject.