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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. int GLManager_Windows::_find_or_create_display(GLWindow &win) {
  49. // find display NYI, only 1 supported so far
  50. if (_displays.size())
  51. return 0;
  52. // for (unsigned int n = 0; n < _displays.size(); n++) {
  53. // const GLDisplay &d = _displays[n];
  54. // if (d.x11_display == p_x11_display)
  55. // return n;
  56. // }
  57. // create
  58. GLDisplay d_temp = {};
  59. _displays.push_back(d_temp);
  60. int new_display_id = _displays.size() - 1;
  61. // create context
  62. GLDisplay &d = _displays[new_display_id];
  63. Error err = _create_context(win, d);
  64. if (err != OK) {
  65. // not good
  66. // delete the _display?
  67. _displays.remove(new_display_id);
  68. return -1;
  69. }
  70. return new_display_id;
  71. }
  72. Error GLManager_Windows::_create_context(GLWindow &win, GLDisplay &gl_display) {
  73. static PIXELFORMATDESCRIPTOR pfd = {
  74. sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
  75. 1,
  76. PFD_DRAW_TO_WINDOW | // Format Must Support Window
  77. PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
  78. PFD_DOUBLEBUFFER,
  79. (BYTE)PFD_TYPE_RGBA,
  80. (BYTE)(OS::get_singleton()->is_layered_allowed() ? 32 : 24),
  81. (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Color Bits Ignored
  82. (BYTE)(OS::get_singleton()->is_layered_allowed() ? 8 : 0), // Alpha Buffer
  83. (BYTE)0, // Shift Bit Ignored
  84. (BYTE)0, // No Accumulation Buffer
  85. (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Accumulation Bits Ignored
  86. (BYTE)24, // 24Bit Z-Buffer (Depth Buffer)
  87. (BYTE)0, // No Stencil Buffer
  88. (BYTE)0, // No Auxiliary Buffer
  89. (BYTE)PFD_MAIN_PLANE, // Main Drawing Layer
  90. (BYTE)0, // Reserved
  91. 0, 0, 0 // Layer Masks Ignored
  92. };
  93. // alias
  94. HDC hDC = win.hDC;
  95. int pixel_format = ChoosePixelFormat(hDC, &pfd);
  96. if (!pixel_format) // Did Windows Find A Matching Pixel Format?
  97. {
  98. return ERR_CANT_CREATE; // Return FALSE
  99. }
  100. BOOL ret = SetPixelFormat(hDC, pixel_format, &pfd);
  101. if (!ret) // Are We Able To Set The Pixel Format?
  102. {
  103. return ERR_CANT_CREATE; // Return FALSE
  104. }
  105. gl_display.hRC = wglCreateContext(hDC);
  106. if (!gl_display.hRC) // Are We Able To Get A Rendering Context?
  107. {
  108. return ERR_CANT_CREATE; // Return FALSE
  109. }
  110. wglMakeCurrent(hDC, gl_display.hRC);
  111. int attribs[] = {
  112. WGL_CONTEXT_MAJOR_VERSION_ARB, 3, //we want a 3.3 context
  113. WGL_CONTEXT_MINOR_VERSION_ARB, 3,
  114. //and it shall be forward compatible so that we can only use up to date functionality
  115. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  116. WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*| _WGL_CONTEXT_DEBUG_BIT_ARB*/,
  117. 0
  118. }; //zero indicates the end of the array
  119. PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = nullptr; //pointer to the method
  120. wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
  121. if (wglCreateContextAttribsARB == nullptr) //OpenGL 3.0 is not supported
  122. {
  123. wglDeleteContext(gl_display.hRC);
  124. gl_display.hRC = 0;
  125. return ERR_CANT_CREATE;
  126. }
  127. HGLRC new_hRC = wglCreateContextAttribsARB(hDC, 0, attribs);
  128. if (!new_hRC) {
  129. wglDeleteContext(gl_display.hRC);
  130. gl_display.hRC = 0;
  131. return ERR_CANT_CREATE; // Return false
  132. }
  133. wglMakeCurrent(hDC, nullptr);
  134. wglDeleteContext(gl_display.hRC);
  135. gl_display.hRC = new_hRC;
  136. if (!wglMakeCurrent(hDC, gl_display.hRC)) // Try To Activate The Rendering Context
  137. {
  138. wglDeleteContext(gl_display.hRC);
  139. gl_display.hRC = 0;
  140. return ERR_CANT_CREATE; // Return FALSE
  141. }
  142. return OK;
  143. }
  144. Error GLManager_Windows::window_create(DisplayServer::WindowID p_window_id, HWND p_hwnd, HINSTANCE p_hinstance, int p_width, int p_height) {
  145. HDC hdc = GetDC(p_hwnd);
  146. if (!hdc) {
  147. return ERR_CANT_CREATE; // Return FALSE
  148. }
  149. // make sure vector is big enough...
  150. // we can mirror the external vector, it is simpler
  151. // to keep the IDs identical for fast lookup
  152. if (p_window_id >= (int)_windows.size()) {
  153. _windows.resize(p_window_id + 1);
  154. }
  155. GLWindow &win = _windows[p_window_id];
  156. win.in_use = true;
  157. win.window_id = p_window_id;
  158. win.width = p_width;
  159. win.height = p_height;
  160. win.hwnd = p_hwnd;
  161. win.hDC = hdc;
  162. win.gldisplay_id = _find_or_create_display(win);
  163. if (win.gldisplay_id == -1) {
  164. // release DC?
  165. _windows.remove(_windows.size() - 1);
  166. return FAILED;
  167. }
  168. // the display could be invalid .. check NYI
  169. GLDisplay &gl_display = _displays[win.gldisplay_id];
  170. // make current
  171. window_make_current(_windows.size() - 1);
  172. return OK;
  173. }
  174. void GLManager_Windows::_internal_set_current_window(GLWindow *p_win) {
  175. _current_window = p_win;
  176. }
  177. void GLManager_Windows::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
  178. get_window(p_window_id).width = p_width;
  179. get_window(p_window_id).height = p_height;
  180. }
  181. int GLManager_Windows::window_get_width(DisplayServer::WindowID p_window_id) {
  182. return get_window(p_window_id).width;
  183. }
  184. int GLManager_Windows::window_get_height(DisplayServer::WindowID p_window_id) {
  185. return get_window(p_window_id).height;
  186. }
  187. void GLManager_Windows::window_destroy(DisplayServer::WindowID p_window_id) {
  188. GLWindow &win = get_window(p_window_id);
  189. win.in_use = false;
  190. if (_current_window == &win) {
  191. _current_window = nullptr;
  192. }
  193. }
  194. void GLManager_Windows::release_current() {
  195. if (!_current_window)
  196. return;
  197. wglMakeCurrent(_current_window->hDC, nullptr);
  198. }
  199. void GLManager_Windows::window_make_current(DisplayServer::WindowID p_window_id) {
  200. if (p_window_id == -1)
  201. return;
  202. GLWindow &win = _windows[p_window_id];
  203. if (!win.in_use)
  204. return;
  205. // noop
  206. if (&win == _current_window)
  207. return;
  208. const GLDisplay &disp = get_display(win.gldisplay_id);
  209. wglMakeCurrent(win.hDC, disp.hRC);
  210. _internal_set_current_window(&win);
  211. }
  212. void GLManager_Windows::make_current() {
  213. if (!_current_window)
  214. return;
  215. if (!_current_window->in_use) {
  216. WARN_PRINT("current window not in use!");
  217. return;
  218. }
  219. const GLDisplay &disp = get_current_display();
  220. wglMakeCurrent(_current_window->hDC, disp.hRC);
  221. }
  222. void GLManager_Windows::swap_buffers() {
  223. // NO NEED TO CALL SWAP BUFFERS for each window...
  224. // see https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glXSwapBuffers.xml
  225. if (!_current_window)
  226. return;
  227. if (!_current_window->in_use) {
  228. WARN_PRINT("current window not in use!");
  229. return;
  230. }
  231. // print_line("\tswap_buffers");
  232. // only for debugging without drawing anything
  233. // glClearColor(Math::randf(), 0, 1, 1);
  234. //glClear(GL_COLOR_BUFFER_BIT);
  235. // const GLDisplay &disp = get_current_display();
  236. SwapBuffers(_current_window->hDC);
  237. }
  238. Error GLManager_Windows::initialize() {
  239. wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
  240. wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC)wglGetProcAddress("wglGetSwapIntervalEXT");
  241. //glWrapperInit(wrapper_get_proc_address);
  242. return OK;
  243. }
  244. void GLManager_Windows::set_use_vsync(bool p_use) {
  245. /*
  246. static bool setup = false;
  247. static PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = nullptr;
  248. static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalMESA = nullptr;
  249. static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI = nullptr;
  250. if (!setup) {
  251. setup = true;
  252. String extensions = glXQueryExtensionsString(x11_display, DefaultScreen(x11_display));
  253. if (extensions.find("GLX_EXT_swap_control") != -1)
  254. glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalEXT");
  255. if (extensions.find("GLX_MESA_swap_control") != -1)
  256. glXSwapIntervalMESA = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalMESA");
  257. if (extensions.find("GLX_SGI_swap_control") != -1)
  258. glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalSGI");
  259. }
  260. int val = p_use ? 1 : 0;
  261. if (glXSwapIntervalMESA) {
  262. glXSwapIntervalMESA(val);
  263. } else if (glXSwapIntervalSGI) {
  264. glXSwapIntervalSGI(val);
  265. } else if (glXSwapIntervalEXT) {
  266. GLXDrawable drawable = glXGetCurrentDrawable();
  267. glXSwapIntervalEXT(x11_display, drawable, val);
  268. } else
  269. return;
  270. use_vsync = p_use;
  271. */
  272. }
  273. bool GLManager_Windows::is_using_vsync() const {
  274. return use_vsync;
  275. }
  276. GLManager_Windows::GLManager_Windows(ContextType p_context_type) {
  277. context_type = p_context_type;
  278. direct_render = false;
  279. glx_minor = glx_major = 0;
  280. use_vsync = false;
  281. _current_window = nullptr;
  282. }
  283. GLManager_Windows::~GLManager_Windows() {
  284. release_current();
  285. }
  286. #endif // GLES3_ENABLED
  287. #endif // WINDOWS