From c65f04ae05848f537364510a80b10343b58f6621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sat, 31 Jan 2015 13:47:27 -0800 Subject: [PATCH] EGL: Added ability to use EGL on Linux. --- src/bgfx.cpp | 19 ++++++++++++++++++- src/bgfx_p.h | 3 +++ src/glcontext_egl.cpp | 12 ++++++++---- src/glcontext_glx.cpp | 41 ++++++++++++++++++----------------------- src/glcontext_glx.h | 4 ++-- src/renderer_gl.h | 21 +++++++++++++++++++-- 6 files changed, 68 insertions(+), 32 deletions(-) diff --git a/src/bgfx.cpp b/src/bgfx.cpp index f254ac7b..ccc6a86b 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -21,17 +21,27 @@ namespace bgfx #if BX_PLATFORM_ANDROID ::ANativeWindow* g_bgfxAndroidWindow = NULL; + void androidSetWindow(::ANativeWindow* _window) { g_bgfxAndroidWindow = _window; } #elif BX_PLATFORM_IOS void* g_bgfxEaglLayer = NULL; + void iosSetEaglLayer(void* _layer) { g_bgfxEaglLayer = _layer; } +#elif BX_PLATFORM_LINUX + ::Display* g_bgfxX11Display; + ::Window g_bgfxX11Window; + void x11SetDisplayWindow(::Display* _display, ::Window _window) + { + g_bgfxX11Display = _display; + g_bgfxX11Window = _window; + } #elif BX_PLATFORM_OSX void* g_bgfxNSWindow = NULL; @@ -1425,7 +1435,14 @@ again: } else { - _type = RendererType::OpenGL; + if (s_rendererCreator[RendererType::OpenGL].supported) + { + _type = RendererType::OpenGL; + } + else if (s_rendererCreator[RendererType::OpenGLES].supported) + { + _type = RendererType::OpenGLES; + } } if (!s_rendererCreator[_type].supported) diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 00bbf9e8..4562aaaf 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -200,6 +200,9 @@ namespace bgfx extern ::ANativeWindow* g_bgfxAndroidWindow; #elif BX_PLATFORM_IOS extern void* g_bgfxEaglLayer; +#elif BX_PLATFORM_LINUX + extern ::Display* g_bgfxX11Display; + extern ::Window g_bgfxX11Window; #elif BX_PLATFORM_OSX extern void* g_bgfxNSWindow; #elif BX_PLATFORM_WINDOWS diff --git a/src/glcontext_egl.cpp b/src/glcontext_egl.cpp index 392a26aa..9133581f 100644 --- a/src/glcontext_egl.cpp +++ b/src/glcontext_egl.cpp @@ -63,8 +63,8 @@ EGL_IMPORT void* eglOpen() { - void* handle = bx::dlopen("libEGL.dll"); - BGFX_FATAL(NULL != handle, Fatal::UnableToInitialize, "Failed to load libEGL dynamic library."); + void* handle = bx::dlopen("libEGL." BX_DL_EXT); + BGFX_FATAL(NULL != handle, Fatal::UnableToInitialize, "Failed to load libEGL dynamic library. %s", dlerror()); #define EGL_IMPORT_FUNC(_proto, _func) \ _func = (_proto)bx::dlsym(handle, #_func); \ @@ -184,6 +184,9 @@ EGL_IMPORT # if BX_PLATFORM_WINDOWS ndt = GetDC(g_bgfxHwnd); nwh = g_bgfxHwnd; +# elif BX_PLATFORM_LINUX + ndt = g_bgfxX11Display; + nwh = g_bgfxX11Window; # endif // BX_PLATFORM_ m_display = eglGetDisplay(ndt); BGFX_FATAL(m_display != EGL_NO_DISPLAY, Fatal::UnableToInitialize, "Failed to create display %p", m_display); @@ -278,6 +281,7 @@ EGL_IMPORT void GlContext::resize(uint32_t _width, uint32_t _height, bool _vsync) { + BX_UNUSED(_width, _height); # if BX_PLATFORM_ANDROID EGLint format; eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format); @@ -334,8 +338,8 @@ EGL_IMPORT void GlContext::import() { BX_TRACE("Import:"); -# if BX_PLATFORM_WINDOWS - void* glesv2 = bx::dlopen("libGLESv2.dll"); +# if BX_PLATFORM_WINDOWS || BX_PLATFORM_LINUX + void* glesv2 = bx::dlopen("libGLESv2." BX_DL_EXT); # define GL_EXTENSION(_optional, _proto, _func, _import) \ { \ if (NULL == _func) \ diff --git a/src/glcontext_glx.cpp b/src/glcontext_glx.cpp index 4f9c4009..0cb11bfb 100644 --- a/src/glcontext_glx.cpp +++ b/src/glcontext_glx.cpp @@ -7,8 +7,10 @@ #if (BX_PLATFORM_FREEBSD || BX_PLATFORM_LINUX) && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL) # include "renderer_gl.h" -# define GLX_GLXEXT_PROTOTYPES -# include + +# if BGFX_USE_GLX +# define GLX_GLXEXT_PROTOTYPES +# include namespace bgfx { @@ -22,43 +24,34 @@ namespace bgfx # define GL_IMPORT(_optional, _proto, _func, _import) _proto _func # include "glimports.h" - static ::Display* s_display; - static ::Window s_window; - struct SwapChainGL { SwapChainGL(::Window _window, XVisualInfo* _visualInfo, GLXContext _context) : m_window(_window) { - m_context = glXCreateContext(s_display, _visualInfo, _context, GL_TRUE); + m_context = glXCreateContext(g_bgfxX11Display, _visualInfo, _context, GL_TRUE); } ~SwapChainGL() { - glXMakeCurrent(s_display, 0, 0); - glXDestroyContext(s_display, m_context); + glXMakeCurrent(g_bgfxX11Display, 0, 0); + glXDestroyContext(g_bgfxX11Display, m_context); } void makeCurrent() { - glXMakeCurrent(s_display, m_window, m_context); + glXMakeCurrent(g_bgfxX11Display, m_window, m_context); } void swapBuffers() { - glXSwapBuffers(s_display, m_window); + glXSwapBuffers(g_bgfxX11Display, m_window); } Window m_window; GLXContext m_context; }; - void x11SetDisplayWindow(::Display* _display, ::Window _window) - { - s_display = _display; - s_window = _window; - } - void GlContext::create(uint32_t _width, uint32_t _height) { BX_UNUSED(_width, _height); @@ -181,13 +174,13 @@ namespace bgfx import(); - glXMakeCurrent(s_display, s_window, m_context); + glXMakeCurrent(g_bgfxX11Display, g_bgfxX11Window, m_context); glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT"); if (NULL != glXSwapIntervalEXT) { BX_TRACE("Using glXSwapIntervalEXT."); - glXSwapIntervalEXT(s_display, s_window, 0); + glXSwapIntervalEXT(g_bgfxX11Display, g_bgfxX11Window, 0); } else { @@ -210,7 +203,7 @@ namespace bgfx glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - glXSwapBuffers(s_display, s_window); + glXSwapBuffers(s_display, g_bgfxX11Window); } void GlContext::destroy() @@ -226,7 +219,7 @@ namespace bgfx if (NULL != glXSwapIntervalEXT) { - glXSwapIntervalEXT(s_display, s_window, interval); + glXSwapIntervalEXT(g_bgfxX11Display, g_bgfxX11Window, interval); } else if (NULL != glXSwapIntervalMESA) { @@ -257,8 +250,8 @@ namespace bgfx { if (NULL == _swapChain) { - glXMakeCurrent(s_display, s_window, m_context); - glXSwapBuffers(s_display, s_window); + glXMakeCurrent(g_bgfxX11Display, g_bgfxX11Window, m_context); + glXSwapBuffers(g_bgfxX11Display, g_bgfxX11Window); } else { @@ -271,7 +264,7 @@ namespace bgfx { if (NULL == _swapChain) { - glXMakeCurrent(s_display, s_window, m_context); + glXMakeCurrent(g_bgfxX11Display, g_bgfxX11Window, m_context); } else { @@ -295,4 +288,6 @@ namespace bgfx } // namespace bgfx +# endif // BGFX_USE_GLX + #endif // (BX_PLATFORM_FREEBSD || BX_PLATFORM_LINUX) && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL) diff --git a/src/glcontext_glx.h b/src/glcontext_glx.h index d91396d7..556f10b2 100644 --- a/src/glcontext_glx.h +++ b/src/glcontext_glx.h @@ -6,7 +6,7 @@ #ifndef BGFX_GLCONTEXT_GLX_H_HEADER_GUARD #define BGFX_GLCONTEXT_GLX_H_HEADER_GUARD -#if BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD +#if BGFX_USE_GLX # include # include @@ -45,6 +45,6 @@ namespace bgfx }; } // namespace bgfx -#endif // BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD +#endif // BGFX_USE_GLX #endif // BGFX_GLCONTEXT_GLX_H_HEADER_GUARD diff --git a/src/renderer_gl.h b/src/renderer_gl.h index 26059d3c..1218392b 100644 --- a/src/renderer_gl.h +++ b/src/renderer_gl.h @@ -6,9 +6,26 @@ #ifndef BGFX_RENDERER_GL_H_HEADER_GUARD #define BGFX_RENDERER_GL_H_HEADER_GUARD -#define BGFX_USE_EGL (BGFX_CONFIG_RENDERER_OPENGLES && (BX_PLATFORM_ANDROID || BX_PLATFORM_EMSCRIPTEN || BX_PLATFORM_QNX || BX_PLATFORM_RPI || BX_PLATFORM_WINDOWS) ) +#define BGFX_USE_EGL (BGFX_CONFIG_RENDERER_OPENGLES && (0 \ + || BX_PLATFORM_ANDROID \ + || BX_PLATFORM_EMSCRIPTEN \ + || BX_PLATFORM_LINUX \ + || BX_PLATFORM_QNX \ + || BX_PLATFORM_RPI \ + || BX_PLATFORM_WINDOWS \ + ) ) + #define BGFX_USE_WGL (BGFX_CONFIG_RENDERER_OPENGL && BX_PLATFORM_WINDOWS) -#define BGFX_USE_GL_DYNAMIC_LIB (BX_PLATFORM_LINUX || BX_PLATFORM_OSX || BX_PLATFORM_WINDOWS) +#define BGFX_USE_GLX (BGFX_CONFIG_RENDERER_OPENGL && (0 \ + || BX_PLATFORM_LINUX \ + || BX_PLATFORM_FREEBSD \ + ) ) + +#define BGFX_USE_GL_DYNAMIC_LIB (0 \ + || BX_PLATFORM_LINUX \ + || BX_PLATFORM_OSX \ + || BX_PLATFORM_WINDOWS \ + ) #if BGFX_CONFIG_RENDERER_OPENGL # if BGFX_CONFIG_RENDERER_OPENGL >= 31