Swap Interval

From OpenGL Wiki
Revision as of 22:43, 10 September 2011 by X-Fi6 (talk | contribs)
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.

On Windows, apart from rendering the picture at least twice as fast as the monitor's refresh rate (per the rules of the Sampling Theorem), SwapBuffers() combined with glFinish() is the only method supported on modern hardware to perfectly achieve vertical synchronization (vsync). Double buffering is required. Because SwapBuffers() is a GL command, it must be executed on the same thread that has the GL rendering context. In GLX, you may use the GLX_SGI_video_sync extension instead, without being tied to double buffering.

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.

Swap Interval is provided via platform-specific extensions.

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.

Vertical synchronization

Because SwapBuffers() is a GL command, it has the control to block until it's time for a vertical retrace. Recall that glFinish() not only queues all GL commands, but waits until they have all been executed by the GPU. Thus, a call to SwapBuffers() followed by a call to glFinish() causes the calling CPU thread to block until the back frame buffer has been rendered, swapped to the front, and dispatched for v-blank.

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!" 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.

External Links