From 2ba3d78cd46d34a42bf3752a27bb920a971b8954 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Wed, 2 Jul 2025 15:49:23 +0200 Subject: [PATCH 1/3] Try to request OpenXR 1.1 --- client/application.cpp | 127 ++++++++++++++++++++++------------------- client/xr/instance.cpp | 24 +++++++- client/xr/instance.h | 6 ++ 3 files changed, 95 insertions(+), 62 deletions(-) diff --git a/client/application.cpp b/client/application.cpp index 8cf3033d..992e01c0 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -26,7 +26,6 @@ #include "spdlog/spdlog.h" #include "utils/contains.h" #include "utils/files.h" -#include "utils/named_thread.h" #include "vk/check.h" #include "wifi_lock.h" #include "xr/actionset.h" @@ -51,6 +50,7 @@ #endif #ifdef __ANDROID__ +#include "utils/named_thread.h" #include #include @@ -66,15 +66,16 @@ struct interaction_profile { std::string profile_name; std::vector required_extensions; + XrVersion min_version = XR_MAKE_VERSION(1, 0, 0); + XrVersion max_version = XR_MAKE_VERSION(2, 0, 0); // exclusive std::vector input_sources; bool available; }; static std::vector interaction_profiles{ interaction_profile{ - "/interaction_profiles/khr/simple_controller", - {}, - { + .profile_name = "/interaction_profiles/khr/simple_controller", + .input_sources = { "/user/hand/left/output/haptic", "/user/hand/right/output/haptic", @@ -90,11 +91,11 @@ static std::vector interaction_profiles{ "/user/hand/right/input/menu/click", "/user/hand/right/input/select/click", - }}, + }, + }, interaction_profile{ - "/interaction_profiles/oculus/touch_controller", - {}, - { + .profile_name = "/interaction_profiles/oculus/touch_controller", + .input_sources = { "/user/hand/left/output/haptic", "/user/hand/right/output/haptic", @@ -129,11 +130,13 @@ static std::vector interaction_profiles{ "/user/hand/right/input/thumbstick/click", "/user/hand/right/input/thumbstick/touch", "/user/hand/right/input/thumbrest/touch", - }}, + }, + }, interaction_profile{ - "/interaction_profiles/facebook/touch_controller_pro", - {"XR_FB_touch_controller_pro"}, - { + .profile_name = "/interaction_profiles/facebook/touch_controller_pro", + .required_extensions = {"XR_FB_touch_controller_pro"}, + .max_version = XR_MAKE_VERSION(1, 1, 0), + .input_sources = { "/user/hand/left/output/haptic", "/user/hand/left/output/haptic_trigger_fb", "/user/hand/left/output/haptic_thumb_fb", @@ -184,11 +187,12 @@ static std::vector interaction_profiles{ "/user/hand/right/input/trigger/slide_fb", "/user/hand/right/input/trigger/proximity_fb", "/user/hand/right/input/thumb_fb/proximity_fb", - }}, + }, + }, interaction_profile{ - "/interaction_profiles/meta/touch_pro_controller", - {"XR_VERSION_1_1"}, - { + .profile_name = "/interaction_profiles/meta/touch_pro_controller", + .min_version = XR_MAKE_VERSION(1, 1, 0), + .input_sources = { "/user/hand/left/output/haptic", "/user/hand/left/output/haptic_trigger", "/user/hand/left/output/haptic_thumb", @@ -239,12 +243,13 @@ static std::vector interaction_profiles{ "/user/hand/right/input/thumbrest/touch", "/user/hand/right/input/thumbrest/force", "/user/hand/right/input/stylus/force", - - }}, + }, + }, interaction_profile{ - "/interaction_profiles/meta/touch_controller_plus", - {"XR_META_touch_controller_plus"}, - { + .profile_name = "/interaction_profiles/meta/touch_controller_plus", + .required_extensions = {"XR_META_touch_controller_plus"}, + .max_version = XR_MAKE_VERSION(1, 1, 0), + .input_sources = { "/user/hand/left/output/haptic", "/user/hand/right/output/haptic", @@ -288,11 +293,12 @@ static std::vector interaction_profiles{ "/user/hand/right/input/trigger/slide_meta", "/user/hand/right/input/trigger/force", - }}, + }, + }, interaction_profile{ - "/interaction_profiles/meta/touch_plus_controller", - {"XR_VERSION_1_1"}, - { + .profile_name = "/interaction_profiles/meta/touch_plus_controller", + .min_version = XR_MAKE_VERSION(1, 1, 0), + .input_sources = { "/user/hand/left/output/haptic", "/user/hand/right/output/haptic", @@ -338,11 +344,12 @@ static std::vector interaction_profiles{ "/user/hand/right/input/thumbstick/touch", "/user/hand/right/input/thumbrest/touch", - }}, + }, + }, interaction_profile{ - "/interaction_profiles/bytedance/pico_neo3_controller", - {"XR_BD_controller_interaction"}, - { + .profile_name = "/interaction_profiles/bytedance/pico_neo3_controller", + .required_extensions = {"XR_BD_controller_interaction"}, + .input_sources = { "/user/hand/left/output/haptic", "/user/hand/right/output/haptic", @@ -380,11 +387,12 @@ static std::vector interaction_profiles{ "/user/hand/right/input/thumbstick/click", "/user/hand/right/input/thumbstick/touch", - }}, + }, + }, interaction_profile{ - "/interaction_profiles/bytedance/pico4_controller", - {"XR_BD_controller_interaction"}, - { + .profile_name = "/interaction_profiles/bytedance/pico4_controller", + .required_extensions = {"XR_BD_controller_interaction"}, + .input_sources = { "/user/hand/left/output/haptic", "/user/hand/right/output/haptic", @@ -422,11 +430,12 @@ static std::vector interaction_profiles{ "/user/hand/right/input/thumbstick/touch", "/user/hand/right/input/squeeze/click", "/user/hand/right/input/squeeze/value", - }}, + }, + }, interaction_profile{ - "/interaction_profiles/bytedance/pico4s_controller", - {"XR_BD_controller_interaction"}, - { + .profile_name = "/interaction_profiles/bytedance/pico4s_controller", + .required_extensions = {"XR_BD_controller_interaction"}, + .input_sources = { "/user/hand/left/output/haptic", "/user/hand/right/output/haptic", @@ -464,11 +473,12 @@ static std::vector interaction_profiles{ "/user/hand/right/input/thumbstick/touch", "/user/hand/right/input/squeeze/click", "/user/hand/right/input/squeeze/value", - }}, + }, + }, interaction_profile{ - "/interaction_profiles/htc/vive_focus3_controller", - {"XR_HTC_vive_focus3_controller_interaction"}, - { + .profile_name = "/interaction_profiles/htc/vive_focus3_controller", + .required_extensions = {"XR_HTC_vive_focus3_controller_interaction"}, + .input_sources = { "/user/hand/left/output/haptic", "/user/hand/right/output/haptic", @@ -505,18 +515,19 @@ static std::vector interaction_profiles{ "/user/hand/right/input/thumbstick/click", "/user/hand/right/input/thumbstick/touch", "/user/hand/right/input/thumbrest/touch", - }}, - interaction_profile{ - "/interaction_profiles/htc/vive_xr_tracker", - {"XR_HTC_vive_xr_tracker_interaction", "XR_HTC_path_enumeration"}, - {}, + }, }, interaction_profile{ - "/interaction_profiles/ext/eye_gaze_interaction", - {"XR_EXT_eye_gaze_interaction"}, - { + .profile_name = "/interaction_profiles/htc/vive_xr_tracker", + .required_extensions = {"XR_HTC_vive_xr_tracker_interaction", "XR_HTC_path_enumeration"}, + }, + interaction_profile{ + .profile_name = "/interaction_profiles/ext/eye_gaze_interaction", + .required_extensions = {"XR_EXT_eye_gaze_interaction"}, + .input_sources = { "/user/eyes_ext/input/gaze_ext/pose", - }}, + }, + }, }; static const std::pair action_suffixes[] = @@ -910,12 +921,13 @@ void application::initialize_actions() std::unordered_map> suggested_bindings; + XrVersion api_version = xr_instance.get_api_version(); // Build the list of all possible input sources, without duplicates, // checking which profiles are supported by the runtime std::vector sources; for (auto & profile: interaction_profiles) { - profile.available = utils::contains_all(xr_extensions, profile.required_extensions); + profile.available = utils::contains_all(xr_extensions, profile.required_extensions) and profile.min_version <= api_version and profile.max_version > api_version; if (!profile.available) continue; @@ -1080,13 +1092,7 @@ void application::initialize() opt_extensions.push_back(XR_FB_COMPOSITION_LAYER_SETTINGS_EXTENSION_NAME); for (const auto & i: interaction_profiles) - { - for (const auto & j: i.required_extensions) - { - if (not j.starts_with("XR_VERSION")) - opt_extensions.push_back(j); - } - } + opt_extensions.insert(opt_extensions.end(), i.required_extensions.begin(), i.required_extensions.end()); for (const auto & i: xr::instance::extensions()) { @@ -1107,7 +1113,10 @@ void application::initialize() xr_instance = xr::instance(app_info.name, extensions); #endif - spdlog::info("Created OpenXR instance, runtime {}, version {}", xr_instance.get_runtime_name(), xr_instance.get_runtime_version()); + spdlog::info("Created OpenXR instance, runtime {}, version {}, API version {}", + xr_instance.get_runtime_name(), + xr_instance.get_runtime_version(), + xr::to_string(xr_instance.get_api_version())); xr_system_id = xr::system(xr_instance, app_info.formfactor); spdlog::info("Created OpenXR system for form factor {}", xr::to_string(app_info.formfactor)); diff --git a/client/xr/instance.cpp b/client/xr/instance.cpp index ab934b1f..64a993fc 100644 --- a/client/xr/instance.cpp +++ b/client/xr/instance.cpp @@ -40,6 +40,26 @@ static XrBool32 debug_callback( return XR_FALSE; } +static std::pair create_instance(XrInstanceCreateInfo & info) +{ + XrResult res; + for (XrVersion version: { + XR_API_VERSION_1_1, + XR_API_VERSION_1_0, + }) + { + info.applicationInfo.apiVersion = version; + XrInstance inst; + res = xrCreateInstance(&info, &inst); + if (XR_SUCCEEDED(res)) + return {version, inst}; + spdlog::info("Failed to create OpenXR instance version {}: {}", + xr::to_string(version), + xr::to_string(res)); + } + throw std::system_error(res, xr::error_category(), "Failed to create OpenXR instance"); +} + #if defined(XR_USE_PLATFORM_ANDROID) xr::instance::instance(std::string_view application_name, void * applicationVM, void * applicationActivity, std::vector extensions) #else @@ -122,7 +142,6 @@ xr::instance::instance(std::string_view application_name, std::vector std::string runtime_version; std::string runtime_name; std::unordered_map loaded_extensions; + XrVersion api_version; public: #if defined(XR_USE_PLATFORM_ANDROID) @@ -70,6 +71,11 @@ public: return runtime_name; } + XrVersion get_api_version() const + { + return api_version; + } + template F get_proc(const char * name) { From 3a33adbd86dec8c355b7ddb9813ec4b23c60ee8c Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Wed, 2 Jul 2025 17:09:43 +0200 Subject: [PATCH 2/3] Remove Quest/simple_controller hack Just don't suggest bindings for simple controller on Quest --- client/application.cpp | 16 ++++++++++ client/scenes/stream_tracking.cpp | 49 +------------------------------ 2 files changed, 17 insertions(+), 48 deletions(-) diff --git a/client/application.cpp b/client/application.cpp index 992e01c0..b7cb4b21 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -929,6 +929,22 @@ void application::initialize_actions() { profile.available = utils::contains_all(xr_extensions, profile.required_extensions) and profile.min_version <= api_version and profile.max_version > api_version; + if (profile.profile_name.ends_with("khr/simple_controller")) + { + switch (guess_model()) + { + // Quest hand tracking creates a fake khr/simple_controller when hand tracking + // is enabled, this messes with native hand tracking + case model::meta_quest_3: + case model::meta_quest_pro: + case model::meta_quest_3s: + case model::oculus_quest_2: + profile.available = false; + default: + break; + } + } + if (!profile.available) continue; diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index 67d31429..0a4d9b74 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -246,20 +246,6 @@ void scenes::stream::tracking() std::vector packets; const bool hand_tracking = config.check_feature(feature::hand_tracking); - // Quest hand tracking creates a fake khr/simple_controller when hand tracking - // is enabled, let's not send it when hand tracking data is invalid - bool block_khr_simple_controller = false; - switch (guess_model()) - { - case model::meta_quest_3: - case model::meta_quest_pro: - case model::meta_quest_3s: - case model::oculus_quest_2: - block_khr_simple_controller = hand_tracking; - break; - default: - break; - } const bool face_tracking = config.check_feature(feature::face_tracking); auto & face_tracker = application::get_face_tracker(); @@ -345,62 +331,29 @@ void scenes::stream::tracking() if (control.enabled[size_t(tid::left_hand)]) { auto joints = locate_hands(application::get_left_hand(), world_space, t0 + Δt); - should_skip_simple_controllers[0] = !joints.has_value(); hands.emplace_back( t0, t0 + Δt, from_headset::hand_tracking::left, joints); } - else - { - should_skip_simple_controllers[0] = false; - } if (control.enabled[size_t(tid::right_hand)]) { auto joints = locate_hands(application::get_right_hand(), world_space, t0 + Δt); - should_skip_simple_controllers[1] = !joints.has_value(); hands.emplace_back( t0, t0 + Δt, from_headset::hand_tracking::right, joints); } - else - { - should_skip_simple_controllers[1] = false; - } } packet.device_poses.clear(); for (auto [device, space]: spaces) { if (enabled(control, device)) - { - switch (device) - { - case device_id::LEFT_AIM: - case device_id::LEFT_GRIP: - case device_id::LEFT_PALM: - case device_id::RIGHT_AIM: - case device_id::RIGHT_GRIP: - case device_id::RIGHT_PALM: { - const bool right = device > device_id::LEFT_PALM; - if (block_khr_simple_controller and interaction_profiles[right].load() == interaction_profile::khr_simple_controller and should_skip_simple_controllers[right]) - { - packet.device_poses.emplace_back(from_headset::tracking::pose{ - .device = device, - }); - continue; - } - [[fallthrough]]; - } - default: - packet.device_poses.emplace_back(locate_space(device, space, world_space, t0 + Δt)); - break; - } - } + packet.device_poses.emplace_back(locate_space(device, space, world_space, t0 + Δt)); } if (body_tracking) From 1ad015ff65452b03d76aaf719bb46612aca1fdc6 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Wed, 2 Jul 2025 21:15:06 +0200 Subject: [PATCH 3/3] Use xrLocateSpaces when runtime has OpenXR 1.1 --- client/scenes/stream_tracking.cpp | 170 ++++++++++++++++++++++++------ 1 file changed, 136 insertions(+), 34 deletions(-) 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{