gl_manager_windows.cpp 18 KB

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