detect_prime_x11.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**************************************************************************/
  2. /* detect_prime_x11.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #if defined(X11_ENABLED) && defined(GLES3_ENABLED)
  31. #include "detect_prime_x11.h"
  32. #include "core/string/print_string.h"
  33. #include "core/string/ustring.h"
  34. #include "thirdparty/glad/glad/gl.h"
  35. #include "thirdparty/glad/glad/glx.h"
  36. #ifdef SOWRAP_ENABLED
  37. #include "x11/dynwrappers/xlib-so_wrap.h"
  38. #else
  39. #include <X11/XKBlib.h>
  40. #include <X11/Xlib.h>
  41. #include <X11/Xutil.h>
  42. #endif
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <sys/types.h>
  46. #include <sys/wait.h>
  47. #include <unistd.h>
  48. #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
  49. #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
  50. typedef GLXContext (*GLXCREATECONTEXTATTRIBSARBPROC)(Display *, GLXFBConfig, GLXContext, Bool, const int *);
  51. // To prevent shadowing warnings
  52. #undef glGetString
  53. struct vendor {
  54. const char *glxvendor = nullptr;
  55. int priority = 0;
  56. };
  57. vendor vendormap[] = {
  58. { "Advanced Micro Devices, Inc.", 30 },
  59. { "AMD", 30 },
  60. { "NVIDIA Corporation", 30 },
  61. { "X.Org", 30 },
  62. { "Intel Open Source Technology Center", 20 },
  63. { "Intel", 20 },
  64. { "nouveau", 10 },
  65. { "Mesa Project", 0 },
  66. { nullptr, 0 }
  67. };
  68. // Runs inside a child. Exiting will not quit the engine.
  69. void create_context() {
  70. Display *x11_display = XOpenDisplay(nullptr);
  71. Window x11_window;
  72. GLXContext glx_context;
  73. static int visual_attribs[] = {
  74. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  75. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  76. GLX_DOUBLEBUFFER, true,
  77. GLX_RED_SIZE, 1,
  78. GLX_GREEN_SIZE, 1,
  79. GLX_BLUE_SIZE, 1,
  80. GLX_DEPTH_SIZE, 24,
  81. None
  82. };
  83. if (gladLoaderLoadGLX(x11_display, XScreenNumberOfScreen(XDefaultScreenOfDisplay(x11_display))) == 0) {
  84. print_verbose("Unable to load GLX, GPU detection skipped.");
  85. quick_exit(1);
  86. }
  87. int fbcount;
  88. GLXFBConfig fbconfig = nullptr;
  89. XVisualInfo *vi = nullptr;
  90. XSetWindowAttributes swa;
  91. swa.event_mask = StructureNotifyMask;
  92. swa.border_pixel = 0;
  93. unsigned long valuemask = CWBorderPixel | CWColormap | CWEventMask;
  94. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
  95. if (!fbc) {
  96. quick_exit(1);
  97. }
  98. vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
  99. fbconfig = fbc[0];
  100. static int context_attribs[] = {
  101. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  102. GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  103. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  104. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
  105. None
  106. };
  107. glx_context = glXCreateContextAttribsARB(x11_display, fbconfig, nullptr, true, context_attribs);
  108. swa.colormap = XCreateColormap(x11_display, RootWindow(x11_display, vi->screen), vi->visual, AllocNone);
  109. x11_window = XCreateWindow(x11_display, RootWindow(x11_display, vi->screen), 0, 0, 10, 10, 0, vi->depth, InputOutput, vi->visual, valuemask, &swa);
  110. if (!x11_window) {
  111. quick_exit(1);
  112. }
  113. glXMakeCurrent(x11_display, x11_window, glx_context);
  114. XFree(vi);
  115. }
  116. int silent_error_handler(Display *display, XErrorEvent *error) {
  117. static char message[1024];
  118. XGetErrorText(display, error->error_code, message, sizeof(message));
  119. print_verbose(vformat("XServer error: %s"
  120. "\n Major opcode of failed request: %d"
  121. "\n Serial number of failed request: %d"
  122. "\n Current serial number in output stream: %d",
  123. String::utf8(message), (uint64_t)error->request_code, (uint64_t)error->minor_code, (uint64_t)error->serial));
  124. quick_exit(1);
  125. return 0;
  126. }
  127. int detect_prime() {
  128. pid_t p;
  129. int priorities[2] = {};
  130. String vendors[2];
  131. String renderers[2];
  132. vendors[0] = "Unknown";
  133. vendors[1] = "Unknown";
  134. renderers[0] = "Unknown";
  135. renderers[1] = "Unknown";
  136. for (int i = 0; i < 2; ++i) {
  137. int fdset[2];
  138. if (pipe(fdset) == -1) {
  139. print_verbose("Failed to pipe(), using default GPU");
  140. return 0;
  141. }
  142. // Fork so the driver initialization can crash without taking down the engine.
  143. p = fork();
  144. if (p > 0) {
  145. // Main thread
  146. int stat_loc = 0;
  147. char string[201];
  148. string[200] = '\0';
  149. close(fdset[1]);
  150. waitpid(p, &stat_loc, 0);
  151. if (!stat_loc) {
  152. // No need to do anything complicated here. Anything less than
  153. // PIPE_BUF will be delivered in one read() call.
  154. // Leave it 'Unknown' otherwise.
  155. if (read(fdset[0], string, sizeof(string) - 1) > 0) {
  156. vendors[i] = string;
  157. renderers[i] = string + strlen(string) + 1;
  158. }
  159. }
  160. close(fdset[0]);
  161. } else {
  162. // In child, exit() here will not quit the engine.
  163. // Prevent false leak reports as we will not be properly
  164. // cleaning up these processes, and fork() makes a copy
  165. // of all globals.
  166. CoreGlobals::leak_reporting_enabled = false;
  167. XSetErrorHandler(&silent_error_handler);
  168. char string[201];
  169. close(fdset[0]);
  170. if (i) {
  171. setenv("DRI_PRIME", "1", 1);
  172. }
  173. create_context();
  174. PFNGLGETSTRINGPROC glGetString = (PFNGLGETSTRINGPROC)glXGetProcAddressARB((GLubyte *)"glGetString");
  175. if (!glGetString) {
  176. print_verbose("Unable to get glGetString, GPU detection skipped.");
  177. quick_exit(1);
  178. }
  179. const char *vendor = (const char *)glGetString(GL_VENDOR);
  180. const char *renderer = (const char *)glGetString(GL_RENDERER);
  181. unsigned int vendor_len = strlen(vendor) + 1;
  182. unsigned int renderer_len = strlen(renderer) + 1;
  183. if (vendor_len + renderer_len >= sizeof(string)) {
  184. renderer_len = 200 - vendor_len;
  185. }
  186. memcpy(&string, vendor, vendor_len);
  187. memcpy(&string[vendor_len], renderer, renderer_len);
  188. if (write(fdset[1], string, vendor_len + renderer_len) == -1) {
  189. print_verbose("Couldn't write vendor/renderer string.");
  190. }
  191. close(fdset[1]);
  192. // The function quick_exit() is used because exit() will call destructors on static objects copied by fork().
  193. // These objects will be freed anyway when the process finishes execution.
  194. quick_exit(0);
  195. }
  196. }
  197. int preferred = 0;
  198. int priority = 0;
  199. if (vendors[0] == vendors[1]) {
  200. print_verbose("Only one GPU found, using default.");
  201. return 0;
  202. }
  203. for (int i = 1; i >= 0; --i) {
  204. vendor *v = vendormap;
  205. while (v->glxvendor) {
  206. if (v->glxvendor == vendors[i]) {
  207. priorities[i] = v->priority;
  208. if (v->priority >= priority) {
  209. priority = v->priority;
  210. preferred = i;
  211. }
  212. }
  213. ++v;
  214. }
  215. }
  216. print_verbose("Found renderers:");
  217. for (int i = 0; i < 2; ++i) {
  218. print_verbose("Renderer " + itos(i) + ": " + renderers[i] + " with priority: " + itos(priorities[i]));
  219. }
  220. print_verbose("Using renderer: " + renderers[preferred]);
  221. return preferred;
  222. }
  223. #endif // X11_ENABLED && GLES3_ENABLED