From 6c73d5f2cc033e7b6345c95dd1e28affa1e399bc Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Thu, 6 Feb 2025 18:11:16 +0100 Subject: [PATCH] Offload on_feedback procdessing --- server/driver/wivrn_pacer.cpp | 50 +++++++++++++++++++++++++++-------- server/driver/wivrn_pacer.h | 13 ++++++--- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/server/driver/wivrn_pacer.cpp b/server/driver/wivrn_pacer.cpp index f09d32f4..f75bfa9d 100644 --- a/server/driver/wivrn_pacer.cpp +++ b/server/driver/wivrn_pacer.cpp @@ -41,6 +41,42 @@ static T lerp_mod(T a, T b, double t, T mod) return T(std::lerp(a, b, t)) % mod; } +wivrn_pacer::wivrn_pacer(uint64_t frame_duration) : + frame_duration_ns(frame_duration), + frame_times(5000), + frame_times_compute(frame_times), + worker([this](std::stop_token t) { + std::vector samples; + while (not t.stop_requested()) + { + samples.clear(); + int64_t res = 0; + { + std::unique_lock lock(compute_mutex); + compute_cv.wait(lock); + for (const auto & time: frame_times_compute) + { + if (time.decoded > time.present) + samples.push_back(time.decoded - time.present); + } + auto it = samples.begin() + (samples.size() * 995) / 1000; + + std::ranges::nth_element(samples, it); + res = *it + 1'000'000; + } + + std::unique_lock lock(mutex); + safe_present_to_decoded_ns = res; + } + }) +{} + +wivrn_pacer::~wivrn_pacer() +{ + worker.request_stop(); + compute_cv.notify_all(); +} + void wivrn_pacer::set_frame_duration(uint64_t frame_duration_ns) { std::lock_guard lock(mutex); @@ -104,17 +140,9 @@ void wivrn_pacer::on_feedback(const wivrn::from_headset::feedback & feedback, co { if (feedback.stream_index % 100 == 0) { - std::vector samples; - samples.reserve(frame_times.size()); - for (const auto & time: frame_times) - { - if (time.decoded > time.present) - samples.push_back(time.decoded - time.present); - } - auto it = samples.begin() + (samples.size() * 995) / 1000; - - std::ranges::nth_element(samples, it); - safe_present_to_decoded_ns = *it + 1'000'000; + std::unique_lock lock(compute_mutex); + std::swap(frame_times, frame_times_compute); + compute_cv.notify_all(); } client_render_phase_ns = lerp_mod(client_render_phase_ns, offset.from_headset(feedback.blitted) % frame_duration_ns, 0.1, frame_duration_ns); diff --git a/server/driver/wivrn_pacer.h b/server/driver/wivrn_pacer.h index 37f8c353..de24ecd2 100644 --- a/server/driver/wivrn_pacer.h +++ b/server/driver/wivrn_pacer.h @@ -21,9 +21,11 @@ #include "wivrn_packets.h" +#include #include #include
#include +#include #include namespace wivrn @@ -63,13 +65,16 @@ private: }; std::vector frame_times; + std::mutex compute_mutex; + std::condition_variable compute_cv; + std::vector frame_times_compute; + std::jthread worker; + std::array in_flight_frames; public: - wivrn_pacer(uint64_t frame_duration) : - frame_duration_ns(frame_duration), - frame_times(5000) - {} + wivrn_pacer(uint64_t frame_duration); + ~wivrn_pacer(); void set_frame_duration(uint64_t frame_duration);