Give each scene its XrActionSet

This commit is contained in:
Guillaume Meunier
2024-01-28 11:33:35 +01:00
parent 705d16cd4e
commit 6de91ad837
12 changed files with 345 additions and 107 deletions
+167 -91
View File
@@ -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 <algorithm>
#include "utils/contains.h"
#include <algorithm>
#include <ctype.h>
#include <string>
#include <exception>
#include <thread>
@@ -58,16 +60,36 @@
using namespace std::chrono_literals;
struct suggested_binding
struct interaction_profile
{
std::string profile_name;
std::vector<std::string> required_extensions;
std::vector<std::string> paths;
std::vector<std::string> input_sources;
bool available;
};
static std::vector<suggested_binding> suggested_bindings{
suggested_binding{
static std::vector<interaction_profile> 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_binding> 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_binding> 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<std::string> actions_path;
for(auto & suggested_binding: suggested_bindings)
// Build an action set with all possible input sources
std::vector<XrActionSet> action_sets;
xr_actionset = xr::actionset(xr_instance, "all_actions", "All actions");
action_sets.push_back(xr_actionset);
std::unordered_map<std::string, std::vector<XrActionSuggestedBinding>> suggested_bindings;
// Build the list of all possible input sources, without duplicates,
// checking which profiles are supported by the runtime
std::vector<std::string> 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<XrActionSuggestedBinding>{});
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<std::string, XrAction> 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<XrActionSuggestedBinding>& 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<XrActionSuggestedBinding> xr_bindings;
std::vector<XrActionSuggestedBinding>& 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<scene> s)
void application::poll_actions()
{
instance().xr_session.sync_actions(instance().xr_actionset);
std::array<XrActionSet, 2> action_sets{
instance().xr_actionset,
instance().current_scene()->current_meta.actionset
};
instance().xr_session.sync_actions(action_sets);
}
std::optional<std::pair<XrTime, bool>> 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()
+2 -1
View File
@@ -23,12 +23,13 @@
#include <android_native_app_glue.h>
#endif
#include "spdlog/spdlog.h"
#include "xr/xr.h"
#include <array>
#include <atomic>
#include <filesystem>
#include <mutex>
#include <unordered_set>
#include <unordered_map>
#include <vulkan/vulkan_raii.hpp>
#include <openxr/openxr.h>
#include <openxr/openxr_platform.h>
+5 -2
View File
@@ -23,9 +23,11 @@
#include <spdlog/spdlog.h>
#include <vulkan/vulkan_raii.hpp>
std::vector<scene::meta *> 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)
{
}
+78 -3
View File
@@ -19,14 +19,56 @@
#pragma once
#include <cstdint>
#include "xr/actionset.h"
#include "xr/session.h"
#include "xr/swapchain.h"
#include <cstdint>
#include <string>
#include <vector>
#include <unordered_map>
// 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<action_binding> paths;
};
struct meta
{
// Filled by the scene class
std::string name;
std::vector<std::pair<std::string, XrActionType>> actions;
std::vector<suggested_binding> bindings;
// Filled by the application class
xr::actionset actionset;
std::unordered_map<std::string, std::pair<XrAction, XrActionType>> actions_by_name;
std::unordered_map<std::string, xr::space> spaces_by_name;
};
private:
// Force derived classes to inherit from scene_impl<T> instead of scene
class key {};
template<typename T>
friend class scene_impl;
friend class application;
protected:
static std::vector<meta*> 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<XrAction, XrActionType> 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<typename T>
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<T>::register_scene();
scene_impl() : scene(key{}, T::get_meta_scene())
{
(void)registered;
}
};
+53 -8
View File
@@ -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;
}
+3 -1
View File
@@ -35,7 +35,7 @@ namespace scenes
{
class stream;
class lobby : public scene
class lobby : public scene_impl<lobby>
{
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
+10
View File
@@ -555,3 +555,13 @@ void scenes::stream::video()
}
}
}
scene::meta& scenes::stream::get_meta_scene()
{
static meta m{
"Stream",
{}
};
return m;
}
+3 -1
View File
@@ -32,7 +32,7 @@
namespace scenes
{
class stream : public scene, public std::enable_shared_from_this<stream>
class stream : public scene_impl<stream>, public std::enable_shared_from_this<stream>
{
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();
+1
View File
@@ -19,6 +19,7 @@
#include "stream.h"
#include "application.h"
#include <spdlog/spdlog.h>
void scenes::stream::read_actions()
{
+1
View File
@@ -21,6 +21,7 @@
#include "utils/ranges.h"
#include "application.h"
#include <thread>
#include <spdlog/spdlog.h>
static from_headset::tracking::pose locate_space(device_id device, XrSpace space, XrSpace reference, XrTime time)
{
+20
View File
@@ -23,6 +23,7 @@
#include "xr.h"
#include <vulkan/vulkan.h>
#include <openxr/openxr_platform.h>
#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<float> xr::session::get_refresh_rates()
return {};
}
void xr::session::sync_actions(std::span<XrActionSet> action_sets)
{
std::vector<XrActiveActionSet> 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{
+2
View File
@@ -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<XrActionSet> action_sets);
};
} // namespace xr