File: compute_render_to_texture.py

package info (click to toggle)
python-moderngl-window 2.4.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 69,220 kB
  • sloc: python: 11,387; makefile: 21
file content (44 lines) | stat: -rw-r--r-- 1,498 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import moderngl as mgl
from pathlib import Path
import moderngl_window as mglw
from moderngl_window import geometry


class ComputeRenderToTexture(mglw.WindowConfig):
    """Simple example rendering to a texture with a compute shader"""
    title = "Render Texture Using Compute Shader"
    resource_dir = (Path(__file__) / '../../resources').resolve()
    gl_version = 4, 3
    aspect_ratio = 1.0

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.compute_shader = self.load_compute_shader('programs/compute/render_to_texture.glsl')
        self.compute_shader['destTex'] = 0
        self.texture_program = self.load_program('programs/texture.glsl')
        self.quad_fs = geometry.quad_fs()
        self.texture = self.ctx.texture((256, 256), 4)
        self.texture.filter = mgl.NEAREST, mgl.NEAREST

    def render(self, time, frame_time):
        self.ctx.clear(0.3, 0.3, 0.3)

        w, h = self.texture.size
        gw, gh = 16, 16
        nx, ny, nz = int(w / gw), int(h / gh), 1

        try:
            self.compute_shader['time'] = time
        except Exception:
            pass
        # Automatically binds as a GL_R32F / r32f (read from the texture)
        self.texture.bind_to_image(0, read=False, write=True)
        self.compute_shader.run(nx, ny, nz)

        # Render texture
        self.texture.use(location=0)
        self.quad_fs.render(self.texture_program)


if __name__ == '__main__':
    mglw.run_window_config(ComputeRenderToTexture)