pyglet.graphics.shader

exception ShaderException
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
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 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.

abstractmethod 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'], ...]

abstractmethod 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
__init__(*shaders: Shader) None
get_uniform_block_cls() type[UniformBlock]
Return type:

type[UniformBlock]

set_attributes(*attributes: Attribute) None

Define the attributes of the vertex shader.

On some backends like OpenGL, this is unnecessary unless you want to redefine the buffers.

Return type:

None

set_samplers(*samplers: Sampler) None
Return type:

None

set_uniform_blocks(
*uniform_blocks: UniformBlockDesc,
) None
Return type:

None

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

Create a VertexList.

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

  • mode (GeometryMode) – 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 (GLBatch | None) – Batch to add the VertexList to, or None if a Batch will not be used. Using a Batch is strongly recommended.

  • group (Group | None) – 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: GeometryMode,
indices: Sequence[int],
batch: GLBatch | None = None,
group: Group | None = None,
**data: Any,
) IndexedVertexList

Create a IndexedVertexList.

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

  • mode (GeometryMode) – 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 (GLBatch | None) – Batch to add the VertexList to, or None if a Batch will not be used. Using a Batch is strongly recommended.

  • group (Group | None) – 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: GeometryMode,
instance_attributes: dict[str, int],
batch: GLBatch | None = None,
group: Group | None = None,
**data: Any,
) InstanceVertexList
Return type:

InstanceVertexList

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

InstanceIndexedVertexList

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
property is_defined: bool

Determine if the ShaderProgram was defined and is ready for use.

property uniform_blocks: dict[str, 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()
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 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 the Uniform Buffer Object to the binding point of this Uniform Block.

Return type:

None

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.

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]]