From 06b5fd7e6c3f90aae62dab3e03cd25d487ffba35 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Mon, 27 Jan 2025 17:17:25 +0100 Subject: [PATCH] Send both min and max prediction times --- client/scenes/stream_stats.cpp | 2 +- client/scenes/stream_tracking.cpp | 19 +++++-------------- common/wivrn_packets.h | 3 ++- server/driver/wivrn_session.cpp | 3 ++- server/driver/wivrn_session.h | 8 ++++++++ 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/client/scenes/stream_stats.cpp b/client/scenes/stream_stats.cpp index 5d65d327..cc05f931 100644 --- a/client/scenes/stream_stats.cpp +++ b/client/scenes/stream_stats.cpp @@ -327,7 +327,7 @@ std::vector scenes::stream::plot_performance_metrics(XrT ImPlot::PopStyleColor(5); { std::lock_guard lock(tracking_control_mutex); - ImGui::Text("%s", fmt::format(_F("Estimated motion to photons latency: {}ms"), std::chrono::duration_cast(tracking_control.offset).count()).c_str()); + ImGui::Text("%s", fmt::format(_F("Estimated motion to photons latency: {}ms"), std::chrono::duration_cast(tracking_control.max_offset).count()).c_str()); } ImGui::End(); diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index 7142f6da..e0ef5bcb 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -192,7 +192,6 @@ void scenes::stream::tracking() XrTime last_hand_sample = t0; std::vector tracking; std::vector hands; - int skip_samples = 0; const bool hand_tracking = config.check_feature(feature::hand_tracking); const bool face_tracking = config.check_feature(feature::face_tracking); @@ -220,9 +219,11 @@ void scenes::stream::tracking() control = tracking_control; } - XrDuration prediction = std::clamp(control.offset.count(), 0, 80'000'000); + XrDuration prediction = std::clamp(control.max_offset.count(), 0, 80'000'000); auto period = std::max(display_time_period.load(), 1'000'000); - for (XrDuration Δt = 0; Δt <= prediction + period / 2; Δt += period, ++samples) + for (XrDuration Δt = display_time_phase - t0 % period + (control.min_offset.count() / period) * period; + Δt <= prediction + period / 2; + Δt += period, ++samples) { auto & packet = tracking.emplace_back(); packet.production_timestamp = t0; @@ -283,23 +284,12 @@ void scenes::stream::tracking() e.code().value() != XR_ERROR_TIME_INVALID) throw; } - - // Make sure predictions are phase-synced with display time - if (Δt == 0 and prediction) - { - Δt = display_time_phase - t0 % period + skip_samples * period; - } } XrDuration busy_time = t.count(); // Target: polling between 1 and 5ms, with 20% busy time tracking_period = std::clamp(std::lerp(tracking_period, busy_time * 5, 0.2), min_tracking_period, max_tracking_period); - if (samples and busy_time / samples > 2'000'000) - { - skip_samples = busy_time / 2'000'000; - } - #ifdef __ANDROID__ if (next_battery_check < now and control.enabled[size_t(tid::battery)]) { @@ -363,4 +353,5 @@ void scenes::stream::operator()(to_headset::tracking_control && packet) audio_handle->set_mic_sate(packet.enabled[m]); tracking_control = packet; + tracking_control.min_offset = std::min(tracking_control.min_offset, tracking_control.max_offset); } diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index 5e693783..9a127b88 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -483,7 +483,8 @@ struct tracking_control last = microphone, }; - std::chrono::nanoseconds offset; + std::chrono::nanoseconds min_offset; + std::chrono::nanoseconds max_offset; std::array enabled; }; diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 57445c77..6f090ed3 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -104,7 +104,8 @@ void wivrn::tracking_control_t::send(wivrn_connection & connection, bool now) return; connection.send_stream(to_headset::tracking_control{ - .offset = std::chrono::nanoseconds(max.exchange(0)), + .min_offset = std::chrono::nanoseconds(min.exchange(80'000'000)), + .max_offset = std::chrono::nanoseconds(max.exchange(0)), .enabled = enabled, }); if (not now) diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index f97ab0a5..0d0293df 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -50,6 +50,7 @@ struct wivrn_comp_target_factory; class tracking_control_t { using T = std::chrono::nanoseconds::rep; + std::atomic min; std::atomic max; std::chrono::steady_clock::time_point next_sample; std::mutex mutex; @@ -68,6 +69,13 @@ public: while (prev < sample and max.compare_exchange_weak(prev, sample)) { } + if (sample > 0) + { + prev = min; + while (prev > sample and min.compare_exchange_weak(prev, sample)) + { + } + } } void send(wivrn_connection & connection, bool now = false);