Path: blob/master/platform/windows/wgl_detect_version.cpp
10277 views
/**************************************************************************/1/* wgl_detect_version.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#if defined(WINDOWS_ENABLED) && defined(GLES3_ENABLED)3132#include "wgl_detect_version.h"33#include "os_windows.h"3435#include "core/string/print_string.h"36#include "core/string/ustring.h"37#include "core/variant/dictionary.h"3839#include <windows.h>4041#include <dwmapi.h>42#include <cstdio>43#include <cstdlib>4445#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x209146#define WGL_CONTEXT_MINOR_VERSION_ARB 0x209247#define WGL_CONTEXT_FLAGS_ARB 0x209448#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0000000249#define WGL_CONTEXT_PROFILE_MASK_ARB 0x912650#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x0000000151#define WGL_VENDOR 0x1F0052#define WGL_RENDERER 0x1F0153#define WGL_VERSION 0x1F025455typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXT)(HDC);56typedef BOOL(APIENTRY *PFNWGLDELETECONTEXT)(HGLRC);57typedef BOOL(APIENTRY *PFNWGLMAKECURRENT)(HDC, HGLRC);58typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC, const int *);59typedef void *(APIENTRY *PFNWGLGETPROCADDRESS)(LPCSTR);60typedef const char *(APIENTRY *PFNWGLGETSTRINGPROC)(unsigned int);6162Dictionary detect_wgl() {63Dictionary gl_info;64gl_info["version"] = 0;65gl_info["vendor"] = String();66gl_info["name"] = String();6768PFNWGLCREATECONTEXT gd_wglCreateContext;69PFNWGLMAKECURRENT gd_wglMakeCurrent;70PFNWGLDELETECONTEXT gd_wglDeleteContext;71PFNWGLGETPROCADDRESS gd_wglGetProcAddress;7273HMODULE module = LoadLibraryW(L"opengl32.dll");74if (!module) {75return gl_info;76}77gd_wglCreateContext = (PFNWGLCREATECONTEXT)(void *)GetProcAddress(module, "wglCreateContext");78gd_wglMakeCurrent = (PFNWGLMAKECURRENT)(void *)GetProcAddress(module, "wglMakeCurrent");79gd_wglDeleteContext = (PFNWGLDELETECONTEXT)(void *)GetProcAddress(module, "wglDeleteContext");80gd_wglGetProcAddress = (PFNWGLGETPROCADDRESS)(void *)GetProcAddress(module, "wglGetProcAddress");81if (!gd_wglCreateContext || !gd_wglMakeCurrent || !gd_wglDeleteContext || !gd_wglGetProcAddress) {82return gl_info;83}8485LPCWSTR class_name = L"EngineWGLDetect";86HINSTANCE hInstance = static_cast<OS_Windows *>(OS::get_singleton())->get_hinstance();87WNDCLASSW wc = {};8889wc.lpfnWndProc = DefWindowProcW;90wc.hInstance = hInstance;91wc.lpszClassName = class_name;9293RegisterClassW(&wc);9495HWND hWnd = CreateWindowExW(WS_EX_APPWINDOW, class_name, L"", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr);96if (hWnd) {97HDC hDC = GetDC(hWnd);98if (hDC) {99static PIXELFORMATDESCRIPTOR pfd = {100sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor1011,102PFD_DRAW_TO_WINDOW | // Format Must Support Window103PFD_SUPPORT_OPENGL | // Format Must Support OpenGL104PFD_DOUBLEBUFFER,105(BYTE)PFD_TYPE_RGBA,106(BYTE)(OS::get_singleton()->is_layered_allowed() ? 32 : 24),107(BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Color Bits Ignored108(BYTE)(OS::get_singleton()->is_layered_allowed() ? 8 : 0), // Alpha Buffer109(BYTE)0, // Shift Bit Ignored110(BYTE)0, // No Accumulation Buffer111(BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Accumulation Bits Ignored112(BYTE)24, // 24Bit Z-Buffer (Depth Buffer)113(BYTE)0, // No Stencil Buffer114(BYTE)0, // No Auxiliary Buffer115(BYTE)PFD_MAIN_PLANE, // Main Drawing Layer116(BYTE)0, // Reserved1170, 0, 0 // Layer Masks Ignored118};119120int pixel_format = ChoosePixelFormat(hDC, &pfd);121SetPixelFormat(hDC, pixel_format, &pfd);122123HGLRC hRC = gd_wglCreateContext(hDC);124if (hRC) {125if (gd_wglMakeCurrent(hDC, hRC)) {126int attribs[] = {127WGL_CONTEXT_MAJOR_VERSION_ARB, 3,128WGL_CONTEXT_MINOR_VERSION_ARB, 3,129WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,130WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,1310132};133134PFNWGLCREATECONTEXTATTRIBSARBPROC gd_wglCreateContextAttribsARB = nullptr;135gd_wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)gd_wglGetProcAddress("wglCreateContextAttribsARB");136if (gd_wglCreateContextAttribsARB) {137HGLRC new_hRC = gd_wglCreateContextAttribsARB(hDC, nullptr, attribs);138if (new_hRC) {139if (gd_wglMakeCurrent(hDC, new_hRC)) {140PFNWGLGETSTRINGPROC gd_wglGetString = (PFNWGLGETSTRINGPROC)(void *)GetProcAddress(module, "glGetString");141if (gd_wglGetString) {142const char *prefixes[] = {143"OpenGL ES-CM ",144"OpenGL ES-CL ",145"OpenGL ES ",146"OpenGL SC ",147nullptr148};149const char *version = (const char *)gd_wglGetString(WGL_VERSION);150if (version) {151const String device_vendor = String::utf8((const char *)gd_wglGetString(WGL_VENDOR)).strip_edges().trim_suffix(" Corporation");152const String device_name = String::utf8((const char *)gd_wglGetString(WGL_RENDERER)).strip_edges().trim_suffix("/PCIe/SSE2");153for (int i = 0; prefixes[i]; i++) {154size_t length = strlen(prefixes[i]);155if (strncmp(version, prefixes[i], length) == 0) {156version += length;157break;158}159}160int major = 0;161int minor = 0;162#ifdef _MSC_VER163sscanf_s(version, "%d.%d", &major, &minor);164#else165sscanf(version, "%d.%d", &major, &minor);166#endif167print_verbose(vformat("Native OpenGL API detected: %d.%d: %s - %s", major, minor, device_vendor, device_name));168gl_info["vendor"] = device_vendor;169gl_info["name"] = device_name;170gl_info["version"] = major * 10000 + minor;171}172}173}174gd_wglMakeCurrent(nullptr, nullptr);175gd_wglDeleteContext(new_hRC);176}177}178}179gd_wglMakeCurrent(nullptr, nullptr);180gd_wglDeleteContext(hRC);181}182ReleaseDC(hWnd, hDC);183}184DestroyWindow(hWnd);185}186UnregisterClassW(class_name, hInstance);187188return gl_info;189}190191#endif // WINDOWS_ENABLED && GLES3_ENABLED192193194