detect_prime.cpp 6.9 KB

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