| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | /*  gl_manager_windows_native.cpp                                         */ | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the        */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including    */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
					
						
							|  |  |  | /* the following conditions:                                              */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be         */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.        */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | #include "gl_manager_windows_native.h"
 | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 14:51:32 +02:00
										 |  |  | #if defined(WINDOWS_ENABLED) && defined(GLES3_ENABLED)
 | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | #include "core/config/project_settings.h"
 | 
					
						
							|  |  |  | #include "core/version.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-31 10:58:38 +01:00
										 |  |  | #include "thirdparty/misc/nvapi_minimal.h"
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 14:51:32 +02:00
										 |  |  | #include <dwmapi.h>
 | 
					
						
							| 
									
										
										
										
											2025-04-28 16:05:13 -05:00
										 |  |  | #include <cstdio>
 | 
					
						
							|  |  |  | #include <cstdlib>
 | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
 | 
					
						
							|  |  |  | #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
 | 
					
						
							|  |  |  | #define WGL_CONTEXT_FLAGS_ARB 0x2094
 | 
					
						
							|  |  |  | #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002
 | 
					
						
							|  |  |  | #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
 | 
					
						
							|  |  |  | #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define _WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXT)(HDC); | 
					
						
							|  |  |  | typedef BOOL(APIENTRY *PFNWGLDELETECONTEXT)(HGLRC); | 
					
						
							|  |  |  | typedef BOOL(APIENTRY *PFNWGLMAKECURRENT)(HDC, HGLRC); | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC, const int *); | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | typedef void *(APIENTRY *PFNWGLGETPROCADDRESS)(LPCSTR); | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | static String format_error_message(DWORD id) { | 
					
						
							|  |  |  | 	LPWSTR messageBuffer = nullptr; | 
					
						
							|  |  |  | 	size_t size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | 
					
						
							|  |  |  | 			nullptr, id, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, nullptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	String msg = "Error " + itos(id) + ": " + String::utf16((const char16_t *)messageBuffer, size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	LocalFree(messageBuffer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return msg; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | const int OGL_THREAD_CONTROL_ID = 0x20C1221E; | 
					
						
							|  |  |  | const int OGL_THREAD_CONTROL_DISABLE = 0x00000002; | 
					
						
							|  |  |  | const int OGL_THREAD_CONTROL_ENABLE = 0x00000001; | 
					
						
							| 
									
										
										
										
											2024-06-29 13:02:30 +02:00
										 |  |  | const int VRR_MODE_ID = 0x1194F158; | 
					
						
							|  |  |  | const int VRR_MODE_FULLSCREEN_ONLY = 0x1; | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef int(__cdecl *NvAPI_Initialize_t)(); | 
					
						
							|  |  |  | typedef int(__cdecl *NvAPI_Unload_t)(); | 
					
						
							|  |  |  | typedef int(__cdecl *NvAPI_GetErrorMessage_t)(unsigned int, NvAPI_ShortString); | 
					
						
							|  |  |  | typedef int(__cdecl *NvAPI_DRS_CreateSession_t)(NvDRSSessionHandle *); | 
					
						
							|  |  |  | typedef int(__cdecl *NvAPI_DRS_DestroySession_t)(NvDRSSessionHandle); | 
					
						
							|  |  |  | typedef int(__cdecl *NvAPI_DRS_LoadSettings_t)(NvDRSSessionHandle); | 
					
						
							|  |  |  | typedef int(__cdecl *NvAPI_DRS_CreateProfile_t)(NvDRSSessionHandle, NVDRS_PROFILE *, NvDRSProfileHandle *); | 
					
						
							|  |  |  | typedef int(__cdecl *NvAPI_DRS_CreateApplication_t)(NvDRSSessionHandle, NvDRSProfileHandle, NVDRS_APPLICATION *); | 
					
						
							|  |  |  | typedef int(__cdecl *NvAPI_DRS_SaveSettings_t)(NvDRSSessionHandle); | 
					
						
							|  |  |  | typedef int(__cdecl *NvAPI_DRS_SetSetting_t)(NvDRSSessionHandle, NvDRSProfileHandle, NVDRS_SETTING *); | 
					
						
							|  |  |  | typedef int(__cdecl *NvAPI_DRS_FindProfileByName_t)(NvDRSSessionHandle, NvAPI_UnicodeString, NvDRSProfileHandle *); | 
					
						
							| 
									
										
										
										
											2023-11-21 16:19:22 +01:00
										 |  |  | typedef int(__cdecl *NvAPI_DRS_GetApplicationInfo_t)(NvDRSSessionHandle, NvDRSProfileHandle, NvAPI_UnicodeString, NVDRS_APPLICATION *); | 
					
						
							|  |  |  | typedef int(__cdecl *NvAPI_DRS_DeleteProfile_t)(NvDRSSessionHandle, NvDRSProfileHandle); | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | NvAPI_GetErrorMessage_t NvAPI_GetErrorMessage__; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-16 14:54:36 +03:00
										 |  |  | static bool nvapi_err_check(const char *msg, int status) { | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 	if (status != 0) { | 
					
						
							|  |  |  | 		if (OS::get_singleton()->is_stdout_verbose()) { | 
					
						
							|  |  |  | 			NvAPI_ShortString err_desc = { 0 }; | 
					
						
							|  |  |  | 			NvAPI_GetErrorMessage__(status, err_desc); | 
					
						
							|  |  |  | 			print_verbose(vformat("%s: %s(code %d)", msg, err_desc, status)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-29 13:02:30 +02:00
										 |  |  | // On windows we have to customize the NVIDIA application profile:
 | 
					
						
							|  |  |  | // * disable threaded optimization when using NVIDIA cards to avoid stuttering, see
 | 
					
						
							|  |  |  | //   https://stackoverflow.com/questions/36959508/nvidia-graphics-driver-causing-noticeable-frame-stuttering/37632948
 | 
					
						
							|  |  |  | //   https://github.com/Ryujinx/Ryujinx/blob/master/src/Ryujinx.Common/GraphicsDriver/NVThreadedOptimization.cs
 | 
					
						
							|  |  |  | // * disable G-SYNC in windowed mode, as it results in unstable editor refresh rates
 | 
					
						
							|  |  |  | void GLManagerNative_Windows::_nvapi_setup_profile() { | 
					
						
							| 
									
										
										
										
											2024-03-12 09:40:40 -05:00
										 |  |  | 	HMODULE nvapi = nullptr; | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | #ifdef _WIN64
 | 
					
						
							|  |  |  | 	nvapi = LoadLibraryA("nvapi64.dll"); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 	nvapi = LoadLibraryA("nvapi.dll"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-12 09:40:40 -05:00
										 |  |  | 	if (nvapi == nullptr) { | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-12 09:40:40 -05:00
										 |  |  | 	void *(__cdecl * NvAPI_QueryInterface)(unsigned int interface_id) = nullptr; | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 	NvAPI_QueryInterface = (void *(__cdecl *)(unsigned int))(void *)GetProcAddress(nvapi, "nvapi_QueryInterface"); | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-12 09:40:40 -05:00
										 |  |  | 	if (NvAPI_QueryInterface == nullptr) { | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 		print_verbose("Error getting NVAPI NvAPI_QueryInterface"); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Setup NVAPI function pointers
 | 
					
						
							|  |  |  | 	NvAPI_Initialize_t NvAPI_Initialize = (NvAPI_Initialize_t)NvAPI_QueryInterface(0x0150E828); | 
					
						
							|  |  |  | 	NvAPI_GetErrorMessage__ = (NvAPI_GetErrorMessage_t)NvAPI_QueryInterface(0x6C2D048C); | 
					
						
							|  |  |  | 	NvAPI_DRS_CreateSession_t NvAPI_DRS_CreateSession = (NvAPI_DRS_CreateSession_t)NvAPI_QueryInterface(0x0694D52E); | 
					
						
							|  |  |  | 	NvAPI_DRS_DestroySession_t NvAPI_DRS_DestroySession = (NvAPI_DRS_DestroySession_t)NvAPI_QueryInterface(0xDAD9CFF8); | 
					
						
							|  |  |  | 	NvAPI_Unload_t NvAPI_Unload = (NvAPI_Unload_t)NvAPI_QueryInterface(0xD22BDD7E); | 
					
						
							|  |  |  | 	NvAPI_DRS_LoadSettings_t NvAPI_DRS_LoadSettings = (NvAPI_DRS_LoadSettings_t)NvAPI_QueryInterface(0x375DBD6B); | 
					
						
							|  |  |  | 	NvAPI_DRS_CreateProfile_t NvAPI_DRS_CreateProfile = (NvAPI_DRS_CreateProfile_t)NvAPI_QueryInterface(0xCC176068); | 
					
						
							|  |  |  | 	NvAPI_DRS_CreateApplication_t NvAPI_DRS_CreateApplication = (NvAPI_DRS_CreateApplication_t)NvAPI_QueryInterface(0x4347A9DE); | 
					
						
							|  |  |  | 	NvAPI_DRS_SaveSettings_t NvAPI_DRS_SaveSettings = (NvAPI_DRS_SaveSettings_t)NvAPI_QueryInterface(0xFCBC7E14); | 
					
						
							|  |  |  | 	NvAPI_DRS_SetSetting_t NvAPI_DRS_SetSetting = (NvAPI_DRS_SetSetting_t)NvAPI_QueryInterface(0x577DD202); | 
					
						
							|  |  |  | 	NvAPI_DRS_FindProfileByName_t NvAPI_DRS_FindProfileByName = (NvAPI_DRS_FindProfileByName_t)NvAPI_QueryInterface(0x7E4A9A0B); | 
					
						
							| 
									
										
										
										
											2023-11-21 16:19:22 +01:00
										 |  |  | 	NvAPI_DRS_GetApplicationInfo_t NvAPI_DRS_GetApplicationInfo = (NvAPI_DRS_GetApplicationInfo_t)NvAPI_QueryInterface(0xED1F8C69); | 
					
						
							|  |  |  | 	NvAPI_DRS_DeleteProfile_t NvAPI_DRS_DeleteProfile = (NvAPI_DRS_DeleteProfile_t)NvAPI_QueryInterface(0x17093206); | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!nvapi_err_check("NVAPI: Init failed", NvAPI_Initialize())) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	print_verbose("NVAPI: Init OK!"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	NvDRSSessionHandle session_handle; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-20 19:31:14 +08:00
										 |  |  | 	if (NvAPI_DRS_CreateSession == nullptr) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 	if (!nvapi_err_check("NVAPI: Error creating DRS session", NvAPI_DRS_CreateSession(&session_handle))) { | 
					
						
							|  |  |  | 		NvAPI_Unload(); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!nvapi_err_check("NVAPI: Error loading DRS settings", NvAPI_DRS_LoadSettings(session_handle))) { | 
					
						
							|  |  |  | 		NvAPI_DRS_DestroySession(session_handle); | 
					
						
							|  |  |  | 		NvAPI_Unload(); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	String app_executable_name = OS::get_singleton()->get_executable_path().get_file(); | 
					
						
							| 
									
										
										
										
											2023-11-21 16:19:22 +01:00
										 |  |  | 	String app_profile_name = GLOBAL_GET("application/config/name"); | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 	// We need a name anyways, so let's use the engine name if an application name is not available
 | 
					
						
							|  |  |  | 	// (this is used mostly by the Project Manager)
 | 
					
						
							| 
									
										
										
										
											2023-11-21 16:19:22 +01:00
										 |  |  | 	if (app_profile_name.is_empty()) { | 
					
						
							| 
									
										
										
										
											2025-03-03 22:27:29 -08:00
										 |  |  | 		app_profile_name = GODOT_VERSION_NAME; | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-11-21 16:19:22 +01:00
										 |  |  | 	String old_profile_name = app_profile_name + " Nvidia Profile"; | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 	Char16String app_profile_name_u16 = app_profile_name.utf16(); | 
					
						
							| 
									
										
										
										
											2023-11-21 16:19:22 +01:00
										 |  |  | 	Char16String old_profile_name_u16 = old_profile_name.utf16(); | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 	Char16String app_executable_name_u16 = app_executable_name.utf16(); | 
					
						
							| 
									
										
										
										
											2023-11-21 16:19:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// A previous error in app creation logic could result in invalid profiles,
 | 
					
						
							|  |  |  | 	// clean these if they exist before proceeding.
 | 
					
						
							|  |  |  | 	NvDRSProfileHandle old_profile_handle; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int old_status = NvAPI_DRS_FindProfileByName(session_handle, (NvU16 *)(old_profile_name_u16.ptrw()), &old_profile_handle); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (old_status == 0) { | 
					
						
							|  |  |  | 		print_verbose("NVAPI: Deleting old profile..."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!nvapi_err_check("NVAPI: Error deleting old profile", NvAPI_DRS_DeleteProfile(session_handle, old_profile_handle))) { | 
					
						
							|  |  |  | 			NvAPI_DRS_DestroySession(session_handle); | 
					
						
							|  |  |  | 			NvAPI_Unload(); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!nvapi_err_check("NVAPI: Error deleting old profile", NvAPI_DRS_SaveSettings(session_handle))) { | 
					
						
							|  |  |  | 			NvAPI_DRS_DestroySession(session_handle); | 
					
						
							|  |  |  | 			NvAPI_Unload(); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-12 09:40:40 -05:00
										 |  |  | 	NvDRSProfileHandle profile_handle = nullptr; | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-02 16:22:07 +02:00
										 |  |  | 	int profile_status = NvAPI_DRS_FindProfileByName(session_handle, (NvU16 *)(app_profile_name_u16.ptrw()), &profile_handle); | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-02 16:22:07 +02:00
										 |  |  | 	if (profile_status != 0) { | 
					
						
							| 
									
										
										
										
											2023-11-21 16:19:22 +01:00
										 |  |  | 		print_verbose("NVAPI: Profile not found, creating..."); | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		NVDRS_PROFILE profile_info; | 
					
						
							|  |  |  | 		profile_info.version = NVDRS_PROFILE_VER; | 
					
						
							|  |  |  | 		profile_info.isPredefined = 0; | 
					
						
							|  |  |  | 		memcpy(profile_info.profileName, app_profile_name_u16.get_data(), sizeof(char16_t) * app_profile_name_u16.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!nvapi_err_check("NVAPI: Error creating profile", NvAPI_DRS_CreateProfile(session_handle, &profile_info, &profile_handle))) { | 
					
						
							|  |  |  | 			NvAPI_DRS_DestroySession(session_handle); | 
					
						
							|  |  |  | 			NvAPI_Unload(); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-09-02 16:22:07 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	NVDRS_APPLICATION_V4 app; | 
					
						
							|  |  |  | 	app.version = NVDRS_APPLICATION_VER_V4; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-21 16:19:22 +01:00
										 |  |  | 	int app_status = NvAPI_DRS_GetApplicationInfo(session_handle, profile_handle, (NvU16 *)(app_executable_name_u16.ptrw()), &app); | 
					
						
							| 
									
										
										
										
											2023-09-02 16:22:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (app_status != 0) { | 
					
						
							| 
									
										
										
										
											2023-11-21 16:19:22 +01:00
										 |  |  | 		print_verbose("NVAPI: Application not found in profile, creating..."); | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		app.isPredefined = 0; | 
					
						
							|  |  |  | 		memcpy(app.appName, app_executable_name_u16.get_data(), sizeof(char16_t) * app_executable_name_u16.size()); | 
					
						
							| 
									
										
										
										
											2023-11-21 16:19:22 +01:00
										 |  |  | 		memcpy(app.launcher, L"", sizeof(wchar_t)); | 
					
						
							|  |  |  | 		memcpy(app.fileInFolder, L"", sizeof(wchar_t)); | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (!nvapi_err_check("NVAPI: Error creating application", NvAPI_DRS_CreateApplication(session_handle, profile_handle, &app))) { | 
					
						
							|  |  |  | 			NvAPI_DRS_DestroySession(session_handle); | 
					
						
							|  |  |  | 			NvAPI_Unload(); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-25 13:55:56 +02:00
										 |  |  | 	NVDRS_SETTING ogl_thread_control_setting = {}; | 
					
						
							| 
									
										
										
										
											2024-06-29 13:02:30 +02:00
										 |  |  | 	ogl_thread_control_setting.version = NVDRS_SETTING_VER; | 
					
						
							|  |  |  | 	ogl_thread_control_setting.settingId = OGL_THREAD_CONTROL_ID; | 
					
						
							|  |  |  | 	ogl_thread_control_setting.settingType = NVDRS_DWORD_TYPE; | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 	int thread_control_val = OGL_THREAD_CONTROL_DISABLE; | 
					
						
							|  |  |  | 	if (!GLOBAL_GET("rendering/gl_compatibility/nvidia_disable_threaded_optimization")) { | 
					
						
							|  |  |  | 		thread_control_val = OGL_THREAD_CONTROL_ENABLE; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-06-29 13:02:30 +02:00
										 |  |  | 	ogl_thread_control_setting.u32CurrentValue = thread_control_val; | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-29 13:02:30 +02:00
										 |  |  | 	if (!nvapi_err_check("NVAPI: Error calling NvAPI_DRS_SetSetting", NvAPI_DRS_SetSetting(session_handle, profile_handle, &ogl_thread_control_setting))) { | 
					
						
							|  |  |  | 		NvAPI_DRS_DestroySession(session_handle); | 
					
						
							|  |  |  | 		NvAPI_Unload(); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-25 13:55:56 +02:00
										 |  |  | 	NVDRS_SETTING vrr_mode_setting = {}; | 
					
						
							| 
									
										
										
										
											2024-06-29 13:02:30 +02:00
										 |  |  | 	vrr_mode_setting.version = NVDRS_SETTING_VER; | 
					
						
							|  |  |  | 	vrr_mode_setting.settingId = VRR_MODE_ID; | 
					
						
							|  |  |  | 	vrr_mode_setting.settingType = NVDRS_DWORD_TYPE; | 
					
						
							|  |  |  | 	vrr_mode_setting.u32CurrentValue = VRR_MODE_FULLSCREEN_ONLY; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!nvapi_err_check("NVAPI: Error calling NvAPI_DRS_SetSetting", NvAPI_DRS_SetSetting(session_handle, profile_handle, &vrr_mode_setting))) { | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 		NvAPI_DRS_DestroySession(session_handle); | 
					
						
							|  |  |  | 		NvAPI_Unload(); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!nvapi_err_check("NVAPI: Error saving settings", NvAPI_DRS_SaveSettings(session_handle))) { | 
					
						
							|  |  |  | 		NvAPI_DRS_DestroySession(session_handle); | 
					
						
							|  |  |  | 		NvAPI_Unload(); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-11-21 16:19:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 	if (thread_control_val == OGL_THREAD_CONTROL_DISABLE) { | 
					
						
							|  |  |  | 		print_verbose("NVAPI: Disabled OpenGL threaded optimization successfully"); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		print_verbose("NVAPI: Enabled OpenGL threaded optimization successfully"); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-06-29 13:02:30 +02:00
										 |  |  | 	print_verbose("NVAPI: Disabled G-SYNC for windowed mode successfully"); | 
					
						
							| 
									
										
										
										
											2023-11-21 16:19:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:42:49 +01:00
										 |  |  | 	NvAPI_DRS_DestroySession(session_handle); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | int GLManagerNative_Windows::_find_or_create_display(GLWindow &win) { | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	// find display NYI, only 1 supported so far
 | 
					
						
							| 
									
										
										
										
											2022-02-16 13:56:32 +01:00
										 |  |  | 	if (_displays.size()) { | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2022-02-16 13:56:32 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create
 | 
					
						
							| 
									
										
										
										
											2021-09-27 01:07:10 +02:00
										 |  |  | 	GLDisplay d_temp = {}; | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	_displays.push_back(d_temp); | 
					
						
							|  |  |  | 	int new_display_id = _displays.size() - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create context
 | 
					
						
							|  |  |  | 	GLDisplay &d = _displays[new_display_id]; | 
					
						
							|  |  |  | 	Error err = _create_context(win, d); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (err != OK) { | 
					
						
							|  |  |  | 		// not good
 | 
					
						
							|  |  |  | 		// delete the _display?
 | 
					
						
							| 
									
										
										
										
											2021-07-03 16:17:03 -06:00
										 |  |  | 		_displays.remove_at(new_display_id); | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return new_display_id; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | static Error _configure_pixel_format(HDC hDC) { | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	static PIXELFORMATDESCRIPTOR pfd = { | 
					
						
							|  |  |  | 		sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
 | 
					
						
							|  |  |  | 		1, | 
					
						
							|  |  |  | 		PFD_DRAW_TO_WINDOW | // Format Must Support Window
 | 
					
						
							|  |  |  | 				PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
 | 
					
						
							|  |  |  | 				PFD_DOUBLEBUFFER, | 
					
						
							|  |  |  | 		(BYTE)PFD_TYPE_RGBA, | 
					
						
							|  |  |  | 		(BYTE)(OS::get_singleton()->is_layered_allowed() ? 32 : 24), | 
					
						
							|  |  |  | 		(BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Color Bits Ignored
 | 
					
						
							|  |  |  | 		(BYTE)(OS::get_singleton()->is_layered_allowed() ? 8 : 0), // Alpha Buffer
 | 
					
						
							|  |  |  | 		(BYTE)0, // Shift Bit Ignored
 | 
					
						
							|  |  |  | 		(BYTE)0, // No Accumulation Buffer
 | 
					
						
							|  |  |  | 		(BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Accumulation Bits Ignored
 | 
					
						
							|  |  |  | 		(BYTE)24, // 24Bit Z-Buffer (Depth Buffer)
 | 
					
						
							|  |  |  | 		(BYTE)0, // No Stencil Buffer
 | 
					
						
							|  |  |  | 		(BYTE)0, // No Auxiliary Buffer
 | 
					
						
							|  |  |  | 		(BYTE)PFD_MAIN_PLANE, // Main Drawing Layer
 | 
					
						
							|  |  |  | 		(BYTE)0, // Reserved
 | 
					
						
							|  |  |  | 		0, 0, 0 // Layer Masks Ignored
 | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int pixel_format = ChoosePixelFormat(hDC, &pfd); | 
					
						
							|  |  |  | 	if (!pixel_format) // Did Windows Find A Matching Pixel Format?
 | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return ERR_CANT_CREATE; // Return FALSE
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	BOOL ret = SetPixelFormat(hDC, pixel_format, &pfd); | 
					
						
							|  |  |  | 	if (!ret) // Are We Able To Set The Pixel Format?
 | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return ERR_CANT_CREATE; // Return FALSE
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | PFNWGLCREATECONTEXT gd_wglCreateContext; | 
					
						
							|  |  |  | PFNWGLMAKECURRENT gd_wglMakeCurrent; | 
					
						
							|  |  |  | PFNWGLDELETECONTEXT gd_wglDeleteContext; | 
					
						
							|  |  |  | PFNWGLGETPROCADDRESS gd_wglGetProcAddress; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Error GLManagerNative_Windows::_create_context(GLWindow &win, GLDisplay &gl_display) { | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 	Error err = _configure_pixel_format(win.hDC); | 
					
						
							|  |  |  | 	if (err != OK) { | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 	HMODULE module = LoadLibraryW(L"opengl32.dll"); | 
					
						
							|  |  |  | 	if (!module) { | 
					
						
							|  |  |  | 		return ERR_CANT_CREATE; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-02-27 12:30:04 +02:00
										 |  |  | 	gd_wglCreateContext = (PFNWGLCREATECONTEXT)(void *)GetProcAddress(module, "wglCreateContext"); | 
					
						
							|  |  |  | 	gd_wglMakeCurrent = (PFNWGLMAKECURRENT)(void *)GetProcAddress(module, "wglMakeCurrent"); | 
					
						
							|  |  |  | 	gd_wglDeleteContext = (PFNWGLDELETECONTEXT)(void *)GetProcAddress(module, "wglDeleteContext"); | 
					
						
							|  |  |  | 	gd_wglGetProcAddress = (PFNWGLGETPROCADDRESS)(void *)GetProcAddress(module, "wglGetProcAddress"); | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 	if (!gd_wglCreateContext || !gd_wglMakeCurrent || !gd_wglDeleteContext || !gd_wglGetProcAddress) { | 
					
						
							|  |  |  | 		return ERR_CANT_CREATE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	gl_display.hRC = gd_wglCreateContext(win.hDC); | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	if (!gl_display.hRC) // Are We Able To Get A Rendering Context?
 | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return ERR_CANT_CREATE; // Return FALSE
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 	if (!gd_wglMakeCurrent(win.hDC, gl_display.hRC)) { | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 		ERR_PRINT("Could not attach OpenGL context to newly created window: " + format_error_message(GetLastError())); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 08:18:39 -07:00
										 |  |  | 	int attribs[] = { | 
					
						
							|  |  |  | 		WGL_CONTEXT_MAJOR_VERSION_ARB, 3, //we want a 3.3 context
 | 
					
						
							|  |  |  | 		WGL_CONTEXT_MINOR_VERSION_ARB, 3, | 
					
						
							|  |  |  | 		//and it shall be forward compatible so that we can only use up to date functionality
 | 
					
						
							|  |  |  | 		WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, | 
					
						
							|  |  |  | 		WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*| _WGL_CONTEXT_DEBUG_BIT_ARB*/, | 
					
						
							|  |  |  | 		0 | 
					
						
							|  |  |  | 	}; //zero indicates the end of the array
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = nullptr; //pointer to the method
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 	wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)gd_wglGetProcAddress("wglCreateContextAttribsARB"); | 
					
						
							| 
									
										
										
										
											2021-10-26 08:18:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (wglCreateContextAttribsARB == nullptr) //OpenGL 3.0 is not supported
 | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 		gd_wglDeleteContext(gl_display.hRC); | 
					
						
							| 
									
										
										
										
											2024-03-12 09:40:40 -05:00
										 |  |  | 		gl_display.hRC = nullptr; | 
					
						
							| 
									
										
										
										
											2021-10-26 08:18:39 -07:00
										 |  |  | 		return ERR_CANT_CREATE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-12 09:40:40 -05:00
										 |  |  | 	HGLRC new_hRC = wglCreateContextAttribsARB(win.hDC, nullptr, attribs); | 
					
						
							| 
									
										
										
										
											2021-10-26 08:18:39 -07:00
										 |  |  | 	if (!new_hRC) { | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 		gd_wglDeleteContext(gl_display.hRC); | 
					
						
							| 
									
										
										
										
											2024-03-12 09:40:40 -05:00
										 |  |  | 		gl_display.hRC = nullptr; | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 		return ERR_CANT_CREATE; | 
					
						
							| 
									
										
										
										
											2021-10-26 08:18:39 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 	if (!gd_wglMakeCurrent(win.hDC, nullptr)) { | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 		ERR_PRINT("Could not detach OpenGL context from newly created window: " + format_error_message(GetLastError())); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 	gd_wglDeleteContext(gl_display.hRC); | 
					
						
							| 
									
										
										
										
											2021-10-26 08:18:39 -07:00
										 |  |  | 	gl_display.hRC = new_hRC; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 	if (!gd_wglMakeCurrent(win.hDC, gl_display.hRC)) // Try to activate the rendering context.
 | 
					
						
							| 
									
										
										
										
											2021-10-26 08:18:39 -07:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 		ERR_PRINT("Could not attach OpenGL context to newly created window with replaced OpenGL context: " + format_error_message(GetLastError())); | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 		gd_wglDeleteContext(gl_display.hRC); | 
					
						
							| 
									
										
										
										
											2024-03-12 09:40:40 -05:00
										 |  |  | 		gl_display.hRC = nullptr; | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 		return ERR_CANT_CREATE; | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-15 14:14:21 -08:00
										 |  |  | 	if (!wglSwapIntervalEXT) { | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 		wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)gd_wglGetProcAddress("wglSwapIntervalEXT"); | 
					
						
							| 
									
										
										
										
											2022-11-15 14:14:21 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | Error GLManagerNative_Windows::window_create(DisplayServer::WindowID p_window_id, HWND p_hwnd, HINSTANCE p_hinstance, int p_width, int p_height) { | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 	HDC hDC = GetDC(p_hwnd); | 
					
						
							|  |  |  | 	if (!hDC) { | 
					
						
							|  |  |  | 		return ERR_CANT_CREATE; | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 	// configure the HDC to use a compatible pixel format
 | 
					
						
							|  |  |  | 	Error result = _configure_pixel_format(hDC); | 
					
						
							|  |  |  | 	if (result != OK) { | 
					
						
							|  |  |  | 		return result; | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 	GLWindow win; | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	win.hwnd = p_hwnd; | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 	win.hDC = hDC; | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	win.gldisplay_id = _find_or_create_display(win); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (win.gldisplay_id == -1) { | 
					
						
							|  |  |  | 		return FAILED; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 12:03:59 -05:00
										 |  |  | 	// WARNING: `p_window_id` is an eternally growing integer since popup windows keep coming and going
 | 
					
						
							|  |  |  | 	// and each of them has a higher id than the previous, so it must be used in a map not a vector.
 | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 	_windows[p_window_id] = win; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	// make current
 | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 	window_make_current(p_window_id); | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | void GLManagerNative_Windows::window_destroy(DisplayServer::WindowID p_window_id) { | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	GLWindow &win = get_window(p_window_id); | 
					
						
							|  |  |  | 	if (_current_window == &win) { | 
					
						
							|  |  |  | 		_current_window = nullptr; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 	_windows.erase(p_window_id); | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | void GLManagerNative_Windows::release_current() { | 
					
						
							| 
									
										
										
										
											2022-02-16 13:56:32 +01:00
										 |  |  | 	if (!_current_window) { | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2022-02-16 13:56:32 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 	if (!gd_wglMakeCurrent(_current_window->hDC, nullptr)) { | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 		ERR_PRINT("Could not detach OpenGL context from window marked current: " + format_error_message(GetLastError())); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-04-09 11:47:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_current_window = nullptr; | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | void GLManagerNative_Windows::window_make_current(DisplayServer::WindowID p_window_id) { | 
					
						
							| 
									
										
										
										
											2022-02-16 13:56:32 +01:00
										 |  |  | 	if (p_window_id == -1) { | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2022-02-16 13:56:32 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 	// crash if our data structures are out of sync, i.e. not found
 | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	GLWindow &win = _windows[p_window_id]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// noop
 | 
					
						
							| 
									
										
										
										
											2022-02-16 13:56:32 +01:00
										 |  |  | 	if (&win == _current_window) { | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2022-02-16 13:56:32 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	const GLDisplay &disp = get_display(win.gldisplay_id); | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 	if (!gd_wglMakeCurrent(win.hDC, disp.hRC)) { | 
					
						
							| 
									
										
										
										
											2022-05-08 19:25:49 -04:00
										 |  |  | 		ERR_PRINT("Could not switch OpenGL context to other window: " + format_error_message(GetLastError())); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-09 11:47:06 +02:00
										 |  |  | 	_current_window = &win; | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | void GLManagerNative_Windows::swap_buffers() { | 
					
						
							| 
									
										
										
										
											2022-09-12 14:57:11 -07:00
										 |  |  | 	SwapBuffers(_current_window->hDC); | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | Error GLManagerNative_Windows::initialize() { | 
					
						
							| 
									
										
										
										
											2024-06-29 13:02:30 +02:00
										 |  |  | 	_nvapi_setup_profile(); | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | void GLManagerNative_Windows::set_use_vsync(DisplayServer::WindowID p_window_id, bool p_use) { | 
					
						
							| 
									
										
										
										
											2022-11-15 14:14:21 -08:00
										 |  |  | 	GLWindow &win = get_window(p_window_id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (&win != _current_window) { | 
					
						
							|  |  |  | 		window_make_current(p_window_id); | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-11-15 14:14:21 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (wglSwapIntervalEXT) { | 
					
						
							|  |  |  | 		win.use_vsync = p_use; | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (!wglSwapIntervalEXT(p_use ? 1 : 0)) { | 
					
						
							| 
									
										
										
										
											2024-06-16 21:22:10 +02:00
										 |  |  | 			WARN_PRINT_ONCE("Could not set V-Sync mode, as changing V-Sync mode is not supported by the graphics driver."); | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2024-06-16 21:22:10 +02:00
										 |  |  | 		WARN_PRINT_ONCE("Could not set V-Sync mode, as changing V-Sync mode is not supported by the graphics driver."); | 
					
						
							| 
									
										
										
										
											2022-11-15 14:14:21 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | bool GLManagerNative_Windows::is_using_vsync(DisplayServer::WindowID p_window_id) const { | 
					
						
							| 
									
										
										
										
											2022-11-15 14:14:21 -08:00
										 |  |  | 	return get_window(p_window_id).use_vsync; | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | HDC GLManagerNative_Windows::get_hdc(DisplayServer::WindowID p_window_id) { | 
					
						
							| 
									
										
										
										
											2022-10-22 20:29:15 -05:00
										 |  |  | 	return get_window(p_window_id).hDC; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | HGLRC GLManagerNative_Windows::get_hglrc(DisplayServer::WindowID p_window_id) { | 
					
						
							| 
									
										
										
										
											2022-10-22 20:29:15 -05:00
										 |  |  | 	const GLWindow &win = get_window(p_window_id); | 
					
						
							|  |  |  | 	const GLDisplay &disp = get_display(win.gldisplay_id); | 
					
						
							|  |  |  | 	return disp.hRC; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | GLManagerNative_Windows::GLManagerNative_Windows() { | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	direct_render = false; | 
					
						
							|  |  |  | 	glx_minor = glx_major = 0; | 
					
						
							|  |  |  | 	_current_window = nullptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 14:49:49 +02:00
										 |  |  | GLManagerNative_Windows::~GLManagerNative_Windows() { | 
					
						
							| 
									
										
										
										
											2020-11-18 18:11:30 +00:00
										 |  |  | 	release_current(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 14:51:32 +02:00
										 |  |  | #endif // WINDOWS_ENABLED && GLES3_ENABLED
 |