From a98410bae05ee15b27e64ef16af98246823db618 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sat, 10 Oct 2026 12:43:24 +0200 Subject: [PATCH] Forward hand tracking data source --- client/render/imgui_impl.cpp | 6 ++-- client/scenes/lobby.cpp | 22 ++++++------- client/scenes/stream_gui.cpp | 8 ++--- client/scenes/stream_tracking.cpp | 53 ++++++++++++++++++------------ client/xr/hand_tracker.cpp | 49 ++++++++++++++------------- client/xr/hand_tracker.h | 10 ++++-- common/wivrn_packets.h | 4 +++ server/driver/hand_joints_list.cpp | 2 +- server/driver/hand_joints_list.h | 8 ++--- server/driver/wivrn_controller.cpp | 11 +++++-- server/driver/wivrn_controller.h | 3 +- server/driver/wivrn_session.cpp | 8 +++-- 12 files changed, 110 insertions(+), 74 deletions(-) diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index 784ac78a..041ac0c3 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -580,10 +580,10 @@ std::vector imgui_context::read_controllers_sta current_aim_interaction = 1; if (ctrl.hand) { - if (auto hand = ctrl.hand->locate(world, display_time); hand and hand->is_input_source()) + if (auto hand = ctrl.hand->locate(world, display_time); hand.joints and hand.is_input_source()) { - XrHandJointLocationEXT & index_tip = hand->joints[XR_HAND_JOINT_INDEX_TIP_EXT].first; - XrHandJointLocationEXT & palm = hand->joints[XR_HAND_JOINT_PALM_EXT].first; + XrHandJointLocationEXT & index_tip = (*hand.joints)[XR_HAND_JOINT_INDEX_TIP_EXT].first; + XrHandJointLocationEXT & palm = (*hand.joints)[XR_HAND_JOINT_PALM_EXT].first; if (index_tip.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) { diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index f4889839..6dc10aaa 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -981,28 +981,28 @@ void scenes::lobby::render(const XrFrameState & frame_state) auto windows = imgui_ctx->windows(); auto left = left_hand->locate(world_space, frame_state.predictedDisplayTime); - if (left) + if (left.joints) { - if (left->is_input_source()) - stick_finger_to_gui(left->joints, windows); + if (left.is_input_source()) + stick_finger_to_gui(*left.joints, windows); hide_left_controller = true; } auto right = right_hand->locate(world_space, frame_state.predictedDisplayTime); - if (right) + if (right.joints) { - if (right->is_input_source()) - stick_finger_to_gui(right->joints, windows); + if (right.is_input_source()) + stick_finger_to_gui(*right.joints, windows); hide_right_controller = true; } - hand_model::apply(world, left ? &left->joints : nullptr, right ? &right->joints : nullptr); + hand_model::apply(world, left.joints ? &*left.joints : nullptr, right.joints ? &*right.joints : nullptr); - if (not new_gui_position and head_position and left and left->is_input_source()) - new_gui_position = check_recenter_gesture(xr::spaces::palm_left, left->joints, *head_position); + if (not new_gui_position and head_position and left.joints and left.is_input_source()) + new_gui_position = check_recenter_gesture(xr::spaces::palm_left, *left.joints, *head_position); - if (not new_gui_position and head_position and right and right->is_input_source()) - new_gui_position = check_recenter_gesture(xr::spaces::palm_right, right->joints, *head_position); + if (not new_gui_position and head_position and right.joints and right.is_input_source()) + new_gui_position = check_recenter_gesture(xr::spaces::palm_right, *right.joints, *head_position); } if (head_position and new_gui_position) diff --git a/client/scenes/stream_gui.cpp b/client/scenes/stream_gui.cpp index a5a60e8d..43bc25e1 100644 --- a/client/scenes/stream_gui.cpp +++ b/client/scenes/stream_gui.cpp @@ -1097,12 +1097,12 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte auto left = left_hand->locate(world_space, predicted_display_time); auto right = right_hand->locate(world_space, predicted_display_time); - if (left and left->is_input_source() and - xr::hand_tracker::check_flags(left->joints, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0)) + if (left.joints and left.is_input_source() and + xr::hand_tracker::check_flags(*left.joints, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0)) hide_left_controller = true; - if (right and right->is_input_source() and - xr::hand_tracker::check_flags(right->joints, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0)) + if (right.joints and right.is_input_source() and + xr::hand_tracker::check_flags(*right.joints, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0)) hide_right_controller = true; } diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index 43d5c1bb..fd828eb0 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -155,16 +155,33 @@ public: } // namespace -static std::optional> locate_hands(xr::hand_tracker & hand, XrSpace space, XrTime time) +static from_headset::hand_tracking locate_hands( + xr::hand_tracker & tracker, + XrSpace space, + XrTime now, + XrTime time) { - auto located = hand.locate(space, time); + using hand_id = from_headset::hand_tracking::hand_id; + auto located = tracker.locate(space, time); - if (located and located->is_input_source()) + from_headset::hand_tracking res{ + .production_timestamp = now, + .timestamp = time, + .hand = tracker.hand() == XR_HAND_LEFT_EXT ? hand_id::left : hand_id::right, + }; + + if (located.data_source == XR_HAND_TRACKING_DATA_SOURCE_CONTROLLER_EXT) + res.hand = tracker.hand() == XR_HAND_LEFT_EXT + ? hand_id::left_controller + : hand_id::right_controller; + + if (located.joints) { - std::array poses; + const auto & joints = *located.joints; + auto & poses = res.joints.emplace(); for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) { - const auto & joint = located->joints[i]; + const auto & joint = joints[i]; poses[i] = { .position = joint.first.pose.position, .orientation = pack(joint.first.pose.orientation), @@ -174,11 +191,9 @@ static std::optional @@ -503,23 +518,19 @@ void scenes::stream::tracking() break; case wivrn::device_id::LEFT_HAND: if (left_hand) - { - hands.emplace_back( + hands.emplace_back(locate_hands( + *left_hand, + height_offset_space, t0, - at_time, - from_headset::hand_tracking::left, - locate_hands(*left_hand, height_offset_space, tracking.timestamp)); - } + at_time)); break; case wivrn::device_id::RIGHT_HAND: if (right_hand) - { - hands.emplace_back( + hands.emplace_back(locate_hands( + *right_hand, + height_offset_space, t0, - at_time, - from_headset::hand_tracking::right, - locate_hands(*right_hand, height_offset_space, tracking.timestamp)); - } + at_time)); break; case wivrn::device_id::BODY: std::visit(utils::overloaded{ diff --git a/client/xr/hand_tracker.cpp b/client/xr/hand_tracker.cpp index c015b093..88221dcd 100644 --- a/client/xr/hand_tracker.cpp +++ b/client/xr/hand_tracker.cpp @@ -27,7 +27,8 @@ #include xr::hand_tracker::hand_tracker(instance & inst, session & session, const XrHandTrackerCreateInfoEXT & info) : - handle(inst.get_proc("xrDestroyHandTrackerEXT")) + handle(inst.get_proc("xrDestroyHandTrackerEXT")), + hand_{info.hand} { auto xrCreateHandTrackerEXT = inst.get_proc("xrCreateHandTrackerEXT"); assert(xrCreateHandTrackerEXT); @@ -61,10 +62,10 @@ xr::hand_tracker::hand_tracker(instance & inst, session & session, const XrHandT } } -std::optional xr::hand_tracker::locate(XrSpace space, XrTime time) +xr::hand_tracker::located_hand xr::hand_tracker::locate(XrSpace space, XrTime time) { if (!id || !xrLocateHandJointsEXT) - return std::nullopt; + return {}; XrHandJointsLocateInfoEXT info{ .type = XR_TYPE_HAND_JOINTS_LOCATE_INFO_EXT, @@ -107,29 +108,33 @@ std::optional xr::hand_tracker::locate(XrSpace s CHECK_XR(xrLocateHandJointsEXT(id, &info, &locations)); - if (!locations.isActive) - return std::nullopt; + located_hand res{}; - if (hand_tracking_data_source_supported && !data_source_state.isActive) - // returned joints are not valid - return std::nullopt; - - // bail if any of the joint is invalid - if (std::ranges::any_of(joints_pos, [](const auto & loc) { return loc.locationFlags == 0; })) - return std::nullopt; - - joint_array joints; - for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) + if (locations.isActive) { - joints[i] = {joints_pos[i], joints_vel[i]}; + res.joints.emplace(); + auto & joints = *res.joints; + for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) + { + if (joints_pos[i].locationFlags == 0) + { + // bail if any of the joint is invalid + res.joints.reset(); + break; + } + joints[i] = {joints_pos[i], joints_vel[i]}; + } } - return located_hand{ - .joints = joints, - .data_source = hand_tracking_data_source_supported - ? std::optional{data_source_state.dataSource} - : std::nullopt, - }; + if (hand_tracking_data_source_supported) + { + res.data_source = data_source_state.dataSource; + if (not data_source_state.isActive) + // returned joints are not valid + res.joints.reset(); + } + + return res; } const xr::hand_tracker::mesh_data * xr::hand_tracker::mesh() diff --git a/client/xr/hand_tracker.h b/client/xr/hand_tracker.h index 2e3a4f3d..fdc1c9ea 100644 --- a/client/xr/hand_tracker.h +++ b/client/xr/hand_tracker.h @@ -39,7 +39,7 @@ public: struct located_hand { - joint_array joints; + std::optional joints; std::optional data_source; // should these joints be used for input? @@ -69,13 +69,19 @@ private: bool hand_tracking_data_source_supported = false; std::optional cached_hand_mesh_fb; bool hand_mesh_fb_fetched = false; + XrHandEXT hand_; public: hand_tracker(instance & inst, session & session, const XrHandTrackerCreateInfoEXT & info); - std::optional locate(XrSpace space, XrTime time); + located_hand locate(XrSpace space, XrTime time); const mesh_data * mesh(); static bool check_flags(const joint_array & joints, XrSpaceLocationFlags position, XrSpaceVelocityFlags velocity); + + XrHandEXT hand() const + { + return hand_; + } }; } // namespace xr diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index b58ce939..714f941d 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -479,6 +479,10 @@ struct hand_tracking { left, right, + + // with data_source + left_controller, + right_controller, }; struct pose { diff --git a/server/driver/hand_joints_list.cpp b/server/driver/hand_joints_list.cpp index 02bc8384..afc43cee 100644 --- a/server/driver/hand_joints_list.cpp +++ b/server/driver/hand_joints_list.cpp @@ -100,7 +100,7 @@ static xrt_hand_joint_set convert_joints(const std::optional { + const from_headset::hand_tracking::hand_id hand; + public: - const int hand_id; + hand_joints_list(from_headset::hand_tracking::hand_id hand) : + hand{hand} {} xrt_hand_joint_set interpolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, float t); xrt_hand_joint_set extrapolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, int64_t ta, int64_t tb, int64_t t); - hand_joints_list(int hand_id) : - hand_id(hand_id) {} - void update_tracking(const wivrn::from_headset::hand_tracking & tracking, const clock_offset & offset); }; } // namespace wivrn diff --git a/server/driver/wivrn_controller.cpp b/server/driver/wivrn_controller.cpp index 64094f00..6b4b9762 100644 --- a/server/driver/wivrn_controller.cpp +++ b/server/driver/wivrn_controller.cpp @@ -738,7 +738,8 @@ wivrn_controller::wivrn_controller(xrt_device_name name, palm(hand_id == 0 ? device_id::LEFT_PALM : device_id::RIGHT_PALM), pinch_ext(hand_id == 0 ? device_id::LEFT_PINCH_POSE : device_id::RIGHT_PINCH_POSE), poke_ext(hand_id == 0 ? device_id::LEFT_POKE : device_id::RIGHT_POKE), - joints(hand_id), + joints_unobstructed(hand_id == 0 ? from_headset::hand_tracking::hand_id::left : from_headset::hand_tracking::hand_id::right), + joints_conforming(hand_id == 0 ? from_headset::hand_tracking::hand_id::left_controller : from_headset::hand_tracking::hand_id::right_controller), inputs_array(input_count, xrt_input{}), cnx(cnx) { @@ -971,12 +972,15 @@ xrt_result_t wivrn_controller::get_hand_tracking(xrt_input_name name, int64_t de { switch (name) { + case XRT_INPUT_HT_CONFORMING_LEFT: + case XRT_INPUT_HT_CONFORMING_RIGHT: case XRT_INPUT_HT_UNOBSTRUCTED_LEFT: case XRT_INPUT_HT_UNOBSTRUCTED_RIGHT: { + auto & joints = (name == XRT_INPUT_HT_CONFORMING_LEFT or name == XRT_INPUT_HT_CONFORMING_RIGHT) ? joints_conforming : joints_unobstructed; *out_timestamp_ns = desired_timestamp_ns; XrTime production_timestamp; std::tie(production_timestamp, *out_value) = joints.get_at(desired_timestamp_ns); - cnx->add_tracking_request(joints.hand_id == 0 ? device_id::LEFT_HAND : device_id::RIGHT_HAND, desired_timestamp_ns, production_timestamp); + cnx->add_tracking_request((name == XRT_INPUT_HT_CONFORMING_LEFT or name == XRT_INPUT_HT_UNOBSTRUCTED_LEFT) ? device_id::LEFT_HAND : device_id::RIGHT_HAND, desired_timestamp_ns, production_timestamp); return XRT_SUCCESS; } @@ -1013,7 +1017,8 @@ void wivrn_controller::update_tracking(const from_headset::tracking & tracking, void wivrn_controller::update_hand_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset) { - joints.update_tracking(tracking, offset); + joints_unobstructed.update_tracking(tracking, offset); + joints_conforming.update_tracking(tracking, offset); } xrt_result_t wivrn_controller::set_output(xrt_output_name name, const xrt_output_value * value) diff --git a/server/driver/wivrn_controller.h b/server/driver/wivrn_controller.h index a77f701b..be0a3380 100644 --- a/server/driver/wivrn_controller.h +++ b/server/driver/wivrn_controller.h @@ -41,7 +41,8 @@ class wivrn_controller : public xrt_device pose_list palm; pose_list pinch_ext; pose_list poke_ext; - hand_joints_list joints; + hand_joints_list joints_unobstructed; + hand_joints_list joints_conforming; std::vector inputs_staging; std::vector inputs_array; diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index ce4e67a4..83bfede6 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -123,11 +123,15 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr connection static_roles.face = &hmd; roles.left = left_controller_index = static_xdev_count++; - static_roles.hand_tracking.unobstructed.left = static_xdevs[left_controller_index] = &left_controller; + static_roles.hand_tracking.conforming.left = + static_roles.hand_tracking.unobstructed.left = + static_xdevs[left_controller_index] = &left_controller; static_xdevs[left_hand_interaction_index = static_xdev_count++] = &left_hand_interaction; roles.right = right_controller_index = static_xdev_count++; - static_roles.hand_tracking.unobstructed.right = static_xdevs[right_controller_index] = &right_controller; + static_roles.hand_tracking.conforming.right = + static_roles.hand_tracking.unobstructed.right = + static_xdevs[right_controller_index] = &right_controller; static_xdevs[right_hand_interaction_index = static_xdev_count++] = &right_hand_interaction; // Expose a gamepad forwarded from the headset as a native OpenXR device (/user/gamepad).