pyglet.graphics.shader

exception MissingAttributeException

Exception for when a Shader has a missing attribute name.

exception MissingUniformException

Exception for when a Shader uniform is missing due to optimization or mispelling.

exception ShaderException
exception UnsupportedShaderType

Exception for trying to create an unsupported shader.

class Attribute

Describes an attribute in a shader.

__init__(
name: str,
location: int,
components: int,
data_type: Literal['f', 'i', 'I', 'h', 'H', 'b', 'B', 'q', 'Q', '?', 'd'],
normalize: bool = False,
divisor: int = 0,
) None

Create the attribute accessor.

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

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

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

  • data_type (Literal['f', 'i', 'I', 'h', 'H', 'b', 'B', 'q', 'Q', '?', 'd']) – Data type intended for use with the attribute.

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

  • divisor (int) – The divisor value if this is an instanced attribute.

set_data_type(
data_type: Literal['f', 'i', 'I', 'h', 'H', 'b', 'B', 'q', 'Q', '?', 'd'],
normalize: bool,
) None

Set datatype to a new format and normalization.

Must be done before this attribute is used, or may cause unexpected behavior.

Return type:

None

set_divisor(divisor: int) None
Return type:

None

c_type: Type[_SimpleCData]
element_size: int
fmt: AttributeFormat
property key: tuple[str, int, int, Literal['f', 'i', 'I', 'h', 'H', 'b', 'B', 'q', 'Q', '?', 'd'], bool, int]

Stable tuple that describes this attribute’s domain-relevant format.

location: int
class AttributeFormat

A format describing the properties of an Attribute.

__init__(
name: str,
components: int,
data_type: Literal['f', 'i', 'I', 'h', 'H', 'b', 'B', 'q', 'Q', '?', 'd'],
normalized: bool,
divisor: int,
) None
components: int
data_type: Literal['f', 'i', 'I', 'h', 'H', 'b', 'B', 'q', 'Q', '?', 'd']
divisor: int
property is_instanced: bool
name: str
normalized: bool
class AttributeView

Describes a view of the attribute at its bound buffer.

__init__(offset: int, stride: int) None
offset: int
stride: int
class ComputeShaderProgram

Backend-agnostic compute shader program container.

__init__(source: str) None
class GraphicsAttribute

A combination of format and view to give the overall attribute information.

__init__(
attribute: Attribute,
view: AttributeView,
) None
disable() None

Disable the attribute.

Return type:

None

enable() None

Enable the attribute.

Return type:

None

set_divisor() None
Return type:

None

set_pointer() None

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

Return type:

None

class PushConstants

PushConstants(stages: ‘tuple[ShaderType]’, constants: ‘list[tuple[str, GLSLDataTypes]]’)

__init__(
stages: tuple[Literal['vertex', 'fragment', 'geometry', 'compute', 'tesscontrol', 'tessevaluation']],
constants: list[tuple[str, Literal['mat4', 'vec4', 'vec3', 'vec2', 'float', 'int', 'uint', 'bool']]],
) None
constants: list[tuple[str, Literal['mat4', 'vec4', 'vec3', 'vec2', 'float', 'int', 'uint', 'bool']]]
stages: tuple[Literal['vertex', 'fragment', 'geometry', 'compute', 'tesscontrol', 'tessevaluation']]
class Sampler

Sampler(name: ‘str’, desc_set: ‘int’, binding: ‘int’, count: ‘int’ = 1, stages: ‘Sequence[ShaderType]’ = (‘fragment’,))

__init__(
name: str,
desc_set: int,
binding: int,
count: int = 1,
stages: Sequence[Literal['vertex', 'fragment', 'geometry', 'compute', 'tesscontrol', 'tessevaluation']] = ('fragment',),
) None
binding: int
count: int = 1
desc_set: int
name: str
stages: Sequence[Literal['vertex', 'fragment', 'geometry', 'compute', 'tesscontrol', 'tessevaluation']] = ('fragment',)
class Shader

Graphics shader.

Shader objects may be compiled on instantiation if OpenGL or already compiled in Vulkan. You can reuse a Shader object in multiple ShaderPrograms.

classmethod supported_shaders() tuple[Literal['vertex', 'fragment', 'geometry', 'compute', 'tesscontrol', 'tessevaluation'], ...]

Return the supported shader types for this shader class.

Return type:

tuple[Literal['vertex', 'fragment', 'geometry', 'compute', 'tesscontrol', 'tessevaluation'], ...]

static get_string_class() type[ShaderSource]

Return the proper ShaderSource class used to validate the shader.

Return type:

type[ShaderSource]

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

Initialize a shader type.

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

Backend-agnostic shader program container.

Concrete backends are responsible for compiling/linking shaders, introspecting attributes and uniforms, and providing API-specific program state management.

__init__(*shaders: _AbstractShader) None

Initialize a shader program from one or more Shader objects.

Parameters:

shaders (_AbstractShader) – One or more Shader instances to be linked into a program by the active backend. At least one shader is required.

class ShaderSource

String source of shader used during load of a Shader instance.

abstractmethod validate() str

Return the validated shader source.

Return type:

str

class TransformFeedbackShaderProgram

Backend-agnostic transform feedback shader program container.

__init__(
*shaders: _AbstractShader,
varyings: Sequence[str],
varying_buffer_type: Literal['interleaved', 'separate'] = 'separate',
) None

Initialize a transform feedback shader program.

Parameters:
  • shaders (_AbstractShader) – One or more Shader instances.

  • varyings (Sequence[str]) – Names of vertex/geometry shader output variables to capture with transform feedback.

  • varying_buffer_type (Literal['interleaved', 'separate']) – Buffer packing mode for captured outputs: "separate" (one buffer per varying) or "interleaved" (single interleaved buffer).

Notes

This class is replaced by a backend-specific implementation at import time when a supported backend is active.

property id: int
class UBOBindingManager

Manages global Uniform Block binding assignments.

__init__(max_binding_count: int) None
add_explicit_binding(
shader_program: ShaderProgram,
ub_name: str,
binding: int,
) None

Used when a uniform block has set its own binding point.

Return type:

None

binding_exists(binding: int) bool

Check if a binding index value is in use.

Return type:

bool

get_binding(
shader_program: ShaderProgram,
ub_name: str,
) int

Retrieve a global Uniform Block binding ID.

Return type:

int

get_name(binding: int) str | None

Return the uniform name associated with the binding number.

Return type:

str | None

return_binding(index: int) None
Return type:

None

property max_value: int
class UniformArrayBase

Backend-agnostic base for uniform array wrappers.

__init__(
uniform: Any,
gl_getter: Callable,
gl_setter: Callable,
gl_type: Any,
is_matrix: bool,
) None
get() UniformArrayBase
Return type:

UniformArrayBase

set(values: Sequence) None
Return type:

None

class UniformBase

Backend-agnostic base for uniform wrappers.

__init__(
*,
name: str,
uniform_type: int,
size: int,
location: Any,
program: Any,
matrix_types: tuple[int, ...],
array_wrapper_factory: Callable[[Any, Callable, Callable, Any, bool], UniformArrayBase],
) None
count
get
length
location
name
program
set
size
type
class UniformBlock
__init__(
program: ShaderProgram,
name: str,
index: int,
size: int,
binding: int,
uniforms: dict,
uniform_count: int,
) None

Initialize a uniform block for a ShaderProgram.

bind(ubo: UniformBufferObject) None

Bind a UBO to the binding point of this uniform block.

Return type:

None

create_ubo(
*,
copies_per_resource: int = 3,
alignment: int | None = None,
strict: bool = False,
) UniformBufferObject

Create a new UniformBufferObject from this uniform block.

Return type:

UniformBufferObject

create_ubo_region(
*,
copies_per_resource: int = 3,
alignment: int | None = None,
strict: bool = False,
) UniformBufferRegion

Create a ring-buffered region for updating and binding this uniform block.

Return type:

UniformBufferRegion

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.

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.

Return type:

None

binding: int
index: int
name: str
program: CallableProxyType[Callable[..., Any] | Any] | Any
size: int
uniform_count
uniforms: dict
view_cls: type[ctypes.Structure]
class UniformBlockDesc
__init__(*args, **kwargs)
bind_num: int
set_num: int
stages: tuple[Literal['vertex', 'fragment', 'geometry', 'compute', 'tesscontrol', 'tessevaluation']]
uniforms: tuple[tuple[str, str]]
get_default_shader() ShaderProgram

A default shader for rendering primitives.

Return type:

ShaderProgram