From 28a8fbd743a51342b1dc0f1ad5971cf6cfd8bdd7 Mon Sep 17 00:00:00 2001 From: Sapphire Date: Wed, 26 Aug 2026 07:31:44 -0500 Subject: [PATCH] Add patches and dashboard setting for maximum steamvr_lh pose prediction --- dashboard/qml/SettingsPage.qml | 32 ++++ dashboard/settings.cpp | 35 +++++ dashboard/settings.h | 4 + docs/configuration.md | 9 +- ...h-Add-m_relation_history_get_clamped.patch | 138 ++++++++++++++++++ ...eamvr_lh-Add-LH_MAX_EXTRAPOLATION_MS.patch | 44 ++++++ server/driver/configuration.cpp | 3 + server/driver/configuration.h | 1 + server/driver/wivrn_session.cpp | 3 + 9 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 patches/monado/0010-a-math-Add-m_relation_history_get_clamped.patch create mode 100644 patches/monado/0011-d-steamvr_lh-Add-LH_MAX_EXTRAPOLATION_MS.patch diff --git a/dashboard/qml/SettingsPage.qml b/dashboard/qml/SettingsPage.qml index b2d8b881..d6da4e37 100644 --- a/dashboard/qml/SettingsPage.qml +++ b/dashboard/qml/SettingsPage.qml @@ -132,6 +132,31 @@ Kirigami.ScrollablePage { toolTipText: i18n("Allows the use of lighthouse-based controllers and trackers.\nRequires SteamVR to be installed.\nDevices must be be powered on before connecting to WiVRn.\nAn external tool such as motoc is needed for calibration.") } } + Controls.CheckBox { + id: lh_max_extrapolation_enabled + visible: Settings.steamvr_lh_supported && steamvr_lh.checked + text: i18n("Clamp extrapolation for SteamVR tracked devices") + } + RowLayout { + visible: Settings.steamvr_lh_supported && steamvr_lh.checked && lh_max_extrapolation_enabled.checked + Kirigami.FormData.label: i18n("SteamVR max pose extrapolation") + Controls.Slider { + id: lh_max_extrapolation + Layout.fillWidth: true + from: 0.0 + to: 100.0 + stepSize: 1.0 + value: Settings.lhMaxExtrapolation + } + Controls.Label { + text: i18ncp("value display for SteamVR max pose extrapolation", "%1ms", "%1ms", lh_max_extrapolation.value) + Layout.preferredWidth: 35 + Layout.alignment: Qt.AlignRight + } + Kirigami.ContextualHelpButton { + toolTipText: i18n("Maximum time in milliseconds that poses may be extrapolated ahead for SteamVR tracked devices. Tune this value if you experience jittery or wobbly tracking.") + } + } RowLayout { visible: Settings.steamvr_lh_supported && steamvr_lh.checked Kirigami.FormData.label: i18n("SteamVR joystick deadzone") @@ -309,6 +334,11 @@ Kirigami.ScrollablePage { Settings.debugGui = debug_gui.checked; Settings.steamVrLh = steamvr_lh.checked; + if (lh_max_extrapolation_enabled.checked) { + Settings.lhMaxExtrapolation = lh_max_extrapolation.value; + } else { + Settings.lhMaxExtrapolation = -1; + } Settings.lhStickDeadzone = lh_stick_deadzone.value; Settings.hidForwarding = hid_forwarding.checked; @@ -319,6 +349,8 @@ Kirigami.ScrollablePage { select_game.load(); debug_gui.checked = Settings.debugGui; steamvr_lh.checked = Settings.steamVrLh; + lh_max_extrapolation_enabled.checked = Settings.lhMaxExtrapolationEnabled; + lh_max_extrapolation.value = Settings.lhMaxExtrapolation; lh_stick_deadzone.value = Settings.lhStickDeadzone; hid_forwarding.checked = Settings.hidForwarding; diff --git a/dashboard/settings.cpp b/dashboard/settings.cpp index 3bd27cfc..5842a80b 100644 --- a/dashboard/settings.cpp +++ b/dashboard/settings.cpp @@ -121,6 +121,8 @@ void Settings::emitAllChanged() openvrChanged(); debugGuiChanged(); steamVrLhChanged(); + lhMaxExtrapolationEnabledChanged(); + lhMaxExtrapolationChanged(); lhStickDeadzoneChanged(); hidForwardingChanged(); portChanged(); @@ -417,6 +419,39 @@ void Settings::set_steamVrLh(const bool & value) steamVrLhChanged(); } +bool Settings::lhMaxExtrapolationEnabled() const +{ + auto it = m_jsonSettings.find("lh-max-extrapolation"); + return it != m_jsonSettings.end() and it->is_number(); +} + +int64_t Settings::lhMaxExtrapolation() const +{ + auto it = m_jsonSettings.find("lh-max-extrapolation"); + if (it != m_jsonSettings.end() and it->is_number()) + return static_cast(*it); + return 50; +} + +void Settings::set_lhMaxExtrapolation(const int64_t & value) +{ + auto old = lhMaxExtrapolation(); + if (value != -1) + { + m_jsonSettings["lh-max-extrapolation"] = value; + } + else + { + m_jsonSettings.erase("lh-max-extrapolation"); + } + + if (old != value) + { + lhMaxExtrapolationEnabledChanged(); + lhMaxExtrapolationChanged(); + } +} + float Settings::lhStickDeadzone() const { auto it = m_jsonSettings.find("lh-stick-deadzone"); diff --git a/dashboard/settings.h b/dashboard/settings.h index f63a39d9..6b0e3ed5 100644 --- a/dashboard/settings.h +++ b/dashboard/settings.h @@ -76,6 +76,8 @@ public: Q_PROPERTY(bool hidForwarding READ hidForwarding WRITE set_hidForwarding NOTIFY hidForwardingChanged) Q_PROPERTY(bool debugGui READ debugGui WRITE set_debugGui NOTIFY debugGuiChanged) Q_PROPERTY(bool steamVrLh READ steamVrLh WRITE set_steamVrLh NOTIFY steamVrLhChanged) + Q_PROPERTY(bool lhMaxExtrapolationEnabled READ lhMaxExtrapolationEnabled NOTIFY lhMaxExtrapolationEnabledChanged) + Q_PROPERTY(int64_t lhMaxExtrapolation READ lhMaxExtrapolation WRITE set_lhMaxExtrapolation NOTIFY lhMaxExtrapolationChanged) Q_PROPERTY(float lhStickDeadzone READ lhStickDeadzone WRITE set_lhStickDeadzone NOTIFY lhStickDeadzoneChanged) Q_PROPERTY(bool flatpak READ flatpak CONSTANT) @@ -92,6 +94,8 @@ public: SETTER_GETTER_NOTIFY(bool, hidForwarding) SETTER_GETTER_NOTIFY(bool, debugGui) SETTER_GETTER_NOTIFY(bool, steamVrLh) + SETTER_GETTER_NOTIFY(bool, lhMaxExtrapolationEnabled) + SETTER_GETTER_NOTIFY(int64_t, lhMaxExtrapolation) SETTER_GETTER_NOTIFY(float, lhStickDeadzone) SETTER_GETTER_NOTIFY(bool, tcpOnly) SETTER_GETTER_NOTIFY(int, port) diff --git a/docs/configuration.md b/docs/configuration.md index abf3236a..3828833a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -155,10 +155,17 @@ Enables the Monado debug gui. ## `use-steamvr-lh` Default value: `false` -Only available when built with `WIVRN_FEATURE_STEAMVR_LIGHTHOUSE` +Only available when built with `WIVRN_FEATURE_STEAMVR_LIGHTHOUSE`. Enables the driver to load SteamVR Lighthouse devices. +## `lh-max-extrapolation` +Default value: unset + +Only available when built with `WIVRN_FEATURE_STEAMVR_LIGHTHOUSE`. + +Maximum time in milliseconds that poses may be extrapolated ahead for SteamVR Lighthouse devices. + ## `lh-stick-deadzone` Default value: `0` diff --git a/patches/monado/0010-a-math-Add-m_relation_history_get_clamped.patch b/patches/monado/0010-a-math-Add-m_relation_history_get_clamped.patch new file mode 100644 index 00000000..d523a498 --- /dev/null +++ b/patches/monado/0010-a-math-Add-m_relation_history_get_clamped.patch @@ -0,0 +1,138 @@ +From 99722fde70de23bad66f47ab7a53748ce0e498d0 Mon Sep 17 00:00:00 2001 +From: Sapphire +Date: Wed, 26 Aug 2026 06:14:13 -0500 +Subject: [PATCH 1/2] a/math: Add m_relation_history_get_clamped + +Signed-off-by: Sapphire +--- + src/xrt/auxiliary/math/m_relation_history.cpp | 33 ++++++++++++++++--- + src/xrt/auxiliary/math/m_relation_history.h | 23 ++++++++++++- + 2 files changed, 50 insertions(+), 6 deletions(-) + +diff --git a/src/xrt/auxiliary/math/m_relation_history.cpp b/src/xrt/auxiliary/math/m_relation_history.cpp +index 700f9e7fc..47a61ed6d 100644 +--- a/src/xrt/auxiliary/math/m_relation_history.cpp ++++ b/src/xrt/auxiliary/math/m_relation_history.cpp +@@ -84,15 +84,20 @@ m_relation_history_push(struct m_relation_history *rh, struct xrt_space_relation + } + + enum m_relation_history_result +-m_relation_history_get(const struct m_relation_history *rh, +- int64_t at_timestamp_ns, +- struct xrt_space_relation *out_relation) ++m_relation_history_get_clamped(const struct m_relation_history *rh, ++ int64_t at_timestamp_ns, ++ int64_t max_extrapolation_ns, ++ int64_t *out_timestamp_ns, ++ struct xrt_space_relation *out_relation) + { + XRT_TRACE_MARKER(); + std::unique_lock lock(rh->mutex); + try { + if (rh->impl.empty() || at_timestamp_ns == 0) { + // Do nothing. You push nothing to the buffer you get nothing from the buffer. ++ if (out_timestamp_ns) { ++ *out_timestamp_ns = 0; ++ } + *out_relation = {}; + return M_RELATION_HISTORY_RESULT_INVALID; + } +@@ -111,18 +116,26 @@ m_relation_history_get(const struct m_relation_history *rh, + // The desired timestamp is after what our buffer contains. + // (pose-prediction) + // Output flags match the most recent buffer entry. +- int64_t diff_prediction_ns = static_cast(at_timestamp_ns) - rh->impl.back().timestamp; ++ int64_t diff_prediction_ns = MIN( ++ static_cast(at_timestamp_ns) - rh->impl.back().timestamp, max_extrapolation_ns); + double delta_s = time_ns_to_s(diff_prediction_ns); + + U_LOG_T("Extrapolating %f s past the back of the buffer!", delta_s); + + m_predict_relation(&rh->impl.back().relation, delta_s, out_relation); ++ if (out_timestamp_ns) { ++ // We extrapolated to the requested timestamp. ++ *out_timestamp_ns = at_timestamp_ns + diff_prediction_ns; ++ } + return M_RELATION_HISTORY_RESULT_PREDICTED; + } + if (at_timestamp_ns == it->timestamp) { + // exact match: + // Flags copied directly along with everything else. + U_LOG_T("Exact match in the buffer!"); ++ if (out_timestamp_ns) { ++ *out_timestamp_ns = at_timestamp_ns; ++ } + *out_relation = it->relation; + return M_RELATION_HISTORY_RESULT_EXACT; + } +@@ -131,10 +144,16 @@ m_relation_history_get(const struct m_relation_history *rh, + // The desired timestamp is before what our buffer contains. + // (an edge case where somebody asks for a really old pose and we do our best) + // Output flags are the same as the input flags for the history entry we use +- int64_t diff_prediction_ns = static_cast(at_timestamp_ns) - rh->impl.front().timestamp; ++ int64_t diff_prediction_ns = ++ -MIN(abs(static_cast(at_timestamp_ns) - rh->impl.front().timestamp), ++ max_extrapolation_ns); + double delta_s = time_ns_to_s(diff_prediction_ns); + U_LOG_T("Extrapolating %f s before the front of the buffer!", delta_s); + m_predict_relation(&rh->impl.front().relation, delta_s, out_relation); ++ if (out_timestamp_ns) { ++ // We extrapolated to the requested timestamp. ++ *out_timestamp_ns = at_timestamp_ns + diff_prediction_ns; ++ } + return M_RELATION_HISTORY_RESULT_REVERSE_PREDICTED; + } + U_LOG_T("Interpolating within buffer!"); +@@ -174,6 +193,10 @@ m_relation_history_get(const struct m_relation_history *rh, + result.linear_velocity = m_vec3_lerp(predecessor.relation.linear_velocity, + successor.relation.linear_velocity, amount_to_lerp); + } ++ if (out_timestamp_ns) { ++ // We interpolated to the requested timestamp. ++ *out_timestamp_ns = at_timestamp_ns; ++ } + *out_relation = result; + return M_RELATION_HISTORY_RESULT_INTERPOLATED; + +diff --git a/src/xrt/auxiliary/math/m_relation_history.h b/src/xrt/auxiliary/math/m_relation_history.h +index 86e08ef5e..5aaa94404 100644 +--- a/src/xrt/auxiliary/math/m_relation_history.h ++++ b/src/xrt/auxiliary/math/m_relation_history.h +@@ -83,12 +83,33 @@ m_relation_history_push_with_motion_estimation(struct m_relation_history *rh, + * Read-only operation - doesn't remove anything from the buffer or anything like that - you can call this as often + * as you want. + * ++ * @param max_extrapolation_ns Maximum time the pose may be extrapolated to. INT64_MAX means extrapolation is unlimited. ++ * @param out_timestamp_ns Optional out parameter for the timestamp of the pose. ++ * @param out_relation Out parameter for the resulting @ref xrt_space_relation. ++ * + * @public @memberof m_relation_history + */ + enum m_relation_history_result ++m_relation_history_get_clamped(const struct m_relation_history *rh, ++ int64_t at_timestamp_ns, ++ int64_t max_extrapolation_ns, ++ int64_t *out_timestamp_ns, ++ struct xrt_space_relation *out_relation); ++ ++/*! ++ * Wrapper for @ref m_relation_history_get_clamped with unlimited extrapolation. ++ * ++ * @copydoc m_relation_history_get_clamped ++ * ++ * @public @memberof m_relation_history ++ */ ++static inline enum m_relation_history_result + m_relation_history_get(const struct m_relation_history *rh, + int64_t at_timestamp_ns, +- struct xrt_space_relation *out_relation); ++ struct xrt_space_relation *out_relation) ++{ ++ return m_relation_history_get_clamped(rh, at_timestamp_ns, INT64_MAX, NULL, out_relation); ++} + + /*! + * Get the latest report in the buffer, if any. +-- +2.55.0 + diff --git a/patches/monado/0011-d-steamvr_lh-Add-LH_MAX_EXTRAPOLATION_MS.patch b/patches/monado/0011-d-steamvr_lh-Add-LH_MAX_EXTRAPOLATION_MS.patch new file mode 100644 index 00000000..0d803661 --- /dev/null +++ b/patches/monado/0011-d-steamvr_lh-Add-LH_MAX_EXTRAPOLATION_MS.patch @@ -0,0 +1,44 @@ +From d098ae99f41017945c2bacb8fcdb317c8b859085 Mon Sep 17 00:00:00 2001 +From: Sapphire +Date: Wed, 26 Aug 2026 06:17:10 -0500 +Subject: [PATCH 2/2] d/steamvr_lh: Add LH_MAX_EXTRAPOLATION_MS + +Signed-off-by: Sapphire +--- + src/xrt/drivers/steamvr_lh/device.cpp | 13 ++++++++++++- + 1 file changed, 12 insertions(+), 1 deletion(-) + +diff --git a/src/xrt/drivers/steamvr_lh/device.cpp b/src/xrt/drivers/steamvr_lh/device.cpp +index fc3da6066..29304677f 100644 +--- a/src/xrt/drivers/steamvr_lh/device.cpp ++++ b/src/xrt/drivers/steamvr_lh/device.cpp +@@ -53,6 +53,7 @@ + DEBUG_GET_ONCE_BOOL_OPTION(lh_emulate_hand, "LH_EMULATE_HAND", true) + DEBUG_GET_ONCE_FLOAT_OPTION(lh_override_ipd, "LH_OVERRIDE_IPD_MM", -1.0f) + DEBUG_GET_ONCE_BOOL_OPTION(lh_standby_on_exit, "LH_STANDBY_ON_EXIT", false) ++DEBUG_GET_ONCE_NUM_OPTION(lh_max_extrapolation_ms, "LH_MAX_EXTRAPOLATION_MS", 50) + + // Each device will have its own input class. + struct InputClass +@@ -740,7 +741,17 @@ ControllerDevice::get_hand_tracking(enum xrt_input_name name, + void + Device::get_pose(uint64_t at_timestamp_ns, xrt_space_relation *out_relation) + { +- m_relation_history_get(this->relation_hist, at_timestamp_ns, out_relation); ++ static const int64_t max_extrapolation_ns = []() -> int64_t { ++ int64_t raw = debug_get_num_option_lh_max_extrapolation_ms(); ++ if (raw == -1) { ++ return INT64_MAX; ++ } ++ ++ return raw * U_TIME_1MS_IN_NS; ++ }(); ++ ++ m_relation_history_get_clamped(this->relation_hist, at_timestamp_ns, max_extrapolation_ns, nullptr, ++ out_relation); + } + + xrt_result_t +-- +2.55.0 + diff --git a/server/driver/configuration.cpp b/server/driver/configuration.cpp index 9a1e41ab..36820f96 100644 --- a/server/driver/configuration.cpp +++ b/server/driver/configuration.cpp @@ -200,6 +200,9 @@ configuration::configuration() if (auto it = json.find("use-steamvr-lh"); it != json.end()) use_steamvr_lh = *it; + if (auto it = json.find("lh-max-extrapolation"); it != json.end()) + lh_max_extrapolation = *it; + if (auto it = json.find("lh-stick-deadzone"); it != json.end()) lh_stick_deadzone = *it; diff --git a/server/driver/configuration.h b/server/driver/configuration.h index 49187bfc..eb24795b 100644 --- a/server/driver/configuration.h +++ b/server/driver/configuration.h @@ -57,6 +57,7 @@ struct configuration std::vector application; bool debug_gui = false; bool use_steamvr_lh = false; + std::optional lh_max_extrapolation; std::optional lh_stick_deadzone; bool hid_forwarding = false; bool tcp_only = false; diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index e2df0696..dac8173a 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -147,6 +147,9 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr connection if (conf.lh_stick_deadzone > 0.01f) setenv("LH_STICK_DEADZONE", std::format("{:.2}", *conf.lh_stick_deadzone).c_str(), true); + if (conf.lh_max_extrapolation.has_value()) + setenv("LH_MAX_EXTRAPOLATION_MS", std::format("{}", *conf.lh_max_extrapolation).c_str(), true); + U_LOG_W("====================="); U_LOG_W("Disregard lighthousedb / chaperone related error messages from the lighthouse driver. These are irrelevant in case of WiVRn."); U_LOG_W("If getting a SIGSEGV right after this, you are likely using an unsupported SteamVR version!");