Debian Codegraph @

platform/windows/wgl_detect_version.cpp

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