File: boom.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 (342 lines) | stat: -rw-r--r-- 8,541 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""
Simple test script for gc

* Leak then mgl object fails on creation?
* If object doesn't have ctx attribute it's not valid?
"""
import logging
import time
from ctypes import c_long

import numpy as np
import moderngl

logging.basicConfig(level=logging.DEBUG)

ctx = moderngl.create_context(standalone=True, require=330)

print(ctx)
ctx.gc_mode = "auto"


def ref_count():
    """Simple refcount demo"""
    var1 = [1, 2]
    print(c_long.from_address(id(var1)).value)


# --- Texture ---

def test_texture():
    return ctx.texture((100, 100), 4, data=np.random.randint(0, 1, 100 * 100 * 4, dtype="u1"), dtype="u1")

def test_texture_mass_create(n=1_000):
    for _ in range(n):
        texture = ctx.texture((1000, 1000), 4, data=np.random.randint(0, 255, 1000 * 1000 * 4, dtype="u1"))


def test_texture_failure():
    try:
        texture = ctx.texture((1000, 1000), 4, data=np.random.randint(0, 255, 1000 * 1001 * 4, dtype="u1"))
    except Exception as ex:
        print(ex)


def test_texture_failure_mass_create(n=1000):
    for i in range(n):
        test_texture_failure()


# --- TextureArray ---

def test_texture_array():
    texture = ctx.texture_array((100, 100, 100), 4, data=np.random.randint(0, 255, 100 * 100 * 100 * 4, dtype="u1"))


def test_texture_array_mass_create(n=1000):
    for _ in range(n):
        texture = ctx.texture_array((100, 100, 100), 4, data=np.random.randint(0, 255, 100 * 100 * 100 * 4, dtype="u1"))


def test_texture_array_failure():
    try:
        texture = ctx.texture_array((100, 100, 10), 4, data=np.random.randint(0, 255, 100 * 100 * 100 * 4, dtype="u1"))
    except Exception as ex:
        print(ex)


def test_texture_array_failure_mass_create(n=1000):
    for i in range(n):
        test_texture_array_failure()


# --- Texture3D ---

def test_texture_3d():
    texture = ctx.texture3d((100, 100, 100), 4, data=np.random.randint(0, 255, 100 * 100 * 100 * 4, dtype="u1"))


def test_texture_3d_mass_create(n=1000):
    for _ in range(n):
        texture = ctx.texture3d((100, 100, 100), 4, data=np.random.randint(0, 255, 100 * 100 * 100 * 4, dtype="u1"))


def test_texture_3d_failure():
    try:
        texture = ctx.texture3d((100, 100, 100), 4, data=np.random.randint(0, 255, 100 * 100 * 101 * 4, dtype="u1"))
    except Exception as ex:
        print(ex)


def test_texture_3d_failure_mass_create(n=1000):
    for i in range(n):
        test_texture_3d_failure()


# --- Framebuffer ---

def test_framebuffer():
    texture = ctx.texture((100, 100), 4)
    fbo = ctx.framebuffer(color_attachments=[texture])


def test_framebuffer_mass_create(n=1000):
    for i in range(n):
        texture = ctx.texture((100, 100), 4)
        ctx.framebuffer(color_attachments=[texture])
        # Depending on drivers FBOs might not release until flush/finish
        ctx.finish()


def test_framebuffer_failure():
    try:
        fbo = ctx.framebuffer(color_attachments=[])
    except Exception as ex:
        print(ex)


def test_framebuffer_failure_mass_create(n=1000):
    for i in range(n):
        test_framebuffer_failure()


# --- RenderBuffer ---

def test_renderbuffer_mass_create(n=1000):
    for i in range(n):
        rb = ctx.renderbuffer((1000, 1000))
        ctx.finish()


def test_renderbuffer_failure_mass_create(n=1000):
    for i in range(n):
        try:
            rb = ctx.renderbuffer((1000, 1000), components=4, dtype="moo")
        except Exception as ex:
            print(ex)
        ctx.finish()

# --- Buffer ---

def test_buffer():
    buffer = ctx.buffer(data=np.random.randint(0, 255, 1024 * 1024 * 1024, dtype="u1"))
    buffer.release()


def test_buffer_mass_create(n=1000):
    for i in range(n):
        b = ctx.buffer(data=np.random.randint(0, 255, 1024 * 1024, dtype="u1"))


def test_buffer_creation_failed():
    try:
        buffer = ctx.buffer(data=None)
    except Exception as ex:
        print(ex)


def test_buffer_creation_failed_mass_create(n=1000):
    for i in range(n):
        test_buffer_creation_failed()


# --- Query ---

def test_query_mass_create(n=1000):
    for _ in range(n):
        q = ctx.query(samples=True, any_samples=True, time=True, primitives=True)
        # print(c_long.from_address(id(q.mglo)))


# --- VertexArray / Program ---

prog_src = {
        "vertex_shader": """
        #version 330
        in vec2 in_pos;
        void main() {
            gl_Position = vec4(in_pos, 0.0, 1.0);
        }
        """,
        "fragment_shader": """
        #version 330
        out vec4 color;
        void main() {
            color = vec4(1.0);
        }
        """
}


def test_vertex_array():
    buffer = ctx.buffer(reserve=1024)
    prog = ctx.program(**prog_src)
    vao = ctx.vertex_array(prog, [(buffer, "2f", "in_pos")])


def test_vertex_array_mass_create(n=1000):
    for i in range(n):
        prog = ctx.program(**prog_src)
        vbo = ctx.buffer(reserve=1_000_000)
        ibo = ctx.buffer(reserve=1_000_000)
        vao = ctx.vertex_array(prog, [(vbo, "2f", "in_pos")], index_buffer=ibo)


def test_vertex_array_creation_failure():
    try:
        buffer = ctx.buffer(reserve=1024)
        prog = ctx.program(**prog_src)
        vao = ctx.vertex_array(prog, [(buffer, "2f", "in_pos_invalid")])
    except moderngl.Error:
        pass


def test_vertex_array_creation_failure_mass_create(n=1000):
    for i in range(n):
        test_vertex_array_creation_failure()


# --- ComputeShader ---

def test_compute_shader():
    compute_shader = ctx.compute_shader('''
        #version 430
        layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
        void main() {
        }
    ''')


def test_compute_shader_mass_create(n=1000):
    for i in range(n):
        test_compute_shader()
        print(i)


def test_compute_shader_fail():
    try:
        compute_shader = ctx.compute_shader('''
            #version 430
            layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
            void main() {
            }
        ''')
    except Exception as ex:
        print(ex)


def test_compute_shader_fail_mass_create(n=1000):
    for i in range(n):
        test_compute_shader_fail()


# --- Sampler ---

def test_sampler_mass_create(n=1000):
    for i in range(n):
        sampler = ctx.sampler()


# --- Scope ---

def test_scope():
    fbo = ctx.simple_framebuffer((100, 100))
    tex1 = ctx.texture((10, 100), 4)
    tex2 = ctx.texture((10, 100), 4)
    ubo1 = ctx.buffer(reserve=16000)
    ubo2 = ctx.buffer(reserve=16000)
    ssob1 = ctx.buffer(reserve=1024)
    ssob2 = ctx.buffer(reserve=1024)

    for i in range(10_000):
        print(i)
        scope = ctx.scope(
            framebuffer=fbo,
            enable_only=moderngl.NOTHING,
            textures=[(tex1, 0), (tex2, 1)],
            uniform_buffers=[(ubo1, 0), (ubo2, 1)],
            storage_buffers=[(ssob1, 0), (ssob2, 1)],
        )
        # scope = ctx.scope(
        #     framebuffer=ctx.simple_framebuffer((100, 100)),
        #     enable_only=moderngl.NOTHING,
        #     textures=[(ctx.texture((10, 100), 4), 0), (ctx.texture((10, 100), 4), 1)],
        #     uniform_buffers=[(ctx.buffer(reserve=16000), 0), (ctx.buffer(reserve=16000), 1)],
        #     storage_buffers=[(ctx.buffer(reserve=1024), 0), (ctx.buffer(reserve=1024), 1)],
        # )
        # with scope:
        #     pass


# ------------------------------------------------------------------------------------------

# ref_count()

N = 100

test_texture()
test_texture_mass_create(N)
test_texture_failure()
test_texture_failure_mass_create(N)

test_texture_array()
test_texture_array_mass_create(N)
test_texture_array_failure()
test_texture_array_failure_mass_create(N)

test_texture_3d()
test_texture_3d_mass_create(N)
test_texture_3d_failure()
test_texture_3d_failure_mass_create(N)

test_framebuffer()
test_framebuffer_mass_create(N)
test_framebuffer_failure()
test_framebuffer_failure_mass_create(N)

test_renderbuffer_mass_create(N)
test_renderbuffer_failure_mass_create(N)

test_buffer()
test_buffer_mass_create(N)
test_buffer_creation_failed()
test_buffer_creation_failed_mass_create(N)

test_query_mass_create()

test_vertex_array()
test_vertex_array_mass_create(N)
test_vertex_array_creation_failure()
test_vertex_array_creation_failure_mass_create(N)

if ctx.version_code >= 430:
    test_compute_shader()
    test_compute_shader_mass_create(N)
    test_compute_shader_fail()
    test_compute_shader_fail_mass_create(N)

test_sampler_mass_create(N)

# test_scope()
print("Done. Waiting")
time.sleep(300)