From 7eea3c2e7bf9658592e990c0ed1e33f0cbd36dbb Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 1 Sep 2024 21:54:41 +0200 Subject: [PATCH] Sync predictions with display time --- client/scenes/stream.cpp | 3 +++ client/scenes/stream.h | 4 +++- client/scenes/stream_tracking.cpp | 7 ++++++- server/driver/history.h | 17 +++++++++++++---- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index c4310aff..f012c23a 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -353,6 +353,9 @@ void scenes::stream::render(const XrFrameState & frame_state) if (exiting) application::pop_scene(); + display_time_phase = frame_state.predictedDisplayTime % frame_state.predictedDisplayPeriod; + display_time_period = frame_state.predictedDisplayPeriod; + std::shared_lock lock(decoder_mutex); if (decoders.empty() or not frame_state.shouldRender) { diff --git a/client/scenes/stream.h b/client/scenes/stream.h index d0bd39f8..5e054f2a 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -80,7 +80,9 @@ private: std::thread network_thread; std::mutex local_floor_mutex; xr::space local_floor; - std::atomic tracking_prediction_offset; + std::atomic tracking_prediction_offset = 0; + std::atomic display_time_phase = 0; + std::atomic display_time_period = 0; std::optional tracking_thread; std::shared_mutex decoder_mutex; diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index 9c961075..316fe6a5 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -193,8 +193,9 @@ void scenes::stream::tracking() timer t(instance); XrDuration prediction = std::min(tracking_prediction_offset, 80'000'000); + auto period = std::max(display_time_period.load(), 1'000'000); // 1 or 2 samples - for (XrDuration Δt = 0; Δt <= prediction; Δt += std::max(1, prediction)) + for (XrDuration Δt = 0; Δt <= prediction + period / 2; Δt += period) { from_headset::hand_tracking hands{}; from_headset::fb_face2 fb_face2{}; @@ -264,6 +265,10 @@ 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; } #ifdef __ANDROID__ diff --git a/server/driver/history.h b/server/driver/history.h index dbeeaf09..b60d4e1c 100644 --- a/server/driver/history.h +++ b/server/driver/history.h @@ -61,10 +61,19 @@ protected: return; } - // Only keep one predicted value - // It should be the last one - if (not data.empty() and t != produced and data.back().at_timestamp_ns != data.back().produced_timestamp) - data.pop_back(); + // Discard outdated predictions + if (t != produced) + { + for (auto it = data.begin(); it != data.end();) + { + if (it->at_timestamp_ns == it->produced_timestamp // not a prediction + or it->produced_timestamp >= produced // recent prediction + or it->at_timestamp_ns > t + 1'000'000) // we don't have far enough data yet + ++it; + else + it = data.erase(it); + } + } // Insert the new sample auto it = std::lower_bound(data.begin(), data.end(), t, [](TimedData & sample, uint64_t t) { return sample.at_timestamp_ns < t; });