detect_prime_x11.cpp 7.0 KB

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