From d77725bc26cadbbfc6f3a5e00c162a4b57d7964e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 17 May 2025 10:17:36 +0200 Subject: [PATCH] libretro: Fix GPU init. Had to revert to the old way for libretro, something is ordering-sensitive here. Fixes #20350 --- Core/System.cpp | 37 +++++++++++++++++++------------ libretro/LibretroD3D11Context.cpp | 7 +++++- libretro/README_WINDOWS.txt | 12 ++++++---- libretro/libretro.cpp | 4 +++- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/Core/System.cpp b/Core/System.cpp index b2e41a986f..41d00f0738 100644 --- a/Core/System.cpp +++ b/Core/System.cpp @@ -512,6 +512,21 @@ void PSP_ForceDebugStats(bool enable) { _assert_(coreCollectDebugStatsCounter >= 0); } +static void InitGPU(std::string *error_string) { + if (!gpu) { // should be! + INFO_LOG(Log::Loader, "Starting graphics..."); + Draw::DrawContext *draw = g_CoreParameter.graphicsContext ? g_CoreParameter.graphicsContext->GetDrawContext() : nullptr; + // This set the `gpu` global. + GPUCore gpuCore = PSP_CoreParameter().gpuCore; + bool success = GPU_Init(gpuCore, g_CoreParameter.graphicsContext, draw); + if (!success) { + *error_string = "Unable to initialize rendering engine."; + CPU_Shutdown(false); + g_bootState = BootState::Failed; + } + } +} + bool PSP_InitStart(const CoreParameter &coreParam) { if (g_bootState != BootState::Off) { ERROR_LOG(Log::Loader, "Can't start loader thread - already on."); @@ -576,20 +591,10 @@ bool PSP_InitStart(const CoreParameter &coreParam) { } // Initialize the GPU as far as we can here (do things like load cache files). - - if (!gpu) { // should be! - INFO_LOG(Log::Loader, "Starting graphics..."); - Draw::DrawContext *draw = g_CoreParameter.graphicsContext ? g_CoreParameter.graphicsContext->GetDrawContext() : nullptr; - // This set the `gpu` global. - GPUCore gpuCore = PSP_CoreParameter().gpuCore; - bool success = GPU_Init(gpuCore, g_CoreParameter.graphicsContext, draw); - if (!success) { - *error_string = "Unable to initialize rendering engine."; - CPU_Shutdown(false); - g_bootState = BootState::Failed; - } - } - + _dbg_assert_(!gpu); +#ifndef __LIBRETRO__ + InitGPU(error_string); +#endif g_bootState = BootState::Complete; }); @@ -619,6 +624,10 @@ BootState PSP_InitUpdate(std::string *error_string) { return BootState::Failed; } +#ifdef __LIBRETRO__ + InitGPU(error_string); +#endif + // Ok, async part of the boot completed, let's finish up things on the main thread. if (gpu) { gpu->FinishInitOnMainThread(); diff --git a/libretro/LibretroD3D11Context.cpp b/libretro/LibretroD3D11Context.cpp index 55d1613ef0..5950e35222 100644 --- a/libretro/LibretroD3D11Context.cpp +++ b/libretro/LibretroD3D11Context.cpp @@ -22,11 +22,16 @@ bool LibretroD3D11Context::Init() { void LibretroD3D11Context::CreateDrawContext() { retro_hw_render_interface_d3d11 *d3d11Interface = nullptr; - if (!Libretro::environ_cb(RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE, (void **)&d3d11Interface) || !d3d11Interface) { + if (!Libretro::environ_cb(RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE, (void **)&d3d11Interface)) { ERROR_LOG(Log::G3D, "Failed to get HW rendering interface!\n"); return; } + if (!d3d11Interface) { + ERROR_LOG(Log::G3D, "D3D11 interface was null!\n"); + return; + } + if (d3d11Interface->interface_version != RETRO_HW_RENDER_INTERFACE_D3D11_VERSION) { ERROR_LOG(Log::G3D, "HW render interface mismatch, expected %u, got %u!\n", RETRO_HW_RENDER_INTERFACE_D3D11_VERSION, d3d11Interface->interface_version); return; diff --git a/libretro/README_WINDOWS.txt b/libretro/README_WINDOWS.txt index 4eb43f7d9d..86db0d7523 100644 --- a/libretro/README_WINDOWS.txt +++ b/libretro/README_WINDOWS.txt @@ -10,14 +10,18 @@ pacman -S make Start Retroarch ones to create the cores directory. -Then use the following in msys: +Then use the following in msys (edit the last part as appropriate): -> cd libretro -> make platform=windows_msvc2019_desktop_x64 -j32 && cp ppsspp_libretro.* /d/retroarch/cores +``` +cd libretro +make platform=windows_msvc2019_desktop_x64 -j32 && cp ppsspp_libretro.* /d/retroarch/cores +``` To make a debug build: -> make DEBUG=1 platform=windows_msvc2019_desktop_x64 -j32 +``` +make DEBUG=1 platform=windows_msvc2019_desktop_x64 -j32 && cp ppsspp_libretro.* /d/retroarch/cores +``` Note that the latter part copies the DLL/PDB into wherever retroarch reads it from. Might need to adjust the path, and adjust -j32 depending on your number of logical CPUs - might not need that many threads (or you might need more...). diff --git a/libretro/libretro.cpp b/libretro/libretro.cpp index 727012b5b8..428dbbdad3 100644 --- a/libretro/libretro.cpp +++ b/libretro/libretro.cpp @@ -1321,8 +1321,9 @@ namespace Libretro static void EmuFrame() { ctx->SetRenderTarget(); - if (ctx->GetDrawContext()) + if (ctx->GetDrawContext()) { ctx->GetDrawContext()->BeginFrame(Draw::DebugFlags::NONE); + } if (gpu) gpu->BeginHostFrame(); @@ -1451,6 +1452,7 @@ bool retro_load_game(const struct retro_game_info *game) retro_check_backend(); ctx = LibretroGraphicsContext::CreateGraphicsContext(); + INFO_LOG(Log::System, "Using %s backend", ctx->Ident()); Core_SetGraphicsContext(ctx);