glcontext_glx.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "bgfx_p.h"
  6. #if BX_PLATFORM_LINUX & (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)
  7. # include "renderer_gl.h"
  8. # define GLX_GLXEXT_PROTOTYPES
  9. # include <glx/glxext.h>
  10. namespace bgfx
  11. {
  12. typedef int (*PFNGLXSWAPINTERVALMESAPROC)(uint32_t _interval);
  13. PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB;
  14. PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT;
  15. PFNGLXSWAPINTERVALMESAPROC glXSwapIntervalMESA;
  16. PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI;
  17. # define GL_IMPORT(_optional, _proto, _func, _import) _proto _func
  18. # include "glimports.h"
  19. static ::Display* s_display;
  20. static ::Window s_window;
  21. void x11SetDisplayWindow(::Display* _display, ::Window _window)
  22. {
  23. s_display = _display;
  24. s_window = _window;
  25. }
  26. void GlContext::create(uint32_t _width, uint32_t _height)
  27. {
  28. BX_UNUSED(_width, _height);
  29. XLockDisplay(s_display);
  30. int major, minor;
  31. bool version = glXQueryVersion(s_display, &major, &minor);
  32. BGFX_FATAL(version, Fatal::UnableToInitialize, "Failed to query GLX version");
  33. BGFX_FATAL( (major == 1 && minor >= 2) || major > 1
  34. , Fatal::UnableToInitialize
  35. , "GLX version is not >=1.2 (%d.%d)."
  36. , major
  37. , minor
  38. );
  39. int32_t screen = DefaultScreen(s_display);
  40. const char* extensions = glXQueryExtensionsString(s_display, screen);
  41. BX_TRACE("GLX extensions:");
  42. dumpExtensions(extensions);
  43. const int attrsGlx[] =
  44. {
  45. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  46. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  47. GLX_DOUBLEBUFFER, true,
  48. GLX_RED_SIZE, 8,
  49. GLX_BLUE_SIZE, 8,
  50. GLX_GREEN_SIZE, 8,
  51. // GLX_ALPHA_SIZE, 8,
  52. GLX_DEPTH_SIZE, 24,
  53. GLX_STENCIL_SIZE, 8,
  54. 0,
  55. };
  56. // Find suitable config
  57. GLXFBConfig bestConfig = NULL;
  58. int numConfigs;
  59. GLXFBConfig* configs = glXChooseFBConfig(s_display, screen, attrsGlx, &numConfigs);
  60. BX_TRACE("glX num configs %d", numConfigs);
  61. XVisualInfo* visualInfo = NULL;
  62. for (int ii = 0; ii < numConfigs; ++ii)
  63. {
  64. visualInfo = glXGetVisualFromFBConfig(s_display, configs[ii]);
  65. if (NULL != visualInfo)
  66. {
  67. BX_TRACE("---");
  68. bool valid = true;
  69. for (uint32_t attr = 6; attr < BX_COUNTOF(attrsGlx)-1 && attrsGlx[attr] != None; attr += 2)
  70. {
  71. int value;
  72. glXGetFBConfigAttrib(s_display, configs[ii], attrsGlx[attr], &value);
  73. BX_TRACE("glX %d/%d %2d: %4x, %8x (%8x%s)"
  74. , ii
  75. , numConfigs
  76. , attr/2
  77. , attrsGlx[attr]
  78. , value
  79. , attrsGlx[attr + 1]
  80. , value < attrsGlx[attr + 1] ? " *" : ""
  81. );
  82. if (value < attrsGlx[attr + 1])
  83. {
  84. valid = false;
  85. #if !BGFX_CONFIG_DEBUG
  86. break;
  87. #endif // BGFX_CONFIG_DEBUG
  88. }
  89. }
  90. if (valid)
  91. {
  92. bestConfig = configs[ii];
  93. BX_TRACE("Best config %d.", ii);
  94. break;
  95. }
  96. }
  97. XFree(visualInfo);
  98. visualInfo = NULL;
  99. }
  100. XFree(configs);
  101. BGFX_FATAL(visualInfo, Fatal::UnableToInitialize, "Failed to find a suitable X11 display configuration.");
  102. BX_TRACE("Create GL 2.1 context.");
  103. m_context = glXCreateContext(s_display, visualInfo, 0, GL_TRUE);
  104. BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create GL 2.1 context.");
  105. XFree(visualInfo);
  106. #if BGFX_CONFIG_RENDERER_OPENGL >= 31
  107. glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress( (const GLubyte*)"glXCreateContextAttribsARB");
  108. if (NULL != glXCreateContextAttribsARB)
  109. {
  110. BX_TRACE("Create GL 3.1 context.");
  111. const int contextAttrs[] =
  112. {
  113. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  114. GLX_CONTEXT_MINOR_VERSION_ARB, 1,
  115. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  116. 0,
  117. };
  118. GLXContext context = glXCreateContextAttribsARB(s_display, bestConfig, 0, true, contextAttrs);
  119. if (NULL != context)
  120. {
  121. glXDestroyContext(s_display, m_context);
  122. m_context = context;
  123. }
  124. }
  125. #else
  126. BX_UNUSED(bestConfig);
  127. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
  128. XUnlockDisplay(s_display);
  129. import();
  130. glXMakeCurrent(s_display, s_window, m_context);
  131. glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT");
  132. if (NULL != glXSwapIntervalEXT)
  133. {
  134. BX_TRACE("Using glXSwapIntervalEXT.");
  135. glXSwapIntervalEXT(s_display, s_window, 0);
  136. }
  137. else
  138. {
  139. glXSwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalMESA");
  140. if (NULL != glXSwapIntervalMESA)
  141. {
  142. BX_TRACE("Using glXSwapIntervalMESA.");
  143. glXSwapIntervalMESA(0);
  144. }
  145. else
  146. {
  147. glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalSGI");
  148. if (NULL != glXSwapIntervalSGI)
  149. {
  150. BX_TRACE("Using glXSwapIntervalSGI.");
  151. glXSwapIntervalSGI(0);
  152. }
  153. }
  154. }
  155. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  156. glClear(GL_COLOR_BUFFER_BIT);
  157. glXSwapBuffers(s_display, s_window);
  158. }
  159. void GlContext::destroy()
  160. {
  161. glXMakeCurrent(s_display, 0, 0);
  162. glXDestroyContext(s_display, m_context);
  163. }
  164. void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, bool _vsync)
  165. {
  166. int32_t interval = _vsync ? 1 : 0;
  167. if (NULL != glXSwapIntervalEXT)
  168. {
  169. glXSwapIntervalEXT(s_display, s_window, interval);
  170. }
  171. else if (NULL != glXSwapIntervalMESA)
  172. {
  173. glXSwapIntervalMESA(interval);
  174. }
  175. else if (NULL != glXSwapIntervalSGI)
  176. {
  177. glXSwapIntervalSGI(interval);
  178. }
  179. }
  180. void GlContext::swap()
  181. {
  182. glXSwapBuffers(s_display, s_window);
  183. }
  184. void GlContext::import()
  185. {
  186. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  187. { \
  188. if (NULL == _func) \
  189. { \
  190. _func = (_proto)glXGetProcAddress( (const GLubyte*)#_import); \
  191. BX_TRACE("%p " #_func " (" #_import ")", _func); \
  192. BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. glXGetProcAddress %s", #_import); \
  193. } \
  194. }
  195. # include "glimports.h"
  196. }
  197. } // namespace bgfx
  198. #endif // BX_PLATFORM_LINUX & (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)