Debian Codegraph @

examples/opengl/compute_shader.py

raw symbols SCIP
1 """Minimal Compute Shader example.
2
3 This example creates a Compute Shader from source,
4 attached a texture to it, and writes into the texture.
5 The texture is then saved to disk as a .png file.
6 """
7
8 import pyglet
9
10 from pyglet.gl import GL_RGBA32F, GL_ALL_BARRIER_BITS
11
12
13 compute_src = """#version 430 core
14 layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
15
16 layout(rgba32f) uniform image2D img_output;
17
18 void main() {
19 vec4 value = vec4(0.0, 0.0, 0.0, 1.0);
20 ivec2 texel_coord = ivec2(gl_GlobalInvocationID.xy);
21 value.r = float(texel_coord.x)/(gl_NumWorkGroups.x);
22 value.g = float(texel_coord.y)/(gl_NumWorkGroups.y);
23
24 imageStore(img_output, texel_coord, value);
25 }
26 """
27
28 program = pyglet.graphics.shader.ComputeShaderProgram(compute_src)
29
30 # Create an RGBA32F Texture that we can bind to the ShaderProgram.
31 out_texture = pyglet.image.Texture.create(540, 540, internalformat=GL_RGBA32F)
32
33 # The image binding unit in the shader needs to be set for the texture.
34 # The uniform set for the image can be used. In most cases this will be 0 unless explicitly
35 # changed via layout qualifier of layout(binding=X, rgba32f)
36 output_binding = program['img_output']
37 out_texture.bind_image_texture(unit=output_binding)
38
39
40 with program:
41 program.dispatch(out_texture.width, out_texture.height, 1, barrier=GL_ALL_BARRIER_BITS)
42
43
44 out_texture.save('compute_output.png')
45 print("Saved 'compute_output.png'")