gl_manager_windows_native.cpp 19 KB

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