gl_manager_windows.cpp 19 KB

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