Просмотр исходного кода

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
Doug Binks 5 лет назад
Родитель
Сommit
ab036d2a05
2 измененных файлов с 36 добавлено и 7 удалено
  1. 10 3
      src/glcontext_nsgl.mm
  2. 26 4
      src/glcontext_wgl.cpp

+ 10 - 3
src/glcontext_nsgl.mm

@@ -150,6 +150,10 @@ namespace bgfx { namespace gl
 			m_view    = glView;
 			m_view    = glView;
 			m_context = glContext;
 			m_context = glContext;
 		}
 		}
+        else
+        {
+            [g_platformData.context makeCurrentContext];
+        }
 
 
 		import();
 		import();
 
 
@@ -175,9 +179,12 @@ namespace bgfx { namespace gl
 
 
 #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
 #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
 		bool hidpi = !!(_flags&BGFX_RESET_HIDPI);
 		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)
 #endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
 
 
 		bool vsync = !!(_flags&BGFX_RESET_VSYNC);
 		bool vsync = !!(_flags&BGFX_RESET_VSYNC);

+ 26 - 4
src/glcontext_wgl.cpp

@@ -109,14 +109,32 @@ namespace bgfx { namespace gl
 		wglGetProcAddress = (PFNWGLGETPROCADDRESSPROC)bx::dlsym(m_opengl32dll, "wglGetProcAddress");
 		wglGetProcAddress = (PFNWGLGETPROCADDRESSPROC)bx::dlsym(m_opengl32dll, "wglGetProcAddress");
 		BGFX_FATAL(NULL != wglGetProcAddress, Fatal::UnableToInitialize, "Failed get wglGetProcAddress.");
 		BGFX_FATAL(NULL != wglGetProcAddress, Fatal::UnableToInitialize, "Failed get wglGetProcAddress.");
 
 
+
+
 		// If g_platformHooks.nwh is NULL, the assumption is that GL context was created
 		// If g_platformHooks.nwh is NULL, the assumption is that GL context was created
 		// by user (for example, using SDL, GLFW, etc.)
 		// by user (for example, using SDL, GLFW, etc.)
 		BX_WARN(NULL != g_platformData.nwh
 		BX_WARN(NULL != g_platformData.nwh
 			, "bgfx::setPlatform with valid window is not called. This might "
 			, "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");
 			wglMakeCurrent = (PFNWGLMAKECURRENTPROC)bx::dlsym(m_opengl32dll, "wglMakeCurrent");
 			BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent.");
 			BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent.");
@@ -283,8 +301,12 @@ namespace bgfx { namespace gl
 		{
 		{
 			wglMakeCurrent(NULL, NULL);
 			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);
 			ReleaseDC( (HWND)g_platformData.nwh, m_hdc);
 			m_hdc = NULL;
 			m_hdc = NULL;