Swap Interval

From OpenGL Wiki
Revision as of 23:12, 10 December 2011 by The Resident (talk | contribs) (move detailed info out of summary, put glfinish explanation in subsection, remove disputed text.)
Jump to: navigation, search

Swap Interval is a means of synchronizing the swapping of the front and back frame buffers with vertical blanks: the periods in which the front frame buffer is dispatched for display on the screen. It is a very common means of controlling frame "tearing," as often seen in high-motion-content graphics.

Swap Interval control is provided via platform-specific extensions.


The plaform-specific SwapBuffers() API commands the GPU to swap front and back buffers. When swap interval is set greater than zero, the swap takes place only during the vertical blanking period, thus eliminating the "torn" frame.

The term "swap interval" itself refers to the number of v-blank periods that occur before the buffers are swapped. A swap interval of 1 tells the GPU to wait for one v-blank before swapping the front and back buffers. A swap interval of 0 specifies that the GPU should not ever wait for v-blanks, thus performing buffer swaps immediately. Some video drivers may force Swap Interval to 1 or to 0 if specified by the user in the video card's control panel.

In Windows

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. wglSwapIntervalEXT(1) is used 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. glXSwapIntervalSGI(1) is used 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, but only when Vsync is enabled!" You probably have several frames queued up in the GPU. You may want to consider calling glFinish. See the synchronization discussion below.

GPU vs CPU synchronization

To glFinish or not to glFinish, that is the question!

Vsync without glFinish: SwapBuffers() commands the GPU to swap front and back buffers. This command is typically treated like any other GL command, and is executed by the GPU asynchronously from the CPU. As such, the CPU rendering thread does not block and is free to continue rendering and queuing subsequent frames, potentially at a higher rate than vertical refresh. While this maximizes throughput, it creates a scenario where several frames are queued and awaiting buffer swap. A newly generated frame will not actually become visible until after all frames queued ahead of it are swapped, thus creating a visible transport delay.

Vsync with glFinish: The CPU thread can be synchronized with the buffer swap by calling glFinish() after SwapBuffers(). glFinish() causes the CPU thread to block until all queued commands are complete, including the buffer swap itself. This ensures that every newly generated frame becomes visible after the next v-blank. While this minimizes transport delay, it kills throughput advantages offered by the GL pipeline and increases the risk of "stutter" due to missed real-time frame deadlines.

External Links