context_gl_windows.cpp 7.9 KB

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