summaryrefslogtreecommitdiff
path: root/platform/windows
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2021-10-31 18:30:38 +0100
committerGitHub <noreply@github.com>2021-10-31 18:30:38 +0100
commit16dc607f8ad2055c8dec543bcd427fe74e5d6059 (patch)
treec99e99c1fd63fdb3d60b0c6d6ec632647e65619a /platform/windows
parentd6f972fad45d28222b1437a047241dd74f0d5b90 (diff)
parent8a10bb7d0dd0cc03353bb751af25a0eca1357c9d (diff)
Merge pull request #54307 from Calinou/add-opengl-renderer-squash
Add OpenGL renderer (squashed)
Diffstat (limited to 'platform/windows')
-rw-r--r--platform/windows/SCsub2
-rw-r--r--platform/windows/context_gl_windows.cpp186
-rw-r--r--platform/windows/detect.py5
-rw-r--r--platform/windows/display_server_windows.cpp89
-rw-r--r--platform/windows/display_server_windows.h13
-rw-r--r--platform/windows/gl_manager_windows.cpp349
-rw-r--r--platform/windows/gl_manager_windows.h (renamed from platform/windows/context_gl_windows.h)94
-rw-r--r--platform/windows/os_windows.h4
-rw-r--r--platform/windows/platform_config.h2
9 files changed, 496 insertions, 248 deletions
diff --git a/platform/windows/SCsub b/platform/windows/SCsub
index 47d8e14680..76234c3065 100644
--- a/platform/windows/SCsub
+++ b/platform/windows/SCsub
@@ -15,7 +15,7 @@ common_win = [
"joypad_windows.cpp",
"windows_terminal_logger.cpp",
"vulkan_context_win.cpp",
- "context_gl_windows.cpp",
+ "gl_manager_windows.cpp",
]
res_file = "godot_res.rc"
diff --git a/platform/windows/context_gl_windows.cpp b/platform/windows/context_gl_windows.cpp
deleted file mode 100644
index 74b12cbb3b..0000000000
--- a/platform/windows/context_gl_windows.cpp
+++ /dev/null
@@ -1,186 +0,0 @@
-/*************************************************************************/
-/* context_gl_windows.cpp */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* 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. */
-/*************************************************************************/
-
-#if defined(OPENGL_ENABLED) || defined(GLES_ENABLED)
-
-// Author: Juan Linietsky <reduzio@gmail.com>, (C) 2008
-
-#include "context_gl_windows.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
-
-#if defined(__GNUC__)
-// Workaround GCC warning from -Wcast-function-type.
-#define wglGetProcAddress (void *)wglGetProcAddress
-#endif
-
-typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC, const int *);
-
-void ContextGL_Windows::release_current() {
- wglMakeCurrent(hDC, nullptr);
-}
-
-void ContextGL_Windows::make_current() {
- wglMakeCurrent(hDC, hRC);
-}
-
-int ContextGL_Windows::get_window_width() {
- return OS::get_singleton()->get_video_mode().width;
-}
-
-int ContextGL_Windows::get_window_height() {
- return OS::get_singleton()->get_video_mode().height;
-}
-
-void ContextGL_Windows::swap_buffers() {
- SwapBuffers(hDC);
-}
-
-void ContextGL_Windows::set_use_vsync(bool p_use) {
- if (wglSwapIntervalEXT) {
- int swap_interval = p_use ? 1 : 0;
- wglSwapIntervalEXT(swap_interval);
- }
-
- use_vsync = p_use;
-}
-
-bool ContextGL_Windows::is_using_vsync() const {
- return use_vsync;
-}
-
-#define _WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
-
-Error ContextGL_Windows::initialize() {
- 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
- };
-
- hDC = GetDC(hWnd);
- if (!hDC) {
- return ERR_CANT_CREATE; // Return FALSE
- }
-
- 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
- }
-
- hRC = wglCreateContext(hDC);
- if (!hRC) // Are We Able To Get A Rendering Context?
- {
- return ERR_CANT_CREATE; // Return FALSE
- }
-
- wglMakeCurrent(hDC, hRC);
-
- if (opengl_3_context) {
- 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(hRC);
- return ERR_CANT_CREATE;
- }
-
- HGLRC new_hRC = wglCreateContextAttribsARB(hDC, 0, attribs);
- if (!new_hRC) {
- wglDeleteContext(hRC);
- return ERR_CANT_CREATE; // Return false
- }
- wglMakeCurrent(hDC, nullptr);
- wglDeleteContext(hRC);
- hRC = new_hRC;
-
- if (!wglMakeCurrent(hDC, hRC)) // Try To Activate The Rendering Context
- {
- return ERR_CANT_CREATE; // Return FALSE
- }
- }
-
- wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
- wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC)wglGetProcAddress("wglGetSwapIntervalEXT");
- //glWrapperInit(wrapper_get_proc_address);
-
- return OK;
-}
-
-ContextGL_Windows::ContextGL_Windows(HWND hwnd, bool p_opengl_3_context) {
- opengl_3_context = p_opengl_3_context;
- hWnd = hwnd;
- use_vsync = false;
- pixel_format = 0;
-}
-
-ContextGL_Windows::~ContextGL_Windows() {
-}
-
-#endif
diff --git a/platform/windows/detect.py b/platform/windows/detect.py
index 6752885f37..22fbbeb74f 100644
--- a/platform/windows/detect.py
+++ b/platform/windows/detect.py
@@ -279,7 +279,7 @@ def configure_msvc(env, manual_msvc_config):
if not env["use_volk"]:
LIBS += ["vulkan"]
- # env.AppendUnique(CPPDEFINES = ['OPENGL_ENABLED'])
+ env.AppendUnique(CPPDEFINES=["GLES3_ENABLED"])
LIBS += ["opengl32"]
env.Append(LINKFLAGS=[p + env["LIBSUFFIX"] for p in LIBS])
@@ -453,8 +453,7 @@ def configure_mingw(env):
if not env["use_volk"]:
env.Append(LIBS=["vulkan"])
- ## TODO !!! Re-enable when OpenGLES Rendering Device is implemented !!!
- # env.Append(CPPDEFINES=['OPENGL_ENABLED'])
+ env.Append(CPPDEFINES=["GLES3_ENABLED"])
env.Append(LIBS=["opengl32"])
env.Append(CPPDEFINES=["MINGW_ENABLED", ("MINGW_HAS_SECURE_API", 1)])
diff --git a/platform/windows/display_server_windows.cpp b/platform/windows/display_server_windows.cpp
index 905c4142a8..e2c9951692 100644
--- a/platform/windows/display_server_windows.cpp
+++ b/platform/windows/display_server_windows.cpp
@@ -38,6 +38,10 @@
#include <avrt.h>
+#if defined(GLES3_ENABLED)
+#include "drivers/gles3/rasterizer_gles3.h"
+#endif
+
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,
@@ -533,6 +537,11 @@ void DisplayServerWindows::delete_sub_window(WindowID p_window) {
context_vulkan->window_destroy(p_window);
}
#endif
+#ifdef GLES3_ENABLED
+ if (rendering_driver == "opengl3") {
+ gl_manager->window_destroy(p_window);
+ }
+#endif
if ((tablet_get_current_driver() == "wintab") && wintab_available && windows[p_window].wtctx) {
wintab_WTClose(windows[p_window].wtctx);
@@ -542,6 +551,12 @@ void DisplayServerWindows::delete_sub_window(WindowID p_window) {
windows.erase(p_window);
}
+void DisplayServerWindows::gl_window_make_current(DisplayServer::WindowID p_window_id) {
+#if defined(GLES3_ENABLED)
+ gl_manager->window_make_current(p_window_id);
+#endif
+}
+
void DisplayServerWindows::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
_THREAD_SAFE_METHOD_
@@ -812,6 +827,11 @@ void DisplayServerWindows::window_set_size(const Size2i p_size, WindowID p_windo
context_vulkan->window_resize(p_window, w, h);
}
#endif
+#if defined(GLES3_ENABLED)
+ if (rendering_driver == "opengl3") {
+ gl_manager->window_resize(p_window, w, h);
+ }
+#endif
if (wd.fullscreen) {
return;
@@ -1591,6 +1611,9 @@ void DisplayServerWindows::make_rendering_thread() {
}
void DisplayServerWindows::swap_buffers() {
+#if defined(GLES3_ENABLED)
+ gl_manager->swap_buffers();
+#endif
}
void DisplayServerWindows::set_native_icon(const String &p_filename) {
@@ -1742,17 +1765,18 @@ void DisplayServerWindows::set_icon(const Ref<Image> &p_icon) {
void DisplayServerWindows::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) {
_THREAD_SAFE_METHOD_
#if defined(VULKAN_ENABLED)
- context_vulkan->set_vsync_mode(p_window, p_vsync_mode);
+ // TODO disabling for now
+ //context_vulkan->set_vsync_mode(p_window, p_vsync_mode);
#endif
}
DisplayServer::VSyncMode DisplayServerWindows::window_get_vsync_mode(WindowID p_window) const {
_THREAD_SAFE_METHOD_
#if defined(VULKAN_ENABLED)
- return context_vulkan->get_vsync_mode(p_window);
-#else
- return DisplayServer::VSYNC_ENABLED;
+ //TODO disabling for now
+ //return context_vulkan->get_vsync_mode(p_window);
#endif
+ return DisplayServer::VSYNC_ENABLED;
}
void DisplayServerWindows::set_context(Context p_context) {
@@ -3086,6 +3110,13 @@ DisplayServer::WindowID DisplayServerWindows::_create_window(WindowMode p_mode,
}
#endif
+#ifdef GLES3_ENABLED
+ if (rendering_driver == "opengl3") {
+ Error err = gl_manager->window_create(id, wd.hWnd, hInstance, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top);
+ ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Failed to create an OpenGL window.");
+ }
+#endif
+
RegisterTouchWindow(wd.hWnd, 0);
TRACKMOUSEEVENT tme;
@@ -3216,6 +3247,8 @@ DisplayServerWindows::DisplayServerWindows(const String &p_rendering_driver, Win
outside = true;
+ rendering_driver = p_rendering_driver;
+
// Note: Wacom WinTab driver API for pen input, for devices incompatible with Windows Ink.
HMODULE wintab_lib = LoadLibraryW(L"wintab32.dll");
if (wintab_lib) {
@@ -3292,8 +3325,6 @@ DisplayServerWindows::DisplayServerWindows(const String &p_rendering_driver, Win
use_raw_input = false;
}
- rendering_driver = "vulkan";
-
#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
context_vulkan = memnew(VulkanContextWindows);
@@ -3305,27 +3336,23 @@ DisplayServerWindows::DisplayServerWindows(const String &p_rendering_driver, Win
}
}
#endif
+ // Init context and rendering device
+#if defined(GLES3_ENABLED)
-#if defined(OPENGL_ENABLED)
- if (rendering_driver_index == VIDEO_DRIVER_GLES2) {
- context_gles2 = memnew(ContextGL_Windows(hWnd, false));
+ if (rendering_driver == "opengl3") {
+ GLManager_Windows::ContextType opengl_api_type = GLManager_Windows::GLES_3_0_COMPATIBLE;
- if (context_gles2->initialize() != OK) {
- memdelete(context_gles2);
- context_gles2 = nullptr;
- ERR_FAIL_V(ERR_UNAVAILABLE);
- }
+ gl_manager = memnew(GLManager_Windows(opengl_api_type));
- context_gles2->set_use_vsync(video_mode.use_vsync);
-
- if (RasterizerGLES2::is_viable() == OK) {
- RasterizerGLES2::register_config();
- RasterizerGLES2::make_current();
- } else {
- memdelete(context_gles2);
- context_gles2 = nullptr;
- ERR_FAIL_V(ERR_UNAVAILABLE);
+ if (gl_manager->initialize() != OK) {
+ memdelete(gl_manager);
+ gl_manager = nullptr;
+ r_error = ERR_UNAVAILABLE;
+ return;
}
+
+ // gl_manager->set_use_vsync(current_videomode.use_vsync);
+ RasterizerGLES3::make_current();
}
#endif
@@ -3386,8 +3413,8 @@ Vector<String> DisplayServerWindows::get_rendering_drivers_func() {
#ifdef VULKAN_ENABLED
drivers.push_back("vulkan");
#endif
-#ifdef OPENGL_ENABLED
- drivers.push_back("opengl");
+#ifdef GLES3_ENABLED
+ drivers.push_back("opengl3");
#endif
return drivers;
@@ -3396,7 +3423,7 @@ Vector<String> DisplayServerWindows::get_rendering_drivers_func() {
DisplayServer *DisplayServerWindows::create_func(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
DisplayServer *ds = memnew(DisplayServerWindows(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_resolution, r_error));
if (r_error != OK) {
- OS::get_singleton()->alert("Your video card driver does not support any of the supported Vulkan versions.\n"
+ OS::get_singleton()->alert("Your video card driver does not support any of the supported Vulkan or OpenGL versions.\n"
"Please update your drivers or if you have a very old or integrated GPU upgrade it.",
"Unable to initialize Video driver");
}
@@ -3417,6 +3444,10 @@ DisplayServerWindows::~DisplayServerWindows() {
SetWindowLongPtr(windows[MAIN_WINDOW_ID].hWnd, GWLP_WNDPROC, (LONG_PTR)user_proc);
};
+#ifdef GLES3_ENABLED
+ // destroy windows .. NYI?
+#endif
+
if (windows.has(MAIN_WINDOW_ID)) {
#ifdef VULKAN_ENABLED
if (rendering_driver == "vulkan") {
@@ -3445,4 +3476,10 @@ DisplayServerWindows::~DisplayServerWindows() {
if (restore_mouse_trails > 1) {
SystemParametersInfoA(SPI_SETMOUSETRAILS, restore_mouse_trails, 0, 0);
}
+#ifdef GLES3_ENABLED
+ if (gl_manager) {
+ memdelete(gl_manager);
+ gl_manager = nullptr;
+ }
+#endif
}
diff --git a/platform/windows/display_server_windows.h b/platform/windows/display_server_windows.h
index 22c4f96a90..8e2c346d5b 100644
--- a/platform/windows/display_server_windows.h
+++ b/platform/windows/display_server_windows.h
@@ -51,15 +51,15 @@
#include "drivers/xaudio2/audio_driver_xaudio2.h"
#endif
-#if defined(OPENGL_ENABLED)
-#include "context_gl_windows.h"
-#endif
-
#if defined(VULKAN_ENABLED)
#include "drivers/vulkan/rendering_device_vulkan.h"
#include "platform/windows/vulkan_context_win.h"
#endif
+#if defined(GLES3_ENABLED)
+#include "gl_manager_windows.h"
+#endif
+
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
@@ -304,8 +304,8 @@ class DisplayServerWindows : public DisplayServer {
int old_x, old_y;
Point2i center;
-#if defined(OPENGL_ENABLED)
- ContextGL_Windows *context_gles2;
+#if defined(GLES3_ENABLED)
+ GLManager_Windows *gl_manager;
#endif
#if defined(VULKAN_ENABLED)
@@ -477,6 +477,7 @@ public:
virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID) override;
virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const override;
+ virtual void gl_window_make_current(DisplayServer::WindowID p_window_id);
virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
diff --git a/platform/windows/gl_manager_windows.cpp b/platform/windows/gl_manager_windows.cpp
new file mode 100644
index 0000000000..98205d6282
--- /dev/null
+++ b/platform/windows/gl_manager_windows.cpp
@@ -0,0 +1,349 @@
+/*************************************************************************/
+/* gl_manager_windows.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* 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
+#ifdef GLES3_ENABLED
+
+#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 *);
+
+int GLManager_Windows::_find_or_create_display(GLWindow &win) {
+ // find display NYI, only 1 supported so far
+ if (_displays.size())
+ return 0;
+
+ // for (unsigned int n = 0; n < _displays.size(); n++) {
+ // const GLDisplay &d = _displays[n];
+ // if (d.x11_display == p_x11_display)
+ // return n;
+ // }
+
+ // create
+ GLDisplay d_temp = {};
+ _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?
+ _displays.remove(new_display_id);
+ return -1;
+ }
+
+ return new_display_id;
+}
+
+Error GLManager_Windows::_create_context(GLWindow &win, GLDisplay &gl_display) {
+ 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
+ };
+
+ // alias
+ HDC hDC = win.hDC;
+
+ 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
+ }
+
+ gl_display.hRC = wglCreateContext(hDC);
+ if (!gl_display.hRC) // Are We Able To Get A Rendering Context?
+ {
+ return ERR_CANT_CREATE; // Return FALSE
+ }
+
+ wglMakeCurrent(hDC, gl_display.hRC);
+
+ 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;
+ }
+
+ HGLRC new_hRC = wglCreateContextAttribsARB(hDC, 0, attribs);
+ if (!new_hRC) {
+ wglDeleteContext(gl_display.hRC);
+ gl_display.hRC = 0;
+ return ERR_CANT_CREATE; // Return false
+ }
+ wglMakeCurrent(hDC, nullptr);
+ wglDeleteContext(gl_display.hRC);
+ gl_display.hRC = new_hRC;
+
+ if (!wglMakeCurrent(hDC, gl_display.hRC)) // Try To Activate The Rendering Context
+ {
+ wglDeleteContext(gl_display.hRC);
+ gl_display.hRC = 0;
+ return ERR_CANT_CREATE; // Return FALSE
+ }
+
+ return OK;
+}
+
+Error GLManager_Windows::window_create(DisplayServer::WindowID p_window_id, HWND p_hwnd, HINSTANCE p_hinstance, int p_width, int p_height) {
+ HDC hdc = GetDC(p_hwnd);
+ if (!hdc) {
+ return ERR_CANT_CREATE; // Return FALSE
+ }
+
+ // make sure vector is big enough...
+ // we can mirror the external vector, it is simpler
+ // to keep the IDs identical for fast lookup
+ if (p_window_id >= (int)_windows.size()) {
+ _windows.resize(p_window_id + 1);
+ }
+
+ GLWindow &win = _windows[p_window_id];
+ win.in_use = true;
+ win.window_id = p_window_id;
+ win.width = p_width;
+ win.height = p_height;
+ win.hwnd = p_hwnd;
+ win.hDC = hdc;
+
+ win.gldisplay_id = _find_or_create_display(win);
+
+ if (win.gldisplay_id == -1) {
+ // release DC?
+ _windows.remove(_windows.size() - 1);
+ return FAILED;
+ }
+
+ // the display could be invalid .. check NYI
+ GLDisplay &gl_display = _displays[win.gldisplay_id];
+
+ // make current
+ window_make_current(_windows.size() - 1);
+
+ 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);
+ win.in_use = false;
+
+ if (_current_window == &win) {
+ _current_window = nullptr;
+ }
+}
+
+void GLManager_Windows::release_current() {
+ if (!_current_window)
+ return;
+
+ wglMakeCurrent(_current_window->hDC, nullptr);
+}
+
+void GLManager_Windows::window_make_current(DisplayServer::WindowID p_window_id) {
+ if (p_window_id == -1)
+ return;
+
+ GLWindow &win = _windows[p_window_id];
+ if (!win.in_use)
+ return;
+
+ // noop
+ if (&win == _current_window)
+ return;
+
+ const GLDisplay &disp = get_display(win.gldisplay_id);
+ wglMakeCurrent(win.hDC, disp.hRC);
+
+ _internal_set_current_window(&win);
+}
+
+void GLManager_Windows::make_current() {
+ if (!_current_window)
+ return;
+ if (!_current_window->in_use) {
+ WARN_PRINT("current window not in use!");
+ return;
+ }
+ const GLDisplay &disp = get_current_display();
+ wglMakeCurrent(_current_window->hDC, disp.hRC);
+}
+
+void GLManager_Windows::swap_buffers() {
+ // NO NEED TO CALL SWAP BUFFERS for each window...
+ // see https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glXSwapBuffers.xml
+
+ if (!_current_window)
+ return;
+ if (!_current_window->in_use) {
+ WARN_PRINT("current window not in use!");
+ return;
+ }
+
+ // print_line("\tswap_buffers");
+
+ // only for debugging without drawing anything
+ // glClearColor(Math::randf(), 0, 1, 1);
+ //glClear(GL_COLOR_BUFFER_BIT);
+
+ // const GLDisplay &disp = get_current_display();
+ SwapBuffers(_current_window->hDC);
+}
+
+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));
+ if (extensions.find("GLX_EXT_swap_control") != -1)
+ glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalEXT");
+ if (extensions.find("GLX_MESA_swap_control") != -1)
+ glXSwapIntervalMESA = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalMESA");
+ if (extensions.find("GLX_SGI_swap_control") != -1)
+ glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalSGI");
+ }
+ 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);
+ } else
+ return;
+ 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();
+}
+
+#endif // GLES3_ENABLED
+#endif // WINDOWS
diff --git a/platform/windows/context_gl_windows.h b/platform/windows/gl_manager_windows.h
index feff1d825b..9733a57420 100644
--- a/platform/windows/context_gl_windows.h
+++ b/platform/windows/gl_manager_windows.h
@@ -1,5 +1,5 @@
/*************************************************************************/
-/* context_gl_windows.h */
+/* gl_manager_windows.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
@@ -28,50 +28,100 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
-#if defined(OPENGL_ENABLED) || defined(GLES_ENABLED)
+#ifndef GL_MANAGER_WINDOWS_H
+#define GL_MANAGER_WINDOWS_H
-// Author: Juan Linietsky <reduzio@gmail.com>, (C) 2008
-
-#ifndef CONTEXT_GL_WIN_H
-#define CONTEXT_GL_WIN_H
+#if defined(WINDOWS_ENABLED) && defined(GLES3_ENABLED)
#include "core/error/error_list.h"
#include "core/os/os.h"
+#include "core/templates/local_vector.h"
+#include "servers/display_server.h"
-#define WIN32_LEAN_AND_MEAN
#include <windows.h>
typedef bool(APIENTRY *PFNWGLSWAPINTERVALEXTPROC)(int interval);
typedef int(APIENTRY *PFNWGLGETSWAPINTERVALEXTPROC)(void);
-class ContextGL_Windows {
- HDC hDC;
- HGLRC hRC;
- unsigned int pixel_format;
- HWND hWnd;
- bool opengl_3_context;
- bool use_vsync;
+class GLManager_Windows {
+public:
+ enum ContextType {
+ GLES_3_0_COMPATIBLE,
+ };
+
+private:
+ // any data specific to the window
+ struct GLWindow {
+ GLWindow() { in_use = false; }
+ bool in_use;
+
+ // the external ID .. should match the GL window number .. unused I think
+ DisplayServer::WindowID window_id;
+ int width;
+ int height;
+
+ // windows specific
+ HDC hDC;
+ HWND hwnd;
+
+ int gldisplay_id;
+ };
+
+ struct GLDisplay {
+ // windows specific
+ HGLRC hRC;
+ };
+
+ LocalVector<GLWindow> _windows;
+ LocalVector<GLDisplay> _displays;
+
+ GLWindow *_current_window;
PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT;
PFNWGLGETSWAPINTERVALEXTPROC wglGetSwapIntervalEXT;
+ // funcs
+ void _internal_set_current_window(GLWindow *p_win);
+
+ GLWindow &get_window(unsigned int id) { return _windows[id]; }
+ const GLWindow &get_window(unsigned int id) const { return _windows[id]; }
+
+ const GLDisplay &get_current_display() const { return _displays[_current_window->gldisplay_id]; }
+ const GLDisplay &get_display(unsigned int id) { return _displays[id]; }
+
+ bool direct_render;
+ int glx_minor, glx_major;
+ bool use_vsync;
+ ContextType context_type;
+
+private:
+ int _find_or_create_display(GLWindow &win);
+ Error _create_context(GLWindow &win, GLDisplay &gl_display);
+
public:
- void release_current();
+ Error window_create(DisplayServer::WindowID p_window_id, HWND p_hwnd, HINSTANCE p_hinstance, int p_width, int p_height);
+ void window_destroy(DisplayServer::WindowID p_window_id);
+ void window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height);
- void make_current();
+ // get directly from the cached GLWindow
+ int window_get_width(DisplayServer::WindowID p_window_id = 0);
+ int window_get_height(DisplayServer::WindowID p_window_id = 0);
- int get_window_width();
- int get_window_height();
+ void release_current();
+ void make_current();
void swap_buffers();
+ void window_make_current(DisplayServer::WindowID p_window_id);
+
Error initialize();
void set_use_vsync(bool p_use);
bool is_using_vsync() const;
- ContextGL_Windows(HWND hwnd, bool p_opengl_3_context);
- ~ContextGL_Windows();
+ GLManager_Windows(ContextType p_context_type);
+ ~GLManager_Windows();
};
-#endif
-#endif
+#endif // defined(WINDOWS_ENABLED) && defined(GLES3_ENABLED)
+
+#endif // GL_MANAGER_WINDOWS_H
diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h
index b8eca956c8..1342d95575 100644
--- a/platform/windows/os_windows.h
+++ b/platform/windows/os_windows.h
@@ -46,10 +46,6 @@
#include "drivers/xaudio2/audio_driver_xaudio2.h"
#endif
-#if defined(OPENGL_ENABLED)
-#include "context_gl_windows.h"
-#endif
-
#if defined(VULKAN_ENABLED)
#include "drivers/vulkan/rendering_device_vulkan.h"
#include "platform/windows/vulkan_context_win.h"
diff --git a/platform/windows/platform_config.h b/platform/windows/platform_config.h
index 481f583f6f..dace0f86af 100644
--- a/platform/windows/platform_config.h
+++ b/platform/windows/platform_config.h
@@ -29,3 +29,5 @@
/*************************************************************************/
#include <malloc.h>
+
+#define OPENGL_INCLUDE_H "thirdparty/glad/glad/glad.h"