From 974c90a36c2b7d692dc2d30c61b1103a6064a12c Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Wed, 2 Oct 2024 17:13:21 +0200 Subject: [PATCH] Store all spaces in an array --- client/application.cpp | 23 ++++++++++---------- client/application.h | 35 +++++-------------------------- client/scene.cpp | 2 -- client/scene.h | 2 -- client/scenes/input_profile.cpp | 8 +++---- client/scenes/lobby.cpp | 9 ++++---- client/scenes/stream.cpp | 6 +++--- client/scenes/stream_tracking.cpp | 15 ++++++------- client/xr/space.h | 13 ++++++++++++ 9 files changed, 49 insertions(+), 64 deletions(-) diff --git a/client/application.cpp b/client/application.cpp index c5509130..555d3a9e 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -650,15 +650,15 @@ void application::initialize_actions() actions_by_name.emplace(name, a); if (name == "/user/hand/left/input/grip/pose") - left_grip_space = xr_session.create_action_space(a); + spaces[size_t(xr::spaces::grip_left)] = xr_session.create_action_space(a); else if (name == "/user/hand/left/input/aim/pose") - left_aim_space = xr_session.create_action_space(a); + spaces[size_t(xr::spaces::aim_left)] = xr_session.create_action_space(a); else if (name == "/user/hand/right/input/grip/pose") - right_grip_space = xr_session.create_action_space(a); + spaces[size_t(xr::spaces::grip_right)] = xr_session.create_action_space(a); else if (name == "/user/hand/right/input/aim/pose") - right_aim_space = xr_session.create_action_space(a); + spaces[size_t(xr::spaces::aim_right)] = xr_session.create_action_space(a); else if (name == "/user/eyes_ext/input/gaze_ext/pose") - eye_gaze_space = xr_session.create_action_space(a); + spaces[size_t(xr::spaces::eye_gaze)] = xr_session.create_action_space(a); } // Build an action set for each scene @@ -828,16 +828,15 @@ void application::initialize() xr_session = xr::session(xr_instance, xr_system_id, vk_instance, vk_physical_device, vk_device, vk_queue_family_index); - auto spaces = xr_session.get_reference_spaces(); - spdlog::info("{} reference spaces", spaces.size()); - for (XrReferenceSpaceType i: spaces) { - spdlog::info(" {}", xr::to_string(i)); + auto spaces = xr_session.get_reference_spaces(); + spdlog::info("{} reference spaces", spaces.size()); + for (XrReferenceSpaceType i: spaces) + spdlog::info(" {}", xr::to_string(i)); } - view_space = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_VIEW); - world_space = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_STAGE); - // world_space = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_LOCAL); + spaces[size_t(xr::spaces::view)] = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_VIEW); + spaces[size_t(xr::spaces::world)] = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_STAGE); config.emplace(xr_system_id); if (xr_instance.has_extension(XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME)) diff --git a/client/application.h b/client/application.h index 79a27592..31f4b0f8 100644 --- a/client/application.h +++ b/client/application.h @@ -115,15 +115,9 @@ class application : public singleton xr::session xr_session; XrSessionState session_state = XR_SESSION_STATE_UNKNOWN; - xr::space world_space; - xr::space view_space; + std::array spaces; xr::actionset xr_actionset; std::vector> actions; - xr::space left_grip_space; - xr::space left_aim_space; - xr::space right_grip_space; - xr::space right_aim_space; - xr::space eye_gaze_space; bool hand_tracking_supported = false; xr::hand_tracker left_hand; @@ -294,30 +288,11 @@ public: return (T)proc; } - static XrSpace view() + static XrSpace space(xr::spaces s) { - return instance().view_space; - }; - static XrSpace left_grip() - { - return instance().left_grip_space; - }; - static XrSpace left_aim() - { - return instance().left_aim_space; - }; - static XrSpace right_grip() - { - return instance().right_grip_space; - }; - static XrSpace right_aim() - { - return instance().right_aim_space; - }; - static XrSpace eye_gaze() - { - return instance().eye_gaze_space; - }; + assert(size_t(s) >= 0 and s < xr::spaces::count); + return instance().spaces[size_t(s)]; + } static wifi_lock & get_wifi_lock() { diff --git a/client/scene.cpp b/client/scene.cpp index 011d76d0..ba25ce4d 100644 --- a/client/scene.cpp +++ b/client/scene.cpp @@ -30,8 +30,6 @@ scene::scene(key, const meta & current_meta) : instance(application::instance().xr_instance), system(application::instance().xr_system_id), session(application::instance().xr_session), - world_space(application::instance().world_space), - view_space(application::instance().view_space), viewconfig(application::instance().app_info.viewconfig), vk_instance(application::instance().vk_instance), diff --git a/client/scene.h b/client/scene.h index 738f9447..423756ed 100644 --- a/client/scene.h +++ b/client/scene.h @@ -73,8 +73,6 @@ protected: xr::instance & instance; xr::system & system; xr::session & session; - xr::space & world_space; - xr::space & view_space; XrViewConfigurationType viewconfig; bool focused = false; diff --git a/client/scenes/input_profile.cpp b/client/scenes/input_profile.cpp index 034d8f3d..30b4121b 100644 --- a/client/scenes/input_profile.cpp +++ b/client/scenes/input_profile.cpp @@ -211,9 +211,9 @@ input_profile::input_profile(const std::filesystem::path & json_profile, scene_l XrSpace space; if (layout == "left") - space = application::left_grip(); + space = application::space(xr::spaces::grip_left); else if (layout == "right") - space = application::right_grip(); + space = application::space(xr::spaces::grip_right); else continue; @@ -292,13 +292,13 @@ void input_profile::apply(XrSpace world_space, XrTime predicted_display_time, bo { for (auto && [space, node]: model_handles) { - if (space == application::left_grip() && hide_left) + if (space == application::space(xr::spaces::grip_left) && hide_left) { node->visible = false; continue; } - if (space == application::right_grip() && hide_right) + if (space == application::space(xr::spaces::grip_right) && hide_right) { node->visible = false; continue; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index daef9cc8..16804ab8 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -372,12 +372,12 @@ std::optional scenes::lobby::check_recenter_action(XrTime predicted_d if (application::read_action_bool(recenter_left_action).value_or(std::pair{0, false}).second) { - aim = application::locate_controller(application::left_aim(), world_space, predicted_display_time); + aim = application::locate_controller(application::space(xr::spaces::aim_left), application::space(xr::spaces::world), predicted_display_time); } if (application::read_action_bool(recenter_right_action).value_or(std::pair{0, false}).second) { - aim = application::locate_controller(application::right_aim(), world_space, predicted_display_time); + aim = application::locate_controller(application::space(xr::spaces::aim_right), application::space(xr::spaces::world), predicted_display_time); } if (aim) @@ -534,13 +534,14 @@ void scenes::lobby::render(const XrFrameState & frame_state) session.begin_frame(); + XrSpace world_space = application::space(xr::spaces::world); auto [flags, views] = session.locate_views(viewconfig, frame_state.predictedDisplayTime, world_space); assert(views.size() == swapchains_lobby.size()); bool hide_left_controller = false; bool hide_right_controller = false; - std::optional> head_position = application::locate_controller(application::view(), world_space, frame_state.predictedDisplayTime); + std::optional> head_position = application::locate_controller(application::space(xr::spaces::view), world_space, frame_state.predictedDisplayTime); std::optional new_gui_position; if (head_position) @@ -731,7 +732,7 @@ void scenes::lobby::on_focused() } swapchain_imgui = xr::swapchain(session, device, swapchain_format, 1500, 1000); - imgui_ctx.emplace(physical_device, device, queue_family_index, queue, world_space, imgui_inputs, swapchain_imgui, glm::vec2{0.6, 0.4}); + imgui_ctx.emplace(physical_device, device, queue_family_index, queue, application::space(xr::spaces::world), imgui_inputs, swapchain_imgui, glm::vec2{0.6, 0.4}); try { diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 9dd781b1..1c80373b 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -94,7 +94,7 @@ std::shared_ptr scenes::stream::create(std::unique_ptrsession.locate_views( XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO, self->instance.now(), - application::view()); + application::space(xr::spaces::view)); assert(views.size() == info.fov.size()); @@ -209,7 +209,7 @@ void scenes::stream::on_focused() device, queue_family_index, queue, - view_space, + application::space(xr::spaces::world), std::span{}, swapchain_imgui, glm::vec2{1.0, 0.6666}); @@ -734,7 +734,7 @@ void scenes::stream::render(const XrFrameState & frame_state) XrCompositionLayerProjection layer{ .type = XR_TYPE_COMPOSITION_LAYER_PROJECTION, .layerFlags = 0, - .space = world_space, + .space = application::space(xr::spaces::world), .viewCount = (uint32_t)layer_view.size(), .views = layer_view.data(), }; diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index 3c56242f..2c6df05a 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -176,16 +176,17 @@ void scenes::stream::tracking() const XrDuration battery_check_interval = 30'000'000'000; // 30s #endif std::vector> spaces = { - {device_id::HEAD, application::view()}, - {device_id::LEFT_AIM, application::left_aim()}, - {device_id::LEFT_GRIP, application::left_grip()}, - {device_id::RIGHT_AIM, application::right_aim()}, - {device_id::RIGHT_GRIP, application::right_grip()}}; + {device_id::HEAD, application::space(xr::spaces::view)}, + {device_id::LEFT_AIM, application::space(xr::spaces::aim_left)}, + {device_id::LEFT_GRIP, application::space(xr::spaces::grip_left)}, + {device_id::RIGHT_AIM, application::space(xr::spaces::aim_right)}, + {device_id::RIGHT_GRIP, application::space(xr::spaces::grip_right)}}; if (application::get_eye_gaze_supported()) - spaces.push_back({device_id::EYE_GAZE, application::eye_gaze()}); + spaces.push_back({device_id::EYE_GAZE, application::space(xr::spaces::eye_gaze)}); - XrSpace view_space = application::view(); + XrSpace view_space = application::space(xr::spaces::view); + XrSpace world_space = application::space(xr::spaces::world); XrDuration tracking_period = 1'000'000; // Send tracking data every 1ms const XrDuration dt = 100'000; // Wake up 0.1ms before measuring the position diff --git a/client/xr/space.h b/client/xr/space.h index 41bfe4f9..91a71e32 100644 --- a/client/xr/space.h +++ b/client/xr/space.h @@ -25,4 +25,17 @@ namespace xr { using space = utils::handle; + +enum class spaces +{ + world, + view, + eye_gaze, + grip_left, + grip_right, + aim_left, + aim_right, + + count +}; } // namespace xr