2
0

detect_prime_x11.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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-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 "detect_prime.h"
  33. #include "core/print_string.h"
  34. #include "core/ustring.h"
  35. #include <stdlib.h>
  36. #include <GL/gl.h>
  37. #include <GL/glx.h>
  38. #include <X11/Xlib.h>
  39. #include <X11/Xutil.h>
  40. #include <cstring>
  41. #include <sys/types.h>
  42. #include <sys/wait.h>
  43. #include <unistd.h>
  44. #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
  45. #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
  46. typedef GLXContext (*GLXCREATECONTEXTATTRIBSARBPROC)(Display *, GLXFBConfig, GLXContext, Bool, const int *);
  47. struct vendor {
  48. const char *glxvendor;
  49. int priority;
  50. };
  51. vendor vendormap[] = {
  52. { "Advanced Micro Devices, Inc.", 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. GLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((const GLubyte *)"glXCreateContextAttribsARB");
  67. static int visual_attribs[] = {
  68. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  69. GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
  70. GLX_DOUBLEBUFFER, true,
  71. GLX_RED_SIZE, 1,
  72. GLX_GREEN_SIZE, 1,
  73. GLX_BLUE_SIZE, 1,
  74. GLX_DEPTH_SIZE, 24,
  75. None
  76. };
  77. int fbcount;
  78. GLXFBConfig fbconfig = 0;
  79. XVisualInfo *vi = nullptr;
  80. XSetWindowAttributes swa;
  81. swa.event_mask = StructureNotifyMask;
  82. swa.border_pixel = 0;
  83. unsigned long valuemask = CWBorderPixel | CWColormap | CWEventMask;
  84. GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
  85. if (!fbc)
  86. exit(1);
  87. vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
  88. fbconfig = fbc[0];
  89. static int context_attribs[] = {
  90. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  91. GLX_CONTEXT_MINOR_VERSION_ARB, 3,
  92. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  93. GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
  94. None
  95. };
  96. glx_context = glXCreateContextAttribsARB(x11_display, fbconfig, nullptr, true, context_attribs);
  97. swa.colormap = XCreateColormap(x11_display, RootWindow(x11_display, vi->screen), vi->visual, AllocNone);
  98. x11_window = XCreateWindow(x11_display, RootWindow(x11_display, vi->screen), 0, 0, 10, 10, 0, vi->depth, InputOutput, vi->visual, valuemask, &swa);
  99. if (!x11_window)
  100. exit(1);
  101. glXMakeCurrent(x11_display, x11_window, glx_context);
  102. XFree(vi);
  103. }
  104. int detect_prime() {
  105. pid_t p;
  106. int priorities[2];
  107. String vendors[2];
  108. String renderers[2];
  109. vendors[0] = "Unknown";
  110. vendors[1] = "Unknown";
  111. renderers[0] = "Unknown";
  112. renderers[1] = "Unknown";
  113. for (int i = 0; i < 2; ++i) {
  114. int fdset[2];
  115. if (pipe(fdset) == -1) {
  116. print_verbose("Failed to pipe(), using default GPU");
  117. return 0;
  118. }
  119. // Fork so the driver initialization can crash without taking down the engine.
  120. p = fork();
  121. if (p > 0) {
  122. // Main thread
  123. int stat_loc = 0;
  124. char string[201];
  125. string[200] = '\0';
  126. close(fdset[1]);
  127. waitpid(p, &stat_loc, 0);
  128. if (!stat_loc) {
  129. // No need to do anything complicated here. Anything less than
  130. // PIPE_BUF will be delivered in one read() call.
  131. // Leave it 'Unknown' otherwise.
  132. if (read(fdset[0], string, sizeof(string) - 1) > 0) {
  133. vendors[i] = string;
  134. renderers[i] = string + strlen(string) + 1;
  135. }
  136. }
  137. close(fdset[0]);
  138. } else {
  139. // In child, exit() here will not quit the engine.
  140. char string[201];
  141. close(fdset[0]);
  142. if (i)
  143. setenv("DRI_PRIME", "1", 1);
  144. create_context();
  145. const char *vendor = (const char *)glGetString(GL_VENDOR);
  146. const char *renderer = (const char *)glGetString(GL_RENDERER);
  147. unsigned int vendor_len = strlen(vendor) + 1;
  148. unsigned int renderer_len = strlen(renderer) + 1;
  149. if (vendor_len + renderer_len >= sizeof(string)) {
  150. renderer_len = 200 - vendor_len;
  151. }
  152. memcpy(&string, vendor, vendor_len);
  153. memcpy(&string[vendor_len], renderer, renderer_len);
  154. if (write(fdset[1], string, vendor_len + renderer_len) == -1) {
  155. print_verbose("Couldn't write vendor/renderer string.");
  156. }
  157. close(fdset[1]);
  158. exit(0);
  159. }
  160. }
  161. int preferred = 0;
  162. int priority = 0;
  163. if (vendors[0] == vendors[1]) {
  164. print_verbose("Only one GPU found, using default.");
  165. return 0;
  166. }
  167. for (int i = 1; i >= 0; --i) {
  168. vendor *v = vendormap;
  169. while (v->glxvendor) {
  170. if (v->glxvendor == vendors[i]) {
  171. priorities[i] = v->priority;
  172. if (v->priority >= priority) {
  173. priority = v->priority;
  174. preferred = i;
  175. }
  176. }
  177. ++v;
  178. }
  179. }
  180. print_verbose("Found renderers:");
  181. for (int i = 0; i < 2; ++i) {
  182. print_verbose("Renderer " + itos(i) + ": " + renderers[i] + " with priority: " + itos(priorities[i]));
  183. }
  184. print_verbose("Using renderer: " + renderers[preferred]);
  185. return preferred;
  186. }
  187. #endif
  188. #endif