pyglet.graphics.shader

exception ShaderException
class Attribute

Abstract accessor for an attribute in a mapped buffer.

__init__(
name: str,
location: int,
count: int,
gl_type: int,
normalize: bool,
instance: bool,
) None

Create the attribute accessor.

Parameters:
  • name (str) – Name of the vertex attribute.

  • location (int) – Location (index) of the vertex attribute.

  • count (int) – Number of components in the attribute.

  • gl_type (int) – OpenGL type enumerant; for example, GL_FLOAT

  • normalize (bool) – True if OpenGL should normalize the values

  • instance (bool) – True if OpenGL should treat this as an instanced attribute.

enable() None

Enable the attribute.

Return type:

None

get_region(
buffer: AttributeBufferObject,
start: int,
count: int,
) Array[Type[_SimpleCData]]

Map a buffer region using this attribute as an accessor.

The returned region consists of a contiguous array of component data elements. For example, if this attribute uses 3 floats per vertex, and the count parameter is 4, the number of floats mapped will be 3 * 4 = 12.

Parameters:
  • buffer (AttributeBufferObject) – The buffer to map.

  • start (int) – Offset of the first vertex to map.

  • count (int) – Number of vertices to map

Return type:

Array[Type[_SimpleCData]]

set_divisor() None
Return type:

None

set_pointer(ptr: int) None

Setup this attribute to point to the currently bound buffer at the given offset.

offset should be based on the currently bound buffer’s ptr member.

Parameters:

ptr (int) – Pointer offset to the currently bound buffer for this attribute.

Return type:

None

set_region(
buffer: AttributeBufferObject,
start: int,
count: int,
data: Sequence[float],
) None

Set the data over a region of the buffer.

Parameters:
  • buffer (AttributeBufferObject) – The buffer to map.

  • start (int) – Offset of the first vertex to map.

  • count (int) – Number of vertices to map

  • data (Sequence[float]) – A sequence of data components.

Return type:

None

c_type: Type[_SimpleCData]
count: int
element_size: int
gl_type: int
instance: bool
location: int
name: str
normalize: bool
stride: int
class ComputeShaderProgram

OpenGL Compute Shader Program.

__init__(source: str) None

Create an OpenGL ComputeShaderProgram from source.

bind() None
Return type:

None

delete() None
Return type:

None

static dispatch(
x: int = 1,
y: int = 1,
z: int = 1,
barrier: int = 4294967295,
) None

Launch one or more compute work groups.

The ComputeShaderProgram should be active (bound) before calling this method. The x, y, and z parameters specify the number of local work groups that will be dispatched in the X, Y and Z dimensions.

Return type:

None

static stop() None
Return type:

None

static unbind() None
Return type:

None

use() None
Return type:

None

property id: int
max_shared_memory_size: int
max_work_group_count: tuple[int, int, int]
max_work_group_invocations: int
max_work_group_size: tuple[int, int, int]
property uniform_blocks: dict[str, pyglet.graphics.shader.UniformBlock]
property uniforms: dict[str, dict[str, Any]]
class Shader

OpenGL shader.

Shader objects are compiled on instantiation. You can reuse a Shader object in multiple ShaderPrograms.

__init__(
source_string: str,
shader_type: Literal['vertex', 'fragment', 'geometry', 'compute', 'tesscontrol', 'tessevaluation'],
) None

Initialize a shader type.

Parameters:
  • source_string (str) – A string containing the source of your shader program.

  • shader_type (Literal['vertex', 'fragment', 'geometry', 'compute', 'tesscontrol', 'tessevaluation']) –

    A string containing the type of shader program:

    • 'vertex'

    • 'fragment'

    • 'geometry'

    • 'compute'

    • 'tesscontrol'

    • 'tessevaluation'

delete() None

Deletes the shader.

This cannot be undone.

Return type:

None

property id: int
type: Literal['vertex', 'fragment', 'geometry', 'compute', 'tesscontrol', 'tessevaluation']
class ShaderProgram

OpenGL shader program.

__init__(*shaders: Shader) None

Initialize the ShaderProgram using at least two Shader instances.

bind() None
Return type:

None

delete() None
Return type:

None

static stop() None
Return type:

None

static unbind() None
Return type:

None

use() None
Return type:

None

vertex_list(
count: int,
mode: int,
batch: Batch = None,
group: Group = None,
**data: Any,
) VertexList

Create a VertexList.

Parameters:
  • count (int) – The number of vertices in the list.

  • mode (int) – OpenGL drawing mode enumeration; for example, one of GL_POINTS, GL_LINES, GL_TRIANGLES, etc. This determines how the list is drawn in the given batch.

  • batch (Batch) – Batch to add the VertexList to, or None if a Batch will not be used. Using a Batch is strongly recommended.

  • group (Group) – Group to add the VertexList to, or None if no group is required.

  • data (Any) – Attribute formats and initial data for the vertex list.

Return type:

VertexList

vertex_list_indexed(
count: int,
mode: int,
indices: Sequence[int],
batch: Batch = None,
group: Group = None,
**data: Any,
) IndexedVertexList

Create a IndexedVertexList.

Parameters:
  • count (int) – The number of vertices in the list.

  • mode (int) – OpenGL drawing mode enumeration; for example, one of GL_POINTS, GL_LINES, GL_TRIANGLES, etc. This determines how the list is drawn in the given batch.

  • indices (Sequence[int]) – Sequence of integers giving indices into the vertex list.

  • batch (Batch) – Batch to add the VertexList to, or None if a Batch will not be used. Using a Batch is strongly recommended.

  • group (Group) – Group to add the VertexList to, or None if no group is required.

  • data (Any) – Attribute formats and initial data for the vertex list.

Return type:

IndexedVertexList

vertex_list_instanced(
count: int,
mode: int,
instance_attributes: Sequence[str],
batch: Batch = None,
group: Group = None,
**data: Any,
) VertexList
Return type:

VertexList

vertex_list_instanced_indexed(
count: int,
mode: int,
indices: Sequence[int],
instance_attributes: Sequence[str],
batch: Batch = None,
group: Group = None,
**data: Any,
) IndexedVertexList
Return type:

IndexedVertexList

property attributes: dict[str, Any]

Attribute metadata dictionary.

This property returns a dictionary containing metadata of all Attributes that were introspected in this ShaderProgram. Modifying this dictionary has no effect.

property id: int
property uniform_blocks: dict[str, pyglet.graphics.shader.UniformBlock]

A dictionary of introspected UniformBlocks.

This property returns a dictionary of UniformBlock instances. They can be accessed by name. For example:

block = my_shader_program.uniform_blocks['WindowBlock']
ubo = block.create_ubo()
property uniforms: dict[str, Any]

Uniform metadata dictionary.

This property returns a dictionary containing metadata of all Uniforms that were introspected in this ShaderProgram. Modifying this dictionary has no effect. To set or get a uniform, the uniform name is used as a key on the ShaderProgram instance. For example:

my_shader_program[uniform_name] = 123
value = my_shader_program[uniform_name]
class ShaderSource

GLSL source container for making source parsing simpler.

We support locating out attributes and applying #defines values.

Note

We do assume the source is neat enough to be parsed this way and doesn’t contain several statements in one line.

__init__(source: str, source_type: c_uint) None

Create a shader source wrapper.

validate() str

Return the validated shader source.

Return type:

str

class UniformBlock
__init__(
program: ShaderProgram,
name: str,
index: int,
size: int,
binding: int,
uniforms: dict[int, tuple[str, Union[Type[ctypes.c_int], Type[ctypes.c_float], Type[ctypes.c_ubyte], int], int, int]],
uniform_count: int,
) None

Initialize a uniform block for a ShaderProgram.

create_ubo() UniformBufferObject

Create a new UniformBufferObject from this uniform block.

Return type:

UniformBufferObject

set_binding(binding: int) None

Rebind the Uniform Block to a new binding index number.

This only affects the program this Uniform Block is derived from.

Binding value of 0 is reserved for the Pyglet’s internal uniform block named WindowBlock. :rtype: None

Warning

By setting a binding manually, the user is expected to manage all Uniform Block bindings for all shader programs manually. Since the internal global ID’s will be unaware of changes set by this function, collisions may occur if you use a lower number.

Note

You must call create_ubo to get another Uniform Buffer Object after calling this, as the previous buffers are still bound to the old binding point.

binding: int
index: int
name: str
program: CallableProxyType[Callable[..., Any] | Any] | Any
size: int
uniform_count
uniforms: dict[int, tuple[str, GLDataType, int, int]]
view_cls: type[Structure] | None
class UniformBufferObject
__init__(
view_class: type[_ctypes.Structure],
buffer_size: int,
binding: int,
) None

Initialize the Uniform Buffer Object with the specified Structure.

bind() None

Bind this buffer to the bind point established by the UniformBuffer parent.

Return type:

None

read() bytes

Read the byte contents of the buffer.

Return type:

bytes

binding: int
buffer: BufferObject
property id: int

The buffer ID associated with this UBO.

view: Structure
get_maximum_binding_count() int

The maximum binding value that can be used for this hardware.

Return type:

int