forked from eclipse-efx/efxclipse-drift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DriftGL_wgl.cpp
456 lines (372 loc) · 14.3 KB
/
DriftGL_wgl.cpp
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#ifdef DRIFTGL_WGL
#include "DriftGL.h"
#include "DriftGL_wgl.h"
#include <iostream>
#include <mutex>
#include <map>
#pragma comment (lib, "opengl32.lib")
namespace driftgl {
int WGL_DRAW_TO_WINDOW_ARB = 0x2001;
int WGL_ACCELERATION_ARB = 0x2003;
int WGL_SUPPORT_OPENGL_ARB = 0x2010;
int WGL_DOUBLE_BUFFER_ARB = 0x2011;
int WGL_PIXEL_TYPE_ARB = 0x2013;
int WGL_COLOR_BITS_ARB = 0x2014;
int WGL_DEPTH_BITS_ARB = 0x2022;
int WGL_STENCIL_BITS_ARB = 0x2023;
int WGL_FULL_ACCELERATION_ARB = 0x2027;
int WGL_TYPE_RGBA_ARB = 0x202B;
int WGL_CONTEXT_MAJOR_VERSION_ARB = 0x2091;
int WGL_CONTEXT_MINOR_VERSION_ARB = 0x2092;
int WGL_CONTEXT_PROFILE_MASK_ARB = 0x9126;
int WGL_CONTEXT_FLAGS_ARB = 0x2094;
int WGL_CONTEXT_DEBUG_BIT_ARB = 0x00000001;
int WGL_CONTEXT_CORE_PROFILE_BIT_ARB = 0x00000001;
int WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB = 0x00000002;
std::mutex knownContextsMutex;
std::map<HGLRC, Context*> knownContexts;
void registerContext(HGLRC hglrc, Context* context) {
knownContextsMutex.lock();
knownContexts[hglrc] = context;
knownContextsMutex.unlock();
}
void unregisterContext(HGLRC hglrc) {
knownContextsMutex.lock();
knownContexts.erase(hglrc);
knownContextsMutex.unlock();
}
Context* getContext(HGLRC hglrc) {
knownContextsMutex.lock();
Context* context = knownContexts[hglrc];
knownContextsMutex.unlock();
return context;
}
typedef HGLRC(APIENTRY * PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC hDC, HGLRC hShareContext, const int *attribList);
typedef BOOL(APIENTRY * PFNWGLCHOOSEPIXELFORMATARBPROC)(HDC hDC, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int * piFormats, UINT *nNumFormats);
static PFNWGLCREATECONTEXTATTRIBSARBPROC pfnWglCreateContextAttribsARB = 0;
static PFNWGLCHOOSEPIXELFORMATARBPROC pfnWglChoosePixelFormatARB = 0;
HGLRC wglCreateContextAttribsARB(HDC hDC, HGLRC hShareContext, const int *attribList) {
return pfnWglCreateContextAttribsARB(hDC, hShareContext, attribList);
}
BOOL wglChoosePixelFormatARB(HDC hDC, const int *piAttribIList, const FLOAT *piAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats) {
return pfnWglChoosePixelFormatARB(hDC, piAttribIList, piAttribFList, nMaxFormats, piFormats, nNumFormats);
}
struct WGLContext : Context {
HWND hWND;
HDC hDC;
HGLRC hGLRC;
};
ATOM registerClass(HINSTANCE hInstance) {
WNDCLASSEX wcex;
ZeroMemory(&wcex, sizeof(wcex));
wcex.cbSize = sizeof(wcex);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcex.lpfnWndProc = DefWindowProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.lpszClassName = "DriftGLDummyWindow";
return RegisterClassEx(&wcex);
}
HWND CreateDriftGLWindow(HINSTANCE hInstance) {
return CreateWindow(
"DriftGLDummyWindow", "DriftGL Dummy Window", // window class, title
WS_CLIPSIBLINGS | WS_CLIPCHILDREN, // style
0, 0, // position x, y
1, 1, // width, height
NULL, NULL, // parent window, menu
hInstance, NULL); // instance, param
}
void SetupPixelFormat(HDC hDC) {
int pixel_format_attribs[] = {
WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
0
};
int pixel_format;
UINT num_formats;
wglChoosePixelFormatARB(hDC, pixel_format_attribs, 0, 1, &pixel_format, &num_formats);
PIXELFORMATDESCRIPTOR pfd;
DescribePixelFormat(hDC, pixel_format, sizeof(pfd), &pfd);
BOOL result = SetPixelFormat(hDC, pixel_format, &pfd);
std::cout << "SetPixelFormat: " << result << std::endl;
}
typedef HANDLE (* PFNWGLDXOPENDEVICENV) (void* dxDevice);
typedef bool (* PFNWGLDXCLOSEDEVICENV) (HANDLE hDevice);
typedef HANDLE (* PFNWGLDXREGISTEROBJECTNV) (HANDLE hDevice, void* dxObject, GLuint name, GLenum type, GLenum access);
typedef bool (* PFNWGLDXUNREGISTEROBJECTNV) (HANDLE hDevice, HANDLE hObject);
typedef bool (* PFNWGLDXOBJECTACCESSNV) (HANDLE hObject, GLenum access);
typedef bool (* PFNWGLDXLOCKOBJECTSNV) (HANDLE hDevice, GLint count, HANDLE* hObjects);
typedef bool (* PFNWGLDXUNLOCKOBJECTSNV) (HANDLE hDevice, GLint count, HANDLE* hObjects);
typedef bool (* PFNWGLDXSETRESOURCESHAREHANDLENV) (void* dxObject, HANDLE shareHandle);
PFNWGLDXOPENDEVICENV pfnWglDXOpenDeviceNV;
PFNWGLDXCLOSEDEVICENV pfnWglDXCloseDeviceNV;
PFNWGLDXREGISTEROBJECTNV pfnWglDXRegisterObjectNV;
PFNWGLDXUNREGISTEROBJECTNV pfnWglDXUnregisterObjectNV;
PFNWGLDXOBJECTACCESSNV pfnWglDXObjectAccessNV;
PFNWGLDXLOCKOBJECTSNV pfnWglDXLockObjectsNV;
PFNWGLDXUNLOCKOBJECTSNV pfnWglDXUnlockObjectsNV;
PFNWGLDXSETRESOURCESHAREHANDLENV pfnWglDXSetResourceShareHandleNV;
HANDLE wglDXOpenDeviceNV(void* dxDevice) {
return pfnWglDXOpenDeviceNV(dxDevice);
}
bool wglDXCloseDeviceNV(HANDLE hDevice) {
return pfnWglDXCloseDeviceNV(hDevice);
}
HANDLE wglDXRegisterObjectNV(HANDLE hDevice, void* dxObject, GLuint name, GLenum type, GLenum access) {
return pfnWglDXRegisterObjectNV(hDevice, dxObject, name, type, access);
}
bool wglDXUnregisterObjectNV(HANDLE hDevice, HANDLE hObject) {
return pfnWglDXUnregisterObjectNV(hDevice, hObject);
}
bool wglDXObjectAccess(HANDLE hObject, GLenum access) {
return pfnWglDXObjectAccessNV(hObject, access);
}
bool wglDXLockObjectsNV(HANDLE hDevice, GLint count, HANDLE* hObjects) {
return pfnWglDXLockObjectsNV(hDevice, count, hObjects);
}
bool wglDXUnlockObjectsNV(HANDLE hDevice, GLint count, HANDLE* hObjects) {
return pfnWglDXUnlockObjectsNV(hDevice, count, hObjects);
}
bool wglDXSetResourceShareHandleNV(void* dxObject, HANDLE shareHandle) {
return pfnWglDXSetResourceShareHandleNV(dxObject, shareHandle);
}
void doInitializeContextCreationPointers(HINSTANCE hInstance) {
ATOM cls = registerClass(hInstance);
HWND window = CreateDriftGLWindow(hInstance);
HDC hDC = GetDC(window);
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, // Flags
PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette.
32, // Colordepth of the framebuffer.
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
24, // Number of bits for the depthbuffer
8, // Number of bits for the stencilbuffer
0, // Number of Aux buffers in the framebuffer.
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int pf = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, pf, &pfd);
HGLRC dummyContext = wglCreateContext(hDC);
BOOL c = wglMakeCurrent(hDC, dummyContext);
if (!c) {
// big error
}
pfnWglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
pfnWglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
wglMakeCurrent(NULL, NULL);
wglDeleteContext(dummyContext);
ReleaseDC(window, hDC);
DestroyWindow(window);
}
void InitializeContextCreationPointers(HINSTANCE hInstance) {
if (pfnWglChoosePixelFormatARB == 0) {
// we do this on a different thread to prevent changes to the calling thread's current gl context
std::thread initPointers(doInitializeContextCreationPointers, hInstance);
initPointers.join();
}
}
void doInitializeGLPointers(HINSTANCE hInstance) {
HWND window = CreateDriftGLWindow(hInstance);
HDC hDC = GetDC(window);
std::cout << "SetupPixelForm from Initialize" << std::endl;
SetupPixelFormat(hDC);
int attribList[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
WGL_CONTEXT_MINOR_VERSION_ARB, 5,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
0,
};
HGLRC realContext = wglCreateContextAttribsARB(hDC, NULL, attribList);
wglMakeCurrent(hDC, realContext);
PROC getString = wglGetProcAddress("glGetString");
procs::Initialize([](const char* name) {
PROC proc = wglGetProcAddress(name);
//std::cout << " * " << name << ": " << proc << std::endl;
if (proc == 0) {
DWORD errWgl = GetLastError();
// fallback for old functions on m$
HMODULE hModuleGL = GetModuleHandle("opengl32");
proc = GetProcAddress(hModuleGL, name);
if (proc == 0) {
DWORD errWin = GetLastError();
std::cout << " ! Could not acquire " << name << " (Error: " << errWgl << " / " << errWin << ")" << std::endl;
}
}
return (void*)proc;
});
pfnWglDXOpenDeviceNV = (PFNWGLDXOPENDEVICENV)wglGetProcAddress("wglDXOpenDeviceNV");
pfnWglDXCloseDeviceNV = (PFNWGLDXCLOSEDEVICENV)wglGetProcAddress("wglDXCloseDeviceNV");
pfnWglDXRegisterObjectNV = (PFNWGLDXREGISTEROBJECTNV)wglGetProcAddress("wglDXRegisterObjectNV");
pfnWglDXUnregisterObjectNV = (PFNWGLDXUNREGISTEROBJECTNV)wglGetProcAddress("wglDXUnregisterObjectNV");
pfnWglDXObjectAccessNV = (PFNWGLDXOBJECTACCESSNV)wglGetProcAddress("wglDXObjectAccessNV");
pfnWglDXLockObjectsNV = (PFNWGLDXLOCKOBJECTSNV)wglGetProcAddress("wglDXLockObjectsNV");
pfnWglDXUnlockObjectsNV = (PFNWGLDXUNLOCKOBJECTSNV)wglGetProcAddress("wglDXUnlockObjectsNV");
pfnWglDXSetResourceShareHandleNV = (PFNWGLDXSETRESOURCESHAREHANDLENV)wglGetProcAddress("wglDXSetResourceShareHandleNV");
//std::cout << "Initialze OpenGL Version: " << glGetString(GL_VERSION) << std::endl;
wglMakeCurrent(NULL, NULL);
//wglDeleteContext(dummyContext);
wglDeleteContext(realContext);
ReleaseDC(window, hDC);
DestroyWindow(window);
}
void InitializeGLPointers(HINSTANCE hInstance) {
// we do this on a different thread to prevent changes to the calling thread's current gl context
std::thread initPointers(doInitializeGLPointers, hInstance);
initPointers.join();
}
bool Initialize() {
HINSTANCE hInstance = NULL;
InitializeContextCreationPointers(hInstance);
InitializeGLPointers(hInstance);
return true;
}
bool Destroy() {
return true;
}
Context* CreateContext(Context* sharedContext, int majorHint, int minorHint) {
//std::cout << "CreateContext" << std::endl << std::flush;
WGLContext* sharedCtx = (WGLContext*)sharedContext;
HGLRC shared = sharedCtx == nullptr ? nullptr : sharedCtx->hGLRC;
WGLContext* ctx = new WGLContext();
HINSTANCE hInstance = NULL;
ctx->hWND = CreateDriftGLWindow(hInstance);
//std::cout << " - hWND = " << ctx->hWND << std::endl;
if (ctx->hWND == NULL) {
// we failed to create the window
delete ctx;
return NULL;
}
ctx->hDC = GetDC(ctx->hWND);
if (ctx->hDC == NULL) {
// we failed to get the HDC
DestroyWindow(ctx->hWND);
delete ctx;
return NULL;
}
//std::cout << " - hDC = " << ctx->hDC << std::endl;
std::cout << "SetupPixelForm from CreateContext" << std::endl;
SetupPixelFormat(ctx->hDC);
// glfw context creation hints
//CONTEXT_CREATION_API NATIVE_CONTEXT_API NATIVE_CONTEXT_API EGL_CONTEXT_API OSMESA_CONTEXT_API
//CONTEXT_VERSION_MAJOR 1 Any valid major version number of the chosen client API
//CONTEXT_VERSION_MINOR 0 Any valid minor version number of the chosen client API
//CONTEXT_ROBUSTNESS NO_ROBUSTNESS NO_ROBUSTNESS NO_RESET_NOTIFICATION LOSE_CONTEXT_ON_RESET
//CONTEXT_RELEASE_BEHAVIOR ANY_RELEASE_BEHAVIOR ANY_RELEASE_BEHAVIOR RELEASE_BEHAVIOR_FLUSH RELEASE_BEHAVIOR_NONE
//CONTEXT_NO_ERROR FALSE TRUE or FALSE
//OPENGL_FORWARD_COMPAT FALSE TRUE or FALSE
//OPENGL_DEBUG_CONTEXT FALSE TRUE or FALSE
//OPENGL_PROFILE OPENGL_ANY_PROFILE OPENGL_ANY_PROFILE OPENGL_CORE_PROFILE OPENGL_COMPAT_PROFILE
int attribList[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, majorHint,
WGL_CONTEXT_MINOR_VERSION_ARB, minorHint,
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB,
// TODO compat vs core contexts
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
0,
};
ctx->hGLRC = wglCreateContextAttribsARB(ctx->hDC, shared, attribList);
//std::cout << " - hGLRC = " << ctx->hGLRC << std::endl << std::flush;
// MakeContextCurrent(ctx);
// std::cout << "OpenGL " << glGetString(GL_VERSION) << std::endl;
if (ctx->hGLRC == NULL) {
// we failed to create the context
ReleaseDC(ctx->hWND, ctx->hDC);
DestroyWindow(ctx->hWND);
delete ctx;
return NULL;
}
registerContext(ctx->hGLRC, ctx);
return ctx;
}
Context* WrapContext(long long nativeContextHandle) {
// TODO win32 wrapped context missing hdc
WGLContext* ctx = new WGLContext();
ctx->hGLRC = (HGLRC) nativeContextHandle;
//registerContext(ctx->hGLRC, ctx);
return ctx;
}
Context* CreateSharedCompatContext(Context* sharedContext) {
//std::cout << "CreateContext" << std::endl << std::flush;
WGLContext* sharedCtx = (WGLContext*)sharedContext;
HGLRC shared = sharedCtx == nullptr ? nullptr : sharedCtx->hGLRC;
WGLContext* ctx = new WGLContext();
HINSTANCE hInstance = NULL;
ctx->hWND = CreateDriftGLWindow(hInstance);
//std::cout << " - hWND = " << ctx->hWND << std::endl;
if (ctx->hWND == NULL) {
// we failed to create the window
delete ctx;
return NULL;
}
ctx->hDC = GetDC(ctx->hWND);
if (ctx->hDC == NULL) {
// we failed to get the HDC
DestroyWindow(ctx->hWND);
delete ctx;
return NULL;
}
//std::cout << " - hDC = " << ctx->hDC << std::endl;
std::cout << "SetupPixelForm from CreateSharedCompatContext" << std::endl;
SetupPixelFormat(ctx->hDC);
int context_attribs[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, 2,
WGL_CONTEXT_MINOR_VERSION_ARB, 1,
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
0
};
ctx->hGLRC = wglCreateContextAttribsARB(ctx->hDC, shared, context_attribs);
//std::cout << " - hGLRC = " << ctx->hGLRC << std::endl << std::flush;
// MakeContextCurrent(ctx);
// std::cout << "OpenGL " << glGetString(GL_VERSION) << std::endl;
if (ctx->hGLRC == NULL) {
// we failed to create the context
ReleaseDC(ctx->hWND, ctx->hDC);
DestroyWindow(ctx->hWND);
delete ctx;
return NULL;
}
registerContext(ctx->hGLRC, ctx);
return ctx;
}
void DestroyContext(Context* context) {
WGLContext* ctx = (WGLContext*)context;
unregisterContext(ctx->hGLRC);
wglDeleteContext(ctx->hGLRC);
ReleaseDC(GetDesktopWindow(), ctx->hDC);
DestroyWindow(ctx->hWND);
delete ctx;
}
bool MakeContextCurrent(Context* context) {
WGLContext* ctx = (WGLContext*)context;
if (ctx == nullptr) {
return wglMakeCurrent(nullptr, nullptr);
}
else {
return wglMakeCurrent(ctx->hDC, ctx->hGLRC);
}
}
bool IsContextCurrent(Context* context) {
HGLRC currentContext = wglGetCurrentContext();
WGLContext* ctx = (WGLContext*) context;
return currentContext == ctx->hGLRC;
}
void* GetNativeContextHandle(Context* context) {
WGLContext* ctx = (WGLContext*)context;
return ctx->hGLRC;
}
Context* GetCurrentContext() {
HGLRC currentContext = wglGetCurrentContext();
return getContext(currentContext);
}
}
#endif