File: test_uniforms.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 (223 lines) | stat: -rw-r--r-- 5,987 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
214
215
216
217
218
219
220
221
222
223
from array import array
import pytest
import struct


@pytest.fixture(scope='module', autouse=True)
def res(ctx_static):
    """Global buffer we transform results into"""
    return ctx_static.buffer(reserve=1024)


def test_float_uniform(ctx, res):
    prog = ctx.program(
        vertex_shader='''
        #version 330
        uniform float Uniform;
        in float v_in;
        out float v_out;
        void main() {
            v_out = Uniform * v_in;
        }
    ''',
        varyings=['v_out']
    )
    vbo = ctx.buffer(struct.pack('f', 1.0))
    vao = ctx.simple_vertex_array(prog, vbo, 'v_in')

    prog['Uniform'].value = 7.0
    vao.transform(res)

    val = struct.unpack('f', res.read(4))[0]
    assert pytest.approx(val) == 7.0


def test_int_uniform(ctx, res):
    prog = ctx.program(
        vertex_shader='''
            #version 330
            uniform int Uniform;
            in int v_in;
            out int v_out;
            void main() {
                v_out = Uniform * v_in;
            }
        ''',
        varyings=['v_out']
    )
    vbo = ctx.buffer(struct.pack('i', 1))
    vao = ctx.simple_vertex_array(prog, vbo, 'v_in')

    prog['Uniform'].value = -2
    vao.transform(res)

    val = struct.unpack('i', res.read(4))[0]
    assert pytest.approx(val) == -2


def test_vec_uniform(ctx, res):
    prog = ctx.program(
        vertex_shader='''
            #version 330
            uniform vec3 Uniform;
            in vec3 v_in;
            out vec3 v_out;
            void main() {
                v_out = Uniform * v_in;
            }
        ''',
        varyings=['v_out']
    )

    vbo = ctx.buffer(struct.pack('3f', 1.0, 1.0, 1.0))
    vao = ctx.simple_vertex_array(prog, vbo, 'v_in')

    prog['Uniform'].value = (0.5, 1.0, 1.5)
    vao.transform(res)

    x, y, z = struct.unpack('3f', res.read(12))
    assert pytest.approx(x) == 0.5
    assert pytest.approx(y) == 1.0
    assert pytest.approx(z) == 1.5


def test_mat_uniform(ctx, res):
    prog = ctx.program(
        vertex_shader='''
            #version 330
            uniform mat2x3 Uniform;
            in float v_in;
            out mat2x3 v_out;
            void main() {
                v_out = Uniform * v_in;
            }
        ''',
        varyings=['v_out']
    )

    vbo = ctx.buffer(struct.pack('f', 1.0))
    vao = ctx.simple_vertex_array(prog, vbo, 'v_in')

    prog['Uniform'].value = (0.0, 1.0, 2.0, 3.0, 4.0, 5.0)
    vao.transform(res)

    m = struct.unpack('6f', res.read(24))
    assert pytest.approx(m[0]) == 0.0
    assert pytest.approx(m[1]) == 1.0
    assert pytest.approx(m[2]) == 2.0
    assert pytest.approx(m[3]) == 3.0
    assert pytest.approx(m[4]) == 4.0
    assert pytest.approx(m[5]) == 5.0


def test_sampler_2d(ctx):
    """RGBA8 2d sampler"""
    prog = ctx.program(
        vertex_shader="""
        #version 330
        uniform sampler2D tex;
        out float color;
        void main() {
            color = texelFetch(tex, ivec2(gl_VertexID, 0), 0).r;
        }
        """,
        varyings=['color'],
    )
    tex = ctx.texture((4, 1), 1, data=array('B', [127, 0, 255, 64]), dtype='f1')
    buff = ctx.buffer(reserve=4 * 4)
    vao = ctx.vertex_array(prog, [])
    tex.use(0)
    vao.transform(buff, vertices=4)
    data = struct.unpack('4f', buff.read())
    assert pytest.approx(data[0], abs=1.0e-3) == 0.498
    assert pytest.approx(data[1]) == 0.0
    assert pytest.approx(data[2]) == 1.0
    assert pytest.approx(data[3], abs=1.0e-3) == 0.25


def test_sampler_2d_int(ctx):
    prog = ctx.program(
        vertex_shader="""
        #version 330
        uniform isampler2D tex;
        out int color;
        void main() {
            color = texelFetch(tex, ivec2(gl_VertexID, 0), 0).r;
        }
        """,
        varyings=['color'],
    )
    tex = ctx.texture((4, 1), 1, data=array('i', [-1, 0, 1000, 4353454]), dtype='i4')
    buff = ctx.buffer(reserve=4 * 4)
    vao = ctx.vertex_array(prog, [])
    tex.use(0)
    vao.transform(buff, vertices=4)
    data = struct.unpack('4i', buff.read())
    assert data[0] == -1
    assert data[1] == 0
    assert data[2] == 1000
    assert data[3] == 4353454


def test_sampler_2d_uint(ctx):
    prog = ctx.program(
        vertex_shader="""
        #version 330
        uniform usampler2D tex;
        out uint color;
        void main() {
            color = texelFetch(tex, ivec2(gl_VertexID, 0), 0).r;
        }
        """,
        varyings=['color'],
    )
    tex = ctx.texture((4, 1), 1, data=array('I', [0, 500, 1000, 4353454]), dtype='u4')
    buff = ctx.buffer(reserve=4 * 4)
    vao = ctx.vertex_array(prog, [])
    tex.use(0)
    vao.transform(buff, vertices=4)
    data = struct.unpack('4I', buff.read())
    assert data[0] == 0
    assert data[1] == 500
    assert data[2] == 1000
    assert data[3] == 4353454


def test_sampler_2d_array(ctx):
    """RGBA8 2d array sampler. Read two pixels from two different layers"""
    prog = ctx.program(
        vertex_shader="""
        #version 330
        uniform sampler2DArray tex;
        out float color;
        void main() {
            color = texelFetch(tex, ivec3(gl_VertexID % 2, 0, gl_VertexID / 2), 0).r;
        }
        """,
        varyings=['color'],
    )
    tex = ctx.texture_array((2, 1, 2), 1, data=array('B', [127, 0, 255, 64]), dtype='f1')
    buff = ctx.buffer(reserve=4 * 4)
    vao = ctx.vertex_array(prog, [])
    tex.use(0)
    vao.transform(buff, vertices=4)
    data = struct.unpack('4f', buff.read())
    assert pytest.approx(data[0], abs=1.0e-3) == 0.498
    assert pytest.approx(data[1]) == 0.0
    assert pytest.approx(data[2]) == 1.0
    assert pytest.approx(data[3], abs=1.0e-3) == 0.25


def test_sampler_1d(ctx):
    prog = ctx.program(
        vertex_shader="""
        #version 330
        uniform sampler1D tex;
        out vec4 color;
        void main() {
            color = texelFetch(tex, 0, 0);
        }
        """,
        varyings=["color"],
    )
    assert "tex" in prog