File: test_glir.py

package info (click to toggle)
python-vispy 0.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,840 kB
  • sloc: python: 59,436; javascript: 6,800; makefile: 69; sh: 6
file content (307 lines) | stat: -rw-r--r-- 9,644 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# -*- coding: utf-8 -*-

import json
import tempfile
from unittest import mock

from vispy import config
from vispy.app import Canvas
from vispy.gloo import glir
from vispy.testing import requires_application, requires_pyopengl, run_tests_if_main

import numpy as np


def test_queue():
    q = glir.GlirQueue()
    parser = glir.GlirParser()

    # Test adding commands and clear
    N = 5
    for i in range(N):
        q.command('FOO', 'BAR', i)
    cmds = q.clear()
    for i in range(N):
        assert cmds[i] == ('FOO', 'BAR', i)

    # Test filter 1
    cmds1 = [('DATA', 1), ('SIZE', 1), ('FOO', 1), ('SIZE', 1), ('FOO', 1),
             ('DATA', 1), ('DATA', 1)]
    cmds2 = [c[0] for c in q._shared._filter(cmds1, parser)]
    assert cmds2 == ['FOO', 'SIZE', 'FOO', 'DATA', 'DATA']

    # Test filter 2
    cmds1 = [('DATA', 1), ('SIZE', 1), ('FOO', 1), ('SIZE', 2), ('SIZE', 2),
             ('DATA', 2), ('SIZE', 1), ('FOO', 1), ('DATA', 1), ('DATA', 1)]
    cmds2 = q._shared._filter(cmds1, parser)
    assert cmds2 == [('FOO', 1), ('SIZE', 2), ('DATA', 2), ('SIZE', 1),
                     ('FOO', 1), ('DATA', 1), ('DATA', 1)]

    # Define shader
    shader1 = """
        precision highp float;uniform mediump vec4 u_foo;uniform vec4 u_bar;
        """.strip().replace(';', ';\n')
    # Convert for desktop
    shader2 = glir.convert_shader('desktop', shader1)
    assert 'highp' not in shader2
    assert 'mediump' not in shader2
    assert 'precision' not in shader2

    # Convert for es2
    shader3 = glir.convert_shader('es2', shader2)
    # make sure precision float is still in the shader
    # it may not be the first (precision int might be there)
    assert 'precision highp float;' in shader3
    # precisions must come before code
    assert shader3.startswith('precision')

    # Define shader with version number
    shader4 = """
        #version 100; precision highp float;uniform mediump vec4 u_foo;uniform vec4 u_bar;
        """.strip().replace(';', ';\n')
    shader5 = glir.convert_shader('es2', shader4)
    assert 'precision highp float;' in shader5
    # make sure that precision is first (version is removed)
    # precisions must come before code
    assert shader3.startswith('precision')


@requires_application()
def test_log_parser():
    """Test GLIR log parsing"""
    glir_file = tempfile.TemporaryFile(mode='r+')

    config.update(glir_file=glir_file)
    with Canvas() as c:
        c.context.set_clear_color('white')
        c.context.clear()

    glir_file.seek(0)
    lines = glir_file.read().split(',\n')

    assert lines[0][0] == '['
    lines[0] = lines[0][1:]

    assert lines[-1][-1] == ']'
    lines[-1] = lines[-1][:-1]

    i = 0

    # The FBO argument may be anything based on the backend.
    expected = json.dumps(['CURRENT', 0, 1])
    assert len(lines[i]) >= len(expected)
    expected = expected.split('1')
    assert lines[i].startswith(expected[0])
    assert lines[i].endswith(expected[1])
    assert int(lines[i][len(expected[0]):-len(expected[1])]) is not None

    # The 'CURRENT' command may have been called multiple times
    while lines[i].startswith('["CURRENT",'):
        i += 1
        if lines[i] == json.dumps(['FUNC', 'colorMask', False, False, False, True]):
            # Qt workaround, see #2040
            i += 4

    assert lines[i] == json.dumps(['FUNC', 'clearColor', 1.0, 1.0, 1.0, 1.0])
    i += 1
    assert lines[i] == json.dumps(['FUNC', 'clear', 17664])
    i += 1
    assert lines[i] == json.dumps(['FUNC', 'finish'])
    i += 1

    config.update(glir_file='')
    glir_file.close()


@requires_application()
def test_capabilities():
    """Test GLIR capability reporting"""
    with Canvas() as c:
        capabilities = c.context.shared.parser.capabilities
        assert capabilities['max_texture_size'] is not None
        assert capabilities['gl_version'] != 'unknown'


@requires_pyopengl()
@mock.patch('vispy.gloo.glir._check_pyopengl_3D')
@mock.patch('vispy.gloo.glir.gl')
def test_texture1d_alignment(gl, check3d):
    """Test that textures set unpack alignment properly.

    See https://github.com/vispy/vispy/pull/1758

    """
    from ..glir import GlirTexture1D
    check3d.return_value = check3d
    t = GlirTexture1D(mock.MagicMock(), 3)

    shape = (393, 1)
    t.set_size(shape, 'luminance', 'luminance')
    t.set_data((0,), np.zeros(shape, np.float32))
    # number of elements doesn't matter, only data type
    gl.glPixelStorei.assert_not_called()
    gl.glPixelStorei.reset_mock()

    # now with bytes
    t.set_data((0,), np.zeros(shape, np.uint8))
    gl.glPixelStorei.assert_has_calls([
        mock.call(gl.GL_UNPACK_ALIGNMENT, 1),
        mock.call(gl.GL_UNPACK_ALIGNMENT, 4),
    ])
    gl.glPixelStorei.reset_mock()

    # different shape
    shape = (394, 1)
    t.set_size(shape, 'luminance', 'luminance')
    t.set_data((0,), np.zeros(shape, np.float32))
    # number of elements doesn't matter, only data type
    gl.glPixelStorei.assert_not_called()
    gl.glPixelStorei.reset_mock()

    t.set_data((0,), np.zeros(shape, np.uint8))
    gl.glPixelStorei.assert_has_calls([
        mock.call(gl.GL_UNPACK_ALIGNMENT, 1),
        mock.call(gl.GL_UNPACK_ALIGNMENT, 4),
    ])
    gl.glPixelStorei.reset_mock()


@requires_pyopengl()
@mock.patch('vispy.gloo.glir._check_pyopengl_3D')
@mock.patch('vispy.gloo.glir.gl')
def test_texture2d_alignment(gl, check3d):
    """Test that textures set unpack alignment properly.

    See https://github.com/vispy/vispy/pull/1758

    """
    from ..glir import GlirTexture2D
    check3d.return_value = gl
    t = GlirTexture2D(mock.MagicMock(), 3)

    shape = (296, 393, 1)
    t.set_size(shape, 'luminance', 'luminance')
    t.set_data((0, 0), np.zeros(shape, np.float32))
    # alignment should have been 4 which is the default
    gl.glPixelStorei.assert_not_called()
    gl.glPixelStorei.reset_mock()

    # now with bytes
    t.set_data((0, 0), np.zeros(shape, np.uint8))
    gl.glPixelStorei.assert_has_calls([
        mock.call(gl.GL_UNPACK_ALIGNMENT, 1),
        mock.call(gl.GL_UNPACK_ALIGNMENT, 4),
    ])
    gl.glPixelStorei.reset_mock()

    # different shape
    shape = (296, 394, 1)
    t.set_size(shape, 'luminance', 'luminance')
    t.set_data((0, 0), np.zeros(shape, np.float32))
    gl.glPixelStorei.assert_has_calls([
        mock.call(gl.GL_UNPACK_ALIGNMENT, 8),
        mock.call(gl.GL_UNPACK_ALIGNMENT, 4),
    ])
    gl.glPixelStorei.reset_mock()

    t.set_data((0, 0), np.zeros(shape, np.uint8))
    gl.glPixelStorei.assert_has_calls([
        mock.call(gl.GL_UNPACK_ALIGNMENT, 2),
        mock.call(gl.GL_UNPACK_ALIGNMENT, 4),
    ])
    gl.glPixelStorei.reset_mock()


@requires_pyopengl()
@mock.patch('vispy.gloo.glir._check_pyopengl_3D')
@mock.patch('vispy.gloo.glir.gl')
def test_texture3d_alignment(gl, check3d):
    """Test that textures set unpack alignment properly.

    See https://github.com/vispy/vispy/pull/1758

    """
    from ..glir import GlirTexture3D
    check3d.return_value = gl
    t = GlirTexture3D(mock.MagicMock(), 3)

    shape = (68, 296, 393, 1)
    t.set_size(shape, 'luminance', 'luminance')
    t.set_data((0, 0, 0), np.zeros(shape, np.float32))
    # alignment should have been 4 which is the default
    gl.glPixelStorei.assert_not_called()
    gl.glPixelStorei.reset_mock()

    # now with bytes
    t.set_data((0, 0, 0), np.zeros(shape, np.uint8))
    gl.glPixelStorei.assert_has_calls([
        mock.call(gl.GL_UNPACK_ALIGNMENT, 1),
        mock.call(gl.GL_UNPACK_ALIGNMENT, 4),
    ])
    gl.glPixelStorei.reset_mock()

    # different shape
    shape = (68, 296, 394, 1)
    t.set_size(shape, 'luminance', 'luminance')
    t.set_data((0, 0, 0), np.zeros(shape, np.float32))
    gl.glPixelStorei.assert_has_calls([
        mock.call(gl.GL_UNPACK_ALIGNMENT, 8),
        mock.call(gl.GL_UNPACK_ALIGNMENT, 4),
    ])
    gl.glPixelStorei.reset_mock()

    t.set_data((0, 0, 0), np.zeros(shape, np.uint8))
    gl.glPixelStorei.assert_has_calls([
        mock.call(gl.GL_UNPACK_ALIGNMENT, 2),
        mock.call(gl.GL_UNPACK_ALIGNMENT, 4),
    ])
    gl.glPixelStorei.reset_mock()


@requires_pyopengl()
@mock.patch('vispy.gloo.glir._check_pyopengl_3D')
@mock.patch('vispy.gloo.glir.gl')
def test_texture_cube_alignment(gl, check3d):
    """Test that textures set unpack alignment properly.

    See https://github.com/vispy/vispy/pull/1758

    """
    from ..glir import GlirTextureCube
    check3d.return_value = gl
    t = GlirTextureCube(mock.MagicMock(), 3)

    shape = (68, 296, 393, 1)
    t.set_size(shape, 'luminance', 'luminance')
    t.set_data((0, 0, 0), np.zeros(shape, np.float32))
    # alignment should have been 4 which is the default
    gl.glPixelStorei.assert_not_called()
    gl.glPixelStorei.reset_mock()

    # now with bytes
    t.set_data((0, 0, 0), np.zeros(shape, np.uint8))
    gl.glPixelStorei.assert_has_calls([
        mock.call(gl.GL_UNPACK_ALIGNMENT, 1),
        mock.call(gl.GL_UNPACK_ALIGNMENT, 4),
    ])
    gl.glPixelStorei.reset_mock()

    # different shape
    shape = (68, 296, 394, 1)
    t.set_size(shape, 'luminance', 'luminance')
    t.set_data((0, 0, 0), np.zeros(shape, np.float32))
    gl.glPixelStorei.assert_has_calls([
        mock.call(gl.GL_UNPACK_ALIGNMENT, 8),
        mock.call(gl.GL_UNPACK_ALIGNMENT, 4),
    ])
    gl.glPixelStorei.reset_mock()

    t.set_data((0, 0, 0), np.zeros(shape, np.uint8))
    gl.glPixelStorei.assert_has_calls([
        mock.call(gl.GL_UNPACK_ALIGNMENT, 2),
        mock.call(gl.GL_UNPACK_ALIGNMENT, 4),
    ])
    gl.glPixelStorei.reset_mock()
# The rest is basically tested via our examples

run_tests_if_main()