glcontext_glx.cpp 8.3 KB

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