Program#
- class moderngl.Program#
A Program object represents fully processed executable code in the OpenGL Shading Language, for one or more Shader stages.
In ModernGL, a Program object can be assigned to
VertexArray
objects. The VertexArray object is capable of binding the Program object once theVertexArray.render()
orVertexArray.transform()
is called.Program objects has no method called
use()
, VertexArrays encapsulate this mechanism.A Program object cannot be instantiated directly, it requires a context. Use
Context.program()
to create one.Uniform buffers can be bound using
Buffer.bind_to_uniform_block()
or can be set individually. For more complex binding yielding higher performance consider usingmoderngl.Scope
.
Create#
- Context.program(*, vertex_shader: str, fragment_shader: Optional[str] = None, geometry_shader: Optional[str] = None, tess_control_shader: Optional[str] = None, tess_evaluation_shader: Optional[str] = None, varyings: Tuple[str, ...] = (), fragment_outputs: Optional[Dict[str, int]] = None, varyings_capture_mode: str = 'interleaved') moderngl.Program
Create a
Program
object.The
varyings
are only used when a transform program is created to specify the names of the output varyings to capture in the output buffer.fragment_outputs
can be used to programmatically map named fragment shader outputs to a framebuffer attachment numbers. This can also be done by usinglayout(location=N)
in the fragment shader.- Parameters
vertex_shader (str) – The vertex shader source.
fragment_shader (str) – The fragment shader source.
geometry_shader (str) – The geometry shader source.
tess_control_shader (str) – The tessellation control shader source.
tess_evaluation_shader (str) – The tessellation evaluation shader source.
varyings (list) – A list of varyings.
fragment_outputs (dict) – A dictionary of fragment outputs.
- Returns
Program
object
Methods#
- Program.get(key: str, default: Any) Union[moderngl.Uniform, moderngl.UniformBlock, moderngl.Subroutine, moderngl.Attribute, moderngl.Varying] #
Returns a Uniform, UniformBlock, Subroutine, Attribute or Varying.
- Parameters
default – This is the value to be returned in case key does not exist.
- Returns
- Program.__getitem__(key: str) Union[moderngl.Uniform, moderngl.UniformBlock, moderngl.Subroutine, moderngl.Attribute, moderngl.Varying] #
Get a member such as uniforms, uniform blocks, subroutines, attributes and varyings by name.
# 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 program['color'].write(buffer)
- Program.__setitem__(key: str, value: Any) None #
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'] uniform.write(camera_matrix)
- Program.__iter__() Generator[str, None, None] #
Yields the internal members names as strings.
This includes all members such as uniforms, attributes etc.
Example:
# Print member information for name in program: member = program[name] print(name, type(member), member)
Output:
vert <class 'moderngl.program_members.attribute.Attribute'> <Attribute: 0> vert_color <class 'moderngl.program_members.attribute.Attribute'> <Attribute: 1> gl_InstanceID <class 'moderngl.program_members.attribute.Attribute'> <Attribute: -1> rotation <class 'moderngl.program_members.uniform.Uniform'> <Uniform: 0> scale <class 'moderngl.program_members.uniform.Uniform'> <Uniform: 1>
We can filter on member type if needed:
for name in prog: member = prog[name] if isinstance(member, moderngl.Uniform): print('Uniform', name, member)
or a less verbose version using dict comprehensions:
uniforms = {name: self.prog[name] for name in self.prog if isinstance(self.prog[name], moderngl.Uniform)} print(uniforms)
Output:
{'rotation': <Uniform: 0>, 'scale': <Uniform: 1>}
- Program.__eq__(value, /)#
Return self==value.
- Program.release() None #
Release the ModernGL object.
Attributes#
- Program.geometry_input: int#
The geometry input primitive.
The GeometryShader’s input primitive if the GeometryShader exists. The geometry input primitive will be used for validation. (from
layout(input_primitive) in;
)This can only be
POINTS
,LINES
,LINES_ADJACENCY
,TRIANGLES
,TRIANGLE_ADJACENCY
.- Type
int
- Program.geometry_output: int#
The geometry output primitive.
The GeometryShader’s output primitive if the GeometryShader exists. This can only be
POINTS
,LINE_STRIP
andTRIANGLE_STRIP
(fromlayout(output_primitive, max_vertices = vert_count) out;
)- Type
int
- Program.geometry_vertices: int#
The maximum number of vertices that.
the geometry shader will output. (from
layout(output_primitive, max_vertices = vert_count) out;
)- Type
int
- Program.subroutines: Tuple[str, ...]#
The subroutine uniforms.
- Type
tuple
- Program.glo: int#
The internal OpenGL object.
This values is provided for debug purposes only.
- Type
int
- Program.mglo: Any#
Internal representation for debug purposes only.
- Program.extra: Any#
Any - Attribute for storing user defined objects
- Program.is_transform: bool#
If this is a tranform program (no fragment shader).
- Type
bool
- Program.ctx: moderngl.Context#
The context this object belongs to
Examples#
A simple program designed for rendering
1my_render_program = ctx.program(
2 vertex_shader='''
3 #version 330
4
5 in vec2 vert;
6
7 void main() {
8 gl_Position = vec4(vert, 0.0, 1.0);
9 }
10 ''',
11 fragment_shader='''
12 #version 330
13
14 out vec4 color;
15
16 void main() {
17 color = vec4(0.3, 0.5, 1.0, 1.0);
18 }
19 ''',
20)
A simple program designed for transforming
1my_transform_program = ctx.program(
2 vertex_shader='''
3 #version 330
4
5 in vec4 vert;
6 out float vert_length;
7
8 void main() {
9 vert_length = length(vert);
10 }
11 ''',
12 varyings=['vert_length']
13)