Sync & queriesGL 3.2

glFenceSync

Drops a marker into the command stream that becomes signalled when the GPU has executed everything before it.

Signature

GLsync glFenceSync(GLenum condition, GLbitfield flags);

How it works

OpenGL calls return long before the GPU runs them; the CPU is usually a frame or two ahead. A fence is how you find out that the GPU has caught up to a point: insert one after the commands you care about, and later ask glClientWaitSync whether it is signalled.

Two common uses: before the CPU overwrites a region of a persistently mapped buffer (wait for the fence placed after the draw that read it), and before mapping a pixel buffer filled by glReadPixels.

Parameters

conditionGLenum

Always GL_SYNC_GPU_COMMANDS_COMPLETE.

flagsGLbitfield

Must be 0.

Return value

A GLsync handle (an opaque pointer), or 0 on error. Delete it with glDeleteSync when done.

Tips & pitfalls

  • A fence is used once: after it signals, delete it and create a new one for the next frame.

Errors

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

GL_INVALID_ENUM
condition is not GL_SYNC_GPU_COMMANDS_COMPLETE.
GL_INVALID_VALUE
flags is not 0.

Example

example.cpp
1drawWithRegion(k);                     // GPU reads region k
2fence[k] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
Official Khronos documentation