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 <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-11-04 12:49:24 +01:00
1 parent 249b653962
commit 5a1d143ac2
10 files changed
+68 -10

No files matched your search

+15 -3
View File
@@ -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);
+4 -1
View File
@@ -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));
+3
View File
@@ -1,4 +1,5 @@
#include "VRBase.h"
#include "VRInput.h"
#include <assert.h>
#include <stdio.h>
@@ -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) {
+20
View File
@@ -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
+1
View File
@@ -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 );
+8
View File
@@ -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));
+1 -3
View File
@@ -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
+5
View File
@@ -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();
}
@@ -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")) {
+5 -1
View File
@@ -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