wgl_detect_version.cpp 7.9 KB

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