Sync & queriesGL 3.0

glBeginConditionalRender

Skips the following draws on the GPU if an occlusion query found no visible samples, with no CPU readback.

Signature

void glBeginConditionalRender(GLuint id, GLenum mode);

How it works

The occlusion-culling trick without the stall. Draw a bounding box inside an occlusion query, then wrap the real object in glBeginConditionalRender / glEndConditionalRender with that query. The GPU itself decides: if the box had no visible samples, the draws inside are dropped. The CPU never waits for the answer.

Parameters

idGLuint

An occlusion query (GL_SAMPLES_PASSED or GL_ANY_SAMPLES_PASSED…) that has ended.

modeGLenum

What to do if the result is not ready yet.

Common values

GL_QUERY_WAIT
The GPU waits for the result, then decides.
GL_QUERY_NO_WAIT
If not ready, draw anyway (never wrong, sometimes wasted).
GL_QUERY_BY_REGION_WAIT / _NO_WAIT
Same, letting the GPU decide per screen region.

Tips & pitfalls

  • Conditional rendering skips draws and clears, but not state changes; nested conditional rendering is not allowed.

Errors

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

GL_INVALID_OPERATION
Conditional rendering is already active, or the query is still running or is not an occlusion query.

Example

example.cpp
1glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2glDepthMask(GL_FALSE);
3glBeginQuery(GL_ANY_SAMPLES_PASSED_CONSERVATIVE, o.query);
4drawBoundingBox(o);
5glEndQuery(GL_ANY_SAMPLES_PASSED_CONSERVATIVE);
6glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
7glDepthMask(GL_TRUE);
8
9glBeginConditionalRender(o.query, GL_QUERY_NO_WAIT);
10drawObject(o);
11glEndConditionalRender();
Official Khronos documentation