GL: Cache current GL context. Issue #262.

This commit is contained in:
Branimir Karadžić 2015-02-17 11:50:31 -08:00
parent a57634cd09
commit 7279a31768
6 changed files with 60 additions and 36 deletions

View file

@ -254,6 +254,7 @@ EGL_IMPORT
success = eglMakeCurrent(m_display, m_surface, m_surface, m_context); success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context."); BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context.");
m_current = NULL;
eglSwapInterval(m_display, 0); eglSwapInterval(m_display, 0);
@ -311,20 +312,24 @@ EGL_IMPORT
void GlContext::swap(SwapChainGL* _swapChain) void GlContext::swap(SwapChainGL* _swapChain)
{ {
makeCurrent(_swapChain);
if (NULL == _swapChain) if (NULL == _swapChain)
{ {
eglMakeCurrent(m_display, m_surface, m_surface, m_context);
eglSwapBuffers(m_display, m_surface); eglSwapBuffers(m_display, m_surface);
} }
else else
{ {
_swapChain->makeCurrent();
_swapChain->swapBuffers(); _swapChain->swapBuffers();
} }
} }
void GlContext::makeCurrent(SwapChainGL* _swapChain) void GlContext::makeCurrent(SwapChainGL* _swapChain)
{ {
if (m_current != _swapChain)
{
m_current = _swapChain;
if (NULL == _swapChain) if (NULL == _swapChain)
{ {
eglMakeCurrent(m_display, m_surface, m_surface, m_context); eglMakeCurrent(m_display, m_surface, m_surface, m_context);
@ -334,6 +339,7 @@ EGL_IMPORT
_swapChain->makeCurrent(); _swapChain->makeCurrent();
} }
} }
}
void GlContext::import() void GlContext::import()
{ {

View file

@ -17,7 +17,8 @@ namespace bgfx
struct GlContext struct GlContext
{ {
GlContext() GlContext()
: m_context(NULL) : m_current(NULL)
, m_context(NULL)
, m_display(NULL) , m_display(NULL)
, m_surface(NULL) , m_surface(NULL)
{ {
@ -41,6 +42,7 @@ namespace bgfx
} }
void* m_eglLibrary; void* m_eglLibrary;
SwapChainGL* m_current;
EGLConfig m_config; EGLConfig m_config;
EGLContext m_context; EGLContext m_context;
EGLDisplay m_display; EGLDisplay m_display;

View file

@ -175,6 +175,7 @@ namespace bgfx
import(); import();
glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context); glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context);
m_current = NULL;
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT"); glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT");
if (NULL != glXSwapIntervalEXT) if (NULL != glXSwapIntervalEXT)
@ -248,20 +249,24 @@ namespace bgfx
void GlContext::swap(SwapChainGL* _swapChain) void GlContext::swap(SwapChainGL* _swapChain)
{ {
makeCurrent(_swapChain);
if (NULL == _swapChain) if (NULL == _swapChain)
{ {
glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context);
glXSwapBuffers( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window); glXSwapBuffers( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window);
} }
else else
{ {
_swapChain->makeCurrent();
_swapChain->swapBuffers(); _swapChain->swapBuffers();
} }
} }
void GlContext::makeCurrent(SwapChainGL* _swapChain) void GlContext::makeCurrent(SwapChainGL* _swapChain)
{ {
if (m_current != _swapChain)
{
m_current = _swapChain;
if (NULL == _swapChain) if (NULL == _swapChain)
{ {
glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context); glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context);
@ -271,6 +276,7 @@ namespace bgfx
_swapChain->makeCurrent(); _swapChain->makeCurrent();
} }
} }
}
void GlContext::import() void GlContext::import()
{ {

View file

@ -18,7 +18,8 @@ namespace bgfx
struct GlContext struct GlContext
{ {
GlContext() GlContext()
: m_context(0) : m_current(NULL)
, m_context(0)
, m_visualInfo(NULL) , m_visualInfo(NULL)
{ {
} }
@ -40,6 +41,7 @@ namespace bgfx
return 0 != m_context; return 0 != m_context;
} }
SwapChainGL* m_current;
GLXContext m_context; GLXContext m_context;
XVisualInfo* m_visualInfo; XVisualInfo* m_visualInfo;
}; };

View file

@ -259,6 +259,7 @@ namespace bgfx
int result = wglMakeCurrent(m_hdc, m_context); int result = wglMakeCurrent(m_hdc, m_context);
BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!"); BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!");
m_current = NULL;
if (NULL != wglSwapIntervalEXT) if (NULL != wglSwapIntervalEXT)
{ {
@ -316,8 +317,29 @@ namespace bgfx
BX_DELETE(g_allocator, _swapChain); BX_DELETE(g_allocator, _swapChain);
} }
void GlContext::swap(SwapChainGL* _swapChain)
{
makeCurrent(_swapChain);
if (NULL == _swapChain)
{
if (NULL != g_bgfxHwnd)
{
SwapBuffers(m_hdc);
}
}
else
{
_swapChain->swapBuffers();
}
}
void GlContext::makeCurrent(SwapChainGL* _swapChain) void GlContext::makeCurrent(SwapChainGL* _swapChain)
{ {
if (m_current != _swapChain)
{
m_current = _swapChain;
if (NULL == _swapChain) if (NULL == _swapChain)
{ {
wglMakeCurrent(m_hdc, m_context); wglMakeCurrent(m_hdc, m_context);
@ -329,22 +351,6 @@ namespace bgfx
_swapChain->makeCurrent(); _swapChain->makeCurrent();
} }
} }
void GlContext::swap(SwapChainGL* _swapChain)
{
if (NULL == _swapChain)
{
if (NULL != g_bgfxHwnd)
{
wglMakeCurrent(m_hdc, m_context);
SwapBuffers(m_hdc);
}
}
else
{
_swapChain->makeCurrent();
_swapChain->swapBuffers();
}
} }
void GlContext::import() void GlContext::import()

View file

@ -61,7 +61,8 @@ typedef void (APIENTRYP PFNGLSTENCILOPPROC) (GLenum fail, GLenum zfail, GLenum z
struct GlContext struct GlContext
{ {
GlContext() GlContext()
: m_opengl32dll(NULL) : m_current(NULL)
, m_opengl32dll(NULL)
, m_context(NULL) , m_context(NULL)
, m_hdc(NULL) , m_hdc(NULL)
{ {
@ -87,6 +88,7 @@ typedef void (APIENTRYP PFNGLSTENCILOPPROC) (GLenum fail, GLenum zfail, GLenum z
int32_t m_contextAttrs[9]; int32_t m_contextAttrs[9];
int m_pixelFormat; int m_pixelFormat;
PIXELFORMATDESCRIPTOR m_pfd; PIXELFORMATDESCRIPTOR m_pfd;
SwapChainGL* m_current;
void* m_opengl32dll; void* m_opengl32dll;
HGLRC m_context; HGLRC m_context;
HDC m_hdc; HDC m_hdc;