Skip to content

Commit

Permalink
Windows: Avoid restarting completely on GPU change.
Browse files Browse the repository at this point in the history
This retains the logger, avoids an annoying window open/close, and most
importantly for me: keeps the debugger attached.
  • Loading branch information
unknownbrackets committed Apr 15, 2017
1 parent 638a015 commit e8e6588
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Core/System.cpp
Expand Up @@ -97,6 +97,10 @@ static volatile CPUThreadState cpuThreadState = CPU_THREAD_NOT_RUNNING;

static GPUBackend gpuBackend;

void ResetUIState() {
globalUIState = UISTATE_MENU;
}

void UpdateUIState(GlobalUIState newState) {
// Never leave the EXIT state.
if (globalUIState != newState && globalUIState != UISTATE_EXIT) {
Expand Down
1 change: 1 addition & 0 deletions Core/System.h
Expand Up @@ -54,6 +54,7 @@ enum PSPDirectories {
class GraphicsContext;
enum class GPUBackend;

void ResetUIState();
void UpdateUIState(GlobalUIState newState);
GlobalUIState GetUIState();

Expand Down
3 changes: 1 addition & 2 deletions UI/GameSettingsScreen.cpp
Expand Up @@ -1057,8 +1057,7 @@ void GameSettingsScreen::CallbackRenderingBackend(bool yes) {
// If the user ends up deciding not to restart, set the config back to the current backend
// so it doesn't get switched by accident.
if (yes) {
g_Config.bRestartRequired = true;
PostMessage(MainWindow::GetHWND(), WM_CLOSE, 0, 0);
System_SendMessage("graphics_restart", "");
} else {
g_Config.iGPUBackend = (int)GetGPUBackend();
}
Expand Down
9 changes: 9 additions & 0 deletions Windows/EmuThread.cpp
Expand Up @@ -112,6 +112,7 @@ unsigned int WINAPI TheThread(void *)
args.push_back(string.c_str());
}

bool performingRestart = NativeIsRestarting();
NativeInit(static_cast<int>(args.size()), &args[0], "1234", "1234", nullptr);

Host *nativeHost = host;
Expand All @@ -123,6 +124,14 @@ unsigned int WINAPI TheThread(void *)

std::string error_string;
if (!host->InitGraphics(&error_string, &graphicsContext)) {
// 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();
W32Util::ExitAndRestart();
}

I18NCategory *err = GetI18NCategory("Error");
Reporting::ReportMessage("Graphics init error: %s", error_string.c_str());

Expand Down
8 changes: 8 additions & 0 deletions Windows/MainWindow.cpp
Expand Up @@ -882,6 +882,14 @@ namespace MainWindow
BrowseBackgroundDone();
break;

case WM_USER_RESTART_EMUTHREAD:
NativeSetRestarting();
EmuThread_Stop();
coreState = CORE_POWERUP;
ResetUIState();
EmuThread_Start();
break;

case WM_MENUSELECT:
// Called when a menu is opened. Also when an item is selected, but meh.
UpdateMenus(true);
Expand Down
1 change: 1 addition & 0 deletions Windows/MainWindow.h
Expand Up @@ -20,6 +20,7 @@ namespace MainWindow
WM_USER_WINDOW_TITLE_CHANGED = WM_USER + 103,
WM_USER_BROWSE_BOOT_DONE = WM_USER + 104,
WM_USER_TOGGLE_FULLSCREEN = WM_USER + 105,
WM_USER_RESTART_EMUTHREAD = WM_USER + 106,
};

enum {
Expand Down
12 changes: 4 additions & 8 deletions Windows/MainWindowMenu.cpp
Expand Up @@ -770,26 +770,22 @@ namespace MainWindow {

case ID_OPTIONS_DIRECT3D9:
g_Config.iGPUBackend = GPU_BACKEND_DIRECT3D9;
g_Config.bRestartRequired = true;
PostMessage(MainWindow::GetHWND(), WM_CLOSE, 0, 0);
PostMessage(MainWindow::GetHWND(), WM_USER_RESTART_EMUTHREAD, 0, 0);
break;

case ID_OPTIONS_DIRECT3D11:
g_Config.iGPUBackend = GPU_BACKEND_DIRECT3D11;
g_Config.bRestartRequired = true;
PostMessage(MainWindow::GetHWND(), WM_CLOSE, 0, 0);
PostMessage(MainWindow::GetHWND(), WM_USER_RESTART_EMUTHREAD, 0, 0);
break;

case ID_OPTIONS_OPENGL:
g_Config.iGPUBackend = GPU_BACKEND_OPENGL;
g_Config.bRestartRequired = true;
PostMessage(MainWindow::GetHWND(), WM_CLOSE, 0, 0);
PostMessage(MainWindow::GetHWND(), WM_USER_RESTART_EMUTHREAD, 0, 0);
break;

case ID_OPTIONS_VULKAN:
g_Config.iGPUBackend = GPU_BACKEND_VULKAN;
g_Config.bRestartRequired = true;
PostMessage(MainWindow::GetHWND(), WM_CLOSE, 0, 0);
PostMessage(MainWindow::GetHWND(), WM_USER_RESTART_EMUTHREAD, 0, 0);
break;

case ID_OPTIONS_NONBUFFEREDRENDERING: setRenderingMode(FB_NON_BUFFERED_MODE); break;
Expand Down
1 change: 0 additions & 1 deletion Windows/WindowsHost.cpp
Expand Up @@ -146,7 +146,6 @@ void WindowsHost::ShutdownGraphics() {
gfx_->Shutdown();
delete gfx_;
gfx_ = nullptr;
PostMessage(mainWindow_, WM_CLOSE, 0, 0);
}

void WindowsHost::SetWindowTitle(const char *message) {
Expand Down
6 changes: 5 additions & 1 deletion Windows/main.cpp
Expand Up @@ -231,7 +231,11 @@ int System_GetPropertyInt(SystemProperty prop) {

void System_SendMessage(const char *command, const char *parameter) {
if (!strcmp(command, "finish")) {
PostMessage(MainWindow::GetHWND(), WM_CLOSE, 0, 0);
if (!NativeIsRestarting()) {
PostMessage(MainWindow::GetHWND(), WM_CLOSE, 0, 0);
}
} else if (!strcmp(command, "graphics_restart")) {
PostMessage(MainWindow::GetHWND(), MainWindow::WM_USER_RESTART_EMUTHREAD, 0, 0);
} else if (!strcmp(command, "setclipboardtext")) {
if (OpenClipboard(MainWindow::GetDisplayHWND())) {
std::wstring data = ConvertUTF8ToWString(parameter);
Expand Down

0 comments on commit e8e6588

Please sign in to comment.