From ad9300f8420dfd08b0cd5c5853dd57fa93698488 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sat, 14 Dec 2024 16:18:28 +0100 Subject: [PATCH] Implement refresh rate change --- CMakeLists.txt | 2 +- server/driver/wivrn_comp_target.cpp | 82 ++++++++++++++++++++++------- server/driver/wivrn_comp_target.h | 6 +-- server/driver/wivrn_pacer.cpp | 7 ++- server/driver/wivrn_pacer.h | 4 +- server/driver/wivrn_session.cpp | 17 +++--- server/driver/wivrn_session.h | 3 ++ 7 files changed, 87 insertions(+), 34 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index aecf6b8a..c5c2494e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -253,7 +253,7 @@ file(GLOB MONADO_PATCHES CONFIGURE_DEPENDS patches/monado/*) FetchContent_Declare(monado GIT_REPOSITORY https://gitlab.freedesktop.org/monado/monado # The flatpak manifest is updated automatically - GIT_TAG 59c34e17b4d9da228c84d94db552426b78a52508 + GIT_TAG e5f5461dff2d745b3abd19cd05785d815304b2c6 PATCH_COMMAND ${CMAKE_CURRENT_LIST_DIR}/patches/apply.sh ${MONADO_PATCHES} EXCLUDE_FROM_ALL ) diff --git a/server/driver/wivrn_comp_target.cpp b/server/driver/wivrn_comp_target.cpp index 914e1f0a..1ea0c9bb 100644 --- a/server/driver/wivrn_comp_target.cpp +++ b/server/driver/wivrn_comp_target.cpp @@ -34,6 +34,7 @@ #include #include "xrt/xrt_config_build.h" // IWYU pragma: keep +#include "xrt/xrt_session.h" #ifdef XRT_FEATURE_RENDERDOC #include "renderdoc_app.h" #endif @@ -712,6 +713,44 @@ static VkResult comp_wivrn_update_timings(struct comp_target * ct) static void comp_wivrn_set_title(struct comp_target * ct, const char * title) {} +static xrt_result_t comp_wivrn_get_refresh_rates(struct comp_target * ct, uint32_t * count, float * refresh_rates_hz) +{ + struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; + const auto & rates = cn->cnx.get_info().available_refresh_rates; + *count = std::min(rates.size(), XRT_MAX_SUPPORTED_REFRESH_RATES); + std::copy_n(rates.begin(), *count, refresh_rates_hz); + return XRT_SUCCESS; +} + +static xrt_result_t comp_wivrn_get_current_refresh_rate(struct comp_target * ct, float * refresh_rate_hz) +{ + struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; + *refresh_rate_hz = cn->desc.fps; + return XRT_SUCCESS; +} + +static xrt_result_t comp_wivrn_request_refresh_rate(struct comp_target * ct, float refresh_rate_hz) +{ + 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; + + auto from = cn->desc.fps; + cn->desc.fps = refresh_rate_hz; + cn->pacer.set_frame_duration(U_TIME_1S_IN_NS / refresh_rate_hz); + cn->cnx.send_control(decltype(cn->desc)(cn->desc)); + cn->cnx.push_event( + { + .display = { + .type = XRT_SESSION_EVENT_DISPLAY_REFRESH_RATE_CHANGE, + .from_display_refresh_rate_hz = from, + .to_display_refresh_rate_hz = refresh_rate_hz, + }, + }); + cn->reset_encoders(); + return XRT_SUCCESS; +} + wivrn_comp_target::~wivrn_comp_target() { if (wivrn_bundle) @@ -771,27 +810,30 @@ void wivrn_comp_target::render_dynamic_foveation(std::arrayfps = fps; - desc.fps = fps; - this->c = c; } } // namespace wivrn diff --git a/server/driver/wivrn_comp_target.h b/server/driver/wivrn_comp_target.h index 4d657e1e..1390d195 100644 --- a/server/driver/wivrn_comp_target.h +++ b/server/driver/wivrn_comp_target.h @@ -78,13 +78,12 @@ struct pseudo_swapchain struct wivrn_comp_target : public comp_target { + to_headset::video_stream_description desc{}; wivrn_pacer pacer; std::optional wivrn_bundle; vk::raii::CommandPool command_pool = nullptr; - float fps; - int64_t current_frame_id = 0; pseudo_swapchain psc; @@ -95,14 +94,13 @@ struct wivrn_comp_target : public comp_target static std::vector wanted_device_extensions; std::vector settings; - to_headset::video_stream_description desc{}; std::list encoder_threads; std::vector> encoders; wivrn::wivrn_session & cnx; std::unique_ptr foveation_renderer = nullptr; - wivrn_comp_target(wivrn::wivrn_session & cnx, struct comp_compositor * c, float fps); + wivrn_comp_target(wivrn::wivrn_session & cnx, struct comp_compositor * c); ~wivrn_comp_target(); void on_feedback(const from_headset::feedback &, const clock_offset &); diff --git a/server/driver/wivrn_pacer.cpp b/server/driver/wivrn_pacer.cpp index 1787ce67..b5377ac2 100644 --- a/server/driver/wivrn_pacer.cpp +++ b/server/driver/wivrn_pacer.cpp @@ -41,6 +41,12 @@ static T lerp_mod(T a, T b, double t, T mod) return T(std::lerp(a, b, t)) % mod; } +void wivrn_pacer::set_frame_duration(uint64_t frame_duration_ns) +{ + std::lock_guard lock(mutex); + this->frame_duration_ns = frame_duration_ns; +} + void wivrn_pacer::predict( int64_t & frame_id, int64_t & out_wake_up_time_ns, @@ -160,6 +166,5 @@ void wivrn_pacer::reset() { std::lock_guard lock(mutex); std::ranges::fill(frame_times, frame_time{}); - in_flight_frames = {}; } } // namespace wivrn diff --git a/server/driver/wivrn_pacer.h b/server/driver/wivrn_pacer.h index 846667e8..37f8c353 100644 --- a/server/driver/wivrn_pacer.h +++ b/server/driver/wivrn_pacer.h @@ -34,7 +34,6 @@ struct clock_offset; class wivrn_pacer { public: - const uint64_t frame_duration_ns; struct frame_info { int64_t frame_id; @@ -44,6 +43,7 @@ public: private: std::mutex mutex; + uint64_t frame_duration_ns; int64_t last_ns = 0; int64_t frame_id = 0; @@ -71,6 +71,8 @@ public: frame_times(5000) {} + void set_frame_duration(uint64_t frame_duration); + void predict( int64_t & out_frame_id, int64_t & out_wake_up_time_ns, diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 30c98ac5..c0c2346d 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -56,9 +56,8 @@ namespace wivrn struct wivrn_comp_target_factory : public comp_target_factory { wivrn_session & session; - float fps; - wivrn_comp_target_factory(wivrn_session & session, float fps) : + wivrn_comp_target_factory(wivrn_session & session) : comp_target_factory{ .name = "WiVRn", .identifier = "wivrn", @@ -71,8 +70,7 @@ struct wivrn_comp_target_factory : public comp_target_factory .optional_device_extension_count = wivrn_comp_target::wanted_device_extensions.size(), .detect = wivrn_comp_target_factory::detect, .create_target = wivrn_comp_target_factory::create_target}, - session(session), - fps(fps) + session(session) { } @@ -84,7 +82,7 @@ struct wivrn_comp_target_factory : public comp_target_factory static bool create_target(const struct comp_target_factory * ctf, struct comp_compositor * c, struct comp_target ** out_ct) { auto self = (wivrn_comp_target_factory *)ctf; - self->session.comp_target = new wivrn_comp_target(self->session, c, self->fps); + self->session.comp_target = new wivrn_comp_target(self->session, c); *out_ct = self->session.comp_target; return true; } @@ -248,7 +246,7 @@ xrt_result_t wivrn::wivrn_session::create_session(std::unique_ptrget_info()); - wivrn_comp_target_factory ctf(*self, self->get_info().preferred_refresh_rate); + wivrn_comp_target_factory ctf(*self); auto xret = comp_main_create_system_compositor(&self->hmd, &ctf, out_xsysc); if (xret != XRT_SUCCESS) { @@ -442,6 +440,11 @@ void wivrn_session::run(std::stop_token stop) } } +xrt_result_t wivrn_session::push_event(const xrt_session_event & event) +{ + return xrt_session_event_sink_push(&xrt_system.broadcast, &event); +} + std::array wivrn_session::set_foveated_size(uint32_t width, uint32_t height) { auto p = hmd.set_foveated_size(width, height); @@ -497,7 +500,7 @@ void wivrn_session::reconnect() .focused = false, }, }; - auto result = xrt_session_event_sink_push(&xrt_system.broadcast, &event); + auto result = push_event(event); if (result != XRT_SUCCESS) { U_LOG_W("Failed to notify session state change"); diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 6756f059..fa154574 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -35,6 +35,7 @@ struct u_system; struct xrt_space_overseer; struct xrt_system_compositor; +union xrt_session_event; namespace wivrn { @@ -166,6 +167,8 @@ public: connection->send_control(std::forward(packet)); } + xrt_result_t push_event(const xrt_session_event &); + std::array set_foveated_size(uint32_t width, uint32_t height); std::array get_foveation_parameters(); bool apply_dynamic_foveation();