glcontext_glx.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright 2011-2022 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  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(::Display* display, ::Window _window, XVisualInfo* _visualInfo, GLXContext _context)
  25. : m_display(display)
  26. , m_window(_window)
  27. {
  28. m_context = glXCreateContext(m_display, _visualInfo, _context, GL_TRUE);
  29. }
  30. ~SwapChainGL()
  31. {
  32. glXMakeCurrent(m_display, 0, 0);
  33. glXDestroyContext(m_display, m_context);
  34. }
  35. void makeCurrent()
  36. {
  37. glXMakeCurrent(m_display, m_window, m_context);
  38. }
  39. void swapBuffers()
  40. {
  41. glXSwapBuffers(m_display, m_window);
  42. }
  43. ::Display* m_display;
  44. Window m_window;
  45. GLXContext m_context;
  46. };
  47. static bool haveGlxExtension(const char* _ext, const char* _extList)
  48. {
  49. // _extList is assumed to be a space-separated, null-terminated list of
  50. // extension names, and no extension name ever contains a space.
  51. const char* end = _extList + bx::strLen(_extList);
  52. const char* searchStart = _extList;
  53. for(;;)
  54. {
  55. bx::StringView found = bx::strFind(searchStart, _ext);
  56. if (found.isEmpty() )
  57. {
  58. return false;
  59. }
  60. // We found the substring, but need an exact match, with a word
  61. // boundary at both the front and back of the found spot.
  62. if ((found.getPtr() == _extList || *(found.getPtr() - 1) == ' ')
  63. && (found.getTerm() == end || *found.getTerm() == ' ') )
  64. {
  65. return true;
  66. }
  67. // else, keep searching
  68. searchStart = found.getTerm();
  69. }
  70. }
  71. template<typename ProtoT>
  72. static ProtoT glXGetProcAddress(const char* _name)
  73. {
  74. return reinterpret_cast<ProtoT>( (void*)::glXGetProcAddress( (const GLubyte*)_name) );
  75. }
  76. void GlContext::create(uint32_t _width, uint32_t _height, uint32_t /*_flags*/)
  77. {
  78. BX_UNUSED(_width, _height);
  79. m_context = (GLXContext)g_platformData.context;
  80. m_display = (::Display*)g_platformData.ndt;
  81. // It's possible the user has provided the window handle, but not
  82. // the display handle. If this is the case, try opening the default
  83. // display
  84. if (NULL == m_display)
  85. {
  86. m_display = XOpenDisplay(NULL);
  87. BGFX_FATAL(m_display, Fatal::UnableToInitialize, "XOpenDisplay(NULL) : Failed to open default display");
  88. }
  89. if (NULL == g_platformData.context)
  90. {
  91. XLockDisplay(m_display);
  92. int major, minor;
  93. bool version = glXQueryVersion(m_display, &major, &minor);
  94. BGFX_FATAL(version, Fatal::UnableToInitialize, "Failed to query GLX version");
  95. BGFX_FATAL( (major == 1 && minor >= 2) || major > 1
  96. , Fatal::UnableToInitialize
  97. , "GLX version is not >=1.2 (%d.%d)."
  98. , major
  99. , minor
  100. );
  101. int32_t screen = DefaultScreen(m_display);
  102. const char* extensions = glXQueryExtensionsString(m_display, screen);
  103. BX_TRACE("GLX extensions:");
  104. dumpExtensions(extensions);
  105. const int attrsGlx[] =
  106. {
  107. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  108. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  109. GLX_DOUBLEBUFFER, true,
  110. GLX_RED_SIZE, 8,
  111. GLX_BLUE_SIZE, 8,
  112. GLX_GREEN_SIZE, 8,
  113. // GLX_ALPHA_SIZE, 8,
  114. GLX_DEPTH_SIZE, 24,
  115. GLX_STENCIL_SIZE, 8,
  116. 0,
  117. };
  118. // Find suitable config
  119. GLXFBConfig bestConfig = NULL;
  120. int numConfigs;
  121. GLXFBConfig* configs = glXChooseFBConfig(m_display, screen, attrsGlx, &numConfigs);
  122. BX_TRACE("glX num configs %d", numConfigs);
  123. for (int ii = 0; ii < numConfigs; ++ii)
  124. {
  125. m_visualInfo = glXGetVisualFromFBConfig(m_display, configs[ii]);
  126. if (NULL != m_visualInfo)
  127. {
  128. BX_TRACE("---");
  129. bool valid = true;
  130. for (uint32_t attr = 6; attr < BX_COUNTOF(attrsGlx)-1 && attrsGlx[attr] != 0; attr += 2)
  131. {
  132. int value;
  133. glXGetFBConfigAttrib(m_display, configs[ii], attrsGlx[attr], &value);
  134. BX_TRACE("glX %d/%d %2d: %4x, %8x (%8x%s)"
  135. , ii
  136. , numConfigs
  137. , attr/2
  138. , attrsGlx[attr]
  139. , value
  140. , attrsGlx[attr + 1]
  141. , value < attrsGlx[attr + 1] ? " *" : ""
  142. );
  143. if (value < attrsGlx[attr + 1])
  144. {
  145. valid = false;
  146. #if !BGFX_CONFIG_DEBUG
  147. break;
  148. #endif // BGFX_CONFIG_DEBUG
  149. }
  150. }
  151. if (valid)
  152. {
  153. bestConfig = configs[ii];
  154. BX_TRACE("Best config %d.", ii);
  155. break;
  156. }
  157. }
  158. XFree(m_visualInfo);
  159. m_visualInfo = NULL;
  160. }
  161. XFree(configs);
  162. BGFX_FATAL(m_visualInfo, Fatal::UnableToInitialize, "Failed to find a suitable X11 display configuration.");
  163. BX_TRACE("Create GL 2.1 context.");
  164. m_context = glXCreateContext(m_display, m_visualInfo, 0, GL_TRUE);
  165. BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create GL 2.1 context.");
  166. glXCreateContextAttribsARB = glXGetProcAddress<PFNGLXCREATECONTEXTATTRIBSARBPROC>("glXCreateContextAttribsARB");
  167. if (NULL != glXCreateContextAttribsARB)
  168. {
  169. BX_TRACE("Create GL %d.%d context.", BGFX_CONFIG_RENDERER_OPENGL / 10, BGFX_CONFIG_RENDERER_OPENGL % 10);
  170. int32_t flags = BGFX_CONFIG_DEBUG ? GLX_CONTEXT_DEBUG_BIT_ARB : 0;
  171. const int contextAttrs[] =
  172. {
  173. GLX_CONTEXT_MAJOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL / 10,
  174. GLX_CONTEXT_MINOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL % 10,
  175. GLX_CONTEXT_FLAGS_ARB, flags,
  176. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  177. 0,
  178. };
  179. GLXContext context = glXCreateContextAttribsARB(m_display, bestConfig, 0, true, contextAttrs);
  180. if (NULL != context)
  181. {
  182. glXDestroyContext(m_display, m_context);
  183. m_context = context;
  184. }
  185. }
  186. XUnlockDisplay(m_display);
  187. }
  188. import();
  189. glXMakeCurrent(m_display, (::Window)g_platformData.nwh, m_context);
  190. m_current = NULL;
  191. const char* extensions = glXQueryExtensionsString(m_display, DefaultScreen(m_display) );
  192. if (NULL != extensions)
  193. {
  194. bool foundSwapControl = false;
  195. if (haveGlxExtension("GLX_EXT_swap_control", extensions) )
  196. {
  197. glXSwapIntervalEXT = glXGetProcAddress<PFNGLXSWAPINTERVALEXTPROC>("glXSwapIntervalEXT");
  198. if (NULL != glXSwapIntervalEXT)
  199. {
  200. BX_TRACE("Using glXSwapIntervalEXT.");
  201. glXSwapIntervalEXT(m_display, (::Window)g_platformData.nwh, 0);
  202. foundSwapControl = true;
  203. }
  204. }
  205. if (!foundSwapControl
  206. && haveGlxExtension("GLX_MESA_swap_control", extensions) )
  207. {
  208. glXSwapIntervalMESA = glXGetProcAddress<PFNGLXSWAPINTERVALMESAPROC>("glXSwapIntervalMESA");
  209. if (NULL != glXSwapIntervalMESA)
  210. {
  211. BX_TRACE("Using glXSwapIntervalMESA.");
  212. glXSwapIntervalMESA(0);
  213. foundSwapControl = true;
  214. }
  215. }
  216. if (!foundSwapControl
  217. && haveGlxExtension("GLX_SGI_swap_control", extensions) )
  218. {
  219. glXSwapIntervalSGI = glXGetProcAddress<PFNGLXSWAPINTERVALSGIPROC>("glXSwapIntervalSGI");
  220. if (NULL != glXSwapIntervalSGI)
  221. {
  222. BX_TRACE("Using glXSwapIntervalSGI.");
  223. glXSwapIntervalSGI(0);
  224. foundSwapControl = true;
  225. }
  226. }
  227. }
  228. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  229. glClear(GL_COLOR_BUFFER_BIT);
  230. glXSwapBuffers(m_display, (::Window)g_platformData.nwh);
  231. g_internalData.context = m_context;
  232. }
  233. void GlContext::destroy()
  234. {
  235. glXMakeCurrent(m_display, 0, 0);
  236. if (NULL == g_platformData.context)
  237. {
  238. glXDestroyContext(m_display, m_context);
  239. XFree(m_visualInfo);
  240. }
  241. // If we opened the display, have to close it
  242. if (NULL == g_platformData.ndt)
  243. {
  244. XCloseDisplay(m_display);
  245. }
  246. m_context = NULL;
  247. m_visualInfo = NULL;
  248. m_display = NULL;
  249. }
  250. void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, uint32_t _flags)
  251. {
  252. bool vsync = !!(_flags&BGFX_RESET_VSYNC);
  253. int32_t interval = vsync ? 1 : 0;
  254. if (NULL != glXSwapIntervalEXT)
  255. {
  256. glXSwapIntervalEXT(m_display, (::Window)g_platformData.nwh, interval);
  257. }
  258. else if (NULL != glXSwapIntervalMESA)
  259. {
  260. glXSwapIntervalMESA(interval);
  261. }
  262. else if (NULL != glXSwapIntervalSGI)
  263. {
  264. glXSwapIntervalSGI(interval);
  265. }
  266. }
  267. uint64_t GlContext::getCaps() const
  268. {
  269. return BGFX_CAPS_SWAP_CHAIN;
  270. }
  271. SwapChainGL* GlContext::createSwapChain(void* _nwh)
  272. {
  273. return BX_NEW(g_allocator, SwapChainGL)(m_display, (::Window)_nwh, m_visualInfo, m_context);
  274. }
  275. void GlContext::destroySwapChain(SwapChainGL* _swapChain)
  276. {
  277. BX_DELETE(g_allocator, _swapChain);
  278. glXMakeCurrent(m_display, (::Window)g_platformData.nwh, m_context);
  279. }
  280. void GlContext::swap(SwapChainGL* _swapChain)
  281. {
  282. makeCurrent(_swapChain);
  283. if (NULL == _swapChain)
  284. {
  285. glXSwapBuffers(m_display, (::Window)g_platformData.nwh);
  286. }
  287. else
  288. {
  289. _swapChain->swapBuffers();
  290. }
  291. }
  292. void GlContext::makeCurrent(SwapChainGL* _swapChain)
  293. {
  294. if (m_current != _swapChain)
  295. {
  296. m_current = _swapChain;
  297. if (NULL == _swapChain)
  298. {
  299. glXMakeCurrent(m_display, (::Window)g_platformData.nwh, m_context);
  300. }
  301. else
  302. {
  303. _swapChain->makeCurrent();
  304. }
  305. }
  306. }
  307. void GlContext::import()
  308. {
  309. BX_TRACE("Import:");
  310. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  311. { \
  312. if (NULL == _func) \
  313. { \
  314. _func = glXGetProcAddress<_proto>(#_import); \
  315. BX_TRACE("%p " #_func " (" #_import ")", _func); \
  316. BGFX_FATAL(_optional || NULL != _func \
  317. , Fatal::UnableToInitialize \
  318. , "Failed to create OpenGL context. glXGetProcAddress %s", #_import); \
  319. } \
  320. }
  321. # include "glimports.h"
  322. # undef GL_EXTENSION
  323. }
  324. } /* namespace gl */ } // namespace bgfx
  325. # endif // BGFX_USE_GLX
  326. #endif // (BX_PLATFORM_BSD || BX_PLATFORM_LINUX) && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)