瀏覽代碼

OpenGL vsync fixes.

rextimmy 8 年之前
父節點
當前提交
415f4a046e

+ 9 - 0
Engine/source/gfx/gl/gfxGLDevice.cpp

@@ -172,12 +172,21 @@ void GFXGLDevice::initGLState()
 
 
    PlatformGL::setVSync(smDisableVSync ? 0 : 1);
    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
    //OpenGL 3 need a binded VAO for render
    GLuint vao;
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    glBindVertexArray(vao);
 }
 }
 
 
+void GFXGLDevice::vsyncCallback()
+{
+   PlatformGL::setVSync(smDisableVSync ? 0 : 1);
+}
+
 GFXGLDevice::GFXGLDevice(U32 adapterIndex) :
 GFXGLDevice::GFXGLDevice(U32 adapterIndex) :
    mAdapterIndex(adapterIndex),
    mAdapterIndex(adapterIndex),
    mNeedUpdateVertexAttrib(false),
    mNeedUpdateVertexAttrib(false),

+ 2 - 0
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
    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
    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.
    void initGLState(); ///< Guaranteed to be called after all extensions have been loaded, use to init card profiler, shader version, max samplers, etc.
    
    

+ 6 - 1
Engine/source/gfx/gl/tGL/tGL.cpp

@@ -41,7 +41,12 @@ namespace GL
 
 
    void gglPerformExtensionBinds(void *context)
    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
    }
    }
 }
 }
 
 

+ 1 - 1
Engine/source/gfx/gl/tGL/tWGL.h

@@ -30,7 +30,7 @@
 #include "tGL.h"
 #include "tGL.h"
 #include <glad/glad_wgl.h>
 #include <glad/glad_wgl.h>
 
 
-#define gglHasWExtension(window, EXTENSION) GLAD_WGL_##EXTENSION
+#define gglHasWExtension(EXTENSION) GLAD_WGL_##EXTENSION
 
 
 #endif //TORQUE_OPENGL
 #endif //TORQUE_OPENGL
 
 

+ 1 - 1
Engine/source/gfx/gl/win32/gfxGLDevice.win.cpp

@@ -269,7 +269,7 @@ void GFXGLDevice::init( const GFXVideoMode &mode, PlatformWindow *window )
    if (!wglMakeCurrent(hdcGL, tempGLRC))
    if (!wglMakeCurrent(hdcGL, tempGLRC))
       AssertFatal(false, "Couldn't make temp GL context.");
       AssertFatal(false, "Couldn't make temp GL context.");
 
 
-   if( gglHasWExtension(hdcGL, ARB_create_context) )
+   if( gglHasWExtension( ARB_create_context) )
    {
    {
       int const create_attribs[] = {
       int const create_attribs[] = {
                WGL_CONTEXT_MAJOR_VERSION_ARB, OGL_MAJOR,
                WGL_CONTEXT_MAJOR_VERSION_ARB, OGL_MAJOR,

+ 6 - 0
Engine/source/platformSDL/sdlPlatformGL.cpp

@@ -6,6 +6,8 @@
 #include "gfx/gl/tGL/tWGL.h"
 #include "gfx/gl/tGL/tWGL.h"
 #endif
 #endif
 
 
+#include "gfx/gl/gfxGLUtils.h"
+
 namespace PlatformGL
 namespace PlatformGL
 {
 {
 
 
@@ -69,6 +71,9 @@ namespace PlatformGL
 
 
    void setVSync(const int i)
    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 )
        if( i == 1 || i == -1 )
        {
        {
            int ret = SDL_GL_SetSwapInterval(-1);
            int ret = SDL_GL_SetSwapInterval(-1);
@@ -78,6 +83,7 @@ namespace PlatformGL
        }
        }
        else
        else
            SDL_GL_SetSwapInterval(0);
            SDL_GL_SetSwapInterval(0);
+
    }
    }
 
 
 }
 }

+ 22 - 1
Engine/source/platformWin32/WinPlatformGL.cpp

@@ -2,11 +2,32 @@
 
 
 #include "platform/platformGL.h"
 #include "platform/platformGL.h"
 #include "gfx/gl/tGL/tWGL.h"
 #include "gfx/gl/tGL/tWGL.h"
+#include "gfx/gl/gfxGLUtils.h"
 
 
 void PlatformGL::setVSync(const int i)
 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);
       wglSwapIntervalEXT(i);
    }
    }
 }
 }