From 7279a31768300003416f63a928946d38d4c2906e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Tue, 17 Feb 2015 11:50:31 -0800 Subject: [PATCH] GL: Cache current GL context. Issue #262. --- src/glcontext_egl.cpp | 22 ++++++++++++++-------- src/glcontext_egl.h | 4 +++- src/glcontext_glx.cpp | 22 ++++++++++++++-------- src/glcontext_glx.h | 4 +++- src/glcontext_wgl.cpp | 40 +++++++++++++++++++++++----------------- src/glcontext_wgl.h | 4 +++- 6 files changed, 60 insertions(+), 36 deletions(-) diff --git a/src/glcontext_egl.cpp b/src/glcontext_egl.cpp index 9ef95846..1a70e37b 100644 --- a/src/glcontext_egl.cpp +++ b/src/glcontext_egl.cpp @@ -254,6 +254,7 @@ EGL_IMPORT success = eglMakeCurrent(m_display, m_surface, m_surface, m_context); BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context."); + m_current = NULL; eglSwapInterval(m_display, 0); @@ -311,27 +312,32 @@ EGL_IMPORT void GlContext::swap(SwapChainGL* _swapChain) { + makeCurrent(_swapChain); + if (NULL == _swapChain) { - eglMakeCurrent(m_display, m_surface, m_surface, m_context); eglSwapBuffers(m_display, m_surface); } else { - _swapChain->makeCurrent(); _swapChain->swapBuffers(); } } void GlContext::makeCurrent(SwapChainGL* _swapChain) { - if (NULL == _swapChain) + if (m_current != _swapChain) { - eglMakeCurrent(m_display, m_surface, m_surface, m_context); - } - else - { - _swapChain->makeCurrent(); + m_current = _swapChain; + + if (NULL == _swapChain) + { + eglMakeCurrent(m_display, m_surface, m_surface, m_context); + } + else + { + _swapChain->makeCurrent(); + } } } diff --git a/src/glcontext_egl.h b/src/glcontext_egl.h index e18aaa71..98fd63f7 100644 --- a/src/glcontext_egl.h +++ b/src/glcontext_egl.h @@ -17,7 +17,8 @@ namespace bgfx struct GlContext { GlContext() - : m_context(NULL) + : m_current(NULL) + , m_context(NULL) , m_display(NULL) , m_surface(NULL) { @@ -41,6 +42,7 @@ namespace bgfx } void* m_eglLibrary; + SwapChainGL* m_current; EGLConfig m_config; EGLContext m_context; EGLDisplay m_display; diff --git a/src/glcontext_glx.cpp b/src/glcontext_glx.cpp index 1168672c..af40d2d9 100644 --- a/src/glcontext_glx.cpp +++ b/src/glcontext_glx.cpp @@ -175,6 +175,7 @@ namespace bgfx import(); glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context); + m_current = NULL; glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT"); if (NULL != glXSwapIntervalEXT) @@ -248,27 +249,32 @@ namespace bgfx void GlContext::swap(SwapChainGL* _swapChain) { + makeCurrent(_swapChain); + if (NULL == _swapChain) { - glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context); glXSwapBuffers( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window); } else { - _swapChain->makeCurrent(); _swapChain->swapBuffers(); } } void GlContext::makeCurrent(SwapChainGL* _swapChain) { - if (NULL == _swapChain) + if (m_current != _swapChain) { - glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context); - } - else - { - _swapChain->makeCurrent(); + m_current = _swapChain; + + if (NULL == _swapChain) + { + glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context); + } + else + { + _swapChain->makeCurrent(); + } } } diff --git a/src/glcontext_glx.h b/src/glcontext_glx.h index 556f10b2..3fa7dbf2 100644 --- a/src/glcontext_glx.h +++ b/src/glcontext_glx.h @@ -18,7 +18,8 @@ namespace bgfx struct GlContext { GlContext() - : m_context(0) + : m_current(NULL) + , m_context(0) , m_visualInfo(NULL) { } @@ -40,6 +41,7 @@ namespace bgfx return 0 != m_context; } + SwapChainGL* m_current; GLXContext m_context; XVisualInfo* m_visualInfo; }; diff --git a/src/glcontext_wgl.cpp b/src/glcontext_wgl.cpp index 85a8fe2b..56aad127 100644 --- a/src/glcontext_wgl.cpp +++ b/src/glcontext_wgl.cpp @@ -184,7 +184,7 @@ namespace bgfx int result; uint32_t numFormats = 0; - do + do { result = wglChoosePixelFormatARB(m_hdc, attrs, NULL, 1, &m_pixelFormat, &numFormats); if (0 == result @@ -259,6 +259,7 @@ namespace bgfx int result = wglMakeCurrent(m_hdc, m_context); BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!"); + m_current = NULL; if (NULL != wglSwapIntervalEXT) { @@ -316,37 +317,42 @@ namespace bgfx BX_DELETE(g_allocator, _swapChain); } - void GlContext::makeCurrent(SwapChainGL* _swapChain) - { - if (NULL == _swapChain) - { - wglMakeCurrent(m_hdc, m_context); - GLenum err = glGetError(); - BX_WARN(0 == err, "wglMakeCurrent failed with GL error: 0x%04x.", err); BX_UNUSED(err); - } - else - { - _swapChain->makeCurrent(); - } - } - void GlContext::swap(SwapChainGL* _swapChain) { + makeCurrent(_swapChain); + if (NULL == _swapChain) { if (NULL != g_bgfxHwnd) { - wglMakeCurrent(m_hdc, m_context); SwapBuffers(m_hdc); } } else { - _swapChain->makeCurrent(); _swapChain->swapBuffers(); } } + void GlContext::makeCurrent(SwapChainGL* _swapChain) + { + if (m_current != _swapChain) + { + m_current = _swapChain; + + if (NULL == _swapChain) + { + wglMakeCurrent(m_hdc, m_context); + GLenum err = glGetError(); + BX_WARN(0 == err, "wglMakeCurrent failed with GL error: 0x%04x.", err); BX_UNUSED(err); + } + else + { + _swapChain->makeCurrent(); + } + } + } + void GlContext::import() { BX_TRACE("Import:"); diff --git a/src/glcontext_wgl.h b/src/glcontext_wgl.h index cda9c149..4d0f64fb 100644 --- a/src/glcontext_wgl.h +++ b/src/glcontext_wgl.h @@ -61,7 +61,8 @@ typedef void (APIENTRYP PFNGLSTENCILOPPROC) (GLenum fail, GLenum zfail, GLenum z struct GlContext { GlContext() - : m_opengl32dll(NULL) + : m_current(NULL) + , m_opengl32dll(NULL) , m_context(NULL) , m_hdc(NULL) { @@ -87,6 +88,7 @@ typedef void (APIENTRYP PFNGLSTENCILOPPROC) (GLenum fail, GLenum zfail, GLenum z int32_t m_contextAttrs[9]; int m_pixelFormat; PIXELFORMATDESCRIPTOR m_pfd; + SwapChainGL* m_current; void* m_opengl32dll; HGLRC m_context; HDC m_hdc;