context_gl_windows.cpp 7.5 KB

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