From 11e4076026a4d9b88b1a9025b83cde14a83439dd Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 2 Mar 2025 18:54:48 +0100 Subject: [PATCH] WIP app pacer --- client/scenes/stream.cpp | 14 +- common/wivrn_packets.h | 2 +- ...main-allow-custom-pacing-app-factory.patch | 82 +++++++ server/CMakeLists.txt | 1 + server/driver/app_pacer.cpp | 231 ++++++++++++++++++ server/driver/app_pacer.h | 49 ++++ server/driver/wivrn_comp_target.cpp | 24 +- server/driver/wivrn_hmd.cpp | 1 - server/driver/wivrn_session.cpp | 52 +++- server/driver/wivrn_session.h | 2 + 10 files changed, 434 insertions(+), 24 deletions(-) create mode 100644 patches/monado/0013-c-main-allow-custom-pacing-app-factory.patch create mode 100644 server/driver/app_pacer.cpp create mode 100644 server/driver/app_pacer.h diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 74034875..c9d0bd83 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -27,6 +27,7 @@ #include "decoder/shard_accumulator.h" #include "hardware.h" #include "spdlog/spdlog.h" +#include "utils/contains.h" #include "utils/named_thread.h" #include "utils/ranges.h" #include "vk/pipeline.h" @@ -183,20 +184,15 @@ std::shared_ptr scenes::stream::create(std::unique_ptrinstance.has_extension(XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME)) { info.available_refresh_rates = self->session.get_refresh_rates(); - if (std::ranges::find(info.available_refresh_rates, config.preferred_refresh_rate) != info.available_refresh_rates.end()) + if (utils::contains(info.available_refresh_rates, config.preferred_refresh_rate)) info.preferred_refresh_rate = config.preferred_refresh_rate; - else - info.preferred_refresh_rate = self->session.get_current_refresh_rate(); - } - - if (info.preferred_refresh_rate == 0) - { - spdlog::warn("Unable to detect preferred refresh rate, using {}", guessed_fps); - info.preferred_refresh_rate = guessed_fps; } if (info.available_refresh_rates.empty()) + { spdlog::warn("Unable to detect refresh rates"); + info.available_refresh_rates = {guessed_fps}; + } info.hand_tracking = config.check_feature(feature::hand_tracking); info.eye_gaze = config.check_feature(feature::eye_gaze); diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index e788046b..b31f09ea 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -41,7 +41,7 @@ namespace wivrn // Default port for server to listen, both TCP and UDP static const int default_port = 9757; -static constexpr int protocol_revision = 1; +static constexpr int protocol_revision = 2; enum class device_id : uint8_t { diff --git a/patches/monado/0013-c-main-allow-custom-pacing-app-factory.patch b/patches/monado/0013-c-main-allow-custom-pacing-app-factory.patch new file mode 100644 index 00000000..a30f8edd --- /dev/null +++ b/patches/monado/0013-c-main-allow-custom-pacing-app-factory.patch @@ -0,0 +1,82 @@ +From edd3447b25387908acbe005f1cbee5f17bae0260 Mon Sep 17 00:00:00 2001 +From: Patrick Nicolas +Date: Thu, 27 Feb 2025 22:45:36 +0100 +Subject: [PATCH] c/main: allow custom pacing app factory + +--- + src/xrt/compositor/main/comp_compositor.c | 10 ++++++---- + src/xrt/compositor/main/comp_main_interface.h | 4 ++++ + src/xrt/targets/common/target_instance.c | 2 +- + 3 files changed, 11 insertions(+), 5 deletions(-) + +diff --git a/src/xrt/compositor/main/comp_compositor.c b/src/xrt/compositor/main/comp_compositor.c +index 26c341385..36e3297f7 100644 +--- a/src/xrt/compositor/main/comp_compositor.c ++++ b/src/xrt/compositor/main/comp_compositor.c +@@ -983,6 +983,7 @@ compositor_init_renderer(struct comp_compositor *c) + xrt_result_t + comp_main_create_system_compositor(struct xrt_device *xdev, + const struct comp_target_factory *ctf, ++ struct u_pacing_app_factory *upaf, + struct xrt_system_compositor **out_xsysc) + { + COMP_TRACE_MARKER(); +@@ -1183,10 +1184,11 @@ comp_main_create_system_compositor(struct xrt_device *xdev, + } + + // Standard app pacer. +- struct u_pacing_app_factory *upaf = NULL; +- xrt_result_t xret = u_pa_factory_create(&upaf); +- assert(xret == XRT_SUCCESS && upaf != NULL); +- (void)xret; ++ if (upaf == NULL) { ++ xrt_result_t xret = u_pa_factory_create(&upaf); ++ assert(xret == XRT_SUCCESS && upaf != NULL); ++ (void)xret; ++ } + + return comp_multi_create_system_compositor(&c->base.base, upaf, sys_info, !c->deferred_surface, out_xsysc); + } +diff --git a/src/xrt/compositor/main/comp_main_interface.h b/src/xrt/compositor/main/comp_main_interface.h +index 0f0678358..f60568b43 100644 +--- a/src/xrt/compositor/main/comp_main_interface.h ++++ b/src/xrt/compositor/main/comp_main_interface.h +@@ -15,6 +15,7 @@ + #include "xrt/xrt_compositor.h" + + struct comp_target_factory; ++struct u_pacing_app_factory; + + #ifdef __cplusplus + extern "C" { +@@ -31,11 +32,14 @@ extern "C" { + * @param xdev The head device + * @param ctf A compositor target factory to force the output device, must remain valid for the lifetime of the + * compositor. If NULL, factory is automatically selected ++ * @param upaf A pacing app factory to customize the application pacer, compositor takes ownership. ++ * If NULL, a default factory is used + * @param out_xsysc The output compositor + */ + xrt_result_t + comp_main_create_system_compositor(struct xrt_device *xdev, + const struct comp_target_factory *ctf, ++ struct u_pacing_app_factory *upaf, + struct xrt_system_compositor **out_xsysc); + + +diff --git a/src/xrt/targets/common/target_instance.c b/src/xrt/targets/common/target_instance.c +index 617561c37..e10ba8358 100644 +--- a/src/xrt/targets/common/target_instance.c ++++ b/src/xrt/targets/common/target_instance.c +@@ -109,7 +109,7 @@ t_instance_create_system(struct xrt_instance *xinst, + + #ifdef XRT_MODULE_COMPOSITOR_MAIN + if (xret == XRT_SUCCESS && xsysc == NULL) { +- xret = comp_main_create_system_compositor(head, NULL, &xsysc); ++ xret = comp_main_create_system_compositor(head, NULL, NULL, &xsysc); + } + #else + if (!use_null) { +-- +2.48.1 + diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index e8e71660..17734dc9 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -85,6 +85,7 @@ if (WIVRN_BUILD_SERVER) encoder/encoder_settings.cpp encoder/video_encoder.cpp + driver/app_pacer.cpp driver/clock_offset.cpp driver/configuration.cpp driver/wivrn_hmd.cpp diff --git a/server/driver/app_pacer.cpp b/server/driver/app_pacer.cpp new file mode 100644 index 00000000..c52dd225 --- /dev/null +++ b/server/driver/app_pacer.cpp @@ -0,0 +1,231 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "app_pacer.h" + +#include "util/u_time.h" +#include "utils/method.h" +#include +#include +#include +#include + +namespace wivrn +{ + +class app_pacer : public u_pacing_app +{ + pacing_app_factory & parent; + int64_t frame_id = 0; + int64_t last_display_time = 0; + int64_t period = 10'000'000; + + std::mutex mutex; // locks cpu/draw/gpu time + int64_t cpu_time = 0; + int64_t gpu_time = 0; + int64_t compositor_time = 0; + + struct frame + { + int64_t frame_id; + int64_t wake_up; + int64_t delivered; + }; + std::array frames{}; + + frame & get_frame(int64_t id) + { + assert(id >= 0); + return frames[id % frames.size()]; + } + +public: + using base = u_pacing_app; + + app_pacer(pacing_app_factory & parent) : + u_pacing_app{ + .predict = method_pointer<&app_pacer::predict>, + .mark_point = method_pointer<&app_pacer::mark_point>, + .mark_discarded = method_pointer<&app_pacer::mark_discarded>, + .mark_delivered = method_pointer<&app_pacer::mark_delivered>, + .mark_gpu_done = method_pointer<&app_pacer::mark_gpu_done>, + .latched = method_pointer<&app_pacer::latched>, + .retired = method_pointer<&app_pacer::retired>, + .info = method_pointer<&app_pacer::info>, + .destroy = method_pointer<&app_pacer::destroy>, + }, + parent(parent) + {} + + void predict(int64_t now_ns, + int64_t * out_frame_id, + int64_t * out_wake_up_time, + int64_t * out_predicted_display_time, + int64_t * out_predicted_display_period); + void mark_point(int64_t frame_id, enum u_timing_point point, int64_t when_ns); + void mark_discarded(int64_t frame_id, int64_t when_ns); + void mark_delivered(int64_t frame_id, int64_t when_ns, int64_t display_time_ns); + void mark_gpu_done(int64_t frame_id, int64_t when_ns); + void latched(int64_t frame_id, int64_t when_ns, int64_t system_frame_id) {} + void retired(int64_t frame_id, int64_t when_ns) {} + void info(int64_t predicted_display_time_ns, + int64_t predicted_display_period_ns, + int64_t extra_ns); + + int64_t get_app_time() + { + std::lock_guard lock(mutex); + return std::max(cpu_time, gpu_time); + } + + void destroy() + { + parent.remove_app(this); + delete this; + } +}; + +void app_pacer::predict(int64_t now_ns, + int64_t * out_frame_id, + int64_t * out_wake_up_time, + int64_t * out_predicted_display_time, + int64_t * out_predicted_display_period) +{ + *out_frame_id = ++frame_id; + get_frame(frame_id) = {.frame_id = frame_id}; + + // no need to lock, called in same thread as writes + auto min_ready = now_ns + cpu_time + gpu_time + compositor_time; + + if (cpu_time > period or gpu_time > period) + { + // We are limited by app time, don't wait + *out_wake_up_time = now_ns; + *out_predicted_display_period = period; // FIXME: may be more than one frame + *out_predicted_display_time = last_display_time; + while (*out_predicted_display_time < min_ready) + *out_predicted_display_time += period; + } + else + { + auto display_time = last_display_time + period; + + // Ensure display time is achievable + while (display_time < min_ready) + display_time += period; + + *out_predicted_display_time = display_time; + *out_predicted_display_period = period; + *out_wake_up_time = display_time - (cpu_time + gpu_time + compositor_time + U_TIME_1MS_IN_NS); + } +} + +void app_pacer::mark_point(int64_t frame_id, enum u_timing_point point, int64_t when_ns) +{ + auto & frame = get_frame(frame_id); + if (frame.frame_id != frame_id) + return; + switch (point) + { + case U_TIMING_POINT_WAKE_UP: + frame.wake_up = when_ns; + break; + case U_TIMING_POINT_BEGIN: + case U_TIMING_POINT_SUBMIT_BEGIN: + case U_TIMING_POINT_SUBMIT_END: + break; + } +} + +void app_pacer::mark_delivered(int64_t frame_id, int64_t when_ns, int64_t display_time_ns) +{ + auto & frame = get_frame(frame_id); + if (frame.frame_id != frame_id) + return; + frame.delivered = when_ns; +} + +void app_pacer::mark_discarded(int64_t frame_id, int64_t when_ns) +{ + auto & frame = get_frame(frame_id); + if (frame.frame_id != frame_id) + return; + frame.delivered = when_ns; +} + +void app_pacer::info(int64_t predicted_display_time_ns, + int64_t predicted_display_period_ns, + int64_t extra_ns) +{ + last_display_time = predicted_display_time_ns; + period = predicted_display_period_ns; + compositor_time = std::max(0, extra_ns); +} + +int64_t lerp0(int64_t a, int64_t b, double t) +{ + if (a == 0) + return b; + return std::lerp(a, b, t); +} + +void app_pacer::mark_gpu_done(int64_t frame_id, int64_t when_ns) +{ + auto & frame = get_frame(frame_id); + if (frame.frame_id != frame_id or not(frame.wake_up and frame.delivered)) + return; + + std::lock_guard lock(mutex); + cpu_time = lerp0(cpu_time, frame.delivered - frame.wake_up, 0.1); + gpu_time = lerp0(gpu_time, when_ns - frame.delivered, 0.1); +} + +pacing_app_factory::pacing_app_factory() : + u_pacing_app_factory{ + .create = method_pointer<&pacing_app_factory::create>, + .destroy = method_pointer<&pacing_app_factory::destroy>, + } +{ +} + +void pacing_app_factory::remove_app(app_pacer * app) +{ + std::lock_guard lock(mutex); + std::erase(app_pacers, app); +} + +xrt_result_t pacing_app_factory::create(struct u_pacing_app ** out_upa) +{ + std::lock_guard lock(mutex); + *out_upa = app_pacers.emplace_back(new app_pacer(*this)); + return XRT_SUCCESS; +} + +void pacing_app_factory::destroy() +{ +} + +int64_t pacing_app_factory::get_frame_time() +{ + std::lock_guard lock(mutex); + if (app_pacers.empty()) + return 1; + return std::ranges::max(std::ranges::transform_view(app_pacers, [](auto * pacer) { return pacer->get_app_time(); })); +} + +} // namespace wivrn diff --git a/server/driver/app_pacer.h b/server/driver/app_pacer.h new file mode 100644 index 00000000..5cd9f983 --- /dev/null +++ b/server/driver/app_pacer.h @@ -0,0 +1,49 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "util/u_pacing.h" + +#include +#include + +namespace wivrn +{ + +class wivrn_session; +class app_pacer; + +class pacing_app_factory : public u_pacing_app_factory +{ + friend class app_pacer; + std::mutex mutex; + std::vector app_pacers; + void remove_app(app_pacer *); + +public: + using base = u_pacing_app_factory; + + pacing_app_factory(); + xrt_result_t create(struct u_pacing_app ** out_upa); + void destroy(); + + int64_t get_frame_time(); +}; + +} // namespace wivrn diff --git a/server/driver/wivrn_comp_target.cpp b/server/driver/wivrn_comp_target.cpp index 9a9e3837..7a8cf387 100644 --- a/server/driver/wivrn_comp_target.cpp +++ b/server/driver/wivrn_comp_target.cpp @@ -75,6 +75,13 @@ static inline struct vk_bundle * get_vk(struct wivrn_comp_target * cn) return &cn->c->base.vk; } +static float get_default_rate(const from_headset::headset_info_packet & info) +{ + if (info.preferred_refresh_rate) + return info.preferred_refresh_rate; + return info.available_refresh_rates.back(); +} + #ifdef XRT_FEATURE_RENDERDOC static auto renderdoc() { @@ -721,16 +728,8 @@ static xrt_result_t comp_wivrn_get_refresh_rates(struct comp_target * ct, uint32 { struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; const auto & rates = cn->cnx.get_info().available_refresh_rates; - if (rates.empty()) - { - *count = 1; - *refresh_rates_hz = cn->cnx.get_info().preferred_refresh_rate; - } - else - { - *count = std::min(rates.size(), XRT_MAX_SUPPORTED_REFRESH_RATES); - std::copy_n(rates.begin(), *count, refresh_rates_hz); - } + *count = std::min(rates.size(), XRT_MAX_SUPPORTED_REFRESH_RATES); + std::copy_n(rates.begin(), *count, refresh_rates_hz); return XRT_SUCCESS; } @@ -745,7 +744,7 @@ static xrt_result_t comp_wivrn_request_refresh_rate(struct comp_target * ct, flo { struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; if (refresh_rate_hz == 0.0f) - refresh_rate_hz = cn->cnx.get_info().preferred_refresh_rate; + refresh_rate_hz = get_default_rate(cn->cnx.get_info()); cn->cnx.send_control(to_headset::refresh_rate_change{.fps = refresh_rate_hz}); return XRT_SUCCESS; @@ -852,9 +851,10 @@ wivrn_comp_target::wivrn_comp_target(wivrn::wivrn_session & cnx, struct comp_com .request_refresh_rate = comp_wivrn_request_refresh_rate, .destroy = comp_wivrn_destroy, }, - desc{.fps = cnx.get_info().preferred_refresh_rate}, + desc{.fps = get_default_rate(cnx.get_info())}, pacer(U_TIME_1S_IN_NS / desc.fps), cnx(cnx) { + c->frame_interval_ns = U_TIME_1S_IN_NS / desc.fps; } } // namespace wivrn diff --git a/server/driver/wivrn_hmd.cpp b/server/driver/wivrn_hmd.cpp index 80177e8b..29e024fb 100644 --- a/server/driver/wivrn_hmd.cpp +++ b/server/driver/wivrn_hmd.cpp @@ -226,7 +226,6 @@ wivrn_hmd::wivrn_hmd(wivrn::wivrn_session * cnx, hmd->distortion.preferred = XRT_DISTORTION_MODEL_NONE; hmd->screens[0].w_pixels = eye_width * 2; hmd->screens[0].h_pixels = eye_height; - hmd->screens[0].nominal_frame_interval_ns = 1000000000 / info.preferred_refresh_rate; // Left hmd->views[0].display.w_pixels = eye_width; diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index d19c99d4..c81acdc3 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -20,6 +20,7 @@ #include "wivrn_session.h" #include "accept_connection.h" +#include "driver/app_pacer.h" #include "main/comp_compositor.h" #include "main/comp_main_interface.h" #include "main/comp_target.h" @@ -269,7 +270,7 @@ xrt_result_t wivrn::wivrn_session::create_session(std::unique_ptrget_info()); wivrn_comp_target_factory ctf(*self); - auto xret = comp_main_create_system_compositor(&self->hmd, &ctf, out_xsysc); + auto xret = comp_main_create_system_compositor(&self->hmd, &ctf, &self->app_pacers, out_xsysc); if (xret != XRT_SUCCESS) { U_LOG_E("Failed to create system compositor"); @@ -539,14 +540,63 @@ void wivrn_session::operator()(to_monado::set_bitrate && data) comp_target->set_bitrate(data.bitrate_bps); } +struct refresh_rate_adjuster +{ + std::chrono::seconds period{10}; + std::chrono::steady_clock::time_point next = std::chrono::steady_clock::now() + period; + bool enabled; + pacing_app_factory & pacers; + const from_headset::headset_info_packet & info; + float last = 0; + + refresh_rate_adjuster(const from_headset::headset_info_packet & info, pacing_app_factory & pacers) : + enabled(info.preferred_refresh_rate == 0 and info.available_refresh_rates.size() > 1), + pacers(pacers), + info(info) + { + } + + void adjust(wivrn_connection & cnx) + { + if (not enabled or std::chrono::steady_clock::now() < next) + return; + + // Maximum refresh rate the application can reach + auto app_rate = float(U_TIME_1S_IN_NS) / pacers.get_frame_time(); + app_rate *= 0.95; // add some margin + // Get the highest rate reachable by the application + // If none can be reached, set it to the maximum + auto requested = info.available_refresh_rates.back(); + for (auto rate: info.available_refresh_rates) + { + if (rate < app_rate) + requested = rate; + } + if (requested != last) + { + U_LOG_I("requesting refresh rate: %.0f (app rate %.1f)", requested, app_rate); + cnx.send_control(to_headset::refresh_rate_change{.fps = requested}); + last = requested; + } + next += period; + } +}; + void wivrn_session::run(std::stop_token stop) { + std::chrono::seconds refresh_rate_adjust_period(10); + auto refresh_rate_adjust = std::chrono::steady_clock::now() + refresh_rate_adjust_period; + const auto & rates = get_info().available_refresh_rates; + bool adjust_refresh = get_info().preferred_refresh_rate == 0 and rates.size() > 1; while (not stop.stop_requested()) { try { offset_est.request_sample(*connection); tracking_control.send(*connection); + if (adjust_refresh and std::chrono::steady_clock::now() > refresh_rate_adjust) + { + } connection->poll(*this, 20); } catch (const std::exception & e) diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 43b3e0d9..95584f23 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -20,6 +20,7 @@ #pragma once #include "clock_offset.h" +#include "driver/app_pacer.h" #include "wivrn_connection.h" #include "wivrn_controller.h" #include "wivrn_hmd.h" @@ -89,6 +90,7 @@ class wivrn_session : public xrt_system_devices { friend wivrn_comp_target_factory; std::unique_ptr connection; + pacing_app_factory app_pacers; u_system & xrt_system; xrt_space_overseer * space_overseer;