detect_prime_x11.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 detect_prime() {
  117. pid_t p;
  118. int priorities[2] = {};
  119. String vendors[2];
  120. String renderers[2];
  121. vendors[0] = "Unknown";
  122. vendors[1] = "Unknown";
  123. renderers[0] = "Unknown";
  124. renderers[1] = "Unknown";
  125. for (int i = 0; i < 2; ++i) {
  126. int fdset[2];
  127. if (pipe(fdset) == -1) {
  128. print_verbose("Failed to pipe(), using default GPU");
  129. return 0;
  130. }
  131. // Fork so the driver initialization can crash without taking down the engine.
  132. p = fork();
  133. if (p > 0) {
  134. // Main thread
  135. int stat_loc = 0;
  136. char string[201];
  137. string[200] = '\0';
  138. close(fdset[1]);
  139. waitpid(p, &stat_loc, 0);
  140. if (!stat_loc) {
  141. // No need to do anything complicated here. Anything less than
  142. // PIPE_BUF will be delivered in one read() call.
  143. // Leave it 'Unknown' otherwise.
  144. if (read(fdset[0], string, sizeof(string) - 1) > 0) {
  145. vendors[i] = string;
  146. renderers[i] = string + strlen(string) + 1;
  147. }
  148. }
  149. close(fdset[0]);
  150. } else {
  151. // In child, exit() here will not quit the engine.
  152. // Prevent false leak reports as we will not be properly
  153. // cleaning up these processes, and fork() makes a copy
  154. // of all globals.
  155. CoreGlobals::leak_reporting_enabled = false;
  156. char string[201];
  157. close(fdset[0]);
  158. if (i) {
  159. setenv("DRI_PRIME", "1", 1);
  160. }
  161. create_context();
  162. PFNGLGETSTRINGPROC glGetString = (PFNGLGETSTRINGPROC)glXGetProcAddressARB((GLubyte *)"glGetString");
  163. if (!glGetString) {
  164. print_verbose("Unable to get glGetString, GPU detection skipped.");
  165. quick_exit(1);
  166. }
  167. const char *vendor = (const char *)glGetString(GL_VENDOR);
  168. const char *renderer = (const char *)glGetString(GL_RENDERER);
  169. unsigned int vendor_len = strlen(vendor) + 1;
  170. unsigned int renderer_len = strlen(renderer) + 1;
  171. if (vendor_len + renderer_len >= sizeof(string)) {
  172. renderer_len = 200 - vendor_len;
  173. }
  174. memcpy(&string, vendor, vendor_len);
  175. memcpy(&string[vendor_len], renderer, renderer_len);
  176. if (write(fdset[1], string, vendor_len + renderer_len) == -1) {
  177. print_verbose("Couldn't write vendor/renderer string.");
  178. }
  179. close(fdset[1]);
  180. // The function quick_exit() is used because exit() will call destructors on static objects copied by fork().
  181. // These objects will be freed anyway when the process finishes execution.
  182. quick_exit(0);
  183. }
  184. }
  185. int preferred = 0;
  186. int priority = 0;
  187. if (vendors[0] == vendors[1]) {
  188. print_verbose("Only one GPU found, using default.");
  189. return 0;
  190. }
  191. for (int i = 1; i >= 0; --i) {
  192. vendor *v = vendormap;
  193. while (v->glxvendor) {
  194. if (v->glxvendor == vendors[i]) {
  195. priorities[i] = v->priority;
  196. if (v->priority >= priority) {
  197. priority = v->priority;
  198. preferred = i;
  199. }
  200. }
  201. ++v;
  202. }
  203. }
  204. print_verbose("Found renderers:");
  205. for (int i = 0; i < 2; ++i) {
  206. print_verbose("Renderer " + itos(i) + ": " + renderers[i] + " with priority: " + itos(priorities[i]));
  207. }
  208. print_verbose("Using renderer: " + renderers[preferred]);
  209. return preferred;
  210. }
  211. #endif // X11_ENABLED && GLES3_ENABLED