BsLinuxGLSupport.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Private/Linux/BsLinuxPlatform.h"
  4. #include "Linux/BsLinuxGLSupport.h"
  5. #include "Linux/BsLinuxContext.h"
  6. #include "Linux/BsLinuxRenderWindow.h"
  7. #include "Linux/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. glXSwapIntervalEXTProc glXSwapIntervalEXT = nullptr;
  28. glXSwapIntervalMESAProc glXSwapIntervalMESA = nullptr;
  29. glXSwapIntervalSGIProc glXSwapIntervalSGI = nullptr;
  30. glXChooseFBConfigProc glXChooseFBConfig = nullptr;
  31. glXGetFBConfigAttribProc glXGetFBConfigAttrib = nullptr;
  32. glXGetVisualFromFBConfigProc glXGetVisualFromFBConfig = 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. void LinuxGLSupport::start()
  80. {
  81. // Retrieve all essential extensions
  82. LinuxPlatform::lockX();
  83. ::Display* display = LinuxPlatform::getXDisplay();
  84. const char* glExtensions = glXQueryExtensionsString(display, DefaultScreen(display));
  85. const char* iter = glExtensions;
  86. do
  87. {
  88. const char* start = iter;
  89. while(*iter != ' ' && *iter)
  90. iter++;
  91. const char* end = iter;
  92. std::string name(start, end);
  93. UINT32 numExtensions = sizeof(gExtensionMap) / sizeof(gExtensionMap[0]);
  94. for (UINT32 i = 0; i < numExtensions; ++i)
  95. {
  96. if(strcmp(name.c_str(), gExtensionMap[i].name) == 0)
  97. {
  98. if(gExtensionMap[i].status != nullptr)
  99. {
  100. if (gExtensionMap[i].func != nullptr)
  101. *gExtensionMap[i].status = gExtensionMap[i].func();
  102. else
  103. *gExtensionMap[i].status = true;
  104. }
  105. else
  106. {
  107. if (gExtensionMap[i].func != nullptr)
  108. gExtensionMap[i].func();
  109. }
  110. }
  111. }
  112. } while(*iter++);
  113. glXChooseFBConfig = (glXChooseFBConfigProc)glXGetProcAddressARB((const GLubyte*)"glXChooseFBConfig");
  114. glXGetFBConfigAttrib = (glXGetFBConfigAttribProc)glXGetProcAddressARB((const GLubyte*)"glXGetFBConfigAttrib");
  115. glXGetVisualFromFBConfig = (glXGetVisualFromFBConfigProc)glXGetProcAddressARB((const GLubyte*)"glXGetVisualFromFBConfig");
  116. LinuxPlatform::unlockX();
  117. }
  118. void LinuxGLSupport::stop()
  119. {
  120. // Do nothing
  121. }
  122. SPtr<LinuxContext> LinuxGLSupport::createContext(::Display* x11display, XVisualInfo& visualInfo)
  123. {
  124. GLRenderAPI* rapi = static_cast<GLRenderAPI*>(RenderAPI::instancePtr());
  125. // If RenderAPI has initialized a context use that, otherwise we create our own
  126. if (!rapi->_isContextInitialized())
  127. return bs_shared_ptr_new<LinuxContext>(x11display, visualInfo);
  128. else
  129. {
  130. SPtr<GLContext> context = rapi->_getMainContext();
  131. return std::static_pointer_cast<LinuxContext>(context);
  132. }
  133. }
  134. void* LinuxGLSupport::getProcAddress(const String& procname)
  135. {
  136. return (void*)glXGetProcAddressARB((const GLubyte*)procname.c_str());
  137. }
  138. GLVisualConfig LinuxGLSupport::findBestVisual(::Display* display, bool depthStencil, UINT32 multisample, bool srgb) const
  139. {
  140. INT32 VISUAL_ATTRIBS[] =
  141. {
  142. GLX_X_RENDERABLE, True,
  143. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  144. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  145. GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
  146. GLX_RED_SIZE, 8,
  147. GLX_GREEN_SIZE, 8,
  148. GLX_BLUE_SIZE, 8,
  149. GLX_ALPHA_SIZE, 8,
  150. GLX_DOUBLEBUFFER, True,
  151. GLX_DEPTH_SIZE, depthStencil ? 24 : 0,
  152. GLX_STENCIL_SIZE, depthStencil ? 8 : 0,
  153. GLX_SAMPLE_BUFFERS, multisample > 1 ? 1 : 0,
  154. 0
  155. };
  156. INT32 numConfigs;
  157. GLXFBConfig* configs = glXChooseFBConfig(display, DefaultScreen(display), VISUAL_ATTRIBS, &numConfigs);
  158. GLVisualCapabilities* caps = bs_stack_new<GLVisualCapabilities>(numConfigs);
  159. // Find a config that best matches the requested properties
  160. INT32 bestScore = 0;
  161. INT32 bestConfig = -1;
  162. for (int i = 0; i < numConfigs; ++i)
  163. {
  164. INT32 configScore = 0;
  165. // Depth buffer contributes the most to score
  166. INT32 depth, stencil;
  167. glXGetFBConfigAttrib(display, configs[i], GLX_DEPTH_SIZE, &depth);
  168. glXGetFBConfigAttrib(display, configs[i], GLX_STENCIL_SIZE, &stencil);
  169. // Depth buffer was requested
  170. if(depthStencil)
  171. {
  172. INT32 score = 0;
  173. if (depth == 24 && stencil == 8)
  174. score = 10000;
  175. else if (depth == 32 && stencil == 8)
  176. score = 9000;
  177. else if (depth == 32)
  178. score = 8000;
  179. else if (depth == 24)
  180. score = 7000;
  181. else if (depth == 16)
  182. score = 6000;
  183. if (score > 0)
  184. {
  185. configScore += score;
  186. caps[i].depthStencil = true;
  187. }
  188. }
  189. else // Depth buffer not requested, prefer configs without it
  190. {
  191. if(depth == 0 && stencil == 0)
  192. configScore += 10000;
  193. }
  194. // sRGB contributes second most
  195. if(srgb)
  196. {
  197. INT32 hasSRGB = 0;
  198. if(extGLX_EXT_framebuffer_sRGB)
  199. glXGetFBConfigAttrib(display, configs[i], GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, &hasSRGB);
  200. if(!hasSRGB && extGLX_ARB_framebuffer_sRGB)
  201. glXGetFBConfigAttrib(display, configs[i], GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT, &hasSRGB);
  202. if(hasSRGB)
  203. {
  204. configScore += 500;
  205. caps[i].srgb = true;
  206. }
  207. }
  208. if((multisample >= 1) && extGLX_ARB_multisample)
  209. {
  210. INT32 hasMultisample, numSamples;
  211. glXGetFBConfigAttrib(display, configs[i], GLX_SAMPLE_BUFFERS, &hasMultisample);
  212. glXGetFBConfigAttrib(display, configs[i], GLX_SAMPLES, &numSamples);
  213. if(hasMultisample && (numSamples <= (INT32)multisample))
  214. {
  215. configScore += (32 - (multisample - numSamples)) * 10;
  216. caps[i].numSamples = (UINT32)numSamples;
  217. }
  218. }
  219. if(configScore > bestScore)
  220. {
  221. bestScore = configScore;
  222. bestConfig = i;
  223. }
  224. }
  225. GLVisualConfig output;
  226. if(bestConfig == -1)
  227. {
  228. if(numConfigs > 0)
  229. bestConfig = 0;
  230. else
  231. {
  232. // Something went wrong
  233. XFree(configs);
  234. bs_stack_delete(caps, (UINT32) numConfigs);
  235. return output;
  236. }
  237. }
  238. XVisualInfo* visualInfo = glXGetVisualFromFBConfig(display, configs[bestConfig]);
  239. output.visualInfo = *visualInfo;
  240. output.caps = caps[bestConfig];
  241. XFree(configs);
  242. XFree(visualInfo);
  243. bs_stack_delete(caps, numConfigs);
  244. return output;
  245. }
  246. SPtr<VideoModeInfo> LinuxGLSupport::getVideoModeInfo() const
  247. {
  248. return bs_shared_ptr_new<LinuxVideoModeInfo>();
  249. }
  250. }}