glcontext_glx.cpp 8.4 KB

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