detect_prime_x11.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*************************************************************************/
  2. /* detect_prime_x11.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifdef X11_ENABLED
  31. #if defined(GLES3_ENABLED)
  32. #include "detect_prime_x11.h"
  33. #include "core/string/print_string.h"
  34. #include "core/string/ustring.h"
  35. #include <stdlib.h>
  36. #include "thirdparty/glad/glad/gl.h"
  37. #include "thirdparty/glad/glad/glx.h"
  38. #include "dynwrappers/xlib-so_wrap.h"
  39. #include <cstring>
  40. #include <sys/types.h>
  41. #include <sys/wait.h>
  42. #include <unistd.h>
  43. #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
  44. #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
  45. typedef GLXContext (*GLXCREATECONTEXTATTRIBSARBPROC)(Display *, GLXFBConfig, GLXContext, Bool, const int *);
  46. struct vendor {
  47. const char *glxvendor = nullptr;
  48. int priority = 0;
  49. };
  50. vendor vendormap[] = {
  51. { "Advanced Micro Devices, Inc.", 30 },
  52. { "AMD", 30 },
  53. { "NVIDIA Corporation", 30 },
  54. { "X.Org", 30 },
  55. { "Intel Open Source Technology Center", 20 },
  56. { "Intel", 20 },
  57. { "nouveau", 10 },
  58. { "Mesa Project", 0 },
  59. { nullptr, 0 }
  60. };
  61. // Runs inside a child. Exiting will not quit the engine.
  62. void create_context() {
  63. Display *x11_display = XOpenDisplay(nullptr);
  64. Window x11_window;
  65. GLXContext glx_context;
  66. static int visual_attribs[] = {
  67. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  68. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  69. GLX_DOUBLEBUFFER, true,
  70. GLX_RED_SIZE, 1,
  71. GLX_GREEN_SIZE, 1,
  72. GLX_BLUE_SIZE, 1,
  73. GLX_DEPTH_SIZE, 24,
  74. None
  75. };
  76. if (gladLoaderLoadGLX(x11_display, XScreenNumberOfScreen(XDefaultScreenOfDisplay(x11_display))) == 0) {
  77. print_verbose("Unable to load GLX, GPU detection skipped.");
  78. quick_exit(1);
  79. }
  80. int fbcount;
  81. GLXFBConfig fbconfig = nullptr;
  82. XVisualInfo *vi = nullptr;
  83. XSetWindowAttributes swa;
  84. swa.event_mask = StructureNotifyMask;
  85. swa.border_pixel = 0;
  86. unsigned long valuemask = CWBorderPixel | CWColormap | CWEventMask;
  87. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
  88. if (!fbc) {
  89. quick_exit(1);
  90. }
  91. vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
  92. fbconfig = fbc[0];
  93. static int context_attribs[] = {
  94. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  95. GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  96. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  97. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
  98. None
  99. };
  100. glx_context = glXCreateContextAttribsARB(x11_display, fbconfig, nullptr, true, context_attribs);
  101. swa.colormap = XCreateColormap(x11_display, RootWindow(x11_display, vi->screen), vi->visual, AllocNone);
  102. x11_window = XCreateWindow(x11_display, RootWindow(x11_display, vi->screen), 0, 0, 10, 10, 0, vi->depth, InputOutput, vi->visual, valuemask, &swa);
  103. if (!x11_window) {
  104. quick_exit(1);
  105. }
  106. glXMakeCurrent(x11_display, x11_window, glx_context);
  107. XFree(vi);
  108. }
  109. int detect_prime() {
  110. pid_t p;
  111. int priorities[2] = {};
  112. String vendors[2];
  113. String renderers[2];
  114. vendors[0] = "Unknown";
  115. vendors[1] = "Unknown";
  116. renderers[0] = "Unknown";
  117. renderers[1] = "Unknown";
  118. for (int i = 0; i < 2; ++i) {
  119. int fdset[2];
  120. if (pipe(fdset) == -1) {
  121. print_verbose("Failed to pipe(), using default GPU");
  122. return 0;
  123. }
  124. // Fork so the driver initialization can crash without taking down the engine.
  125. p = fork();
  126. if (p > 0) {
  127. // Main thread
  128. int stat_loc = 0;
  129. char string[201];
  130. string[200] = '\0';
  131. close(fdset[1]);
  132. waitpid(p, &stat_loc, 0);
  133. if (!stat_loc) {
  134. // No need to do anything complicated here. Anything less than
  135. // PIPE_BUF will be delivered in one read() call.
  136. // Leave it 'Unknown' otherwise.
  137. if (read(fdset[0], string, sizeof(string) - 1) > 0) {
  138. vendors[i] = string;
  139. renderers[i] = string + strlen(string) + 1;
  140. }
  141. }
  142. close(fdset[0]);
  143. } else {
  144. // In child, exit() here will not quit the engine.
  145. // Prevent false leak reports as we will not be properly
  146. // cleaning up these processes, and fork() makes a copy
  147. // of all globals.
  148. CoreGlobals::leak_reporting_enabled = false;
  149. char string[201];
  150. close(fdset[0]);
  151. if (i) {
  152. setenv("DRI_PRIME", "1", 1);
  153. }
  154. create_context();
  155. PFNGLGETSTRINGPROC glGetString = (PFNGLGETSTRINGPROC)glXGetProcAddressARB((GLubyte *)"glGetString");
  156. if (!glGetString) {
  157. print_verbose("Unable to get glGetString, GPU detection skipped.");
  158. quick_exit(1);
  159. }
  160. const char *vendor = (const char *)glGetString(GL_VENDOR);
  161. const char *renderer = (const char *)glGetString(GL_RENDERER);
  162. unsigned int vendor_len = strlen(vendor) + 1;
  163. unsigned int renderer_len = strlen(renderer) + 1;
  164. if (vendor_len + renderer_len >= sizeof(string)) {
  165. renderer_len = 200 - vendor_len;
  166. }
  167. memcpy(&string, vendor, vendor_len);
  168. memcpy(&string[vendor_len], renderer, renderer_len);
  169. if (write(fdset[1], string, vendor_len + renderer_len) == -1) {
  170. print_verbose("Couldn't write vendor/renderer string.");
  171. }
  172. close(fdset[1]);
  173. // The function quick_exit() is used because exit() will call destructors on static objects copied by fork().
  174. // These objects will be freed anyway when the process finishes execution.
  175. quick_exit(0);
  176. }
  177. }
  178. int preferred = 0;
  179. int priority = 0;
  180. if (vendors[0] == vendors[1]) {
  181. print_verbose("Only one GPU found, using default.");
  182. return 0;
  183. }
  184. for (int i = 1; i >= 0; --i) {
  185. vendor *v = vendormap;
  186. while (v->glxvendor) {
  187. if (v->glxvendor == vendors[i]) {
  188. priorities[i] = v->priority;
  189. if (v->priority >= priority) {
  190. priority = v->priority;
  191. preferred = i;
  192. }
  193. }
  194. ++v;
  195. }
  196. }
  197. print_verbose("Found renderers:");
  198. for (int i = 0; i < 2; ++i) {
  199. print_verbose("Renderer " + itos(i) + ": " + renderers[i] + " with priority: " + itos(priorities[i]));
  200. }
  201. print_verbose("Using renderer: " + renderers[preferred]);
  202. return preferred;
  203. }
  204. #endif
  205. #endif