Question

I know of two ways of getting the screen refresh rate on Windows. The first is the simple EnumDisplaySettings function, which fills a struct that contains a rounded screen refresh rate value in fps. The second is using DirectX, which, during initialization, gives you the option of retrieving the refresh rate by dividing a numerator and a denominator by each other.

From my own experience, the first method is insufficient. My laptop monitor does not manage to achieve an exact frame rate of 60 - rather, it has multiple settings which border around 59.9 and 60.1 in fps. The second method gives me an exact floating point value which can be used to achieve the effect of VSync without using VSync directly.

I'm asking this because of the lack of reliable VSync in OpenGL, and I don't want to rely on an extra graphics API at startup only to use it for one small feature. Is there another way?

Était-ce utile?

La solution

Following up on our comments, effectively VSYNC causes blocking... but more accurately, it blocks when the swap-chain is full of back buffers that need to be swapped. Triple buffering can be used to reduce blocking in an implementation of VSYNC, but neither WGL nor GLX exposes arbitrary control over the number of back buffers in the swap-chain, so you have to modify driver settings to enable it in an OpenGL context.

In OpenGL, the GL itself does not handle buffer swaps or VSYNC as I pointed out in an earlier comment, you must use the platform-specific window system layer (e.g. WGL, GLX, AGL/CGL, EGL) to set something known as a Swap Interval.

  • Setting the swap interval to 1 will enable VSYNC
  • Setting the swap interval to 0 will disable VSYNC

  • Setting the swap interval to -1 will enable adaptive VSYNC on newer drivers

    Support is determined by the existence of:

    WGL_EXT_swap_control_tear (Windows)

    or

    GLX_EXT_swap_control_tear (Any platform that uses X11, such as Linux)

Adaptive VSYNC is an interesting new feature that blocks if your application is exceeding the display's refresh rate but allows tearing when you are under the refresh rate. This is opposed to the normal behavior where anything below the refresh rate is throttled to some lower factor of the refresh rate.


In any case, since you are using Windows in this example, you will want to look into wglSwapIntervalEXT (...).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top