glcontext_glx.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "bgfx_p.h"
  6. #if (BX_PLATFORM_FREEBSD || BX_PLATFORM_LINUX) && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)
  7. # include "renderer_gl.h"
  8. # if BGFX_USE_GLX
  9. # define GLX_GLXEXT_PROTOTYPES
  10. # include <glx/glxext.h>
  11. namespace bgfx { namespace gl
  12. {
  13. typedef int (*PFNGLXSWAPINTERVALMESAPROC)(uint32_t _interval);
  14. PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB;
  15. PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT;
  16. PFNGLXSWAPINTERVALMESAPROC glXSwapIntervalMESA;
  17. PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI;
  18. # define GL_IMPORT(_optional, _proto, _func, _import) _proto _func
  19. # include "glimports.h"
  20. struct SwapChainGL
  21. {
  22. SwapChainGL(::Window _window, XVisualInfo* _visualInfo, GLXContext _context)
  23. : m_window(_window)
  24. {
  25. m_context = glXCreateContext( (::Display*)g_bgfxX11Display, _visualInfo, _context, GL_TRUE);
  26. }
  27. ~SwapChainGL()
  28. {
  29. glXMakeCurrent( (::Display*)g_bgfxX11Display, 0, 0);
  30. glXDestroyContext( (::Display*)g_bgfxX11Display, m_context);
  31. }
  32. void makeCurrent()
  33. {
  34. glXMakeCurrent( (::Display*)g_bgfxX11Display, m_window, m_context);
  35. }
  36. void swapBuffers()
  37. {
  38. glXSwapBuffers( (::Display*)g_bgfxX11Display, m_window);
  39. }
  40. Window m_window;
  41. GLXContext m_context;
  42. };
  43. void GlContext::create(uint32_t _width, uint32_t _height)
  44. {
  45. BX_UNUSED(_width, _height);
  46. XLockDisplay( (::Display*)g_bgfxX11Display);
  47. int major, minor;
  48. bool version = glXQueryVersion( (::Display*)g_bgfxX11Display, &major, &minor);
  49. BGFX_FATAL(version, Fatal::UnableToInitialize, "Failed to query GLX version");
  50. BGFX_FATAL( (major == 1 && minor >= 2) || major > 1
  51. , Fatal::UnableToInitialize
  52. , "GLX version is not >=1.2 (%d.%d)."
  53. , major
  54. , minor
  55. );
  56. int32_t screen = DefaultScreen( (::Display*)g_bgfxX11Display);
  57. const char* extensions = glXQueryExtensionsString( (::Display*)g_bgfxX11Display, screen);
  58. BX_TRACE("GLX extensions:");
  59. dumpExtensions(extensions);
  60. const int attrsGlx[] =
  61. {
  62. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  63. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  64. GLX_DOUBLEBUFFER, true,
  65. GLX_RED_SIZE, 8,
  66. GLX_BLUE_SIZE, 8,
  67. GLX_GREEN_SIZE, 8,
  68. // GLX_ALPHA_SIZE, 8,
  69. GLX_DEPTH_SIZE, 24,
  70. GLX_STENCIL_SIZE, 8,
  71. 0,
  72. };
  73. // Find suitable config
  74. GLXFBConfig bestConfig = NULL;
  75. int numConfigs;
  76. GLXFBConfig* configs = glXChooseFBConfig( (::Display*)g_bgfxX11Display, screen, attrsGlx, &numConfigs);
  77. BX_TRACE("glX num configs %d", numConfigs);
  78. for (int ii = 0; ii < numConfigs; ++ii)
  79. {
  80. m_visualInfo = glXGetVisualFromFBConfig( (::Display*)g_bgfxX11Display, configs[ii]);
  81. if (NULL != m_visualInfo)
  82. {
  83. BX_TRACE("---");
  84. bool valid = true;
  85. for (uint32_t attr = 6; attr < BX_COUNTOF(attrsGlx)-1 && attrsGlx[attr] != None; attr += 2)
  86. {
  87. int value;
  88. glXGetFBConfigAttrib( (::Display*)g_bgfxX11Display, configs[ii], attrsGlx[attr], &value);
  89. BX_TRACE("glX %d/%d %2d: %4x, %8x (%8x%s)"
  90. , ii
  91. , numConfigs
  92. , attr/2
  93. , attrsGlx[attr]
  94. , value
  95. , attrsGlx[attr + 1]
  96. , value < attrsGlx[attr + 1] ? " *" : ""
  97. );
  98. if (value < attrsGlx[attr + 1])
  99. {
  100. valid = false;
  101. #if !BGFX_CONFIG_DEBUG
  102. break;
  103. #endif // BGFX_CONFIG_DEBUG
  104. }
  105. }
  106. if (valid)
  107. {
  108. bestConfig = configs[ii];
  109. BX_TRACE("Best config %d.", ii);
  110. break;
  111. }
  112. }
  113. XFree(m_visualInfo);
  114. m_visualInfo = NULL;
  115. }
  116. XFree(configs);
  117. BGFX_FATAL(m_visualInfo, Fatal::UnableToInitialize, "Failed to find a suitable X11 display configuration.");
  118. BX_TRACE("Create GL 2.1 context.");
  119. m_context = glXCreateContext( (::Display*)g_bgfxX11Display, m_visualInfo, 0, GL_TRUE);
  120. BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create GL 2.1 context.");
  121. #if BGFX_CONFIG_RENDERER_OPENGL >= 31
  122. glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress( (const GLubyte*)"glXCreateContextAttribsARB");
  123. if (NULL != glXCreateContextAttribsARB)
  124. {
  125. BX_TRACE("Create GL 3.1 context.");
  126. const int contextAttrs[] =
  127. {
  128. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  129. GLX_CONTEXT_MINOR_VERSION_ARB, 1,
  130. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  131. 0,
  132. };
  133. GLXContext context = glXCreateContextAttribsARB( (::Display*)g_bgfxX11Display, bestConfig, 0, true, contextAttrs);
  134. if (NULL != context)
  135. {
  136. glXDestroyContext( (::Display*)g_bgfxX11Display, m_context);
  137. m_context = context;
  138. }
  139. }
  140. #else
  141. BX_UNUSED(bestConfig);
  142. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
  143. XUnlockDisplay( (::Display*)g_bgfxX11Display);
  144. import();
  145. glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context);
  146. m_current = NULL;
  147. glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT");
  148. if (NULL != glXSwapIntervalEXT)
  149. {
  150. BX_TRACE("Using glXSwapIntervalEXT.");
  151. glXSwapIntervalEXT( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, 0);
  152. }
  153. else
  154. {
  155. glXSwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalMESA");
  156. if (NULL != glXSwapIntervalMESA)
  157. {
  158. BX_TRACE("Using glXSwapIntervalMESA.");
  159. glXSwapIntervalMESA(0);
  160. }
  161. else
  162. {
  163. glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalSGI");
  164. if (NULL != glXSwapIntervalSGI)
  165. {
  166. BX_TRACE("Using glXSwapIntervalSGI.");
  167. glXSwapIntervalSGI(0);
  168. }
  169. }
  170. }
  171. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  172. glClear(GL_COLOR_BUFFER_BIT);
  173. glXSwapBuffers( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window);
  174. }
  175. void GlContext::destroy()
  176. {
  177. glXMakeCurrent( (::Display*)g_bgfxX11Display, 0, 0);
  178. glXDestroyContext( (::Display*)g_bgfxX11Display, m_context);
  179. XFree(m_visualInfo);
  180. }
  181. void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, bool _vsync)
  182. {
  183. int32_t interval = _vsync ? 1 : 0;
  184. if (NULL != glXSwapIntervalEXT)
  185. {
  186. glXSwapIntervalEXT( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, interval);
  187. }
  188. else if (NULL != glXSwapIntervalMESA)
  189. {
  190. glXSwapIntervalMESA(interval);
  191. }
  192. else if (NULL != glXSwapIntervalSGI)
  193. {
  194. glXSwapIntervalSGI(interval);
  195. }
  196. }
  197. bool GlContext::isSwapChainSupported()
  198. {
  199. return true;
  200. }
  201. SwapChainGL* GlContext::createSwapChain(void* _nwh)
  202. {
  203. return BX_NEW(g_allocator, SwapChainGL)( (::Window)_nwh, m_visualInfo, m_context);
  204. }
  205. void GlContext::destroySwapChain(SwapChainGL* _swapChain)
  206. {
  207. BX_DELETE(g_allocator, _swapChain);
  208. }
  209. void GlContext::swap(SwapChainGL* _swapChain)
  210. {
  211. makeCurrent(_swapChain);
  212. if (NULL == _swapChain)
  213. {
  214. glXSwapBuffers( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window);
  215. }
  216. else
  217. {
  218. _swapChain->swapBuffers();
  219. }
  220. }
  221. void GlContext::makeCurrent(SwapChainGL* _swapChain)
  222. {
  223. if (m_current != _swapChain)
  224. {
  225. m_current = _swapChain;
  226. if (NULL == _swapChain)
  227. {
  228. glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context);
  229. }
  230. else
  231. {
  232. _swapChain->makeCurrent();
  233. }
  234. }
  235. }
  236. void GlContext::import()
  237. {
  238. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  239. { \
  240. if (NULL == _func) \
  241. { \
  242. _func = (_proto)glXGetProcAddress( (const GLubyte*)#_import); \
  243. BX_TRACE("%p " #_func " (" #_import ")", _func); \
  244. BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. glXGetProcAddress %s", #_import); \
  245. } \
  246. }
  247. # include "glimports.h"
  248. }
  249. } /* namespace gl */ } // namespace bgfx
  250. # endif // BGFX_USE_GLX
  251. #endif // (BX_PLATFORM_FREEBSD || BX_PLATFORM_LINUX) && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)