Add pacer

This commit is contained in:
Patrick Nicolas
2024-02-16 22:02:57 +01:00
parent c3e2cd0cb8
commit 2cc0e6154e
5 changed files with 189 additions and 47 deletions
+1
View File
@@ -71,6 +71,7 @@ add_executable(wivrn-server
encoder/yuv_converter.cpp encoder/yuv_converter.cpp
driver/clock_offset.cpp driver/clock_offset.cpp
driver/wivrn_hmd.cpp driver/wivrn_hmd.cpp
driver/wivrn_pacer.cpp
driver/wivrn_comp_target.cpp driver/wivrn_comp_target.cpp
driver/wivrn_controller.cpp driver/wivrn_controller.cpp
driver/pose_list.cpp driver/pose_list.cpp
+11 -43
View File
@@ -20,7 +20,6 @@
#include "wivrn_comp_target.h" #include "wivrn_comp_target.h"
#include "main/comp_compositor.h" #include "main/comp_compositor.h"
#include "math/m_space.h" #include "math/m_space.h"
#include "util/u_pacing.h"
#include "encoder/video_encoder.h" #include "encoder/video_encoder.h"
#include "vk/vk_cmd_pool.h" #include "vk/vk_cmd_pool.h"
#include "xrt_cast.h" #include "xrt_cast.h"
@@ -131,6 +130,7 @@ static void create_encoders(wivrn_comp_target * cn, std::vector<encoder_settings
std::string name = "encoder " + std::to_string(group); std::string name = "encoder " + std::to_string(group);
os_thread_helper_name(&thread.thread, name.c_str()); os_thread_helper_name(&thread.thread, name.c_str());
} }
cn->pacer.set_stream_count(cn->encoders.size());
cn->cnx->send_control(desc); cn->cnx->send_control(desc);
} }
@@ -356,10 +356,6 @@ static void comp_wivrn_create_images(struct comp_target * ct, const struct comp_
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct;
uint64_t now_ns = os_monotonic_get_ns(); uint64_t now_ns = os_monotonic_get_ns();
if (cn->upc == NULL)
{
u_pc_fake_create(ct->c->settings.nominal_frame_interval_ns, now_ns, &cn->upc);
}
// Free old images. // Free old images.
destroy_images(cn); destroy_images(cn);
@@ -604,36 +600,12 @@ static void comp_wivrn_calc_frame_pacing(struct comp_target * ct,
{ {
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct;
int64_t frame_id /*= ++cn->current_frame_id*/; //-1; cn->pacer.predict(
uint64_t desired_present_time_ns /*= cn->next_frame_timestamp*/; *out_wake_up_time_ns,
uint64_t wake_up_time_ns /*= desired_present_time_ns - 5 * U_TIME_1MS_IN_NS*/; *out_desired_present_time_ns,
uint64_t present_slop_ns /*= U_TIME_HALF_MS_IN_NS*/; *out_present_slop_ns,
uint64_t predicted_display_time_ns /*= desired_present_time_ns + 5 * U_TIME_1MS_IN_NS*/; *out_predicted_display_time_ns);
*out_frame_id = cn->current_frame_id;
#if 1
uint64_t predicted_display_period_ns = U_TIME_1S_IN_NS / 60;
uint64_t min_display_period_ns = predicted_display_period_ns;
uint64_t now_ns = os_monotonic_get_ns();
u_pc_predict(cn->upc, //
now_ns, //
&frame_id, //
&wake_up_time_ns, //
&desired_present_time_ns, //
&present_slop_ns, //
&predicted_display_time_ns, //
&predicted_display_period_ns, //
&min_display_period_ns); //
cn->current_frame_id = frame_id;
#endif
*out_frame_id = frame_id;
*out_wake_up_time_ns = wake_up_time_ns;
*out_desired_present_time_ns = desired_present_time_ns;
*out_predicted_display_time_ns = predicted_display_time_ns;
*out_present_slop_ns = present_slop_ns;
} }
static void comp_wivrn_mark_timing_point(struct comp_target * ct, static void comp_wivrn_mark_timing_point(struct comp_target * ct,
@@ -644,22 +616,20 @@ static void comp_wivrn_mark_timing_point(struct comp_target * ct,
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct;
assert(frame_id == cn->current_frame_id); assert(frame_id == cn->current_frame_id);
cn->pacer.mark_timing_point(point, frame_id, when_ns);
switch (point) switch (point)
{ {
case COMP_TARGET_TIMING_POINT_WAKE_UP: case COMP_TARGET_TIMING_POINT_WAKE_UP:
cn->cnx->dump_time("wake_up", cn->frame_index + 1, when_ns); cn->cnx->dump_time("wake_up", cn->frame_index + 1, when_ns);
u_pc_mark_point(cn->upc, U_TIMING_POINT_WAKE_UP, cn->current_frame_id, when_ns);
break; break;
case COMP_TARGET_TIMING_POINT_BEGIN: case COMP_TARGET_TIMING_POINT_BEGIN:
cn->cnx->dump_time("begin", cn->frame_index + 1, when_ns); cn->cnx->dump_time("begin", cn->frame_index + 1, when_ns);
u_pc_mark_point(cn->upc, U_TIMING_POINT_BEGIN, cn->current_frame_id, when_ns);
break; break;
case COMP_TARGET_TIMING_POINT_SUBMIT_BEGIN: case COMP_TARGET_TIMING_POINT_SUBMIT_BEGIN:
u_pc_mark_point(cn->upc, U_TIMING_POINT_SUBMIT_BEGIN, cn->current_frame_id, when_ns);
break; break;
case COMP_TARGET_TIMING_POINT_SUBMIT_END: case COMP_TARGET_TIMING_POINT_SUBMIT_END:
cn->cnx->dump_time("submit", cn->frame_index + 1, when_ns); cn->cnx->dump_time("submit", cn->frame_index + 1, when_ns);
u_pc_mark_point(cn->upc, U_TIMING_POINT_SUBMIT_END, cn->current_frame_id, when_ns);
break; break;
default: default:
assert(false); assert(false);
@@ -689,14 +659,11 @@ static void comp_wivrn_destroy(struct comp_target * ct)
static void comp_wivrn_info_gpu(struct comp_target * ct, int64_t frame_id, uint64_t gpu_start_ns, uint64_t gpu_end_ns, uint64_t when_ns) static void comp_wivrn_info_gpu(struct comp_target * ct, int64_t frame_id, uint64_t gpu_start_ns, uint64_t gpu_end_ns, uint64_t when_ns)
{ {
COMP_TRACE_MARKER(); COMP_TRACE_MARKER();
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct;
u_pc_info_gpu(cn->upc, frame_id, gpu_start_ns, gpu_end_ns, when_ns);
} }
void wivrn_comp_target::on_feedback(const from_headset::feedback & feedback, const clock_offset & o) void wivrn_comp_target::on_feedback(const from_headset::feedback & feedback, const clock_offset & o)
{ {
pacer.on_feedback(feedback, o);
if (not feedback.sent_to_decoder) if (not feedback.sent_to_decoder)
{ {
if (encoders.size() < feedback.stream_index) if (encoders.size() < feedback.stream_index)
@@ -707,6 +674,7 @@ void wivrn_comp_target::on_feedback(const from_headset::feedback & feedback, con
wivrn_comp_target::wivrn_comp_target(std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx, struct comp_compositor * c, float fps) : wivrn_comp_target::wivrn_comp_target(std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx, struct comp_compositor * c, float fps) :
comp_target{}, comp_target{},
pacer(U_TIME_1S_IN_NS / fps),
cnx(cnx) cnx(cnx)
{ {
check_ready = comp_wivrn_check_ready; check_ready = comp_wivrn_check_ready;
+2 -4
View File
@@ -29,8 +29,7 @@
#include <list> #include <list>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "driver/wivrn_pacer.h"
struct u_pacing_compositor;
namespace xrt::drivers::wivrn namespace xrt::drivers::wivrn
{ {
@@ -54,8 +53,7 @@ struct pseudo_swapchain
struct wivrn_comp_target : public comp_target struct wivrn_comp_target : public comp_target
{ {
//! Compositor frame pacing helper wivrn_pacer pacer;
u_pacing_compositor * upc{};
vk_cmd_pool pool; vk_cmd_pool pool;
+109
View File
@@ -0,0 +1,109 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 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 "wivrn_pacer.h"
#include "driver/clock_offset.h"
#include "os/os_time.h"
#include <cmath>
void wivrn_pacer::set_stream_count(size_t count)
{
std::lock_guard lock(mutex);
last_feedback.resize(count);
}
void wivrn_pacer::predict(
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);
if (next_frame_ns == 0)
next_frame_ns = os_monotonic_get_ns();
next_frame_ns += frame_duration_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;
}
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 >= last_feedback.size())
return;
auto & last = last_feedback[feedback.stream_index];
if (feedback.stream_index == 0 and
last.displayed and
feedback.displayed and
feedback.frame_index == last.frame_index + 1)
{
frame_duration_ns = std::lerp(frame_duration_ns, (feedback.displayed - last.displayed) / offset.a, 0.1);
}
last = feedback;
bool same_frame = true;
uint64_t decoded_ns = 0;
for (const auto & f: last_feedback)
{
same_frame = same_frame and (f.frame_index == feedback.frame_index);
decoded_ns = std::max(decoded_ns, f.received_from_decoder);
}
if (same_frame and feedback.blitted)
{
mean_client_wait_ns = std::lerp(mean_client_wait_ns, feedback.blitted - decoded_ns, 0.1);
}
if (mean_client_wait_ns > frame_duration_ns / 2)
next_frame_ns += frame_duration_ns / 1000;
if (mean_client_wait_ns < frame_duration_ns / 4)
next_frame_ns -= frame_duration_ns / 1000;
}
void wivrn_pacer::mark_timing_point(
comp_target_timing_point point,
int64_t frame_id,
uint64_t when_ns)
{
std::lock_guard lock(mutex);
switch (point)
{
//! Woke up after sleeping in wait frame.
case COMP_TARGET_TIMING_POINT_WAKE_UP:
last_wake_up_ns = when_ns;
return;
//! Began CPU side work for GPU.
case COMP_TARGET_TIMING_POINT_BEGIN:
return;
//! Just before submitting work to the GPU.
case COMP_TARGET_TIMING_POINT_SUBMIT_BEGIN:
return;
//! 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);
}
}
+66
View File
@@ -0,0 +1,66 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 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 "wivrn_packets.h"
#include <cstdint>
#include <main/comp_target.h>
#include <mutex>
#include <vector>
struct clock_offset;
class wivrn_pacer
{
std::mutex mutex;
uint64_t next_frame_ns;
uint64_t frame_duration_ns;
uint64_t mean_client_wait_ns = 0;
uint64_t mean_wake_up_to_present_ns = 0;
uint64_t last_wake_up_ns = 0;
// Last feedback for each encoder
std::vector<xrt::drivers::wivrn::from_headset::feedback> last_feedback;
public:
wivrn_pacer(uint64_t frame_duration) :
next_frame_ns(0),
frame_duration_ns(frame_duration)
{}
void set_stream_count(size_t count);
void predict(
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);
void on_feedback(const xrt::drivers::wivrn::from_headset::feedback &, const clock_offset&);
void mark_timing_point(
comp_target_timing_point point,
int64_t frame_id,
uint64_t when_ns);
};