Pacer rewrite
This commit is contained in:
@@ -468,8 +468,9 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
|
||||
}
|
||||
|
||||
auto & view_info = cn->psc.view_info;
|
||||
view_info.display_time = cn->cnx->get_offset().to_headset(desired_present_time_ns);
|
||||
view_info.foveation = cn->cnx->get_foveation_parameters();
|
||||
auto info = cn->pacer.present_to_info(desired_present_time_ns);
|
||||
view_info.display_time = cn->cnx->get_offset().to_headset(info.predicted_display_time);
|
||||
for (int eye = 0; eye < 2; ++eye)
|
||||
{
|
||||
const auto & slot = cn->c->base.slot;
|
||||
@@ -491,7 +492,7 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
|
||||
}
|
||||
// set bits to 1 for index 1..num encoder threads + 1
|
||||
cn->psc.status = (1 << (cn->encoder_threads.size() + 1)) - 2;
|
||||
cn->psc.frame_index = cn->current_frame_id;
|
||||
cn->psc.frame_index = info.frame_id;
|
||||
cn->psc.status.notify_all();
|
||||
|
||||
return VK_SUCCESS;
|
||||
@@ -531,11 +532,11 @@ static void comp_wivrn_calc_frame_pacing(struct comp_target * ct,
|
||||
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct;
|
||||
|
||||
cn->pacer.predict(
|
||||
*out_frame_id,
|
||||
*out_wake_up_time_ns,
|
||||
*out_desired_present_time_ns,
|
||||
*out_present_slop_ns,
|
||||
*out_predicted_display_time_ns);
|
||||
*out_frame_id = cn->current_frame_id++;
|
||||
}
|
||||
|
||||
static void comp_wivrn_mark_timing_point(struct comp_target * ct,
|
||||
@@ -544,7 +545,6 @@ static void comp_wivrn_mark_timing_point(struct comp_target * ct,
|
||||
uint64_t when_ns)
|
||||
{
|
||||
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct;
|
||||
assert(frame_id + 1 == cn->current_frame_id);
|
||||
|
||||
cn->pacer.mark_timing_point(point, frame_id, when_ns);
|
||||
|
||||
|
||||
@@ -36,108 +36,90 @@ void wivrn_pacer::set_stream_count(size_t count)
|
||||
}
|
||||
|
||||
void wivrn_pacer::predict(
|
||||
int64_t & frame_id,
|
||||
uint64_t & out_wake_up_time_ns,
|
||||
uint64_t & out_desired_present_time_ns,
|
||||
uint64_t & out_present_slop_ns,
|
||||
uint64_t & out_predicted_display_time_ns)
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
frame_id = this->frame_id++;
|
||||
auto now = os_monotonic_get_ns();
|
||||
|
||||
next_frame_ns += frame_duration_ns;
|
||||
uint64_t predicted_client_render = last_ns + frame_duration_ns;
|
||||
// snap to phase
|
||||
predicted_client_render = (predicted_client_render / frame_duration_ns) * frame_duration_ns + client_render_phase_ns;
|
||||
|
||||
if (next_frame_ns < now)
|
||||
next_frame_ns = now;
|
||||
if (now + mean_wake_up_to_present_ns + safe_present_to_decoded_ns > predicted_client_render)
|
||||
predicted_client_render += frame_duration_ns * ((now + mean_wake_up_to_present_ns + safe_present_to_decoded_ns - predicted_client_render) / frame_duration_ns);
|
||||
|
||||
out_predicted_display_time_ns = predicted_client_render + mean_render_to_display_ns;
|
||||
out_desired_present_time_ns = predicted_client_render - safe_present_to_decoded_ns;
|
||||
out_wake_up_time_ns = out_desired_present_time_ns - mean_wake_up_to_present_ns;
|
||||
|
||||
last_ns = predicted_client_render;
|
||||
|
||||
in_flight_frames[frame_id % in_flight_frames.size()] = {
|
||||
.frame_id = frame_id,
|
||||
.present_ns = out_desired_present_time_ns,
|
||||
.predicted_display_time = out_predicted_display_time_ns,
|
||||
};
|
||||
|
||||
out_wake_up_time_ns = next_frame_ns;
|
||||
out_desired_present_time_ns = out_wake_up_time_ns + mean_wake_up_to_present_ns;
|
||||
out_present_slop_ns = 0;
|
||||
out_predicted_display_time_ns = out_desired_present_time_ns + mean_present_to_display_ns;
|
||||
}
|
||||
|
||||
void wivrn_pacer::on_feedback(const xrt::drivers::wivrn::from_headset::feedback & feedback, const clock_offset & offset)
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
if (feedback.stream_index >= streams.size())
|
||||
if (feedback.stream_index >= streams.size() or feedback.times_displayed > 1)
|
||||
return;
|
||||
|
||||
auto & last = streams[feedback.stream_index].last_feedback;
|
||||
auto & when = in_flight_frames[feedback.frame_index % in_flight_frames.size()];
|
||||
if (when.frame_id != feedback.frame_index)
|
||||
return;
|
||||
|
||||
if (feedback.stream_index == 0 and
|
||||
last.displayed and
|
||||
feedback.displayed and
|
||||
feedback.frame_index == last.frame_index + 1)
|
||||
auto & stream = streams[feedback.stream_index];
|
||||
if (stream.times.size() < num_wait_times)
|
||||
stream.times.push_back(offset.from_headset(feedback.received_from_decoder) - when.present_ns);
|
||||
else
|
||||
{
|
||||
frame_duration_ns = std::lerp(frame_duration_ns, (feedback.displayed - last.displayed), 0.1);
|
||||
stream.times[stream.next_times_index] = offset.from_headset(feedback.received_from_decoder) - when.present_ns;
|
||||
stream.next_times_index = (stream.next_times_index + 1) % num_wait_times;
|
||||
}
|
||||
|
||||
auto blitted = feedback.blitted;
|
||||
if (last.blitted and feedback.received_from_decoder and not feedback.blitted)
|
||||
if (feedback.stream_index == 0)
|
||||
{
|
||||
// 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));
|
||||
}
|
||||
|
||||
last = feedback;
|
||||
|
||||
if (blitted and feedback.times_displayed <= 1)
|
||||
{
|
||||
auto & stream = streams[feedback.stream_index];
|
||||
if (stream.times.size() < num_wait_times)
|
||||
stream.times.push_back(blitted - feedback.received_from_decoder);
|
||||
else
|
||||
uint64_t safe_time = 0;
|
||||
for (const auto & stream: streams)
|
||||
{
|
||||
stream.times[stream.next_times_index] = blitted - feedback.received_from_decoder;
|
||||
stream.next_times_index = (stream.next_times_index + 1) % num_wait_times;
|
||||
}
|
||||
|
||||
if (feedback.stream_index == 0)
|
||||
{
|
||||
bool wait_more = false;
|
||||
bool wait_less = true;
|
||||
for (const auto & stream: streams)
|
||||
if (stream.times.size() >= min_wait_times)
|
||||
{
|
||||
if (stream.times.size() >= min_wait_times)
|
||||
double mean_time = 0;
|
||||
double time_variance = 0;
|
||||
|
||||
// https://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods
|
||||
for (size_t i = 0; i < stream.times.size(); ++i)
|
||||
{
|
||||
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;
|
||||
int64_t t = stream.times[i];
|
||||
double prev_mean = mean_time;
|
||||
mean_time += (t - mean_time) / (i + 1);
|
||||
time_variance += (t - prev_mean) * (t - mean_time);
|
||||
}
|
||||
|
||||
time_variance /= (stream.times.size() - 1);
|
||||
double time_std_dev = std::sqrt(time_variance);
|
||||
|
||||
safe_time = std::max<uint64_t>(safe_time, mean_time + 3 * 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 (safe_time > 0 and safe_time < 100'000'000)
|
||||
safe_present_to_decoded_ns = std::lerp(safe_present_to_decoded_ns, safe_time, 0.1);
|
||||
|
||||
client_render_phase_ns = std::lerp(client_render_phase_ns, offset.from_headset(feedback.blitted) % frame_duration_ns, 0.1);
|
||||
}
|
||||
|
||||
if (feedback.displayed)
|
||||
{
|
||||
auto & when = in_flight_frames[feedback.frame_index % in_flight_frames.size()];
|
||||
if (when.frame_id == feedback.frame_index)
|
||||
{
|
||||
auto displayed = offset.from_headset(feedback.displayed);
|
||||
if (displayed > when.present_ns)
|
||||
mean_present_to_display_ns = std::lerp(mean_present_to_display_ns, displayed - when.present_ns, 0.1);
|
||||
when.frame_id = 0;
|
||||
}
|
||||
}
|
||||
mean_render_to_display_ns = std::lerp(mean_render_to_display_ns, feedback.displayed - feedback.blitted, 0.1);
|
||||
}
|
||||
void wivrn_pacer::mark_timing_point(
|
||||
comp_target_timing_point point,
|
||||
@@ -163,10 +145,21 @@ void wivrn_pacer::mark_timing_point(
|
||||
//! Just after submitting work to the GPU.
|
||||
case COMP_TARGET_TIMING_POINT_SUBMIT_END:
|
||||
mean_wake_up_to_present_ns = std::lerp(mean_wake_up_to_present_ns, when_ns - last_wake_up_ns, 0.1);
|
||||
in_flight_frames[frame_id % in_flight_frames.size()] = {.frame_id = uint64_t(frame_id), .present_ns = when_ns};
|
||||
}
|
||||
}
|
||||
|
||||
wivrn_pacer::frame_info wivrn_pacer::present_to_info(uint64_t present)
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
for (const auto & info: in_flight_frames)
|
||||
{
|
||||
if (info.present_ns == present)
|
||||
return info;
|
||||
}
|
||||
assert(false);
|
||||
return {};
|
||||
}
|
||||
|
||||
void wivrn_pacer::reset()
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
|
||||
+21
-14
@@ -30,13 +30,25 @@ struct clock_offset;
|
||||
|
||||
class wivrn_pacer
|
||||
{
|
||||
public:
|
||||
const uint64_t frame_duration_ns;
|
||||
struct frame_info
|
||||
{
|
||||
int64_t frame_id;
|
||||
uint64_t present_ns;
|
||||
uint64_t predicted_display_time;
|
||||
};
|
||||
|
||||
private:
|
||||
std::mutex mutex;
|
||||
uint64_t last_ns = 0;
|
||||
int64_t frame_id = 0;
|
||||
|
||||
uint64_t next_frame_ns;
|
||||
uint64_t frame_duration_ns;
|
||||
uint64_t client_render_phase_ns = 0;
|
||||
|
||||
uint64_t mean_wake_up_to_present_ns;
|
||||
uint64_t mean_present_to_display_ns;
|
||||
uint64_t mean_wake_up_to_present_ns = 1'000'000;
|
||||
uint64_t safe_present_to_decoded_ns = 0;
|
||||
uint64_t mean_render_to_display_ns = 0;
|
||||
|
||||
uint64_t last_wake_up_ns = 0;
|
||||
|
||||
@@ -50,24 +62,17 @@ class wivrn_pacer
|
||||
};
|
||||
std::vector<stream_data> streams;
|
||||
|
||||
struct frame_history
|
||||
{
|
||||
uint64_t frame_id;
|
||||
uint64_t present_ns;
|
||||
};
|
||||
std::array<frame_history, 4> in_flight_frames;
|
||||
std::array<frame_info, 4> in_flight_frames;
|
||||
|
||||
public:
|
||||
wivrn_pacer(uint64_t frame_duration) :
|
||||
next_frame_ns(0),
|
||||
frame_duration_ns(frame_duration),
|
||||
mean_wake_up_to_present_ns(1'000'000),
|
||||
mean_present_to_display_ns(frame_duration)
|
||||
frame_duration_ns(frame_duration)
|
||||
{}
|
||||
|
||||
void set_stream_count(size_t count);
|
||||
|
||||
void predict(
|
||||
int64_t & out_frame_id,
|
||||
uint64_t & out_wake_up_time_ns,
|
||||
uint64_t & out_desired_present_time_ns,
|
||||
uint64_t & out_present_slop_ns,
|
||||
@@ -80,5 +85,7 @@ public:
|
||||
int64_t frame_id,
|
||||
uint64_t when_ns);
|
||||
|
||||
frame_info present_to_info(uint64_t present);
|
||||
|
||||
void reset();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user