context_gl_windows.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*************************************************************************/
  2. /* context_gl_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. #if defined(OPENGL_ENABLED) || defined(GLES_ENABLED)
  31. // Author: Juan Linietsky <reduzio@gmail.com>, (C) 2008
  32. #include "context_gl_windows.h"
  33. #include <dwmapi.h>
  34. #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
  35. #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
  36. #define WGL_CONTEXT_FLAGS_ARB 0x2094
  37. #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002
  38. #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
  39. #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
  40. #if defined(__GNUC__)
  41. // Workaround GCC warning from -Wcast-function-type.
  42. #define wglGetProcAddress (void *)wglGetProcAddress
  43. #endif
  44. typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC, const int *);
  45. void ContextGL_Windows::release_current() {
  46. wglMakeCurrent(hDC, NULL);
  47. }
  48. void ContextGL_Windows::make_current() {
  49. wglMakeCurrent(hDC, hRC);
  50. }
  51. HDC ContextGL_Windows::get_hdc() {
  52. return hDC;
  53. }
  54. HGLRC ContextGL_Windows::get_hglrc() {
  55. return hRC;
  56. }
  57. int ContextGL_Windows::get_window_width() {
  58. return OS::get_singleton()->get_video_mode().width;
  59. }
  60. int ContextGL_Windows::get_window_height() {
  61. return OS::get_singleton()->get_video_mode().height;
  62. }
  63. bool ContextGL_Windows::should_vsync_via_compositor() {
  64. if (OS::get_singleton()->is_window_fullscreen() || !OS::get_singleton()->is_vsync_via_compositor_enabled()) {
  65. return false;
  66. }
  67. // Note: All Windows versions supported by Godot have a compositor.
  68. // It can be disabled on earlier Windows versions.
  69. BOOL dwm_enabled;
  70. if (SUCCEEDED(DwmIsCompositionEnabled(&dwm_enabled))) {
  71. return dwm_enabled;
  72. }
  73. return false;
  74. }
  75. void ContextGL_Windows::swap_buffers() {
  76. SwapBuffers(hDC);
  77. if (use_vsync) {
  78. bool vsync_via_compositor_now = should_vsync_via_compositor();
  79. if (vsync_via_compositor_now && wglGetSwapIntervalEXT() == 0) {
  80. DwmFlush();
  81. }
  82. if (vsync_via_compositor_now != vsync_via_compositor) {
  83. // The previous frame had a different operating mode than this
  84. // frame. Set the 'vsync_via_compositor' member variable and the
  85. // OpenGL swap interval to their proper values.
  86. set_use_vsync(true);
  87. }
  88. }
  89. }
  90. void ContextGL_Windows::set_use_vsync(bool p_use) {
  91. vsync_via_compositor = p_use && should_vsync_via_compositor();
  92. if (wglSwapIntervalEXT) {
  93. int swap_interval = (p_use && !vsync_via_compositor) ? 1 : 0;
  94. wglSwapIntervalEXT(swap_interval);
  95. }
  96. use_vsync = p_use;
  97. }
  98. bool ContextGL_Windows::is_using_vsync() const {
  99. return use_vsync;
  100. }
  101. #define _WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
  102. Error ContextGL_Windows::initialize() {
  103. static PIXELFORMATDESCRIPTOR pfd = {
  104. sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
  105. 1,
  106. PFD_DRAW_TO_WINDOW | // Format Must Support Window
  107. PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
  108. PFD_DOUBLEBUFFER,
  109. (BYTE)PFD_TYPE_RGBA,
  110. (BYTE)(OS::get_singleton()->is_layered_allowed() ? 32 : 24),
  111. (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Color Bits Ignored
  112. (BYTE)(OS::get_singleton()->is_layered_allowed() ? 8 : 0), // Alpha Buffer
  113. (BYTE)0, // Shift Bit Ignored
  114. (BYTE)0, // No Accumulation Buffer
  115. (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Accumulation Bits Ignored
  116. (BYTE)24, // 24Bit Z-Buffer (Depth Buffer)
  117. (BYTE)0, // No Stencil Buffer
  118. (BYTE)0, // No Auxiliary Buffer
  119. (BYTE)PFD_MAIN_PLANE, // Main Drawing Layer
  120. (BYTE)0, // Reserved
  121. 0, 0, 0 // Layer Masks Ignored
  122. };
  123. hDC = GetDC(hWnd);
  124. if (!hDC) {
  125. return ERR_CANT_CREATE; // Return FALSE
  126. }
  127. pixel_format = ChoosePixelFormat(hDC, &pfd);
  128. if (!pixel_format) // Did Windows Find A Matching Pixel Format?
  129. {
  130. return ERR_CANT_CREATE; // Return FALSE
  131. }
  132. BOOL ret = SetPixelFormat(hDC, pixel_format, &pfd);
  133. if (!ret) // Are We Able To Set The Pixel Format?
  134. {
  135. return ERR_CANT_CREATE; // Return FALSE
  136. }
  137. hRC = wglCreateContext(hDC);
  138. if (!hRC) // Are We Able To Get A Rendering Context?
  139. {
  140. return ERR_CANT_CREATE; // Return FALSE
  141. }
  142. wglMakeCurrent(hDC, hRC);
  143. if (opengl_3_context) {
  144. int attribs[] = {
  145. WGL_CONTEXT_MAJOR_VERSION_ARB, 3, //we want a 3.3 context
  146. WGL_CONTEXT_MINOR_VERSION_ARB, 3,
  147. //and it shall be forward compatible so that we can only use up to date functionality
  148. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  149. WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*| _WGL_CONTEXT_DEBUG_BIT_ARB*/,
  150. 0
  151. }; //zero indicates the end of the array
  152. PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = NULL; //pointer to the method
  153. wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
  154. if (wglCreateContextAttribsARB == NULL) //OpenGL 3.0 is not supported
  155. {
  156. wglDeleteContext(hRC);
  157. return ERR_CANT_CREATE;
  158. }
  159. HGLRC new_hRC = wglCreateContextAttribsARB(hDC, 0, attribs);
  160. if (!new_hRC) {
  161. wglDeleteContext(hRC);
  162. return ERR_CANT_CREATE; // Return false
  163. }
  164. wglMakeCurrent(hDC, NULL);
  165. wglDeleteContext(hRC);
  166. hRC = new_hRC;
  167. if (!wglMakeCurrent(hDC, hRC)) // Try To Activate The Rendering Context
  168. {
  169. return ERR_CANT_CREATE; // Return FALSE
  170. }
  171. }
  172. wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
  173. wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC)wglGetProcAddress("wglGetSwapIntervalEXT");
  174. //glWrapperInit(wrapper_get_proc_address);
  175. return OK;
  176. }
  177. ContextGL_Windows::ContextGL_Windows(HWND hwnd, bool p_opengl_3_context) {
  178. opengl_3_context = p_opengl_3_context;
  179. hWnd = hwnd;
  180. use_vsync = false;
  181. vsync_via_compositor = false;
  182. }
  183. ContextGL_Windows::~ContextGL_Windows() {
  184. }
  185. #endif