2020-11-18 18:11:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* gl_manager_windows.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2020-11-18 18:11:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "gl_manager_windows.h"
|
|
|
|
|
|
|
|
#ifdef WINDOWS_ENABLED
|
2021-10-26 15:18:39 +00:00
|
|
|
#ifdef GLES3_ENABLED
|
2020-11-18 18:11:30 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <dwmapi.h>
|
|
|
|
|
|
|
|
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
|
|
|
|
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
|
|
|
|
#define WGL_CONTEXT_FLAGS_ARB 0x2094
|
|
|
|
#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002
|
|
|
|
#define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
|
|
|
|
#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
|
|
|
|
|
|
|
|
#define _WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
|
|
|
|
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
// Workaround GCC warning from -Wcast-function-type.
|
|
|
|
#define wglGetProcAddress (void *)wglGetProcAddress
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC, const int *);
|
|
|
|
|
2022-05-08 23:25:49 +00:00
|
|
|
static String format_error_message(DWORD id) {
|
|
|
|
LPWSTR messageBuffer = nullptr;
|
|
|
|
size_t size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
nullptr, id, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, nullptr);
|
|
|
|
|
|
|
|
String msg = "Error " + itos(id) + ": " + String::utf16((const char16_t *)messageBuffer, size);
|
|
|
|
|
|
|
|
LocalFree(messageBuffer);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2020-11-18 18:11:30 +00:00
|
|
|
int GLManager_Windows::_find_or_create_display(GLWindow &win) {
|
|
|
|
// find display NYI, only 1 supported so far
|
2022-02-16 12:56:32 +00:00
|
|
|
if (_displays.size()) {
|
2020-11-18 18:11:30 +00:00
|
|
|
return 0;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
|
|
|
|
// create
|
2021-09-26 23:07:10 +00:00
|
|
|
GLDisplay d_temp = {};
|
2020-11-18 18:11:30 +00:00
|
|
|
_displays.push_back(d_temp);
|
|
|
|
int new_display_id = _displays.size() - 1;
|
|
|
|
|
|
|
|
// create context
|
|
|
|
GLDisplay &d = _displays[new_display_id];
|
|
|
|
Error err = _create_context(win, d);
|
|
|
|
|
|
|
|
if (err != OK) {
|
|
|
|
// not good
|
|
|
|
// delete the _display?
|
2021-07-03 22:17:03 +00:00
|
|
|
_displays.remove_at(new_display_id);
|
2020-11-18 18:11:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_display_id;
|
|
|
|
}
|
|
|
|
|
2022-05-08 23:25:49 +00:00
|
|
|
static Error _configure_pixel_format(HDC hDC) {
|
2020-11-18 18:11:30 +00:00
|
|
|
static PIXELFORMATDESCRIPTOR pfd = {
|
|
|
|
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
|
|
|
|
1,
|
|
|
|
PFD_DRAW_TO_WINDOW | // Format Must Support Window
|
|
|
|
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
|
|
|
|
PFD_DOUBLEBUFFER,
|
|
|
|
(BYTE)PFD_TYPE_RGBA,
|
|
|
|
(BYTE)(OS::get_singleton()->is_layered_allowed() ? 32 : 24),
|
|
|
|
(BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Color Bits Ignored
|
|
|
|
(BYTE)(OS::get_singleton()->is_layered_allowed() ? 8 : 0), // Alpha Buffer
|
|
|
|
(BYTE)0, // Shift Bit Ignored
|
|
|
|
(BYTE)0, // No Accumulation Buffer
|
|
|
|
(BYTE)0, (BYTE)0, (BYTE)0, (BYTE)0, // Accumulation Bits Ignored
|
|
|
|
(BYTE)24, // 24Bit Z-Buffer (Depth Buffer)
|
|
|
|
(BYTE)0, // No Stencil Buffer
|
|
|
|
(BYTE)0, // No Auxiliary Buffer
|
|
|
|
(BYTE)PFD_MAIN_PLANE, // Main Drawing Layer
|
|
|
|
(BYTE)0, // Reserved
|
|
|
|
0, 0, 0 // Layer Masks Ignored
|
|
|
|
};
|
|
|
|
|
|
|
|
int pixel_format = ChoosePixelFormat(hDC, &pfd);
|
|
|
|
if (!pixel_format) // Did Windows Find A Matching Pixel Format?
|
|
|
|
{
|
|
|
|
return ERR_CANT_CREATE; // Return FALSE
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ret = SetPixelFormat(hDC, pixel_format, &pfd);
|
|
|
|
if (!ret) // Are We Able To Set The Pixel Format?
|
|
|
|
{
|
|
|
|
return ERR_CANT_CREATE; // Return FALSE
|
|
|
|
}
|
|
|
|
|
2022-05-08 23:25:49 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error GLManager_Windows::_create_context(GLWindow &win, GLDisplay &gl_display) {
|
|
|
|
Error err = _configure_pixel_format(win.hDC);
|
|
|
|
if (err != OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl_display.hRC = wglCreateContext(win.hDC);
|
2020-11-18 18:11:30 +00:00
|
|
|
if (!gl_display.hRC) // Are We Able To Get A Rendering Context?
|
|
|
|
{
|
|
|
|
return ERR_CANT_CREATE; // Return FALSE
|
|
|
|
}
|
|
|
|
|
2022-05-08 23:25:49 +00:00
|
|
|
if (!wglMakeCurrent(win.hDC, gl_display.hRC)) {
|
|
|
|
ERR_PRINT("Could not attach OpenGL context to newly created window: " + format_error_message(GetLastError()));
|
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
|
2021-10-26 15:18:39 +00:00
|
|
|
int attribs[] = {
|
|
|
|
WGL_CONTEXT_MAJOR_VERSION_ARB, 3, //we want a 3.3 context
|
|
|
|
WGL_CONTEXT_MINOR_VERSION_ARB, 3,
|
|
|
|
//and it shall be forward compatible so that we can only use up to date functionality
|
|
|
|
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
|
|
|
|
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB /*| _WGL_CONTEXT_DEBUG_BIT_ARB*/,
|
|
|
|
0
|
|
|
|
}; //zero indicates the end of the array
|
|
|
|
|
|
|
|
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = nullptr; //pointer to the method
|
|
|
|
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
|
|
|
|
|
|
|
|
if (wglCreateContextAttribsARB == nullptr) //OpenGL 3.0 is not supported
|
|
|
|
{
|
|
|
|
wglDeleteContext(gl_display.hRC);
|
|
|
|
gl_display.hRC = 0;
|
|
|
|
return ERR_CANT_CREATE;
|
|
|
|
}
|
|
|
|
|
2022-05-08 23:25:49 +00:00
|
|
|
HGLRC new_hRC = wglCreateContextAttribsARB(win.hDC, 0, attribs);
|
2021-10-26 15:18:39 +00:00
|
|
|
if (!new_hRC) {
|
2020-11-18 18:11:30 +00:00
|
|
|
wglDeleteContext(gl_display.hRC);
|
2021-10-26 15:18:39 +00:00
|
|
|
gl_display.hRC = 0;
|
2022-05-08 23:25:49 +00:00
|
|
|
return ERR_CANT_CREATE;
|
2021-10-26 15:18:39 +00:00
|
|
|
}
|
2022-05-08 23:25:49 +00:00
|
|
|
|
|
|
|
if (!wglMakeCurrent(win.hDC, nullptr)) {
|
|
|
|
ERR_PRINT("Could not detach OpenGL context from newly created window: " + format_error_message(GetLastError()));
|
|
|
|
}
|
|
|
|
|
2021-10-26 15:18:39 +00:00
|
|
|
wglDeleteContext(gl_display.hRC);
|
|
|
|
gl_display.hRC = new_hRC;
|
|
|
|
|
2022-05-08 23:25:49 +00:00
|
|
|
if (!wglMakeCurrent(win.hDC, gl_display.hRC)) // Try To Activate The Rendering Context
|
2021-10-26 15:18:39 +00:00
|
|
|
{
|
2022-05-08 23:25:49 +00:00
|
|
|
ERR_PRINT("Could not attach OpenGL context to newly created window with replaced OpenGL context: " + format_error_message(GetLastError()));
|
2021-10-26 15:18:39 +00:00
|
|
|
wglDeleteContext(gl_display.hRC);
|
|
|
|
gl_display.hRC = 0;
|
2022-05-08 23:25:49 +00:00
|
|
|
return ERR_CANT_CREATE;
|
2020-11-18 18:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error GLManager_Windows::window_create(DisplayServer::WindowID p_window_id, HWND p_hwnd, HINSTANCE p_hinstance, int p_width, int p_height) {
|
2022-05-08 23:25:49 +00:00
|
|
|
HDC hDC = GetDC(p_hwnd);
|
|
|
|
if (!hDC) {
|
|
|
|
return ERR_CANT_CREATE;
|
2020-11-18 18:11:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-08 23:25:49 +00:00
|
|
|
// configure the HDC to use a compatible pixel format
|
|
|
|
Error result = _configure_pixel_format(hDC);
|
|
|
|
if (result != OK) {
|
|
|
|
return result;
|
2020-11-18 18:11:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-08 23:25:49 +00:00
|
|
|
GLWindow win;
|
2020-11-18 18:11:30 +00:00
|
|
|
win.width = p_width;
|
|
|
|
win.height = p_height;
|
|
|
|
win.hwnd = p_hwnd;
|
2022-05-08 23:25:49 +00:00
|
|
|
win.hDC = hDC;
|
2020-11-18 18:11:30 +00:00
|
|
|
|
|
|
|
win.gldisplay_id = _find_or_create_display(win);
|
|
|
|
|
|
|
|
if (win.gldisplay_id == -1) {
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
2022-05-08 23:25:49 +00:00
|
|
|
// WARNING: p_window_id is an eternally growing integer since popup windows keep coming and going
|
|
|
|
// and each of them has a higher id than the previous, so it must be used in a map not a vector
|
|
|
|
_windows[p_window_id] = win;
|
|
|
|
|
2020-11-18 18:11:30 +00:00
|
|
|
// make current
|
2022-05-08 23:25:49 +00:00
|
|
|
window_make_current(p_window_id);
|
2020-11-18 18:11:30 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_Windows::_internal_set_current_window(GLWindow *p_win) {
|
|
|
|
_current_window = p_win;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_Windows::window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {
|
|
|
|
get_window(p_window_id).width = p_width;
|
|
|
|
get_window(p_window_id).height = p_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GLManager_Windows::window_get_width(DisplayServer::WindowID p_window_id) {
|
|
|
|
return get_window(p_window_id).width;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GLManager_Windows::window_get_height(DisplayServer::WindowID p_window_id) {
|
|
|
|
return get_window(p_window_id).height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_Windows::window_destroy(DisplayServer::WindowID p_window_id) {
|
|
|
|
GLWindow &win = get_window(p_window_id);
|
|
|
|
if (_current_window == &win) {
|
|
|
|
_current_window = nullptr;
|
|
|
|
}
|
2022-05-08 23:25:49 +00:00
|
|
|
_windows.erase(p_window_id);
|
2020-11-18 18:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_Windows::release_current() {
|
2022-02-16 12:56:32 +00:00
|
|
|
if (!_current_window) {
|
2020-11-18 18:11:30 +00:00
|
|
|
return;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
|
2022-05-08 23:25:49 +00:00
|
|
|
if (!wglMakeCurrent(_current_window->hDC, nullptr)) {
|
|
|
|
ERR_PRINT("Could not detach OpenGL context from window marked current: " + format_error_message(GetLastError()));
|
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_Windows::window_make_current(DisplayServer::WindowID p_window_id) {
|
2022-02-16 12:56:32 +00:00
|
|
|
if (p_window_id == -1) {
|
2020-11-18 18:11:30 +00:00
|
|
|
return;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
|
2022-05-08 23:25:49 +00:00
|
|
|
// crash if our data structures are out of sync, i.e. not found
|
2020-11-18 18:11:30 +00:00
|
|
|
GLWindow &win = _windows[p_window_id];
|
|
|
|
|
|
|
|
// noop
|
2022-02-16 12:56:32 +00:00
|
|
|
if (&win == _current_window) {
|
2020-11-18 18:11:30 +00:00
|
|
|
return;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
|
|
|
|
const GLDisplay &disp = get_display(win.gldisplay_id);
|
2022-05-08 23:25:49 +00:00
|
|
|
if (!wglMakeCurrent(win.hDC, disp.hRC)) {
|
|
|
|
ERR_PRINT("Could not switch OpenGL context to other window: " + format_error_message(GetLastError()));
|
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
|
|
|
|
_internal_set_current_window(&win);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_Windows::make_current() {
|
2022-02-16 12:56:32 +00:00
|
|
|
if (!_current_window) {
|
2020-11-18 18:11:30 +00:00
|
|
|
return;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
const GLDisplay &disp = get_current_display();
|
2022-05-08 23:25:49 +00:00
|
|
|
if (!wglMakeCurrent(_current_window->hDC, disp.hRC)) {
|
|
|
|
ERR_PRINT("Could not switch OpenGL context to window marked current: " + format_error_message(GetLastError()));
|
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_Windows::swap_buffers() {
|
2022-09-12 21:57:11 +00:00
|
|
|
SwapBuffers(_current_window->hDC);
|
2020-11-18 18:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Error GLManager_Windows::initialize() {
|
|
|
|
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
|
|
|
|
wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC)wglGetProcAddress("wglGetSwapIntervalEXT");
|
|
|
|
//glWrapperInit(wrapper_get_proc_address);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLManager_Windows::set_use_vsync(bool p_use) {
|
|
|
|
/*
|
|
|
|
static bool setup = false;
|
|
|
|
static PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = nullptr;
|
|
|
|
static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalMESA = nullptr;
|
|
|
|
static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI = nullptr;
|
|
|
|
|
|
|
|
if (!setup) {
|
|
|
|
setup = true;
|
|
|
|
String extensions = glXQueryExtensionsString(x11_display, DefaultScreen(x11_display));
|
2022-02-16 12:56:32 +00:00
|
|
|
if (extensions.find("GLX_EXT_swap_control") != -1) {
|
2020-11-18 18:11:30 +00:00
|
|
|
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalEXT");
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
|
|
|
if (extensions.find("GLX_MESA_swap_control") != -1) {
|
2020-11-18 18:11:30 +00:00
|
|
|
glXSwapIntervalMESA = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalMESA");
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
|
|
|
if (extensions.find("GLX_SGI_swap_control") != -1) {
|
2020-11-18 18:11:30 +00:00
|
|
|
glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalSGI");
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
}
|
|
|
|
int val = p_use ? 1 : 0;
|
|
|
|
if (glXSwapIntervalMESA) {
|
|
|
|
glXSwapIntervalMESA(val);
|
|
|
|
} else if (glXSwapIntervalSGI) {
|
|
|
|
glXSwapIntervalSGI(val);
|
|
|
|
} else if (glXSwapIntervalEXT) {
|
|
|
|
GLXDrawable drawable = glXGetCurrentDrawable();
|
|
|
|
glXSwapIntervalEXT(x11_display, drawable, val);
|
2022-02-16 12:56:32 +00:00
|
|
|
} else {
|
2020-11-18 18:11:30 +00:00
|
|
|
return;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
use_vsync = p_use;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLManager_Windows::is_using_vsync() const {
|
|
|
|
return use_vsync;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLManager_Windows::GLManager_Windows(ContextType p_context_type) {
|
|
|
|
context_type = p_context_type;
|
|
|
|
|
|
|
|
direct_render = false;
|
|
|
|
glx_minor = glx_major = 0;
|
|
|
|
use_vsync = false;
|
|
|
|
_current_window = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLManager_Windows::~GLManager_Windows() {
|
|
|
|
release_current();
|
|
|
|
}
|
|
|
|
|
2021-10-26 15:18:39 +00:00
|
|
|
#endif // GLES3_ENABLED
|
2020-11-18 18:11:30 +00:00
|
|
|
#endif // WINDOWS
|