From a83c95ab9bbddf220f291da172450f51e0a76be1 Mon Sep 17 00:00:00 2001 From: sonninnos Date: Tue, 28 May 2024 08:07:20 +0300 Subject: [PATCH] libretro: context cleanup --- libretro/LibretroGLContext.cpp | 2 +- libretro/LibretroGLCoreContext.cpp | 2 +- libretro/LibretroGraphicsContext.cpp | 29 ++++++++++++++-------------- libretro/LibretroVulkanContext.cpp | 18 +++++++++-------- libretro/LibretroVulkanContext.h | 4 ++-- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/libretro/LibretroGLContext.cpp b/libretro/LibretroGLContext.cpp index 240b3e1e1a..45a33eb69b 100644 --- a/libretro/LibretroGLContext.cpp +++ b/libretro/LibretroGLContext.cpp @@ -8,7 +8,7 @@ #include "libretro/LibretroGLContext.h" bool LibretroGLContext::Init() { - if (!LibretroHWRenderContext::Init(true)) + if (!LibretroHWRenderContext::Init(false)) return false; g_Config.iGPUBackend = (int)GPUBackend::OPENGL; diff --git a/libretro/LibretroGLCoreContext.cpp b/libretro/LibretroGLCoreContext.cpp index 54e324c4c7..98fc09ca08 100644 --- a/libretro/LibretroGLCoreContext.cpp +++ b/libretro/LibretroGLCoreContext.cpp @@ -8,7 +8,7 @@ #include "libretro/LibretroGLCoreContext.h" bool LibretroGLCoreContext::Init() { - if (!LibretroHWRenderContext::Init(true)) + if (!LibretroHWRenderContext::Init(false)) return false; g_Config.iGPUBackend = (int)GPUBackend::OPENGL; diff --git a/libretro/LibretroGraphicsContext.cpp b/libretro/LibretroGraphicsContext.cpp index afd93be889..59888c0fac 100644 --- a/libretro/LibretroGraphicsContext.cpp +++ b/libretro/LibretroGraphicsContext.cpp @@ -2,7 +2,6 @@ #include "libretro/LibretroGraphicsContext.h" #include "libretro/LibretroGLContext.h" #include "libretro/LibretroGLCoreContext.h" -#include "libretro/libretro.h" #include "libretro/LibretroVulkanContext.h" #ifdef _WIN32 #include "libretro/LibretroD3D11Context.h" @@ -26,9 +25,9 @@ static void context_destroy() { ((LibretroHWRenderContext *)Libretro::ctx)->Cont bool LibretroHWRenderContext::Init(bool cache_context) { hw_render_.cache_context = cache_context; if (!Libretro::environ_cb(RETRO_ENVIRONMENT_SET_HW_RENDER, &hw_render_)) - return false; - libretro_get_proc_address = hw_render_.get_proc_address; - return true; + return false; + libretro_get_proc_address = hw_render_.get_proc_address; + return true; } LibretroHWRenderContext::LibretroHWRenderContext(retro_hw_context_type context_type, unsigned version_major, unsigned version_minor) { @@ -43,10 +42,8 @@ LibretroHWRenderContext::LibretroHWRenderContext(retro_hw_context_type context_t void LibretroHWRenderContext::ContextReset() { INFO_LOG(G3D, "Context reset"); - // needed to restart the thread - // TODO: find a way to move this to ContextDestroy. - if (!hw_render_.cache_context && Libretro::useEmuThread && draw_ && Libretro::emuThreadState != Libretro::EmuThreadState::PAUSED) { - DestroyDrawContext(); + if (gpu) { + gpu->DeviceLost(); } if (!draw_) { @@ -66,11 +63,15 @@ void LibretroHWRenderContext::ContextDestroy() { INFO_LOG(G3D, "Context destroy"); if (Libretro::useEmuThread) { -#if 0 - Libretro::EmuThreadPause(); -#else Libretro::EmuThreadStop(); -#endif + } + + if (gpu) { + gpu->DeviceLost(); + } + + if (!hw_render_.cache_context && Libretro::useEmuThread && draw_ && Libretro::emuThreadState != Libretro::EmuThreadState::PAUSED) { + DestroyDrawContext(); } if (!hw_render_.cache_context && !Libretro::useEmuThread) { @@ -139,6 +140,6 @@ LibretroGraphicsContext *LibretroGraphicsContext::CreateGraphicsContext() { #endif ctx = new LibretroSoftwareContext(); - ctx->Init(); - return ctx; + ctx->Init(); + return ctx; } diff --git a/libretro/LibretroVulkanContext.cpp b/libretro/LibretroVulkanContext.cpp index 125563f0e3..c7e125ba6c 100644 --- a/libretro/LibretroVulkanContext.cpp +++ b/libretro/LibretroVulkanContext.cpp @@ -41,7 +41,7 @@ static bool create_device(retro_vulkan_context *context, VkInstance instance, Vk vk = new VulkanContext(); - vk_libretro_init(instance, gpu, surface, get_instance_proc_addr, required_device_extensions, num_required_device_extensions, required_device_layers, num_required_device_layers, required_features); + vk_libretro_init(instance, gpu, surface, get_instance_proc_addr, required_device_extensions, num_required_device_extensions, required_device_layers, num_required_device_layers, required_features); // TODO: Here we'll inject the instance and all of the stuff into the VulkanContext. @@ -97,17 +97,17 @@ static const VkApplicationInfo *GetApplicationInfo(void) { } bool LibretroVulkanContext::Init() { - if (!LibretroHWRenderContext::Init(false)) { + if (!LibretroHWRenderContext::Init(true)) { return false; } static const struct retro_hw_render_context_negotiation_interface_vulkan iface = { - RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN, - RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN_VERSION, - GetApplicationInfo, - create_device, // Callback above. - nullptr, - }; + RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN, + RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN_VERSION, + GetApplicationInfo, + create_device, // Callback above. + nullptr, + }; Libretro::environ_cb(RETRO_ENVIRONMENT_SET_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE, (void *)&iface); g_Config.iGPUBackend = (int)GPUBackend::VULKAN; @@ -131,6 +131,8 @@ void LibretroVulkanContext::ContextReset() { void LibretroVulkanContext::ContextDestroy() { INFO_LOG(G3D, "LibretroVulkanContext::ContextDestroy()"); + vk->WaitUntilQueueIdle(); + LibretroHWRenderContext::ContextDestroy(); } void LibretroVulkanContext::CreateDrawContext() { diff --git a/libretro/LibretroVulkanContext.h b/libretro/LibretroVulkanContext.h index c6d3ea4ac6..49227f6b3a 100644 --- a/libretro/LibretroVulkanContext.h +++ b/libretro/LibretroVulkanContext.h @@ -13,8 +13,8 @@ public: void *GetAPIContext() override; void CreateDrawContext() override; - void ContextReset() override; - void ContextDestroy() override; + void ContextReset() override; + void ContextDestroy() override; GPUCore GetGPUCore() override { return GPUCORE_VULKAN; } const char *Ident() override { return "Vulkan"; }