File: test_compute_shader.py

package info (click to toggle)
python-moderngl 5.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,700 kB
  • sloc: python: 15,758; cpp: 14,665; makefile: 14
file content (213 lines) | stat: -rw-r--r-- 6,109 bytes parent folder | download
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from array import array
import struct
import pytest


def test_1(ctx):
    if ctx.version_code < 430:
        pytest.skip('compute shaders not supported')

    compute_shader = ctx.compute_shader('''
        #version 430

        layout (local_size_x = 1, local_size_y = 1) in;

        layout (std430, binding = 1) buffer Input {
            float v1[4];
        };

        layout (std430, binding = 2) buffer Output {
            float v2[4];
        };

        void main() {
            v2[0] = v1[3];
            v2[1] = v1[2];
            v2[2] = v1[1];
            v2[3] = v1[0];
        }
    ''')

    buf1 = ctx.buffer(struct.pack('4f', 1.0, 2.0, 3.0, 4.0))
    buf2 = ctx.buffer(struct.pack('4f', 0.0, 0.0, 0.0, 0.0))

    buf1.bind_to_storage_buffer(1)
    buf2.bind_to_storage_buffer(2)

    compute_shader.run()

    a, b, c, d = struct.unpack('4f', buf2.read())

    assert pytest.approx(a) == 4.0
    assert pytest.approx(b) == 3.0
    assert pytest.approx(c) == 2.0
    assert pytest.approx(d) == 1.0


def test_image(ctx):
    if ctx.version_code < 430:
        pytest.skip('compute shaders not supported')

    texture = ctx.texture((100, 100), 4)
    texture.bind_to_image(0, read=True, write=True)
    assert ctx.error == 'GL_NO_ERROR'
    texture.release()


def test_image_float(ctx):
    if ctx.version_code < 430:
        pytest.skip('compute shaders not supported')

    texture = ctx.texture((100, 100), 4, dtype='f4')
    texture.bind_to_image(0, read=True, write=True)
    assert ctx.error == 'GL_NO_ERROR'
    texture.release()


def test_image_wrong_format(ctx):
    if ctx.version_code < 430:
        pytest.skip('compute shaders not supported')

    texture = ctx.texture((100, 100), 4)
    texture.bind_to_image(0, read=True, write=True, format=13371337)
    assert ctx.error == 'GL_INVALID_VALUE'
    texture.release()


def test_3d_image(ctx):
    if ctx.version_code < 430:
        pytest.skip('compute shaders not supported')

    program = ctx.compute_shader(
        """
        #version 430

        layout(local_size_x=4, local_size_y=4, local_size_z=4) in;

        layout(rgba32f, binding=0) uniform image3D img_in;
        layout(rgba32f, binding=1) uniform image3D img_out;

        void main() {
            vec4 fragment = imageLoad(img_in, ivec3(gl_LocalInvocationID.xyz));
            imageStore(img_out, ivec3(gl_LocalInvocationID.xyz), fragment);
        }
        """
    )
    tex_in = ctx.texture3d((4, 4, 4), 4, data=array('f', [v for v in range(4 * 4 * 4 * 4)]), dtype="f4")
    tex_out = ctx.texture3d((4, 4, 4), 4, dtype="f4")

    tex_in.bind_to_image(0, read=True, write=False)
    tex_out.bind_to_image(1, read=False, write=True)
    program.run(group_x=1)
    assert ctx.error == 'GL_NO_ERROR'

    data_in = struct.unpack("256f", tex_in.read())
    data_out = struct.unpack("256f", tex_out.read())

    assert data_in == data_out


def test_texture_array_image(ctx):
    if ctx.version_code < 430:
        pytest.skip('compute shaders not supported')

    program = ctx.compute_shader(
        """
        #version 430

        layout(local_size_x=4, local_size_y=4, local_size_z=4) in;

        layout(rgba32f, binding=0) uniform image2DArray img_in;
        layout(rgba32f, binding=1) uniform image2DArray img_out;

        void main() {
            vec4 fragment = imageLoad(img_in, ivec3(gl_LocalInvocationID.xyz));
            imageStore(img_out, ivec3(gl_LocalInvocationID.xyz), fragment);
        }
        """
    )
    tex_in = ctx.texture_array((4, 4, 4), 4, data=array('f', [v for v in range(4 * 4 * 4 * 4)]), dtype="f4")
    tex_out = ctx.texture_array((4, 4, 4), 4, dtype="f4")

    tex_in.bind_to_image(0, read=True, write=False)
    tex_out.bind_to_image(1, read=False, write=True)
    program.run(group_x=1)

    data_in = struct.unpack("256f", tex_in.read())
    data_out = struct.unpack("256f", tex_out.read())

    assert data_in == data_out


def test_texture_cube_image(ctx):
    if ctx.version_code < 430:
        pytest.skip('compute shaders not supported')

    program = ctx.compute_shader(
        """
        #version 450

        layout(local_size_x=4, local_size_y=4) in;

        layout(rgba8, binding=0) uniform imageCube img_in;
        layout(rgba8, binding=1) uniform imageCube img_out;

        void main() {
            for (int i = 0; i < 6; i++) {
                vec4 fragment = imageLoad(img_in, ivec3(gl_LocalInvocationID.xy, i));
                imageStore(img_out, ivec3(gl_LocalInvocationID.xy, i), fragment);
            }
        }
        """
    )
    data = ([1] * 64) + ([2] * 64) + ([3] * 64) + ([4] * 64) + ([5] * 64) + ([6] * 64)
    tex_in = ctx.texture_cube((4, 4), 4, data=array('B', data), dtype="f1")
    tex_out = ctx.texture_cube((4, 4), 4, data=array('B', [0] * 64 * 6), dtype="f1")
    tex_in.bind_to_image(0, read=True, write=False)
    tex_out.bind_to_image(1, read=False, write=True)
    program.run(group_x=1)

    for face in range(0, 6):
        assert struct.unpack("64B", tex_in.read(face)) == struct.unpack("64B", tex_out.read(face))


def test_ssbo_binding(ctx):
    if ctx.version_code < 430:
        pytest.skip('compute shaders not supported')

    program = ctx.compute_shader(
        """
        #version 430

        layout(local_size_x=1, local_size_y=1, local_size_z=1) in;

        layout(std430, binding=0) buffer ssbo_in {
            float data[4];
        } in_data;

        layout(std430, binding=1) buffer ssbo_out {
            float data[4];
        } out_data;

        void main() {
            out_data.data[gl_LocalInvocationIndex] = in_data.data[gl_LocalInvocationIndex];
        }
        """
    )
    ssbo_in = program['ssbo_in']
    ssbo_out = program['ssbo_out']

    assert ssbo_in.name == 'ssbo_in'
    assert ssbo_out.name == 'ssbo_out'

    assert ssbo_in.binding == 0
    assert ssbo_in.value == 0
    assert ssbo_out.binding == 1
    assert ssbo_out.value == 1

    ssbo_in.binding = 2
    ssbo_out.binding = 3
    assert ssbo_in.binding == 2
    assert ssbo_in.value == 2
    assert ssbo_out.binding == 3
    assert ssbo_out.value == 3