Skip to content

Commit

Permalink
Merge pull request #55918 from BastiaanOlij/fix_win_opengl_destroy_crash
Browse files Browse the repository at this point in the history
Fix incorrect destroy of OpenGL driver if using Vulkan on Windows
  • Loading branch information
akien-mga authored Dec 18, 2021
2 parents aef799f + d08b28a commit 8485d0c
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 78 deletions.
4 changes: 2 additions & 2 deletions platform/android/display_server_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class DisplayServerAndroid : public DisplayServer {
CursorShape cursor_shape = CursorShape::CURSOR_ARROW;

#if defined(VULKAN_ENABLED)
VulkanContextAndroid *context_vulkan;
RenderingDeviceVulkan *rendering_device_vulkan;
VulkanContextAndroid *context_vulkan = nullptr;
RenderingDeviceVulkan *rendering_device_vulkan = nullptr;
#endif

ObjectID window_attached_instance_id;
Expand Down
4 changes: 2 additions & 2 deletions platform/iphone/display_server_iphone.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class DisplayServerIPhone : public DisplayServer {
_THREAD_SAFE_CLASS_

#if defined(VULKAN_ENABLED)
VulkanContextIPhone *context_vulkan;
RenderingDeviceVulkan *rendering_device_vulkan;
VulkanContextIPhone *context_vulkan = nullptr;
RenderingDeviceVulkan *rendering_device_vulkan = nullptr;
#endif

DisplayServer::ScreenOrientation screen_orientation;
Expand Down
28 changes: 13 additions & 15 deletions platform/iphone/display_server_iphone.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#if defined(GLES3_ENABLED)
// FIXME: Add support for both OpenGL and Vulkan when OpenGL is implemented
// again,
// Note that we should be checking "opengl3" as the driver, might never enable this seeing OpenGL is deprecated on iOS
// We are hardcoding the rendering_driver to "vulkan" down below

if (rendering_driver == "opengl_es") {
bool gl_initialization_error = false;
Expand Down Expand Up @@ -131,18 +133,16 @@

DisplayServerIPhone::~DisplayServerIPhone() {
#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
if (rendering_device_vulkan) {
rendering_device_vulkan->finalize();
memdelete(rendering_device_vulkan);
rendering_device_vulkan = nullptr;
}
if (rendering_device_vulkan) {
rendering_device_vulkan->finalize();
memdelete(rendering_device_vulkan);
rendering_device_vulkan = nullptr;
}

if (context_vulkan) {
context_vulkan->window_destroy(MAIN_WINDOW_ID);
memdelete(context_vulkan);
context_vulkan = nullptr;
}
if (context_vulkan) {
context_vulkan->window_destroy(MAIN_WINDOW_ID);
memdelete(context_vulkan);
context_vulkan = nullptr;
}
#endif
}
Expand Down Expand Up @@ -565,10 +565,8 @@
Size2i size = Size2i(viewSize.width, viewSize.height) * screen_get_max_scale();

#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
if (context_vulkan) {
context_vulkan->window_resize(MAIN_WINDOW_ID, size.x, size.y);
}
if (context_vulkan) {
context_vulkan->window_resize(MAIN_WINDOW_ID, size.x, size.y);
}
#endif

Expand Down
24 changes: 12 additions & 12 deletions platform/linuxbsd/display_server_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ void DisplayServerX11::delete_sub_window(WindowID p_id) {
}

#ifdef VULKAN_ENABLED
if (rendering_driver == "vulkan") {
if (context_vulkan) {
context_vulkan->window_destroy(p_id);
}
#endif
Expand Down Expand Up @@ -2927,7 +2927,7 @@ void DisplayServerX11::_window_changed(XEvent *event) {
wd.size = new_rect.size;

#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
if (context_vulkan) {
context_vulkan->window_resize(window_id, wd.size.width, wd.size.height);
}
#endif
Expand Down Expand Up @@ -4672,12 +4672,12 @@ DisplayServerX11::~DisplayServerX11() {
//destroy all windows
for (KeyValue<WindowID, WindowData> &E : windows) {
#ifdef VULKAN_ENABLED
if (rendering_driver == "vulkan") {
if (context_vulkan) {
context_vulkan->window_destroy(E.key);
}
#endif
#ifdef GLES3_ENABLED
if (rendering_driver == "opengl3") {
if (gl_manager) {
gl_manager->window_destroy(E.key);
}
#endif
Expand All @@ -4693,15 +4693,15 @@ DisplayServerX11::~DisplayServerX11() {

//destroy drivers
#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
if (rendering_device_vulkan) {
rendering_device_vulkan->finalize();
memdelete(rendering_device_vulkan);
}
if (rendering_device_vulkan) {
rendering_device_vulkan->finalize();
memdelete(rendering_device_vulkan);
rendering_device_vulkan = nullptr;
}

if (context_vulkan) {
memdelete(context_vulkan);
}
if (context_vulkan) {
memdelete(context_vulkan);
context_vulkan = nullptr;
}
#endif

Expand Down
54 changes: 25 additions & 29 deletions platform/osx/display_server_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ - (void)windowWillClose:(NSNotification *)notification {
}

#if defined(GLES3_ENABLED)
if (DS_OSX->rendering_driver == "opengl3") {
if (DS_OSX->gl_manager) {
DS_OSX->gl_manager->window_destroy(window_id);
}
#endif
#ifdef VULKAN_ENABLED
if (DS_OSX->rendering_driver == "vulkan") {
if (DS_OSX->context_vulkan) {
DS_OSX->context_vulkan->window_destroy(window_id);
}
#endif
Expand Down Expand Up @@ -272,12 +272,12 @@ - (void)windowDidResize:(NSNotification *)notification {
}

#if defined(GLES3_ENABLED)
if (DS_OSX->rendering_driver == "opengl3") {
if (DS_OSX->gl_manager) {
DS_OSX->gl_manager->window_resize(window_id, wd.size.width, wd.size.height);
}
#endif
#if defined(VULKAN_ENABLED)
if (DS_OSX->rendering_driver == "vulkan") {
if (DS_OSX->context_vulkan) {
DS_OSX->context_vulkan->window_resize(window_id, wd.size.width, wd.size.height);
}
#endif
Expand Down Expand Up @@ -403,7 +403,7 @@ + (void)initialize {

- (CALayer *)makeBackingLayer {
#if defined(VULKAN_ENABLED)
if (DS_OSX->rendering_driver == "vulkan") {
if (DS_OSX->context_vulkan) {
CALayer *layer = [[CAMetalLayer class] layer];
return layer;
}
Expand All @@ -413,12 +413,12 @@ - (CALayer *)makeBackingLayer {

- (void)updateLayer {
#if defined(GLES3_ENABLED)
if (DS_OSX->rendering_driver == "opengl3") {
if (DS_OSX->gl_manager) {
DS_OSX->gl_manager->window_update(window_id);
}
#endif
#if defined(VULKAN_ENABLED)
if (DS_OSX->rendering_driver == "vulkan") {
if (DS_OSX->context_vulkan) {
[super updateLayer];
}
#endif
Expand Down Expand Up @@ -2577,12 +2577,12 @@ static void displays_arrangement_changed(CGDirectDisplayID display_id, CGDisplay
[layer setOpaque:NO];
}
#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
if (context_vulkan) {
//TODO - implement transparency for Vulkan
}
#endif
#if defined(GLES3_ENABLED)
if (rendering_driver == "opengl3") {
if (gl_manager) {
//TODO - reimplement OpenGLES
}
#endif
Expand All @@ -2596,24 +2596,24 @@ static void displays_arrangement_changed(CGDirectDisplayID display_id, CGDisplay
[layer setOpaque:YES];
}
#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
if (context_vulkan) {
//TODO - implement transparency for Vulkan
}
#endif
#if defined(GLES3_ENABLED)
if (rendering_driver == "opengl3") {
if (gl_manager) {
//TODO - reimplement OpenGLES
}
#endif
wd.layered_window = false;
}
#if defined(GLES3_ENABLED)
if (rendering_driver == "opengl3") {
if (gl_manager) {
//TODO - reimplement OpenGLES
}
#endif
#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
if (context_vulkan) {
//TODO - implement transparency for Vulkan
}
#endif
Expand Down Expand Up @@ -3451,12 +3451,12 @@ void _update_keyboard_layouts() {
void DisplayServerOSX::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) {
_THREAD_SAFE_METHOD_
#if defined(GLES3_ENABLED)
if (rendering_driver == "opengl3") {
if (gl_manager) {
gl_manager->swap_buffers();
}
#endif
#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
if (context_vulkan) {
context_vulkan->set_vsync_mode(p_window, p_vsync_mode);
}
#endif
Expand All @@ -3465,12 +3465,12 @@ void _update_keyboard_layouts() {
DisplayServer::VSyncMode DisplayServerOSX::window_get_vsync_mode(WindowID p_window) const {
_THREAD_SAFE_METHOD_
#if defined(GLES3_ENABLED)
if (rendering_driver == "opengl3") {
if (gl_manager) {
return (gl_manager->is_using_vsync() ? DisplayServer::VSyncMode::VSYNC_ENABLED : DisplayServer::VSyncMode::VSYNC_DISABLED);
}
#endif
#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
if (context_vulkan) {
return context_vulkan->get_vsync_mode(p_window);
}
#endif
Expand Down Expand Up @@ -3586,19 +3586,15 @@ void _update_keyboard_layouts() {
}

#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
if (context_vulkan) {
Error err = context_vulkan->window_create(window_id_counter, p_vsync_mode, wd.window_view, p_rect.size.width, p_rect.size.height);
ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create a Vulkan context");
}
if (context_vulkan) {
Error err = context_vulkan->window_create(window_id_counter, p_vsync_mode, wd.window_view, p_rect.size.width, p_rect.size.height);
ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create a Vulkan context");
}
#endif
#if defined(GLES3_ENABLED)
if (rendering_driver == "opengl3") {
if (gl_manager) {
Error err = gl_manager->window_create(window_id_counter, wd.window_view, p_rect.size.width, p_rect.size.height);
ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create an OpenGL context");
}
if (gl_manager) {
Error err = gl_manager->window_create(window_id_counter, wd.window_view, p_rect.size.width, p_rect.size.height);
ERR_FAIL_COND_V_MSG(err != OK, INVALID_WINDOW_ID, "Can't create an OpenGL context");
}
#endif
id = window_id_counter++;
Expand All @@ -3618,12 +3614,12 @@ void _update_keyboard_layouts() {
}

#if defined(GLES3_ENABLED)
if (rendering_driver == "opengl3") {
if (gl_manager) {
gl_manager->window_resize(id, wd.size.width, wd.size.height);
}
#endif
#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
if (context_vulkan) {
context_vulkan->window_resize(id, wd.size.width, wd.size.height);
}
#endif
Expand Down
31 changes: 16 additions & 15 deletions platform/windows/display_server_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,12 @@ void DisplayServerWindows::delete_sub_window(WindowID p_window) {
}

#ifdef VULKAN_ENABLED
if (rendering_driver == "vulkan") {
if (context_vulkan) {
context_vulkan->window_destroy(p_window);
}
#endif
#ifdef GLES3_ENABLED
if (rendering_driver == "opengl3") {
if (gl_manager) {
gl_manager->window_destroy(p_window);
}
#endif
Expand Down Expand Up @@ -828,12 +828,12 @@ void DisplayServerWindows::window_set_size(const Size2i p_size, WindowID p_windo
wd.height = h;

#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
if (context_vulkan) {
context_vulkan->window_resize(p_window, w, h);
}
#endif
#if defined(GLES3_ENABLED)
if (rendering_driver == "opengl3") {
if (gl_manager) {
gl_manager->window_resize(p_window, w, h);
}
#endif
Expand Down Expand Up @@ -2646,7 +2646,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
windows[window_id].height = window_h;

#if defined(VULKAN_ENABLED)
if ((rendering_driver == "vulkan") && window_created) {
if (context_vulkan && window_created) {
context_vulkan->window_resize(window_id, windows[window_id].width, windows[window_id].height);
}
#endif
Expand Down Expand Up @@ -3108,7 +3108,7 @@ DisplayServer::WindowID DisplayServerWindows::_create_window(WindowMode p_mode,
}

#ifdef VULKAN_ENABLED
if (rendering_driver == "vulkan") {
if (context_vulkan) {
if (context_vulkan->window_create(id, p_vsync_mode, wd.hWnd, hInstance, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top) == -1) {
memdelete(context_vulkan);
context_vulkan = nullptr;
Expand All @@ -3119,7 +3119,7 @@ DisplayServer::WindowID DisplayServerWindows::_create_window(WindowMode p_mode,
#endif

#ifdef GLES3_ENABLED
if (rendering_driver == "opengl3") {
if (gl_manager) {
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.");
}
Expand Down Expand Up @@ -3458,7 +3458,7 @@ DisplayServerWindows::~DisplayServerWindows() {

if (windows.has(MAIN_WINDOW_ID)) {
#ifdef VULKAN_ENABLED
if (rendering_driver == "vulkan") {
if (context_vulkan) {
context_vulkan->window_destroy(MAIN_WINDOW_ID);
}
#endif
Expand All @@ -3470,14 +3470,15 @@ DisplayServerWindows::~DisplayServerWindows() {
}

#if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") {
if (rendering_device_vulkan) {
rendering_device_vulkan->finalize();
memdelete(rendering_device_vulkan);
}
if (rendering_device_vulkan) {
rendering_device_vulkan->finalize();
memdelete(rendering_device_vulkan);
rendering_device_vulkan = nullptr;
}

if (context_vulkan)
memdelete(context_vulkan);
if (context_vulkan) {
memdelete(context_vulkan);
context_vulkan = nullptr;
}
#endif

Expand Down
Loading

0 comments on commit 8485d0c

Please sign in to comment.