2
0

glcontext_glx.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright 2011-2020 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(::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. void GlContext::create(uint32_t _width, uint32_t _height)
  72. {
  73. BX_UNUSED(_width, _height);
  74. m_context = (GLXContext)g_platformData.context;
  75. m_display = (::Display*)g_platformData.ndt;
  76. // It's possible the user has provided the window handle, but not
  77. // the display handle. If this is the case, try opening the default
  78. // display
  79. if (NULL == m_display)
  80. {
  81. m_display = XOpenDisplay(NULL);
  82. BGFX_FATAL(m_display, Fatal::UnableToInitialize, "XOpenDisplay(NULL) : Failed to open default display");
  83. }
  84. if (NULL == g_platformData.context)
  85. {
  86. XLockDisplay(m_display);
  87. int major, minor;
  88. bool version = glXQueryVersion(m_display, &major, &minor);
  89. BGFX_FATAL(version, Fatal::UnableToInitialize, "Failed to query GLX version");
  90. BGFX_FATAL( (major == 1 && minor >= 2) || major > 1
  91. , Fatal::UnableToInitialize
  92. , "GLX version is not >=1.2 (%d.%d)."
  93. , major
  94. , minor
  95. );
  96. int32_t screen = DefaultScreen(m_display);
  97. const char* extensions = glXQueryExtensionsString(m_display, screen);
  98. BX_TRACE("GLX extensions:");
  99. dumpExtensions(extensions);
  100. const int attrsGlx[] =
  101. {
  102. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  103. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  104. GLX_DOUBLEBUFFER, true,
  105. GLX_RED_SIZE, 8,
  106. GLX_BLUE_SIZE, 8,
  107. GLX_GREEN_SIZE, 8,
  108. // GLX_ALPHA_SIZE, 8,
  109. GLX_DEPTH_SIZE, 24,
  110. GLX_STENCIL_SIZE, 8,
  111. 0,
  112. };
  113. // Find suitable config
  114. GLXFBConfig bestConfig = NULL;
  115. int numConfigs;
  116. GLXFBConfig* configs = glXChooseFBConfig(m_display, screen, attrsGlx, &numConfigs);
  117. BX_TRACE("glX num configs %d", numConfigs);
  118. for (int ii = 0; ii < numConfigs; ++ii)
  119. {
  120. m_visualInfo = glXGetVisualFromFBConfig(m_display, configs[ii]);
  121. if (NULL != m_visualInfo)
  122. {
  123. BX_TRACE("---");
  124. bool valid = true;
  125. for (uint32_t attr = 6; attr < BX_COUNTOF(attrsGlx)-1 && attrsGlx[attr] != 0; attr += 2)
  126. {
  127. int value;
  128. glXGetFBConfigAttrib(m_display, configs[ii], attrsGlx[attr], &value);
  129. BX_TRACE("glX %d/%d %2d: %4x, %8x (%8x%s)"
  130. , ii
  131. , numConfigs
  132. , attr/2
  133. , attrsGlx[attr]
  134. , value
  135. , attrsGlx[attr + 1]
  136. , value < attrsGlx[attr + 1] ? " *" : ""
  137. );
  138. if (value < attrsGlx[attr + 1])
  139. {
  140. valid = false;
  141. #if !BGFX_CONFIG_DEBUG
  142. break;
  143. #endif // BGFX_CONFIG_DEBUG
  144. }
  145. }
  146. if (valid)
  147. {
  148. bestConfig = configs[ii];
  149. BX_TRACE("Best config %d.", ii);
  150. break;
  151. }
  152. }
  153. XFree(m_visualInfo);
  154. m_visualInfo = NULL;
  155. }
  156. XFree(configs);
  157. BGFX_FATAL(m_visualInfo, Fatal::UnableToInitialize, "Failed to find a suitable X11 display configuration.");
  158. BX_TRACE("Create GL 2.1 context.");
  159. m_context = glXCreateContext(m_display, m_visualInfo, 0, GL_TRUE);
  160. BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create GL 2.1 context.");
  161. glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress( (const GLubyte*)"glXCreateContextAttribsARB");
  162. if (NULL != glXCreateContextAttribsARB)
  163. {
  164. BX_TRACE("Create GL %d.%d context.", BGFX_CONFIG_RENDERER_OPENGL / 10, BGFX_CONFIG_RENDERER_OPENGL % 10);
  165. int32_t flags = BGFX_CONFIG_DEBUG ? GLX_CONTEXT_DEBUG_BIT_ARB : 0;
  166. const int contextAttrs[] =
  167. {
  168. GLX_CONTEXT_MAJOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL / 10,
  169. GLX_CONTEXT_MINOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL % 10,
  170. GLX_CONTEXT_FLAGS_ARB, flags,
  171. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  172. 0,
  173. };
  174. GLXContext context = glXCreateContextAttribsARB(m_display, bestConfig, 0, true, contextAttrs);
  175. if (NULL != context)
  176. {
  177. glXDestroyContext(m_display, m_context);
  178. m_context = context;
  179. }
  180. }
  181. XUnlockDisplay(m_display);
  182. }
  183. import();
  184. glXMakeCurrent(m_display, (::Window)g_platformData.nwh, m_context);
  185. m_current = NULL;
  186. const char* extensions = glXQueryExtensionsString(m_display, DefaultScreen(m_display));
  187. if (NULL != extensions)
  188. {
  189. bool foundSwapControl = false;
  190. if (haveGlxExtension("GLX_EXT_swap_control", extensions)) {
  191. glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT");
  192. if (NULL != glXSwapIntervalEXT)
  193. {
  194. BX_TRACE("Using glXSwapIntervalEXT.");
  195. glXSwapIntervalEXT(m_display, (::Window)g_platformData.nwh, 0);
  196. foundSwapControl = true;
  197. }
  198. }
  199. if (!foundSwapControl && haveGlxExtension("GLX_MESA_swap_control", extensions)) {
  200. glXSwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalMESA");
  201. if (NULL != glXSwapIntervalMESA)
  202. {
  203. BX_TRACE("Using glXSwapIntervalMESA.");
  204. glXSwapIntervalMESA(0);
  205. foundSwapControl = true;
  206. }
  207. }
  208. if (!foundSwapControl && haveGlxExtension("GLX_SGI_swap_control", extensions)) {
  209. glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalSGI");
  210. if (NULL != glXSwapIntervalSGI)
  211. {
  212. BX_TRACE("Using glXSwapIntervalSGI.");
  213. glXSwapIntervalSGI(0);
  214. foundSwapControl = true;
  215. }
  216. }
  217. }
  218. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  219. glClear(GL_COLOR_BUFFER_BIT);
  220. glXSwapBuffers(m_display, (::Window)g_platformData.nwh);
  221. g_internalData.context = m_context;
  222. }
  223. void GlContext::destroy()
  224. {
  225. glXMakeCurrent(m_display, 0, 0);
  226. if (NULL == g_platformData.context)
  227. {
  228. glXDestroyContext(m_display, m_context);
  229. XFree(m_visualInfo);
  230. }
  231. // If we opened the display, have to close it
  232. if (NULL == g_platformData.ndt)
  233. {
  234. XCloseDisplay(m_display);
  235. }
  236. m_context = NULL;
  237. m_visualInfo = NULL;
  238. m_display = NULL;
  239. }
  240. void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, uint32_t _flags)
  241. {
  242. bool vsync = !!(_flags&BGFX_RESET_VSYNC);
  243. int32_t interval = vsync ? 1 : 0;
  244. if (NULL != glXSwapIntervalEXT)
  245. {
  246. glXSwapIntervalEXT(m_display, (::Window)g_platformData.nwh, interval);
  247. }
  248. else if (NULL != glXSwapIntervalMESA)
  249. {
  250. glXSwapIntervalMESA(interval);
  251. }
  252. else if (NULL != glXSwapIntervalSGI)
  253. {
  254. glXSwapIntervalSGI(interval);
  255. }
  256. }
  257. uint64_t GlContext::getCaps() const
  258. {
  259. return BGFX_CAPS_SWAP_CHAIN;
  260. }
  261. SwapChainGL* GlContext::createSwapChain(void* _nwh)
  262. {
  263. return BX_NEW(g_allocator, SwapChainGL)(m_display, (::Window)_nwh, m_visualInfo, m_context);
  264. }
  265. void GlContext::destroySwapChain(SwapChainGL* _swapChain)
  266. {
  267. BX_DELETE(g_allocator, _swapChain);
  268. glXMakeCurrent(m_display, (::Window)g_platformData.nwh, m_context);
  269. }
  270. void GlContext::swap(SwapChainGL* _swapChain)
  271. {
  272. makeCurrent(_swapChain);
  273. if (NULL == _swapChain)
  274. {
  275. glXSwapBuffers(m_display, (::Window)g_platformData.nwh);
  276. }
  277. else
  278. {
  279. _swapChain->swapBuffers();
  280. }
  281. }
  282. void GlContext::makeCurrent(SwapChainGL* _swapChain)
  283. {
  284. if (m_current != _swapChain)
  285. {
  286. m_current = _swapChain;
  287. if (NULL == _swapChain)
  288. {
  289. glXMakeCurrent(m_display, (::Window)g_platformData.nwh, m_context);
  290. }
  291. else
  292. {
  293. _swapChain->makeCurrent();
  294. }
  295. }
  296. }
  297. void GlContext::import()
  298. {
  299. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  300. { \
  301. if (NULL == _func) \
  302. { \
  303. _func = (_proto)glXGetProcAddress( (const GLubyte*)#_import); \
  304. BX_TRACE("%p " #_func " (" #_import ")", _func); \
  305. BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. glXGetProcAddress %s", #_import); \
  306. } \
  307. }
  308. # include "glimports.h"
  309. }
  310. } /* namespace gl */ } // namespace bgfx
  311. # endif // BGFX_USE_GLX
  312. #endif // (BX_PLATFORM_BSD || BX_PLATFORM_LINUX) && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)