Selaa lähdekoodia

Fixed GLFW3 integration on Linux.

Branimir Karadžić 10 vuotta sitten
vanhempi
sitoutus
4b60ddf44d
6 muutettua tiedostoa jossa 121 lisäystä ja 96 poistoa
  1. 4 3
      include/bgfxplatform.h
  2. 7 1
      scripts/bgfx.lua
  3. 9 0
      scripts/genie.lua
  4. 3 1
      src/bgfx.cpp
  5. 1 0
      src/bgfx_p.h
  6. 97 91
      src/glcontext_glx.cpp

+ 4 - 3
include/bgfxplatform.h

@@ -55,7 +55,7 @@ namespace bgfx
 namespace bgfx
 {
 	///
-	void x11SetDisplayWindow(void* _display, uint32_t _window);
+	void x11SetDisplayWindow(void* _display, uint32_t _window, void* _glx = NULL);
 
 } // namespace bgfx
 
@@ -155,8 +155,9 @@ namespace bgfx
 	{
 #	if BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD
 		::Display* display = glfwGetX11Display();
-		::Window window    = glfwGetX11Window(_window);
-		x11SetDisplayWindow(display, window);
+		::Window   window  = glfwGetX11Window(_window);
+		void* glx          = glfwGetGLXContext(_window);
+		x11SetDisplayWindow(display, window, glx);
 #	elif BX_PLATFORM_OSX
 		void* window = glfwGetCocoaWindow(_window);
 		void* nsgl   = glfwGetNSGLContext(_window);

+ 7 - 1
scripts/bgfx.lua

@@ -26,7 +26,7 @@ function bgfxProject(_name, _kind, _defines)
 				}
 
 			configuration { "linux-*" }
-				buildoptions { 
+				buildoptions {
 					"-fPIC",
 				}
 
@@ -42,6 +42,12 @@ function bgfxProject(_name, _kind, _defines)
 			_defines,
 		}
 
+		if _OPTIONS["with-glfw"] then
+			defines {
+				"BGFX_CONFIG_MULTITHREADED=0",
+			}
+		end
+
 		if _OPTIONS["with-ovr"] then
 			defines {
 				"BGFX_CONFIG_USE_OVR=1",

+ 9 - 0
scripts/genie.lua

@@ -134,6 +134,15 @@ function exampleProject(_name)
 			"glfw3"
 		}
 
+		configuration { "linux" }
+			links {
+				"Xrandr",
+				"Xinerama",
+				"Xi",
+				"Xxf86vm",
+				"Xcursor",
+			}
+
 		configuration { "osx" }
 			linkoptions {
 				"-framework CoreVideo",

+ 3 - 1
src/bgfx.cpp

@@ -44,11 +44,13 @@ namespace bgfx
 #elif BX_PLATFORM_LINUX
 	void*    g_bgfxX11Display;
 	uint32_t g_bgfxX11Window;
+	void*    g_bgfxGLX;
 
-	void x11SetDisplayWindow(void* _display, uint32_t _window)
+	void x11SetDisplayWindow(void* _display, uint32_t _window, void* _glx)
 	{
 		g_bgfxX11Display = _display;
 		g_bgfxX11Window  = _window;
+		g_bgfxGLX        = _glx;
 	}
 #elif BX_PLATFORM_OSX
 	void* g_bgfxNSWindow = NULL;

+ 1 - 0
src/bgfx_p.h

@@ -204,6 +204,7 @@ namespace bgfx
 #elif BX_PLATFORM_LINUX
 	extern void*    g_bgfxX11Display;
 	extern uint32_t g_bgfxX11Window;
+	extern void*    g_bgfxGLX;
 #elif BX_PLATFORM_OSX
 	extern void* g_bgfxNSWindow;
 	extern void* g_bgfxNSGL;

+ 97 - 91
src/glcontext_glx.cpp

@@ -55,122 +55,128 @@ namespace bgfx { namespace gl
 	void GlContext::create(uint32_t _width, uint32_t _height)
 	{
 		BX_UNUSED(_width, _height);
-		XLockDisplay( (::Display*)g_bgfxX11Display);
 
-		int major, minor;
-		bool version = glXQueryVersion( (::Display*)g_bgfxX11Display, &major, &minor);
-		BGFX_FATAL(version, Fatal::UnableToInitialize, "Failed to query GLX version");
-		BGFX_FATAL( (major == 1 && minor >= 2) || major > 1
-				, Fatal::UnableToInitialize
-				, "GLX version is not >=1.2 (%d.%d)."
-				, major
-				, minor
-				);
+		m_context = (GLXContext)g_bgfxGLX;
 
-		int32_t screen = DefaultScreen( (::Display*)g_bgfxX11Display);
+		if (NULL == m_context)
+		{
+			XLockDisplay( (::Display*)g_bgfxX11Display);
 
-		const char* extensions = glXQueryExtensionsString( (::Display*)g_bgfxX11Display, screen);
-		BX_TRACE("GLX extensions:");
-		dumpExtensions(extensions);
+			int major, minor;
+			bool version = glXQueryVersion( (::Display*)g_bgfxX11Display, &major, &minor);
+			BGFX_FATAL(version, Fatal::UnableToInitialize, "Failed to query GLX version");
+			BGFX_FATAL( (major == 1 && minor >= 2) || major > 1
+					, Fatal::UnableToInitialize
+					, "GLX version is not >=1.2 (%d.%d)."
+					, major
+					, minor
+					);
 
-		const int attrsGlx[] =
-		{
-			GLX_RENDER_TYPE, GLX_RGBA_BIT,
-			GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
-			GLX_DOUBLEBUFFER, true,
-			GLX_RED_SIZE, 8,
-			GLX_BLUE_SIZE, 8,
-			GLX_GREEN_SIZE, 8,
-//			GLX_ALPHA_SIZE, 8,
-			GLX_DEPTH_SIZE, 24,
-			GLX_STENCIL_SIZE, 8,
-			0,
-		};
-
-		// Find suitable config
-		GLXFBConfig bestConfig = NULL;
-
-		int numConfigs;
-		GLXFBConfig* configs = glXChooseFBConfig( (::Display*)g_bgfxX11Display, screen, attrsGlx, &numConfigs);
-
-		BX_TRACE("glX num configs %d", numConfigs);
-
-		for (int ii = 0; ii < numConfigs; ++ii)
-		{
-			m_visualInfo = glXGetVisualFromFBConfig( (::Display*)g_bgfxX11Display, configs[ii]);
-			if (NULL != m_visualInfo)
+			int32_t screen = DefaultScreen( (::Display*)g_bgfxX11Display);
+
+			const char* extensions = glXQueryExtensionsString( (::Display*)g_bgfxX11Display, screen);
+			BX_TRACE("GLX extensions:");
+			dumpExtensions(extensions);
+
+			const int attrsGlx[] =
 			{
-				BX_TRACE("---");
-				bool valid = true;
-				for (uint32_t attr = 6; attr < BX_COUNTOF(attrsGlx)-1 && attrsGlx[attr] != None; attr += 2)
+				GLX_RENDER_TYPE, GLX_RGBA_BIT,
+				GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
+				GLX_DOUBLEBUFFER, true,
+				GLX_RED_SIZE, 8,
+				GLX_BLUE_SIZE, 8,
+				GLX_GREEN_SIZE, 8,
+				//			GLX_ALPHA_SIZE, 8,
+				GLX_DEPTH_SIZE, 24,
+				GLX_STENCIL_SIZE, 8,
+				0,
+			};
+
+			// Find suitable config
+			GLXFBConfig bestConfig = NULL;
+
+			int numConfigs;
+			GLXFBConfig* configs = glXChooseFBConfig( (::Display*)g_bgfxX11Display, screen, attrsGlx, &numConfigs);
+
+			BX_TRACE("glX num configs %d", numConfigs);
+
+			for (int ii = 0; ii < numConfigs; ++ii)
+			{
+				m_visualInfo = glXGetVisualFromFBConfig( (::Display*)g_bgfxX11Display, configs[ii]);
+				if (NULL != m_visualInfo)
 				{
-					int value;
-					glXGetFBConfigAttrib( (::Display*)g_bgfxX11Display, configs[ii], attrsGlx[attr], &value);
-					BX_TRACE("glX %d/%d %2d: %4x, %8x (%8x%s)"
-							, ii
-							, numConfigs
-							, attr/2
-							, attrsGlx[attr]
-							, value
-							, attrsGlx[attr + 1]
-							, value < attrsGlx[attr + 1] ? " *" : ""
-							);
-
-					if (value < attrsGlx[attr + 1])
+					BX_TRACE("---");
+					bool valid = true;
+					for (uint32_t attr = 6; attr < BX_COUNTOF(attrsGlx)-1 && attrsGlx[attr] != None; attr += 2)
 					{
-						valid = false;
+						int value;
+						glXGetFBConfigAttrib( (::Display*)g_bgfxX11Display, configs[ii], attrsGlx[attr], &value);
+						BX_TRACE("glX %d/%d %2d: %4x, %8x (%8x%s)"
+								, ii
+								, numConfigs
+								, attr/2
+								, attrsGlx[attr]
+								, value
+								, attrsGlx[attr + 1]
+								, value < attrsGlx[attr + 1] ? " *" : ""
+								);
+
+						if (value < attrsGlx[attr + 1])
+						{
+							valid = false;
 #if !BGFX_CONFIG_DEBUG
-						break;
+							break;
 #endif // BGFX_CONFIG_DEBUG
+						}
 					}
-				}
 
-				if (valid)
-				{
-					bestConfig = configs[ii];
-					BX_TRACE("Best config %d.", ii);
-					break;
+					if (valid)
+					{
+						bestConfig = configs[ii];
+						BX_TRACE("Best config %d.", ii);
+						break;
+					}
 				}
-			}
 
-			XFree(m_visualInfo);
-			m_visualInfo = NULL;
-		}
+				XFree(m_visualInfo);
+				m_visualInfo = NULL;
+			}
 
-		XFree(configs);
-		BGFX_FATAL(m_visualInfo, Fatal::UnableToInitialize, "Failed to find a suitable X11 display configuration.");
+			XFree(configs);
+			BGFX_FATAL(m_visualInfo, Fatal::UnableToInitialize, "Failed to find a suitable X11 display configuration.");
 
-		BX_TRACE("Create GL 2.1 context.");
-		m_context = glXCreateContext( (::Display*)g_bgfxX11Display, m_visualInfo, 0, GL_TRUE);
-		BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create GL 2.1 context.");
+			BX_TRACE("Create GL 2.1 context.");
+			m_context = glXCreateContext( (::Display*)g_bgfxX11Display, m_visualInfo, 0, GL_TRUE);
+			BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create GL 2.1 context.");
 
 #if BGFX_CONFIG_RENDERER_OPENGL >= 31
-		glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress( (const GLubyte*)"glXCreateContextAttribsARB");
+			glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress( (const GLubyte*)"glXCreateContextAttribsARB");
 
-		if (NULL != glXCreateContextAttribsARB)
-		{
-			BX_TRACE("Create GL 3.1 context.");
-			const int contextAttrs[] =
+			if (NULL != glXCreateContextAttribsARB)
 			{
-				GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
-				GLX_CONTEXT_MINOR_VERSION_ARB, 1,
-				GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
-				0,
-			};
+				BX_TRACE("Create GL 3.1 context.");
+				const int contextAttrs[] =
+				{
+					GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
+					GLX_CONTEXT_MINOR_VERSION_ARB, 1,
+					GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
+					0,
+				};
 
-			GLXContext context = glXCreateContextAttribsARB( (::Display*)g_bgfxX11Display, bestConfig, 0, true, contextAttrs);
+				GLXContext context = glXCreateContextAttribsARB( (::Display*)g_bgfxX11Display, bestConfig, 0, true, contextAttrs);
 
-			if (NULL != context)
-			{
-				glXDestroyContext( (::Display*)g_bgfxX11Display, m_context);
-				m_context = context;
+				if (NULL != context)
+				{
+					glXDestroyContext( (::Display*)g_bgfxX11Display, m_context);
+					m_context = context;
+				}
 			}
-		}
 #else
-		BX_UNUSED(bestConfig);
+			BX_UNUSED(bestConfig);
 #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
 
-		XUnlockDisplay( (::Display*)g_bgfxX11Display);
+			XUnlockDisplay( (::Display*)g_bgfxX11Display);
+		}
 
 		import();