pyglet.graphics.shader

exception ShaderException
class Attribute

Abstract accessor for an attribute in a mapped buffer.

__init__(
name: str,
location: int,
components: int,
data_type: Literal['f', 'i', 'I', 'h', 'H', 'b', 'B', 'q', 'Q', '?', 'd'],
normalize: bool = False,
instance: bool = False,
) 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

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

disable() None

Disable the attribute.

Return type:

None

enable() None

Enable the attribute.

Return type:

None

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

Set data type to a new format.

Must be done before you render anything, or may cause unexpected behavior.

This is done to normalize certain data types after a shader is loaded or inspected.

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

c_type: Type[_SimpleCData]
count: int
element_size: int
instance: bool
location: int
name: str
normalize: bool
stride: int
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 ShaderBase

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.

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

Initialize a shader type.

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

type: Literal['vertex', 'fragment', 'geometry', 'compute', 'tesscontrol', 'tessevaluation']
class ShaderProgramBase
__init__(*shaders: ShaderBase) None
get_uniform_block_cls() type[pyglet.graphics.shader.UniformBlockBase]
Return type:

type[UniformBlockBase]

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: Batch = None,
group: Group = 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 (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: GeometryMode,
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 (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 (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: GeometryMode,
instance_attributes: Sequence[str],
batch: Batch = None,
group: Group = None,
**data: Any,
) VertexList
Return type:

VertexList

vertex_list_instanced_indexed(
count: int,
mode: GeometryMode,
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
property is_defined: bool

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

property uniform_blocks: dict[str, pyglet.graphics.shader.UniformBlockBase]

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.

abstract validate() str

Return the validated shader source.

Return type:

str

class UniformBlockBase
__init__(
program: ShaderProgramBase,
name: str,
index: int,
size: int,
binding: int,
uniforms: dict,
uniform_count: int,
) None

Initialize a uniform block for a ShaderProgram.

bind(ubo: UniformBufferObjectBase) None

Bind the Uniform Buffer Object to the binding point of this Uniform Block.

Return type:

None

create_ubo() UniformBufferObjectBase

Create a new UniformBufferObject from this uniform block.

Return type:

UniformBufferObjectBase

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
view_cls: type[ctypes.Structure] | None
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]]
class UniformBufferObjectBase
abstract read() bytes
Return type:

bytes