wgl_detect_version.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**************************************************************************/
  2. /* wgl_detect_version.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(WINDOWS_ENABLED) && defined(GLES3_ENABLED)
  31. #include "wgl_detect_version.h"
  32. #include "os_windows.h"
  33. #include "core/string/print_string.h"
  34. #include "core/string/ustring.h"
  35. #include "core/variant/dictionary.h"
  36. #include <windows.h>
  37. #include <dwmapi.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
  41. #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
  42. #define WGL_CONTEXT_FLAGS_ARB 0x2094
  43. #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002
  44. #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
  45. #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
  46. #define WGL_VENDOR 0x1F00
  47. #define WGL_RENDERER 0x1F01
  48. #define WGL_VERSION 0x1F02
  49. #if defined(__GNUC__)
  50. // Workaround GCC warning from -Wcast-function-type.
  51. #define GetProcAddress (void *)GetProcAddress
  52. #endif
  53. typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXT)(HDC);
  54. typedef BOOL(APIENTRY *PFNWGLDELETECONTEXT)(HGLRC);
  55. typedef BOOL(APIENTRY *PFNWGLMAKECURRENT)(HDC, HGLRC);
  56. typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC, const int *);
  57. typedef void *(APIENTRY *PFNWGLGETPROCADDRESS)(LPCSTR);
  58. typedef const char *(APIENTRY *PFNWGLGETSTRINGPROC)(unsigned int);
  59. Dictionary detect_wgl() {
  60. Dictionary gl_info;
  61. gl_info["version"] = 0;
  62. gl_info["vendor"] = String();
  63. gl_info["name"] = String();
  64. PFNWGLCREATECONTEXT gd_wglCreateContext;
  65. PFNWGLMAKECURRENT gd_wglMakeCurrent;
  66. PFNWGLDELETECONTEXT gd_wglDeleteContext;
  67. PFNWGLGETPROCADDRESS gd_wglGetProcAddress;
  68. HMODULE module = LoadLibraryW(L"opengl32.dll");
  69. if (!module) {
  70. return gl_info;
  71. }
  72. gd_wglCreateContext = (PFNWGLCREATECONTEXT)GetProcAddress(module, "wglCreateContext");
  73. gd_wglMakeCurrent = (PFNWGLMAKECURRENT)GetProcAddress(module, "wglMakeCurrent");
  74. gd_wglDeleteContext = (PFNWGLDELETECONTEXT)GetProcAddress(module, "wglDeleteContext");
  75. gd_wglGetProcAddress = (PFNWGLGETPROCADDRESS)GetProcAddress(module, "wglGetProcAddress");
  76. if (!gd_wglCreateContext || !gd_wglMakeCurrent || !gd_wglDeleteContext || !gd_wglGetProcAddress) {
  77. return gl_info;
  78. }
  79. LPCWSTR class_name = L"EngineWGLDetect";
  80. HINSTANCE hInstance = static_cast<OS_Windows *>(OS::get_singleton())->get_hinstance();
  81. WNDCLASSW wc = {};
  82. wc.lpfnWndProc = DefWindowProcW;
  83. wc.hInstance = hInstance;
  84. wc.lpszClassName = class_name;
  85. RegisterClassW(&wc);
  86. HWND hWnd = CreateWindowExW(WS_EX_APPWINDOW, class_name, L"", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr);
  87. if (hWnd) {
  88. HDC hDC = GetDC(hWnd);
  89. if (hDC) {
  90. static PIXELFORMATDESCRIPTOR pfd = {
  91. sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
  92. 1,
  93. PFD_DRAW_TO_WINDOW | // Format Must Support Window
  94. PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
  95. PFD_DOUBLEBUFFER,
  96. (BYTE)PFD_TYPE_RGBA,
  97. (BYTE)(OS::get_singleton()->is_layered_allowed() ? 32 : 24),
  98. (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Color Bits Ignored
  99. (BYTE)(OS::get_singleton()->is_layered_allowed() ? 8 : 0), // Alpha Buffer
  100. (BYTE)0, // Shift Bit Ignored
  101. (BYTE)0, // No Accumulation Buffer
  102. (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Accumulation Bits Ignored
  103. (BYTE)24, // 24Bit Z-Buffer (Depth Buffer)
  104. (BYTE)0, // No Stencil Buffer
  105. (BYTE)0, // No Auxiliary Buffer
  106. (BYTE)PFD_MAIN_PLANE, // Main Drawing Layer
  107. (BYTE)0, // Reserved
  108. 0, 0, 0 // Layer Masks Ignored
  109. };
  110. int pixel_format = ChoosePixelFormat(hDC, &pfd);
  111. SetPixelFormat(hDC, pixel_format, &pfd);
  112. HGLRC hRC = gd_wglCreateContext(hDC);
  113. if (hRC) {
  114. if (gd_wglMakeCurrent(hDC, hRC)) {
  115. int attribs[] = {
  116. WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  117. WGL_CONTEXT_MINOR_VERSION_ARB, 3,
  118. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  119. WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
  120. 0
  121. };
  122. PFNWGLCREATECONTEXTATTRIBSARBPROC gd_wglCreateContextAttribsARB = nullptr;
  123. gd_wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)gd_wglGetProcAddress("wglCreateContextAttribsARB");
  124. if (gd_wglCreateContextAttribsARB) {
  125. HGLRC new_hRC = gd_wglCreateContextAttribsARB(hDC, nullptr, attribs);
  126. if (new_hRC) {
  127. if (gd_wglMakeCurrent(hDC, new_hRC)) {
  128. PFNWGLGETSTRINGPROC gd_wglGetString = (PFNWGLGETSTRINGPROC)GetProcAddress(module, "glGetString");
  129. if (gd_wglGetString) {
  130. const char *prefixes[] = {
  131. "OpenGL ES-CM ",
  132. "OpenGL ES-CL ",
  133. "OpenGL ES ",
  134. "OpenGL SC ",
  135. nullptr
  136. };
  137. const char *version = (const char *)gd_wglGetString(WGL_VERSION);
  138. if (version) {
  139. const String device_vendor = String::utf8((const char *)gd_wglGetString(WGL_VENDOR)).strip_edges().trim_suffix(" Corporation");
  140. const String device_name = String::utf8((const char *)gd_wglGetString(WGL_RENDERER)).strip_edges().trim_suffix("/PCIe/SSE2");
  141. for (int i = 0; prefixes[i]; i++) {
  142. size_t length = strlen(prefixes[i]);
  143. if (strncmp(version, prefixes[i], length) == 0) {
  144. version += length;
  145. break;
  146. }
  147. }
  148. int major = 0;
  149. int minor = 0;
  150. #ifdef _MSC_VER
  151. sscanf_s(version, "%d.%d", &major, &minor);
  152. #else
  153. sscanf(version, "%d.%d", &major, &minor);
  154. #endif
  155. print_verbose(vformat("Native OpenGL API detected: %d.%d: %s - %s", major, minor, device_vendor, device_name));
  156. gl_info["vendor"] = device_vendor;
  157. gl_info["name"] = device_name;
  158. gl_info["version"] = major * 10000 + minor;
  159. }
  160. }
  161. }
  162. gd_wglMakeCurrent(nullptr, nullptr);
  163. gd_wglDeleteContext(new_hRC);
  164. }
  165. }
  166. }
  167. gd_wglMakeCurrent(nullptr, nullptr);
  168. gd_wglDeleteContext(hRC);
  169. }
  170. ReleaseDC(hWnd, hDC);
  171. }
  172. DestroyWindow(hWnd);
  173. }
  174. UnregisterClassW(class_name, hInstance);
  175. return gl_info;
  176. }
  177. #endif // WINDOWS_ENABLED && GLES3_ENABLED