From 54770ef3cc06c48d31474a4b48b192e764edf0f7 Mon Sep 17 00:00:00 2001 From: BearOso Date: Mon, 20 Feb 2023 15:15:38 -0600 Subject: [PATCH] win32: Try to handle race in changing drivers. --- win32/CVulkan.cpp | 10 ++++++++-- win32/win32_display.cpp | 22 +++++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/win32/CVulkan.cpp b/win32/CVulkan.cpp index 345e1a61..4e37ae62 100644 --- a/win32/CVulkan.cpp +++ b/win32/CVulkan.cpp @@ -14,9 +14,15 @@ bool CVulkan::Initialize(HWND hWnd) current_width = window_rect.right - window_rect.left; current_height = window_rect.bottom - window_rect.top; - context = std::make_unique(); - if (!context->init_win32(0, hWnd)) + try { + context = std::make_unique(); + if (!context->init_win32(0, hWnd)) + return false; + } + catch (std::exception& e) + { return false; + } if (GUI.shaderEnabled && GUI.OGLshaderFileName && GUI.OGLshaderFileName[0]) { diff --git a/win32/win32_display.cpp b/win32/win32_display.cpp index 69210564..12324697 100644 --- a/win32/win32_display.cpp +++ b/win32/win32_display.cpp @@ -107,6 +107,9 @@ returns true if successful, false otherwise bool WinDisplayReset(void) { S9xDisplayOutput->DeInitialize(); + if (S9xDisplayOutput == (IS9xDisplayOutput *)&VulkanDriver && GUI.outputMethod == OPENGL) + Sleep(500); + switch(GUI.outputMethod) { default: case DIRECT3D: @@ -124,14 +127,23 @@ bool WinDisplayReset(void) S9xDisplayOutput = &VulkanDriver; break; } - if(S9xDisplayOutput->Initialize(GUI.hWnd)) { + + bool initialized = S9xDisplayOutput->Initialize(GUI.hWnd); + + if (!initialized) { + S9xDisplayOutput->DeInitialize(); + Sleep(500); + initialized = S9xDisplayOutput->Initialize(GUI.hWnd); + } + + if (initialized) { S9xGraphicsDeinit(); - S9xSetWinPixelFormat (); + S9xSetWinPixelFormat(); S9xGraphicsInit(); - if (GUI.DWMSync) - { - HMODULE dwmlib = LoadLibrary(TEXT("dwmapi")); + if (GUI.DWMSync) + { + HMODULE dwmlib = LoadLibrary(TEXT("dwmapi")); DwmFlushProc = (DWMFLUSHPROC)GetProcAddress(dwmlib, "DwmFlush"); DwmIsCompositionEnabledProc = (DWMISCOMPOSITIONENABLEDPROC)GetProcAddress(dwmlib, "DwmIsCompositionEnabled");