Store face tracker in variant

This commit is contained in:
Sapphire
2025-06-30 11:34:34 +02:00
committed by xytovl
parent dd4d442f43
commit 8dc2b7a5e0
14 changed files with 137 additions and 208 deletions
+21 -45
View File
@@ -1136,40 +1136,6 @@ void application::initialize()
XrSystemEyeGazeInteractionPropertiesEXT eye_gaze_properties = xr_system_id.eye_gaze_interaction_properties();
spdlog::info(" Eye gaze support: {}", (bool)eye_gaze_properties.supportsEyeGazeInteraction);
eye_gaze_supported = eye_gaze_properties.supportsEyeGazeInteraction;
if (eye_gaze_supported)
{
switch (guess_model())
{
case model::pico_4_pro:
case model::pico_4_enterprise:
pico_face_tracking_supported = true;
break;
default:
pico_face_tracking_supported = false;
break;
}
}
else
pico_face_tracking_supported = false;
spdlog::info(" PICO face tracking support: {}", (bool)pico_face_tracking_supported);
}
if (utils::contains(xr_extensions, XR_FB_FACE_TRACKING2_EXTENSION_NAME))
{
XrSystemFaceTrackingProperties2FB fb_face2_properties = xr_system_id.fb_face_tracking2_properties();
spdlog::info(" FB face tracking support: {}", (bool)fb_face2_properties.supportsVisualFaceTracking);
fb_face_tracking2_supported = fb_face2_properties.supportsVisualFaceTracking;
}
if (utils::contains(xr_extensions, XR_HTC_FACIAL_TRACKING_EXTENSION_NAME))
{
XrSystemFacialTrackingPropertiesHTC htc_face_properties = xr_system_id.htc_face_tracking_properties();
spdlog::info(" HTC eye tracking support: {}", (bool)htc_face_properties.supportEyeFacialTracking);
spdlog::info(" HTC lip tracking support: {}", (bool)htc_face_properties.supportLipFacialTracking);
htc_face_tracking_eye_supported = htc_face_properties.supportEyeFacialTracking;
htc_face_tracking_lip_supported = htc_face_properties.supportLipFacialTracking;
}
if (utils::contains(xr_extensions, XR_FB_COMPOSITION_LAYER_SETTINGS_EXTENSION_NAME))
@@ -1216,24 +1182,34 @@ void application::initialize()
right_hand = xr_session.create_hand_tracker(XR_HAND_RIGHT_EXT);
}
if (fb_face_tracking2_supported)
if (utils::contains(xr_extensions, XR_FB_FACE_TRACKING2_EXTENSION_NAME))
{
fb_face_tracker2 = xr_session.create_fb_face_tracker2();
XrSystemFaceTrackingProperties2FB fb_face2_properties = xr_system_id.fb_face_tracking2_properties();
spdlog::info(" FB face tracking support: {}", (bool)fb_face2_properties.supportsVisualFaceTracking);
if (fb_face2_properties.supportsVisualFaceTracking)
face_tracker = xr_session.create_fb_face_tracker2();
}
if (htc_face_tracking_eye_supported)
if (std::holds_alternative<std::monostate>(face_tracker) && utils::contains(xr_extensions, XR_HTC_FACIAL_TRACKING_EXTENSION_NAME))
{
htc_face_tracker_eye = xr_session.create_htc_face_tracker(XR_FACIAL_TRACKING_TYPE_EYE_DEFAULT_HTC);
XrSystemFacialTrackingPropertiesHTC htc_face_properties = xr_system_id.htc_face_tracking_properties();
spdlog::info(" HTC eye tracking support: {}", (bool)htc_face_properties.supportEyeFacialTracking);
spdlog::info(" HTC lip tracking support: {}", (bool)htc_face_properties.supportLipFacialTracking);
face_tracker = xr_session.create_htc_face_tracker(htc_face_properties.supportEyeFacialTracking, htc_face_properties.supportLipFacialTracking);
}
if (htc_face_tracking_lip_supported)
if (std::holds_alternative<std::monostate>(face_tracker) && eye_gaze_supported)
{
htc_face_tracker_lip = xr_session.create_htc_face_tracker(XR_FACIAL_TRACKING_TYPE_LIP_DEFAULT_HTC);
}
if (pico_face_tracking_supported)
{
pico_face_tracker = xr_session.create_pico_face_tracker();
switch (guess_model())
{
case model::pico_4_pro:
case model::pico_4_enterprise:
spdlog::info(" PICO face tracking support: true");
face_tracker = xr_session.create_pico_face_tracker();
break;
default:
break;
}
}
if (utils::contains_all(xr_extensions, std::array{XR_FB_BODY_TRACKING_EXTENSION_NAME, XR_META_BODY_TRACKING_FULL_BODY_EXTENSION_NAME, XR_META_BODY_TRACKING_FIDELITY_EXTENSION_NAME}))
+5 -45
View File
@@ -129,17 +129,7 @@ class application : public singleton<application>
xr::hand_tracker left_hand;
xr::hand_tracker right_hand;
bool fb_face_tracking2_supported = false;
xr::fb_face_tracker2 fb_face_tracker2;
bool htc_face_tracking_eye_supported = false;
xr::htc_face_tracker htc_face_tracker_eye;
bool htc_face_tracking_lip_supported = false;
xr::htc_face_tracker htc_face_tracker_lip;
bool pico_face_tracking_supported = false;
xr::pico_face_tracker pico_face_tracker;
std::variant<std::monostate, xr::fb_face_tracker2, xr::htc_face_tracker, xr::pico_face_tracker> face_tracker;
std::variant<std::monostate, xr::fb_body_tracker, xr::htc_body_tracker, xr::pico_body_tracker> body_tracker;
bool eye_gaze_supported = false;
@@ -430,24 +420,9 @@ public:
return instance().hand_tracking_supported;
}
static bool get_fb_face_tracking2_supported()
static bool get_face_tracking_supported()
{
return instance().fb_face_tracking2_supported;
}
static bool get_htc_face_tracking_eye_supported()
{
return instance().htc_face_tracking_eye_supported;
}
static bool get_htc_face_tracking_lip_supported()
{
return instance().htc_face_tracking_lip_supported;
}
static bool get_pico_face_tracking_supported()
{
return instance().pico_face_tracking_supported;
return !std::holds_alternative<std::monostate>(instance().face_tracker);
}
static bool get_body_tracking_supported()
@@ -475,24 +450,9 @@ public:
return instance().right_hand;
}
static xr::fb_face_tracker2 & get_fb_face_tracker2()
static auto & get_face_tracker()
{
return instance().fb_face_tracker2;
}
static xr::htc_face_tracker & get_htc_face_tracker_eye()
{
return instance().htc_face_tracker_eye;
}
static xr::htc_face_tracker & get_htc_face_tracker_lip()
{
return instance().htc_face_tracker_lip;
}
static xr::pico_face_tracker & get_pico_face_tracker()
{
return instance().pico_face_tracker;
return instance().face_tracker;
}
static auto & get_body_tracker()
+3 -4
View File
@@ -106,18 +106,17 @@ bool configuration::check_feature(feature f) const
{
case model::htc_vive_focus_3:
case model::htc_vive_focus_vision:
[[fallthrough]];
case model::htc_vive_xr_elite:
if (not(application::get_htc_face_tracking_eye_supported() or application::get_htc_face_tracking_lip_supported()))
if (not std::holds_alternative<xr::htc_face_tracker>(application::get_face_tracker()))
return false;
break;
case model::pico_4_pro:
case model::pico_4_enterprise:
if (not application::get_pico_face_tracking_supported())
if (not std::holds_alternative<xr::pico_face_tracker>(application::get_face_tracker()))
return false;
break;
default:
if (not application::get_fb_face_tracking2_supported())
if (not std::holds_alternative<xr::fb_face_tracker2>(application::get_face_tracker()))
return false;
break;
}
+7 -15
View File
@@ -728,7 +728,7 @@ void scenes::lobby::gui_settings()
ImGui::EndDisabled();
}
{
ImGui::BeginDisabled(not(application::get_fb_face_tracking2_supported() or application::get_htc_face_tracking_eye_supported() or application::get_htc_face_tracking_lip_supported() or application::get_pico_face_tracking_supported()));
ImGui::BeginDisabled(not application::get_face_tracking_supported());
bool enabled = config.check_feature(feature::face_tracking);
if (ImGui::Checkbox(_S("Enable face tracking"), &enabled))
{
@@ -1400,7 +1400,7 @@ static const char * get_face_icon(XrTime predicted_display_time)
{
static const auto w = face_weights();
wivrn::from_headset::tracking::fb_face2 expression;
application::get_fb_face_tracker2().get_weights(predicted_display_time, expression);
std::get<xr::fb_face_tracker2>(application::get_face_tracker()).get_weights(predicted_display_time, expression);
if (not expression.is_valid)
return ICON_FA_FACE_MEH;
@@ -1464,24 +1464,16 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time)
});
}
if (application::get_fb_face_tracking2_supported())
if (application::get_face_tracking_supported())
{
const char * icon_enabled = std::holds_alternative<xr::fb_face_tracker2>(application::get_face_tracker())
? get_face_icon(predicted_display_time)
: ICON_FA_FACE_KISS_WINK_HEART;
items.push_back({
.f = feature::face_tracking,
.tooltip_enabled = _("Face tracking is enabled"),
.tooltip_disabled = _("Face tracking is disabled"),
.icon_enabled = get_face_icon(predicted_display_time),
.icon_disabled = ICON_FA_FACE_MEH_BLANK,
});
}
if (application::get_htc_face_tracking_eye_supported() or application::get_htc_face_tracking_lip_supported() or application::get_pico_face_tracking_supported())
{
items.push_back({
.f = feature::face_tracking,
.tooltip_enabled = _("Face tracking is enabled"),
.tooltip_disabled = _("Face tracking is disabled"),
.icon_enabled = ICON_FA_FACE_KISS_WINK_HEART,
.icon_enabled = icon_enabled,
.icon_disabled = ICON_FA_FACE_MEH_BLANK,
});
}
+16 -6
View File
@@ -18,6 +18,7 @@
*/
#include "utils/overloaded.h"
#include "xr/fb_body_tracker.h"
#include "xr/fb_face_tracker2.h"
#include "xr/htc_body_tracker.h"
#include "xr/pico_body_tracker.h"
#define GLM_FORCE_RADIANS
@@ -219,12 +220,21 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
if (config.check_feature(feature::face_tracking))
{
if (application::get_fb_face_tracking2_supported())
info.face_tracking = from_headset::face_type::fb2;
else if (application::get_htc_face_tracking_eye_supported() or application::get_htc_face_tracking_lip_supported())
info.face_tracking = from_headset::face_type::htc;
else if (application::get_pico_face_tracking_supported())
info.face_tracking = from_headset::face_type::pico;
info.face_tracking = std::visit(utils::overloaded{
[](std::monostate &) {
return from_headset::face_type::none;
},
[](xr::fb_face_tracker2 &) {
return from_headset::face_type::fb2;
},
[](xr::htc_face_tracker &) {
return from_headset::face_type::htc;
},
[](xr::pico_face_tracker &) {
return from_headset::face_type::fb2;
},
},
application::get_face_tracker());
}
info.num_generic_trackers = 0;
+18 -38
View File
@@ -261,21 +261,15 @@ void scenes::stream::tracking()
break;
}
from_headset::face_type face_tracking = from_headset::face_type::none;
if (config.check_feature(feature::face_tracking))
const bool face_tracking = config.check_feature(feature::face_tracking);
auto & face_tracker = application::get_face_tracker();
if (face_tracking)
{
if (application::get_fb_face_tracking2_supported())
face_tracking = from_headset::face_type::fb2;
else if (application::get_htc_face_tracking_eye_supported() or application::get_htc_face_tracking_lip_supported())
face_tracking = from_headset::face_type::htc;
else if (application::get_pico_face_tracking_supported())
{
face_tracking = from_headset::face_type::pico;
// We can't start the face tracking on application initialisation like we do for
// other face trackers due to a Pico runtime bug where face tracking freezes when
// the headset is taken off or the application is quit.
application::get_pico_face_tracker().start();
}
// We can't start the face tracking on application initialisation like we do for
// other face trackers due to a Pico runtime bug where face tracking freezes when
// the headset is taken off or the application is quit.
if (auto * face_pico = std::get_if<xr::pico_face_tracker>(&face_tracker))
face_pico->start();
}
const bool body_tracking = config.check_feature(feature::body_tracking);
@@ -430,29 +424,15 @@ void scenes::stream::tracking()
},
body_tracker);
if (control.enabled[size_t(tid::face)])
if (face_tracking && control.enabled[size_t(tid::face)])
{
switch (face_tracking)
{
case wivrn::from_headset::face_type::none:
break;
case wivrn::from_headset::face_type::fb2:
application::get_fb_face_tracker2().get_weights(t0 + Δt, packet.face.emplace<wivrn::from_headset::tracking::fb_face2>());
break;
case wivrn::from_headset::face_type::htc: {
auto & face_htc = packet.face.emplace<wivrn::from_headset::tracking::htc_face>();
face_htc.eye_active = false;
face_htc.lip_active = false;
if (application::get_htc_face_tracking_eye_supported())
application::get_htc_face_tracker_eye().get_weights(t0 + Δt, face_htc);
if (application::get_htc_face_tracking_lip_supported())
application::get_htc_face_tracker_lip().get_weights(t0 + Δt, face_htc);
break;
}
case wivrn::from_headset::face_type::pico:
application::get_pico_face_tracker().get_weights(t0 + Δt, packet.face.emplace<wivrn::from_headset::tracking::fb_face2>());
break;
}
std::visit(utils::overloaded{
[](std::monostate &) {},
[&](auto & ft) {
ft.get_weights(t0 + Δt, packet.face.emplace<typename std::remove_reference_t<decltype(ft)>::packet_type>());
},
},
face_tracker);
}
}
catch (const std::system_error & e)
@@ -555,8 +535,8 @@ void scenes::stream::tracking()
}
}
if (face_tracking == from_headset::face_type::pico)
application::get_pico_face_tracker().stop();
if (auto * face_pico = std::get_if<xr::pico_face_tracker>(&face_tracker))
face_pico->stop();
}
void scenes::stream::operator()(to_headset::tracking_control && packet)
+2 -1
View File
@@ -34,9 +34,10 @@ class fb_face_tracker2 : public utils::handle<XrFaceTracker2FB, destroy_fb_face_
PFN_xrDestroyFaceTracker2FB xrDestroyFaceTracker2FB{};
public:
using packet_type = wivrn::from_headset::tracking::fb_face2;
fb_face_tracker2() = default;
fb_face_tracker2(instance & inst, XrFaceTracker2FB h);
void get_weights(XrTime time, struct wivrn::from_headset::tracking::fb_face2 & out_expressions);
void get_weights(XrTime time, packet_type & out_expressions);
};
} // namespace xr
+48 -30
View File
@@ -1,6 +1,7 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 galister <galister@librevr.org>
* Copyright (C) 2025 Sapphire <imsapphire0@gmail.com>
*
* 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
@@ -19,26 +20,50 @@
#include "htc_face_tracker.h"
#include "wivrn_packets.h"
#include "xr/instance.h"
#include "xr/session.h"
#include <openxr/openxr.h>
static PFN_xrDestroyFacialTrackerHTC xrDestroyFacialTrackerHTC{};
XrResult xr::destroy_htc_face_tracker(XrFacialTrackerHTC id)
xr::htc_face_tracker::htc_face_tracker(instance & inst, session & s, bool eye, bool lip) :
s(s)
{
return xrDestroyFacialTrackerHTC(id);
}
if (!eye && !lip)
return;
xr::htc_face_tracker::htc_face_tracker(instance & inst, XrFacialTrackerHTC h, XrFacialTrackingTypeHTC facialTrackingTypeHTC)
{
id = h;
xrCreateFacialTrackerHTC = inst.get_proc<PFN_xrCreateFacialTrackerHTC>("xrCreateFacialTrackerHTC");
xrGetFacialExpressionsHTC = inst.get_proc<PFN_xrGetFacialExpressionsHTC>("xrGetFacialExpressionsHTC");
xrDestroyFacialTrackerHTC = inst.get_proc<PFN_xrDestroyFacialTrackerHTC>("xrDestroyFacialTrackerHTC");
trackerType = facialTrackingTypeHTC;
XrFacialTrackerCreateInfoHTC create_info{
.type = XR_TYPE_FACIAL_TRACKER_CREATE_INFO_HTC,
.next = nullptr,
};
if (eye)
{
create_info.facialTrackingType = XR_FACIAL_TRACKING_TYPE_EYE_DEFAULT_HTC;
xrCreateFacialTrackerHTC(s, &create_info, &this->eye);
}
if (lip)
{
create_info.facialTrackingType = XR_FACIAL_TRACKING_TYPE_LIP_DEFAULT_HTC;
xrCreateFacialTrackerHTC(s, &create_info, &this->lip);
}
}
void xr::htc_face_tracker::get_weights(XrTime time, wivrn::from_headset::tracking::htc_face & out_expressions)
xr::htc_face_tracker::~htc_face_tracker()
{
if (!id || !xrGetFacialExpressionsHTC)
if (!xrDestroyFacialTrackerHTC)
return;
if (eye)
CHECK_XR(xrDestroyFacialTrackerHTC(std::exchange(eye, nullptr)));
if (lip)
CHECK_XR(xrDestroyFacialTrackerHTC(std::exchange(lip, nullptr)));
}
void xr::htc_face_tracker::get_weights(XrTime time, packet_type & out_expressions)
{
if (!(eye || lip) || !xrGetFacialExpressionsHTC)
return;
XrFacialExpressionsHTC expressions{
@@ -47,25 +72,18 @@ void xr::htc_face_tracker::get_weights(XrTime time, wivrn::from_headset::trackin
.sampleTime = time,
};
bool * is_active;
switch (trackerType)
if (eye)
{
case XR_FACIAL_TRACKING_TYPE_EYE_DEFAULT_HTC:
expressions.expressionCount = out_expressions.eye.size();
expressions.expressionWeightings = out_expressions.eye.data();
is_active = &out_expressions.eye_active;
break;
case XR_FACIAL_TRACKING_TYPE_LIP_DEFAULT_HTC:
expressions.expressionCount = out_expressions.lip.size();
expressions.expressionWeightings = out_expressions.lip.data();
is_active = &out_expressions.lip_active;
break;
default:
throw std::system_error(1, std::generic_category(), "htc_face_tracker with unknown trackerType");
expressions.expressionCount = out_expressions.eye.size();
expressions.expressionWeightings = out_expressions.eye.data();
CHECK_XR(xrGetFacialExpressionsHTC(eye, &expressions));
out_expressions.eye_active = expressions.isActive;
}
if (lip)
{
expressions.expressionCount = out_expressions.lip.size();
expressions.expressionWeightings = out_expressions.lip.data();
CHECK_XR(xrGetFacialExpressionsHTC(lip, &expressions));
out_expressions.lip_active = expressions.isActive;
}
CHECK_XR(xrGetFacialExpressionsHTC(id, &expressions));
*is_active = expressions.isActive;
}
+11 -6
View File
@@ -1,6 +1,7 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 galister <galister@librevr.org>
* Copyright (C) 2025 Sapphire <imsapphire0@gmail.com>
*
* 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
@@ -25,19 +26,23 @@
namespace xr
{
class instance;
class session;
XrResult destroy_htc_face_tracker(XrFacialTrackerHTC);
class htc_face_tracker : public utils::handle<XrFacialTrackerHTC, destroy_htc_face_tracker>
class htc_face_tracker
{
XrSession s;
XrFacialTrackerHTC eye{};
XrFacialTrackerHTC lip{};
PFN_xrCreateFacialTrackerHTC xrCreateFacialTrackerHTC{};
PFN_xrGetFacialExpressionsHTC xrGetFacialExpressionsHTC{};
PFN_xrDestroyFacialTrackerHTC xrDestroyFacialTrackerHTC{};
XrFacialTrackingTypeHTC trackerType{};
public:
using packet_type = wivrn::from_headset::tracking::htc_face;
htc_face_tracker() = default;
htc_face_tracker(instance & inst, XrFacialTrackerHTC h, XrFacialTrackingTypeHTC facialTrackingTypeHTC);
htc_face_tracker(instance & inst, session & s, bool eye, bool lip);
~htc_face_tracker();
void get_weights(XrTime time, struct wivrn::from_headset::tracking::htc_face & out_expressions);
void get_weights(XrTime time, packet_type & out_expressions);
};
} // namespace xr
+2 -1
View File
@@ -40,12 +40,13 @@ class pico_face_tracker
bool started{};
public:
using packet_type = wivrn::from_headset::tracking::fb_face2;
pico_face_tracker() = default;
pico_face_tracker(instance & inst, session & s);
~pico_face_tracker();
void start();
void stop();
void get_weights(XrTime time, struct wivrn::from_headset::tracking::fb_face2 & out_expressions);
void get_weights(XrTime time, packet_type & out_expressions);
};
} // namespace xr
+2 -14
View File
@@ -123,21 +123,9 @@ xr::fb_face_tracker2 xr::session::create_fb_face_tracker2()
return {*inst, ft};
}
xr::htc_face_tracker xr::session::create_htc_face_tracker(XrFacialTrackingTypeHTC facialTrackingType)
xr::htc_face_tracker xr::session::create_htc_face_tracker(bool eye, bool lip)
{
XrFacialTrackerCreateInfoHTC create_info{
.type = XR_TYPE_FACIAL_TRACKER_CREATE_INFO_HTC,
.next = nullptr,
.facialTrackingType = facialTrackingType,
};
XrFacialTrackerHTC ft;
auto xrCreateFacialTrackerHTC = inst->get_proc<PFN_xrCreateFacialTrackerHTC>("xrCreateFacialTrackerHTC");
assert(xrCreateFacialTrackerHTC);
CHECK_XR(xrCreateFacialTrackerHTC(id, &create_info, &ft));
return {*inst, ft, facialTrackingType};
return {*inst, *this, eye, lip};
}
xr::pico_face_tracker xr::session::create_pico_face_tracker()
+1 -1
View File
@@ -55,7 +55,7 @@ public:
space create_action_space(XrAction action, const XrPosef & pose = {{0, 0, 0, 1}, {0, 0, 0}});
hand_tracker create_hand_tracker(XrHandEXT hand, XrHandJointSetEXT hand_joint_set = XR_HAND_JOINT_SET_DEFAULT_EXT);
fb_face_tracker2 create_fb_face_tracker2();
htc_face_tracker create_htc_face_tracker(XrFacialTrackingTypeHTC);
htc_face_tracker create_htc_face_tracker(bool eye, bool lip);
pico_face_tracker create_pico_face_tracker();
fb_body_tracker create_fb_body_tracker();
htc_body_tracker create_htc_body_tracker();
-1
View File
@@ -198,7 +198,6 @@ enum face_type : uint8_t
none,
fb2,
htc,
pico,
};
struct headset_info_packet
+1 -1
View File
@@ -211,7 +211,7 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection
}
auto face = get_info().face_tracking;
if (face == from_headset::face_type::fb2 || face == from_headset::face_type::pico || is_forced_extension("FB_face_tracking2"))
if (face == from_headset::face_type::fb2 || is_forced_extension("FB_face_tracking2"))
{
fb_face2_tracker = std::make_unique<wivrn_fb_face2_tracker>(&hmd, *this);
static_roles.face = fb_face2_tracker.get();