context_gl_windows.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. void ContextGL_Windows::swap_buffers() {
  58. SwapBuffers(hDC);
  59. }
  60. void ContextGL_Windows::set_use_vsync(bool p_use) {
  61. if (wglSwapIntervalEXT) {
  62. int swap_interval = p_use ? 1 : 0;
  63. wglSwapIntervalEXT(swap_interval);
  64. }
  65. use_vsync = p_use;
  66. }
  67. bool ContextGL_Windows::is_using_vsync() const {
  68. return use_vsync;
  69. }
  70. #define _WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
  71. Error ContextGL_Windows::initialize() {
  72. static PIXELFORMATDESCRIPTOR pfd = {
  73. sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
  74. 1,
  75. PFD_DRAW_TO_WINDOW | // Format Must Support Window
  76. PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
  77. PFD_DOUBLEBUFFER,
  78. (BYTE)PFD_TYPE_RGBA,
  79. (BYTE)(OS::get_singleton()->is_layered_allowed() ? 32 : 24),
  80. (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Color Bits Ignored
  81. (BYTE)(OS::get_singleton()->is_layered_allowed() ? 8 : 0), // Alpha Buffer
  82. (BYTE)0, // Shift Bit Ignored
  83. (BYTE)0, // No Accumulation Buffer
  84. (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Accumulation Bits Ignored
  85. (BYTE)24, // 24Bit Z-Buffer (Depth Buffer)
  86. (BYTE)0, // No Stencil Buffer
  87. (BYTE)0, // No Auxiliary Buffer
  88. (BYTE)PFD_MAIN_PLANE, // Main Drawing Layer
  89. (BYTE)0, // Reserved
  90. 0, 0, 0 // Layer Masks Ignored
  91. };
  92. hDC = GetDC(hWnd);
  93. if (!hDC) {
  94. return ERR_CANT_CREATE; // Return FALSE
  95. }
  96. pixel_format = ChoosePixelFormat(hDC, &pfd);
  97. if (!pixel_format) // Did Windows Find A Matching Pixel Format?
  98. {
  99. return ERR_CANT_CREATE; // Return FALSE
  100. }
  101. BOOL ret = SetPixelFormat(hDC, pixel_format, &pfd);
  102. if (!ret) // Are We Able To Set The Pixel Format?
  103. {
  104. return ERR_CANT_CREATE; // Return FALSE
  105. }
  106. hRC = wglCreateContext(hDC);
  107. if (!hRC) // Are We Able To Get A Rendering Context?
  108. {
  109. return ERR_CANT_CREATE; // Return FALSE
  110. }
  111. wglMakeCurrent(hDC, hRC);
  112. if (opengl_3_context) {
  113. int attribs[] = {
  114. WGL_CONTEXT_MAJOR_VERSION_ARB, 3, //we want a 3.3 context
  115. WGL_CONTEXT_MINOR_VERSION_ARB, 3,
  116. //and it shall be forward compatible so that we can only use up to date functionality
  117. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  118. WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*| _WGL_CONTEXT_DEBUG_BIT_ARB*/,
  119. 0
  120. }; //zero indicates the end of the array
  121. PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = nullptr; //pointer to the method
  122. wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
  123. if (wglCreateContextAttribsARB == nullptr) //OpenGL 3.0 is not supported
  124. {
  125. wglDeleteContext(hRC);
  126. return ERR_CANT_CREATE;
  127. }
  128. HGLRC new_hRC = wglCreateContextAttribsARB(hDC, 0, attribs);
  129. if (!new_hRC) {
  130. wglDeleteContext(hRC);
  131. return ERR_CANT_CREATE; // Return false
  132. }
  133. wglMakeCurrent(hDC, nullptr);
  134. wglDeleteContext(hRC);
  135. hRC = new_hRC;
  136. if (!wglMakeCurrent(hDC, hRC)) // Try To Activate The Rendering Context
  137. {
  138. return ERR_CANT_CREATE; // Return FALSE
  139. }
  140. }
  141. wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
  142. wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC)wglGetProcAddress("wglGetSwapIntervalEXT");
  143. //glWrapperInit(wrapper_get_proc_address);
  144. return OK;
  145. }
  146. ContextGL_Windows::ContextGL_Windows(HWND hwnd, bool p_opengl_3_context) {
  147. opengl_3_context = p_opengl_3_context;
  148. hWnd = hwnd;
  149. use_vsync = false;
  150. pixel_format = 0;
  151. }
  152. ContextGL_Windows::~ContextGL_Windows() {
  153. }
  154. #endif