-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathWindowlessContext.cpp
More file actions
288 lines (221 loc) · 7.98 KB
/
WindowlessContext.cpp
File metadata and controls
288 lines (221 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright (c) Facebook, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#include "WindowlessContext.h"
#include <Corrade/configure.h>
#if defined(CORRADE_TARGET_APPLE)
#include <Magnum/Platform/WindowlessCglApplication.h>
#elif defined(CORRADE_TARGET_EMSCRIPTEN)
#include <Magnum/Platform/WindowlessEglApplication.h>
#elif defined(CORRADE_TARGET_UNIX)
#include <Magnum/Platform/GLContext.h>
#ifdef ESP_BUILD_EGL_SUPPORT
#include <glad/glad_egl.h>
#else
#include <Magnum/Platform/WindowlessGlxApplication.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#elif defined(CORRADE_TARGET_WINDOWS)
#include <Magnum/Platform/WindowlessWglApplication.h>
#endif
#include <Magnum/Platform/GLContext.h>
namespace Mn = Magnum;
// Based on code from:
// https://devblogs.nvidia.com/parallelforall/egl-eye-opengl-visualization-without-x-server/
// https://github.com/facebookresearch/House3D/blob/master/renderer/gl/glContext.cc
namespace esp {
namespace gfx {
#if defined(CORRADE_TARGET_UNIX) && !defined(CORRADE_TARGET_APPLE)
namespace {
struct ESPContext {
virtual void makeCurrent() = 0;
virtual bool isValid() = 0;
virtual int gpuDevice() const = 0;
virtual ~ESPContext(){};
ESP_SMART_POINTERS(ESPContext);
};
#ifdef ESP_BUILD_EGL_SUPPORT
const int MAX_DEVICES = 128;
#define CHECK_EGL_ERROR() \
do { \
EGLint err = eglGetError(); \
CHECK(err == EGL_SUCCESS) << "EGL error:" << err; \
} while (0)
bool isNvidiaGpuReadable(int device) {
const std::string dev = "/dev/nvidia" + std::to_string(device);
const int retval = open(dev.c_str(), O_RDONLY);
if (retval == -1) {
return false;
}
close(retval);
return true;
}
struct ESPEGLContext : ESPContext {
explicit ESPEGLContext(int device)
: magnumGlContext_{Mn::NoCreate}, gpuDevice_{device} {
CHECK(gladLoadEGL()) << "Failed to load EGL";
static const EGLint configAttribs[] = {EGL_SURFACE_TYPE,
EGL_PBUFFER_BIT,
EGL_BLUE_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_RED_SIZE,
8,
EGL_DEPTH_SIZE,
24,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_BIT,
EGL_NONE};
// 1. Initialize EGL
{
EGLDeviceEXT eglDevices[MAX_DEVICES];
EGLint numDevices;
eglQueryDevicesEXT(MAX_DEVICES, eglDevices, &numDevices);
CHECK_EGL_ERROR();
CHECK(numDevices > 0) << "[EGL] No devices detected";
LOG(INFO) << "[EGL] Detected " << numDevices << " EGL devices";
int eglDevId;
for (eglDevId = 0; eglDevId < numDevices; ++eglDevId) {
EGLAttrib cudaDevNumber;
if (eglQueryDeviceAttribEXT(eglDevices[eglDevId], EGL_CUDA_DEVICE_NV,
&cudaDevNumber) == EGL_FALSE)
continue;
if (cudaDevNumber == device)
break;
}
CHECK(eglDevId < numDevices)
<< "[EGL] Could not find an EGL device for CUDA device " << device;
// CHECK(isNvidiaGpuReadable(eglDevId))
// << "[EGL] EGL device " << eglDevId << ", CUDA device " << device
// << " is not readable";
LOG(INFO) << "[EGL] Selected EGL device " << eglDevId
<< " for CUDA device " << device;
display_ = eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT,
eglDevices[eglDevId], 0);
CHECK_EGL_ERROR();
}
EGLint major, minor;
EGLBoolean retval = eglInitialize(display_, &major, &minor);
if (!retval) {
LOG(ERROR) << "[EGL] Failed to initialize.";
}
CHECK_EGL_ERROR();
LOG(INFO) << "[EGL] Version: " << eglQueryString(display_, EGL_VERSION);
LOG(INFO) << "[EGL] Vendor: " << eglQueryString(display_, EGL_VENDOR);
// 2. Select an appropriate configuration
EGLint numConfigs;
EGLConfig eglConfig;
eglChooseConfig(display_, configAttribs, &eglConfig, 1, &numConfigs);
if (numConfigs != 1) {
LOG(ERROR)
<< "[EGL] Cannot create EGL config. Your driver may not support EGL.";
}
CHECK_EGL_ERROR();
// 3. Bind the API
retval = eglBindAPI(EGL_OPENGL_API);
if (!retval) {
LOG(ERROR) << "[EGL] failed to bind OpenGL API";
}
CHECK_EGL_ERROR();
// 4. Create a context
context_ = eglCreateContext(display_, eglConfig, EGL_NO_CONTEXT, NULL);
CHECK_EGL_ERROR();
// 5. Make context current and create Magnum context
makeCurrent();
CHECK(magnumGlContext_.tryCreate())
<< "[EGL] Failed to create OpenGL context";
isValid_ = true;
};
void makeCurrent() {
EGLBoolean retval =
eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, context_);
if (!retval) {
LOG(ERROR) << "[EGL] Failed to make EGL context current";
}
CHECK_EGL_ERROR();
};
bool isValid() { return isValid_; };
int gpuDevice() const { return gpuDevice_; }
~ESPEGLContext() {
eglDestroyContext(display_, context_);
eglTerminate(display_);
}
private:
EGLDisplay display_;
EGLContext context_;
Mn::Platform::GLContext magnumGlContext_;
bool isValid_ = false;
int gpuDevice_;
ESP_SMART_POINTERS(ESPEGLContext);
};
#else // ESP_BUILD_EGL_SUPPORT not defined
struct ESPGLXContext : ESPContext {
ESPGLXContext()
: glxCtx_{Mn::Platform::WindowlessGlxContext::Configuration()},
magnumGlContext_{Mn::NoCreate} {
CHECK(glxCtx_.isCreated())
<< "[GLX] Failed to created headless glX context";
makeCurrent();
CHECK(magnumGlContext_.tryCreate())
<< "[GLX] Failed to create OpenGL Context";
isValid_ = true;
};
void makeCurrent() { glxCtx_.makeCurrent(); };
bool isValid() { return isValid_; };
int gpuDevice() const { return 0; }
private:
Mn::Platform::WindowlessGlxContext glxCtx_;
Mn::Platform::GLContext magnumGlContext_;
bool isValid_ = false;
ESP_SMART_POINTERS(ESPGLXContext);
};
#endif
}; // namespace
struct WindowlessContext::Impl {
explicit Impl(int device) {
#ifdef ESP_BUILD_EGL_SUPPORT
glContext_ = ESPEGLContext::create_unique(device);
#else
CHECK_EQ(device, 0)
<< "glX context does not support multiple GPUs. Please compile with "
"BUILD_GUI_VIEWERS=0 for multi-gpu support via EGL";
CHECK(std::getenv("DISPLAY") != nullptr)
<< "DISPLAY not detected. For headless systems, compile with "
"--headless for EGL support";
glContext_ = ESPGLXContext::create_unique();
#endif
makeCurrent();
}
~Impl() { LOG(INFO) << "Deconstructing GL context"; }
void makeCurrent() { glContext_->makeCurrent(); }
int gpuDevice() const { return glContext_->gpuDevice(); }
ESPContext::uptr glContext_ = nullptr;
};
#else // not defined(CORRADE_TARGET_UNIX) && !defined(CORRADE_TARGET_APPLE)
struct WindowlessContext::Impl {
explicit Impl(int) : glContext_({}), magnumGlContext_(Mn::NoCreate) {
glContext_.makeCurrent();
if (!magnumGlContext_.tryCreate()) {
LOG(ERROR) << "Failed to create GL context";
}
}
~Impl() { LOG(INFO) << "Deconstructing GL context"; }
void makeCurrent() { glContext_.makeCurrent(); }
int gpuDevice() const { return 0; }
Mn::Platform::WindowlessGLContext glContext_;
Mn::Platform::GLContext magnumGlContext_;
};
#endif
WindowlessContext::WindowlessContext(int device /* = 0 */)
: pimpl_(spimpl::make_unique_impl<Impl>(device)) {}
void WindowlessContext::makeCurrent() {
pimpl_->makeCurrent();
}
int WindowlessContext::gpuDevice() const {
return pimpl_->gpuDevice();
}
} // namespace gfx
} // namespace esp