When using double-buffered OpenGL, Swap Interval is a means of synchronizing the swapping of front and back frame buffers with a video card's vertical blanking period, AKA Vsync.
The term "swap interval" itself refers to the number of vblank periods that must occur between buffer swaps. A swap interval of 1 specifies that at the very least, the GPU must wait for a single vblank before swapping the front and back buffers. A swap interval of 0 specifies that the GPU should not wait for any vblanks, thus perform buffer swaps immediately.
Application control of swap interval is provided via platform-specific extensions.
Use the WGL_EXT_swap_control extension to control swap interval. Check both the standard extensions string via glGetString(GL_EXTENSIONS) and the WGL-specific extensions string via wglGetExtensionsStringARB() to verify that WGL_EXT_swap_control is actually present.
The extension provides the wglSwapIntervalEXT() function, which directly specifies the swap interval. (e.g. wglSwapIntervalEXT(1) to enable vsync; wglSwapIntervalEXT(0) to disable vsync)
In Linux / GLX
Use the GLX_SGI_swap_control extension to control swap interval. Check both the standard extensions string via glGetString(GL_EXTENSIONS) and the GLX-specific extensions string via glXQueryExtensionsString() to verify that the extension is actually present.
The extension provides glxSwapIntervalSGI(), which also directly specifies the swap interval. (e.g. glxSwapIntervalSGI(1) to enable vsync; glxSwapIntervalSGI(0) to disable vsync)
- Some ATI GLX drivers may report WGL_EXT_swap_control yet actually export glxSwapIntervalSGI.
- Your application's use of swap interval may be overridden by external, driver-specific configuration. For example, forcing Vsync Off in a driver's control panel will prevent Vsync, even if swap interval is set to 1 in your application.
- "My rendered objects lag behind the mouse cursor!" With swap interval set to non-zero, older traditional drivers typically caused the calling thread to block SwapBuffers until the actual buffer swap was completed. In modern drivers, the swap is blocked on the GPU, allowing the application CPU thread to continue execution and even render and enqueue subsequent frames before the first swap occurs. This behavior could cause significant transport delay / latency if the application expects traditional behavior. If the application requires either more deterministic behavior associated with vblank or minimal transport delay, the two following mechanisms can be used. Please note that these mechanisms trade the multi-frame bandwidth advantages of the GL's pipeline for minimized transport delay, thus making your application more sensitive and likely to "stutter" when getting close to frame real-time deadlines.
- glFinish: A call to glFinish immediately after SwapBuffers causes the calling CPU thread to block until all queued GL commands are completed, including the actual buffer swap during vblank.
- GLX_SGI_video_sync: GLX_SGI_video_sync provides a method to synchronize CPU thread execution to the completion of a full video frame, though not necessarily to the buffer swap.