Tweak pacer for faster convergence

This commit is contained in:
Patrick Nicolas
2024-04-28 20:43:22 +02:00
parent 62d74db3dc
commit b526c98683
2 changed files with 44 additions and 24 deletions
+43 -23
View File
@@ -23,9 +23,9 @@
#include <cmath>
// How many samples of wait time to store per decoder
const size_t num_wait_times = 1000;
const size_t num_wait_times = 100;
// How many samples of wait time are required to use them
const size_t min_wait_times = 100;
const size_t min_wait_times = 50;
void wivrn_pacer::set_stream_count(size_t count)
{
@@ -73,40 +73,60 @@ void wivrn_pacer::on_feedback(const xrt::drivers::wivrn::from_headset::feedback
{
frame_duration_ns = std::lerp(frame_duration_ns, (feedback.displayed - last.displayed) / offset.a, 0.1);
}
last = feedback;
if (feedback.blitted and feedback.times_displayed == 1)
auto blitted = feedback.blitted;
if (last.blitted and feedback.received_from_decoder and not feedback.blitted)
{
// We skipped a frame, certainly because it was too late
// estimate the moment it should have been displayed
blitted = last.blitted + frame_duration_ns * (int64_t(feedback.frame_index) - int64_t(last.frame_index));
}
if (blitted and feedback.times_displayed <= 1)
{
auto & stream = streams[feedback.stream_index];
if (stream.times.size() < num_wait_times)
stream.times.push_back(feedback.blitted - feedback.received_from_decoder);
stream.times.push_back(blitted - feedback.received_from_decoder);
else
{
stream.times[stream.next_times_index] = feedback.blitted - feedback.received_from_decoder;
stream.times[stream.next_times_index] = blitted - feedback.received_from_decoder;
stream.next_times_index = (stream.next_times_index + 1) % num_wait_times;
}
if (stream.times.size() >= min_wait_times)
if (feedback.stream_index == 0)
{
double mean_wait_time = 0;
double wait_time_variance = 0;
// https://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods
for (size_t i = 0; i < stream.times.size(); ++i)
bool wait_more = false;
bool wait_less = true;
for (const auto & stream: streams)
{
uint64_t t = stream.times[i];
uint64_t prev_mean = mean_wait_time;
mean_wait_time += (t - mean_wait_time) / (i + 1);
wait_time_variance += (t - prev_mean) * (t - mean_wait_time);
if (stream.times.size() >= min_wait_times)
{
double mean_wait_time = 0;
double wait_time_variance = 0;
// https://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods
for (size_t i = 0; i < stream.times.size(); ++i)
{
int64_t t = stream.times[i];
double prev_mean = mean_wait_time;
mean_wait_time += (t - mean_wait_time) / (i + 1);
wait_time_variance += (t - prev_mean) * (t - mean_wait_time);
}
wait_time_variance /= (stream.times.size() - 1);
double wait_time_std_dev = std::sqrt(wait_time_variance);
if (mean_wait_time < 4 * wait_time_std_dev)
wait_less = false;
if (mean_wait_time < 3 * wait_time_std_dev)
wait_more = true;
}
}
wait_time_variance /= (stream.times.size() - 1);
double wait_time_std_dev = std::sqrt(wait_time_variance);
if (mean_wait_time > 2 * wait_time_std_dev)
next_frame_ns += frame_duration_ns / 1000;
if (mean_wait_time < wait_time_std_dev)
if (wait_more)
next_frame_ns -= frame_duration_ns / 1000;
else if (wait_less)
next_frame_ns += frame_duration_ns / 1000;
}
}
if (feedback.displayed)
+1 -1
View File
@@ -45,7 +45,7 @@ class wivrn_pacer
{
// Last feedback for each encoder
xrt::drivers::wivrn::from_headset::feedback last_feedback;
std::vector<uint64_t> times;
std::vector<int64_t> times;
size_t next_times_index = 0;
};
std::vector<stream_data> streams;