45

How do you enable vertical sync?

Is it something simple like glEnable(GL_VSYNC)? (though there's no such thing as GL_VSYNC or anything like it in the options that glEnable accepts).

or is there no standard way to do this in opengl?

hasen
  • 161,647
  • 65
  • 194
  • 231

4 Answers4

47

On Windows there is OpenGL extension method wglSwapIntervalEXT. From the post by b2b3 http://www.gamedev.net/community/forums/topic.asp?topic_id=360862:

If you are working on Windows you have to use extensions to use wglSwapIntervalExt function. It is defined in wglext.h. You will also want to download glext.h file. In wglext file all entry points for Windows specific extensions are declared. All such functions start with prefix wgl. To get more info about all published extensions you can look into OpenGL Extension Registry.

wglSwapIntervalEXT is from WGL_EXT_swap_control extension. It lets you specify minimum number of frames before each buffer swap. Usually it is used for vertical synchronization (if you set swap interval to 1). More info about whole extension can be found here. Before using this function you need query whether you card has support for WGL_EXT_swap_control and then obtain pointer to the function using wglGetProcAddress function.

To test for support of given extension you can use function like this:

#include <windows.h>
#include "wglext.h"

bool WGLExtensionSupported(const char *extension_name)
{
    // this is pointer to function which returns pointer to string with list of all wgl extensions
    PFNWGLGETEXTENSIONSSTRINGEXTPROC _wglGetExtensionsStringEXT = NULL;

    // determine pointer to wglGetExtensionsStringEXT function
    _wglGetExtensionsStringEXT = (PFNWGLGETEXTENSIONSSTRINGEXTPROC) wglGetProcAddress("wglGetExtensionsStringEXT");

    if (strstr(_wglGetExtensionsStringEXT(), extension_name) == NULL)
    {
        // string was not found
        return false;
    }

    // extension is supported
    return true;
}

To initialize your function pointers you need to:

PFNWGLSWAPINTERVALEXTPROC       wglSwapIntervalEXT = NULL;
PFNWGLGETSWAPINTERVALEXTPROC    wglGetSwapIntervalEXT = NULL;

if (WGLExtensionSupported("WGL_EXT_swap_control"))
{
    // Extension is supported, init pointers.
    wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");

    // this is another function from WGL_EXT_swap_control extension
    wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC) wglGetProcAddress("wglGetSwapIntervalEXT");
}

Then you can use these pointers as any other pointer to function. To enable vync you can call wglSwapIntervalEXT(1), to disable it you call wglSwapIntervalEXT(0).

To get current swap interval you need to call wglGetSwapIntervalEXT().

TankorSmash
  • 12,186
  • 6
  • 68
  • 106
eugensk
  • 1,882
  • 1
  • 14
  • 20
  • 3
    In your first code snippet if (strstr(_wglGetExtensionsString(), extension_name) == NULL) should read if (strstr(_wglGetExtensionsStringEXT(), extension_name) == NULL) -- But thanks, this helped me!!! – HoboBen Jun 14 '13 at 06:04
  • 4
    Also I used wglGetProcAddress instead of LogGetProcAddress – HoboBen Jun 14 '13 at 06:25
  • 4
    That is not an extension to OpenGL, but rather to WGL (the Microsoft Windows window system API for OpenGL). Buffer swapping is by its very nature a window system specific operation. As far as GL is concerned it just draws to a front/back left/right buffer or some arbitrary FBO. The window system is the only thing with enough knowledge of the underlying host system to synchronize the presentation of drawn buffers to some event (in this case the monitor's vertical retrace). – Andon M. Coleman Jun 06 '14 at 03:34
7

WGL case is described in the answer by eugensk00.

For CGL (MacOSX) see this answer to another SO question.

For EGL there's eglSwapInterval() function, but apparently (see this and this) it doesn't guarantee tearing-free result — only waits given period (maybe it's just due to broken drivers).

For GLX (Linux with X11 etc.) there are at least 3 similar extensions for this, with varying degree of functionality. OpenGL wiki currently lists only one, which is unsupported by Mesa <= 10.5.9 (and maybe higher). Here's a list from most feature-complete extension (listed in OpenGL wiki) to the least:

  1. GLX_EXT_swap_control

    • Set swap interval per-drawable per-display: glXSwapIntervalEXT(dpy, drawable, interval)

    • Get current swap interval: glXQueryDrawable(dpy, drawable, GLX_SWAP_INTERVAL_EXT, &interval)

    • Get maximum swap interval: glXQueryDrawable(dpy, drawable, GLX_MAX_SWAP_INTERVAL_EXT, &maxInterval)

    • Disable Vsync: set interval to 0

  2. GLX_MESA_swap_control

    • Set swap interval per-context: glXSwapIntervalMESA(interval)

    • Get current swap interval: glXGetSwapIntervalMESA()

    • Get maximum swap interval: unsupported

    • Disable Vsync: set interval to 0

  3. GLX_SGI_swap_control

    • Set swap interval: glXSwapIntervalSGI(interval).

    • Get current swap interval: unsupported

    • Get maximum swap interval: unsupported

    • Disable Vsync: unsupported (interval==0 is an error)

For adaptive Vsync see OpenGL wiki.

Ruslan
  • 18,162
  • 8
  • 67
  • 136
5
((BOOL(WINAPI*)(int))wglGetProcAddress("wglSwapIntervalEXT"))(1);

https://www.khronos.org/opengl/wiki/Swap_Interval

"wglSwapIntervalEXT(1) is used to enable vsync; wglSwapIntervalEXT(0) to disable vsync."

"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 never wait for v-blanks"


Alternatively: (wgl function typedefs are in #include <GL/wglext.h>)

((PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT"))(1);
PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
wglSwapIntervalEXT(1);
Puddle
  • 2,993
  • 1
  • 19
  • 32
  • Code only answers are discouraged. Please add some explanation as to how this solves the problem, or how this differs from the existing answers. [From Review](https://stackoverflow.com/review/low-quality-posts/22545702) – Nick Mar 23 '19 at 01:05
  • Upvoting because it was the simplest, fastest solution to a quick test I was doing. – Jari Komppa Oct 18 '20 at 10:54
0

For WGL case described in the answer by eugensk.

If you run into a nullptr error, make sure you are running {wglGetProcAddress} part code in an OpenGL Context.

i.e. after codes {glfwMakeContextCurrent(window);}.

See answer here.

ling
  • 23
  • 7