WIP app pacer

This commit is contained in:
Patrick Nicolas
2025-03-19 17:43:37 +01:00
parent dcc808981f
commit 11e4076026
10 changed files with 434 additions and 24 deletions
+1
View File
@@ -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
+231
View File
@@ -0,0 +1,231 @@
/*
* WiVRn VR streaming
* Copyright (C) 2025 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "app_pacer.h"
#include "util/u_time.h"
#include "utils/method.h"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <ranges>
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<frame, 16> 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<int64_t>(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
+49
View File
@@ -0,0 +1,49 @@
/*
* WiVRn VR streaming
* Copyright (C) 2025 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "util/u_pacing.h"
#include <mutex>
#include <vector>
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_pacer *> 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
+12 -12
View File
@@ -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<uint32_t>(rates.size(), XRT_MAX_SUPPORTED_REFRESH_RATES);
std::copy_n(rates.begin(), *count, refresh_rates_hz);
}
*count = std::min<uint32_t>(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
-1
View File
@@ -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;
+51 -1
View File
@@ -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_ptr<wivrn_connecti
send_to_main(self->get_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)
+2
View File
@@ -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<wivrn_connection> connection;
pacing_app_factory app_pacers;
u_system & xrt_system;
xrt_space_overseer * space_overseer;