glcontext_glx.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 2011-2013 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) _proto _func
  18. # include "glimports.h"
  19. # undef GL_IMPORT
  20. static ::Display* s_display;
  21. static ::Window s_window;
  22. void x11SetDisplayWindow(::Display* _display, ::Window _window)
  23. {
  24. s_display = _display;
  25. s_window = _window;
  26. }
  27. void GlContext::create(uint32_t _width, uint32_t _height)
  28. {
  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 = 0;
  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 < 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. break;
  94. }
  95. }
  96. XFree(visualInfo);
  97. visualInfo = 0;
  98. }
  99. XFree(configs);
  100. BGFX_FATAL(visualInfo, Fatal::UnableToInitialize, "Failed to find a suitable X11 display configuration.");
  101. BX_TRACE("Create GL 2.1 context.");
  102. m_context = glXCreateContext(s_display, visualInfo, 0, GL_TRUE);
  103. BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create GL 2.1 context.");
  104. XFree(visualInfo);
  105. #if BGFX_CONFIG_RENDERER_OPENGL >= 31
  106. glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress( (const GLubyte*)"glXCreateContextAttribsARB");
  107. if (NULL != glXCreateContextAttribsARB)
  108. {
  109. BX_TRACE("Create GL 3.1 context.");
  110. const int contextAttrs[] =
  111. {
  112. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  113. GLX_CONTEXT_MINOR_VERSION_ARB, 1,
  114. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  115. 0,
  116. };
  117. GLXContext context = glXCreateContextAttribsARB(s_display, bestConfig, 0, true, contextAttrs);
  118. if (NULL != context)
  119. {
  120. glXDestroyContext(s_display, m_context);
  121. m_context = context;
  122. }
  123. }
  124. #else
  125. BX_UNUSED(bestConfig);
  126. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
  127. XUnlockDisplay(s_display);
  128. import();
  129. glXMakeCurrent(s_display, s_window, m_context);
  130. glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT");
  131. if (NULL != glXSwapIntervalEXT)
  132. {
  133. BX_TRACE("Using glXSwapIntervalEXT.");
  134. glXSwapIntervalEXT(s_display, s_window, 0);
  135. }
  136. else
  137. {
  138. glXSwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalMESA");
  139. if (NULL != glXSwapIntervalMESA)
  140. {
  141. BX_TRACE("Using glXSwapIntervalMESA.");
  142. glXSwapIntervalMESA(0);
  143. }
  144. else
  145. {
  146. glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalSGI");
  147. if (NULL != glXSwapIntervalSGI)
  148. {
  149. BX_TRACE("Using glXSwapIntervalSGI.");
  150. glXSwapIntervalSGI(0);
  151. }
  152. }
  153. }
  154. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  155. glClear(GL_COLOR_BUFFER_BIT);
  156. glXSwapBuffers(s_display, s_window);
  157. }
  158. void GlContext::destroy()
  159. {
  160. glXMakeCurrent(s_display, 0, 0);
  161. glXDestroyContext(s_display, m_context);
  162. }
  163. void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, bool _vsync)
  164. {
  165. int32_t interval = _vsync ? 1 : 0;
  166. if (NULL != glXSwapIntervalEXT)
  167. {
  168. glXSwapIntervalEXT(s_display, s_window, interval);
  169. }
  170. else if (NULL != glXSwapIntervalMESA)
  171. {
  172. glXSwapIntervalMESA(interval);
  173. }
  174. else if (NULL != glXSwapIntervalSGI)
  175. {
  176. glXSwapIntervalSGI(interval);
  177. }
  178. }
  179. void GlContext::swap()
  180. {
  181. glXSwapBuffers(s_display, s_window);
  182. }
  183. void GlContext::import()
  184. {
  185. # define GL_IMPORT(_optional, _proto, _func) \
  186. { \
  187. _func = (_proto)glXGetProcAddress((const GLubyte*)#_func); \
  188. BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. glXGetProcAddress %s", #_func); \
  189. }
  190. # include "glimports.h"
  191. # undef GL_IMPORT
  192. }
  193. } // namespace bgfx
  194. #endif // BX_PLATFORM_LINUX & (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)