File size: 13,013 Bytes
111ba01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright (c) 2020, NVIDIA CORPORATION.  All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto.  Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.

//------------------------------------------------------------------------
// Common.
//------------------------------------------------------------------------

#include "framework.h"
#include "glutil.h"
#include <iostream>
#include <iomanip>

// Create the function pointers.
#define GLUTIL_EXT(return_type, name, ...) return_type (GLAPIENTRY* name)(__VA_ARGS__) = 0;
#include "glutil_extlist.h"
#undef GLUTIL_EXT

// Track initialization status.
static volatile bool s_glExtInitialized = false;

// Error strings.
const char* getGLErrorString(GLenum err)
{
    switch(err)
    {
        case GL_NO_ERROR:                       return "GL_NO_ERROR";
        case GL_INVALID_ENUM:                   return "GL_INVALID_ENUM";
        case GL_INVALID_VALUE:                  return "GL_INVALID_VALUE";
        case GL_INVALID_OPERATION:              return "GL_INVALID_OPERATION";
        case GL_STACK_OVERFLOW:                 return "GL_STACK_OVERFLOW";
        case GL_STACK_UNDERFLOW:                return "GL_STACK_UNDERFLOW";
        case GL_OUT_OF_MEMORY:                  return "GL_OUT_OF_MEMORY";
        case GL_INVALID_FRAMEBUFFER_OPERATION:  return "GL_INVALID_FRAMEBUFFER_OPERATION";
        case GL_TABLE_TOO_LARGE:                return "GL_TABLE_TOO_LARGE";
        case GL_CONTEXT_LOST:                   return "GL_CONTEXT_LOST";
    }
    return "Unknown error";
}

//------------------------------------------------------------------------
// Windows.
//------------------------------------------------------------------------

#ifdef _WIN32

static CRITICAL_SECTION getInitializedCriticalSection(void)
{
    CRITICAL_SECTION cs;
    InitializeCriticalSection(&cs);
    return cs;
}

static CRITICAL_SECTION s_getProcAddressMutex = getInitializedCriticalSection();

static void safeGetProcAddress(const char* name, PROC* pfn)
{
    PROC result = wglGetProcAddress(name);
    if (!result)
    {
        LeaveCriticalSection(&s_getProcAddressMutex); // Prepare for thread exit.
        LOG(FATAL) << "wglGetProcAddress() failed for '" << name << "'";
        exit(1); // Should never get here but make sure we exit.
    }
    *pfn = result;
}

static void initializeGLExtensions(void)
{
    // Use critical section for thread safety.
    EnterCriticalSection(&s_getProcAddressMutex);

    // Only dig function pointers if not done already.
    if (!s_glExtInitialized)
    {
        // Generate code to populate the function pointers.
#define GLUTIL_EXT(return_type, name, ...) safeGetProcAddress(#name, (PROC*)&name);
#include "glutil_extlist.h"
#undef GLUTIL_EXT

        // Mark as initialized.
        s_glExtInitialized = true;
    }

    // Done.
    LeaveCriticalSection(&s_getProcAddressMutex);
    return;
}

void setGLContext(GLContext& glctx)
{
    if (!glctx.hglrc)
        LOG(FATAL) << "setGLContext() called with null gltcx";
    if (!wglMakeCurrent(glctx.hdc, glctx.hglrc))
        LOG(FATAL) << "wglMakeCurrent() failed when setting GL context";

    if (glctx.extInitialized)
        return;
    initializeGLExtensions();
    glctx.extInitialized = 1;
}

void releaseGLContext(void)
{
    if (!wglMakeCurrent(NULL, NULL))
        LOG(FATAL) << "wglMakeCurrent() failed when releasing GL context";
}

extern "C" int set_gpu(const char*); // In setgpu.lib
GLContext createGLContext(int cudaDeviceIdx)
{
    if (cudaDeviceIdx >= 0)
    {
        char pciBusId[256] = "";
        LOG(INFO) << "Creating GL context for Cuda device " << cudaDeviceIdx;
        if (cudaDeviceGetPCIBusId(pciBusId, 255, cudaDeviceIdx))
        {
            LOG(INFO) << "PCI bus id query failed";
        }
        else
        {
            int res = set_gpu(pciBusId);
            LOG(INFO) << "Selecting device with PCI bus id " << pciBusId << " - " << (res ? "failed, expect crash or major slowdown" : "success");
        }
    }

    HINSTANCE hInstance = GetModuleHandle(NULL);
    WNDCLASS wc = {};
    wc.style         = CS_OWNDC;
    wc.lpfnWndProc   = DefWindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = "__DummyGLClassCPP";
    int res = RegisterClass(&wc);

    HWND hwnd = CreateWindow(
        "__DummyGLClassCPP",        // lpClassName
        "__DummyGLWindowCPP",       // lpWindowName
        WS_OVERLAPPEDWINDOW,        // dwStyle
        CW_USEDEFAULT,              // x
        CW_USEDEFAULT,              // y
        0, 0,                       // nWidth, nHeight
        NULL, NULL,                 // hWndParent, hMenu
        hInstance,                  // hInstance
        NULL                        // lpParam
    );

    PIXELFORMATDESCRIPTOR pfd = {};
    pfd.dwFlags      = PFD_SUPPORT_OPENGL;
    pfd.iPixelType   = PFD_TYPE_RGBA;
    pfd.iLayerType   = PFD_MAIN_PLANE;
    pfd.cColorBits   = 32;
    pfd.cDepthBits   = 24;
    pfd.cStencilBits = 8;

    HDC hdc = GetDC(hwnd);
    int pixelformat = ChoosePixelFormat(hdc, &pfd);
    SetPixelFormat(hdc, pixelformat, &pfd);

    HGLRC hglrc = wglCreateContext(hdc);
    LOG(INFO) << std::hex << std::setfill('0')
              << "WGL OpenGL context created (hdc: 0x" << std::setw(8) << (uint32_t)(uintptr_t)hdc
              << ", hglrc: 0x" << std::setw(8) << (uint32_t)(uintptr_t)hglrc << ")";

    GLContext glctx = {hdc, hglrc, 0};
    return glctx;
}

void destroyGLContext(GLContext& glctx)
{
    if (!glctx.hglrc)
        LOG(FATAL) << "destroyGLContext() called with null gltcx";

    // If this is the current context, release it.
    if (wglGetCurrentContext() == glctx.hglrc)
        releaseGLContext();

    HWND hwnd = WindowFromDC(glctx.hdc);
    if (!hwnd)
        LOG(FATAL) << "WindowFromDC() failed";
    if (!ReleaseDC(hwnd, glctx.hdc))
        LOG(FATAL) << "ReleaseDC() failed";
    if (!wglDeleteContext(glctx.hglrc))
        LOG(FATAL) << "wglDeleteContext() failed";
    if (!DestroyWindow(hwnd))
        LOG(FATAL) << "DestroyWindow() failed";

    LOG(INFO) << std::hex << std::setfill('0')
              << "WGL OpenGL context destroyed (hdc: 0x" << std::setw(8) << (uint32_t)(uintptr_t)glctx.hdc
              << ", hglrc: 0x" << std::setw(8) << (uint32_t)(uintptr_t)glctx.hglrc << ")";

    memset(&glctx, 0, sizeof(GLContext));
}

#endif // _WIN32

//------------------------------------------------------------------------
// Linux.
//------------------------------------------------------------------------

#ifdef __linux__

static pthread_mutex_t s_getProcAddressMutex;

typedef void (*PROCFN)();

static void safeGetProcAddress(const char* name, PROCFN* pfn)
{
    PROCFN result = eglGetProcAddress(name);
    if (!result)
    {
        pthread_mutex_unlock(&s_getProcAddressMutex); // Prepare for thread exit.
        LOG(FATAL) << "wglGetProcAddress() failed for '" << name << "'";
        exit(1); // Should never get here but make sure we exit.
    }
    *pfn = result;
}

static void initializeGLExtensions(void)
{
    pthread_mutex_lock(&s_getProcAddressMutex);

    // Only dig function pointers if not done already.
    if (!s_glExtInitialized)
    {
        // Generate code to populate the function pointers.
#define GLUTIL_EXT(return_type, name, ...) safeGetProcAddress(#name, (PROCFN*)&name);
#include "glutil_extlist.h"
#undef GLUTIL_EXT

        // Mark as initialized.
        s_glExtInitialized = true;
    }

    pthread_mutex_unlock(&s_getProcAddressMutex);
    return;
}

void setGLContext(GLContext& glctx)
{
    if (!glctx.context)
        LOG(FATAL) << "setGLContext() called with null gltcx";

    if (!eglMakeCurrent(glctx.display, EGL_NO_SURFACE, EGL_NO_SURFACE, glctx.context))
        LOG(ERROR) << "eglMakeCurrent() failed when setting GL context";

    if (glctx.extInitialized)
        return;
    initializeGLExtensions();
    glctx.extInitialized = 1;
}

void releaseGLContext(void)
{
    EGLDisplay display = eglGetCurrentDisplay();
    if (display == EGL_NO_DISPLAY)
        LOG(WARNING) << "releaseGLContext() called with no active display";
    if (!eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
        LOG(FATAL) << "eglMakeCurrent() failed when releasing GL context";
}

static EGLDisplay getCudaDisplay(int cudaDeviceIdx)
{
    typedef EGLBoolean (*eglQueryDevicesEXT_t)(EGLint, EGLDeviceEXT, EGLint*);
    typedef EGLBoolean (*eglQueryDeviceAttribEXT_t)(EGLDeviceEXT, EGLint, EGLAttrib*);
    typedef EGLDisplay (*eglGetPlatformDisplayEXT_t)(EGLenum, void*, const EGLint*);

    eglQueryDevicesEXT_t eglQueryDevicesEXT = (eglQueryDevicesEXT_t)eglGetProcAddress("eglQueryDevicesEXT");
    if (!eglQueryDevicesEXT)
    {
        LOG(INFO) << "eglGetProcAddress(\"eglQueryDevicesEXT\") failed";
        return 0;
    }

    eglQueryDeviceAttribEXT_t eglQueryDeviceAttribEXT = (eglQueryDeviceAttribEXT_t)eglGetProcAddress("eglQueryDeviceAttribEXT");
    if (!eglQueryDeviceAttribEXT)
    {
        LOG(INFO) << "eglGetProcAddress(\"eglQueryDeviceAttribEXT\") failed";
        return 0;
    }

    eglGetPlatformDisplayEXT_t eglGetPlatformDisplayEXT = (eglGetPlatformDisplayEXT_t)eglGetProcAddress("eglGetPlatformDisplayEXT");
    if (!eglGetPlatformDisplayEXT)
    {
        LOG(INFO) << "eglGetProcAddress(\"eglGetPlatformDisplayEXT\") failed";
        return 0;
    }

    int num_devices = 0;
    eglQueryDevicesEXT(0, 0, &num_devices);
    if (!num_devices)
        return 0;

    EGLDisplay display = 0;
    EGLDeviceEXT* devices = (EGLDeviceEXT*)malloc(num_devices * sizeof(void*));
    eglQueryDevicesEXT(num_devices, devices, &num_devices);
    for (int i=0; i < num_devices; i++)
    {
        EGLDeviceEXT device = devices[i];
        intptr_t value = -1;
        if (eglQueryDeviceAttribEXT(device, EGL_CUDA_DEVICE_NV, &value) && value == cudaDeviceIdx)
        {
            display = eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, device, 0);
            break;
        }
    }

    free(devices);
    return display;
}

GLContext createGLContext(int cudaDeviceIdx)
{
    EGLDisplay display = 0;

    if (cudaDeviceIdx >= 0)
    {
        char pciBusId[256] = "";
        LOG(INFO) << "Creating GL context for Cuda device " << cudaDeviceIdx;
        display = getCudaDisplay(cudaDeviceIdx);
        if (!display)
            LOG(INFO) << "Failed, falling back to default display";
    }

    if (!display)
    {
        display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
        if (display == EGL_NO_DISPLAY)
            LOG(FATAL) << "eglGetDisplay() failed";
    }

    EGLint major;
    EGLint minor;
    if (!eglInitialize(display, &major, &minor))
        LOG(FATAL) << "eglInitialize() failed";

    // Choose configuration.

    const EGLint context_attribs[] = {
        EGL_RED_SIZE,           8,
        EGL_GREEN_SIZE,         8,
        EGL_BLUE_SIZE,          8,
        EGL_ALPHA_SIZE,         8,
        EGL_DEPTH_SIZE,         24,
        EGL_STENCIL_SIZE,       8,
        EGL_RENDERABLE_TYPE,    EGL_OPENGL_BIT,
        EGL_SURFACE_TYPE,       EGL_PBUFFER_BIT,
        EGL_NONE
    };

    EGLConfig config;
    EGLint num_config;
    if (!eglChooseConfig(display, context_attribs, &config, 1, &num_config))
        LOG(FATAL) << "eglChooseConfig() failed";

    // Create GL context.

    if (!eglBindAPI(EGL_OPENGL_API))
        LOG(FATAL) << "eglBindAPI() failed";

    EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
    if (context == EGL_NO_CONTEXT)
        LOG(FATAL) << "eglCreateContext() failed";

    // Done.

    LOG(INFO) << "EGL " << (int)minor << "." << (int)major << " OpenGL context created (disp: 0x"
              << std::hex << std::setfill('0')
              << std::setw(16) << (uintptr_t)display
              << ", ctx: 0x" << std::setw(16) << (uintptr_t)context << ")";

    GLContext glctx = {display, context, 0};
    return glctx;
}

void destroyGLContext(GLContext& glctx)
{
    if (!glctx.context)
        LOG(FATAL) << "destroyGLContext() called with null gltcx";

    // If this is the current context, release it.
    if (eglGetCurrentContext() == glctx.context)
        releaseGLContext();

    if (!eglDestroyContext(glctx.display, glctx.context))
        LOG(ERROR) << "eglDestroyContext() failed";

    LOG(INFO) << "EGL OpenGL context destroyed (disp: 0x"
              << std::hex << std::setfill('0')
              << std::setw(16) << (uintptr_t)glctx.display
              << ", ctx: 0x" << std::setw(16) << (uintptr_t)glctx.context << ")";

    memset(&glctx, 0, sizeof(GLContext));
}

//------------------------------------------------------------------------

#endif // __linux__

//------------------------------------------------------------------------