bgfx/src/glcontext_glx.cpp

294 lines
7.6 KiB
C++
Raw Normal View History

2013-01-13 21:39:25 -05:00
/*
2015-01-01 18:04:46 -05:00
* Copyright 2011-2015 Branimir Karadzic. All rights reserved.
2013-01-13 21:39:25 -05:00
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include "bgfx_p.h"
2014-08-24 20:41:41 -04:00
#if (BX_PLATFORM_FREEBSD || BX_PLATFORM_LINUX) && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)
2013-01-13 21:39:25 -05:00
# include "renderer_gl.h"
# if BGFX_USE_GLX
# define GLX_GLXEXT_PROTOTYPES
# include <glx/glxext.h>
2013-01-13 21:39:25 -05:00
namespace bgfx
{
2013-04-28 01:39:15 -04:00
typedef int (*PFNGLXSWAPINTERVALMESAPROC)(uint32_t _interval);
2013-04-27 18:16:05 -04:00
PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB;
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT;
PFNGLXSWAPINTERVALMESAPROC glXSwapIntervalMESA;
PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI;
2013-04-27 18:16:05 -04:00
2013-12-08 01:01:32 -05:00
# define GL_IMPORT(_optional, _proto, _func, _import) _proto _func
# include "glimports.h"
2013-01-13 21:39:25 -05:00
2014-09-28 17:38:52 -04:00
struct SwapChainGL
{
SwapChainGL(::Window _window, XVisualInfo* _visualInfo, GLXContext _context)
: m_window(_window)
{
2015-02-06 19:49:04 -05:00
m_context = glXCreateContext( (::Display*)g_bgfxX11Display, _visualInfo, _context, GL_TRUE);
2014-09-28 17:38:52 -04:00
}
~SwapChainGL()
{
2015-02-06 19:49:04 -05:00
glXMakeCurrent( (::Display*)g_bgfxX11Display, 0, 0);
glXDestroyContext( (::Display*)g_bgfxX11Display, m_context);
2014-09-28 17:38:52 -04:00
}
void makeCurrent()
{
2015-02-06 19:49:04 -05:00
glXMakeCurrent( (::Display*)g_bgfxX11Display, m_window, m_context);
2014-09-28 17:38:52 -04:00
}
void swapBuffers()
{
2015-02-06 19:49:04 -05:00
glXSwapBuffers( (::Display*)g_bgfxX11Display, m_window);
2014-09-28 17:38:52 -04:00
}
Window m_window;
GLXContext m_context;
};
2013-01-13 21:39:25 -05:00
void GlContext::create(uint32_t _width, uint32_t _height)
{
2013-12-07 13:26:17 -05:00
BX_UNUSED(_width, _height);
2015-02-06 19:49:04 -05:00
XLockDisplay( (::Display*)g_bgfxX11Display);
2013-01-13 21:39:25 -05:00
int major, minor;
2015-02-06 19:49:04 -05:00
bool version = glXQueryVersion( (::Display*)g_bgfxX11Display, &major, &minor);
2013-01-13 21:39:25 -05:00
BGFX_FATAL(version, Fatal::UnableToInitialize, "Failed to query GLX version");
BGFX_FATAL( (major == 1 && minor >= 2) || major > 1
2013-01-13 21:39:25 -05:00
, Fatal::UnableToInitialize
, "GLX version is not >=1.2 (%d.%d)."
2013-01-13 21:39:25 -05:00
, major
, minor
);
2015-02-06 19:49:04 -05:00
int32_t screen = DefaultScreen( (::Display*)g_bgfxX11Display);
2013-04-27 02:43:31 -04:00
2015-02-06 19:49:04 -05:00
const char* extensions = glXQueryExtensionsString( (::Display*)g_bgfxX11Display, screen);
2013-04-27 02:43:31 -04:00
BX_TRACE("GLX extensions:");
dumpExtensions(extensions);
2013-01-13 21:39:25 -05:00
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,
2013-01-13 21:39:25 -05:00
GLX_DEPTH_SIZE, 24,
GLX_STENCIL_SIZE, 8,
0,
2013-01-13 21:39:25 -05:00
};
// Find suitable config
GLXFBConfig bestConfig = NULL;
int numConfigs;
2015-02-06 19:49:04 -05:00
GLXFBConfig* configs = glXChooseFBConfig( (::Display*)g_bgfxX11Display, screen, attrsGlx, &numConfigs);
2013-01-13 21:39:25 -05:00
BX_TRACE("glX num configs %d", numConfigs);
for (int ii = 0; ii < numConfigs; ++ii)
{
2015-02-06 19:49:04 -05:00
m_visualInfo = glXGetVisualFromFBConfig( (::Display*)g_bgfxX11Display, configs[ii]);
2014-09-28 17:38:52 -04:00
if (NULL != m_visualInfo)
2013-01-13 21:39:25 -05:00
{
BX_TRACE("---");
bool valid = true;
2013-08-07 00:04:28 -04:00
for (uint32_t attr = 6; attr < BX_COUNTOF(attrsGlx)-1 && attrsGlx[attr] != None; attr += 2)
2013-01-13 21:39:25 -05:00
{
int value;
2015-02-06 19:49:04 -05:00
glXGetFBConfigAttrib( (::Display*)g_bgfxX11Display, configs[ii], attrsGlx[attr], &value);
2013-01-13 21:39:25 -05:00
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;
#endif // BGFX_CONFIG_DEBUG
}
}
if (valid)
{
bestConfig = configs[ii];
2013-10-19 18:36:26 -04:00
BX_TRACE("Best config %d.", ii);
2013-01-13 21:39:25 -05:00
break;
}
}
2014-09-28 17:38:52 -04:00
XFree(m_visualInfo);
m_visualInfo = NULL;
2013-01-13 21:39:25 -05:00
}
XFree(configs);
2014-09-28 17:38:52 -04:00
BGFX_FATAL(m_visualInfo, Fatal::UnableToInitialize, "Failed to find a suitable X11 display configuration.");
2013-01-13 21:39:25 -05:00
BX_TRACE("Create GL 2.1 context.");
2015-02-06 19:49:04 -05:00
m_context = glXCreateContext( (::Display*)g_bgfxX11Display, m_visualInfo, 0, GL_TRUE);
2013-01-13 21:39:25 -05:00
BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create GL 2.1 context.");
2013-03-09 18:56:26 -05:00
#if BGFX_CONFIG_RENDERER_OPENGL >= 31
2013-04-27 18:16:05 -04:00
glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress( (const GLubyte*)"glXCreateContextAttribsARB");
2013-05-17 00:31:59 -04:00
2013-01-13 21:39:25 -05:00
if (NULL != glXCreateContextAttribsARB)
{
2013-03-04 23:24:02 -05:00
BX_TRACE("Create GL 3.1 context.");
2013-01-13 21:39:25 -05:00
const int contextAttrs[] =
{
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
2013-03-04 23:24:02 -05:00
GLX_CONTEXT_MINOR_VERSION_ARB, 1,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
0,
2013-01-13 21:39:25 -05:00
};
2015-02-06 19:49:04 -05:00
GLXContext context = glXCreateContextAttribsARB( (::Display*)g_bgfxX11Display, bestConfig, 0, true, contextAttrs);
2013-01-13 21:39:25 -05:00
if (NULL != context)
{
2015-02-06 19:49:04 -05:00
glXDestroyContext( (::Display*)g_bgfxX11Display, m_context);
2013-01-13 21:39:25 -05:00
m_context = context;
}
}
2013-03-09 18:56:26 -05:00
#else
BX_UNUSED(bestConfig);
#endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
2013-01-13 21:39:25 -05:00
2015-02-06 19:49:04 -05:00
XUnlockDisplay( (::Display*)g_bgfxX11Display);
2013-01-13 21:39:25 -05:00
import();
2015-02-06 19:49:04 -05:00
glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context);
2013-05-17 00:31:59 -04:00
2013-04-27 18:16:05 -04:00
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT");
2013-04-27 02:43:31 -04:00
if (NULL != glXSwapIntervalEXT)
{
BX_TRACE("Using glXSwapIntervalEXT.");
2015-02-06 19:49:04 -05:00
glXSwapIntervalEXT( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, 0);
2013-04-27 02:43:31 -04:00
}
else
{
2013-04-28 01:39:15 -04:00
glXSwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalMESA");
if (NULL != glXSwapIntervalMESA)
2013-04-27 02:43:31 -04:00
{
2013-04-28 01:39:15 -04:00
BX_TRACE("Using glXSwapIntervalMESA.");
glXSwapIntervalMESA(0);
}
else
{
glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalSGI");
if (NULL != glXSwapIntervalSGI)
{
BX_TRACE("Using glXSwapIntervalSGI.");
glXSwapIntervalSGI(0);
}
2013-04-27 02:43:31 -04:00
}
}
2013-01-13 21:39:25 -05:00
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
2015-02-06 19:49:04 -05:00
glXSwapBuffers( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window);
2013-01-13 21:39:25 -05:00
}
void GlContext::destroy()
{
2015-02-06 19:49:04 -05:00
glXMakeCurrent( (::Display*)g_bgfxX11Display, 0, 0);
glXDestroyContext( (::Display*)g_bgfxX11Display, m_context);
2014-09-28 17:38:52 -04:00
XFree(m_visualInfo);
2013-01-13 21:39:25 -05:00
}
2013-04-27 18:16:05 -04:00
void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, bool _vsync)
2013-01-13 21:39:25 -05:00
{
2013-04-27 18:16:05 -04:00
int32_t interval = _vsync ? 1 : 0;
if (NULL != glXSwapIntervalEXT)
{
2015-02-06 19:49:04 -05:00
glXSwapIntervalEXT( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, interval);
2013-04-27 18:16:05 -04:00
}
2013-04-28 01:39:15 -04:00
else if (NULL != glXSwapIntervalMESA)
{
glXSwapIntervalMESA(interval);
}
2013-04-27 18:16:05 -04:00
else if (NULL != glXSwapIntervalSGI)
{
glXSwapIntervalSGI(interval);
}
2013-01-13 21:39:25 -05:00
}
2014-09-23 23:35:39 -04:00
bool GlContext::isSwapChainSupported()
{
2014-09-28 17:38:52 -04:00
return true;
2014-09-23 23:35:39 -04:00
}
2014-09-28 17:38:52 -04:00
SwapChainGL* GlContext::createSwapChain(void* _nwh)
2013-01-13 21:39:25 -05:00
{
2014-09-28 17:38:52 -04:00
return BX_NEW(g_allocator, SwapChainGL)( (::Window)_nwh, m_visualInfo, m_context);
2014-09-07 20:17:38 -04:00
}
2014-11-30 12:06:47 -05:00
void GlContext::destroySwapChain(SwapChainGL* _swapChain)
2014-09-07 20:17:38 -04:00
{
2014-09-28 17:38:52 -04:00
BX_DELETE(g_allocator, _swapChain);
2014-09-07 20:17:38 -04:00
}
void GlContext::swap(SwapChainGL* _swapChain)
{
2014-09-28 17:38:52 -04:00
if (NULL == _swapChain)
{
2015-02-06 19:49:04 -05:00
glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context);
glXSwapBuffers( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window);
2014-09-28 17:38:52 -04:00
}
else
{
_swapChain->makeCurrent();
_swapChain->swapBuffers();
}
2013-01-13 21:39:25 -05:00
}
2014-09-28 17:38:52 -04:00
void GlContext::makeCurrent(SwapChainGL* _swapChain)
2014-09-07 20:17:38 -04:00
{
2014-09-28 17:38:52 -04:00
if (NULL == _swapChain)
{
2015-02-06 19:49:04 -05:00
glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context);
2014-09-28 17:38:52 -04:00
}
else
{
_swapChain->makeCurrent();
}
2014-09-07 20:17:38 -04:00
}
2013-01-13 21:39:25 -05:00
void GlContext::import()
{
# define GL_EXTENSION(_optional, _proto, _func, _import) \
{ \
if (NULL == _func) \
{ \
2014-02-19 23:53:06 -05:00
_func = (_proto)glXGetProcAddress( (const GLubyte*)#_import); \
BX_TRACE("%p " #_func " (" #_import ")", _func); \
BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. glXGetProcAddress %s", #_import); \
} \
}
2013-01-13 21:39:25 -05:00
# include "glimports.h"
}
} // namespace bgfx
# endif // BGFX_USE_GLX
2014-08-24 20:41:41 -04:00
#endif // (BX_PLATFORM_FREEBSD || BX_PLATFORM_LINUX) && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)