Add hand tracking

This commit is contained in:
Guillaume Meunier
2024-02-25 16:09:32 +01:00
parent 91f6dc08f0
commit a2271d486a
23 changed files with 615 additions and 93 deletions
+15 -1
View File
@@ -725,6 +725,7 @@ void application::initialize()
std::vector<std::string> 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;
+20
View File
@@ -19,6 +19,7 @@
#pragma once
#include "xr/hand_tracker.h"
#ifdef __ANDROID__
#include <android_native_app_glue.h>
#endif
@@ -118,6 +119,10 @@ class application : public singleton<application>
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;
}
};
+3
View File
@@ -35,6 +35,7 @@
#include <vulkan/vulkan_core.h>
#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> scenes::stream::create(std::unique_ptr<wivrn_ses
if (info.available_refresh_rates.empty())
spdlog::warn("Unable to detect refresh rates");
info.hand_tracking = application::get_hand_tracking_supported();
audio::get_audio_description(info);
self->network_session->send_control(info);
+47
View File
@@ -63,6 +63,42 @@ static from_headset::tracking::pose locate_space(device_id device, XrSpace space
return res;
}
static std::array<from_headset::hand_tracking::pose, XR_HAND_JOINT_COUNT_EXT> locate_hands(xr::hand_tracker& hand, XrSpace space, XrTime time)
{
std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> joints = hand.locate(space, time);
std::array<from_headset::hand_tracking::pose, XR_HAND_JOINT_COUNT_EXT> 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<std::pair<device_id, XrSpace>> 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)
{
+61
View File
@@ -0,0 +1,61 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "hand_tracker.h"
std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> xr::hand_tracker::locate(XrSpace space, XrTime time)
{
if (!xrLocateHandJointsEXT)
xrLocateHandJointsEXT = inst->get_proc<PFN_xrLocateHandJointsEXT>("xrLocateHandJointsEXT");
XrHandJointsLocateInfoEXT info{
.type = XR_TYPE_HAND_JOINTS_LOCATE_INFO_EXT,
.next = nullptr,
.baseSpace = space,
.time = time,
};
std::array<XrHandJointLocationEXT, XR_HAND_JOINT_COUNT_EXT> joints_pos;
std::array<XrHandJointVelocityEXT, XR_HAND_JOINT_COUNT_EXT> 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<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> joints;
for(int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++)
{
joints[i] = { joints_pos[i], joints_vel[i] };
}
return joints;
}
+58
View File
@@ -0,0 +1,58 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "utils/handle.h"
#include <openxr/openxr.h>
#include <array>
#include <utility>
#include "instance.h"
namespace xr
{
class hand_tracker : public utils::handle<XrHandTrackerEXT>
{
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<PFN_xrDestroyHandTrackerEXT>("xrDestroyHandTrackerEXT");
if (id != XR_NULL_HANDLE && xrDestroyHandTrackerEXT)
xrDestroyHandTrackerEXT(id);
}
using joint = std::pair<XrHandJointLocationEXT, XrHandJointVelocityEXT>;
std::array<joint, XR_HAND_JOINT_COUNT_EXT> locate(XrSpace space, XrTime time);
};
} // namespace xr
+20
View File
@@ -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<PFN_xrCreateHandTrackerEXT>("xrCreateHandTrackerEXT");
assert(xrCreateHandTrackerEXT);
CHECK_XR(xrCreateHandTrackerEXT(id, &create_info, &ht));
return {inst, ht};
}
std::vector<vk::Format> xr::session::get_swapchain_formats() const
{
std::vector<vk::Format> formats;
+2
View File
@@ -27,6 +27,7 @@
#include <openxr/openxr.h>
#include "space.h"
#include "hand_tracker.h"
namespace xr
{
@@ -48,6 +49,7 @@ public:
std::vector<XrReferenceSpaceType> 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<vk::Format> get_swapchain_formats() const;
+19
View File
@@ -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 =
+2
View File
@@ -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;