From 5a1d143ac2968ed06e8d8de7e9f4c721b4650c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 4 Nov 2026 12:24:30 +0100 Subject: [PATCH] VR: Restart in-process when changing the backend A graphics restart recreates the activity, but on VR devices onDestroy called System.exit, so the app just quit. The XR instance (created for the old activity), the GL session (entered once per process) and the controller input (bound to the first session) all outlived the activity. Now the native shutdown takes OpenXR down (ShutdownVR: renderer, session, instance), the new activity sets it up again, and the exit is skipped for restarts. Input is created per session, which also keeps the controllers working when Vulkan recreates its session with the graphics context. The passthrough layer is destroyed with the renderer. Co-Authored-By: Claude Opus 5.5 --- Common/VR/PPSSPPVR.cpp | 18 ++++++++++++++--- Common/VR/PPSSPPVR.h | 5 ++++- Common/VR/VRBase.cpp | 3 +++ Common/VR/VRInput.cpp | 20 +++++++++++++++++++ Common/VR/VRInput.h | 1 + Common/VR/VRRenderer.cpp | 8 ++++++++ Core/EmuThread.cpp | 4 +--- android/jni/app-android.cpp | 5 +++++ .../src/org/ppsspp/ppsspp/PpssppActivity.java | 8 ++++++-- docs/vr-openxr-vulkan.md | 6 +++++- 10 files changed, 68 insertions(+), 10 deletions(-) diff --git a/Common/VR/PPSSPPVR.cpp b/Common/VR/PPSSPPVR.cpp index e04a77b68b..a6b150c011 100644 --- a/Common/VR/PPSSPPVR.cpp +++ b/Common/VR/PPSSPPVR.cpp @@ -188,15 +188,27 @@ void InitVROnAndroid(void* vm, void* activity, const char* system, int version, } #endif -void EnterVR(bool firstStart) { - if (firstStart) { - engine_t* engine = VR_GetEngine(); +void EnterVR() { + engine_t* engine = VR_GetEngine(); + // The session outlives the render loop (it restarts when the surface comes back), not the native side. + if (engine->appState.Session == XR_NULL_HANDLE) { VR_EnterVR(engine); IN_VRInit(engine); } VR_SetConfig(VR_CONFIG_VIEWPORT_VALID, false); } +void ShutdownVR() { + engine_t* engine = VR_GetEngine(); + if (engine->appState.Instance == XR_NULL_HANDLE) { + return; + } + // GL's swapchains (Vulkan's go with its presentation). + VR_DestroyRenderer(engine); + VR_LeaveVR(engine); + VR_Destroy(engine); +} + void GetVRResolutionPerEye(int* width, int* height) { if (VR_GetEngine()->appState.Instance) { VR_GetResolution(VR_GetEngine(), width, height); diff --git a/Common/VR/PPSSPPVR.h b/Common/VR/PPSSPPVR.h index 3dde8b3871..0ce5ef0773 100644 --- a/Common/VR/PPSSPPVR.h +++ b/Common/VR/PPSSPPVR.h @@ -30,7 +30,10 @@ enum VRAppMode { // VR app flow integration bool IsVREnabled(); void InitVROnAndroid(void* vm, void* activity, const char* system, int version, const char* name); -void EnterVR(bool firstStart); +// GL: creates the session (once per instance, on the GL thread). +void EnterVR(); +// Tears down what InitVROnAndroid and EnterVR set up, so a new activity can start over in this process. +void ShutdownVR(); void GetVRResolutionPerEye(int* width, int* height); void SetVRCallbacks(void(*axis)(const AxisInput *axis, size_t count), bool(*key)(const KeyInput &key), void(*touch)(const TouchInput &touch)); diff --git a/Common/VR/VRBase.cpp b/Common/VR/VRBase.cpp index d92c506b2b..503476fea0 100644 --- a/Common/VR/VRBase.cpp +++ b/Common/VR/VRBase.cpp @@ -1,4 +1,5 @@ #include "VRBase.h" +#include "VRInput.h" #include #include @@ -165,6 +166,7 @@ void VR_Destroy( engine_t* engine ) { if (engine == &vr_engine) { xrDestroyInstance(engine->appState.Instance); ovrApp_Destroy(&engine->appState); + vr_initialized = 0; } } @@ -220,6 +222,7 @@ void VR_EnterVR( engine_t* engine ) { void VR_LeaveVR( engine_t* engine ) { if (engine->appState.Session) { + IN_VRShutdown(engine); OXR(xrDestroySpace(engine->appState.HeadSpace)); // StageSpace is optional. if (engine->appState.StageSpace != XR_NULL_HANDLE) { diff --git a/Common/VR/VRInput.cpp b/Common/VR/VRInput.cpp index 9a55562c37..f3be56b2fc 100644 --- a/Common/VR/VRInput.cpp +++ b/Common/VR/VRInput.cpp @@ -375,6 +375,26 @@ void IN_VRInit( engine_t *engine ) { inputInitialized = 1; } +void IN_VRShutdown( engine_t* engine ) { + if (!inputInitialized) { + return; + } + // The aim spaces belong to the session and the action set is attached to it, so a new session + // (a new graphics context or activity) needs new ones. + if (leftControllerAimSpace != XR_NULL_HANDLE) { + OXR(xrDestroySpace(leftControllerAimSpace)); + leftControllerAimSpace = XR_NULL_HANDLE; + } + if (rightControllerAimSpace != XR_NULL_HANDLE) { + OXR(xrDestroySpace(rightControllerAimSpace)); + rightControllerAimSpace = XR_NULL_HANDLE; + } + // Destroys its actions too. + OXR(xrDestroyActionSet(runningActionSet)); + runningActionSet = XR_NULL_HANDLE; + inputInitialized = 0; +} + void IN_VRInputFrame( engine_t* engine ) { // sync action data diff --git a/Common/VR/VRInput.h b/Common/VR/VRInput.h index 126cc56eaa..b882a627e9 100644 --- a/Common/VR/VRInput.h +++ b/Common/VR/VRInput.h @@ -29,6 +29,7 @@ typedef enum ovrButton_ { } ovrButton; void IN_VRInit( engine_t *engine ); +void IN_VRShutdown( engine_t *engine ); void IN_VRInputFrame( engine_t* engine ); uint32_t IN_VRGetButtonState( int controllerIndex ); XrVector2f IN_VRGetJoystickState( int controllerIndex ); diff --git a/Common/VR/VRRenderer.cpp b/Common/VR/VRRenderer.cpp index 4d16c89917..5fcab45d2f 100644 --- a/Common/VR/VRRenderer.cpp +++ b/Common/VR/VRRenderer.cpp @@ -283,9 +283,17 @@ void VR_InitRenderer( engine_t* engine ) { } void VR_DestroyRenderer( engine_t* engine ) { + if (!initialized) { + return; + } if (VR_GetPlatformFlag(VRPlatformFlag::VR_PLATFORM_EXTENSION_PASSTHROUGH)) { if (passthroughRunning) { OXR(xrPassthroughLayerPauseFB(passthroughLayer)); + passthroughRunning = false; + } + if (passthroughLayer != XR_NULL_HANDLE) { + OXR(xrDestroyPassthroughLayerFB(passthroughLayer)); + passthroughLayer = XR_NULL_HANDLE; } OXR(xrPassthroughPauseFB(passthrough)); OXR(xrDestroyPassthroughFB(passthrough)); diff --git a/Core/EmuThread.cpp b/Core/EmuThread.cpp index 91f71ce30c..e2290f6927 100644 --- a/Core/EmuThread.cpp +++ b/Core/EmuThread.cpp @@ -165,9 +165,7 @@ void RunGraphicsLoop(GraphicsContext *graphicsContext, Application *application, std::thread emuThread = EmuThread_Start(graphicsContext, application, frame); if (IsVREnabled()) { - static bool vrFirstStart = true; - EnterVR(vrFirstStart); - vrFirstStart = false; + EnterVR(); } // Normally ThreadFrame() returning false is what gets us out of here - the frame callback diff --git a/android/jni/app-android.cpp b/android/jni/app-android.cpp index 637002fc4e..2b0f450f7f 100644 --- a/android/jni/app-android.cpp +++ b/android/jni/app-android.cpp @@ -945,6 +945,11 @@ extern "C" void Java_org_ppsspp_ppsspp_NativeApp_shutdown(JNIEnv *, jclass) { INFO_LOG(Log::G3D, "Not shutting down renderer - not initialized"); } + // The instance belongs to this activity. A restart (recreate) creates a new one in this process. + if (IsVREnabled()) { + ShutdownVR(); + } + NativeShutdown(); g_VFS.Clear(); } diff --git a/android/src/org/ppsspp/ppsspp/PpssppActivity.java b/android/src/org/ppsspp/ppsspp/PpssppActivity.java index 006866e06c..25e3e0f5b6 100644 --- a/android/src/org/ppsspp/ppsspp/PpssppActivity.java +++ b/android/src/org/ppsspp/ppsspp/PpssppActivity.java @@ -148,6 +148,8 @@ public class PpssppActivity extends AppCompatActivity implements SensorEventList // This is to avoid losing the game/menu state etc when we are just // switched-away from or rotated etc. private boolean shuttingDown; + // Shutting down for a graphics restart (recreate), which comes back up in this process. + private boolean restarting; // There's one render loop thread. Whether it runs, and on which activity's surface (activityNumber). // surfaceChanged also fires for rotations and resizes, where the thread just keeps going. @@ -1133,8 +1135,9 @@ public class PpssppActivity extends AppCompatActivity implements SensorEventList Log.i(TAG, "in onDestroy, but not shutting down."); } } - // Really ugly workaround for VR issues when PPSSPP restarts - if (isVRDevice()) { + // Really ugly workaround for VR issues when PPSSPP restarts. A graphics restart is fine, since the + // native shutdown takes OpenXR down too and the new activity sets it up again. + if (isVRDevice() && !restarting) { System.exit(0); } Log.i(TAG, "onDestroy end"); @@ -1942,6 +1945,7 @@ public class PpssppActivity extends AppCompatActivity implements SensorEventList overrideShortcutParam = params; } shuttingDown = true; + restarting = true; recreate(); return true; } else if (command.equals("ask_permission") && params.equals("storage")) { diff --git a/docs/vr-openxr-vulkan.md b/docs/vr-openxr-vulkan.md index 1bc953c2b2..3819de58fa 100644 --- a/docs/vr-openxr-vulkan.md +++ b/docs/vr-openxr-vulkan.md @@ -105,6 +105,10 @@ so they don't depend on the backend. (`GL_EXT_sRGB_write_control`). - **The flat screen** stays upright. It used to take the head's pitch at recenter time, often looking down while putting the headset on. Recentering is the system gesture (hold the Meta button). +- **Restarting** (changing the backend): the native shutdown takes OpenXR down (renderer, input, + session, instance, `ShutdownVR`), and the recreated activity sets it up again in the same process. + It used to `System.exit` in `onDestroy` instead, which killed the restart. Controller input is + created per session, since its spaces and attached action set belong to one. ## Phase 2: PC VR @@ -117,5 +121,5 @@ becomes the non-pacing one. The OpenXR loader on desktop is our dynamic one (`Op ## Status - [x] Phase 1: runs on a Quest 2 (menus, Burnout Legends, GTA LCS in flat mode, stereo and 6DoF on) - at 72 fps. Skyplane not checked specifically yet. + at 72 fps, backend switching both ways. Skyplane not checked specifically yet. - [ ] Phase 2