From 2ef508139f35edfeffb320685acf99b796c360e7 Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Sat, 27 Apr 2013 15:16:05 -0700 Subject: [PATCH] GL/GLES: vsync. --- examples/common/processevents.h | 5 +++++ src/glcontext_egl.cpp | 5 ++++- src/glcontext_egl.h | 2 +- src/glcontext_glx.cpp | 22 ++++++++++++++++++---- src/glcontext_glx.h | 2 +- src/glcontext_nsgl.h | 2 +- src/glcontext_ppapi.cpp | 2 +- src/glcontext_ppapi.h | 2 +- src/glcontext_wgl.cpp | 6 +++++- src/glcontext_wgl.h | 2 +- src/renderer_gl.cpp | 7 ++++--- 11 files changed, 42 insertions(+), 15 deletions(-) diff --git a/examples/common/processevents.h b/examples/common/processevents.h index 3228e9f2..540e00b7 100644 --- a/examples/common/processevents.h +++ b/examples/common/processevents.h @@ -70,6 +70,11 @@ inline bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, bgfx::setDebug(_debug); return false; } + else if (key->m_key == Key::F7) + { + _reset ^= BGFX_RESET_VSYNC; + reset = true; + } else if (key->m_key == Key::F8) { _reset ^= BGFX_RESET_MSAA_X16; diff --git a/src/glcontext_egl.cpp b/src/glcontext_egl.cpp index 92c93d55..a66b23ab 100644 --- a/src/glcontext_egl.cpp +++ b/src/glcontext_egl.cpp @@ -77,6 +77,8 @@ namespace bgfx success = eglMakeCurrent(m_display, m_surface, m_surface, m_context); BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context."); + eglSwapInterval(m_display, 0); + # if BX_PLATFORM_EMSCRIPTEN emscripten_set_canvas_size(_width, _height); # endif // BX_PLATFORM_EMSCRIPTEN @@ -93,8 +95,9 @@ namespace bgfx m_context = NULL; } - void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/) + void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, bool _vsync) { + eglSwapInterval(m_display, _vsync ? 1 : 0); } void GlContext::swap() diff --git a/src/glcontext_egl.h b/src/glcontext_egl.h index 1fe35748..73cc0337 100644 --- a/src/glcontext_egl.h +++ b/src/glcontext_egl.h @@ -23,7 +23,7 @@ namespace bgfx void create(uint32_t _width, uint32_t _height); void destroy(); - void resize(uint32_t _width, uint32_t _height); + void resize(uint32_t _width, uint32_t _height, bool _vsync); void swap(); void import(); diff --git a/src/glcontext_glx.cpp b/src/glcontext_glx.cpp index 04f0f930..2874b5e0 100644 --- a/src/glcontext_glx.cpp +++ b/src/glcontext_glx.cpp @@ -12,6 +12,10 @@ namespace bgfx { + PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB; + PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT; + PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI; + # define GL_IMPORT(_optional, _proto, _func) _proto _func # include "glimports.h" # undef GL_IMPORT @@ -119,7 +123,7 @@ namespace bgfx XFree(visualInfo); #if BGFX_CONFIG_RENDERER_OPENGL >= 31 - PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress( (const GLubyte*)"glXCreateContextAttribsARB"); + glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress( (const GLubyte*)"glXCreateContextAttribsARB"); if (NULL != glXCreateContextAttribsARB) { BX_TRACE("Create GL 3.1 context."); @@ -147,7 +151,7 @@ namespace bgfx import(); - PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT"); + glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT"); if (NULL != glXSwapIntervalEXT) { BX_TRACE("Using glXSwapIntervalEXT."); @@ -155,7 +159,7 @@ namespace bgfx } else { - PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalSGI"); + glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalSGI"); if (NULL != glXSwapIntervalSGI) { BX_TRACE("Using glXSwapIntervalSGI."); @@ -176,8 +180,18 @@ namespace bgfx glXDestroyContext(s_display, m_context); } - void GlContext::resize(uint32_t _width, uint32_t _height) + void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, bool _vsync) { + int32_t interval = _vsync ? 1 : 0; + + if (NULL != glXSwapIntervalEXT) + { + glXSwapIntervalEXT(s_display, 0, interval); + } + else if (NULL != glXSwapIntervalSGI) + { + glXSwapIntervalSGI(interval); + } } void GlContext::swap() diff --git a/src/glcontext_glx.h b/src/glcontext_glx.h index ff9e09e5..2c2e6167 100644 --- a/src/glcontext_glx.h +++ b/src/glcontext_glx.h @@ -22,7 +22,7 @@ namespace bgfx void create(uint32_t _width, uint32_t _height); void destroy(); - void resize(uint32_t _width, uint32_t _height); + void resize(uint32_t _width, uint32_t _height, bool _vsync); void swap(); void import(); diff --git a/src/glcontext_nsgl.h b/src/glcontext_nsgl.h index e779c834..b8e7c1b9 100755 --- a/src/glcontext_nsgl.h +++ b/src/glcontext_nsgl.h @@ -19,7 +19,7 @@ namespace bgfx void create(uint32_t _width, uint32_t _height); void destroy(); - void resize(uint32_t _width, uint32_t _height); + void resize(uint32_t _width, uint32_t _height, bool _vsync); void swap(); void import(); diff --git a/src/glcontext_ppapi.cpp b/src/glcontext_ppapi.cpp index e6be1983..3ba6a622 100644 --- a/src/glcontext_ppapi.cpp +++ b/src/glcontext_ppapi.cpp @@ -47,7 +47,7 @@ namespace bgfx { } - void GlContext::resize(uint32_t _width, uint32_t _height) + void GlContext::resize(uint32_t _width, uint32_t _height, bool /*_vsync*/) { m_graphicsInterface->ResizeBuffers(m_context, _width, _height); } diff --git a/src/glcontext_ppapi.h b/src/glcontext_ppapi.h index 222d86fc..053fde2e 100644 --- a/src/glcontext_ppapi.h +++ b/src/glcontext_ppapi.h @@ -28,7 +28,7 @@ namespace bgfx void create(uint32_t _width, uint32_t _height); void destroy(); - void resize(uint32_t _width, uint32_t _height); + void resize(uint32_t _width, uint32_t _height, bool _vsync); void swap(); void import(); diff --git a/src/glcontext_wgl.cpp b/src/glcontext_wgl.cpp index d46c9728..b0b57b13 100644 --- a/src/glcontext_wgl.cpp +++ b/src/glcontext_wgl.cpp @@ -238,8 +238,12 @@ namespace bgfx m_opengl32dll = NULL; } - void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/) + void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, bool _vsync) { + if (NULL != wglSwapIntervalEXT) + { + wglSwapIntervalEXT(_vsync ? 1 : 0); + } } void GlContext::swap() diff --git a/src/glcontext_wgl.h b/src/glcontext_wgl.h index 8aee18fb..44044681 100644 --- a/src/glcontext_wgl.h +++ b/src/glcontext_wgl.h @@ -66,7 +66,7 @@ namespace bgfx void create(uint32_t _width, uint32_t _height); void destroy(); - void resize(uint32_t _width, uint32_t _height); + void resize(uint32_t _width, uint32_t _height, bool _vsync); void swap(); void import(); diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index c1abfe0b..314dff30 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -291,7 +291,8 @@ namespace bgfx uint32_t msaa = 1<<( (m_resolution.m_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT); msaa = uint32_min(m_maxMsaa, msaa == 0 ? 0 : 1<