Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/linuxbsd/x11/detect_prime_x11.cpp
10278 views
1
/**************************************************************************/
2
/* detect_prime_x11.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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
31
#if defined(X11_ENABLED) && defined(GLES3_ENABLED)
32
33
#include "detect_prime_x11.h"
34
35
#include "core/string/print_string.h"
36
#include "core/string/ustring.h"
37
38
#include "thirdparty/glad/glad/gl.h"
39
#include "thirdparty/glad/glad/glx.h"
40
41
#ifdef SOWRAP_ENABLED
42
#include "x11/dynwrappers/xlib-so_wrap.h"
43
#else
44
#include <X11/XKBlib.h>
45
#include <X11/Xlib.h>
46
#include <X11/Xutil.h>
47
#endif
48
49
#include <sys/types.h>
50
#include <sys/wait.h>
51
#include <unistd.h>
52
#include <cstdlib>
53
54
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
55
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
56
57
typedef GLXContext (*GLXCREATECONTEXTATTRIBSARBPROC)(Display *, GLXFBConfig, GLXContext, Bool, const int *);
58
59
// To prevent shadowing warnings
60
#undef glGetString
61
62
int silent_error_handler(Display *display, XErrorEvent *error) {
63
static char message[1024];
64
XGetErrorText(display, error->error_code, message, sizeof(message));
65
print_verbose(vformat("XServer error: %s"
66
"\n Major opcode of failed request: %d"
67
"\n Serial number of failed request: %d"
68
"\n Current serial number in output stream: %d",
69
String::utf8(message), (uint64_t)error->request_code, (uint64_t)error->minor_code, (uint64_t)error->serial));
70
71
quick_exit(1);
72
return 0;
73
}
74
75
// Runs inside a child. Exiting will not quit the engine.
76
void DetectPrimeX11::create_context() {
77
XSetErrorHandler(&silent_error_handler);
78
79
Display *x11_display = XOpenDisplay(nullptr);
80
Window x11_window;
81
GLXContext glx_context;
82
83
static int visual_attribs[] = {
84
GLX_RENDER_TYPE, GLX_RGBA_BIT,
85
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
86
GLX_DOUBLEBUFFER, true,
87
GLX_RED_SIZE, 1,
88
GLX_GREEN_SIZE, 1,
89
GLX_BLUE_SIZE, 1,
90
GLX_DEPTH_SIZE, 24,
91
None
92
};
93
94
if (gladLoaderLoadGLX(x11_display, XScreenNumberOfScreen(XDefaultScreenOfDisplay(x11_display))) == 0) {
95
print_verbose("Unable to load GLX, GPU detection skipped.");
96
quick_exit(1);
97
}
98
int fbcount;
99
GLXFBConfig fbconfig = nullptr;
100
XVisualInfo *vi = nullptr;
101
102
XSetWindowAttributes swa;
103
swa.event_mask = StructureNotifyMask;
104
swa.border_pixel = 0;
105
unsigned long valuemask = CWBorderPixel | CWColormap | CWEventMask;
106
107
GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
108
if (!fbc) {
109
quick_exit(1);
110
}
111
112
vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
113
114
fbconfig = fbc[0];
115
116
static int context_attribs[] = {
117
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
118
GLX_CONTEXT_MINOR_VERSION_ARB, 3,
119
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
120
GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
121
None
122
};
123
124
glx_context = glXCreateContextAttribsARB(x11_display, fbconfig, nullptr, true, context_attribs);
125
126
swa.colormap = XCreateColormap(x11_display, RootWindow(x11_display, vi->screen), vi->visual, AllocNone);
127
x11_window = XCreateWindow(x11_display, RootWindow(x11_display, vi->screen), 0, 0, 10, 10, 0, vi->depth, InputOutput, vi->visual, valuemask, &swa);
128
129
if (!x11_window) {
130
quick_exit(1);
131
}
132
133
glXMakeCurrent(x11_display, x11_window, glx_context);
134
XFree(vi);
135
}
136
137
int DetectPrimeX11::detect_prime() {
138
pid_t p;
139
int priorities[2] = {};
140
String vendors[2];
141
String renderers[2];
142
143
vendors[0] = "Unknown";
144
vendors[1] = "Unknown";
145
renderers[0] = "Unknown";
146
renderers[1] = "Unknown";
147
148
for (int i = 0; i < 2; ++i) {
149
int fdset[2];
150
151
if (pipe(fdset) == -1) {
152
print_verbose("Failed to pipe(), using default GPU");
153
return 0;
154
}
155
156
// Fork so the driver initialization can crash without taking down the engine.
157
p = fork();
158
159
if (p > 0) {
160
// Main thread
161
162
int stat_loc = 0;
163
char string[201];
164
string[200] = '\0';
165
166
close(fdset[1]);
167
168
waitpid(p, &stat_loc, 0);
169
170
if (!stat_loc) {
171
// No need to do anything complicated here. Anything less than
172
// PIPE_BUF will be delivered in one read() call.
173
// Leave it 'Unknown' otherwise.
174
if (read(fdset[0], string, sizeof(string) - 1) > 0) {
175
vendors[i] = string;
176
renderers[i] = string + strlen(string) + 1;
177
}
178
}
179
180
close(fdset[0]);
181
182
} else {
183
// In child, exit() here will not quit the engine.
184
185
// Prevent false leak reports as we will not be properly
186
// cleaning up these processes, and fork() makes a copy
187
// of all globals.
188
CoreGlobals::leak_reporting_enabled = false;
189
190
char string[201];
191
192
close(fdset[0]);
193
194
if (i) {
195
setenv("DRI_PRIME", "1", 1);
196
}
197
198
create_context();
199
200
PFNGLGETSTRINGPROC glGetString = (PFNGLGETSTRINGPROC)glXGetProcAddressARB((GLubyte *)"glGetString");
201
if (!glGetString) {
202
print_verbose("Unable to get glGetString, GPU detection skipped.");
203
quick_exit(1);
204
}
205
206
const char *vendor = (const char *)glGetString(GL_VENDOR);
207
const char *renderer = (const char *)glGetString(GL_RENDERER);
208
209
unsigned int vendor_len = strlen(vendor) + 1;
210
unsigned int renderer_len = strlen(renderer) + 1;
211
212
if (vendor_len + renderer_len >= sizeof(string)) {
213
renderer_len = 200 - vendor_len;
214
}
215
216
memcpy(&string, vendor, vendor_len);
217
memcpy(&string[vendor_len], renderer, renderer_len);
218
219
if (write(fdset[1], string, vendor_len + renderer_len) == -1) {
220
print_verbose("Couldn't write vendor/renderer string.");
221
}
222
close(fdset[1]);
223
224
// The function quick_exit() is used because exit() will call destructors on static objects copied by fork().
225
// These objects will be freed anyway when the process finishes execution.
226
quick_exit(0);
227
}
228
}
229
230
int preferred = 0;
231
int priority = 0;
232
233
if (vendors[0] == vendors[1]) {
234
print_verbose("Only one GPU found, using default.");
235
return 0;
236
}
237
238
for (int i = 1; i >= 0; --i) {
239
const Vendor *v = vendor_map;
240
while (v->glxvendor) {
241
if (v->glxvendor == vendors[i]) {
242
priorities[i] = v->priority;
243
244
if (v->priority >= priority) {
245
priority = v->priority;
246
preferred = i;
247
}
248
}
249
++v;
250
}
251
}
252
253
print_verbose("Found renderers:");
254
for (int i = 0; i < 2; ++i) {
255
print_verbose("Renderer " + itos(i) + ": " + renderers[i] + " with priority: " + itos(priorities[i]));
256
}
257
258
print_verbose("Using renderer: " + renderers[preferred]);
259
return preferred;
260
}
261
262
#endif // X11_ENABLED && GLES3_ENABLED
263
264