Sync & queriesGL 1.5

glBeginQuery

Starts measuring: everything drawn until glEndQuery is counted (or timed) by query id.

Signature

void glBeginQuery(GLenum target, GLuint id);

How it works

Wrap the work you want to measure between glBeginQuery and glEndQuery with the same target. Only one query per target can be active at a time, so timer queries cannot be nested; for nested or many timings use pairs of glQueryCounter timestamps instead.

An occlusion query draws something cheap (an object's bounding box, with colour and depth writes off) and counts how many samples pass the depth test. Zero means the real object is completely hidden and need not be drawn.

Parameters

targetGLenum

What to measure.

Common values

GL_TIME_ELAPSED
GPU time between begin and end, in nanoseconds.
GL_SAMPLES_PASSED
How many samples passed the depth test.
GL_ANY_SAMPLES_PASSED / _CONSERVATIVE
Just whether any did (0 or 1); the conservative one may say 1 when unsure, and is faster.
GL_PRIMITIVES_GENERATED
Primitives that left the geometry stage.
idGLuint

The query (from glGenQueries).

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GL_INVALID_OPERATION
A query of that target is already active, or id is in use with another target.

Example

example.cpp
1glBeginQuery(GL_TIME_ELAPSED, q[frame % N]);
2drawScene();
3glEndQuery(GL_TIME_ELAPSED);
Official Khronos documentation