From 11c0601a67107c20afb47d148d55578368bde828 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 5 Jul 2020 22:56:39 +1000 Subject: [PATCH] libretro: Work around hw render interface pointer being freed --- .../libretro_vulkan_host_display.cpp | 13 ++++++++----- .../libretro_vulkan_host_display.h | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/duckstation-libretro/libretro_vulkan_host_display.cpp b/src/duckstation-libretro/libretro_vulkan_host_display.cpp index 1d1810e88..676ce2e58 100644 --- a/src/duckstation-libretro/libretro_vulkan_host_display.cpp +++ b/src/duckstation-libretro/libretro_vulkan_host_display.cpp @@ -128,8 +128,11 @@ bool LibretroVulkanHostDisplay::CreateRenderDevice(const WindowInfo& wi, std::st return false; } + // Keeping the pointer instead of memcpying can cause crashes, e.g. fullscreen switches. + std::memcpy(&m_ri, reinterpret_cast(ri), + sizeof(retro_hw_render_interface_vulkan)); + // TODO: Grab queue? it should be the same - m_ri = reinterpret_cast(ri); return true; } @@ -203,13 +206,13 @@ bool LibretroVulkanHostDisplay::Render() vkCmdEndRenderPass(cmdbuffer); m_frame_texture.TransitionToLayout(cmdbuffer, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); m_frame_view.image_layout = m_frame_texture.GetLayout(); - m_ri->set_image(m_ri->handle, &m_frame_view, 0, nullptr, VK_QUEUE_FAMILY_IGNORED); + m_ri.set_image(m_ri.handle, &m_frame_view, 0, nullptr, VK_QUEUE_FAMILY_IGNORED); // TODO: We can't use this because it doesn't support passing fences... - // m_ri->set_command_buffers(m_ri->handle, 1, &cmdbuffer); - m_ri->lock_queue(m_ri->handle); + // m_ri.set_command_buffers(m_ri.handle, 1, &cmdbuffer); + m_ri.lock_queue(m_ri.handle); g_vulkan_context->SubmitCommandBuffer(); - m_ri->unlock_queue(m_ri->handle); + m_ri.unlock_queue(m_ri.handle); g_vulkan_context->MoveToNextCommandBuffer(); g_retro_video_refresh_callback(RETRO_HW_FRAME_BUFFER_VALID, display_width, display_height, 0); diff --git a/src/duckstation-libretro/libretro_vulkan_host_display.h b/src/duckstation-libretro/libretro_vulkan_host_display.h index c5d22df1d..9b7f1ce46 100644 --- a/src/duckstation-libretro/libretro_vulkan_host_display.h +++ b/src/duckstation-libretro/libretro_vulkan_host_display.h @@ -33,7 +33,7 @@ private: bool CheckFramebufferSize(u32 width, u32 height); - const retro_hw_render_interface_vulkan* m_ri = nullptr; + retro_hw_render_interface_vulkan m_ri; Vulkan::Texture m_frame_texture; retro_vulkan_image m_frame_view = {};