Difference between revisions of "Swap Interval"

From OpenGL Wiki
Jump to: navigation, search
(Disputed.)
(move detailed info out of summary, put glfinish explanation in subsection, remove disputed text.)
Line 1: Line 1:
 
{{Disputed}}
 
{{Disputed}}
  
'''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.
+
'''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.
  
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.
+
Swap Interval control is provided via platform-specific [[extensions]].
 +
 
 +
== Usage ==
 +
 
 +
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.
 
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.
Line 15: Line 17:
 
The extension provides the wglSwapIntervalEXT() function, which directly specifies the swap interval. wglSwapIntervalEXT(1) is used to enable vsync; wglSwapIntervalEXT(0) to disable vsync.
 
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 ===
+
=== In Linux / GLX ===
 
 
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.
 
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.
Line 29: Line 27:
 
* 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, 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 [[#GPU vs CPU synchronization|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 ==
 
== External Links ==
 +
 
* [http://www.opengl.org/registry/specs/EXT/wgl_swap_control.txt WGL_EXT_swap_control]
 
* [http://www.opengl.org/registry/specs/EXT/wgl_swap_control.txt WGL_EXT_swap_control]
 
* [http://www.opengl.org/registry/specs/SGI/swap_control.txt GLX_SGI_swap_control]
 
* [http://www.opengl.org/registry/specs/SGI/swap_control.txt GLX_SGI_swap_control]
 
* [http://www.opengl.org/registry/specs/SGI/video_sync.txt GLX_SGI_video_sync]
 
* [http://www.opengl.org/registry/specs/SGI/video_sync.txt GLX_SGI_video_sync]

Revision as of 23:12, 10 December 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. 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.

Usage

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.

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, 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