Windows: Move the graphics context creation to main.cpp

This commit is contained in:
Henrik Rydgård committed 2026-07-21 13:13:01 +02:00
1 parent cc69070f19
commit 32f975ef85
3 files changed
+127 -119

No files matched your search

+6 -117
View File
@@ -20,18 +20,11 @@
#include "Windows/W32Util/Misc.h"
#include "Windows/MainWindow.h"
#include "Core/EmuThread.h"
#include "Core/Reporting.h"
#include "Core/Core.h"
#include "Core/System.h"
#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/D3D11Context.h"
enum class EmuThreadState {
DISABLED,
START_REQUESTED,
@@ -47,9 +40,8 @@ static std::string g_error_message;
static bool g_inLoop;
class GraphicsContext;
static GraphicsContext *g_graphicsContext;
void MainThreadFunc();
void MainThreadFunc(GraphicsContext *graphicsContext);
bool MainThread_Ready() {
return g_inLoop;
@@ -102,111 +94,13 @@ static void EmuThreadJoin() {
INFO_LOG(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::DIRECT3D11:
graphicsContext = new D3D11Context();
break;
case (int)GPUBackend::VULKAN:
graphicsContext = new WindowsVulkanContext();
break;
default:
return false;
}
if (graphicsContext->Init(MainWindow::GetHInstance(), MainWindow::GetHWND(), error_message)) {
*ctx = graphicsContext;
return true;
} else {
delete graphicsContext;
*ctx = nullptr;
return false;
}
}
void MainThreadFunc() {
void MainThreadFunc(GraphicsContext *graphicsContext) {
const bool useEmuThread = g_Config.iGPUBackend == (int)GPUBackend::OPENGL;
SetCurrentThreadName(useEmuThread ? "RenderThread" : "EmuThread");
const bool performingRestart = NativeIsRestarting();
if (g_Config.sFailedGPUBackends.find("ALL") != std::string::npos) {
Reporting::ReportMessage("Graphics init error: %s", "ALL");
auto err = GetI18NCategory(I18NCat::ERRORS);
const char *defaultErrorAll = "PPSSPP failed to startup with any graphics backend. Try upgrading your graphics and other drivers.";
std::string_view genericError = err->T("GenericAllStartupError", defaultErrorAll);
std::wstring title = ConvertUTF8ToWString(err->T("GenericGraphicsError", "Graphics Error"));
MessageBox(0, ConvertUTF8ToWString(genericError).c_str(), title.c_str(), MB_OK);
// Let's continue (and probably crash) just so they have a way to keep trying.
}
System_Notify(SystemNotification::UI);
std::string error_string;
bool success = CreateGraphicsBackend(&error_string, &g_graphicsContext);
if (success) {
// Main thread is the render thread.
success = g_graphicsContext->InitFromRenderThread(&error_string);
}
if (!success) {
// Before anything: are we restarting right now?
if (performingRestart) {
// Okay, switching graphics didn't work out. Probably a driver bug - fallback to restart.
// This happens on NVIDIA when switching OpenGL -> Vulkan.
g_Config.Save("switch_graphics_failed");
W32Util::ExitAndRestart();
}
auto err = GetI18NCategory(I18NCat::ERRORS);
Reporting::ReportMessage("Graphics init error: %s", error_string.c_str());
const char *defaultErrorVulkan = "Failed initializing graphics. Try upgrading your graphics drivers.\n\nWould you like to try switching to OpenGL?\n\nError message:";
const char *defaultErrorOpenGL = "Failed initializing graphics. Try upgrading your graphics drivers.\n\nWould you like to try switching to DirectX 9?\n\nError message:";
const char *defaultErrorDirect3D9 = "Failed initializing graphics. Try upgrading your graphics drivers and directx 9 runtime.\n\nWould you like to try switching to OpenGL?\n\nError message:";
std::string_view genericError;
GPUBackend nextBackend = GPUBackend::VULKAN;
switch (g_Config.iGPUBackend) {
case (int)GPUBackend::VULKAN:
nextBackend = GPUBackend::OPENGL;
genericError = err->T("GenericVulkanError", defaultErrorVulkan);
break;
case (int)GPUBackend::OPENGL:
default:
nextBackend = GPUBackend::DIRECT3D11;
genericError = err->T("GenericOpenGLError", defaultErrorOpenGL);
break;
}
std::string full_error = StringFromFormat("%.*s\n\n%s", (int)genericError.size(), genericError.data(), error_string.c_str());
std::wstring title = ConvertUTF8ToWString(err->T("GenericGraphicsError", "Graphics Error"));
bool yes = IDYES == MessageBox(0, ConvertUTF8ToWString(full_error).c_str(), title.c_str(), MB_ICONERROR | MB_YESNO);
ERROR_LOG(Log::Boot, "%s", full_error.c_str());
if (yes) {
// Change the config to the alternative and restart.
g_Config.iGPUBackend = (int)nextBackend;
// Clear this to ensure we try their selection.
g_Config.sFailedGPUBackends.clear();
g_Config.Save("save_graphics_fallback");
W32Util::ExitAndRestart();
}
// No safe way out without graphics.
ExitProcess(1);
}
GraphicsContext *graphicsContext = g_graphicsContext;
bool success = graphicsContext->InitFromRenderThread(&error_string);
if (!useEmuThread) {
NativeInitGraphics(graphicsContext);
@@ -266,14 +160,9 @@ void MainThreadFunc() {
NativeShutdownGraphics();
}
g_graphicsContext->ThreadEnd();
g_graphicsContext->ShutdownFromRenderThread();
g_graphicsContext->Shutdown();
delete g_graphicsContext;
g_graphicsContext = nullptr;
PostMessage(MainWindow::GetHWND(), MainWindow::WM_USER_UPDATE_UI, 0, 0);
graphicsContext->ThreadEnd();
graphicsContext->ShutdownFromRenderThread();
graphicsContext->Shutdown();
}
#endif