From 6de91ad8374c23a3d569c58622979a17b23054fc Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sun, 21 Jan 2024 21:29:15 +0100 Subject: [PATCH] Give each scene its XrActionSet --- client/application.cpp | 258 +++++++++++++++++++----------- client/application.h | 3 +- client/scene.cpp | 7 +- client/scene.h | 81 +++++++++- client/scenes/lobby.cpp | 61 ++++++- client/scenes/lobby.h | 4 +- client/scenes/stream.cpp | 10 ++ client/scenes/stream.h | 4 +- client/scenes/stream_actions.cpp | 1 + client/scenes/stream_tracking.cpp | 1 + client/xr/session.cpp | 20 +++ client/xr/session.h | 2 + 12 files changed, 345 insertions(+), 107 deletions(-) diff --git a/client/application.cpp b/client/application.cpp index 2cf07eed..0e45f731 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -26,10 +26,12 @@ #include "spdlog/spdlog.h" #include "utils/check.h" #include "utils/files.h" +#include "xr/actionset.h" #include "xr/xr.h" #include #include "utils/contains.h" #include +#include #include #include #include @@ -58,16 +60,36 @@ using namespace std::chrono_literals; -struct suggested_binding +struct interaction_profile { std::string profile_name; std::vector required_extensions; - std::vector paths; + std::vector input_sources; bool available; }; -static std::vector suggested_bindings{ - suggested_binding{ +static std::vector interaction_profiles{ + interaction_profile{ + "/interaction_profiles/khr/simple_controller", + {}, + { + "/user/hand/left/output/haptic", + "/user/hand/right/output/haptic", + + "/user/hand/left/input/grip/pose", + "/user/hand/left/input/aim/pose", + + "/user/hand/right/input/grip/pose", + "/user/hand/right/input/aim/pose", + + "/user/hand/left/input/menu/click", + "/user/hand/left/input/select/click", + + "/user/hand/right/input/menu/click", + "/user/hand/right/input/select/click", + + }}, + interaction_profile{ "/interaction_profiles/oculus/touch_controller", {}, { @@ -106,27 +128,7 @@ static std::vector suggested_bindings{ "/user/hand/right/input/thumbstick/touch", "/user/hand/right/input/thumbrest/touch", }}, - suggested_binding{ - "/interaction_profiles/khr/simple_controller", - {}, - { - "/user/hand/left/output/haptic", - "/user/hand/right/output/haptic", - - "/user/hand/left/input/grip/pose", - "/user/hand/left/input/aim/pose", - - "/user/hand/right/input/grip/pose", - "/user/hand/right/input/aim/pose", - - "/user/hand/left/input/menu/click", - "/user/hand/left/input/select/click", - - "/user/hand/right/input/menu/click", - "/user/hand/right/input/select/click", - - }}, - suggested_binding{ + interaction_profile{ "/interaction_profiles/bytedance/pico_neo3_controller", { "XR_BD_controller_interaction" }, { @@ -168,7 +170,7 @@ static std::vector suggested_bindings{ "/user/hand/right/input/thumbrest/touch", }}, - suggested_binding{ + interaction_profile{ "/interaction_profiles/bytedance/pico4_controller", { "XR_BD_controller_interaction" }, { @@ -554,38 +556,58 @@ void application::log_views() } } +static std::string make_xr_name(std::string name) +{ + // Generate a name suitable for a path component (see OpenXR spec ยง6.2) + for(char& c: name) + { + if (!isalnum(c) && c != '-' && c != '_' && c != '.') + c = '_'; + else + c = tolower(c); + } + + size_t pos = name.find_first_of("abcdefghijklmnopqrstuvwxyz"); + + return name.substr(pos); +} + void application::initialize_actions() { - xr_actionset = xr::actionset(xr_instance, "actions", "Actions"); + spdlog::info("Initializing actions"); - // Build the list of all possible actions, without duplicates - std::vector actions_path; - for(auto & suggested_binding: suggested_bindings) + // Build an action set with all possible input sources + std::vector action_sets; + xr_actionset = xr::actionset(xr_instance, "all_actions", "All actions"); + action_sets.push_back(xr_actionset); + + std::unordered_map> suggested_bindings; + + // 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) { - suggested_binding.available = utils::contains_all(xr_extensions, suggested_binding.required_extensions); + profile.available = utils::contains_all(xr_extensions, profile.required_extensions); - if (!suggested_binding.available) + if (!profile.available) continue; - for(const std::string& action_path: suggested_binding.paths) + suggested_bindings.emplace(profile.profile_name, std::vector{}); + + for(const std::string& source: profile.input_sources) { - if (!utils::contains(actions_path, action_path)) - actions_path.push_back(action_path); + if (!utils::contains(sources, source)) + sources.push_back(source); } } - // For each possible action, create a XrAction + // For each possible input source, create a XrAction and add it to the suggested binding std::unordered_map actions_by_name; - for(std::string& name: actions_path) + for(std::string& name: sources) { - std::string name_without_slashes = name.substr(1); - - for (char & i: name_without_slashes) - { - if (i == '/') - i = '_'; - } + std::string name_without_slashes = make_xr_name(name); XrActionType type = guess_action_type(name); @@ -603,27 +625,70 @@ void application::initialize_actions() right_aim_space = xr_session.create_action_space(a); } - // Suggest bindings for all supported controllers - for (const auto & suggested_binding: suggested_bindings) + // Build an action set for each scene + for(scene::meta* i: scene::scene_registry) { - if (!suggested_binding.available) + spdlog::info(" Scene \"{}\"", i->name); + + std::string actionset_name = make_xr_name(i->name); + + i->actionset = xr::actionset(xr_instance, actionset_name, i->name); + action_sets.push_back(i->actionset); + + for(auto& [action_name, action_type] : i->actions) { - spdlog::info("Skipping interaction profile {}", suggested_binding.profile_name); + spdlog::info(" Creating action {}", action_name); + XrAction a = i->actionset.create_action(action_type, action_name); + i->actions_by_name[action_name] = std::make_pair(a, action_type); + + if (action_type == XR_ACTION_TYPE_POSE_INPUT) + i->spaces_by_name[action_name] = xr_session.create_action_space(a); + } + + for(const scene::suggested_binding& j: i->bindings) + { + // Skip unsupported profiles + if (!suggested_bindings.contains(j.profile_name)) + continue; + + spdlog::info(" Adding suggested bindings for {}", j.profile_name); + std::vector& xr_bindings = suggested_bindings[j.profile_name]; + + for(const scene::action_binding& k: j.paths) + { + spdlog::info(" {} => {}", k.action_name, k.input_source); + XrAction a = i->actions_by_name[k.action_name].first; + assert(a != XR_NULL_HANDLE); + + xr_bindings.push_back(XrActionSuggestedBinding{ + .action = a, + .binding = string_to_path(k.input_source) + }); + } + } + } + + // Suggest bindings for all supported controllers + for (const auto & profile: interaction_profiles) + { + if (!profile.available) + { + spdlog::info("Skipping interaction profile {}", profile.profile_name); continue; } - std::vector xr_bindings; + std::vector& xr_bindings = suggested_bindings[profile.profile_name]; - for (const auto & name: suggested_binding.paths) + for (const auto & name: profile.input_sources) { xr_bindings.push_back({actions_by_name[name], string_to_path(name)}); } - spdlog::info("Suggesting bindings for interaction profile {}", suggested_binding.profile_name); - xr_instance.suggest_bindings(suggested_binding.profile_name, xr_bindings); + spdlog::info("Suggesting bindings for interaction profile {}", profile.profile_name); + xr_instance.suggest_bindings(profile.profile_name, xr_bindings); } - xr_session.attach_actionsets({xr_actionset}); + xr_session.attach_actionsets(action_sets); } void application::initialize() @@ -640,7 +705,7 @@ void application::initialize() opt_extensions.push_back(XR_KHR_COMPOSITION_LAYER_COLOR_SCALE_BIAS_EXTENSION_NAME); opt_extensions.push_back(XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME); - for(const auto& i:suggested_bindings) + for(const auto& i: interaction_profiles) { for(const auto& j: i.required_extensions) { @@ -884,6 +949,14 @@ void application::cleanup() // Remove all scenes before destroying the allocator scene_stack.clear(); + // Empty the meta objects while the OpenXR instance still exists + for(auto i: scene::scene_registry) + { + xr::actionset tmp = std::move(i->actionset); + i->actions_by_name.clear(); // Not strictly necessary + i->spaces_by_name.clear(); + } + if (allocator) { vmaDestroyAllocator(allocator); @@ -1025,7 +1098,12 @@ void application::push_scene(std::shared_ptr s) void application::poll_actions() { - instance().xr_session.sync_actions(instance().xr_actionset); + std::array action_sets{ + instance().xr_actionset, + instance().current_scene()->current_meta.actionset + }; + + instance().xr_session.sync_actions(action_sets); } std::optional> application::read_action_bool(XrAction action) @@ -1185,42 +1263,6 @@ void application::interaction_profile_changed() std::string current_profile = xr_session.get_current_interaction_profile(device); spdlog::info("Current interaction profile for {}: {}", device, current_profile); - - for(const auto& binding: suggested_bindings) - { - if (binding.profile_name != current_profile) - continue; - - for(const std::string& action_name: binding.paths) - { - if (!action_name.starts_with(device)) - continue; - - XrAction action = XR_NULL_HANDLE; - - for(const auto&[i,j,k]:actions) - { - if (action_name == k) - { - action = i; - break; - } - } - - if (!action) - continue; - - auto sources = xr_session.localized_sources_for_action(action); - if (!sources.empty()) - { - spdlog::info(" Sources for {}", action_name); - for (auto & k: sources) - spdlog::info(" {}", k); - } - else - spdlog::warn(" No source for {}", action_name); - } - } } catch (std::exception & e) { @@ -1228,6 +1270,40 @@ void application::interaction_profile_changed() continue; } } + + spdlog::info("Global actions:"); + for(auto& [action, type, name]: actions) + { + auto sources = xr_session.localized_sources_for_action(action); + if (!sources.empty()) + { + spdlog::info(" Sources for {}", name); + for (auto & k: sources) + spdlog::info(" {}", k); + } + else + spdlog::warn(" No source for {}", name); + } + + if (current_scene()) + { + spdlog::info("Actions for current scene ({}):", current_scene()->current_meta.name); + + for(auto& [name, action_and_type]: current_scene()->current_meta.actions_by_name) + { + auto [action, type] = action_and_type; + + auto sources = xr_session.localized_sources_for_action(action); + if (!sources.empty()) + { + spdlog::info(" Sources for {}", name); + for (auto & k: sources) + spdlog::info(" {}", k); + } + else + spdlog::warn(" No source for {}", name); + } + } } void application::poll_events() diff --git a/client/application.h b/client/application.h index 6908c0ee..94b1ff43 100644 --- a/client/application.h +++ b/client/application.h @@ -23,12 +23,13 @@ #include #endif -#include "spdlog/spdlog.h" #include "xr/xr.h" #include +#include #include #include #include +#include #include #include #include diff --git a/client/scene.cpp b/client/scene.cpp index 2079e2ba..7ee40b1b 100644 --- a/client/scene.cpp +++ b/client/scene.cpp @@ -23,9 +23,11 @@ #include #include +std::vector scene::scene_registry; + scene::~scene() {} -scene::scene() : +scene::scene(key, const meta& current_meta) : instance(application::instance().xr_instance), session(application::instance().xr_session), world_space(application::instance().world_space), @@ -37,7 +39,8 @@ scene::scene() : physical_device(application::instance().vk_physical_device), queue(application::instance().vk_queue), commandpool(application::instance().vk_cmdpool), - queue_family_index(application::instance().vk_queue_family_index) + queue_family_index(application::instance().vk_queue_family_index), + current_meta(current_meta) { } diff --git a/client/scene.h b/client/scene.h index 1a75b4bd..8b4d8fd9 100644 --- a/client/scene.h +++ b/client/scene.h @@ -19,14 +19,56 @@ #pragma once -#include - +#include "xr/actionset.h" #include "xr/session.h" #include "xr/swapchain.h" +#include +#include +#include +#include + +// See http://www.nirfriedman.com/2018/04/29/unforgettable-factory/ +// for automatic registration of scenes + class scene { +public: + struct action_binding + { + std::string action_name; + std::string input_source; + }; + + struct suggested_binding + { + std::string profile_name; + std::vector paths; + }; + + struct meta + { + // Filled by the scene class + std::string name; + std::vector> actions; + std::vector bindings; + + // Filled by the application class + xr::actionset actionset; + std::unordered_map> actions_by_name; + std::unordered_map spaces_by_name; + }; + +private: + // Force derived classes to inherit from scene_impl instead of scene + class key {}; + template + friend class scene_impl; + friend class application; + protected: + static std::vector scene_registry; + xr::instance & instance; xr::session & session; xr::space & world_space; @@ -40,8 +82,20 @@ protected: vk::raii::CommandPool& commandpool; uint32_t queue_family_index; + const meta& current_meta; + + std::pair get_action(const std::string & name) + { + return current_meta.actions_by_name.at(name); + } + + XrSpace get_action_space(const std::string & name) + { + return current_meta.spaces_by_name.at(name); + } + public: - scene(); + scene(key, const meta&); virtual ~scene(); @@ -49,3 +103,24 @@ public: virtual void on_unfocused(); virtual void on_focused(); }; + +template +class scene_impl : public scene +{ + friend T; + friend scene; + + // Magically register all classes that derive from scene_impl + static bool register_scene() + { + scene_registry.emplace_back(&T::get_meta_scene()); + return true; + } + + static inline bool registered = scene_impl::register_scene(); + + scene_impl() : scene(key{}, T::get_meta_scene()) + { + (void)registered; + } +}; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 5b6bafbc..536b792b 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -100,16 +100,16 @@ scenes::lobby::lobby() : std::array imgui_inputs{ imgui_context::controller{ - .aim = application::left_aim(), - .trigger = application::get_action("/user/hand/left/input/select/click"), - .squeeze = application::get_action("/user/hand/left/input/squeeze/value"), - .scroll = application::get_action("/user/hand/left/input/thumbstick"), + .aim = get_action_space("left_aim"), + .trigger = get_action("left_trigger"), + .squeeze = get_action("left_squeeze"), + .scroll = get_action("left_scroll"), }, imgui_context::controller{ - .aim = application::right_aim(), - .trigger = application::get_action("/user/hand/right/input/select/click"), - .squeeze = application::get_action("/user/hand/right/input/squeeze/value"), - .scroll = application::get_action("/user/hand/right/input/thumbstick"), + .aim = get_action_space("right_aim"), + .trigger = get_action("right_trigger"), + .squeeze = get_action("right_squeeze"), + .scroll = get_action("right_scroll"), }, }; @@ -347,3 +347,48 @@ void scenes::lobby::on_unfocused() { discover.reset(); } + +scene::meta& scenes::lobby::get_meta_scene() +{ + static meta m{ + .name = "Lobby", + .actions = { + {"left_aim", XR_ACTION_TYPE_POSE_INPUT}, + {"left_trigger", XR_ACTION_TYPE_FLOAT_INPUT}, + {"left_squeeze", XR_ACTION_TYPE_FLOAT_INPUT}, + {"left_scroll", XR_ACTION_TYPE_VECTOR2F_INPUT}, + {"right_aim", XR_ACTION_TYPE_POSE_INPUT}, + {"right_trigger", XR_ACTION_TYPE_FLOAT_INPUT}, + {"right_squeeze", XR_ACTION_TYPE_FLOAT_INPUT}, + {"right_scroll", XR_ACTION_TYPE_VECTOR2F_INPUT}, + }, + .bindings = { + suggested_binding{ + "/interaction_profiles/oculus/touch_controller", + { + {"left_aim", "/user/hand/left/input/aim/pose"}, + {"left_trigger", "/user/hand/left/input/trigger/value"}, + {"left_squeeze", "/user/hand/left/input/squeeze/value"}, + {"left_scroll", "/user/hand/left/input/thumbstick"}, + {"right_aim", "/user/hand/right/input/aim/pose"}, + {"right_trigger", "/user/hand/right/input/trigger/value"}, + {"right_squeeze", "/user/hand/right/input/squeeze/value"}, + {"right_scroll", "/user/hand/right/input/thumbstick"}, + } + }, + suggested_binding{ + "/interaction_profiles/khr/simple_controller", + { + {"left_aim", "/user/hand/left/input/aim/pose"}, + {"left_trigger", "/user/hand/left/input/select/click"}, + {"left_squeeze", "/user/hand/left/input/menu/click"}, + {"right_aim", "/user/hand/right/input/aim/pose"}, + {"right_trigger", "/user/hand/right/input/select/click"}, + {"right_squeeze", "/user/hand/right/input/menu/click"}, + } + }, + } + }; + + return m; +} diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index f12e3e8a..1cea78f8 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -35,7 +35,7 @@ namespace scenes { class stream; -class lobby : public scene +class lobby : public scene_impl { std::string status_string; std::string last_status_string; @@ -70,5 +70,7 @@ public: void on_unfocused() override; void on_focused() override; + + static meta& get_meta_scene(); }; } // namespace scenes diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 5a5b790b..fad58ecb 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -555,3 +555,13 @@ void scenes::stream::video() } } } + +scene::meta& scenes::stream::get_meta_scene() +{ + static meta m{ + "Stream", + {} + }; + + return m; +} diff --git a/client/scenes/stream.h b/client/scenes/stream.h index a1832514..3e6662fc 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -32,7 +32,7 @@ namespace scenes { -class stream : public scene, public std::enable_shared_from_this +class stream : public scene_impl, public std::enable_shared_from_this { static const size_t view_count = 2; @@ -121,6 +121,8 @@ public: return !exiting; } + static meta& get_meta_scene(); + private: void process_packets(); void tracking(); diff --git a/client/scenes/stream_actions.cpp b/client/scenes/stream_actions.cpp index cb9e631f..a3d2d208 100644 --- a/client/scenes/stream_actions.cpp +++ b/client/scenes/stream_actions.cpp @@ -19,6 +19,7 @@ #include "stream.h" #include "application.h" +#include void scenes::stream::read_actions() { diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index a0c792e3..14940813 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -21,6 +21,7 @@ #include "utils/ranges.h" #include "application.h" #include +#include static from_headset::tracking::pose locate_space(device_id device, XrSpace space, XrSpace reference, XrTime time) { diff --git a/client/xr/session.cpp b/client/xr/session.cpp index 2b68a78a..13214c23 100644 --- a/client/xr/session.cpp +++ b/client/xr/session.cpp @@ -23,6 +23,7 @@ #include "xr.h" #include #include +#include "utils/ranges.h" xr::session::session(xr::instance & inst, xr::system & sys, vk::raii::Instance& vk_inst, vk::raii::PhysicalDevice& pdev, vk::raii::Device& dev, int queue_family_index) : inst(&inst) @@ -232,6 +233,25 @@ std::vector xr::session::get_refresh_rates() return {}; } +void xr::session::sync_actions(std::span action_sets) +{ + std::vector active_action_sets(action_sets.size()); + + for(auto&& [i, j]: utils::zip(active_action_sets, action_sets)) + { + i.actionSet = j; + i.subactionPath = XR_NULL_PATH; + } + + XrActionsSyncInfo sync_info{ + .type = XR_TYPE_ACTIONS_SYNC_INFO, + .countActiveActionSets = (uint32_t)active_action_sets.size(), + .activeActionSets = active_action_sets.data(), + }; + + CHECK_XR(xrSyncActions(id, &sync_info)); +} + void xr::session::sync_actions(XrActionSet action_set, XrPath subaction_path) { XrActiveActionSet active_action_set{ diff --git a/client/xr/session.h b/client/xr/session.h index bb8b5eba..c825571f 100644 --- a/client/xr/session.h +++ b/client/xr/session.h @@ -71,5 +71,7 @@ public: void sync_actions(XrActionSet action_set, XrPath subaction_path = XR_NULL_PATH); void sync_actions(XrActionSet action_set, const std::string & subaction_path); + + void sync_actions(std::span action_sets); }; } // namespace xr