Query#

class moderngl.Query#

This class represents a Query object.

Create#

Context.query(*, samples: bool = False, any_samples: bool = False, time: bool = False, primitives: bool = False) moderngl.query.Query

Create a Query object.

Keyword Arguments
  • samples (bool) – Query GL_SAMPLES_PASSED or not.

  • any_samples (bool) – Query GL_ANY_SAMPLES_PASSED or not.

  • time (bool) – Query GL_TIME_ELAPSED or not.

  • primitives (bool) – Query GL_PRIMITIVES_GENERATED or not.

Attributes#

Query.samples#

The number of samples passed.

Type

int

Query.primitives#

The number of primitives generated.

Type

int

Query.elapsed#

The time elapsed in nanoseconds.

Type

int

Query.crender#

Can be used in a with statement.

Type

ConditionalRender

Query.extra#

Any - Attribute for storing user defined objects

Query.mglo#

Internal representation for debug purposes only.

Query.ctx#

The context this object belongs to

Examples#

Simple query example

 1import moderngl
 2import numpy as np
 3
 4ctx = moderngl.create_standalone_context()
 5prog = ctx.program(
 6    vertex_shader='''
 7        #version 330
 8
 9        in vec2 in_vert;
10
11        void main() {
12            gl_Position = vec4(in_vert, 0.0, 1.0);
13        }
14    ''',
15    fragment_shader='''
16        #version 330
17
18        out vec4 color;
19
20        void main() {
21            color = vec4(1.0, 0.0, 0.0, 1.0);
22        }
23    ''',
24)
25
26vertices = np.array([
27    0.0, 0.0,
28    1.0, 0.0,
29    0.0, 1.0,
30], dtype='f4')
31
32vbo = ctx.buffer(vertices.tobytes())
33vao = ctx.simple_vertex_array(prog, vbo, 'in_vert')
34
35fbo = ctx.simple_framebuffer((64, 64))
36fbo.use()
37
38query = ctx.query(samples=True, time=True)
39
40with query:
41    vao.render()
42
43print('It took %d nanoseconds' % query.elapsed)
44print('to render %d samples' % query.samples)

Output

It took 13529 nanoseconds
to render 496 samples