From ab036d2a058d20069b29e1c0a275a476edd99e22 Mon Sep 17 00:00:00 2001 From: Doug Binks Date: Tue, 25 Aug 2020 15:47:46 +0100 Subject: [PATCH] Add support for non main thread rendering for WGL and NSGL when developer provides context (#2241) * WGL support for passing a context when using renderFrame on non main thread * NSGL support for passing a context when using renderFrame on non main thread * WGL: Moved no window warning and added check prior to calling GetDC --- src/glcontext_nsgl.mm | 13 ++++++++++--- src/glcontext_wgl.cpp | 30 ++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/glcontext_nsgl.mm b/src/glcontext_nsgl.mm index 4e78b1670..838acff04 100644 --- a/src/glcontext_nsgl.mm +++ b/src/glcontext_nsgl.mm @@ -150,6 +150,10 @@ namespace bgfx { namespace gl m_view = glView; m_context = glContext; } + else + { + [g_platformData.context makeCurrentContext]; + } import(); @@ -175,9 +179,12 @@ namespace bgfx { namespace gl #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070) bool hidpi = !!(_flags&BGFX_RESET_HIDPI); - NSOpenGLView* glView = (NSOpenGLView*)m_view; - if ([glView respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)]) - [glView setWantsBestResolutionOpenGLSurface:hidpi]; + if (m_view) + { + NSOpenGLView* glView = (NSOpenGLView*)m_view; + if ([glView respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)]) + [glView setWantsBestResolutionOpenGLSurface:hidpi]; + } #endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070) bool vsync = !!(_flags&BGFX_RESET_VSYNC); diff --git a/src/glcontext_wgl.cpp b/src/glcontext_wgl.cpp index f179e640a..74cd755d5 100644 --- a/src/glcontext_wgl.cpp +++ b/src/glcontext_wgl.cpp @@ -109,14 +109,32 @@ namespace bgfx { namespace gl wglGetProcAddress = (PFNWGLGETPROCADDRESSPROC)bx::dlsym(m_opengl32dll, "wglGetProcAddress"); BGFX_FATAL(NULL != wglGetProcAddress, Fatal::UnableToInitialize, "Failed get wglGetProcAddress."); + + // If g_platformHooks.nwh is NULL, the assumption is that GL context was created // by user (for example, using SDL, GLFW, etc.) BX_WARN(NULL != g_platformData.nwh , "bgfx::setPlatform with valid window is not called. This might " - "be intentional when GL context is created by the user." + "be intentional when GL context is created by the user." ); - if (NULL != g_platformData.nwh) + if (NULL != g_platformData.nwh && NULL != g_platformData.context ) + { + // user has provided a context and a window + wglMakeCurrent = (PFNWGLMAKECURRENTPROC)bx::dlsym(m_opengl32dll, "wglMakeCurrent"); + BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent."); + + m_hdc = GetDC( (HWND)g_platformData.nwh); + BGFX_FATAL(NULL != m_hdc, Fatal::UnableToInitialize, "GetDC failed!"); + + HGLRC context = (HGLRC)g_platformData.context; + int result = wglMakeCurrent(m_hdc, context ); + BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!"); + + m_context = context; + } + + if (NULL != g_platformData.nwh && NULL == g_platformData.context ) { wglMakeCurrent = (PFNWGLMAKECURRENTPROC)bx::dlsym(m_opengl32dll, "wglMakeCurrent"); BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent."); @@ -283,8 +301,12 @@ namespace bgfx { namespace gl { wglMakeCurrent(NULL, NULL); - wglDeleteContext(m_context); - m_context = NULL; + if (NULL == g_platformData.context) + { + wglDeleteContext(m_context); + m_context = NULL; + + } ReleaseDC( (HWND)g_platformData.nwh, m_hdc); m_hdc = NULL;