gl_manager_windows.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. #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. if (!wglSwapIntervalEXT) {
  157. wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
  158. }
  159. return OK;
  160. }
  161. Error GLManager_Windows::window_create(DisplayServer::WindowID p_window_id, HWND p_hwnd, HINSTANCE p_hinstance, int p_width, int p_height) {
  162. HDC hDC = GetDC(p_hwnd);
  163. if (!hDC) {
  164. return ERR_CANT_CREATE;
  165. }
  166. // configure the HDC to use a compatible pixel format
  167. Error result = _configure_pixel_format(hDC);
  168. if (result != OK) {
  169. return result;
  170. }
  171. GLWindow win;
  172. win.width = p_width;
  173. win.height = p_height;
  174. win.hwnd = p_hwnd;
  175. win.hDC = hDC;
  176. win.gldisplay_id = _find_or_create_display(win);
  177. if (win.gldisplay_id == -1) {
  178. return FAILED;
  179. }
  180. // WARNING: p_window_id is an eternally growing integer since popup windows keep coming and going
  181. // and each of them has a higher id than the previous, so it must be used in a map not a vector
  182. _windows[p_window_id] = win;
  183. // make current
  184. window_make_current(p_window_id);
  185. return OK;
  186. }
  187. void GLManager_Windows::_internal_set_current_window(GLWindow *p_win) {
  188. _current_window = p_win;
  189. }
  190. void GLManager_Windows::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
  191. get_window(p_window_id).width = p_width;
  192. get_window(p_window_id).height = p_height;
  193. }
  194. int GLManager_Windows::window_get_width(DisplayServer::WindowID p_window_id) {
  195. return get_window(p_window_id).width;
  196. }
  197. int GLManager_Windows::window_get_height(DisplayServer::WindowID p_window_id) {
  198. return get_window(p_window_id).height;
  199. }
  200. void GLManager_Windows::window_destroy(DisplayServer::WindowID p_window_id) {
  201. GLWindow &win = get_window(p_window_id);
  202. if (_current_window == &win) {
  203. _current_window = nullptr;
  204. }
  205. _windows.erase(p_window_id);
  206. }
  207. void GLManager_Windows::release_current() {
  208. if (!_current_window) {
  209. return;
  210. }
  211. if (!wglMakeCurrent(_current_window->hDC, nullptr)) {
  212. ERR_PRINT("Could not detach OpenGL context from window marked current: " + format_error_message(GetLastError()));
  213. }
  214. }
  215. void GLManager_Windows::window_make_current(DisplayServer::WindowID p_window_id) {
  216. if (p_window_id == -1) {
  217. return;
  218. }
  219. // crash if our data structures are out of sync, i.e. not found
  220. GLWindow &win = _windows[p_window_id];
  221. // noop
  222. if (&win == _current_window) {
  223. return;
  224. }
  225. const GLDisplay &disp = get_display(win.gldisplay_id);
  226. if (!wglMakeCurrent(win.hDC, disp.hRC)) {
  227. ERR_PRINT("Could not switch OpenGL context to other window: " + format_error_message(GetLastError()));
  228. }
  229. _internal_set_current_window(&win);
  230. }
  231. void GLManager_Windows::make_current() {
  232. if (!_current_window) {
  233. return;
  234. }
  235. const GLDisplay &disp = get_current_display();
  236. if (!wglMakeCurrent(_current_window->hDC, disp.hRC)) {
  237. ERR_PRINT("Could not switch OpenGL context to window marked current: " + format_error_message(GetLastError()));
  238. }
  239. }
  240. void GLManager_Windows::swap_buffers() {
  241. SwapBuffers(_current_window->hDC);
  242. }
  243. Error GLManager_Windows::initialize() {
  244. return OK;
  245. }
  246. void GLManager_Windows::set_use_vsync(DisplayServer::WindowID p_window_id, bool p_use) {
  247. GLWindow &win = get_window(p_window_id);
  248. GLWindow *current = _current_window;
  249. if (&win != _current_window) {
  250. window_make_current(p_window_id);
  251. }
  252. if (wglSwapIntervalEXT) {
  253. win.use_vsync = p_use;
  254. wglSwapIntervalEXT(p_use ? 1 : 0);
  255. }
  256. if (current != _current_window) {
  257. _current_window = current;
  258. make_current();
  259. }
  260. }
  261. bool GLManager_Windows::is_using_vsync(DisplayServer::WindowID p_window_id) const {
  262. return get_window(p_window_id).use_vsync;
  263. }
  264. HDC GLManager_Windows::get_hdc(DisplayServer::WindowID p_window_id) {
  265. return get_window(p_window_id).hDC;
  266. }
  267. HGLRC GLManager_Windows::get_hglrc(DisplayServer::WindowID p_window_id) {
  268. const GLWindow &win = get_window(p_window_id);
  269. const GLDisplay &disp = get_display(win.gldisplay_id);
  270. return disp.hRC;
  271. }
  272. GLManager_Windows::GLManager_Windows(ContextType p_context_type) {
  273. context_type = p_context_type;
  274. direct_render = false;
  275. glx_minor = glx_major = 0;
  276. _current_window = nullptr;
  277. }
  278. GLManager_Windows::~GLManager_Windows() {
  279. release_current();
  280. }
  281. #endif // GLES3_ENABLED
  282. #endif // WINDOWS