detect_prime_x11.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/variant/variant.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 <sys/types.h>
  44. #include <sys/wait.h>
  45. #include <unistd.h>
  46. #include <cstdlib>
  47. #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
  48. #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
  49. typedef GLXContext (*GLXCREATECONTEXTATTRIBSARBPROC)(Display *, GLXFBConfig, GLXContext, Bool, const int *);
  50. // To prevent shadowing warnings
  51. #undef glGetString
  52. int silent_error_handler(Display *display, XErrorEvent *error) {
  53. static char message[1024];
  54. XGetErrorText(display, error->error_code, message, sizeof(message));
  55. print_verbose(vformat("XServer error: %s"
  56. "\n Major opcode of failed request: %d"
  57. "\n Serial number of failed request: %d"
  58. "\n Current serial number in output stream: %d",
  59. String::utf8(message), (uint64_t)error->request_code, (uint64_t)error->minor_code, (uint64_t)error->serial));
  60. quick_exit(1);
  61. return 0;
  62. }
  63. // Runs inside a child. Exiting will not quit the engine.
  64. void DetectPrimeX11::create_context() {
  65. XSetErrorHandler(&silent_error_handler);
  66. Display *x11_display = XOpenDisplay(nullptr);
  67. Window x11_window;
  68. GLXContext glx_context;
  69. static int visual_attribs[] = {
  70. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  71. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  72. GLX_DOUBLEBUFFER, true,
  73. GLX_RED_SIZE, 1,
  74. GLX_GREEN_SIZE, 1,
  75. GLX_BLUE_SIZE, 1,
  76. GLX_DEPTH_SIZE, 24,
  77. None
  78. };
  79. if (gladLoaderLoadGLX(x11_display, XScreenNumberOfScreen(XDefaultScreenOfDisplay(x11_display))) == 0) {
  80. print_verbose("Unable to load GLX, GPU detection skipped.");
  81. quick_exit(1);
  82. }
  83. int fbcount;
  84. GLXFBConfig fbconfig = nullptr;
  85. XVisualInfo *vi = nullptr;
  86. XSetWindowAttributes swa;
  87. swa.event_mask = StructureNotifyMask;
  88. swa.border_pixel = 0;
  89. unsigned long valuemask = CWBorderPixel | CWColormap | CWEventMask;
  90. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
  91. if (!fbc) {
  92. quick_exit(1);
  93. }
  94. vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
  95. fbconfig = fbc[0];
  96. static int context_attribs[] = {
  97. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  98. GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  99. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  100. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
  101. None
  102. };
  103. glx_context = glXCreateContextAttribsARB(x11_display, fbconfig, nullptr, true, context_attribs);
  104. swa.colormap = XCreateColormap(x11_display, RootWindow(x11_display, vi->screen), vi->visual, AllocNone);
  105. x11_window = XCreateWindow(x11_display, RootWindow(x11_display, vi->screen), 0, 0, 10, 10, 0, vi->depth, InputOutput, vi->visual, valuemask, &swa);
  106. if (!x11_window) {
  107. quick_exit(1);
  108. }
  109. glXMakeCurrent(x11_display, x11_window, glx_context);
  110. XFree(vi);
  111. }
  112. int DetectPrimeX11::detect_prime() {
  113. pid_t p;
  114. int priorities[2] = {};
  115. String vendors[2];
  116. String renderers[2];
  117. vendors[0] = "Unknown";
  118. vendors[1] = "Unknown";
  119. renderers[0] = "Unknown";
  120. renderers[1] = "Unknown";
  121. for (int i = 0; i < 2; ++i) {
  122. int fdset[2];
  123. if (pipe(fdset) == -1) {
  124. print_verbose("Failed to pipe(), using default GPU");
  125. return 0;
  126. }
  127. // Fork so the driver initialization can crash without taking down the engine.
  128. p = fork();
  129. if (p > 0) {
  130. // Main thread
  131. int stat_loc = 0;
  132. char string[201];
  133. string[200] = '\0';
  134. close(fdset[1]);
  135. waitpid(p, &stat_loc, 0);
  136. if (!stat_loc) {
  137. // No need to do anything complicated here. Anything less than
  138. // PIPE_BUF will be delivered in one read() call.
  139. // Leave it 'Unknown' otherwise.
  140. if (read(fdset[0], string, sizeof(string) - 1) > 0) {
  141. vendors[i] = string;
  142. renderers[i] = string + strlen(string) + 1;
  143. }
  144. }
  145. close(fdset[0]);
  146. } else {
  147. // In child, exit() here will not quit the engine.
  148. // Prevent false leak reports as we will not be properly
  149. // cleaning up these processes, and fork() makes a copy
  150. // of all globals.
  151. CoreGlobals::leak_reporting_enabled = false;
  152. char string[201];
  153. close(fdset[0]);
  154. if (i) {
  155. setenv("DRI_PRIME", "1", 1);
  156. }
  157. create_context();
  158. PFNGLGETSTRINGPROC glGetString = (PFNGLGETSTRINGPROC)glXGetProcAddressARB((GLubyte *)"glGetString");
  159. if (!glGetString) {
  160. print_verbose("Unable to get glGetString, GPU detection skipped.");
  161. quick_exit(1);
  162. }
  163. const char *vendor = (const char *)glGetString(GL_VENDOR);
  164. const char *renderer = (const char *)glGetString(GL_RENDERER);
  165. unsigned int vendor_len = strlen(vendor) + 1;
  166. unsigned int renderer_len = strlen(renderer) + 1;
  167. if (vendor_len + renderer_len >= sizeof(string)) {
  168. renderer_len = 200 - vendor_len;
  169. }
  170. memcpy(&string, vendor, vendor_len);
  171. memcpy(&string[vendor_len], renderer, renderer_len);
  172. if (write(fdset[1], string, vendor_len + renderer_len) == -1) {
  173. print_verbose("Couldn't write vendor/renderer string.");
  174. }
  175. close(fdset[1]);
  176. // The function quick_exit() is used because exit() will call destructors on static objects copied by fork().
  177. // These objects will be freed anyway when the process finishes execution.
  178. quick_exit(0);
  179. }
  180. }
  181. int preferred = 0;
  182. int priority = 0;
  183. if (vendors[0] == vendors[1]) {
  184. print_verbose("Only one GPU found, using default.");
  185. return 0;
  186. }
  187. for (int i = 1; i >= 0; --i) {
  188. const Vendor *v = vendor_map;
  189. while (v->glxvendor) {
  190. if (v->glxvendor == vendors[i]) {
  191. priorities[i] = v->priority;
  192. if (v->priority >= priority) {
  193. priority = v->priority;
  194. preferred = i;
  195. }
  196. }
  197. ++v;
  198. }
  199. }
  200. print_verbose("Found renderers:");
  201. for (int i = 0; i < 2; ++i) {
  202. print_verbose("Renderer " + itos(i) + ": " + renderers[i] + " with priority: " + itos(priorities[i]));
  203. }
  204. print_verbose("Using renderer: " + renderers[preferred]);
  205. return preferred;
  206. }
  207. #endif // X11_ENABLED && GLES3_ENABLED