-
Notifications
You must be signed in to change notification settings - Fork 5
/
xxGraphicWGL.cpp
404 lines (357 loc) · 13.1 KB
/
xxGraphicWGL.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
//==============================================================================
// xxGraphic : OpenGL WGL Source
//
// Copyright (c) 2019-2024 TAiGA
// https://github.com/metarutaiga/xxGraphic
//==============================================================================
#include "internal/xxGraphicInternalGL.h"
#define _GDI32_
#include "gl/wgl.h"
#include "dxsdk/d3d11.h"
#include "dxsdk/d3d10.h"
#include "dxsdk/d3d9.h"
#include "xxGraphicWGL.h"
static void* g_gdiLibrary = nullptr;
static void* g_glLibrary = nullptr;
static HWND g_dummyWindow = nullptr;
static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT;
static PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
static PFNWGLDXOPENDEVICENVPROC wglDXOpenDeviceNV;
static PFNWGLDXCLOSEDEVICENVPROC wglDXCloseDeviceNV;
static PFNWGLDXREGISTEROBJECTNVPROC wglDXRegisterObjectNV;
static PFNWGLDXUNREGISTEROBJECTNVPROC wglDXUnregisterObjectNV;
static PFNWGLDXLOCKOBJECTSNVPROC wglDXLockObjectsNV;
static PFNWGLDXUNLOCKOBJECTSNVPROC wglDXUnlockObjectsNV;
//==============================================================================
// Initialize - WGL
//==============================================================================
extern PFNGLSHADERSOURCEPROC glShaderSourceEntry;
static PFNGLSHADERSOURCEPROC glShaderSourceInternal;
//------------------------------------------------------------------------------
static void GL_APIENTRY wglShaderSourceLegacy(GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length)
{
GLchar const** replaceString = xxAlloc(GLchar const*, count);
for (GLsizei i = 0; i < count; ++i)
{
const GLchar* val = (i >= 2) ? string[i - 2] : nullptr;
const GLchar* var = string[i];
if (strcmp(var, "#version 100") == 0)
var = "#version 120";
if (strncmp(var, "precision", sizeof("precision") - 1) == 0)
var = "";
if (val)
{
if (strcmp(val, "uniform") == 0 && strcmp(var, "uniform highp") == 0)
var = "uniform";
}
replaceString[i] = var;
}
glShaderSourceInternal(shader, count, replaceString, length);
xxFree(replaceString);
}
//------------------------------------------------------------------------------
static void GL_APIENTRY wglShaderSource(GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length)
{
GLchar const** replaceString = xxAlloc(GLchar const*, count);
bool fragmentShader = false;
for (GLsizei i = 0; i < count; ++i)
{
const GLchar* val = (i >= 2) ? string[i - 2] : nullptr;
const GLchar* var = string[i];
if (strcmp(var, "#version 100") == 0)
var = "#version 140";
if (strncmp(var, "precision", sizeof("precision") - 1) == 0)
var = "";
if (val)
{
if (strcmp(val, "SHADER_FRAGMENT") == 0 && strcmp(var, "1") == 0)
{
var = "1\n"
"#ifndef gl_FragColor\n"
"#define gl_FragColor fragColor\n"
"out vec4 fragColor;\n"
"#endif";
fragmentShader = true;
}
if (strcmp(val, "uniform") == 0 && strcmp(var, "uniform highp") == 0)
var = "uniform";
if (strcmp(val, "attribute") == 0 && strcmp(var, "attribute") == 0)
var = "in";
if (strcmp(val, "varying") == 0 && strcmp(var, "varying") == 0)
var = fragmentShader ? "in" : "out";
}
replaceString[i] = var;
}
glShaderSourceInternal(shader, count, replaceString, length);
xxFree(replaceString);
}
//------------------------------------------------------------------------------
static bool wglSymbolFailed = false;
static void* GL_APIENTRY wglSymbolImpl(char const* name, bool* failed = nullptr)
{
void* ptr = nullptr;
if (ptr == nullptr)
ptr = wglGetProcAddress(name);
if (ptr == nullptr && g_glLibrary)
ptr = xxGetProcAddress(g_glLibrary, name);
if (ptr == nullptr && g_gdiLibrary)
ptr = xxGetProcAddress(g_gdiLibrary, name);
if (ptr == nullptr)
{
static bool internal;
if (failed == &internal)
return nullptr;
ptr = getSymbolExtension(wglSymbolImpl, name, &internal);
}
if (ptr == nullptr)
{
bool failed = wglSymbolFailed;
if (strcmp(name, "glClearDepthf") == 0)
ptr = wglSymbolImpl("glClearDepth");
if (strcmp(name, "glDepthRangef") == 0)
ptr = wglSymbolImpl("glDepthRange");
wglSymbolFailed = failed;
}
if (ptr == nullptr)
xxLog("WGL", "%s is not found", name);
wglSymbolFailed |= (ptr == nullptr);
if (failed)
(*failed) |= (ptr == nullptr);
return ptr;
}
#define wglSymbol(var) (void*&)var = wglSymbolImpl(#var);
//------------------------------------------------------------------------------
uint64_t glCreateContextWGL(uint64_t instance, void* view, void** display)
{
HGLRC hInstance = reinterpret_cast<HGLRC>(instance);
HWND hWnd = reinterpret_cast<HWND>(view);
HDC hDC = GetDC(hWnd);
PIXELFORMATDESCRIPTOR desc = {};
desc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
desc.nVersion = 1;
desc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
desc.iPixelType = PFD_TYPE_RGBA;
desc.cColorBits = 32;
desc.cDepthBits = 24;
desc.cStencilBits = 8;
desc.iLayerType = PFD_MAIN_PLANE;
int pixelFormat = ChoosePixelFormat(hDC, &desc);
SetPixelFormat(hDC, pixelFormat, &desc);
HGLRC hGLRC;
if (wglCreateContextAttribsARB)
{
int attribs[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
WGL_CONTEXT_MINOR_VERSION_ARB, 2,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
0,
};
hGLRC = wglCreateContextAttribsARB(hDC, hInstance, attribs);
if (hGLRC == nullptr)
{
ReleaseDC(hWnd, hDC);
return 0;
}
}
else
{
hGLRC = wglCreateContext(hDC);
if (hGLRC == nullptr)
{
ReleaseDC(hWnd, hDC);
return 0;
}
if (hInstance)
{
wglShareLists(hInstance, hGLRC);
}
}
wglMakeCurrent(hDC, hGLRC);
if (wglSwapIntervalEXT == nullptr)
(void*&)wglSwapIntervalEXT = wglGetProcAddress("wglSwapIntervalEXT");
if (wglSwapIntervalEXT)
wglSwapIntervalEXT(0);
GLuint vao = 0;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
if (display)
{
(*display) = hDC;
}
else
{
ReleaseDC(hWnd, hDC);
}
return reinterpret_cast<uint64_t>(hGLRC);
}
//------------------------------------------------------------------------------
void glDestroyContextWGL(uint64_t context, void* view, void* display)
{
HGLRC hGLRC = reinterpret_cast<HGLRC>(context);
if (hGLRC == nullptr)
return;
HWND hWnd = reinterpret_cast<HWND>(view);
HDC hDC = reinterpret_cast<HDC>(display);
if (hDC == nullptr)
hDC = GetDC(hWnd);
wglMakeCurrent(hDC, hGLRC);
GLuint vao = 0;
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint*)&vao);
glDeleteVertexArrays(1, &vao);
glBindVertexArray(0);
wglMakeCurrent(nullptr, nullptr);
wglDeleteContext(hGLRC);
ReleaseDC(hWnd, hDC);
}
//------------------------------------------------------------------------------
void* glGetProcAddressWGL(char const* name)
{
return wglSymbolImpl(name);
}
//------------------------------------------------------------------------------
float glGetScaleContextWGL(uint64_t context, void* view)
{
return 1.0f;
}
//------------------------------------------------------------------------------
void glMakeCurrentContextWGL(uint64_t context, void* display)
{
HGLRC hGLRC = reinterpret_cast<HGLRC>(context);
HDC hDC = reinterpret_cast<HDC>(display);
wglMakeCurrent(hDC, hGLRC);
}
//------------------------------------------------------------------------------
void glPresentContextWGL(uint64_t context, void* display)
{
HGLRC hGLRC = reinterpret_cast<HGLRC>(context);
HDC hDC = reinterpret_cast<HDC>(display);
wglMakeCurrent(hDC, hGLRC);
SwapBuffers(hDC);
}
//------------------------------------------------------------------------------
uint64_t xxGraphicCreateWGL(int version)
{
if (g_gdiLibrary == nullptr)
g_gdiLibrary = xxLoadLibrary("gdi32.dll");
if (g_gdiLibrary == nullptr)
return 0;
if (g_glLibrary == nullptr)
g_glLibrary = xxLoadLibrary("opengl32.dll");
if (g_glLibrary == nullptr)
return 0;
WNDCLASSEXA wc = { sizeof(WNDCLASSEXA), CS_OWNDC, ::DefWindowProcA, 0, 0, ::GetModuleHandleA(nullptr), nullptr, nullptr, nullptr, nullptr, "xxGraphic", nullptr };
::RegisterClassExA(&wc);
g_dummyWindow = ::CreateWindowA(wc.lpszClassName, "xxGraphic", WS_OVERLAPPEDWINDOW, 0, 0, 1, 1, nullptr, nullptr, wc.hInstance, nullptr);
if (g_dummyWindow == nullptr)
return 0;
glCreateContext = glCreateContextWGL;
glDestroyContext = glDestroyContextWGL;
glGetProcAddress = glGetProcAddressWGL;
glGetScaleContext = glGetScaleContextWGL;
glMakeCurrentContext = glMakeCurrentContextWGL;
glPresentContext = glPresentContextWGL;
uint64_t context = glCreateContextWGL(0, g_dummyWindow, nullptr);
if (context == 0)
return 0;
wglSymbolFailed = false;
wglSymbol(wglCreateContextAttribsARB);
if (wglSymbolFailed == false)
{
glDestroyContextWGL(context, g_dummyWindow, nullptr);
context = glCreateContextWGL(0, g_dummyWindow, nullptr);
}
if (context == 0)
{
wglCreateContextAttribsARB = nullptr;
context = glCreateContextWGL(0, g_dummyWindow, nullptr);
}
wglSymbol(wglDXOpenDeviceNV);
wglSymbol(wglDXCloseDeviceNV);
wglSymbol(wglDXRegisterObjectNV);
wglSymbol(wglDXUnregisterObjectNV);
wglSymbol(wglDXLockObjectsNV);
wglSymbol(wglDXUnlockObjectsNV);
(void*&)glShaderSourceInternal = glGetProcAddress("glShaderSource");
glShaderSourceEntry = version <= 200 ? wglShaderSourceLegacy : wglShaderSource;
return context;
}
//------------------------------------------------------------------------------
void xxGraphicDestroyWGL(uint64_t context)
{
glDestroyContextWGL(context, g_dummyWindow, nullptr);
if (g_dummyWindow)
{
::DestroyWindow(g_dummyWindow);
::UnregisterClassA("xxGraphic", ::GetModuleHandleA(nullptr));
g_dummyWindow = nullptr;
}
if (g_glLibrary)
{
xxFreeLibrary(g_glLibrary);
g_glLibrary = nullptr;
}
if (g_gdiLibrary)
{
xxFreeLibrary(g_gdiLibrary);
g_gdiLibrary = nullptr;
}
}
//==============================================================================
// Extension
//==============================================================================
void const* xxGetDeviceFromDirect3DTexture(void const* texture)
{
if (wglDXOpenDeviceNV == nullptr)
return nullptr;
IUnknown* unknown = (IUnknown*)texture;
if (unknown == nullptr)
return nullptr;
ID3D11Texture2D* d3d11Texture = nullptr;
ID3D10Texture2D* d3d10Texture = nullptr;
IDirect3DTexture9* d3d9Texture = nullptr;
if (SUCCEEDED(unknown->QueryInterface(IID_PPV_ARGS(&d3d11Texture))))
{
ID3D11Device* d3d11Device = nullptr;
d3d11Texture->GetDevice(&d3d11Device);
d3d11Texture->Release();
if (d3d11Device)
d3d11Device->Release();
return wglDXOpenDeviceNV(d3d11Device);
}
else if (SUCCEEDED(unknown->QueryInterface(IID_PPV_ARGS(&d3d10Texture))))
{
ID3D10Device* d3d10Device = nullptr;
d3d10Texture->GetDevice(&d3d10Device);
d3d10Texture->Release();
if (d3d10Device)
d3d10Device->Release();
return wglDXOpenDeviceNV(d3d10Device);
}
else if (SUCCEEDED(unknown->QueryInterface(IID_PPV_ARGS(&d3d9Texture))))
{
IDirect3DDevice9* d3d9Device = nullptr;
d3d9Texture->GetDevice(&d3d9Device);
d3d9Texture->Release();
if (d3d9Device)
d3d9Device->Release();
return wglDXOpenDeviceNV(d3d9Device);
}
return nullptr;
}
//------------------------------------------------------------------------------
void const* xxCreateImageFromDirect3DTexture(void const* device, void const* texture, int id)
{
if (wglDXRegisterObjectNV == nullptr)
return nullptr;
return wglDXRegisterObjectNV((HANDLE)device, (void*)texture, id, GL_TEXTURE_2D, WGL_ACCESS_READ_ONLY_NV);
}
//------------------------------------------------------------------------------
void xxDestroyImage(void const* device, void const* image)
{
if (wglDXUnregisterObjectNV == nullptr || wglDXCloseDeviceNV == nullptr)
return;
wglDXUnregisterObjectNV((HANDLE)device, (HANDLE)image);
wglDXCloseDeviceNV((HANDLE)device);
}
//==============================================================================