1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "VerticalSync.h"
#include "GL/myGL.h"
#include "System/Config/ConfigHandler.h"
#include "System/Log/ILog.h"
#if defined HEADLESS
#elif defined WIN32
#include <GL/wglew.h>
#elif !defined(__APPLE__)
#include <GL/glxew.h>
#endif
CONFIG(int, VSync).defaultValue(0).minimumValue(0).description("Vertical synchronization, update render frames in monitor's refresh rate.\n <=0: off\n 1: enabled \n x: render with monitor-Hz/x FPS");
CVerticalSync VSync;
/******************************************************************************/
CVerticalSync::CVerticalSync()
{
interval = -1;
}
CVerticalSync::~CVerticalSync()
{
}
/******************************************************************************/
void CVerticalSync::Init()
{
SetInterval(configHandler->GetInt("VSync"));
}
void CVerticalSync::SetInterval(int i)
{
i = std::max(0, i);
if (i == interval)
return;
configHandler->Set("VSync", i);
interval = i;
#if defined HEADLESS
#elif !defined(WIN32) && !defined(__APPLE__)
#ifdef GLXEW_EXT_swap_control
if (GLXEW_EXT_swap_control) {
//System 1
// This is the prefered way cause glXSwapIntervalEXT won't lock the thread until the OGL cmd queue is full!
// And so we can process SimFrames while waiting for VSync.
Display* dpy = glXGetCurrentDisplay();
GLXDrawable drawable = glXGetCurrentDrawable();
#ifdef GLXEW_EXT_swap_control_tear
// this enables so called `adaptive vsync` or also called late syncing (~ it won't vsync if FPS < monitor refresh rate)
if (GLXEW_EXT_swap_control_tear) {
if (interval != 0)
LOG("Using Adaptive VSync");
glXSwapIntervalEXT(dpy, drawable, -interval);
} else
#endif
{
if (interval != 0)
LOG("Using VSync");
glXSwapIntervalEXT(dpy, drawable, interval);
}
} else
#endif
if (!GLXEW_SGI_video_sync) {
interval = 0; // disable
} else {
if (interval != 0)
LOG("Using SGI VSync");
}
#elif defined WIN32
if (WGLEW_EXT_swap_control) {
if (interval != 0)
LOG("Using VSync");
wglSwapIntervalEXT(interval);
}
#endif
if (interval == 0)
LOG("VSync disabled");
}
/******************************************************************************/
void CVerticalSync::Delay() const
{
#if defined HEADLESS
#elif !defined(WIN32) && !defined(__APPLE__)
if (interval <= 0)
return;
#ifdef GLXEW_EXT_swap_control_tear
if (GLXEW_EXT_swap_control_tear)
return;
#endif
#ifdef GLXEW_EXT_swap_control
if (GLXEW_EXT_swap_control)
return;
#endif
GLuint frameCount;
if (glXGetVideoSyncSGI(&frameCount) == 0) {
//System 2
//Note: This locks the thread (similar to glFinish)!!!
glXWaitVideoSyncSGI(interval, (frameCount+1) % interval, &frameCount);
}
#endif
}
/******************************************************************************/
|