glcontext_glx.cpp 8.3 KB

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