BsLinuxGLSupport.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Linux/BsLinuxPlatform.h"
  4. #include "Linux/BsLinuxGLSupport.h"
  5. #include "Linux/BsLinuxContext.h"
  6. #include "Linux/BsLinuxRenderWindow.h"
  7. #include "BsLinuxVideoModeInfo.h"
  8. #include "BsGLRenderAPI.h"
  9. namespace bs { namespace ct
  10. {
  11. bool extGLX_ARB_multisample = false;
  12. bool extGLX_ARB_framebuffer_sRGB = false;
  13. bool extGLX_EXT_framebuffer_sRGB = false;
  14. bool extGLX_ARB_create_context = false;
  15. bool extGLX_ARB_create_context_profile = false;
  16. bool extGLX_EXT_swap_control = false;
  17. bool extGLX_MESA_swap_control = false;
  18. bool extGLX_SGI_swap_control = false;
  19. typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
  20. glXCreateContextAttribsARBProc glXCreateContextAttribsARB = nullptr;
  21. bool Load_ARB_create_context()
  22. {
  23. glXCreateContextAttribsARB =
  24. (glXCreateContextAttribsARBProc)glXGetProcAddressARB((const GLubyte*)"glXCreateContextAttribsARB");
  25. return glXCreateContextAttribsARB != nullptr;
  26. }
  27. typedef void (*glXSwapIntervalEXTProc)(::Display*, GLXDrawable, int);
  28. typedef int (*glXSwapIntervalMESAProc)(int);
  29. typedef int (*glXSwapIntervalSGIProc)(int);
  30. glXSwapIntervalEXTProc glXSwapIntervalEXT = nullptr;
  31. glXSwapIntervalMESAProc glXSwapIntervalMESA = nullptr;
  32. glXSwapIntervalSGIProc glXSwapIntervalSGI = nullptr;
  33. bool Load_EXT_swap_control()
  34. {
  35. glXSwapIntervalEXT = (glXSwapIntervalEXTProc)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
  36. return glXSwapIntervalEXT != nullptr;
  37. }
  38. bool Load_MESA_swap_control()
  39. {
  40. glXSwapIntervalMESA = (glXSwapIntervalMESAProc)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalMESA");
  41. return glXSwapIntervalMESA != nullptr;
  42. }
  43. bool Load_SGI_swap_control()
  44. {
  45. glXSwapIntervalSGI = (glXSwapIntervalSGIProc)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalSGI");
  46. return glXSwapIntervalSGI != nullptr;
  47. }
  48. typedef bool (*ExtensionFunc)(void);
  49. struct GLExtension
  50. {
  51. const char* name;
  52. bool* status;
  53. ExtensionFunc func;
  54. };
  55. static GLExtension gExtensionMap[] = {
  56. { "GLX_ARB_multisample", &extGLX_ARB_multisample, nullptr },
  57. { "GLX_ARB_framebuffer_sRGB", &extGLX_ARB_framebuffer_sRGB, nullptr },
  58. { "GLX_EXT_framebuffer_sRGB", &extGLX_EXT_framebuffer_sRGB, nullptr },
  59. { "GLX_ARB_create_context", &extGLX_ARB_create_context, Load_ARB_create_context },
  60. { "GLX_ARB_create_context_profile", &extGLX_ARB_create_context_profile, nullptr },
  61. { "GLX_EXT_swap_control", &extGLX_EXT_swap_control, Load_EXT_swap_control },
  62. { "GLX_MESA_swap_control", &extGLX_MESA_swap_control, Load_MESA_swap_control },
  63. { "GLX_SGI_swap_control", &extGLX_SGI_swap_control, Load_SGI_swap_control },
  64. };
  65. LinuxGLSupport::LinuxGLSupport()
  66. { }
  67. SPtr<bs::RenderWindow> LinuxGLSupport::newWindow(RENDER_WINDOW_DESC& desc, UINT32 windowId,
  68. SPtr<bs::RenderWindow> parentWindow)
  69. {
  70. if(parentWindow != nullptr)
  71. {
  72. ::Window x11window;
  73. parentWindow->getCustomAttribute("WINDOW", &x11window);
  74. desc.platformSpecific["parentWindowHandle"] = toString((UINT64)x11window);
  75. }
  76. bs::LinuxRenderWindow* window = new (bs_alloc<bs::LinuxRenderWindow>()) bs::LinuxRenderWindow(desc, windowId, *this);
  77. return SPtr<bs::RenderWindow>(window, &bs::CoreObject::_delete<bs::LinuxRenderWindow, GenAlloc>);
  78. }
  79. SPtr<RenderWindow> LinuxGLSupport::newWindowCore(RENDER_WINDOW_DESC& desc, UINT32 windowId)
  80. {
  81. LinuxRenderWindow* window = new (bs_alloc<LinuxRenderWindow>()) LinuxRenderWindow(desc, windowId, *this);
  82. return bs_shared_ptr<LinuxRenderWindow>(window);
  83. }
  84. void LinuxGLSupport::start()
  85. {
  86. // Retrieve all essential extensions
  87. LinuxPlatform::lockX();
  88. ::Display* display = LinuxPlatform::getXDisplay();
  89. const char* glExtensions = glXQueryExtensionsString(display, DefaultScreen(display));
  90. const char* iter = glExtensions;
  91. do
  92. {
  93. const char* start = iter;
  94. while(*iter != ' ' && *iter)
  95. iter++;
  96. const char* end = iter;
  97. const char* name = std::string(start, end).c_str();
  98. UINT32 numExtensions = sizeof(gExtensionMap) / sizeof(gExtensionMap[0]);
  99. for (UINT32 i = 0; i < numExtensions; ++i)
  100. {
  101. if(strcmp(name, gExtensionMap[i].name) == 0)
  102. {
  103. if(gExtensionMap[i].func != nullptr)
  104. *gExtensionMap[i].status = gExtensionMap[i].func();
  105. else
  106. *gExtensionMap[i].status = true;
  107. }
  108. }
  109. } while(*iter++);
  110. LinuxPlatform::unlockX();
  111. }
  112. void LinuxGLSupport::stop()
  113. {
  114. // Do nothing
  115. }
  116. SPtr<LinuxContext> LinuxGLSupport::createContext(::Display* x11display, XVisualInfo& visualInfo)
  117. {
  118. GLRenderAPI* rapi = static_cast<GLRenderAPI*>(RenderAPI::instancePtr());
  119. // If RenderAPI has initialized a context use that, otherwise we create our own
  120. if (!rapi->_isContextInitialized())
  121. return bs_shared_ptr_new<LinuxContext>(x11display, visualInfo);
  122. else
  123. {
  124. SPtr<GLContext> context = rapi->getMainContext();
  125. context->setCurrent();
  126. return std::static_pointer_cast<LinuxContext>(context);
  127. }
  128. }
  129. void* LinuxGLSupport::getProcAddress(const String& procname)
  130. {
  131. return (void*)glXGetProcAddressARB((const GLubyte*)procname.c_str());
  132. }
  133. GLVisualConfig LinuxGLSupport::findBestVisual(::Display* display, bool depthStencil, UINT32 multisample, bool srgb) const
  134. {
  135. static constexpr INT32 VISUAL_ATTRIBS[] =
  136. {
  137. GLX_X_RENDERABLE, True,
  138. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  139. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  140. GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
  141. GLX_RED_SIZE, 8,
  142. GLX_GREEN_SIZE, 8,
  143. GLX_BLUE_SIZE, 8,
  144. GLX_ALPHA_SIZE, 8
  145. };
  146. INT32 numConfigs;
  147. GLXFBConfig* configs = glXChooseFBConfig(display, DefaultScreen(display), VISUAL_ATTRIBS, &numConfigs);
  148. GLVisualCapabilities* caps = bs_stack_new<GLVisualCapabilities>(numConfigs);
  149. // Find a config that best matches the requested properties
  150. INT32 bestScore = 0;
  151. INT32 bestConfig = -1;
  152. for (int i = 0; i < numConfigs; ++i)
  153. {
  154. INT32 configScore = 0;
  155. // Depth buffer contributes the most to score
  156. if(depthStencil)
  157. {
  158. INT32 depth, stencil;
  159. glXGetFBConfigAttrib(display, configs[i], GLX_DEPTH_SIZE, &depth);
  160. glXGetFBConfigAttrib(display, configs[i], GLX_STENCIL_SIZE, &stencil);
  161. INT32 score = 0;
  162. if(depth == 24 && stencil == 8)
  163. score = 10000;
  164. else if(depth == 32 && stencil == 8)
  165. score = 9000;
  166. else if(depth == 32)
  167. score = 8000;
  168. else if(depth == 16)
  169. score = 7000;
  170. if(score > 0)
  171. {
  172. configScore += score;
  173. caps[i].depthStencil = true;
  174. }
  175. }
  176. // sRGB contributes second most
  177. if(srgb)
  178. {
  179. INT32 hasSRGB = 0;
  180. if(extGLX_EXT_framebuffer_sRGB)
  181. glXGetFBConfigAttrib(display, configs[i], GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, &hasSRGB);
  182. if(!hasSRGB && extGLX_ARB_framebuffer_sRGB)
  183. glXGetFBConfigAttrib(display, configs[i], GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, &hasSRGB);
  184. if(hasSRGB)
  185. {
  186. configScore += 500;
  187. caps[i].srgb = true;
  188. }
  189. }
  190. if((multisample >= 1) && extGLX_ARB_multisample)
  191. {
  192. INT32 hasMultisample, numSamples;
  193. glXGetFBConfigAttrib(display, configs[i], GLX_SAMPLE_BUFFERS, &hasMultisample);
  194. glXGetFBConfigAttrib(display, configs[i], GLX_SAMPLES, &numSamples);
  195. if(hasMultisample && (numSamples <= (INT32)multisample))
  196. {
  197. configScore += (32 - (multisample - numSamples)) * 10;
  198. caps[i].numSamples = (UINT32)numSamples;
  199. }
  200. }
  201. if(configScore > bestScore)
  202. {
  203. bestScore = configScore;
  204. bestConfig = i;
  205. }
  206. }
  207. GLVisualConfig output;
  208. if(bestConfig == -1)
  209. {
  210. // Something went wrong
  211. XFree(configs);
  212. bs_stack_delete(caps, (UINT32)numConfigs);
  213. return output;
  214. }
  215. XVisualInfo* visualInfo = glXGetVisualFromFBConfig(display, configs[bestConfig]);
  216. output.visualInfo = *visualInfo;
  217. output.caps = caps[bestConfig];
  218. XFree(configs);
  219. XFree(visualInfo);
  220. bs_stack_delete(caps, numConfigs);
  221. return output;
  222. }
  223. SPtr<VideoModeInfo> LinuxGLSupport::getVideoModeInfo() const
  224. {
  225. return bs_shared_ptr_new<LinuxVideoModeInfo>();
  226. }
  227. }}