context_gl_win.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*************************************************************************/
  2. /* context_gl_win.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_win.h"
  33. #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
  34. #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
  35. #define WGL_CONTEXT_FLAGS_ARB 0x2094
  36. #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002
  37. #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
  38. #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
  39. typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC, const int *);
  40. void ContextGL_Win::release_current() {
  41. wglMakeCurrent(hDC, NULL);
  42. }
  43. void ContextGL_Win::make_current() {
  44. wglMakeCurrent(hDC, hRC);
  45. }
  46. int ContextGL_Win::get_window_width() {
  47. return OS::get_singleton()->get_video_mode().width;
  48. }
  49. int ContextGL_Win::get_window_height() {
  50. return OS::get_singleton()->get_video_mode().height;
  51. }
  52. void ContextGL_Win::swap_buffers() {
  53. SwapBuffers(hDC);
  54. }
  55. void ContextGL_Win::set_use_vsync(bool p_use) {
  56. if (wglSwapIntervalEXT) {
  57. wglSwapIntervalEXT(p_use ? 1 : 0);
  58. }
  59. use_vsync = p_use;
  60. }
  61. bool ContextGL_Win::is_using_vsync() const {
  62. return use_vsync;
  63. }
  64. #define _WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
  65. Error ContextGL_Win::initialize() {
  66. static PIXELFORMATDESCRIPTOR pfd = {
  67. sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
  68. 1,
  69. PFD_DRAW_TO_WINDOW | // Format Must Support Window
  70. PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
  71. PFD_DOUBLEBUFFER,
  72. (BYTE)PFD_TYPE_RGBA,
  73. (BYTE)(OS::get_singleton()->is_layered_allowed() ? 32 : 24),
  74. (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Color Bits Ignored
  75. (BYTE)(OS::get_singleton()->is_layered_allowed() ? 8 : 0), // Alpha Buffer
  76. (BYTE)0, // Shift Bit Ignored
  77. (BYTE)0, // No Accumulation Buffer
  78. (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Accumulation Bits Ignored
  79. (BYTE)24, // 24Bit Z-Buffer (Depth Buffer)
  80. (BYTE)0, // No Stencil Buffer
  81. (BYTE)0, // No Auxiliary Buffer
  82. (BYTE)PFD_MAIN_PLANE, // Main Drawing Layer
  83. (BYTE)0, // Reserved
  84. 0, 0, 0 // Layer Masks Ignored
  85. };
  86. hDC = GetDC(hWnd);
  87. if (!hDC) {
  88. return ERR_CANT_CREATE; // Return FALSE
  89. }
  90. pixel_format = ChoosePixelFormat(hDC, &pfd);
  91. if (!pixel_format) // Did Windows Find A Matching Pixel Format?
  92. {
  93. return ERR_CANT_CREATE; // Return FALSE
  94. }
  95. BOOL ret = SetPixelFormat(hDC, pixel_format, &pfd);
  96. if (!ret) // Are We Able To Set The Pixel Format?
  97. {
  98. return ERR_CANT_CREATE; // Return FALSE
  99. }
  100. hRC = wglCreateContext(hDC);
  101. if (!hRC) // Are We Able To Get A Rendering Context?
  102. {
  103. return ERR_CANT_CREATE; // Return FALSE
  104. }
  105. wglMakeCurrent(hDC, hRC);
  106. if (opengl_3_context) {
  107. int attribs[] = {
  108. WGL_CONTEXT_MAJOR_VERSION_ARB, 3, //we want a 3.3 context
  109. WGL_CONTEXT_MINOR_VERSION_ARB, 3,
  110. //and it shall be forward compatible so that we can only use up to date functionality
  111. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  112. WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*| _WGL_CONTEXT_DEBUG_BIT_ARB*/,
  113. 0
  114. }; //zero indicates the end of the array
  115. PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = NULL; //pointer to the method
  116. wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
  117. if (wglCreateContextAttribsARB == NULL) //OpenGL 3.0 is not supported
  118. {
  119. wglDeleteContext(hRC);
  120. return ERR_CANT_CREATE;
  121. }
  122. HGLRC new_hRC = wglCreateContextAttribsARB(hDC, 0, attribs);
  123. if (!new_hRC) {
  124. wglDeleteContext(hRC);
  125. return ERR_CANT_CREATE; // Return false
  126. }
  127. wglMakeCurrent(hDC, NULL);
  128. wglDeleteContext(hRC);
  129. hRC = new_hRC;
  130. if (!wglMakeCurrent(hDC, hRC)) // Try To Activate The Rendering Context
  131. {
  132. return ERR_CANT_CREATE; // Return FALSE
  133. }
  134. }
  135. wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
  136. //glWrapperInit(wrapper_get_proc_address);
  137. return OK;
  138. }
  139. ContextGL_Win::ContextGL_Win(HWND hwnd, bool p_opengl_3_context) {
  140. opengl_3_context = p_opengl_3_context;
  141. hWnd = hwnd;
  142. use_vsync = false;
  143. }
  144. ContextGL_Win::~ContextGL_Win() {
  145. }
  146. #endif