diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index 0a4d9b74..03b685be 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -22,6 +22,7 @@ #include "utils/overloaded.h" #include "wivrn_packets.h" #include "xr/fb_body_tracker.h" +#include #include #include #include @@ -35,46 +36,28 @@ using tid = to_headset::tracking_control::id; static const XrDuration min_tracking_period = 2'000'000; static const XrDuration max_tracking_period = 5'000'000; -static from_headset::tracking::pose locate_space(device_id device, XrSpace space, XrSpace reference, XrTime time) +static uint8_t cast_flags(XrSpaceLocationFlags location, XrSpaceVelocityFlags velocity) { - XrSpaceVelocity velocity{ - .type = XR_TYPE_SPACE_VELOCITY, - }; + uint8_t flags = 0; + if (location & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) + flags |= from_headset::tracking::orientation_valid; - XrSpaceLocation location{ - .type = XR_TYPE_SPACE_LOCATION, - .next = &velocity, - }; + if (location & XR_SPACE_LOCATION_POSITION_VALID_BIT) + flags |= from_headset::tracking::position_valid; - xrLocateSpace(space, reference, time, &location); + if (velocity & XR_SPACE_VELOCITY_LINEAR_VALID_BIT) + flags |= from_headset::tracking::linear_velocity_valid; - from_headset::tracking::pose res{ - .pose = location.pose, - .linear_velocity = velocity.linearVelocity, - .angular_velocity = velocity.angularVelocity, - .device = device, - .flags = 0, - }; + if (velocity & XR_SPACE_VELOCITY_ANGULAR_VALID_BIT) + flags |= from_headset::tracking::angular_velocity_valid; - if (location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) - res.flags |= from_headset::tracking::orientation_valid; + if (location & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT) + flags |= from_headset::tracking::orientation_tracked; - if (location.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) - res.flags |= from_headset::tracking::position_valid; + if (location & XR_SPACE_LOCATION_POSITION_TRACKED_BIT) + flags |= from_headset::tracking::position_tracked; - if (velocity.velocityFlags & XR_SPACE_VELOCITY_LINEAR_VALID_BIT) - res.flags |= from_headset::tracking::linear_velocity_valid; - - if (velocity.velocityFlags & XR_SPACE_VELOCITY_ANGULAR_VALID_BIT) - res.flags |= from_headset::tracking::angular_velocity_valid; - - if (location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT) - res.flags |= from_headset::tracking::orientation_tracked; - - if (location.locationFlags & XR_SPACE_LOCATION_POSITION_TRACKED_BIT) - res.flags |= from_headset::tracking::position_tracked; - - return res; + return flags; } namespace @@ -92,6 +75,122 @@ public: return instance.now() - start; } }; + +from_headset::tracking::pose locate_space(device_id device, XrSpace space, XrSpace reference, XrTime time) +{ + XrSpaceVelocity velocity{ + .type = XR_TYPE_SPACE_VELOCITY, + }; + + XrSpaceLocation location{ + .type = XR_TYPE_SPACE_LOCATION, + .next = &velocity, + }; + + auto res = xrLocateSpace(space, reference, time, &location); + + if (XR_SUCCEEDED(res)) + return { + .pose = location.pose, + .linear_velocity = velocity.linearVelocity, + .angular_velocity = velocity.angularVelocity, + .device = device, + .flags = cast_flags(location.locationFlags, velocity.velocityFlags), + }; + spdlog::warn("xrLocateSpace failed for {}: {}", magic_enum::enum_name(device), xr::to_string(res)); + return {}; +} + +class locate_spaces_functor +{ + std::vector locations; + std::vector velocities; + std::vector devices; + std::vector spaces; + XrSpace reference; + PFN_xrLocateSpaces locate_spaces = nullptr; + +public: + locate_spaces_functor(xr::instance & instance, XrSpace reference) : + reference(reference) + { + if (instance.get_api_version() >= XR_MAKE_VERSION(1, 1, 0)) + { + try + { + locate_spaces = instance.get_proc("xrLocateSpaces"); + } + catch (std::exception & e) + { + spdlog::warn("Failed to load xrLocateSpaces function, fallback to xrLocateSpace"); + } + } + } + + void add_space(wivrn::device_id device, XrSpace space, XrTime t, std::vector & out) + { + if (not locate_spaces) + { + // store, will be located later + devices.push_back(device); + spaces.push_back(space); + } + else + out.push_back(locate_space(device, space, reference, t)); + } + + void resolve( + xr::session & session, + XrTime t, + std::vector & out) + { + assert(devices.size() == spaces.size()); + if (locate_spaces) + { + locations.resize(spaces.size()); + velocities.resize(spaces.size()); + XrSpaceVelocities spc_velocities{ + .type = XR_TYPE_SPACE_VELOCITIES, + .velocityCount = uint32_t(velocities.size()), + .velocities = velocities.data(), + }; + XrSpaceLocations spc_locations{ + .type = XR_TYPE_SPACE_LOCATIONS, + .locationCount = uint32_t(locations.size()), + .locations = locations.data(), + }; + XrSpacesLocateInfo info{ + .type = XR_TYPE_SPACES_LOCATE_INFO, + .baseSpace = reference, + .time = t, + .spaceCount = uint32_t(spaces.size()), + .spaces = spaces.data(), + }; + auto res = locate_spaces(session, &info, &spc_locations); + if (XR_SUCCEEDED(res)) + { + for (size_t i = 0; i < devices.size(); ++i) + { + const auto & location = locations[i]; + const auto & velocity = velocities[i]; + + out.push_back({ + .pose = location.pose, + .linear_velocity = velocity.linearVelocity, + .angular_velocity = velocity.angularVelocity, + .device = devices[i], + .flags = cast_flags(location.locationFlags, velocity.velocityFlags), + }); + } + } + else + spdlog::warn("xrLocateSpaces failed: {}", xr::to_string(res)); + devices.clear(); + spaces.clear(); + } + } +}; + } // namespace static std::optional> locate_hands(xr::hand_tracker & hand, XrSpace space, XrTime time) @@ -272,6 +371,8 @@ void scenes::stream::tracking() } } + locate_spaces_functor locate_spaces{instance, world_space}; + on_interaction_profile_changed({}); while (not exiting) @@ -353,8 +454,9 @@ void scenes::stream::tracking() for (auto [device, space]: spaces) { if (enabled(control, device)) - packet.device_poses.emplace_back(locate_space(device, space, world_space, t0 + Δt)); + locate_spaces.add_space(device, space, t0 + Δt, packet.device_poses); } + locate_spaces.resolve(session, t0 + Δt, packet.device_poses); if (body_tracking) std::visit(utils::overloaded{