diff --git a/DrvOpenXR/DrvOpenXR.cpp b/DrvOpenXR/DrvOpenXR.cpp index 35cc3cae..2312dbff 100644 --- a/DrvOpenXR/DrvOpenXR.cpp +++ b/DrvOpenXR/DrvOpenXR.cpp @@ -651,6 +651,11 @@ XrResult DrvOpenXR::SetupSession() return XR_SUCCESS; } +XrResult DrvOpenXR::FindSystem() +{ + return CreateSystemID(); +} + void DrvOpenXR::KeepOverlaysInstance() { if (currentBackend) diff --git a/DrvOpenXR/XrBackend.cpp b/DrvOpenXR/XrBackend.cpp index 815a7fda..998940f9 100644 --- a/DrvOpenXR/XrBackend.cpp +++ b/DrvOpenXR/XrBackend.cpp @@ -81,6 +81,9 @@ using namespace vr; std::mutex inputRestartMutex; +// This thread is a helper program's loop making its lost session again, between its passes: its loop and thread are +// kept through the rebuild (not stopped, nor started again) +static thread_local bool recoveringInHelperLoop = false; std::unique_ptr XrBackend::temporaryGraphics = nullptr; // Runs a step that reads the store's documents. They are files that another program, or another build, wrote: a value @@ -383,6 +386,9 @@ bool XrBackend::CheckOrInitCompositors(const vr::Texture_t* tex) if (!usingApplicationGraphicsAPI) { if (appGraphicsUnusable) return false; // tried once already: the session stays on the temporary device + // Not while the session is lost: the first Submit after it's made again moves it to the game's device + if (sessionLost && !quitting) + return false; // What can be checked before the session goes down is checked here, so a texture that can't be used // leaves the running session alone @@ -534,8 +540,8 @@ bool XrBackend::CheckOrInitCompositors(const vr::Texture_t* tex) graphicsBinding = nullptr; usingApplicationGraphicsAPI = false; appGraphicsUnusable = true; - if (!temporaryGraphics || XR_FAILED(DrvOpenXR::SetupSession())) { - OnSessionLost(); // not even the temporary device gets a session: OnSessionLost tells the game to quit + if (!temporaryGraphics || XR_FAILED(RebuildSession(RebuildReason::AppGraphics))) { + OnSessionLost(); // not even the temporary device gets a session: lost until one can be made } else { OOVR_LOG("The game's frames can't be shown: telling the game to quit"); EnqueueQuit(); @@ -553,7 +559,7 @@ bool XrBackend::CheckOrInitCompositors(const vr::Texture_t* tex) graphicsBinding = std::make_unique>(d3dInfo); // Kept with the binding, for each later session too gfxInterop = std::move(interop); - if (XR_FAILED(DrvOpenXR::SetupSession())) + if (XR_FAILED(RebuildSession(RebuildReason::AppGraphics))) return giveUp("the OpenXR runtime refused a D3D11 session for the game's copied frames"); } #endif @@ -588,7 +594,7 @@ bool XrBackend::CheckOrInitCompositors(const vr::Texture_t* tex) d3dInfo.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR; d3dInfo.device = dev; graphicsBinding = std::make_unique>(d3dInfo); - XrResult made = DrvOpenXR::SetupSession(); + XrResult made = RebuildSession(RebuildReason::AppGraphics); dev->Release(); if (XR_FAILED(made)) @@ -614,7 +620,7 @@ bool XrBackend::CheckOrInitCompositors(const vr::Texture_t* tex) d3dInfo.device = device.Get(); d3dInfo.queue = d3dTexData->m_pCommandQueue; graphicsBinding = std::make_unique>(d3dInfo); - if (XR_FAILED(DrvOpenXR::SetupSession())) + if (XR_FAILED(RebuildSession(RebuildReason::AppGraphics))) return giveUp("the OpenXR runtime refused a session on the game's D3D12 device"); // The ComPtr releases the device: no explicit Release here @@ -672,7 +678,7 @@ bool XrBackend::CheckOrInitCompositors(const vr::Texture_t* tex) return giveUp("the app's VkQueue isn't one of its device's queues"); graphicsBinding = std::make_unique>(binding); - if (XR_FAILED(DrvOpenXR::SetupSession())) + if (XR_FAILED(RebuildSession(RebuildReason::AppGraphics))) return giveUp("the OpenXR runtime refused a session on the game's Vulkan device"); break; } @@ -691,7 +697,7 @@ bool XrBackend::CheckOrInitCompositors(const vr::Texture_t* tex) binding.hDC = wglGetCurrentDC(); graphicsBinding = std::make_unique>(binding); - if (XR_FAILED(DrvOpenXR::SetupSession())) + if (XR_FAILED(RebuildSession(RebuildReason::AppGraphics))) return giveUp("the OpenXR runtime refused a session on the game's OpenGL context"); #else // Only support xlib for now (same as Monado) @@ -752,7 +758,7 @@ bool XrBackend::CheckOrInitCompositors(const vr::Texture_t* tex) return giveUp("glXCreateContextAttribsARB failed"); graphicsBinding = std::make_unique>(binding); - if (XR_FAILED(DrvOpenXR::SetupSession())) + if (XR_FAILED(RebuildSession(RebuildReason::AppGraphics))) return giveUp("the OpenXR runtime refused a session on the game's OpenGL context"); #endif // End of platform-specific code @@ -767,7 +773,7 @@ bool XrBackend::CheckOrInitCompositors(const vr::Texture_t* tex) binding.next = nullptr; graphicsBinding = std::make_unique>(binding); - if (XR_FAILED(DrvOpenXR::SetupSession())) + if (XR_FAILED(RebuildSession(RebuildReason::AppGraphics))) return giveUp("the OpenXR runtime refused a session on the game's OpenGL ES context"); #endif break; @@ -800,6 +806,10 @@ void XrBackend::WaitForTrackingData() // A game that waits for frames before its first submit still sees the session become ready PumpEvents(); + // A lost session is made again here, between the game's frames, once the system is back + if (sessionLost && !quitting) + RecoverLostSession(); + // Make sure the OpenXR session is active before doing anything else, and if not then skip if (!sessionActive) { renderingFrame = false; @@ -1066,6 +1076,7 @@ IBackend::openvr_enum_t XrBackend::StoreEyeTexture( void XrBackend::UseSubmittedPose(vr::EVREye eye, const vr::Texture_t* texture, vr::EVRSubmitFlags submitFlags) { + auto lock = xr_session.lock_shared(); // held while reading: a session restart waits for it if (!oovr_global_configuration.UseSubmittedPose() || !(submitFlags & vr::Submit_TextureWithPose) || !xr_gbl) return; // The head pose the game rendered with, in its tracking space: the projection layer's space @@ -1512,6 +1523,11 @@ IBackend::openvr_enum_t XrBackend::SetSkyboxOverride(const vr::Texture_t* pTextu if (!CheckOrInitCompositors(pTextures)) return 1; // VRCompositorError_RequestFailed + { + // Shown again on a new session, until ClearSkyboxOverride + std::lock_guard pending(pendingSkyboxLock); + shownSkybox.Set(pTextures, unTextureCount); + } // Taken after CheckOrInitCompositors, which may restart the session, and held to the end: a session restart on // another thread replaces xr_gbl @@ -1668,6 +1684,7 @@ void XrBackend::ClearSkyboxOverride() { std::lock_guard pending(pendingSkyboxLock); pendingSkybox.Clear(); + shownSkybox.Clear(); } std::lock_guard lock(skyboxLock); skybox.layerCount = 0; @@ -1906,6 +1923,8 @@ bool XrBackend::GetPlayAreaPoints(vr::HmdVector3_t* points, int* count) { if (count) *count = 0; + // Held while asking: a session restart waits for it + auto lock = xr_session.lock_shared(); if (!xr_session.get()) return false; // the session is lost @@ -1979,12 +1998,117 @@ static void PostRoleChange() OcPostEvent(VREvent_TrackedDeviceRoleChanged, k_unTrackedDeviceIndexInvalid, VREvent_Data_t{}, false); } -// The runtime ends the game's session by its own choice: the driver asks to quit, and the game is told to +// The user quit from the runtime: the driver asks to quit, and the game is told to, once void XrBackend::OnRuntimeQuit() { - if (!sessionLost) - OcPostEvent(VREvent_DriverRequestedQuit, k_unTrackedDeviceIndexInvalid, VREvent_Data_t{}, false); - OnSessionLost(); + sessionActive = false; + renderingFrame = false; + sessionLost = true; + if (quitting.exchange(true)) + return; + OOVR_LOG("The OpenXR runtime ended the session: telling the game to quit"); + OcPostEvent(VREvent_DriverRequestedQuit, k_unTrackedDeviceIndexInvalid, VREvent_Data_t{}, false); + EnqueueQuit(); +} + +XrBackend::StateOutcome XrBackend::OutcomeOfState(XrSessionState state, bool sawStopping, bool exitExpected) +{ + if (exitExpected) + return StateOutcome::Ignored; + if (state == XR_SESSION_STATE_LOSS_PENDING) + return StateOutcome::Lost; + if (state == XR_SESSION_STATE_EXITING) + return sawStopping ? StateOutcome::Quit : StateOutcome::Lost; + return StateOutcome::Ignored; +} + +bool XrBackend::RecoveryDue(std::chrono::steady_clock::time_point lastTry, std::chrono::steady_clock::time_point now) +{ + return lastTry == std::chrono::steady_clock::time_point{} || now - lastTry >= kRecoveryInterval; +} + +const char* XrBackend::RebuildReasonName(RebuildReason reason) +{ + switch (reason) { + case RebuildReason::AppGraphics: + return "the game's graphics device"; + case RebuildReason::Inputs: + return "the game's inputs"; + case RebuildReason::Loss: + return "a lost session"; + } + return "?"; +} + +// The running backend, if it's OpenXR's (the unit tests run others) +static XrBackend* CurrentBackend() +{ + BackendManager* manager = BackendManager::InstancePtr(); + return manager ? dynamic_cast(manager->GetBackendInstance()) : nullptr; +} + +bool XrBackend::CurrentSessionGone() +{ + XrBackend* backend = CurrentBackend(); + return !xr_gbl || (backend && backend->IsSessionLost()); +} + +bool XrBackend::CurrentSessionLost() +{ + XrBackend* backend = CurrentBackend(); + return backend && backend->IsSessionLost() && !backend->IsQuitting(); +} + +bool XrBackend::InputsMayAttach() +{ + if (IsHelperSession() || BaseClientCore::appType != vr::VRApplication_Scene) + return true; + XrBackend* backend = CurrentBackend(); + if (!backend || backend->usingApplicationGraphicsAPI || backend->appGraphicsUnusable) + return true; + // No Submit yet: a game that never submits (or takes a long time to) gets its inputs after a while + using clock = std::chrono::steady_clock; + const clock::rep now = clock::now().time_since_epoch().count(); + clock::rep expected = 0; + if (backend->inputsFirstAsked.compare_exchange_strong(expected, now)) + return false; + return clock::duration(now - expected) >= kInputsWithoutSubmit; +} + +// The devices the game knows of now: the headset, the hands that have a controller, and the trackers +static std::vector PresentDevices(IBackend& backend) +{ + std::vector present; + for (TrackedDeviceIndex_t i = 0; i < k_unMaxTrackedDeviceCount; i++) { + if (backend.GetDevice(i)) + present.push_back(i); + } + return present; +} + +XrResult XrBackend::RebuildSession(RebuildReason reason) +{ + auto lock = xr_session.lock(); + OOVR_LOGF("Making the OpenXR session again, for %s", RebuildReasonName(reason)); + + // Never in the middle of a frame: one the game has open on a running session is ended as if it had submitted + if (renderingFrame && sessionActive && !sessionLost && xr_gbl) + SubmitFrames(false); + + XrResult res = DrvOpenXR::SetupSession(); + if (XR_FAILED(res)) + return res; + + // The skybox the game set is shown again from its texture, at the new session's first frame + { + std::lock_guard pending(pendingSkyboxLock); + if (!shownSkybox.Empty()) + pendingSkybox = shownSkybox; + } + // The overlays' swapchains went with the old session: made again from their textures + if (BaseOverlay* overlay = GetUnsafeBaseOverlay()) + overlay->_OnSessionRebuilt(); + return XR_SUCCESS; } void XrBackend::PumpEvents() @@ -2003,7 +2127,7 @@ void XrBackend::PumpEvents() if (XR_FAILED(res)) { OOVR_LOG_ONCE("xrPollEvent failed: no events this frame"); if (res == XR_ERROR_INSTANCE_LOST) - OnRuntimeQuit(); + OnSessionLost(); break; } @@ -2028,11 +2152,11 @@ void XrBackend::PumpEvents() continue; } - // The instance is going away (the runtime updating or shutting down): the game is told to quit while it can + // The instance is going away (the runtime updating or shutting down): its session with it if (ev.type == XR_TYPE_EVENT_DATA_INSTANCE_LOSS_PENDING) { OOVR_LOG("The OpenXR instance will be lost"); if (!exitExpected) - OnRuntimeQuit(); + OnSessionLost(); continue; } @@ -2133,6 +2257,7 @@ void XrBackend::PumpEvents() break; } sessionActive = true; + sawStopping = false; frameAwaitingBegin = false; frameBegun = false; frameCallFailures = 0; @@ -2147,22 +2272,27 @@ void XrBackend::PumpEvents() if (sessionActive && XR_FAILED(xrEndSession(xr_session.get()))) // could be inactive if we missed XR_SESSION_STATE_READY OOVR_LOG("xrEndSession failed"); sessionActive = false; + sawStopping = true; renderingFrame = false; frameBegun = false; frameAwaitingBegin = false; break; } - case XR_SESSION_STATE_EXITING: { - OOVR_LOGF("Exiting"); - // The runtime ending the session (the user quitting from its menu, say) is the game's cue to quit; - // OpenComposite ending it itself isn't - if (!exitExpected) - OnRuntimeQuit(); - break; - } + case XR_SESSION_STATE_EXITING: case XR_SESSION_STATE_LOSS_PENDING: { - // If the headset is unplugged or the user decides to exit the app - OnRuntimeQuit(); + // The user quitting from the runtime's menu (EXITING after STOPPING) is the game's cue to quit; a session + // lost (the headset unplugged, the runtime restarting) is made again once the system is back; OpenLamina + // ending the session itself is neither + switch (OutcomeOfState(sessionState, sawStopping, exitExpected)) { + case StateOutcome::Quit: + OnRuntimeQuit(); + break; + case StateOutcome::Lost: + OnSessionLost(); + break; + case StateOutcome::Ignored: + break; + } break; } default: @@ -2223,8 +2353,13 @@ void XrBackend::PumpEvents() */ // Not while a session is being ended (ShutdownSession pumps events while it waits, maybe under the input restart) BaseInput* input = GetUnsafeBaseInput(); - if (input && !exitExpected && !input->AreActionsLoaded() && sessionState == XR_SESSION_STATE_FOCUSED && !Hand(XrController::XCT_LEFT) && !Hand(XrController::XCT_RIGHT)) { - QueryForInteractionProfile(); + // Not before the game's device is known (InputsMayAttach): another session restart for it would follow + if (input && !exitExpected && !input->AreActionsLoaded() && !input->LegacyInputAsked() && sessionState == XR_SESSION_STATE_FOCUSED + && !Hand(XrController::XCT_LEFT) && !Hand(XrController::XCT_RIGHT) && InputsMayAttach()) { + // Under the session lock (shared, if it's free): not while another thread makes and binds the game's actions + SessionLock sessionLock(xr_session, SessionLock::TryShared{}); + if (sessionLock.IsHeld() && !input->AreActionsLoaded()) + QueryForInteractionProfile(); } } @@ -2254,11 +2389,59 @@ void XrBackend::OnSessionLost() { sessionActive = false; renderingFrame = false; - if (sessionLost) + if (sessionLost.exchange(true)) return; - sessionLost = true; - OOVR_LOG("The OpenXR session is lost: telling the game to quit"); - EnqueueQuit(); + if (quitting) + return; + // An EXITING after the loss is the lost session's, not the user quitting + sawStopping = false; + OOVR_LOG("The OpenXR session is lost: making it again once the system is back"); + // The headset and controllers go away until then, as SteamVR's do when their driver goes + if (!devicesDeactivated.exchange(true)) { + for (TrackedDeviceIndex_t i : PresentDevices(*this)) + OcPostEvent(VREvent_TrackedDeviceDeactivated, i, VREvent_Data_t{}, false); + } +} + +void XrBackend::RecoverLostSession(std::mutex* passLock) +{ + if (!sessionLost || quitting || ownFramesStopped) + return; + // One thread tries at a time; the others carry on without a session + std::unique_lock guard(recoveryLock, std::try_to_lock); + if (!guard.owns_lock()) + return; + using clock = std::chrono::steady_clock; + const auto now = clock::now(); + if (!RecoveryDue(clock::time_point(clock::duration(lastRecoveryTry.load())), now)) + return; + + // The session lock, then the helper loop's pass (the order its passes take them in). The helper loop never waits + // for the session lock: a thread holding it may be stopping the loop. Not a conditional expression: MSVC wants a + // copy constructor for one, which SessionLock lacks. + auto lock = [passLock] { + if (passLock) + return xr_session.try_lock(); + return xr_session.lock(); + }(); + if (!lock.IsHeld()) + return; // the next pass tries again + lastRecoveryTry = now.time_since_epoch().count(); + std::unique_lock pass; + if (passLock) + pass = std::unique_lock(*passLock); + if (!sessionLost || quitting) + return; + // The lost session first, once: its handles are no use any more + if (!lostSessionDestroyed) { + if (xr_gbl || xr_session.get()) + DrvOpenXR::ShutdownSession(); + lostSessionDestroyed = true; + } + if (XR_FAILED(DrvOpenXR::FindSystem())) + return; // no headset yet: the next try in a second + if (XR_SUCCEEDED(RebuildSession(RebuildReason::Loss)) && !sessionLost) + OOVR_LOG("The OpenXR session is back"); } // Own frames call xrEndFrame on their own thread, where the runtime may use the session's device while the game's @@ -2305,6 +2488,14 @@ void XrBackend::OnSessionCreated() { sessionLost = false; exitExpected = false; + sawStopping = false; + lostSessionDestroyed = false; + lastRecoveryTry = 0; + // The devices are back, whichever path made the session after a loss + if (devicesDeactivated.exchange(false)) { + for (TrackedDeviceIndex_t i : PresentDevices(*this)) + OcPostEvent(VREvent_TrackedDeviceActivated, i, VREvent_Data_t{}, false); + } toldIpd = NAN; // the new session's first IPD is the one the game has // The gap from the old session's last frame isn't dropped frames RestartFrameTiming(); @@ -2335,7 +2526,8 @@ void XrBackend::OnSessionCreated() // A helper program's session has its own loop, whatever ownFrames says: an overlay session's frames are all // OpenLamina's, and a headless one's events come from nowhere else if (IsHelperSession()) { - StartHelperLoop(); + if (!recoveringInHelperLoop) + StartHelperLoop(); return; } @@ -2397,6 +2589,18 @@ void XrBackend::StartHelperLoop() }, std::chrono::milliseconds(0), [this, overlaySession](const OwnFrameLoop::LayerSource& source, const std::function& wanted) -> XrDuration { + // A lost session is made again here, between passes, about once a second until the runtime has one: no other + // thread of a helper program runs frames to do it. Its loop and thread are kept through the rebuild. + if (sessionLost) { + if (!quitting) { + recoveringInHelperLoop = true; + ScopeGuard done([] { recoveringInHelperLoop = false; }); + RecoverLostSession(&helperPassLock); + } + std::lock_guard pass(helperPassLock); + PumpEvents(); + return 0; + } // The session lock (shared) through the frame, if it's free: a session restart holds it while it stops this // loop. RunOwnFrame's own try then finds it held. SessionLock lock(xr_session, SessionLock::TryShared{}); @@ -2555,6 +2759,10 @@ void XrBackend::StopOwnFrames(bool forGood) { if (forGood) ownFramesStopped = true; + // The helper loop rebuilding its own session from its thread: it goes on (under the session lock, which a stop for + // good waits for) + if (recoveringInHelperLoop && !forGood) + return; // Joins its thread, which doesn't wait for the session lock that a session restart here holds ownFrames.reset(); // Goes with the interfaces, before them @@ -3100,12 +3308,13 @@ void XrBackend::MaybeRestartForInputs() if (infoSet == XR_NULL_HANDLE && (!input || !input->AreActionsLoaded())) return; - OOVR_LOG("Restarting session for inputs..."); - if (XR_FAILED(DrvOpenXR::SetupSession())) { - // No session to put the actions on: the game is told to quit - auto* backend = (XrBackend*)BackendManager::Instance().GetBackendInstance(); - if (backend) - backend->OnSessionLost(); + auto* backend = (XrBackend*)BackendManager::Instance().GetBackendInstance(); + // A lost session isn't restarted: the one made again has the game's actions bound + if (!backend || backend->IsSessionLost()) + return; + if (XR_FAILED(backend->RebuildSession(RebuildReason::Inputs))) { + // No session to put the actions on: lost until one can be made + backend->OnSessionLost(); return; } OOVR_LOG("Session restart successful!"); diff --git a/DrvOpenXR/XrBackend.h b/DrvOpenXR/XrBackend.h index 88e4897b..fe405bd8 100644 --- a/DrvOpenXR/XrBackend.h +++ b/DrvOpenXR/XrBackend.h @@ -66,6 +66,16 @@ class XrBackend : public IBackend { std::atomic sessionActive{ false }; std::atomic sessionLost{ false }; std::atomic exitExpected{ false }; + // The user quit from the runtime: no session is made again + std::atomic quitting{ false }; + // The session went through STOPPING since it last ran (an EXITING after it is the user quitting) + std::atomic sawStopping{ false }; + // The game has been told the devices are deactivated (a loss), and not yet that they're back + std::atomic devicesDeactivated{ false }; + // The last try to find the system again (RecoverLostSession), and whether the lost session is destroyed yet + std::atomic lastRecoveryTry{ 0 }; // 0 before the first + bool lostSessionDestroyed = false; + std::mutex recoveryLock; // What the game has been told of the session, as SteamVR tells it: whether it lost input focus (sessionEvents), and // the IPD (NaN until the session's first) std::atomic inputFocusLost{ false }; @@ -89,15 +99,76 @@ class XrBackend : public IBackend { // DrvOpenXR::KeepOverlaysInstance void KeepOverlaysInstance(); + /** Why the session is made again (RebuildSession) */ + enum class RebuildReason { + AppGraphics, // the game's first Submit: from the temporary device to the game's own + Inputs, // the game's actions, which can only be attached to a session that has none yet + Loss, // the runtime lost the session (or one couldn't be made): a new one once the system is back + }; + static const char* RebuildReasonName(RebuildReason reason); + + /** + * Makes the session again, under the session lock: never in the middle of a frame (a frame the game has open is + * ended first, as if it had submitted). What the game set on the old session is put back on the new one: its eye + * compositors are made again at its next Submit, the skybox is shown again from the game's texture, and the overlays' + * swapchains are made again from their textures. XR_SUCCESS, or what xrCreateSession answered. + */ + XrResult RebuildSession(RebuildReason reason); + /** - * The session is gone or going (LOSS_PENDING, an EXITING OpenComposite didn't ask for, or a session restart - * that failed): VREvent_Quit is queued once, and no more frames are begun. There's no reconnecting. + * The session is lost (LOSS_PENDING, XR_ERROR_SESSION_LOST, an EXITING the user didn't ask for, or a session that + * couldn't be made): no more frames; the headset and controllers are deactivated, their poses invalid, until + * RecoverLostSession makes a new session. The game isn't told to quit. */ void OnSessionLost(); /** Pumps events until done() is true, for at most timeoutMs: whether it was */ bool WaitForEvents(const std::function& done, int timeoutMs); - /** The runtime ends the session on its own: VREvent_DriverRequestedQuit, then OnSessionLost */ + /** + * The user quit from the runtime (EXITING after STOPPING): VREvent_DriverRequestedQuit and VREvent_Quit, once, and + * no session is made again + */ void OnRuntimeQuit(); + /** + * Whether the game's actions (its manifest's, or legacy input's) and the info set may be attached now: once the + * session is on the game's device (or will stay where it is: a helper program's, or a game whose device can't be + * used). Before that the session restart for its first Submit would follow, and one more for a manifest it loads by + * then. A game that hasn't submitted within kInputsWithoutSubmit of first being asked about gets them all the same. + */ + static bool InputsMayAttach(); + static constexpr std::chrono::seconds kInputsWithoutSubmit{ 2 }; + + /** Whether the current backend's session is gone (lost, or not made again yet): the devices have no poses */ + static bool CurrentSessionGone(); + /** Whether the current backend's session is lost and is to be made again (not the user quitting) */ + static bool CurrentSessionLost(); + /** Whether the user quit from the runtime (OnRuntimeQuit): the session isn't made again */ + bool IsQuitting() const { return quitting; } + + /** What an unasked session state change means for the game */ + enum class StateOutcome { + Ignored, + Lost, + Quit, + }; + /** + * For a state the session went to: LOSS_PENDING is a loss; EXITING is the user quitting if the session went + * through STOPPING since it last ran (sawStopping), a loss otherwise; neither while OpenLamina ends the session + * itself (exitExpected) + */ + static StateOutcome OutcomeOfState(XrSessionState state, bool sawStopping, bool exitExpected); + + /** The wait between tries to find the system again after a loss */ + static constexpr std::chrono::milliseconds kRecoveryInterval{ 1000 }; + /** Whether a try is due at now: the first at once, then one each kRecoveryInterval (lastTry is {} before the first) */ + static bool RecoveryDue(std::chrono::steady_clock::time_point lastTry, std::chrono::steady_clock::time_point now); + + /** + * After a loss, on a thread that holds no session lock and no frame: destroys the lost session (once), then about + * once a second asks for the system (xrGetSystem) and, once it's there, makes the session again (RebuildSession); + * the devices are activated again. Retried for as long as it takes; nothing after the user quit. passLock is the + * helper loop's pass lock, taken after the session lock. + */ + void RecoverLostSession(std::mutex* passLock = nullptr); bool IsSessionLost() const { return sessionLost; } /** The session is lost, not running, or stopping, exiting or about to be lost: nothing can be shown in it any more */ bool IsSessionEnded() const @@ -269,6 +340,8 @@ class XrBackend : public IBackend { // A skybox asked for while the session wasn't running, shown at the next frame's start; under pendingSkyboxLock std::mutex pendingSkyboxLock; SkyboxRequest pendingSkybox; + // The skybox shown until ClearSkyboxOverride, shown again on a new session (RebuildSession); under pendingSkyboxLock + SkyboxRequest shownSkybox; bool EquirectSkyboxAvailable() const; // The session lock held (shared) and the session running IBackend::openvr_enum_t ShowSkybox(const vr::Texture_t* textures, uint32_t count); @@ -306,6 +379,8 @@ class XrBackend : public IBackend { // Whether we've restarted the session to use the application's rendering API yet std::atomic usingApplicationGraphicsAPI{ false }; + // When InputsMayAttach was first asked before a Submit (steady_clock ticks; 0 before) + std::atomic inputsFirstAsked{ 0 }; // An overlay application's textures, copied onto its session's device std::unique_ptr overlayCopy; diff --git a/DrvOpenXR/XrController.cpp b/DrvOpenXR/XrController.cpp index 87daa0cb..74847523 100644 --- a/DrvOpenXR/XrController.cpp +++ b/DrvOpenXR/XrController.cpp @@ -1,4 +1,5 @@ #include "XrController.h" +#include "XrBackend.h" #include "../OpenOVR/Misc/LibMonado.h" @@ -146,6 +147,13 @@ void XrController::GetPose(vr::ETrackingUniverseOrigin origin, vr::TrackedDevice pose->bPoseIsValid = false; pose->eTrackingResult = vr::TrackingResult_Running_OutOfRange; + // No session (it was lost): deactivated until it's made again + if (XrBackend::CurrentSessionGone()) { + pose->bDeviceIsConnected = false; + pose->eTrackingResult = vr::TrackingResult_Uninitialized; + return; + } + BaseInput* input = GetUnsafeBaseInput(); if (input == nullptr) return; diff --git a/DrvOpenXR/XrDriverPrivate.h b/DrvOpenXR/XrDriverPrivate.h index 0a1e6b0c..139fdb6f 100644 --- a/DrvOpenXR/XrDriverPrivate.h +++ b/DrvOpenXR/XrDriverPrivate.h @@ -18,5 +18,7 @@ namespace DrvOpenXR { // XR_SUCCESS, or what xrCreateSession answered (no session then: the caller decides what next) XrResult SetupSession(); void ShutdownSession(); +// xrGetSystem for the headset again (after a loss): XR_SUCCESS once it is there +XrResult FindSystem(); void FullShutdown(); } // namespace DrvOpenXR diff --git a/DrvOpenXR/XrHMD.cpp b/DrvOpenXR/XrHMD.cpp index 529dedcc..2c3fa533 100644 --- a/DrvOpenXR/XrHMD.cpp +++ b/DrvOpenXR/XrHMD.cpp @@ -391,7 +391,7 @@ void XrHMD::GetPose(vr::ETrackingUniverseOrigin origin, vr::TrackedDevicePose_t* { // Held to the end: a session restart on another thread replaces xr_gbl and its spaces, and waits for this to end auto lock = xr_session.lock_shared(); - if (!xr_gbl) { + if (XrBackend::CurrentSessionGone()) { // No session (it was lost, or a restart failed): no pose if (pose) { *pose = {}; @@ -427,7 +427,11 @@ static const float kDefaultIpd = 0.064f; float XrHMD::GetIPD() { - if (!xr_gbl) + const bool haveSession = [] { + auto lock = xr_session.lock_shared(); // held while reading: a session restart waits for it + return xr_gbl != nullptr; + }(); + if (!haveSession) return kDefaultIpd + IpdOffset(); static float ipd = kDefaultIpd; diff --git a/OpenOVR/Misc/xrutil.cpp b/OpenOVR/Misc/xrutil.cpp index a479da45..86121c4a 100644 --- a/OpenOVR/Misc/xrutil.cpp +++ b/OpenOVR/Misc/xrutil.cpp @@ -457,6 +457,11 @@ SessionLock SessionWrapper::try_lock_shared() return SessionLock(*this, SessionLock::TryShared{}); } +SessionLock SessionWrapper::try_lock() +{ + return SessionLock(*this, SessionLock::TryExclusive{}); +} + void SessionWrapper::reset() { session = XR_NULL_HANDLE; @@ -513,6 +518,18 @@ SessionLock::SessionLock(SessionWrapper& p, TryShared) owner = true; } +SessionLock::SessionLock(SessionWrapper& p, TryExclusive) + : parent(p) +{ + if (thread_lock != Held::Unlocked) + return; // this thread holds it already + if (!p.mutex.try_lock()) + return; + lock = std::unique_lock(p.mutex, std::adopt_lock); + thread_lock = Held::Exclusive; + owner = true; +} + SessionLock::~SessionLock() { if (upgraded) { diff --git a/OpenOVR/Misc/xrutil.h b/OpenOVR/Misc/xrutil.h index 794a5b55..354d8ea4 100644 --- a/OpenOVR/Misc/xrutil.h +++ b/OpenOVR/Misc/xrutil.h @@ -159,6 +159,11 @@ class SessionWrapper { */ [[nodiscard]] SessionLock try_lock_shared(); + /* + * The exclusive lock if it's free now, without waiting (see SessionLock::IsHeld) + */ + [[nodiscard]] SessionLock try_lock(); + /* * Resets the underlying XrSession to XR_NULL_HANDLE. Does not actually call xrDestroySession. */ @@ -183,10 +188,12 @@ class SessionLock { public: struct TryShared {}; + struct TryExclusive {}; SessionLock(SessionWrapper& p, bool exclusive); SessionLock(SessionWrapper& p, std::chrono::milliseconds limit); SessionLock(SessionWrapper& p, TryShared); + SessionLock(SessionWrapper& p, TryExclusive); ~SessionLock(); operator XrSession&(); // Whether this thread holds the lock, through this SessionLock or an outer one diff --git a/OpenOVR/Reimpl/BaseInput.cpp b/OpenOVR/Reimpl/BaseInput.cpp index ad2ccc66..8462a582 100644 --- a/OpenOVR/Reimpl/BaseInput.cpp +++ b/OpenOVR/Reimpl/BaseInput.cpp @@ -781,6 +781,12 @@ void BaseInput::LoadEmptyManifestIfRequired() { if (hasLoadedActions) return; + // Asked for, legacy input comes in place of the info set (which would cost a session restart) + legacyInputAsked = true; + // Not before the game's first frame on its own device: the session restart for it would come anyway, and a + // manifest the game loads by then would need another + if (!XrBackend::InputsMayAttach()) + return; // As in SetActionManifestPath, exclusive to the end; another thread may have loaded the actions while this waited auto sessionLock = xr_session.lock(); @@ -845,6 +851,9 @@ void BaseInput::BindInputsForSession() // If we didn't request the restart but we also haven't even loaded actions yet, there's nothing to bind yet. if (restartingSession || !hasLoadedActions) return; + // No session to bind to (a lost one was destroyed): the one made again binds them + if (!xr_gbl || !xr_session.get()) + return; // Since the session has changed, any actionspaces we previously created are now invalid for (const std::unique_ptr& action : actions.GetItems()) { diff --git a/OpenOVR/Reimpl/BaseInput.h b/OpenOVR/Reimpl/BaseInput.h index 4dc6fefe..5230e377 100644 --- a/OpenOVR/Reimpl/BaseInput.h +++ b/OpenOVR/Reimpl/BaseInput.h @@ -508,6 +508,9 @@ class BaseInput { void GetHandSpace(ITrackedDevice::TrackedDeviceType hand, XrSpace& space, bool aimPose); bool AreActionsLoaded(); + // The game asked for legacy input: it's loaded (at once, or at its next call once it may be attached), with no info + // set before it + bool LegacyInputAsked() const { return legacyInputAsked; } /** One hand's laser for pointing at overlays (LaserMouse) */ struct HandPointer { @@ -871,6 +874,7 @@ class BaseInput { // Set under the exclusive session lock; read without it on the way in to LoadEmptyManifestIfRequired std::atomic hasLoadedActions = false; + std::atomic legacyInputAsked = false; std::string loadedActionsPath; std::mutex loadedActionsPathLock; // read before the session lock, to answer a repeated manifest at once bool usingLegacyInput = false; diff --git a/OpenOVR/Reimpl/BaseOverlay.cpp b/OpenOVR/Reimpl/BaseOverlay.cpp index aa25452e..171658d8 100644 --- a/OpenOVR/Reimpl/BaseOverlay.cpp +++ b/OpenOVR/Reimpl/BaseOverlay.cpp @@ -1869,6 +1869,18 @@ bool BaseOverlay::_AnyVisible() return false; } +void BaseOverlay::_OnSessionRebuilt() +{ + std::lock_guard lock(layersLock); + for (const auto& kv : overlays) { + OverlayData* overlay = kv.second; + // An image from memory finds its own swapchain gone (its upload is made again); a texture's compositor went + // with the session, and the layer mustn't name its swapchain any more + if (overlay && overlay->rawImage.Empty() && !overlay->compositor.lock()) + overlay->compositionLayer.quad.subImage = XrSwapchainSubImage{}; + } +} + int BaseOverlay::_BuildLayers(XrCompositionLayerBaseHeader* sceneLayer, XrCompositionLayerBaseHeader const* const*& layers, bool mayUpload) { // Note that at least on MSVC, this shouldn't be doing any memory allocations diff --git a/OpenOVR/Reimpl/BaseOverlay.h b/OpenOVR/Reimpl/BaseOverlay.h index e2155c94..d49a83e2 100644 --- a/OpenOVR/Reimpl/BaseOverlay.h +++ b/OpenOVR/Reimpl/BaseOverlay.h @@ -286,6 +286,10 @@ class BaseOverlay { // Whether the handle is one of this program's overlays bool _IsOwnOverlay(vr::VROverlayHandle_t handle); + // A new session (XrBackend::RebuildSession): the overlays' texture swapchains went with the old one, and are made + // again from their textures as they're next shown + void _OnSessionRebuilt(); + // Whether the keyboard is shown bool _KeyboardShown() { diff --git a/OpenOVR/Reimpl/BaseSystem.cpp b/OpenOVR/Reimpl/BaseSystem.cpp index 6ea1b66a..bdae52b5 100644 --- a/OpenOVR/Reimpl/BaseSystem.cpp +++ b/OpenOVR/Reimpl/BaseSystem.cpp @@ -479,7 +479,8 @@ ETrackedDeviceClass BaseSystem::GetTrackedDeviceClass(vr::TrackedDeviceIndex_t d bool BaseSystem::IsTrackedDeviceConnected(vr::TrackedDeviceIndex_t deviceIndex) { - return BackendManager::Instance().GetDevice(deviceIndex) != nullptr; + // None while the session is lost: the devices are deactivated until it's made again + return BackendManager::Instance().GetDevice(deviceIndex) != nullptr && !XrBackend::CurrentSessionLost(); } bool BaseSystem::GetBoolTrackedDeviceProperty(vr::TrackedDeviceIndex_t unDeviceIndex, ETrackedDeviceProperty prop, ETrackedPropertyError* pErrorL) @@ -843,7 +844,11 @@ bool BaseSystem::GetControllerState(vr::TrackedDeviceIndex_t controllerDeviceInd memset(controllerState, 0, controllerStateSize); // The headset's proximity sensor, while the runtime says whether the user wears it - if (controllerDeviceIndex == k_unTrackedDeviceIndex_Hmd && xr_gbl && xr_gbl->supportsUserPresence) { + const bool userPresence = [] { + auto lock = xr_session.lock_shared(); // held while reading: a session restart waits for it + return xr_gbl && xr_gbl->supportsUserPresence; + }(); + if (controllerDeviceIndex == k_unTrackedDeviceIndex_Hmd && userPresence) { std::shared_ptr hmd = BackendManager::Instance().GetDevice(k_unTrackedDeviceIndex_Hmd); return hmd && hmd->GetControllerState(controllerState); } @@ -1240,6 +1245,7 @@ static bool GazeInHead(glm::quat* gazeInHead) // session locked (shared). static glm::quat EyeInHead(EVREye eye) { + auto lock = xr_session.lock_shared(); // held while reading: a session restart waits for it if (!xr_gbl) return glm::identity(); XruCachedViews cached = xr_gbl->GetCachedViews(xr_gbl->viewSpace); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 161ac3a4..09c04177 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -134,6 +134,7 @@ add_executable(oc_unit_tests unit/KeyboardTests.cpp unit/RenderModelTests.cpp unit/HmdTests.cpp + unit/SessionLifecycleTests.cpp unit/DeviceSlotsTests.cpp unit/EntryPointsTests.cpp unit/BaseMailboxTests.cpp diff --git a/tests/conformance/expect/xr_failures.json b/tests/conformance/expect/xr_failures.json index ed400e35..9c43e825 100644 --- a/tests/conformance/expect/xr_failures.json +++ b/tests/conformance/expect/xr_failures.json @@ -37,8 +37,62 @@ "frame-loop" ], "frames": 120, - "quit_events": 1, - "quit_from_frame": 60 + "quit_events": 0, + "device_events": [ + 101, + 100 + ], + "sessions_lost": 1, + "sessions_created_min": 3, + "frames_ended_min": 30 + }, + "lost_unavailable": { + "cases": [ + "frame-loop" + ], + "frames": 120, + "quit_events": 0, + "device_events": [ + 101, + 100 + ], + "sessions_lost": 1, + "sessions_created_min": 3 + }, + "lost_before_submit": { + "cases": [ + "frame-loop" + ], + "frames": 120, + "quit_events": 0, + "device_events": [ + 101, + 100 + ], + "sessions_lost": 1, + "sessions_created_min": 3, + "frames_ended_min": 20 + }, + "lost_input": { + "cases": [ + "input-frames" + ], + "action_attaches_min": 3 + }, + "lost_overlay": { + "cases": [ + "overlay-app-loss" + ], + "loss_record": { + "device_events": [ + 101, + 100 + ], + "sessions_lost": 1, + "sessions_created_min": 2, + "frames_ended_min": 20, + "manifest": 0 + } }, "exiting": { "cases": [ diff --git a/tests/conformance/helper_cases.cpp b/tests/conformance/helper_cases.cpp index b8c22657..9c7464f6 100644 --- a/tests/conformance/helper_cases.cpp +++ b/tests/conformance/helper_cases.cpp @@ -729,6 +729,57 @@ void OverlayAppCase(CaseContext& c) c.Record("frames", r); } +// An overlay application whose session (or instance) the runtime loses (xr_failures.py's lost_overlay scripts): its +// overlay from memory is shown for 2.5 s while it polls its events every 11 ms, and loads the manifest (--manifest) +// 0.5 s in, while its session is lost. The device events and quits it got, in order, the manifest's answer, and what +// the stand-in saw: the sessions made and lost, and the last session's frames. +void OverlayAppLossCase(CaseContext& c) +{ + using namespace vr::IVROverlay_028; + Json::Value r; + r["init"] = (int)c.core->Init(vr::VRApplication_Overlay, nullptr); + if (r["init"].asInt() != vr::VRInitError_None) { + c.Record("loss", r); + return; + } + auto* ov = Interface(c, "IVROverlay_028"); + auto* sys = Interface(c, "IVRSystem_022"); + Json::Value events(Json::arrayValue); + if (ov && sys) { + vr::VROverlayHandle_t h = vr::k_ulOverlayHandleInvalid; + ov->CreateOverlay("oc.helper.loss", "Helper", &h); + std::vector grey(64 * 64 * 4, 0x80); + ov->SetOverlayRaw(h, grey.data(), 64, 64, 4); + vr::HmdMatrix34_t m = At(0, 1.5f, -2); + ov->SetOverlayTransformAbsolute(h, vr::TrackingUniverseStanding, &m); + ov->ShowOverlay(h); + const auto start = std::chrono::steady_clock::now(); + const auto end = start + std::chrono::milliseconds(2500); + bool manifestLoaded = false; + while (std::chrono::steady_clock::now() < end) { + if (!manifestLoaded && !c.manifest.empty() && std::chrono::steady_clock::now() >= start + std::chrono::milliseconds(500)) { + manifestLoaded = true; + auto* in = Interface(c, "IVRInput_011"); + r["manifest"] = in ? (int)in->SetActionManifestPath(c.manifest.c_str()) : -1; + } + vr::VREvent_t ev{}; + while (sys->PollNextEvent(&ev, sizeof(ev))) { + if (ev.eventType == vr::VREvent_TrackedDeviceActivated || ev.eventType == vr::VREvent_TrackedDeviceDeactivated + || ev.eventType == vr::VREvent_Quit || ev.eventType == vr::VREvent_DriverRequestedQuit) + events.append((int)ev.eventType); + } + std::this_thread::sleep_for(std::chrono::milliseconds(11)); + } + ov->DestroyOverlay(h); + } + c.core->Cleanup(); + r["events"] = events; + Json::Value dump = ReadStandInDump(); + for (const char* key : { "sessions_created", "sessions_lost", "instances_created", "instances_lost", "frames_ended" }) + r[key] = dump[key]; + c.Record("loss", r); +} + // An overlay application's textures of each API, at once: each is copied onto OpenLamina's own D3D11 device (the // overlay session's), and its quad shows the test pattern. A kind whose device can't be made here skips the case. struct OverlayTextureKind { @@ -1045,6 +1096,7 @@ const std::map& conformance::HelperCases() { static const std::map cases = { { "overlay-app", OverlayAppCase }, + { "overlay-app-loss", OverlayAppLossCase }, { "overlay-app-two-apis", [](CaseContext& c) { OverlayAppTexturesCase(c, "textures", { { GfxKind::D3D11, 29, "d3d11" }, { GfxKind::D3D12, 29, "d3d12" } }); } }, diff --git a/tests/conformance/scripts/lost.json b/tests/conformance/scripts/lost.json index af8587bf..b7683ed2 100644 --- a/tests/conformance/scripts/lost.json +++ b/tests/conformance/scripts/lost.json @@ -1 +1 @@ -{ "#": "The session is being lost at frame 60: one VREvent_Quit, then everything fails softly", "events": [{ "frame": 60, "type": "session_state", "state": "LOSS_PENDING" }] } +{ "#": "The session is lost at frame 60, and the system is there again at once: the devices deactivated, a new session made, the devices activated again, and no VREvent_Quit", "events": [{ "frame": 60, "type": "session_state", "state": "LOSS_PENDING" }] } diff --git a/tests/conformance/scripts/lost_before_submit.json b/tests/conformance/scripts/lost_before_submit.json new file mode 100644 index 00000000..9a7229dd --- /dev/null +++ b/tests/conformance/scripts/lost_before_submit.json @@ -0,0 +1 @@ +{ "#": "The session is lost at its first frame, before the game's first Submit, and the headset is gone for 300 ms: the game's Submits answer an error until the session is made again, which then moves to the game's device; no VREvent_Quit", "events": [{ "frame": 1, "type": "session_state", "state": "LOSS_PENDING" }], "loss_unavailable_ms": 300 } diff --git a/tests/conformance/scripts/lost_input.json b/tests/conformance/scripts/lost_input.json new file mode 100644 index 00000000..38bdee62 --- /dev/null +++ b/tests/conformance/scripts/lost_input.json @@ -0,0 +1 @@ +{ "#": "The session is lost at frame 20 and the system is there again at once (case input-frames): the game's actions are attached to the session made again", "events": [{ "frame": 20, "type": "session_state", "state": "LOSS_PENDING" }], "interaction_profile": "/interaction_profiles/valve/index_controller" } diff --git a/tests/conformance/scripts/lost_overlay.json b/tests/conformance/scripts/lost_overlay.json new file mode 100644 index 00000000..a2f4150a --- /dev/null +++ b/tests/conformance/scripts/lost_overlay.json @@ -0,0 +1 @@ +{ "#": "An overlay application's session is lost at its frame 30, and the headset is gone for 300 ms: its own loop makes the session again about a second later; the devices deactivated and activated again, and no VREvent_Quit", "extensions": ["XR_KHR_D3D11_enable", "XR_EXTX_overlay", "XR_MND_headless"], "events": [{ "frame": 30, "type": "session_state", "state": "LOSS_PENDING" }], "loss_unavailable_ms": 300 } diff --git a/tests/conformance/scripts/lost_unavailable.json b/tests/conformance/scripts/lost_unavailable.json new file mode 100644 index 00000000..3dccca0e --- /dev/null +++ b/tests/conformance/scripts/lost_unavailable.json @@ -0,0 +1 @@ +{ "#": "The session is lost at frame 10, and the headset is gone for 300 ms: the first try to find it fails, the next (a second later) makes the session again; no VREvent_Quit", "events": [{ "frame": 10, "type": "session_state", "state": "LOSS_PENDING" }], "loss_unavailable_ms": 300 } diff --git a/tests/conformance/xr_failures.py b/tests/conformance/xr_failures.py index f33b67ed..cc30b8d4 100644 --- a/tests/conformance/xr_failures.py +++ b/tests/conformance/xr_failures.py @@ -64,6 +64,44 @@ def check(name, want, out): misses.append(f"frame-loop: {len(quits)} VREvent_Quit polled (frames {quits}), expected {want['quit_events']}") elif "quit_from_frame" in want and quits and quits[0] < want["quit_from_frame"]: misses.append(f"frame-loop: VREvent_Quit polled at frame {quits[0]}, expected from frame {want['quit_from_frame']}") + if "device_events" in want: + # A loss: the devices deactivated (101), then activated again (100) on the new session, in that order (after + # any activations at the start); the runtime asks for no quit (704) + seq = [e for r in frames for e in r.get("events", []) if e in (100, 101, 704)] + rest = iter(seq) + if not all(any(e == w for e in rest) for w in want["device_events"]): + misses.append(f"frame-loop: device events {seq}, expected {want['device_events']} in that order") + if 704 in seq: + misses.append("frame-loop: VREvent_DriverRequestedQuit polled after a loss") + for key, at_least in (("sessions_lost", False), ("sessions_created_min", True)): + if key in want: + dump = json.loads(dump_path.read_text(encoding="utf-8")) if dump_path.exists() else {} + name = "sessions_created" if at_least else key + got = dump.get(name, 0) + if (got < want[key]) if at_least else (got != want[key]): + misses.append(f"frame-loop: the runtime saw {got} {name.replace('_', ' ')}, expected {'at least ' if at_least else ''}{want[key]}") + if "loss_record" in want: + # A helper program's loss (case overlay-app-loss): its device events in order, no quit, and a session made again + # that ran frames + r = by_method.get(("overlay-app-loss", "loss")) or {} + want_r = want["loss_record"] + events = r.get("events", []) + rest = iter(events) + if not all(any(e == w for e in rest) for w in want_r["device_events"]): + misses.append(f"overlay-app-loss: device events {events}, expected {want_r['device_events']} in that order") + if 700 in events or 704 in events: + misses.append(f"overlay-app-loss: a quit polled after a loss ({events})") + for key, value in want_r.items(): + if key.endswith("_min") and (r.get(key.removesuffix("_min")) or 0) < value: + misses.append(f"overlay-app-loss: {key.removesuffix('_min')} {r.get(key.removesuffix('_min'))}, expected at least {value}") + elif key in ("sessions_lost", "instances_lost", "manifest") and r.get(key) != value: + misses.append(f"overlay-app-loss: {key} {r.get(key)}, expected {value}") + if "action_attaches_min" in want: + # Case input-frames through a loss: the game's actions are attached to the session made again too + path = out / "raw" / "case-input-frames.dump.json" + dump = json.loads(path.read_text(encoding="utf-8")) if path.exists() else {} + if dump.get("sessions_lost") != 1 or dump.get("action_attaches", 0) < want["action_attaches_min"]: + misses.append(f"input-frames: {dump.get('sessions_lost')} sessions lost and {dump.get('action_attaches')} action attaches, expected 1 and at least {want['action_attaches_min']}") if "old_event_quits" in want: # IVRSystem_009's events (case old-events): the quit arrives, and nothing is written past the 32-byte event olds = [r for r in records if r.get("case") == "old-events" and str(r.get("method", "")).startswith("frame ")] diff --git a/tests/runtime/actions.cpp b/tests/runtime/actions.cpp index 05ed2880..3e76755e 100644 --- a/tests/runtime/actions.cpp +++ b/tests/runtime/actions.cpp @@ -169,6 +169,11 @@ XrResult XRAPI_CALL AttachSessionActionSets(XrSession session, const XrSessionAc it->second->attached = true; } s->actionsAttached = true; + if (Dump::Enabled()) { + Dump& d = GetDump(); + std::lock_guard dg(d.lock); + d.root["action_attaches"] = d.root["action_attaches"].asUInt64() + 1; + } return XR_SUCCESS; } diff --git a/tests/runtime/runtime.cpp b/tests/runtime/runtime.cpp index 4eb3cbae..1d1cfcce 100644 --- a/tests/runtime/runtime.cpp +++ b/tests/runtime/runtime.cpp @@ -401,6 +401,11 @@ XrResult XRAPI_CALL GetSystem(XrInstance, const XrSystemGetInfo* info, XrSystemI FAIL_POINT("xrGetSystem"); if (info->formFactor != XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY) return XR_ERROR_FORM_FACTOR_UNSUPPORTED; + { + std::lock_guard g(St().lock); + if (std::chrono::steady_clock::now() < St().systemBackAt) + return XR_ERROR_FORM_FACTOR_UNAVAILABLE; // gone after a scripted loss + } *system = kSystem; return XR_SUCCESS; } @@ -502,6 +507,11 @@ XrResult XRAPI_CALL CreateSession(XrInstance, const XrSessionCreateInfo* info, X d.root["session"]["binding"] = binding; d.root["session"]["overlay"] = session->overlay; } + if (Dump::Enabled()) { + Dump& d = GetDump(); + std::lock_guard dg(d.lock); + d.root["sessions_created"] = d.root["sessions_created"].asUInt64() + 1; + } RtSession* raw = session.get(); St().session = std::move(session); QueueState(raw, XR_SESSION_STATE_IDLE); @@ -528,6 +538,8 @@ XrResult XRAPI_CALL BeginSession(XrSession, const XrSessionBeginInfo* info) RtSession* s = St().session.get(); if (!s) return XR_ERROR_HANDLE_INVALID; + if (s->lost) + return XR_ERROR_SESSION_LOST; if (s->running) return XR_ERROR_SESSION_RUNNING; if (info->primaryViewConfigurationType != XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO) @@ -542,6 +554,8 @@ XrResult XRAPI_CALL EndSession(XrSession) RtSession* s = St().session.get(); if (!s) return XR_ERROR_HANDLE_INVALID; + if (s->lost) + return XR_ERROR_SESSION_LOST; if (!s->running) return XR_ERROR_SESSION_NOT_RUNNING; if (s->state != XR_SESSION_STATE_STOPPING) @@ -557,6 +571,8 @@ XrResult XRAPI_CALL RequestExitSession(XrSession) { std::lock_guard g(St().lock); RtSession* s = St().session.get(); + if (s && s->lost) + return XR_ERROR_SESSION_LOST; if (!s || !s->running) return XR_ERROR_SESSION_NOT_RUNNING; s->exitRequested = true; @@ -814,6 +830,8 @@ XrResult XRAPI_CALL WaitFrame(XrSession, const XrFrameWaitInfo*, XrFrameState* f RtSession* s = St().session.get(); if (!s) return XR_ERROR_HANDLE_INVALID; + if (s->lost) + return XR_ERROR_SESSION_LOST; if (!s->running) return XR_ERROR_SESSION_NOT_RUNNING; // A second wait before the first one's frame began @@ -826,7 +844,29 @@ XrResult XRAPI_CALL WaitFrame(XrSession, const XrFrameWaitInfo*, XrFrameState* f QueueState(s, XR_SESSION_STATE_VISIBLE); else if (s->framesWaited == 3) QueueState(s, XR_SESSION_STATE_FOCUSED); - for (const ScriptEvent& ev : GetScript().events) { + for (size_t i = 0; i < GetScript().events.size(); i++) { + const ScriptEvent& ev = GetScript().events[i]; + const bool loss = ev.type == "session_state" && ev.state == "LOSS_PENDING"; + // A loss is sent once: the session made after it runs on + if (loss && ev.frame == s->framesWaited && St().lossesSent.insert(i).second) { + QueueState(s, XR_SESSION_STATE_LOSS_PENDING); + s->lost = true; + St().systemBackAt = std::chrono::steady_clock::now() + std::chrono::milliseconds(GetScript().lossUnavailableMs); + if (Dump::Enabled()) { + Dump& d = GetDump(); + std::lock_guard dg(d.lock); + d.root["sessions_lost"] = d.root["sessions_lost"].asUInt64() + 1; + } + continue; + } + if (loss) + continue; + // EXITING is the user quitting from the runtime: STOPPING first, EXITING once the session has ended + if (ev.frame == s->framesWaited && ev.type == "session_state" && ev.state == "EXITING" && s->running) { + s->exitRequested = true; + QueueState(s, XR_SESSION_STATE_STOPPING); + continue; + } if (ev.frame == s->framesWaited && ev.type == "session_state") QueueState(s, StateFromName(ev.state)); if (ev.frame == s->framesWaited && ev.type == "refresh_rate" && ev.rate > 0) @@ -848,6 +888,8 @@ XrResult XRAPI_CALL BeginFrame(XrSession, const XrFrameBeginInfo*) RtSession* s = St().session.get(); if (!s) return XR_ERROR_HANDLE_INVALID; + if (s->lost) + return XR_ERROR_SESSION_LOST; if (!s->running) return XR_ERROR_SESSION_NOT_RUNNING; // As Monado answers a headless session: it has no frames to begin @@ -1187,6 +1229,8 @@ XrResult XRAPI_CALL EndFrame(XrSession, const XrFrameEndInfo* info) RtSession* s = St().session.get(); if (!s) return XR_ERROR_HANDLE_INVALID; + if (s->lost) + return XR_ERROR_SESSION_LOST; if (s->headless || !s->frameOpen) { CountFrameOrderError(); // an end without a begin return XR_ERROR_CALL_ORDER_INVALID; diff --git a/tests/runtime/runtime_internal.h b/tests/runtime/runtime_internal.h index 5614e16e..516d586e 100644 --- a/tests/runtime/runtime_internal.h +++ b/tests/runtime/runtime_internal.h @@ -64,6 +64,7 @@ #include "script.h" +#include #include #include #include @@ -182,6 +183,7 @@ struct RtSession { bool overlay = false; bool running = false; bool exitRequested = false; // xrRequestExitSession: xrEndSession then goes on to EXITING, as the spec says + bool lost = false; // a scripted LOSS_PENDING: its calls answer XR_ERROR_SESSION_LOST XrSessionState state = XR_SESSION_STATE_UNKNOWN; uint64_t framesWaited = 0; uint64_t framesBegun = 0; @@ -214,6 +216,9 @@ struct RtState { std::vector paths{ "" }; XrTime time = 1000000000; bool presenceSent = false; + // A scripted loss: the system is gone (xrGetSystem fails) until then; the loss events already sent (by index) + std::chrono::steady_clock::time_point systemBackAt{}; + std::set lossesSent; }; RtState& St(); diff --git a/tests/runtime/script.cpp b/tests/runtime/script.cpp index 2150dc67..0cada9af 100644 --- a/tests/runtime/script.cpp +++ b/tests/runtime/script.cpp @@ -139,6 +139,7 @@ static Script LoadScript() add(v); } } + s.lossUnavailableMs = root["loss_unavailable_ms"].asUInt(); for (const Json::Value& r : root["refresh_rates"]) s.refreshRates.push_back(r.asFloat()); auto addIpd = [&](const Json::Value& e) { diff --git a/tests/runtime/script.h b/tests/runtime/script.h index 1a50ec32..96d0270e 100644 --- a/tests/runtime/script.h +++ b/tests/runtime/script.h @@ -7,6 +7,9 @@ // and XR_KHR_win32_convert_performance_counter_time is XR_KHR_convert_timespec_time), // "ipd": 0.063, "display_hz": 90, "stage_bounds": [width, depth], // "events": [{ "frame": 60, "type": "session_state", "state": "LOSS_PENDING" }] +// (LOSS_PENDING loses the session for real, once per process: its calls answer XR_ERROR_SESSION_LOST +// from then on, and xrGetSystem answers XR_ERROR_FORM_FACTOR_UNAVAILABLE for loss_unavailable_ms; +// EXITING on a running session is the user quitting: STOPPING, then EXITING after xrEndSession), // (or { "frame": 10, "type": "refresh_rate", "rate": 120 }: the display changes to that rate, announced // with XrEventDataDisplayRefreshRateChangedFB), // "fail": [{ "function": "xrEndFrame", "result": "XR_ERROR_RUNTIME_FAILURE", "after": 29, "count": 1 }] @@ -122,6 +125,7 @@ struct Script { std::map> poses; std::vector refreshRates; // empty: displayHz alone std::vector ipdChanges; + uint32_t lossUnavailableMs = 0; // how long the system is gone after a scripted LOSS_PENDING }; // The scripted pose of a path at a frame (the last entry whose from_frame has been reached), nullptr if none diff --git a/tests/unit/SessionLifecycleTests.cpp b/tests/unit/SessionLifecycleTests.cpp new file mode 100644 index 00000000..44a49015 --- /dev/null +++ b/tests/unit/SessionLifecycleTests.cpp @@ -0,0 +1,36 @@ +#include "doctest.h" + +#include "stdafx.h" + +#include "XrBackend.h" + +#include + +using Outcome = XrBackend::StateOutcome; + +TEST_CASE("A lost session is made again, and only the user quitting quits") +{ + // LOSS_PENDING is a loss, whatever came before + CHECK(XrBackend::OutcomeOfState(XR_SESSION_STATE_LOSS_PENDING, false, false) == Outcome::Lost); + CHECK(XrBackend::OutcomeOfState(XR_SESSION_STATE_LOSS_PENDING, true, false) == Outcome::Lost); + // EXITING after STOPPING is the user quitting from the runtime; without STOPPING, the session went away + CHECK(XrBackend::OutcomeOfState(XR_SESSION_STATE_EXITING, true, false) == Outcome::Quit); + CHECK(XrBackend::OutcomeOfState(XR_SESSION_STATE_EXITING, false, false) == Outcome::Lost); + // OpenLamina ending the session itself (a restart) is neither + CHECK(XrBackend::OutcomeOfState(XR_SESSION_STATE_EXITING, true, true) == Outcome::Ignored); + CHECK(XrBackend::OutcomeOfState(XR_SESSION_STATE_LOSS_PENDING, false, true) == Outcome::Ignored); + // Other states mean nothing for the game here + CHECK(XrBackend::OutcomeOfState(XR_SESSION_STATE_STOPPING, false, false) == Outcome::Ignored); + CHECK(XrBackend::OutcomeOfState(XR_SESSION_STATE_IDLE, true, false) == Outcome::Ignored); +} + +TEST_CASE("The system is asked for at once after a loss, then about once a second") +{ + using clock = std::chrono::steady_clock; + const clock::time_point t = clock::now(); + CHECK(XrBackend::RecoveryDue({}, t)); + CHECK_FALSE(XrBackend::RecoveryDue(t, t)); + CHECK_FALSE(XrBackend::RecoveryDue(t, t + std::chrono::milliseconds(999))); + CHECK(XrBackend::RecoveryDue(t, t + XrBackend::kRecoveryInterval)); + CHECK(XrBackend::RecoveryDue(t, t + std::chrono::seconds(30))); +}