class ComputeShader#

Returned by Context.compute_shader()

A Compute Shader is a Shader Stage that is used entirely for computing arbitrary information.

While it can do rendering, it is generally used for tasks not directly related to drawing.

Methods# int = 1, group_y: int = 1, group_z: int = 1) None:#
  • group_x (int) – Workgroup size x.

  • group_y (int) – Workgroup size y.

  • group_z (int) – Workgroup size z.

Run the compute shader.

run_indirect(self, buffer: Buffer, offset: int = 0) None:#

Run the compute shader indirectly from a Buffer object.

  • buffer (Buffer) – the buffer containing a single workgroup size at offset.

  • offset (int) – the offset into the buffer in bytes.

ComputeShader.get(key, default)#

Returns a Uniform, UniformBlock or StorageBlock.


default – This is the value to be returned in case key does not exist.


Get a member such as uniforms, uniform blocks and storage blocks.

# Get a uniform
uniform = program['color']

# Uniform values can be set on the returned object
# or the `__setitem__` shortcut can be used.
program['color'].value = 1.0, 1.0, 1.0, 1.0

# Still when writing byte data we need to use the `write()` method

# Set binding for a storage block (if supported)
program['DataBlock'].binding = 0
ComputeShader.__setitem__(key, value)#

Set a value of uniform or uniform block.

# Set a vec4 uniform
uniform['color'] = 1.0, 1.0, 1.0, 1.0

# Optionally we can store references to a member and set the value directly
uniform = program['color']
uniform.value = 1.0, 0.0, 0.0, 0.0

uniform = program['cameraMatrix']

# Set binding for a storage block (if supported)
program['DataBlock'].binding = 0

Yields the internal members names as strings.


for member in program:
    obj = program[member]
    print(member, obj)
    if isinstance(obj, moderngl.StorageBlock):
        print('This is a storage block member')

This includes all members such as uniforms, uniform blocks and storage blocks.


ComputeShader.ctx: Context#

The context this object belongs to

ComputeShader.glo: int#

The internal OpenGL object. This values is provided for interoperability and debug purposes only.

ComputeShader.extra: Any#

User defined data.