glcontext_glx.cpp 8.4 KB

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