File: test_framebuffer.py

package info (click to toggle)
python-vispy 0.15.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,868 kB
  • sloc: python: 59,799; javascript: 6,800; makefile: 69; sh: 6
file content (195 lines) | stat: -rw-r--r-- 5,482 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
# -*- coding: utf-8 -*-

from vispy.testing import run_tests_if_main, assert_raises

from vispy import gloo
from vispy.gloo import FrameBuffer, RenderBuffer


def test_renderbuffer():

    # Set with no args
    assert_raises(ValueError, RenderBuffer)

    # Set shape only
    R = RenderBuffer((10, 20))
    assert R.shape == (10, 20)
    assert R.format is None

    # Set both shape and format
    R = RenderBuffer((10, 20), 'color')
    assert R.shape == (10, 20)
    assert R.format == 'color'
    #
    glir_cmds = R._glir.clear()
    assert len(glir_cmds) == 2
    assert glir_cmds[0][0] == 'CREATE'
    assert glir_cmds[1][0] == 'SIZE'

    # Orther formats
    assert RenderBuffer((10, 20), 'depth').format == 'depth'
    assert RenderBuffer((10, 20), 'stencil').format == 'stencil'

    # Test reset size and format
    R.resize((9, 9), 'depth')
    assert R.shape == (9, 9)
    assert R.format == 'depth'
    R.resize((8, 8), 'stencil')
    assert R.shape == (8, 8)
    assert R.format == 'stencil'

    # Wrong formats
    assert_raises(ValueError, R.resize, (9, 9), 'no_format')
    assert_raises(ValueError, R.resize, (9, 9), [])

    # Resizable
    R = RenderBuffer((10, 20), 'color', False)
    assert_raises(RuntimeError, R.resize, (9, 9), 'color')

    # Attaching sets the format
    F = FrameBuffer()
    #
    R = RenderBuffer((9, 9))
    F.color_buffer = R
    assert F.color_buffer is R
    assert R.format == 'color'
    #
    F.depth_buffer = RenderBuffer((9, 9))
    assert F.depth_buffer.format == 'depth'
    #
    F.stencil_buffer = RenderBuffer((9, 9))
    assert F.stencil_buffer.format == 'stencil'


def test_framebuffer():

    # Test init with no args
    F = FrameBuffer()
    glir_cmds = F._glir.clear()
    assert len(glir_cmds) == 1
    glir_cmds[0][0] == 'CREATE'

    # Activate / deactivate
    F.activate()
    glir_cmd = F._glir.clear()[-1]
    assert glir_cmd[0] == 'FRAMEBUFFER'
    assert glir_cmd[2] is True
    #
    F.deactivate()
    glir_cmd = F._glir.clear()[-1]
    assert glir_cmd[0] == 'FRAMEBUFFER'
    assert glir_cmd[2] is False
    #
    with F:
        pass
    glir_cmds = F._glir.clear()
    assert len(glir_cmds) == 2
    assert glir_cmds[0][0] == 'FRAMEBUFFER'
    assert glir_cmds[1][0] == 'FRAMEBUFFER'
    assert glir_cmds[0][2] is True and glir_cmds[1][2] is False

    # Init with args
    R = RenderBuffer((3, 3))
    F = FrameBuffer(R)
    assert F.color_buffer is R
    #
    R2 = RenderBuffer((3, 3))
    F.color_buffer = R2
    assert F.color_buffer is R2

    # Wrong buffers
    F = FrameBuffer()
    assert_raises(TypeError, FrameBuffer.color_buffer.fset, F, 'FOO')
    assert_raises(TypeError, FrameBuffer.color_buffer.fset, F, [])
    assert_raises(TypeError, FrameBuffer.depth_buffer.fset, F, 'FOO')
    assert_raises(TypeError, FrameBuffer.stencil_buffer.fset, F, 'FOO')
    color_buffer = RenderBuffer((9, 9), 'color')
    assert_raises(ValueError, FrameBuffer.depth_buffer.fset, F, color_buffer)
    # But None is allowed!
    F.color_buffer = None

    # Shape
    R1 = RenderBuffer((3, 3))
    R2 = RenderBuffer((3, 3))
    R3 = RenderBuffer((3, 3))
    F = FrameBuffer(R1, R2, R3)
    assert F.shape == R1.shape 
    assert R1.format == 'color'
    assert R2.format == 'depth'
    assert R3.format == 'stencil'
    # Resize
    F.resize((10, 10))
    assert F.shape == (10, 10)
    assert F.shape == R1.shape 
    assert F.shape == R2.shape 
    assert F.shape == R3.shape 
    assert R1.format == 'color'
    assert R2.format == 'depth'
    assert R3.format == 'stencil'
    # Shape from any buffer
    F.color_buffer = None
    assert F.shape == (10, 10)
    F.depth_buffer = None
    assert F.shape == (10, 10)
    F.stencil_buffer = None
    assert_raises(RuntimeError, FrameBuffer.shape.fget, F)

    # Also with Texture luminance
    T = gloo.Texture2D((20, 30))
    R = RenderBuffer(T.shape)
    assert T.format == 'luminance'
    F = FrameBuffer(T, R)
    assert F.shape == T.shape[:2]
    assert F.shape == R.shape
    assert T.format == 'luminance'
    assert R.format == 'depth'
    # Resize
    F.resize((10, 10))
    assert F.shape == (10, 10)
    assert T.shape == (10, 10, 1)
    assert F.shape == R.shape 
    assert T.format == 'luminance'
    assert R.format == 'depth'

    # Also with Texture RGB
    T = gloo.Texture2D((20, 30, 3))
    R = RenderBuffer(T.shape)
    assert T.format == 'rgb'
    F = FrameBuffer(T, R)
    assert F.shape == T.shape[:2]
    assert F.shape == R.shape
    assert T.format == 'rgb'
    assert R.format == 'depth'
    # Resize
    F.resize((10, 10))
    assert F.shape == (10, 10)
    assert T.shape == (10, 10, 3)
    assert F.shape == R.shape 
    assert T.format == 'rgb'
    assert R.format == 'depth'

    # Also with Texture for depth
    T1 = gloo.Texture2D((20, 30, 3))
    T2 = gloo.Texture2D((20, 30, 1))
    assert T1.format == 'rgb'
    assert T2.format == 'luminance'
    F = FrameBuffer(T1, T2)
    assert F.shape == T1.shape[:2]
    assert F.shape == T2.shape[:2]
    assert T1.format == 'rgb'
    assert T2.format == 'luminance'
    # Resize
    F.resize((10, 10))
    assert F.shape == (10, 10)
    assert T1.shape == (10, 10, 3)
    assert T2.shape == (10, 10, 1)
    assert T1.format == 'rgb'
    assert T2.format == 'luminance'

    # Wrong shape in resize
    assert_raises(ValueError, F. resize, (9, 9, 1))
    assert_raises(ValueError, F. resize, (9,))
    assert_raises(ValueError, F. resize, 'FOO')


run_tests_if_main()