From b5e5586ef6622ec27cdbd314f4ae7b12abd577cd Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Wed, 20 Aug 2025 09:52:45 +0200 Subject: [PATCH] Fix HTC face tracker The tracker must be created precisely between the session creation and session begin, or else it would fail when we access it with XR_RUNTIME_FAILURE. As it is not possible to recreate one later, disable the destroy and store the handles in static variables so we can access them later, behaving as if we were actually destroying and recreating one. --- client/application.cpp | 19 +++++++++---- client/xr/htc_face_tracker.cpp | 52 ++++++++++++++-------------------- client/xr/htc_face_tracker.h | 12 ++------ 3 files changed, 38 insertions(+), 45 deletions(-) diff --git a/client/application.cpp b/client/application.cpp index 9205cce1..c4a2227c 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -32,6 +32,7 @@ #include "xr/actionset.h" #include "xr/check.h" #include "xr/htc_exts.h" +#include "xr/htc_face_tracker.h" #include "xr/meta_body_tracking_fidelity.h" #include "xr/to_string.h" #include @@ -1282,6 +1283,19 @@ void application::initialize() xr_session = xr::session(xr_instance, xr_system_id, vk_instance, vk_physical_device, vk_device, vk_queue, vk_queue_family_index); + spaces[size_t(xr::spaces::view)] = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_VIEW); + spaces[size_t(xr::spaces::world)] = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_STAGE); + + config.emplace(xr_system_id); + + // HTC face tracker fails if created later + // we can destroy it right away, it actually stores static handles + if (xr_system_id.face_tracker_supported() == xr::face_tracker_type::htc) + { + auto props = xr_system_id.htc_face_tracking_properties(); + xr::htc_face_tracker(xr_instance, xr_session, props.supportEyeFacialTracking, props.supportLipFacialTracking); + } + { auto spaces = xr_session.get_reference_spaces(); spdlog::info("{} reference spaces", spaces.size()); @@ -1289,11 +1303,6 @@ void application::initialize() spdlog::info(" {}", xr::to_string(i)); } - spaces[size_t(xr::spaces::view)] = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_VIEW); - spaces[size_t(xr::spaces::world)] = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_STAGE); - - config.emplace(xr_system_id); - vk::CommandPoolCreateInfo cmdpool_create_info; cmdpool_create_info.queueFamilyIndex = vk_queue_family_index; cmdpool_create_info.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer; diff --git a/client/xr/htc_face_tracker.cpp b/client/xr/htc_face_tracker.cpp index 2ec75761..3d5dbc23 100644 --- a/client/xr/htc_face_tracker.cpp +++ b/client/xr/htc_face_tracker.cpp @@ -18,53 +18,43 @@ */ #include "htc_face_tracker.h" -#include "wivrn_packets.h" #include "xr/instance.h" #include "xr/session.h" #include -xr::htc_face_tracker::htc_face_tracker(instance & inst, session & s, bool eye, bool lip) : - s(s) +static XrFacialTrackerHTC make_tracker(xr::instance & inst, xr::session & s, XrFacialTrackingTypeHTC type) { - if (!eye && !lip) - return; - - xrCreateFacialTrackerHTC = inst.get_proc("xrCreateFacialTrackerHTC"); - xrGetFacialExpressionsHTC = inst.get_proc("xrGetFacialExpressionsHTC"); - xrDestroyFacialTrackerHTC = inst.get_proc("xrDestroyFacialTrackerHTC"); - XrFacialTrackerCreateInfoHTC create_info{ .type = XR_TYPE_FACIAL_TRACKER_CREATE_INFO_HTC, - .next = nullptr, + .facialTrackingType = type, }; - - 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); - } + auto xrCreateFacialTrackerHTC = inst.get_proc("xrCreateFacialTrackerHTC"); + XrFacialTrackerHTC id; + CHECK_XR(xrCreateFacialTrackerHTC(s, &create_info, &id)); + return id; } -xr::htc_face_tracker::~htc_face_tracker() +static XrFacialTrackerHTC get_eye_tracker(xr::instance & inst, xr::session & s) { - if (!xrDestroyFacialTrackerHTC) - return; + static XrFacialTrackerHTC res = make_tracker(inst, s, XR_FACIAL_TRACKING_TYPE_EYE_DEFAULT_HTC); + return res; +} +static XrFacialTrackerHTC get_lip_tracker(xr::instance & inst, xr::session & s) +{ + static XrFacialTrackerHTC res = make_tracker(inst, s, XR_FACIAL_TRACKING_TYPE_LIP_DEFAULT_HTC); + return res; +} - if (eye) - CHECK_XR(xrDestroyFacialTrackerHTC(std::exchange(eye, nullptr))); - if (lip) - CHECK_XR(xrDestroyFacialTrackerHTC(std::exchange(lip, nullptr))); +xr::htc_face_tracker::htc_face_tracker(instance & inst, session & s, bool eye, bool lip) : + xrGetFacialExpressionsHTC(inst.get_proc("xrGetFacialExpressionsHTC")), + eye(eye ? get_eye_tracker(inst, s) : XR_NULL_HANDLE), + lip(lip ? get_lip_tracker(inst, s) : XR_NULL_HANDLE) +{ } void xr::htc_face_tracker::get_weights(XrTime time, packet_type & out_expressions) { - if (!(eye || lip) || !xrGetFacialExpressionsHTC) - return; + assert(xrGetFacialExpressionsHTC); XrFacialExpressionsHTC expressions{ .type = XR_TYPE_FACIAL_EXPRESSIONS_HTC, diff --git a/client/xr/htc_face_tracker.h b/client/xr/htc_face_tracker.h index fecd802c..5fe228a3 100644 --- a/client/xr/htc_face_tracker.h +++ b/client/xr/htc_face_tracker.h @@ -19,7 +19,6 @@ #pragma once -#include "utils/handle.h" #include "wivrn_packets.h" #include @@ -30,18 +29,13 @@ class session; class htc_face_tracker { - XrSession s; - XrFacialTrackerHTC eye{}; - XrFacialTrackerHTC lip{}; - PFN_xrCreateFacialTrackerHTC xrCreateFacialTrackerHTC{}; - PFN_xrGetFacialExpressionsHTC xrGetFacialExpressionsHTC{}; - PFN_xrDestroyFacialTrackerHTC xrDestroyFacialTrackerHTC{}; + PFN_xrGetFacialExpressionsHTC xrGetFacialExpressionsHTC; + XrFacialTrackerHTC eye; + XrFacialTrackerHTC lip; public: using packet_type = wivrn::from_headset::tracking::htc_face; - htc_face_tracker() = default; htc_face_tracker(instance & inst, session & s, bool eye, bool lip); - ~htc_face_tracker(); void get_weights(XrTime time, packet_type & out_expressions); };