Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/windows/gl_manager_windows_native.cpp
10277 views
1
/**************************************************************************/
2
/* gl_manager_windows_native.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
#include "gl_manager_windows_native.h"
32
33
#if defined(WINDOWS_ENABLED) && defined(GLES3_ENABLED)
34
35
#include "core/config/project_settings.h"
36
#include "core/version.h"
37
38
#include "thirdparty/misc/nvapi_minimal.h"
39
40
#include <dwmapi.h>
41
#include <cstdio>
42
#include <cstdlib>
43
44
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
45
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
46
#define WGL_CONTEXT_FLAGS_ARB 0x2094
47
#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002
48
#define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
49
#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
50
51
#define _WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
52
53
typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXT)(HDC);
54
typedef BOOL(APIENTRY *PFNWGLDELETECONTEXT)(HGLRC);
55
typedef BOOL(APIENTRY *PFNWGLMAKECURRENT)(HDC, HGLRC);
56
typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC, const int *);
57
typedef void *(APIENTRY *PFNWGLGETPROCADDRESS)(LPCSTR);
58
59
static String format_error_message(DWORD id) {
60
LPWSTR messageBuffer = nullptr;
61
size_t size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
62
nullptr, id, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, nullptr);
63
64
String msg = "Error " + itos(id) + ": " + String::utf16((const char16_t *)messageBuffer, size);
65
66
LocalFree(messageBuffer);
67
68
return msg;
69
}
70
71
const int OGL_THREAD_CONTROL_ID = 0x20C1221E;
72
const int OGL_THREAD_CONTROL_DISABLE = 0x00000002;
73
const int OGL_THREAD_CONTROL_ENABLE = 0x00000001;
74
const int VRR_MODE_ID = 0x1194F158;
75
const int VRR_MODE_FULLSCREEN_ONLY = 0x1;
76
77
typedef int(__cdecl *NvAPI_Initialize_t)();
78
typedef int(__cdecl *NvAPI_Unload_t)();
79
typedef int(__cdecl *NvAPI_GetErrorMessage_t)(unsigned int, NvAPI_ShortString);
80
typedef int(__cdecl *NvAPI_DRS_CreateSession_t)(NvDRSSessionHandle *);
81
typedef int(__cdecl *NvAPI_DRS_DestroySession_t)(NvDRSSessionHandle);
82
typedef int(__cdecl *NvAPI_DRS_LoadSettings_t)(NvDRSSessionHandle);
83
typedef int(__cdecl *NvAPI_DRS_CreateProfile_t)(NvDRSSessionHandle, NVDRS_PROFILE *, NvDRSProfileHandle *);
84
typedef int(__cdecl *NvAPI_DRS_CreateApplication_t)(NvDRSSessionHandle, NvDRSProfileHandle, NVDRS_APPLICATION *);
85
typedef int(__cdecl *NvAPI_DRS_SaveSettings_t)(NvDRSSessionHandle);
86
typedef int(__cdecl *NvAPI_DRS_SetSetting_t)(NvDRSSessionHandle, NvDRSProfileHandle, NVDRS_SETTING *);
87
typedef int(__cdecl *NvAPI_DRS_FindProfileByName_t)(NvDRSSessionHandle, NvAPI_UnicodeString, NvDRSProfileHandle *);
88
typedef int(__cdecl *NvAPI_DRS_GetApplicationInfo_t)(NvDRSSessionHandle, NvDRSProfileHandle, NvAPI_UnicodeString, NVDRS_APPLICATION *);
89
typedef int(__cdecl *NvAPI_DRS_DeleteProfile_t)(NvDRSSessionHandle, NvDRSProfileHandle);
90
NvAPI_GetErrorMessage_t NvAPI_GetErrorMessage__;
91
92
static bool nvapi_err_check(const char *msg, int status) {
93
if (status != 0) {
94
if (OS::get_singleton()->is_stdout_verbose()) {
95
NvAPI_ShortString err_desc = { 0 };
96
NvAPI_GetErrorMessage__(status, err_desc);
97
print_verbose(vformat("%s: %s(code %d)", msg, err_desc, status));
98
}
99
return false;
100
}
101
return true;
102
}
103
104
// On windows we have to customize the NVIDIA application profile:
105
// * disable threaded optimization when using NVIDIA cards to avoid stuttering, see
106
// https://stackoverflow.com/questions/36959508/nvidia-graphics-driver-causing-noticeable-frame-stuttering/37632948
107
// https://github.com/Ryujinx/Ryujinx/blob/master/src/Ryujinx.Common/GraphicsDriver/NVThreadedOptimization.cs
108
// * disable G-SYNC in windowed mode, as it results in unstable editor refresh rates
109
void GLManagerNative_Windows::_nvapi_setup_profile() {
110
HMODULE nvapi = nullptr;
111
#ifdef _WIN64
112
nvapi = LoadLibraryA("nvapi64.dll");
113
#else
114
nvapi = LoadLibraryA("nvapi.dll");
115
#endif
116
117
if (nvapi == nullptr) {
118
return;
119
}
120
121
void *(__cdecl * NvAPI_QueryInterface)(unsigned int interface_id) = nullptr;
122
123
NvAPI_QueryInterface = (void *(__cdecl *)(unsigned int))(void *)GetProcAddress(nvapi, "nvapi_QueryInterface");
124
125
if (NvAPI_QueryInterface == nullptr) {
126
print_verbose("Error getting NVAPI NvAPI_QueryInterface");
127
return;
128
}
129
130
// Setup NVAPI function pointers
131
NvAPI_Initialize_t NvAPI_Initialize = (NvAPI_Initialize_t)NvAPI_QueryInterface(0x0150E828);
132
NvAPI_GetErrorMessage__ = (NvAPI_GetErrorMessage_t)NvAPI_QueryInterface(0x6C2D048C);
133
NvAPI_DRS_CreateSession_t NvAPI_DRS_CreateSession = (NvAPI_DRS_CreateSession_t)NvAPI_QueryInterface(0x0694D52E);
134
NvAPI_DRS_DestroySession_t NvAPI_DRS_DestroySession = (NvAPI_DRS_DestroySession_t)NvAPI_QueryInterface(0xDAD9CFF8);
135
NvAPI_Unload_t NvAPI_Unload = (NvAPI_Unload_t)NvAPI_QueryInterface(0xD22BDD7E);
136
NvAPI_DRS_LoadSettings_t NvAPI_DRS_LoadSettings = (NvAPI_DRS_LoadSettings_t)NvAPI_QueryInterface(0x375DBD6B);
137
NvAPI_DRS_CreateProfile_t NvAPI_DRS_CreateProfile = (NvAPI_DRS_CreateProfile_t)NvAPI_QueryInterface(0xCC176068);
138
NvAPI_DRS_CreateApplication_t NvAPI_DRS_CreateApplication = (NvAPI_DRS_CreateApplication_t)NvAPI_QueryInterface(0x4347A9DE);
139
NvAPI_DRS_SaveSettings_t NvAPI_DRS_SaveSettings = (NvAPI_DRS_SaveSettings_t)NvAPI_QueryInterface(0xFCBC7E14);
140
NvAPI_DRS_SetSetting_t NvAPI_DRS_SetSetting = (NvAPI_DRS_SetSetting_t)NvAPI_QueryInterface(0x577DD202);
141
NvAPI_DRS_FindProfileByName_t NvAPI_DRS_FindProfileByName = (NvAPI_DRS_FindProfileByName_t)NvAPI_QueryInterface(0x7E4A9A0B);
142
NvAPI_DRS_GetApplicationInfo_t NvAPI_DRS_GetApplicationInfo = (NvAPI_DRS_GetApplicationInfo_t)NvAPI_QueryInterface(0xED1F8C69);
143
NvAPI_DRS_DeleteProfile_t NvAPI_DRS_DeleteProfile = (NvAPI_DRS_DeleteProfile_t)NvAPI_QueryInterface(0x17093206);
144
145
if (!nvapi_err_check("NVAPI: Init failed", NvAPI_Initialize())) {
146
return;
147
}
148
149
print_verbose("NVAPI: Init OK!");
150
151
NvDRSSessionHandle session_handle;
152
153
if (NvAPI_DRS_CreateSession == nullptr) {
154
return;
155
}
156
157
if (!nvapi_err_check("NVAPI: Error creating DRS session", NvAPI_DRS_CreateSession(&session_handle))) {
158
NvAPI_Unload();
159
return;
160
}
161
162
if (!nvapi_err_check("NVAPI: Error loading DRS settings", NvAPI_DRS_LoadSettings(session_handle))) {
163
NvAPI_DRS_DestroySession(session_handle);
164
NvAPI_Unload();
165
return;
166
}
167
168
String app_executable_name = OS::get_singleton()->get_executable_path().get_file();
169
String app_profile_name = GLOBAL_GET("application/config/name");
170
// We need a name anyways, so let's use the engine name if an application name is not available
171
// (this is used mostly by the Project Manager)
172
if (app_profile_name.is_empty()) {
173
app_profile_name = GODOT_VERSION_NAME;
174
}
175
String old_profile_name = app_profile_name + " Nvidia Profile";
176
Char16String app_profile_name_u16 = app_profile_name.utf16();
177
Char16String old_profile_name_u16 = old_profile_name.utf16();
178
Char16String app_executable_name_u16 = app_executable_name.utf16();
179
180
// A previous error in app creation logic could result in invalid profiles,
181
// clean these if they exist before proceeding.
182
NvDRSProfileHandle old_profile_handle;
183
184
int old_status = NvAPI_DRS_FindProfileByName(session_handle, (NvU16 *)(old_profile_name_u16.ptrw()), &old_profile_handle);
185
186
if (old_status == 0) {
187
print_verbose("NVAPI: Deleting old profile...");
188
189
if (!nvapi_err_check("NVAPI: Error deleting old profile", NvAPI_DRS_DeleteProfile(session_handle, old_profile_handle))) {
190
NvAPI_DRS_DestroySession(session_handle);
191
NvAPI_Unload();
192
return;
193
}
194
195
if (!nvapi_err_check("NVAPI: Error deleting old profile", NvAPI_DRS_SaveSettings(session_handle))) {
196
NvAPI_DRS_DestroySession(session_handle);
197
NvAPI_Unload();
198
return;
199
}
200
}
201
202
NvDRSProfileHandle profile_handle = nullptr;
203
204
int profile_status = NvAPI_DRS_FindProfileByName(session_handle, (NvU16 *)(app_profile_name_u16.ptrw()), &profile_handle);
205
206
if (profile_status != 0) {
207
print_verbose("NVAPI: Profile not found, creating...");
208
209
NVDRS_PROFILE profile_info;
210
profile_info.version = NVDRS_PROFILE_VER;
211
profile_info.isPredefined = 0;
212
memcpy(profile_info.profileName, app_profile_name_u16.get_data(), sizeof(char16_t) * app_profile_name_u16.size());
213
214
if (!nvapi_err_check("NVAPI: Error creating profile", NvAPI_DRS_CreateProfile(session_handle, &profile_info, &profile_handle))) {
215
NvAPI_DRS_DestroySession(session_handle);
216
NvAPI_Unload();
217
return;
218
}
219
}
220
221
NVDRS_APPLICATION_V4 app;
222
app.version = NVDRS_APPLICATION_VER_V4;
223
224
int app_status = NvAPI_DRS_GetApplicationInfo(session_handle, profile_handle, (NvU16 *)(app_executable_name_u16.ptrw()), &app);
225
226
if (app_status != 0) {
227
print_verbose("NVAPI: Application not found in profile, creating...");
228
229
app.isPredefined = 0;
230
memcpy(app.appName, app_executable_name_u16.get_data(), sizeof(char16_t) * app_executable_name_u16.size());
231
memcpy(app.launcher, L"", sizeof(wchar_t));
232
memcpy(app.fileInFolder, L"", sizeof(wchar_t));
233
234
if (!nvapi_err_check("NVAPI: Error creating application", NvAPI_DRS_CreateApplication(session_handle, profile_handle, &app))) {
235
NvAPI_DRS_DestroySession(session_handle);
236
NvAPI_Unload();
237
return;
238
}
239
}
240
241
NVDRS_SETTING ogl_thread_control_setting = {};
242
ogl_thread_control_setting.version = NVDRS_SETTING_VER;
243
ogl_thread_control_setting.settingId = OGL_THREAD_CONTROL_ID;
244
ogl_thread_control_setting.settingType = NVDRS_DWORD_TYPE;
245
int thread_control_val = OGL_THREAD_CONTROL_DISABLE;
246
if (!GLOBAL_GET("rendering/gl_compatibility/nvidia_disable_threaded_optimization")) {
247
thread_control_val = OGL_THREAD_CONTROL_ENABLE;
248
}
249
ogl_thread_control_setting.u32CurrentValue = thread_control_val;
250
251
if (!nvapi_err_check("NVAPI: Error calling NvAPI_DRS_SetSetting", NvAPI_DRS_SetSetting(session_handle, profile_handle, &ogl_thread_control_setting))) {
252
NvAPI_DRS_DestroySession(session_handle);
253
NvAPI_Unload();
254
return;
255
}
256
257
NVDRS_SETTING vrr_mode_setting = {};
258
vrr_mode_setting.version = NVDRS_SETTING_VER;
259
vrr_mode_setting.settingId = VRR_MODE_ID;
260
vrr_mode_setting.settingType = NVDRS_DWORD_TYPE;
261
vrr_mode_setting.u32CurrentValue = VRR_MODE_FULLSCREEN_ONLY;
262
263
if (!nvapi_err_check("NVAPI: Error calling NvAPI_DRS_SetSetting", NvAPI_DRS_SetSetting(session_handle, profile_handle, &vrr_mode_setting))) {
264
NvAPI_DRS_DestroySession(session_handle);
265
NvAPI_Unload();
266
return;
267
}
268
269
if (!nvapi_err_check("NVAPI: Error saving settings", NvAPI_DRS_SaveSettings(session_handle))) {
270
NvAPI_DRS_DestroySession(session_handle);
271
NvAPI_Unload();
272
return;
273
}
274
275
if (thread_control_val == OGL_THREAD_CONTROL_DISABLE) {
276
print_verbose("NVAPI: Disabled OpenGL threaded optimization successfully");
277
} else {
278
print_verbose("NVAPI: Enabled OpenGL threaded optimization successfully");
279
}
280
print_verbose("NVAPI: Disabled G-SYNC for windowed mode successfully");
281
282
NvAPI_DRS_DestroySession(session_handle);
283
}
284
285
int GLManagerNative_Windows::_find_or_create_display(GLWindow &win) {
286
// find display NYI, only 1 supported so far
287
if (_displays.size()) {
288
return 0;
289
}
290
291
// create
292
GLDisplay d_temp = {};
293
_displays.push_back(d_temp);
294
int new_display_id = _displays.size() - 1;
295
296
// create context
297
GLDisplay &d = _displays[new_display_id];
298
Error err = _create_context(win, d);
299
300
if (err != OK) {
301
// not good
302
// delete the _display?
303
_displays.remove_at(new_display_id);
304
return -1;
305
}
306
307
return new_display_id;
308
}
309
310
static Error _configure_pixel_format(HDC hDC) {
311
static PIXELFORMATDESCRIPTOR pfd = {
312
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
313
1,
314
PFD_DRAW_TO_WINDOW | // Format Must Support Window
315
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
316
PFD_DOUBLEBUFFER,
317
(BYTE)PFD_TYPE_RGBA,
318
(BYTE)(OS::get_singleton()->is_layered_allowed() ? 32 : 24),
319
(BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Color Bits Ignored
320
(BYTE)(OS::get_singleton()->is_layered_allowed() ? 8 : 0), // Alpha Buffer
321
(BYTE)0, // Shift Bit Ignored
322
(BYTE)0, // No Accumulation Buffer
323
(BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Accumulation Bits Ignored
324
(BYTE)24, // 24Bit Z-Buffer (Depth Buffer)
325
(BYTE)0, // No Stencil Buffer
326
(BYTE)0, // No Auxiliary Buffer
327
(BYTE)PFD_MAIN_PLANE, // Main Drawing Layer
328
(BYTE)0, // Reserved
329
0, 0, 0 // Layer Masks Ignored
330
};
331
332
int pixel_format = ChoosePixelFormat(hDC, &pfd);
333
if (!pixel_format) // Did Windows Find A Matching Pixel Format?
334
{
335
return ERR_CANT_CREATE; // Return FALSE
336
}
337
338
BOOL ret = SetPixelFormat(hDC, pixel_format, &pfd);
339
if (!ret) // Are We Able To Set The Pixel Format?
340
{
341
return ERR_CANT_CREATE; // Return FALSE
342
}
343
344
return OK;
345
}
346
347
PFNWGLCREATECONTEXT gd_wglCreateContext;
348
PFNWGLMAKECURRENT gd_wglMakeCurrent;
349
PFNWGLDELETECONTEXT gd_wglDeleteContext;
350
PFNWGLGETPROCADDRESS gd_wglGetProcAddress;
351
352
Error GLManagerNative_Windows::_create_context(GLWindow &win, GLDisplay &gl_display) {
353
Error err = _configure_pixel_format(win.hDC);
354
if (err != OK) {
355
return err;
356
}
357
358
HMODULE module = LoadLibraryW(L"opengl32.dll");
359
if (!module) {
360
return ERR_CANT_CREATE;
361
}
362
gd_wglCreateContext = (PFNWGLCREATECONTEXT)(void *)GetProcAddress(module, "wglCreateContext");
363
gd_wglMakeCurrent = (PFNWGLMAKECURRENT)(void *)GetProcAddress(module, "wglMakeCurrent");
364
gd_wglDeleteContext = (PFNWGLDELETECONTEXT)(void *)GetProcAddress(module, "wglDeleteContext");
365
gd_wglGetProcAddress = (PFNWGLGETPROCADDRESS)(void *)GetProcAddress(module, "wglGetProcAddress");
366
if (!gd_wglCreateContext || !gd_wglMakeCurrent || !gd_wglDeleteContext || !gd_wglGetProcAddress) {
367
return ERR_CANT_CREATE;
368
}
369
370
gl_display.hRC = gd_wglCreateContext(win.hDC);
371
if (!gl_display.hRC) // Are We Able To Get A Rendering Context?
372
{
373
return ERR_CANT_CREATE; // Return FALSE
374
}
375
376
if (!gd_wglMakeCurrent(win.hDC, gl_display.hRC)) {
377
ERR_PRINT("Could not attach OpenGL context to newly created window: " + format_error_message(GetLastError()));
378
}
379
380
int attribs[] = {
381
WGL_CONTEXT_MAJOR_VERSION_ARB, 3, //we want a 3.3 context
382
WGL_CONTEXT_MINOR_VERSION_ARB, 3,
383
//and it shall be forward compatible so that we can only use up to date functionality
384
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
385
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*| _WGL_CONTEXT_DEBUG_BIT_ARB*/,
386
0
387
}; //zero indicates the end of the array
388
389
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = nullptr; //pointer to the method
390
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)gd_wglGetProcAddress("wglCreateContextAttribsARB");
391
392
if (wglCreateContextAttribsARB == nullptr) //OpenGL 3.0 is not supported
393
{
394
gd_wglDeleteContext(gl_display.hRC);
395
gl_display.hRC = nullptr;
396
return ERR_CANT_CREATE;
397
}
398
399
HGLRC new_hRC = wglCreateContextAttribsARB(win.hDC, nullptr, attribs);
400
if (!new_hRC) {
401
gd_wglDeleteContext(gl_display.hRC);
402
gl_display.hRC = nullptr;
403
return ERR_CANT_CREATE;
404
}
405
406
if (!gd_wglMakeCurrent(win.hDC, nullptr)) {
407
ERR_PRINT("Could not detach OpenGL context from newly created window: " + format_error_message(GetLastError()));
408
}
409
410
gd_wglDeleteContext(gl_display.hRC);
411
gl_display.hRC = new_hRC;
412
413
if (!gd_wglMakeCurrent(win.hDC, gl_display.hRC)) // Try to activate the rendering context.
414
{
415
ERR_PRINT("Could not attach OpenGL context to newly created window with replaced OpenGL context: " + format_error_message(GetLastError()));
416
gd_wglDeleteContext(gl_display.hRC);
417
gl_display.hRC = nullptr;
418
return ERR_CANT_CREATE;
419
}
420
421
if (!wglSwapIntervalEXT) {
422
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)gd_wglGetProcAddress("wglSwapIntervalEXT");
423
}
424
425
return OK;
426
}
427
428
Error GLManagerNative_Windows::window_create(DisplayServer::WindowID p_window_id, HWND p_hwnd, HINSTANCE p_hinstance, int p_width, int p_height) {
429
HDC hDC = GetDC(p_hwnd);
430
if (!hDC) {
431
return ERR_CANT_CREATE;
432
}
433
434
// configure the HDC to use a compatible pixel format
435
Error result = _configure_pixel_format(hDC);
436
if (result != OK) {
437
return result;
438
}
439
440
GLWindow win;
441
win.hwnd = p_hwnd;
442
win.hDC = hDC;
443
444
win.gldisplay_id = _find_or_create_display(win);
445
446
if (win.gldisplay_id == -1) {
447
return FAILED;
448
}
449
450
// WARNING: `p_window_id` is an eternally growing integer since popup windows keep coming and going
451
// and each of them has a higher id than the previous, so it must be used in a map not a vector.
452
_windows[p_window_id] = win;
453
454
// make current
455
window_make_current(p_window_id);
456
457
return OK;
458
}
459
460
void GLManagerNative_Windows::window_destroy(DisplayServer::WindowID p_window_id) {
461
GLWindow &win = get_window(p_window_id);
462
if (_current_window == &win) {
463
_current_window = nullptr;
464
}
465
_windows.erase(p_window_id);
466
}
467
468
void GLManagerNative_Windows::release_current() {
469
if (!_current_window) {
470
return;
471
}
472
473
if (!gd_wglMakeCurrent(_current_window->hDC, nullptr)) {
474
ERR_PRINT("Could not detach OpenGL context from window marked current: " + format_error_message(GetLastError()));
475
}
476
477
_current_window = nullptr;
478
}
479
480
void GLManagerNative_Windows::window_make_current(DisplayServer::WindowID p_window_id) {
481
if (p_window_id == -1) {
482
return;
483
}
484
485
// crash if our data structures are out of sync, i.e. not found
486
GLWindow &win = _windows[p_window_id];
487
488
// noop
489
if (&win == _current_window) {
490
return;
491
}
492
493
const GLDisplay &disp = get_display(win.gldisplay_id);
494
if (!gd_wglMakeCurrent(win.hDC, disp.hRC)) {
495
ERR_PRINT("Could not switch OpenGL context to other window: " + format_error_message(GetLastError()));
496
}
497
498
_current_window = &win;
499
}
500
501
void GLManagerNative_Windows::swap_buffers() {
502
SwapBuffers(_current_window->hDC);
503
}
504
505
Error GLManagerNative_Windows::initialize() {
506
_nvapi_setup_profile();
507
return OK;
508
}
509
510
void GLManagerNative_Windows::set_use_vsync(DisplayServer::WindowID p_window_id, bool p_use) {
511
GLWindow &win = get_window(p_window_id);
512
513
if (&win != _current_window) {
514
window_make_current(p_window_id);
515
}
516
517
if (wglSwapIntervalEXT) {
518
win.use_vsync = p_use;
519
520
if (!wglSwapIntervalEXT(p_use ? 1 : 0)) {
521
WARN_PRINT_ONCE("Could not set V-Sync mode, as changing V-Sync mode is not supported by the graphics driver.");
522
}
523
} else {
524
WARN_PRINT_ONCE("Could not set V-Sync mode, as changing V-Sync mode is not supported by the graphics driver.");
525
}
526
}
527
528
bool GLManagerNative_Windows::is_using_vsync(DisplayServer::WindowID p_window_id) const {
529
return get_window(p_window_id).use_vsync;
530
}
531
532
HDC GLManagerNative_Windows::get_hdc(DisplayServer::WindowID p_window_id) {
533
return get_window(p_window_id).hDC;
534
}
535
536
HGLRC GLManagerNative_Windows::get_hglrc(DisplayServer::WindowID p_window_id) {
537
const GLWindow &win = get_window(p_window_id);
538
const GLDisplay &disp = get_display(win.gldisplay_id);
539
return disp.hRC;
540
}
541
542
GLManagerNative_Windows::GLManagerNative_Windows() {
543
direct_render = false;
544
glx_minor = glx_major = 0;
545
_current_window = nullptr;
546
}
547
548
GLManagerNative_Windows::~GLManagerNative_Windows() {
549
release_current();
550
}
551
552
#endif // WINDOWS_ENABLED && GLES3_ENABLED
553
554