From 2786786c9f65566462cce340985c5b6e09bf2fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 21 Mar 2023 12:36:57 +0100 Subject: [PATCH] Avoid Host in Windows graphics init --- Core/Host.h | 2 +- UI/NativeApp.cpp | 6 ------ Windows/EmuThread.cpp | 43 +++++++++++++++++++++++++++++++++++++++-- Windows/WindowsHost.cpp | 41 +-------------------------------------- Windows/WindowsHost.h | 3 --- 5 files changed, 43 insertions(+), 52 deletions(-) diff --git a/Core/Host.h b/Core/Host.h index 920f8f087a..c6399370ab 100644 --- a/Core/Host.h +++ b/Core/Host.h @@ -30,7 +30,7 @@ public: virtual bool InitGraphics(std::string *error_string, GraphicsContext **ctx) = 0; virtual void ShutdownGraphics() = 0; - virtual void UpdateSound() {} + virtual void UpdateSound() {} // still needed for libretro, will need a proper effort. virtual void PollControllers() {} virtual void ToggleDebugConsoleVisibility() {} diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index d0d9746f88..e2c05d2b31 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -1426,12 +1426,6 @@ void NativeShutdown() { screenManager = nullptr; } - if (host) { - host->ShutdownGraphics(); - delete host; - host = nullptr; - } - #if !PPSSPP_PLATFORM(UWP) #endif g_Config.Save("NativeShutdown"); diff --git a/Windows/EmuThread.cpp b/Windows/EmuThread.cpp index 5c3798bc7c..4748557d18 100644 --- a/Windows/EmuThread.cpp +++ b/Windows/EmuThread.cpp @@ -26,6 +26,13 @@ #include "Core/Config.h" #include "Core/ConfigValues.h" +#if PPSSPP_API(ANY_GL) +#include "Windows/GPU/WindowsGLContext.h" +#endif +#include "Windows/GPU/WindowsVulkanContext.h" +#include "Windows/GPU/D3D9Context.h" +#include "Windows/GPU/D3D11Context.h" + enum class EmuThreadState { DISABLED, START_REQUESTED, @@ -112,6 +119,37 @@ static void EmuThreadJoin() { INFO_LOG(SYSTEM, "EmuThreadJoin - joined"); } +bool CreateGraphicsBackend(std::string *error_message, GraphicsContext **ctx) { + WindowsGraphicsContext *graphicsContext = nullptr; + switch (g_Config.iGPUBackend) { +#if PPSSPP_API(ANY_GL) + case (int)GPUBackend::OPENGL: + graphicsContext = new WindowsGLContext(); + break; +#endif + case (int)GPUBackend::DIRECT3D9: + graphicsContext = new D3D9Context(); + break; + case (int)GPUBackend::DIRECT3D11: + graphicsContext = new D3D11Context(); + break; + case (int)GPUBackend::VULKAN: + graphicsContext = new WindowsVulkanContext(); + break; + default: + return false; + } + + if (graphicsContext->Init(MainWindow::GetHInstance(), MainWindow::GetDisplayHWND(), error_message)) { + *ctx = graphicsContext; + return true; + } else { + delete graphicsContext; + *ctx = nullptr; + return false; + } +} + void MainThreadFunc() { if (useEmuThread) { // We'll start up a separate thread we'll call Emu @@ -164,7 +202,7 @@ void MainThreadFunc() { System_Notify(SystemNotification::UI); std::string error_string; - bool success = host->InitGraphics(&error_string, &g_graphicsContext); + bool success = CreateGraphicsBackend(&error_string, &g_graphicsContext); if (success) { // Main thread is the render thread. @@ -299,7 +337,8 @@ shutdown: g_graphicsContext->ThreadEnd(); g_graphicsContext->ShutdownFromRenderThread(); - // NativeShutdown deletes the graphics context through host->ShutdownGraphics(). + g_graphicsContext->Shutdown(); + NativeShutdown(); PostMessage(MainWindow::GetHWND(), MainWindow::WM_USER_UPDATE_UI, 0, 0); diff --git a/Windows/WindowsHost.cpp b/Windows/WindowsHost.cpp index f12f744344..5c2b09c666 100644 --- a/Windows/WindowsHost.cpp +++ b/Windows/WindowsHost.cpp @@ -53,13 +53,6 @@ #include "Windows/WindowsHost.h" #include "Windows/MainWindow.h" -#if PPSSPP_API(ANY_GL) -#include "Windows/GPU/WindowsGLContext.h" -#endif -#include "Windows/GPU/WindowsVulkanContext.h" -#include "Windows/GPU/D3D9Context.h" -#include "Windows/GPU/D3D11Context.h" - #include "Windows/Debugger/DebuggerShared.h" #include "Windows/Debugger/Debugger_Disasm.h" #include "Windows/Debugger/Debugger_MemoryDlg.h" @@ -117,42 +110,10 @@ void WindowsHost::UpdateConsolePosition() { } bool WindowsHost::InitGraphics(std::string *error_message, GraphicsContext **ctx) { - WindowsGraphicsContext *graphicsContext = nullptr; - switch (g_Config.iGPUBackend) { -#if PPSSPP_API(ANY_GL) - case (int)GPUBackend::OPENGL: - graphicsContext = new WindowsGLContext(); - break; -#endif - case (int)GPUBackend::DIRECT3D9: - graphicsContext = new D3D9Context(); - break; - case (int)GPUBackend::DIRECT3D11: - graphicsContext = new D3D11Context(); - break; - case (int)GPUBackend::VULKAN: - graphicsContext = new WindowsVulkanContext(); - break; - default: - return false; - } - - if (graphicsContext->Init(hInstance_, displayWindow_, error_message)) { - *ctx = graphicsContext; - gfx_ = graphicsContext; - return true; - } else { - delete graphicsContext; - *ctx = nullptr; - gfx_ = nullptr; - return false; - } + return true; } void WindowsHost::ShutdownGraphics() { - gfx_->Shutdown(); - delete gfx_; - gfx_ = nullptr; } void WindowsHost::SetWindowTitle(const char *message) { diff --git a/Windows/WindowsHost.h b/Windows/WindowsHost.h index 36682955a2..3717bf5291 100644 --- a/Windows/WindowsHost.h +++ b/Windows/WindowsHost.h @@ -24,8 +24,6 @@ extern float g_mouseDeltaX; extern float g_mouseDeltaY; -class GraphicsContext; - class WindowsHost : public Host { public: WindowsHost(HINSTANCE hInstance, HWND mainWindow, HWND displayWindow); @@ -61,7 +59,6 @@ private: HINSTANCE hInstance_; HWND displayWindow_; HWND mainWindow_; - GraphicsContext *gfx_ = nullptr; size_t numDinputDevices_ = 0; std::wstring lastTitle_;