Put not requested tracking on standby

This commit is contained in:
Patrick Nicolas
2024-09-03 23:34:23 +02:00
parent 843a83761a
commit 96bbfa1a23
17 changed files with 178 additions and 51 deletions
+3 -2
View File
@@ -80,7 +80,8 @@ private:
std::thread network_thread;
std::mutex local_floor_mutex;
xr::space local_floor;
std::atomic<std::chrono::nanoseconds::rep> tracking_prediction_offset = 0;
std::mutex tracking_control_mutex;
to_headset::tracking_control tracking_control;
std::atomic<XrDuration> display_time_phase = 0;
std::atomic<XrDuration> display_time_period = 0;
std::optional<std::thread> tracking_thread;
@@ -138,7 +139,7 @@ public:
void operator()(to_headset::video_stream_data_shard &&);
void operator()(to_headset::haptics &&);
void operator()(to_headset::timesync_query &&);
void operator()(to_headset::prediction_offset &&);
void operator()(to_headset::tracking_control &&);
void operator()(to_headset::audio_stream_description &&);
void operator()(to_headset::video_stream_description &&);
void operator()(audio_data &&);
+4 -1
View File
@@ -313,7 +313,10 @@ XrCompositionLayerQuad scenes::stream::plot_performance_metrics(XrTime predicted
}
ImPlot::PopStyleColor(5);
ImGui::Text("%s", fmt::format(_F("Estimated motion to photons latency: {}ms"), tracking_prediction_offset.load() / 1'000'000).c_str());
{
std::lock_guard lock(tracking_control_mutex);
ImGui::Text("%s", fmt::format(_F("Estimated motion to photons latency: {}ms"), std::chrono::duration_cast<std::chrono::milliseconds>(tracking_control.offset).count()).c_str());
}
ImGui::End();
return imgui_ctx->end_frame();
+48 -13
View File
@@ -27,6 +27,8 @@
#include "jnipp.h"
#endif
using tid = to_headset::tracking_control::id;
static from_headset::tracking::pose locate_space(device_id device, XrSpace space, XrSpace reference, XrTime time)
{
XrSpaceVelocity velocity{
@@ -137,6 +139,26 @@ static std::optional<std::array<from_headset::hand_tracking::pose, XR_HAND_JOINT
return std::nullopt;
}
static bool enabled(const to_headset::tracking_control & control, device_id id)
{
switch (id)
{
case device_id::HEAD:
return true;
case device_id::LEFT_AIM:
return control.enabled[size_t(tid::left_aim)];
case device_id::LEFT_GRIP:
return control.enabled[size_t(tid::left_grip)];
case device_id::RIGHT_AIM:
return control.enabled[size_t(tid::right_aim)];
case device_id::RIGHT_GRIP:
return control.enabled[size_t(tid::right_grip)];
default:
break;
}
__builtin_unreachable();
}
void scenes::stream::tracking()
{
#ifdef __ANDROID__
@@ -200,7 +222,13 @@ void scenes::stream::tracking()
t.resume();
};
XrDuration prediction = std::min<XrDuration>(tracking_prediction_offset, 80'000'000);
to_headset::tracking_control control;
{
std::lock_guard lock(tracking_control_mutex);
control = tracking_control;
}
XrDuration prediction = std::min<XrDuration>(control.offset.count(), 80'000'000);
auto period = std::max<XrDuration>(display_time_period.load(), 1'000'000);
for (XrDuration Δt = 0; Δt <= prediction + period / 2; Δt += period, ++samples)
{
@@ -232,23 +260,30 @@ void scenes::stream::tracking()
std::lock_guard lock(local_floor_mutex);
for (auto [device, space]: spaces)
{
packet.device_poses.push_back(locate_space(device, space, local_floor, t0 + Δt));
if (enabled(control, device))
packet.device_poses.push_back(locate_space(device, space, local_floor, t0 + Δt));
}
send_stream(packet);
if (application::get_hand_tracking_supported())
{
hands.hand = xrt::drivers::wivrn::from_headset::hand_tracking::left;
hands.joints = locate_hands(application::get_left_hand(), local_floor, hands.timestamp);
send_stream(hands);
if (control.enabled[size_t(tid::left_hand)])
{
hands.hand = xrt::drivers::wivrn::from_headset::hand_tracking::left;
hands.joints = locate_hands(application::get_left_hand(), local_floor, hands.timestamp);
send_stream(hands);
}
hands.hand = xrt::drivers::wivrn::from_headset::hand_tracking::right;
hands.joints = locate_hands(application::get_right_hand(), local_floor, hands.timestamp);
send_stream(hands);
if (control.enabled[size_t(tid::right_hand)])
{
hands.hand = xrt::drivers::wivrn::from_headset::hand_tracking::right;
hands.joints = locate_hands(application::get_right_hand(), local_floor, hands.timestamp);
send_stream(hands);
}
}
if (application::get_fb_face_tracking2_supported())
if (application::get_fb_face_tracking2_supported() and control.enabled[size_t(tid::face)])
{
application::get_fb_face_tracker2().get_weights(t0 + Δt, &fb_face2);
send_stream(fb_face2);
@@ -278,7 +313,7 @@ void scenes::stream::tracking()
}
#ifdef __ANDROID__
if (next_battery_check < now)
if (next_battery_check < now and control.enabled[size_t(tid::battery)])
{
timer t2(instance);
from_headset::battery battery{};
@@ -317,8 +352,8 @@ void scenes::stream::tracking()
}
}
void scenes::stream::operator()(to_headset::prediction_offset && packet)
void scenes::stream::operator()(to_headset::tracking_control && packet)
{
if (packet.offset.count() >= 0)
tracking_prediction_offset = std::lerp(packet.offset.count(), tracking_prediction_offset.load(), 0.2);
std::lock_guard lock(tracking_control_mutex);
tracking_control = packet;
}
+16 -2
View File
@@ -356,12 +356,26 @@ struct timesync_query
XrTime query;
};
struct prediction_offset
struct tracking_control
{
enum class id
{
left_aim,
left_grip,
right_aim,
right_grip,
left_hand,
right_hand,
face,
battery,
last = battery,
};
std::chrono::nanoseconds offset;
std::array<bool, size_t(id::last) + 1> enabled;
};
using packets = std::variant<handshake, audio_stream_description, video_stream_description, audio_data, video_stream_data_shard, haptics, timesync_query, prediction_offset>;
using packets = std::variant<handshake, audio_stream_description, video_stream_description, audio_data, video_stream_data_shard, haptics, timesync_query, tracking_control>;
} // namespace to_headset
+3 -2
View File
@@ -118,12 +118,13 @@ static xrt_hand_joint_set convert_joints(const std::optional<std::array<from_hea
return output_joints;
}
void hand_joints_list::update_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset)
bool hand_joints_list::update_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset)
{
if (tracking.hand == hand_id)
add_sample(
return add_sample(
tracking.production_timestamp,
tracking.timestamp,
convert_joints(tracking.joints),
offset);
return true;
}
+3 -3
View File
@@ -24,14 +24,14 @@
class hand_joints_list : public history<hand_joints_list, xrt_hand_joint_set>
{
int hand_id;
public:
const int hand_id;
static xrt_hand_joint_set interpolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, float t);
static xrt_hand_joint_set extrapolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, uint64_t ta, uint64_t tb, uint64_t t);
hand_joints_list(int hand_id) :
hand_id(hand_id) {}
void update_tracking(const xrt::drivers::wivrn::from_headset::hand_tracking & tracking, const clock_offset & offset);
bool update_tracking(const xrt::drivers::wivrn::from_headset::hand_tracking & tracking, const clock_offset & offset);
};
+15 -3
View File
@@ -20,6 +20,7 @@
#pragma once
#include "clock_offset.h"
#include "os/os_time.h"
#include "util/u_logging.h"
#include <algorithm>
#include <cstddef>
@@ -37,14 +38,21 @@ class history
std::mutex mutex;
std::list<TimedData> data;
XrTime last_request;
protected:
void add_sample(XrTime produced_timestamp, XrTime timestamp, const Data & sample, const clock_offset & offset)
history() :
last_request(os_monotonic_get_ns()) {}
// return true if object is active (last request is not too old)
bool add_sample(XrTime produced_timestamp, XrTime timestamp, const Data & sample, const clock_offset & offset)
{
XrTime produced = offset.from_headset(produced_timestamp);
XrTime t = offset.from_headset(timestamp);
std::lock_guard lock(mutex);
bool active = produced - last_request < 1'000'000'000;
// Discard outdated data, packets could be reordered
if (not data.empty())
{
@@ -54,11 +62,11 @@ protected:
U_LOG_D("not using history: clock_offset not stable");
data.clear();
data.emplace_back(sample, produced, t);
return;
return active;
}
if (data.back().produced_timestamp > produced)
return;
return active;
}
// Discard outdated predictions
@@ -86,6 +94,8 @@ protected:
while (data.size() > MaxSamples)
data.pop_front();
return active;
}
public:
@@ -94,6 +104,8 @@ public:
std::lock_guard lock(mutex);
std::chrono::nanoseconds ex(0);
last_request = at_timestamp_ns;
if (data.empty())
{
return {};
+3 -3
View File
@@ -60,16 +60,16 @@ xrt_space_relation pose_list::extrapolate(const xrt_space_relation & a, const xr
return res;
}
void pose_list::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
bool pose_list::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
{
for (const auto & pose: tracking.device_poses)
{
if (pose.device != device)
continue;
add_sample(tracking.production_timestamp, tracking.timestamp, convert_pose(pose), offset);
return;
return add_sample(tracking.production_timestamp, tracking.timestamp, convert_pose(pose), offset);
}
return true;
}
xrt_space_relation pose_list::convert_pose(const from_headset::tracking::pose & pose)
+3 -3
View File
@@ -26,16 +26,16 @@
class pose_list : public history<pose_list, xrt_space_relation>
{
xrt::drivers::wivrn::device_id device;
public:
const xrt::drivers::wivrn::device_id device;
static xrt_space_relation interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t);
static xrt_space_relation extrapolate(const xrt_space_relation & a, const xrt_space_relation & b, uint64_t ta, uint64_t tb, uint64_t t);
pose_list(xrt::drivers::wivrn::device_id id) :
device(id) {}
void update_tracking(const xrt::drivers::wivrn::from_headset::tracking &, const clock_offset & offset);
bool update_tracking(const xrt::drivers::wivrn::from_headset::tracking &, const clock_offset & offset);
static xrt_space_relation convert_pose(const xrt::drivers::wivrn::from_headset::tracking::pose &);
};
+3 -3
View File
@@ -35,7 +35,7 @@ tracked_views view_list::extrapolate(const tracked_views & a, const tracked_view
return result;
}
void view_list::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
bool view_list::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
{
for (const auto & pose: tracking.device_poses)
{
@@ -53,7 +53,7 @@ void view_list::update_tracking(const from_headset::tracking & tracking, const c
view.fovs[eye] = xrt_cast(tracking.views[eye].fov);
}
add_sample(tracking.production_timestamp, tracking.timestamp, view, offset);
return;
return add_sample(tracking.production_timestamp, tracking.timestamp, view, offset);
}
return true;
}
+1 -1
View File
@@ -36,5 +36,5 @@ public:
static tracked_views interpolate(const tracked_views & a, const tracked_views & b, float t);
static tracked_views extrapolate(const tracked_views & a, const tracked_views & b, uint64_t ta, uint64_t tb, uint64_t t);
void update_tracking(const from_headset::tracking & tracking, const clock_offset & offset);
bool update_tracking(const from_headset::tracking & tracking, const clock_offset & offset);
};
+9 -3
View File
@@ -315,9 +315,11 @@ xrt_space_relation wivrn_controller::get_tracked_pose(xrt_input_name name, uint6
{
case XRT_INPUT_TOUCH_AIM_POSE:
std::tie(extrapolation_time, res) = aim.get_at(at_timestamp_ns);
cnx->set_enabled(aim.device, true);
break;
case XRT_INPUT_TOUCH_GRIP_POSE:
std::tie(extrapolation_time, res) = grip.get_at(at_timestamp_ns);
cnx->set_enabled(grip.device, true);
break;
default:
U_LOG_W("Unknown input name requested");
@@ -335,6 +337,7 @@ std::pair<xrt_hand_joint_set, uint64_t> wivrn_controller::get_hand_tracking(xrt_
case XRT_INPUT_GENERIC_HAND_TRACKING_RIGHT: {
auto [extrapolation_time, data] = joints.get_at(desired_timestamp_ns);
cnx->add_predict_offset(extrapolation_time);
cnx->set_enabled(joints.hand_id == 0 ? to_headset::tracking_control::id::left_hand : to_headset::tracking_control::id::right_hand, true);
return {data, desired_timestamp_ns};
}
@@ -346,13 +349,16 @@ std::pair<xrt_hand_joint_set, uint64_t> wivrn_controller::get_hand_tracking(xrt_
void wivrn_controller::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
{
aim.update_tracking(tracking, offset);
grip.update_tracking(tracking, offset);
if (not aim.update_tracking(tracking, offset))
cnx->set_enabled(aim.device, false);
if (not grip.update_tracking(tracking, offset))
cnx->set_enabled(grip.device, false);
}
void wivrn_controller::update_hand_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset)
{
joints.update_tracking(tracking, offset);
if (not joints.update_tracking(tracking, offset))
cnx->set_enabled(joints.hand_id == 0 ? to_headset::tracking_control::id::left_hand : to_headset::tracking_control::id::right_hand, false);
}
void wivrn_controller::set_output(xrt_output_name name, const xrt_output_value * value)
+3 -1
View File
@@ -79,13 +79,15 @@ void wivrn_fb_face2_tracker::update_tracking(const from_headset::fb_face2 & face
data.weights = face.weights;
data.confidences = face.confidences;
face_list.update_tracking(face.production_timestamp, face.timestamp, data, offset);
if (not face_list.update_tracking(face.production_timestamp, face.timestamp, data, offset))
cnx->set_enabled(to_headset::tracking_control::id::face, false);
}
xrt_result_t wivrn_fb_face2_tracker::get_face_tracking(enum xrt_input_name facial_expression_type, struct xrt_facial_expression_set * inout_value)
{
if (facial_expression_type == XRT_INPUT_FB_FACE_TRACKING2_VISUAL)
{
cnx->set_enabled(to_headset::tracking_control::id::face, true);
auto [_, data] = face_list.get_at(inout_value->face_expression_set2_fb.sample_time_ns);
inout_value->face_expression_set2_fb.is_valid = data.is_valid;
+2 -2
View File
@@ -75,9 +75,9 @@ public:
return result;
}
void update_tracking(const XrTime & production_timestamp, const XrTime & timestamp, const wivrn_fb_face2_data & data, const clock_offset & offset)
bool update_tracking(const XrTime & production_timestamp, const XrTime & timestamp, const wivrn_fb_face2_data & data, const clock_offset & offset)
{
this->add_sample(production_timestamp, timestamp, data, offset);
return this->add_sample(production_timestamp, timestamp, data, offset);
}
};
+4
View File
@@ -268,6 +268,8 @@ void wivrn_hmd::update_tracking(const from_headset::tracking & tracking, const c
void wivrn_hmd::update_battery(const from_headset::battery & new_battery)
{
// We will only request a new sample if the current one is consumed
cnx->set_enabled(to_headset::tracking_control::id::battery, false);
std::lock_guard lock(mutex);
battery = new_battery;
}
@@ -312,6 +314,8 @@ xrt_result_t wivrn_hmd::get_battery_status(struct xrt_device * xdev,
bool * out_charging,
float * out_charge)
{
cnx->set_enabled(to_headset::tracking_control::id::battery, true);
std::lock_guard lock(mutex);
*out_present = battery.present;
*out_charging = battery.charging;
+37 -3
View File
@@ -39,6 +39,7 @@
#include "xrt/xrt_session.h"
#include <cmath>
#include <magic_enum.hpp>
#include <vulkan/vulkan.h>
struct wivrn_comp_target_factory : public comp_target_factory
@@ -78,18 +79,27 @@ struct wivrn_comp_target_factory : public comp_target_factory
}
};
void xrt::drivers::wivrn::max_accumulator::send(wivrn_connection & connection)
void xrt::drivers::wivrn::tracking_control_t::send(wivrn_connection & connection)
{
if (std::chrono::steady_clock::now() < next_sample)
return;
if (auto offset = max.exchange(0))
connection.send_stream(to_headset::prediction_offset{
connection.send_stream(to_headset::tracking_control{
.offset = std::chrono::nanoseconds(offset),
.enabled = enabled,
});
next_sample += std::chrono::seconds(1);
}
void xrt::drivers::wivrn::tracking_control_t::set_enabled(to_headset::tracking_control::id id, bool enabled)
{
std::lock_guard lock(mutex);
if (enabled != this->enabled[size_t(id)])
U_LOG_I("%s tracking: %s", std::string(magic_enum::enum_name(id)).c_str(), enabled ? "enabled" : "disabled");
this->enabled[size_t(id)] = enabled;
}
xrt::drivers::wivrn::wivrn_session::wivrn_session(xrt::drivers::wivrn::TCP && tcp, u_system & system) :
connection(std::move(tcp)), xrt_system(system)
{
@@ -300,6 +310,30 @@ void wivrn_session::operator()(from_headset::timesync_response && timesync)
offset_est.add_sample(timesync);
}
static auto to_tracking_control(device_id id)
{
using tid = to_headset::tracking_control::id;
switch (id)
{
case device_id::LEFT_AIM:
return tid::left_aim;
case device_id::LEFT_GRIP:
return tid::left_grip;
case device_id::RIGHT_AIM:
return tid::right_aim;
case device_id::RIGHT_GRIP:
return tid::right_grip;
default:
break;
}
__builtin_unreachable();
}
void wivrn_session::set_enabled(device_id id, bool enabled)
{
tracking_control.set_enabled(to_tracking_control(id), enabled);
}
void wivrn_session::operator()(from_headset::feedback && feedback)
{
assert(comp_target);
@@ -343,7 +377,7 @@ void wivrn_session::run(std::weak_ptr<wivrn_session> weak_self)
if (self and not self->quit)
{
self->offset_est.request_sample(self->connection);
self->predict_offset.send(self->connection);
self->tracking_control.send(self->connection);
self->connection.poll(*self, 20);
}
else
+21 -6
View File
@@ -48,15 +48,20 @@ namespace xrt::drivers::wivrn
{
struct wivrn_comp_target;
class max_accumulator
class tracking_control_t
{
using T = std::chrono::nanoseconds::rep;
std::atomic<T> max;
std::chrono::steady_clock::time_point next_sample;
std::mutex mutex;
decltype(to_headset::tracking_control::enabled) enabled;
public:
max_accumulator() :
next_sample(std::chrono::steady_clock::now()) {}
tracking_control_t() :
next_sample(std::chrono::steady_clock::now())
{
enabled.fill(true);
}
void add(std::chrono::nanoseconds s)
{
auto sample = s.count();
@@ -66,6 +71,8 @@ public:
}
}
void send(wivrn_connection & connection);
void set_enabled(to_headset::tracking_control::id id, bool enabled);
};
class wivrn_session : public std::enable_shared_from_this<wivrn_session>
@@ -91,8 +98,9 @@ class wivrn_session : public std::enable_shared_from_this<wivrn_session>
clock_offset_estimator offset_est;
// offsets in ns of each requested frame from its generation time
max_accumulator predict_offset;
// prediction offset and enabled tracking to configure client
tracking_control_t tracking_control;
std::mutex tracking_control_mutex;
std::mutex csv_mutex;
std::ofstream feedback_csv;
@@ -117,9 +125,16 @@ public:
void add_predict_offset(std::chrono::nanoseconds off)
{
predict_offset.add(off);
tracking_control.add(off);
}
void set_enabled(to_headset::tracking_control::id id, bool enabled)
{
tracking_control.set_enabled(id, enabled);
}
void set_enabled(device_id id, bool enabled);
void operator()(from_headset::handshake &&) {}
void operator()(from_headset::headset_info_packet &&);
void operator()(from_headset::tracking &&);