glcontext_glx.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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(::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. #if BGFX_CONFIG_RENDERER_OPENGL >= 31
  138. glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress( (const GLubyte*)"glXCreateContextAttribsARB");
  139. if (NULL != glXCreateContextAttribsARB)
  140. {
  141. BX_TRACE("Create GL %d.%d context.", BGFX_CONFIG_RENDERER_OPENGL / 10, BGFX_CONFIG_RENDERER_OPENGL % 10);
  142. int32_t flags = BGFX_CONFIG_DEBUG ? GLX_CONTEXT_DEBUG_BIT_ARB : 0;
  143. const int contextAttrs[] =
  144. {
  145. GLX_CONTEXT_MAJOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL / 10,
  146. GLX_CONTEXT_MINOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL % 10,
  147. GLX_CONTEXT_FLAGS_ARB, flags,
  148. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  149. 0,
  150. };
  151. GLXContext context = glXCreateContextAttribsARB(m_display, bestConfig, 0, true, contextAttrs);
  152. if (NULL != context)
  153. {
  154. glXDestroyContext(m_display, m_context);
  155. m_context = context;
  156. }
  157. }
  158. #else
  159. BX_UNUSED(bestConfig);
  160. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
  161. XUnlockDisplay(m_display);
  162. }
  163. import();
  164. glXMakeCurrent(m_display, (::Window)g_platformData.nwh, m_context);
  165. m_current = NULL;
  166. glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT");
  167. if (NULL != glXSwapIntervalEXT)
  168. {
  169. BX_TRACE("Using glXSwapIntervalEXT.");
  170. glXSwapIntervalEXT(m_display, (::Window)g_platformData.nwh, 0);
  171. }
  172. else
  173. {
  174. glXSwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalMESA");
  175. if (NULL != glXSwapIntervalMESA)
  176. {
  177. BX_TRACE("Using glXSwapIntervalMESA.");
  178. glXSwapIntervalMESA(0);
  179. }
  180. else
  181. {
  182. glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalSGI");
  183. if (NULL != glXSwapIntervalSGI)
  184. {
  185. BX_TRACE("Using glXSwapIntervalSGI.");
  186. glXSwapIntervalSGI(0);
  187. }
  188. }
  189. }
  190. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  191. glClear(GL_COLOR_BUFFER_BIT);
  192. glXSwapBuffers(m_display, (::Window)g_platformData.nwh);
  193. g_internalData.context = m_context;
  194. }
  195. void GlContext::destroy()
  196. {
  197. glXMakeCurrent(m_display, 0, 0);
  198. if (NULL == g_platformData.context)
  199. {
  200. glXDestroyContext(m_display, m_context);
  201. XFree(m_visualInfo);
  202. }
  203. // If we opened the display, have to close it
  204. if (NULL == g_platformData.ndt)
  205. {
  206. XCloseDisplay(m_display);
  207. }
  208. m_context = NULL;
  209. m_visualInfo = NULL;
  210. m_display = NULL;
  211. }
  212. void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, uint32_t _flags)
  213. {
  214. bool vsync = !!(_flags&BGFX_RESET_VSYNC);
  215. int32_t interval = vsync ? 1 : 0;
  216. if (NULL != glXSwapIntervalEXT)
  217. {
  218. glXSwapIntervalEXT(m_display, (::Window)g_platformData.nwh, interval);
  219. }
  220. else if (NULL != glXSwapIntervalMESA)
  221. {
  222. glXSwapIntervalMESA(interval);
  223. }
  224. else if (NULL != glXSwapIntervalSGI)
  225. {
  226. glXSwapIntervalSGI(interval);
  227. }
  228. }
  229. uint64_t GlContext::getCaps() const
  230. {
  231. return BGFX_CAPS_SWAP_CHAIN;
  232. }
  233. SwapChainGL* GlContext::createSwapChain(void* _nwh)
  234. {
  235. return BX_NEW(g_allocator, SwapChainGL)(m_display, (::Window)_nwh, m_visualInfo, m_context);
  236. }
  237. void GlContext::destroySwapChain(SwapChainGL* _swapChain)
  238. {
  239. BX_DELETE(g_allocator, _swapChain);
  240. glXMakeCurrent(m_display, (::Window)g_platformData.nwh, m_context);
  241. }
  242. void GlContext::swap(SwapChainGL* _swapChain)
  243. {
  244. makeCurrent(_swapChain);
  245. if (NULL == _swapChain)
  246. {
  247. glXSwapBuffers(m_display, (::Window)g_platformData.nwh);
  248. }
  249. else
  250. {
  251. _swapChain->swapBuffers();
  252. }
  253. }
  254. void GlContext::makeCurrent(SwapChainGL* _swapChain)
  255. {
  256. if (m_current != _swapChain)
  257. {
  258. m_current = _swapChain;
  259. if (NULL == _swapChain)
  260. {
  261. glXMakeCurrent(m_display, (::Window)g_platformData.nwh, m_context);
  262. }
  263. else
  264. {
  265. _swapChain->makeCurrent();
  266. }
  267. }
  268. }
  269. void GlContext::import()
  270. {
  271. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  272. { \
  273. if (NULL == _func) \
  274. { \
  275. _func = (_proto)glXGetProcAddress( (const GLubyte*)#_import); \
  276. BX_TRACE("%p " #_func " (" #_import ")", _func); \
  277. BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. glXGetProcAddress %s", #_import); \
  278. } \
  279. }
  280. # include "glimports.h"
  281. }
  282. } /* namespace gl */ } // namespace bgfx
  283. # endif // BGFX_USE_GLX
  284. #endif // (BX_PLATFORM_BSD || BX_PLATFORM_LINUX) && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)