diff --git a/client/application.cpp b/client/application.cpp index cefde89a..cbbbd154 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -725,6 +725,7 @@ void application::initialize() std::vector opt_extensions; opt_extensions.push_back(XR_KHR_COMPOSITION_LAYER_COLOR_SCALE_BIAS_EXTENSION_NAME); opt_extensions.push_back(XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME); + opt_extensions.push_back(XR_EXT_HAND_TRACKING_EXTENSION_NAME); for(const auto& i: interaction_profiles) { @@ -770,7 +771,6 @@ void application::initialize() spdlog::info(" Orientation tracking: {}", (bool)properties.trackingProperties.orientationTracking); spdlog::info(" Position tracking: {}", (bool)properties.trackingProperties.positionTracking); - // Log view configurations and blend modes log_views(); @@ -789,6 +789,20 @@ void application::initialize() world_space = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_STAGE); // world_space = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_LOCAL); + if (utils::contains(xr_extensions, XR_EXT_HAND_TRACKING_EXTENSION_NAME)) + { + XrSystemHandTrackingPropertiesEXT hand_tracking_properties = xr_system_id.hand_tracking_properties(); + spdlog::info("Hand tracking support: {}", (bool)hand_tracking_properties.supportsHandTracking); + hand_tracking_supported = hand_tracking_properties.supportsHandTracking; + + if (hand_tracking_supported) + { + left_hand = xr_session.create_hand_tracker(XR_HAND_LEFT_EXT); + right_hand = xr_session.create_hand_tracker(XR_HAND_RIGHT_EXT); + } + } + + vk::CommandPoolCreateInfo cmdpool_create_info; cmdpool_create_info.queueFamilyIndex = vk_queue_family_index; cmdpool_create_info.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer; diff --git a/client/application.h b/client/application.h index 640e19e0..91d41c09 100644 --- a/client/application.h +++ b/client/application.h @@ -19,6 +19,7 @@ #pragma once +#include "xr/hand_tracker.h" #ifdef __ANDROID__ #include #endif @@ -118,6 +119,10 @@ class application : public singleton xr::space right_grip_space; xr::space right_aim_space; + bool hand_tracking_supported = false; + xr::hand_tracker left_hand; + xr::hand_tracker right_hand; + bool session_running = false; bool session_focused = false; bool session_visible = false; @@ -379,4 +384,19 @@ public: { return instance().last_scene_gpu_time; } + + static bool get_hand_tracking_supported() + { + return instance().hand_tracking_supported; + } + + static xr::hand_tracker& get_left_hand() + { + return instance().left_hand; + } + + static xr::hand_tracker& get_right_hand() + { + return instance().right_hand; + } }; diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 24472de7..2e60aa52 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -35,6 +35,7 @@ #include #include "audio/audio.h" #include "hardware.h" +#include "utils/contains.h" using namespace xrt::drivers::wivrn; @@ -108,6 +109,8 @@ std::shared_ptr scenes::stream::create(std::unique_ptrnetwork_session->send_control(info); diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index 48593965..61ad1922 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -63,6 +63,42 @@ static from_headset::tracking::pose locate_space(device_id device, XrSpace space return res; } +static std::array locate_hands(xr::hand_tracker& hand, XrSpace space, XrTime time) +{ + std::array joints = hand.locate(space, time); + + std::array poses; + for(int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) + { + poses[i] = { + .pose = joints[i].first.pose, + .linear_velocity = joints[i].second.linearVelocity, + .angular_velocity = joints[i].second.angularVelocity, + .radius = joints[i].first.radius, + }; + + if (joints[i].first.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) + poses[i].flags |= from_headset::hand_tracking::orientation_valid; + + if (joints[i].first.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) + poses[i].flags |= from_headset::hand_tracking::position_valid; + + if (joints[i].second.velocityFlags & XR_SPACE_VELOCITY_LINEAR_VALID_BIT) + poses[i].flags |= from_headset::hand_tracking::linear_velocity_valid; + + if (joints[i].second.velocityFlags & XR_SPACE_VELOCITY_ANGULAR_VALID_BIT) + poses[i].flags |= from_headset::hand_tracking::angular_velocity_valid; + + if (joints[i].first.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT) + poses[i].flags |= from_headset::hand_tracking::orientation_tracked; + + if (joints[i].first.locationFlags & XR_SPACE_LOCATION_POSITION_TRACKED_BIT) + poses[i].flags |= from_headset::hand_tracking::position_tracked; + } + + return poses; +} + void scenes::stream::tracking() { std::vector> spaces = { @@ -91,7 +127,10 @@ void scenes::stream::tracking() for (XrTime Δt = 0; Δt < extrapolation_horizon; Δt += tracking_period) { from_headset::tracking packet{}; + from_headset::hand_tracking hands{}; + packet.timestamp = t0 + Δt; + hands.timestamp = t0 + Δt; try { @@ -112,6 +151,14 @@ void scenes::stream::tracking() } network_session->send_stream(packet); + + if (application::get_hand_tracking_supported()) + { + hands.left = locate_hands(application::get_left_hand(), world_space, hands.timestamp); + hands.right = locate_hands(application::get_right_hand(), world_space, hands.timestamp); + + network_session->send_stream(hands); + } } catch (const std::system_error & e) { diff --git a/client/xr/hand_tracker.cpp b/client/xr/hand_tracker.cpp new file mode 100644 index 00000000..a5f602b5 --- /dev/null +++ b/client/xr/hand_tracker.cpp @@ -0,0 +1,61 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * + * 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 . + */ + +#include "hand_tracker.h" + + +std::array xr::hand_tracker::locate(XrSpace space, XrTime time) +{ + if (!xrLocateHandJointsEXT) + xrLocateHandJointsEXT = inst->get_proc("xrLocateHandJointsEXT"); + + XrHandJointsLocateInfoEXT info{ + .type = XR_TYPE_HAND_JOINTS_LOCATE_INFO_EXT, + .next = nullptr, + .baseSpace = space, + .time = time, + }; + + std::array joints_pos; + std::array joints_vel; + + XrHandJointVelocitiesEXT velocities{ + .type = XR_TYPE_HAND_JOINT_VELOCITIES_EXT, + .next = nullptr, + .jointCount = joints_vel.size(), + .jointVelocities = joints_vel.data(), + }; + + XrHandJointLocationsEXT locations{ + .type = XR_TYPE_HAND_JOINT_LOCATIONS_EXT, + .next = &velocities, + .jointCount = joints_pos.size(), + .jointLocations = joints_pos.data(), + }; + + CHECK_XR(xrLocateHandJointsEXT(id, &info, &locations)); + + std::array joints; + for(int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) + { + joints[i] = { joints_pos[i], joints_vel[i] }; + } + + return joints; +} diff --git a/client/xr/hand_tracker.h b/client/xr/hand_tracker.h new file mode 100644 index 00000000..40325ae8 --- /dev/null +++ b/client/xr/hand_tracker.h @@ -0,0 +1,58 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * + * 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 . + */ + +#pragma once + +#include "utils/handle.h" +#include +#include +#include + +#include "instance.h" + +namespace xr +{ +class hand_tracker : public utils::handle +{ + instance * inst; + PFN_xrLocateHandJointsEXT xrLocateHandJointsEXT{}; + +public: + hand_tracker() = default; + hand_tracker(instance * i, XrHandTrackerEXT h) + { + inst = i; + id = h; + } + hand_tracker(hand_tracker &&) = default; + hand_tracker & operator=(hand_tracker &&) = default; + + ~hand_tracker() + { + auto xrDestroyHandTrackerEXT = inst->get_proc("xrDestroyHandTrackerEXT"); + + if (id != XR_NULL_HANDLE && xrDestroyHandTrackerEXT) + xrDestroyHandTrackerEXT(id); + } + + using joint = std::pair; + + std::array locate(XrSpace space, XrTime time); +}; +} // namespace xr diff --git a/client/xr/session.cpp b/client/xr/session.cpp index 7dd2d39d..e33ede02 100644 --- a/client/xr/session.cpp +++ b/client/xr/session.cpp @@ -76,6 +76,26 @@ xr::space xr::session::create_action_space(XrAction action, const XrPosef & pose return s; } + +xr::hand_tracker xr::session::create_hand_tracker(XrHandEXT hand, XrHandJointSetEXT hand_joint_set) +{ + XrHandTrackerCreateInfoEXT create_info + { + .type = XR_TYPE_HAND_TRACKER_CREATE_INFO_EXT, + .next = nullptr, + .hand = hand, + .handJointSet = hand_joint_set, + }; + + XrHandTrackerEXT ht; + + auto xrCreateHandTrackerEXT = inst->get_proc("xrCreateHandTrackerEXT"); + assert(xrCreateHandTrackerEXT); + + CHECK_XR(xrCreateHandTrackerEXT(id, &create_info, &ht)); + return {inst, ht}; +} + std::vector xr::session::get_swapchain_formats() const { std::vector formats; diff --git a/client/xr/session.h b/client/xr/session.h index 5688f0ff..036aaa5d 100644 --- a/client/xr/session.h +++ b/client/xr/session.h @@ -27,6 +27,7 @@ #include #include "space.h" +#include "hand_tracker.h" namespace xr { @@ -48,6 +49,7 @@ public: std::vector get_reference_spaces() const; space create_reference_space(XrReferenceSpaceType ref, const XrPosef & pose = {{0, 0, 0, 1}, {0, 0, 0}}); 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); std::vector get_swapchain_formats() const; diff --git a/client/xr/system.cpp b/client/xr/system.cpp index 21f5c07e..0250c27e 100644 --- a/client/xr/system.cpp +++ b/client/xr/system.cpp @@ -63,6 +63,25 @@ XrSystemProperties xr::system::properties() const return prop; } +XrSystemHandTrackingPropertiesEXT xr::system::hand_tracking_properties() const +{ + if (!id) + throw std::invalid_argument("this"); + + XrSystemHandTrackingPropertiesEXT hand_tracking_prop + { + .type = XR_TYPE_SYSTEM_HAND_TRACKING_PROPERTIES_EXT, + }; + + XrSystemProperties prop{ + .type = XR_TYPE_SYSTEM_PROPERTIES, + .next = &hand_tracking_prop, + }; + CHECK_XR(xrGetSystemProperties(*inst, id, &prop)); + + return hand_tracking_prop; +} + vk::raii::PhysicalDevice xr::system::physical_device(vk::raii::Instance& vulkan) const { auto xrGetVulkanGraphicsDevice2KHR = diff --git a/client/xr/system.h b/client/xr/system.h index 085247af..97dad9fa 100644 --- a/client/xr/system.h +++ b/client/xr/system.h @@ -54,6 +54,8 @@ public: } XrSystemProperties properties() const; + XrSystemHandTrackingPropertiesEXT hand_tracking_properties() const; + XrGraphicsRequirementsVulkan2KHR graphics_requirements() const; vk::raii::PhysicalDevice physical_device(vk::raii::Instance& vulkan) const; vk::raii::Device create_device(vk::raii::PhysicalDevice& pdev, vk::DeviceCreateInfo & create_info) const; diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index 3a8fbeb8..bb081311 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -105,6 +105,7 @@ struct headset_info_packet std::optional speaker; std::optional microphone; std::array fov; + bool hand_tracking; }; struct handshake @@ -146,6 +147,32 @@ struct tracking std::vector device_poses; }; +struct hand_tracking +{ + enum flags : uint8_t + { + orientation_valid = 1 << 0, + position_valid = 1 << 1, + linear_velocity_valid = 1 << 2, + angular_velocity_valid = 1 << 3, + orientation_tracked = 1 << 4, + position_tracked = 1 << 5 + }; + + struct pose + { + XrPosef pose; + XrVector3f linear_velocity; + XrVector3f angular_velocity; + float radius; + uint8_t flags; + }; + + uint64_t timestamp; + std::array left; + std::array right; +}; + struct inputs { struct input_value @@ -183,7 +210,7 @@ struct feedback uint8_t received_data_packets; }; -using packets = std::variant; +using packets = std::variant; } // namespace from_headset namespace to_headset diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 1b3ab745..4baa5950 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -78,6 +78,7 @@ add_executable(wivrn-server driver/wivrn_controller.cpp driver/pose_list.cpp driver/view_list.cpp + driver/hand_joints_list.cpp driver/wivrn_session.cpp driver/wivrn_connection.cpp driver/xrt_cast.cpp diff --git a/server/driver/hand_joints_list.cpp b/server/driver/hand_joints_list.cpp new file mode 100644 index 00000000..632a03a6 --- /dev/null +++ b/server/driver/hand_joints_list.cpp @@ -0,0 +1,123 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * + * 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 . + */ + +#include "hand_joints_list.h" +#include "pose_list.h" +#include "xrt_cast.h" + +using namespace xrt::drivers::wivrn; + +static_assert(XRT_HAND_JOINT_COUNT == XR_HAND_JOINT_COUNT_EXT); + +namespace +{ + xrt_hand_joint_value interpolate(const xrt_hand_joint_value & a, const xrt_hand_joint_value & b, float t) + { + return { + .relation = pose_list::interpolate(a.relation, b.relation, t), + .radius = a.radius * (1-t) + b.radius * t + }; + } + + xrt_hand_joint_value extrapolate(const xrt_hand_joint_value & a, const xrt_hand_joint_value & b, uint64_t ta, uint64_t tb, uint64_t t) + { + float λ = std::clamp((t - ta) / float(tb - ta), 0.f, 1.f); + + return { + .relation = pose_list::extrapolate(a.relation, b.relation, ta, tb, t), + .radius = a.radius * (1-λ) + b.radius * λ + }; + } +} + +xrt_hand_joint_set hand_joints_list::interpolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, float t) +{ + xrt_hand_joint_set j = a; + for(int i = 0; i < XRT_HAND_JOINT_COUNT; i++) + { + j.values.hand_joint_set_default[i] = ::interpolate(a.values.hand_joint_set_default[i], b.values.hand_joint_set_default[i], t); + } + return j; +} + +xrt_hand_joint_set hand_joints_list::extrapolate(const xrt_hand_joint_set & a, const xrt_hand_joint_set & b, uint64_t ta, uint64_t tb, uint64_t t) +{ + xrt_hand_joint_set j = a; + for(int i = 0; i < XRT_HAND_JOINT_COUNT; i++) + { + j.values.hand_joint_set_default[i] = ::extrapolate(a.values.hand_joint_set_default[i], b.values.hand_joint_set_default[i], ta, tb, t); + } + return j; +} + +static xrt_hand_joint_set convert_joints(const std::array& input_joints) +{ + xrt_hand_joint_set output_joints; + + output_joints.is_active = true; + output_joints.hand_pose = xrt_space_relation{ + .relation_flags = XRT_SPACE_RELATION_BITMASK_ALL, + .pose = { + .orientation = {0, 0, 0, 1}, + .position = {0, 0, 0} + }, + .linear_velocity = {0, 0, 0}, + .angular_velocity = {0, 0, 0}, + }; // TODO + + for(int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) + { + xrt_hand_joint_value& res = output_joints.values.hand_joint_set_default[i]; + + res.radius = input_joints[i].radius; + res.relation.pose = xrt_cast(input_joints[i].pose); + res.relation.angular_velocity = xrt_cast(input_joints[i].angular_velocity); + res.relation.linear_velocity = xrt_cast(input_joints[i].linear_velocity); + + int flags = 0; + if (input_joints[i].flags & from_headset::hand_tracking::position_valid) + flags |= XRT_SPACE_RELATION_POSITION_VALID_BIT; + + if (input_joints[i].flags & from_headset::hand_tracking::orientation_valid) + flags |= XRT_SPACE_RELATION_ORIENTATION_VALID_BIT; + + if (input_joints[i].flags & from_headset::hand_tracking::linear_velocity_valid) + flags |= XRT_SPACE_RELATION_LINEAR_VELOCITY_VALID_BIT; + + if (input_joints[i].flags & from_headset::hand_tracking::angular_velocity_valid) + flags |= XRT_SPACE_RELATION_ANGULAR_VELOCITY_VALID_BIT; + + if (input_joints[i].flags & from_headset::hand_tracking::position_tracked) + flags |= XRT_SPACE_RELATION_POSITION_TRACKED_BIT; + + if (input_joints[i].flags & from_headset::hand_tracking::orientation_tracked) + flags |= XRT_SPACE_RELATION_ORIENTATION_TRACKED_BIT; + + res.relation.relation_flags = (xrt_space_relation_flags)flags; + } + return output_joints; +} + +void hand_joints_list::update_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset) +{ + if (hand_id == 0) + add_sample(tracking.timestamp, convert_joints(tracking.left), offset); + else + add_sample(tracking.timestamp, convert_joints(tracking.right), offset); +} diff --git a/server/driver/hand_joints_list.h b/server/driver/hand_joints_list.h new file mode 100644 index 00000000..8c4b20c8 --- /dev/null +++ b/server/driver/hand_joints_list.h @@ -0,0 +1,37 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * + * 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 . + */ + +#pragma once + +#include "history.h" +#include "xrt/xrt_defines.h" + + +class hand_joints_list : public history +{ + int hand_id; + +public: + 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); +}; diff --git a/server/driver/history.h b/server/driver/history.h new file mode 100644 index 00000000..875fc9de --- /dev/null +++ b/server/driver/history.h @@ -0,0 +1,96 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022-2024 Guillaume Meunier + * Copyright (C) 2022-2024 Patrick Nicolas + * + * 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 . + */ + +#pragma once + +#include +#include +#include +#include +#include +#include "clock_offset.h" + +template +class history +{ + struct TimedData : public Data + { + TimedData(const Data & d, uint64_t t) : + Data(d), at_timestamp_ns(t) {} + uint64_t at_timestamp_ns; + }; + + std::mutex mutex; + std::vector data; + +protected: + void add_sample(uint64_t timestamp, const Data & sample, const clock_offset & offset) + { + std::lock_guard lock(mutex); + + uint64_t t = offset.from_headset(timestamp); + auto it = std::lower_bound(data.begin(), data.end(), t, [](TimedData & sample, uint64_t t) { return sample.at_timestamp_ns < t; }); + + if (it == data.end()) + data.emplace_back(sample, t); + else if (it->at_timestamp_ns == t) + *it = TimedData(sample, t); + else + data.emplace(it, sample, t); + + if (data.size() > MaxSamples) + data.erase(data.begin()); + } + +public: + Data get_at(uint64_t at_timestamp_ns) + { + std::lock_guard lock(mutex); + + if (data.empty()) + { + return {}; + } + + if (data.size() == 1) + { + return data[0]; + } + + if (data.front().at_timestamp_ns > at_timestamp_ns) + { + return Derived::extrapolate(data[0], data[1], data[0].at_timestamp_ns, data[1].at_timestamp_ns, at_timestamp_ns); + } + + for (size_t i = 1; i < data.size(); ++i) + { + if (data[i].at_timestamp_ns > at_timestamp_ns) + { + float t = float(data[i].at_timestamp_ns - at_timestamp_ns) / + (data[i].at_timestamp_ns - data[i - 1].at_timestamp_ns); + return Derived::interpolate(data[i - 1], data[i], t); + } + } + + const auto & d0 = data[data.size() - 2]; + const auto & d1 = data.back(); + + return Derived::extrapolate(d0, d1, d0.at_timestamp_ns, d1.at_timestamp_ns, at_timestamp_ns); + } +}; diff --git a/server/driver/pose_list.cpp b/server/driver/pose_list.cpp index 96c69890..5ae24b31 100644 --- a/server/driver/pose_list.cpp +++ b/server/driver/pose_list.cpp @@ -27,7 +27,7 @@ using namespace xrt::auxiliary::math; using namespace xrt::drivers::wivrn; -xrt_space_relation interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t) +xrt_space_relation pose_list::interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t) { xrt_space_relation result; xrt_space_relation_flags flags = xrt_space_relation_flags(a.relation_flags & b.relation_flags); @@ -35,7 +35,7 @@ xrt_space_relation interpolate(const xrt_space_relation & a, const xrt_space_rel return result; } -xrt_space_relation extrapolate(const xrt_space_relation & a, const xrt_space_relation & b, uint64_t ta, uint64_t tb, uint64_t t) +xrt_space_relation pose_list::extrapolate(const xrt_space_relation & a, const xrt_space_relation & b, uint64_t ta, uint64_t tb, uint64_t t) { if (t < ta) return a; diff --git a/server/driver/pose_list.h b/server/driver/pose_list.h index 5a556749..eb5c175a 100644 --- a/server/driver/pose_list.h +++ b/server/driver/pose_list.h @@ -21,89 +21,18 @@ #include "wivrn_packets.h" #include "clock_offset.h" +#include "history.h" #include "xrt/xrt_defines.h" -#include -#include -#include -template -class history -{ - struct TimedData : public Data - { - TimedData(const Data & d, uint64_t t) : - Data(d), at_timestamp_ns(t) {} - uint64_t at_timestamp_ns; - }; - std::mutex mutex; - std::vector data; - -protected: - void add_sample(uint64_t timestamp, const Data & sample, const clock_offset & offset) - { - std::lock_guard lock(mutex); - - uint64_t t = offset.from_headset(timestamp); - auto it = std::lower_bound(data.begin(), data.end(), t, [](TimedData & sample, uint64_t t) { return sample.at_timestamp_ns < t; }); - - if (it == data.end()) - data.emplace_back(sample, t); - else if (it->at_timestamp_ns == t) - *it = TimedData(sample, t); - else - data.emplace(it, sample, t); - - if (data.size() > MaxSamples) - data.erase(data.begin()); - } - -public: - Data get_at(uint64_t at_timestamp_ns) - { - std::lock_guard lock(mutex); - - if (data.empty()) - { - return {}; - } - - if (data.size() == 1) - { - return data[0]; - } - - if (data.front().at_timestamp_ns > at_timestamp_ns) - { - return extrapolate(data[0], data[1], data[0].at_timestamp_ns, data[1].at_timestamp_ns, at_timestamp_ns); - } - - for (size_t i = 1; i < data.size(); ++i) - { - if (data[i].at_timestamp_ns > at_timestamp_ns) - { - float t = float(data[i].at_timestamp_ns - at_timestamp_ns) / - (data[i].at_timestamp_ns - data[i - 1].at_timestamp_ns); - return interpolate(data[i - 1], data[i], t); - } - } - - const auto & d0 = data[data.size() - 2]; - const auto & d1 = data.back(); - - return extrapolate(d0, d1, d0.at_timestamp_ns, d1.at_timestamp_ns, at_timestamp_ns); - } -}; - -xrt_space_relation interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t); - -xrt_space_relation extrapolate(const xrt_space_relation & a, const xrt_space_relation & b, uint64_t ta, uint64_t tb, uint64_t t); - -class pose_list : public history +class pose_list : public history { xrt::drivers::wivrn::device_id device; public: + 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) {} diff --git a/server/driver/view_list.cpp b/server/driver/view_list.cpp index 1d80c2a0..a0b5d0bb 100644 --- a/server/driver/view_list.cpp +++ b/server/driver/view_list.cpp @@ -21,17 +21,17 @@ #include "pose_list.h" #include "xrt_cast.h" -tracked_views interpolate(const tracked_views & a, const tracked_views & b, float t) +tracked_views view_list::interpolate(const tracked_views & a, const tracked_views & b, float t) { tracked_views result = a; - result.relation = interpolate(a.relation, b.relation, t); + result.relation = pose_list::interpolate(a.relation, b.relation, t); return result; } -tracked_views extrapolate(const tracked_views & a, const tracked_views & b, uint64_t ta, uint64_t tb, uint64_t t) +tracked_views view_list::extrapolate(const tracked_views & a, const tracked_views & b, uint64_t ta, uint64_t tb, uint64_t t) { tracked_views result = a; - result.relation = extrapolate(a.relation, b.relation, ta, tb, t); + result.relation = pose_list::extrapolate(a.relation, b.relation, ta, tb, t); return result; } diff --git a/server/driver/view_list.h b/server/driver/view_list.h index d75a402d..d033d8b6 100644 --- a/server/driver/view_list.h +++ b/server/driver/view_list.h @@ -30,12 +30,12 @@ struct tracked_views std::array fovs; }; -tracked_views interpolate(const tracked_views & a, const tracked_views & b, float t); -tracked_views extrapolate(const tracked_views & a, const tracked_views & b, uint64_t ta, uint64_t tb, uint64_t t); - -class view_list : public history +class view_list : public history { 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); }; diff --git a/server/driver/wivrn_controller.cpp b/server/driver/wivrn_controller.cpp index 4bfba5db..12220c32 100644 --- a/server/driver/wivrn_controller.cpp +++ b/server/driver/wivrn_controller.cpp @@ -34,6 +34,8 @@ enum wivrn_controller_input_index { WIVRN_CONTROLLER_AIM_POSE, WIVRN_CONTROLLER_GRIP_POSE, + WIVRN_CONTROLLER_HAND_TRACKER, + WIVRN_CONTROLLER_MENU_CLICK, // /user/hand/left/input/menu/click WIVRN_CONTROLLER_A_CLICK, // /user/hand/right/input/a/click WIVRN_CONTROLLER_A_TOUCH, // /user/hand/right/input/a/touch @@ -136,6 +138,12 @@ static void wivrn_controller_get_tracked_pose(xrt_device * xdev, uint64_t at_timestamp_ns, xrt_space_relation * out_relation); +static void wivrn_controller_get_hand_tracking(xrt_device * xdev, + xrt_input_name name, + uint64_t desired_timestamp_ns, + xrt_hand_joint_set * out_value, + uint64_t *out_timestamp_ns); + static void wivrn_controller_set_output(struct xrt_device * xdev, enum xrt_output_name name, const union xrt_output_value * value); static void wivrn_controller_update_inputs(xrt_device * xdev); @@ -143,19 +151,23 @@ static void wivrn_controller_update_inputs(xrt_device * xdev); wivrn_controller::wivrn_controller(int hand_id, xrt_device * hmd, std::shared_ptr cnx) : - xrt_device{}, grip(hand_id == 0 ? device_id::LEFT_GRIP : device_id::RIGHT_GRIP), aim(hand_id == 0 ? device_id::LEFT_AIM : device_id::RIGHT_AIM), cnx(cnx) + xrt_device{}, + grip(hand_id == 0 ? device_id::LEFT_GRIP : device_id::RIGHT_GRIP), + aim(hand_id == 0 ? device_id::LEFT_AIM : device_id::RIGHT_AIM), + joints(hand_id), + cnx(cnx) { xrt_device * base = this; base->destroy = wivrn_controller_destroy; base->get_tracked_pose = wivrn_controller_get_tracked_pose; - base->get_hand_tracking = NULL; // TODO + base->get_hand_tracking = wivrn_controller_get_hand_tracking; base->set_output = wivrn_controller_set_output; base->update_inputs = wivrn_controller_update_inputs; base->name = XRT_DEVICE_TOUCH_CONTROLLER; base->orientation_tracking_supported = true; - base->hand_tracking_supported = false; + base->hand_tracking_supported = true; base->position_tracking_supported = true; base->tracking_origin = hmd->tracking_origin; @@ -197,6 +209,9 @@ wivrn_controller::wivrn_controller(int hand_id, SET_INPUT(THUMBSTICK_TOUCH); SET_INPUT(THUMBREST_TOUCH); + inputs[WIVRN_CONTROLLER_HAND_TRACKER].name = hand_id == 0 ? XRT_INPUT_GENERIC_HAND_TRACKING_LEFT : XRT_INPUT_GENERIC_HAND_TRACKING_RIGHT; + inputs[WIVRN_CONTROLLER_HAND_TRACKER].active = true; + output_count = 1; outputs = &haptic_output; haptic_output.name = XRT_OUTPUT_NAME_TOUCH_HAPTIC; @@ -308,12 +323,31 @@ xrt_space_relation wivrn_controller::get_tracked_pose(xrt_input_name name, uint6 } } +std::pair wivrn_controller::get_hand_tracking(xrt_input_name name, uint64_t desired_timestamp_ns) +{ + switch(name) + { + case XRT_INPUT_GENERIC_HAND_TRACKING_LEFT: + case XRT_INPUT_GENERIC_HAND_TRACKING_RIGHT: + return { joints.get_at(desired_timestamp_ns), desired_timestamp_ns }; + + default: + U_LOG_W("Unknown input name requested"); + return {}; + } +} + void wivrn_controller::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset) { aim.update_tracking(tracking, offset); grip.update_tracking(tracking, offset); } +void wivrn_controller::update_hand_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset) +{ + joints.update_tracking(tracking, offset); +} + void wivrn_controller::set_output(xrt_output_name name, const xrt_output_value * value) { auto id = device_type == XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER ? device_id::LEFT_CONTROLLER_HAPTIC @@ -357,6 +391,15 @@ static void wivrn_controller_get_tracked_pose(xrt_device * xdev, *out_relation = static_cast(xdev)->get_tracked_pose(name, at_timestamp_ns); } +static void wivrn_controller_get_hand_tracking(xrt_device * xdev, + xrt_input_name name, + uint64_t desired_timestamp_ns, + xrt_hand_joint_set * out_value, + uint64_t *out_timestamp_ns) +{ + std::tie(*out_value, *out_timestamp_ns) = static_cast(xdev)->get_hand_tracking(name, desired_timestamp_ns); +} + static void wivrn_controller_set_output(struct xrt_device * xdev, enum xrt_output_name name, const union xrt_output_value * value) { static_cast(xdev)->set_output(name, value); diff --git a/server/driver/wivrn_controller.h b/server/driver/wivrn_controller.h index 6c7b1c49..afa5e676 100644 --- a/server/driver/wivrn_controller.h +++ b/server/driver/wivrn_controller.h @@ -22,6 +22,7 @@ #include "xrt/xrt_device.h" #include "pose_list.h" +#include "hand_joints_list.h" #include "wivrn_session.h" #include @@ -34,6 +35,7 @@ class wivrn_controller : public xrt_device pose_list grip; pose_list aim; + hand_joints_list joints; std::vector inputs_staging; std::vector inputs_array; @@ -51,14 +53,15 @@ public: void update_inputs(); xrt_space_relation get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns); + std::pair get_hand_tracking(xrt_input_name name, uint64_t desired_timestamp_ns); void set_output(xrt_output_name name, const xrt_output_value * value); void set_inputs(const from_headset::inputs &, const clock_offset &); void update_tracking(const from_headset::tracking &, const clock_offset &); + void update_hand_tracking(const from_headset::hand_tracking &, const clock_offset &); private: - void - set_inputs(device_id input_id, float value, uint64_t last_change_time); + void set_inputs(device_id input_id, float value, uint64_t last_change_time); }; diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 789cdaae..214d88b4 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -120,9 +120,15 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi if (self->hmd) usysds->base.base.static_roles.head = devices->xdevs[n++] = self->hmd.get(); if (self->left_hand) + { devices->xdevs[n++] = self->left_hand.get(); + usysds->base.base.static_roles.hand_tracking.left = self->left_hand.get(); + } if (self->right_hand) + { devices->xdevs[n++] = self->right_hand.get(); + usysds->base.base.static_roles.hand_tracking.right = self->right_hand.get(); + } devices->xdev_count = n; u_system_devices_static_finalize(usysds, self->left_hand.get(), self->right_hand.get()); @@ -179,6 +185,16 @@ void wivrn_session::operator()(from_headset::tracking && tracking) right_hand->update_tracking(tracking, offset); } +void wivrn_session::operator()(from_headset::hand_tracking && hand_tracking) +{ + auto offset = offset_est.get_offset(); + if (not offset) + return; + + left_hand->update_hand_tracking(hand_tracking, offset); + right_hand->update_hand_tracking(hand_tracking, offset); +} + void wivrn_session::operator()(from_headset::inputs && inputs) { auto offset = get_offset(); diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 53f3e5b2..d1f30e42 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -77,6 +77,7 @@ public: void operator()(from_headset::handshake&&) {} void operator()(from_headset::headset_info_packet &&); void operator()(from_headset::tracking &&); + void operator()(from_headset::hand_tracking &&); void operator()(from_headset::inputs &&); void operator()(from_headset::timesync_response &&); void operator()(from_headset::feedback &&);