Implement refresh rate change

This commit is contained in:
Patrick Nicolas
2025-01-07 20:07:06 +01:00
parent c545db61d7
commit ad9300f842
7 changed files with 87 additions and 34 deletions
+1 -1
View File
@@ -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
)
+62 -20
View File
@@ -34,6 +34,7 @@
#include <vulkan/vulkan_handles.hpp>
#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<uint32_t>(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::array<to_headset::foveatio
}
}
wivrn_comp_target::wivrn_comp_target(wivrn::wivrn_session & cnx, struct comp_compositor * c, float fps) :
comp_target{},
pacer(U_TIME_1S_IN_NS / fps),
wivrn_comp_target::wivrn_comp_target(wivrn::wivrn_session & cnx, struct comp_compositor * c) :
comp_target{
.c = c,
.init_pre_vulkan = comp_wivrn_init_pre_vulkan,
.init_post_vulkan = comp_wivrn_init_post_vulkan,
.check_ready = comp_wivrn_check_ready,
.create_images = comp_wivrn_create_images,
.has_images = comp_wivrn_has_images,
.acquire = comp_wivrn_acquire,
.present = comp_wivrn_present,
.flush = comp_wivrn_flush,
.calc_frame_pacing = comp_wivrn_calc_frame_pacing,
.mark_timing_point = comp_wivrn_mark_timing_point,
.update_timings = comp_wivrn_update_timings,
.info_gpu = comp_wivrn_info_gpu,
.set_title = comp_wivrn_set_title,
.get_refresh_rates = comp_wivrn_get_refresh_rates,
.get_current_refresh_rate = comp_wivrn_get_current_refresh_rate,
.request_refresh_rate = comp_wivrn_request_refresh_rate,
.destroy = comp_wivrn_destroy,
},
desc{.fps = cnx.get_info().preferred_refresh_rate},
pacer(U_TIME_1S_IN_NS / desc.fps),
cnx(cnx)
{
check_ready = comp_wivrn_check_ready;
create_images = comp_wivrn_create_images;
has_images = comp_wivrn_has_images;
acquire = comp_wivrn_acquire;
present = comp_wivrn_present;
calc_frame_pacing = comp_wivrn_calc_frame_pacing;
mark_timing_point = comp_wivrn_mark_timing_point;
update_timings = comp_wivrn_update_timings;
info_gpu = comp_wivrn_info_gpu;
destroy = comp_wivrn_destroy;
init_pre_vulkan = comp_wivrn_init_pre_vulkan;
init_post_vulkan = comp_wivrn_init_post_vulkan;
set_title = comp_wivrn_set_title;
flush = comp_wivrn_flush;
this->fps = fps;
desc.fps = fps;
this->c = c;
}
} // namespace wivrn
+2 -4
View File
@@ -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_vk_bundle> 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<const char *> wanted_device_extensions;
std::vector<encoder_settings> settings;
to_headset::video_stream_description desc{};
std::list<std::jthread> encoder_threads;
std::vector<std::shared_ptr<video_encoder>> encoders;
wivrn::wivrn_session & cnx;
std::unique_ptr<wivrn_foveation_renderer> 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 &);
+6 -1
View File
@@ -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
+3 -1
View File
@@ -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,
+10 -7
View File
@@ -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_ptr<wivrn_connecti
send_to_main(self->get_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<to_headset::foveation_parameter, 2> 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");
+3
View File
@@ -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<T>(packet));
}
xrt_result_t push_event(const xrt_session_event &);
std::array<to_headset::foveation_parameter, 2> set_foveated_size(uint32_t width, uint32_t height);
std::array<to_headset::foveation_parameter, 2> get_foveation_parameters();
bool apply_dynamic_foveation();