diff --git a/Engine/source/gfx/gl/gfxGLDevice.cpp b/Engine/source/gfx/gl/gfxGLDevice.cpp index 7723cdc4a..ce22f16a1 100644 --- a/Engine/source/gfx/gl/gfxGLDevice.cpp +++ b/Engine/source/gfx/gl/gfxGLDevice.cpp @@ -172,12 +172,21 @@ void GFXGLDevice::initGLState() PlatformGL::setVSync(smDisableVSync ? 0 : 1); + //install vsync callback + Con::NotifyDelegate clbk(this, &GFXGLDevice::vsyncCallback); + Con::addVariableNotify("$pref::Video::disableVerticalSync", clbk); + //OpenGL 3 need a binded VAO for render GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); } +void GFXGLDevice::vsyncCallback() +{ + PlatformGL::setVSync(smDisableVSync ? 0 : 1); +} + GFXGLDevice::GFXGLDevice(U32 adapterIndex) : mAdapterIndex(adapterIndex), mNeedUpdateVertexAttrib(false), diff --git a/Engine/source/gfx/gl/gfxGLDevice.h b/Engine/source/gfx/gl/gfxGLDevice.h index fc37d3220..f2bf4afe1 100644 --- a/Engine/source/gfx/gl/gfxGLDevice.h +++ b/Engine/source/gfx/gl/gfxGLDevice.h @@ -256,6 +256,8 @@ private: GFXVertexBuffer* findVolatileVBO(U32 numVerts, const GFXVertexFormat *vertexFormat, U32 vertSize); ///< Returns an existing volatile VB which has >= numVerts and the same vert flags/size, or creates a new VB if necessary GFXPrimitiveBuffer* findVolatilePBO(U32 numIndices, U32 numPrimitives); ///< Returns an existing volatile PB which has >= numIndices, or creates a new PB if necessary + + void vsyncCallback(); ///< Vsync callback void initGLState(); ///< Guaranteed to be called after all extensions have been loaded, use to init card profiler, shader version, max samplers, etc. diff --git a/Engine/source/gfx/gl/tGL/tGL.cpp b/Engine/source/gfx/gl/tGL/tGL.cpp index b13fec60a..de412ca19 100644 --- a/Engine/source/gfx/gl/tGL/tGL.cpp +++ b/Engine/source/gfx/gl/tGL/tGL.cpp @@ -41,7 +41,12 @@ namespace GL void gglPerformExtensionBinds(void *context) { - + #ifdef TORQUE_OS_WIN + if (!gladLoadWGL(wglGetCurrentDC())) + { + AssertFatal(false, "Unable to load GLAD WGL extensions. Make sure your OpenGL drivers are up to date!"); + } + #endif } } diff --git a/Engine/source/gfx/gl/tGL/tWGL.h b/Engine/source/gfx/gl/tGL/tWGL.h index 5dbf1da68..22f4a3496 100644 --- a/Engine/source/gfx/gl/tGL/tWGL.h +++ b/Engine/source/gfx/gl/tGL/tWGL.h @@ -30,7 +30,7 @@ #include "tGL.h" #include -#define gglHasWExtension(window, EXTENSION) GLAD_WGL_##EXTENSION +#define gglHasWExtension(EXTENSION) GLAD_WGL_##EXTENSION #endif //TORQUE_OPENGL diff --git a/Engine/source/gfx/gl/win32/gfxGLDevice.win.cpp b/Engine/source/gfx/gl/win32/gfxGLDevice.win.cpp index 1b88f2dc9..f391f511b 100644 --- a/Engine/source/gfx/gl/win32/gfxGLDevice.win.cpp +++ b/Engine/source/gfx/gl/win32/gfxGLDevice.win.cpp @@ -269,7 +269,7 @@ void GFXGLDevice::init( const GFXVideoMode &mode, PlatformWindow *window ) if (!wglMakeCurrent(hdcGL, tempGLRC)) AssertFatal(false, "Couldn't make temp GL context."); - if( gglHasWExtension(hdcGL, ARB_create_context) ) + if( gglHasWExtension( ARB_create_context) ) { int const create_attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, OGL_MAJOR, diff --git a/Engine/source/platformSDL/sdlPlatformGL.cpp b/Engine/source/platformSDL/sdlPlatformGL.cpp index dbd163ecc..47eab147b 100644 --- a/Engine/source/platformSDL/sdlPlatformGL.cpp +++ b/Engine/source/platformSDL/sdlPlatformGL.cpp @@ -6,6 +6,8 @@ #include "gfx/gl/tGL/tWGL.h" #endif +#include "gfx/gl/gfxGLUtils.h" + namespace PlatformGL { @@ -69,6 +71,9 @@ namespace PlatformGL void setVSync(const int i) { + PRESERVE_FRAMEBUFFER(); + // Nvidia needs to have the default framebuffer bound or the vsync calls fail + glBindFramebuffer(GL_FRAMEBUFFER, 0); if( i == 1 || i == -1 ) { int ret = SDL_GL_SetSwapInterval(-1); @@ -78,6 +83,7 @@ namespace PlatformGL } else SDL_GL_SetSwapInterval(0); + } } diff --git a/Engine/source/platformWin32/WinPlatformGL.cpp b/Engine/source/platformWin32/WinPlatformGL.cpp index a30212bad..cd9e1975e 100644 --- a/Engine/source/platformWin32/WinPlatformGL.cpp +++ b/Engine/source/platformWin32/WinPlatformGL.cpp @@ -2,11 +2,32 @@ #include "platform/platformGL.h" #include "gfx/gl/tGL/tWGL.h" +#include "gfx/gl/gfxGLUtils.h" void PlatformGL::setVSync(const int i) { - if (gglHasWExtension(wglGetCurrentDC(), EXT_swap_control)) + if (gglHasWExtension(EXT_swap_control)) { + PRESERVE_FRAMEBUFFER(); + // NVidia needs to have the default framebuffer bound or the vsync calls fail + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + if (gglHasWExtension(EXT_swap_control_tear)) + { + if (i == 1 || i == -1) + { + BOOL ret = wglSwapIntervalEXT(-1); + + if (!ret) + wglSwapIntervalEXT(1); + } + else + { + wglSwapIntervalEXT(i); + } + return; + } + //fallback with no EXT_swap_control_tear wglSwapIntervalEXT(i); } }