glcontext_glx.cpp 8.1 KB

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