detect_prime.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*************************************************************************/
  2. /* detect_prime.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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(OPENGL_ENABLED)
  32. #include "core/print_string.h"
  33. #include "core/ustring.h"
  34. #include <stdlib.h>
  35. #include <GL/gl.h>
  36. #include <GL/glx.h>
  37. #include <X11/Xlib.h>
  38. #include <X11/Xutil.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;
  48. int priority;
  49. };
  50. vendor vendormap[] = {
  51. { "Advanced Micro Devices, Inc.", 30 },
  52. { "NVIDIA Corporation", 30 },
  53. { "X.Org", 30 },
  54. { "Intel Open Source Technology Center", 20 },
  55. { "Intel", 20 },
  56. { "nouveau", 10 },
  57. { "Mesa Project", 0 },
  58. { NULL, 0 }
  59. };
  60. // Runs inside a child. Exiting will not quit the engine.
  61. void create_context() {
  62. Display *x11_display = XOpenDisplay(NULL);
  63. Window x11_window;
  64. GLXContext glx_context;
  65. GLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((const GLubyte *)"glXCreateContextAttribsARB");
  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. int fbcount;
  77. GLXFBConfig fbconfig = 0;
  78. XVisualInfo *vi = NULL;
  79. XSetWindowAttributes swa;
  80. swa.event_mask = StructureNotifyMask;
  81. swa.border_pixel = 0;
  82. unsigned long valuemask = CWBorderPixel | CWColormap | CWEventMask;
  83. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
  84. if (!fbc)
  85. exit(1);
  86. vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
  87. fbconfig = fbc[0];
  88. static int context_attribs[] = {
  89. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  90. GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  91. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  92. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
  93. None
  94. };
  95. glx_context = glXCreateContextAttribsARB(x11_display, fbconfig, NULL, true, context_attribs);
  96. swa.colormap = XCreateColormap(x11_display, RootWindow(x11_display, vi->screen), vi->visual, AllocNone);
  97. x11_window = XCreateWindow(x11_display, RootWindow(x11_display, vi->screen), 0, 0, 10, 10, 0, vi->depth, InputOutput, vi->visual, valuemask, &swa);
  98. if (!x11_window)
  99. exit(1);
  100. glXMakeCurrent(x11_display, x11_window, glx_context);
  101. XFree(vi);
  102. }
  103. int detect_prime() {
  104. pid_t p;
  105. int priorities[2];
  106. String vendors[2];
  107. String renderers[2];
  108. vendors[0] = "Unknown";
  109. vendors[1] = "Unknown";
  110. renderers[0] = "Unknown";
  111. renderers[1] = "Unknown";
  112. for (int i = 0; i < 2; ++i) {
  113. int fdset[2];
  114. if (pipe(fdset) == -1) {
  115. print_verbose("Failed to pipe(), using default GPU");
  116. return 0;
  117. }
  118. // Fork so the driver initialization can crash without taking down the engine.
  119. p = fork();
  120. if (p > 0) {
  121. // Main thread
  122. int stat_loc = 0;
  123. char string[201];
  124. string[200] = '\0';
  125. close(fdset[1]);
  126. waitpid(p, &stat_loc, 0);
  127. if (!stat_loc) {
  128. // No need to do anything complicated here. Anything less than
  129. // PIPE_BUF will be delivered in one read() call.
  130. // Leave it 'Unknown' otherwise.
  131. if (read(fdset[0], string, sizeof(string) - 1) > 0) {
  132. vendors[i] = string;
  133. renderers[i] = string + strlen(string) + 1;
  134. }
  135. }
  136. close(fdset[0]);
  137. } else {
  138. // In child, exit() here will not quit the engine.
  139. char string[201];
  140. close(fdset[0]);
  141. if (i) setenv("DRI_PRIME", "1", 1);
  142. create_context();
  143. const char *vendor = (const char *)glGetString(GL_VENDOR);
  144. const char *renderer = (const char *)glGetString(GL_RENDERER);
  145. unsigned int vendor_len = strlen(vendor) + 1;
  146. unsigned int renderer_len = strlen(renderer) + 1;
  147. if (vendor_len + renderer_len >= sizeof(string)) {
  148. renderer_len = 200 - vendor_len;
  149. }
  150. memcpy(&string, vendor, vendor_len);
  151. memcpy(&string[vendor_len], renderer, renderer_len);
  152. if (write(fdset[1], string, vendor_len + renderer_len) == -1) {
  153. print_verbose("Couldn't write vendor/renderer string.");
  154. }
  155. close(fdset[1]);
  156. exit(0);
  157. }
  158. }
  159. int preferred = 0;
  160. int priority = 0;
  161. if (vendors[0] == vendors[1]) {
  162. print_verbose("Only one GPU found, using default.");
  163. return 0;
  164. }
  165. for (int i = 1; i >= 0; --i) {
  166. vendor *v = vendormap;
  167. while (v->glxvendor) {
  168. if (v->glxvendor == vendors[i]) {
  169. priorities[i] = v->priority;
  170. if (v->priority >= priority) {
  171. priority = v->priority;
  172. preferred = i;
  173. }
  174. }
  175. ++v;
  176. }
  177. }
  178. print_verbose("Found renderers:");
  179. for (int i = 0; i < 2; ++i) {
  180. print_verbose("Renderer " + itos(i) + ": " + renderers[i] + " with priority: " + itos(priorities[i]));
  181. }
  182. print_verbose("Using renderer: " + renderers[preferred]);
  183. return preferred;
  184. }
  185. #endif
  186. #endif