Swap Interval: Difference between revisions

From OpenGL Wiki
Jump to navigation Jump to search
m (glxSwapIntervalSGI converted to glXSwapIntervalSGI)
No edit summary
Line 1: Line 1:
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.
'''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.


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.
On Windows, apart from rendering the picture at least twice as fast as the monitor's refresh rate (per the rules of the [http://en.wikipedia.org/wiki/Nyquist–Shannon_sampling_theorem 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.


Application control of swap interval is provided via platform-specific [[extensions]].
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 ==
== 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.
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 ===


The extension provides the wglSwapIntervalEXT() function, which directly specifies the swap interval. (e.g.  wglSwapIntervalEXT(1) to enable vsync;  wglSwapIntervalEXT(0) to disable vsync)
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 ==
== 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.
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)
The extension provides glXSwapIntervalSGI(), which also directly specifies the swap interval. glXSwapIntervalSGI(1) is used to enable vsync; glXSwapIntervalSGI(0) to disable vsync.


== Idiosyncrasies ==
== Idiosyncrasies ==


* Some ATI GLX drivers may report WGL_EXT_swap_control yet actually export glXSwapIntervalSGI.
* 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.
* 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.
* '''"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.


== External Links ==
== External Links ==

Revision as of 22:43, 10 September 2011

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.

Idiosyncrasies

  • 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