2
0

gl_manager_windows.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*************************************************************************/
  2. /* gl_manager_windows.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifdef WINDOWS_ENABLED
  32. #ifdef GLES3_ENABLED
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <dwmapi.h>
  36. #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
  37. #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
  38. #define WGL_CONTEXT_FLAGS_ARB 0x2094
  39. #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002
  40. #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
  41. #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
  42. #define _WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
  43. #if defined(__GNUC__)
  44. // Workaround GCC warning from -Wcast-function-type.
  45. #define wglGetProcAddress (void *)wglGetProcAddress
  46. #endif
  47. typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC, const int *);
  48. static String format_error_message(DWORD id) {
  49. LPWSTR messageBuffer = nullptr;
  50. size_t size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  51. nullptr, id, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, nullptr);
  52. String msg = "Error " + itos(id) + ": " + String::utf16((const char16_t *)messageBuffer, size);
  53. LocalFree(messageBuffer);
  54. return msg;
  55. }
  56. int GLManager_Windows::_find_or_create_display(GLWindow &win) {
  57. // find display NYI, only 1 supported so far
  58. if (_displays.size()) {
  59. return 0;
  60. }
  61. // create
  62. GLDisplay d_temp = {};
  63. _displays.push_back(d_temp);
  64. int new_display_id = _displays.size() - 1;
  65. // create context
  66. GLDisplay &d = _displays[new_display_id];
  67. Error err = _create_context(win, d);
  68. if (err != OK) {
  69. // not good
  70. // delete the _display?
  71. _displays.remove_at(new_display_id);
  72. return -1;
  73. }
  74. return new_display_id;
  75. }
  76. static Error _configure_pixel_format(HDC hDC) {
  77. static PIXELFORMATDESCRIPTOR pfd = {
  78. sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
  79. 1,
  80. PFD_DRAW_TO_WINDOW | // Format Must Support Window
  81. PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
  82. PFD_DOUBLEBUFFER,
  83. (BYTE)PFD_TYPE_RGBA,
  84. (BYTE)(OS::get_singleton()->is_layered_allowed() ? 32 : 24),
  85. (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Color Bits Ignored
  86. (BYTE)(OS::get_singleton()->is_layered_allowed() ? 8 : 0), // Alpha Buffer
  87. (BYTE)0, // Shift Bit Ignored
  88. (BYTE)0, // No Accumulation Buffer
  89. (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Accumulation Bits Ignored
  90. (BYTE)24, // 24Bit Z-Buffer (Depth Buffer)
  91. (BYTE)0, // No Stencil Buffer
  92. (BYTE)0, // No Auxiliary Buffer
  93. (BYTE)PFD_MAIN_PLANE, // Main Drawing Layer
  94. (BYTE)0, // Reserved
  95. 0, 0, 0 // Layer Masks Ignored
  96. };
  97. int pixel_format = ChoosePixelFormat(hDC, &pfd);
  98. if (!pixel_format) // Did Windows Find A Matching Pixel Format?
  99. {
  100. return ERR_CANT_CREATE; // Return FALSE
  101. }
  102. BOOL ret = SetPixelFormat(hDC, pixel_format, &pfd);
  103. if (!ret) // Are We Able To Set The Pixel Format?
  104. {
  105. return ERR_CANT_CREATE; // Return FALSE
  106. }
  107. return OK;
  108. }
  109. Error GLManager_Windows::_create_context(GLWindow &win, GLDisplay &gl_display) {
  110. Error err = _configure_pixel_format(win.hDC);
  111. if (err != OK) {
  112. return err;
  113. }
  114. gl_display.hRC = wglCreateContext(win.hDC);
  115. if (!gl_display.hRC) // Are We Able To Get A Rendering Context?
  116. {
  117. return ERR_CANT_CREATE; // Return FALSE
  118. }
  119. if (!wglMakeCurrent(win.hDC, gl_display.hRC)) {
  120. ERR_PRINT("Could not attach OpenGL context to newly created window: " + format_error_message(GetLastError()));
  121. }
  122. int attribs[] = {
  123. WGL_CONTEXT_MAJOR_VERSION_ARB, 3, //we want a 3.3 context
  124. WGL_CONTEXT_MINOR_VERSION_ARB, 3,
  125. //and it shall be forward compatible so that we can only use up to date functionality
  126. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  127. WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*| _WGL_CONTEXT_DEBUG_BIT_ARB*/,
  128. 0
  129. }; //zero indicates the end of the array
  130. PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = nullptr; //pointer to the method
  131. wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
  132. if (wglCreateContextAttribsARB == nullptr) //OpenGL 3.0 is not supported
  133. {
  134. wglDeleteContext(gl_display.hRC);
  135. gl_display.hRC = 0;
  136. return ERR_CANT_CREATE;
  137. }
  138. HGLRC new_hRC = wglCreateContextAttribsARB(win.hDC, 0, attribs);
  139. if (!new_hRC) {
  140. wglDeleteContext(gl_display.hRC);
  141. gl_display.hRC = 0;
  142. return ERR_CANT_CREATE;
  143. }
  144. if (!wglMakeCurrent(win.hDC, nullptr)) {
  145. ERR_PRINT("Could not detach OpenGL context from newly created window: " + format_error_message(GetLastError()));
  146. }
  147. wglDeleteContext(gl_display.hRC);
  148. gl_display.hRC = new_hRC;
  149. if (!wglMakeCurrent(win.hDC, gl_display.hRC)) // Try To Activate The Rendering Context
  150. {
  151. ERR_PRINT("Could not attach OpenGL context to newly created window with replaced OpenGL context: " + format_error_message(GetLastError()));
  152. wglDeleteContext(gl_display.hRC);
  153. gl_display.hRC = 0;
  154. return ERR_CANT_CREATE;
  155. }
  156. return OK;
  157. }
  158. Error GLManager_Windows::window_create(DisplayServer::WindowID p_window_id, HWND p_hwnd, HINSTANCE p_hinstance, int p_width, int p_height) {
  159. HDC hDC = GetDC(p_hwnd);
  160. if (!hDC) {
  161. return ERR_CANT_CREATE;
  162. }
  163. // configure the HDC to use a compatible pixel format
  164. Error result = _configure_pixel_format(hDC);
  165. if (result != OK) {
  166. return result;
  167. }
  168. GLWindow win;
  169. win.width = p_width;
  170. win.height = p_height;
  171. win.hwnd = p_hwnd;
  172. win.hDC = hDC;
  173. win.gldisplay_id = _find_or_create_display(win);
  174. if (win.gldisplay_id == -1) {
  175. return FAILED;
  176. }
  177. // WARNING: p_window_id is an eternally growing integer since popup windows keep coming and going
  178. // and each of them has a higher id than the previous, so it must be used in a map not a vector
  179. _windows[p_window_id] = win;
  180. // make current
  181. window_make_current(p_window_id);
  182. return OK;
  183. }
  184. void GLManager_Windows::_internal_set_current_window(GLWindow *p_win) {
  185. _current_window = p_win;
  186. }
  187. void GLManager_Windows::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
  188. get_window(p_window_id).width = p_width;
  189. get_window(p_window_id).height = p_height;
  190. }
  191. int GLManager_Windows::window_get_width(DisplayServer::WindowID p_window_id) {
  192. return get_window(p_window_id).width;
  193. }
  194. int GLManager_Windows::window_get_height(DisplayServer::WindowID p_window_id) {
  195. return get_window(p_window_id).height;
  196. }
  197. void GLManager_Windows::window_destroy(DisplayServer::WindowID p_window_id) {
  198. GLWindow &win = get_window(p_window_id);
  199. if (_current_window == &win) {
  200. _current_window = nullptr;
  201. }
  202. _windows.erase(p_window_id);
  203. }
  204. void GLManager_Windows::release_current() {
  205. if (!_current_window) {
  206. return;
  207. }
  208. if (!wglMakeCurrent(_current_window->hDC, nullptr)) {
  209. ERR_PRINT("Could not detach OpenGL context from window marked current: " + format_error_message(GetLastError()));
  210. }
  211. }
  212. void GLManager_Windows::window_make_current(DisplayServer::WindowID p_window_id) {
  213. if (p_window_id == -1) {
  214. return;
  215. }
  216. // crash if our data structures are out of sync, i.e. not found
  217. GLWindow &win = _windows[p_window_id];
  218. // noop
  219. if (&win == _current_window) {
  220. return;
  221. }
  222. const GLDisplay &disp = get_display(win.gldisplay_id);
  223. if (!wglMakeCurrent(win.hDC, disp.hRC)) {
  224. ERR_PRINT("Could not switch OpenGL context to other window: " + format_error_message(GetLastError()));
  225. }
  226. _internal_set_current_window(&win);
  227. }
  228. void GLManager_Windows::make_current() {
  229. if (!_current_window) {
  230. return;
  231. }
  232. const GLDisplay &disp = get_current_display();
  233. if (!wglMakeCurrent(_current_window->hDC, disp.hRC)) {
  234. ERR_PRINT("Could not switch OpenGL context to window marked current: " + format_error_message(GetLastError()));
  235. }
  236. }
  237. void GLManager_Windows::swap_buffers() {
  238. SwapBuffers(_current_window->hDC);
  239. }
  240. Error GLManager_Windows::initialize() {
  241. wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
  242. wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC)wglGetProcAddress("wglGetSwapIntervalEXT");
  243. //glWrapperInit(wrapper_get_proc_address);
  244. return OK;
  245. }
  246. void GLManager_Windows::set_use_vsync(bool p_use) {
  247. /*
  248. static bool setup = false;
  249. static PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = nullptr;
  250. static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalMESA = nullptr;
  251. static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI = nullptr;
  252. if (!setup) {
  253. setup = true;
  254. String extensions = glXQueryExtensionsString(x11_display, DefaultScreen(x11_display));
  255. if (extensions.find("GLX_EXT_swap_control") != -1) {
  256. glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalEXT");
  257. }
  258. if (extensions.find("GLX_MESA_swap_control") != -1) {
  259. glXSwapIntervalMESA = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalMESA");
  260. }
  261. if (extensions.find("GLX_SGI_swap_control") != -1) {
  262. glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalSGI");
  263. }
  264. }
  265. int val = p_use ? 1 : 0;
  266. if (glXSwapIntervalMESA) {
  267. glXSwapIntervalMESA(val);
  268. } else if (glXSwapIntervalSGI) {
  269. glXSwapIntervalSGI(val);
  270. } else if (glXSwapIntervalEXT) {
  271. GLXDrawable drawable = glXGetCurrentDrawable();
  272. glXSwapIntervalEXT(x11_display, drawable, val);
  273. } else {
  274. return;
  275. }
  276. use_vsync = p_use;
  277. */
  278. }
  279. bool GLManager_Windows::is_using_vsync() const {
  280. return use_vsync;
  281. }
  282. HDC GLManager_Windows::get_hdc(DisplayServer::WindowID p_window_id) {
  283. return get_window(p_window_id).hDC;
  284. }
  285. HGLRC GLManager_Windows::get_hglrc(DisplayServer::WindowID p_window_id) {
  286. const GLWindow &win = get_window(p_window_id);
  287. const GLDisplay &disp = get_display(win.gldisplay_id);
  288. return disp.hRC;
  289. }
  290. GLManager_Windows::GLManager_Windows(ContextType p_context_type) {
  291. context_type = p_context_type;
  292. direct_render = false;
  293. glx_minor = glx_major = 0;
  294. use_vsync = false;
  295. _current_window = nullptr;
  296. }
  297. GLManager_Windows::~GLManager_Windows() {
  298. release_current();
  299. }
  300. #endif // GLES3_ENABLED
  301. #endif // WINDOWS