diff --git a/client/application.h b/client/application.h index ebf482ca..377749c1 100644 --- a/client/application.h +++ b/client/application.h @@ -476,6 +476,11 @@ public: return *instance().config; } + static XrSessionState get_session_state() + { + return instance().session_state; + } + static boost::locale::gnu_gettext::messages_info & get_messages_info() { return instance().messages_info; diff --git a/client/configuration.cpp b/client/configuration.cpp index 259d92e5..abe7f15e 100644 --- a/client/configuration.cpp +++ b/client/configuration.cpp @@ -205,9 +205,6 @@ configuration::configuration(xr::system & system) servers.emplace(data.service.txt["cookie"], data); } - if (auto val = root["show_performance_metrics"]; val.is_bool()) - show_performance_metrics = val.get_bool(); - if (auto val = root["preferred_refresh_rate"]; val.is_double()) preferred_refresh_rate = val.get_double(); @@ -269,7 +266,6 @@ configuration::configuration(xr::system & system) resolution_scale = 1.4; sgsr = {}; openxr_post_processing = {}; - show_performance_metrics = false; passthrough_enabled = system.passthrough_supported() == xr::system::passthrough_type::color; } } @@ -355,8 +351,7 @@ void configuration::save() std::ofstream json(application::get_config_path() / "client.json"); - json << "{\"servers\":[" << servers_str << "]," - << "\"show_performance_metrics\":" << std::boolalpha << show_performance_metrics; + json << "{\"servers\":[" << servers_str << "]"; if (preferred_refresh_rate) json << ",\"preferred_refresh_rate\":" << *preferred_refresh_rate; if (minimum_refresh_rate) diff --git a/client/configuration.h b/client/configuration.h index 16fa76f9..8d34faae 100644 --- a/client/configuration.h +++ b/client/configuration.h @@ -50,7 +50,6 @@ public: std::optional preferred_refresh_rate; std::optional minimum_refresh_rate; float resolution_scale = 1.4; - bool show_performance_metrics = false; bool passthrough_enabled = false; bool mic_unprocessed_audio = false; diff --git a/client/constants.h b/client/constants.h index de5c29ed..69d817d8 100644 --- a/client/constants.h +++ b/client/constants.h @@ -18,7 +18,6 @@ #pragma once -#include #include #include #include @@ -105,6 +104,16 @@ constexpr int zindex_controllers = 1; constexpr int zindex_recenter_tip = 2; } // namespace constants::lobby +namespace constants::stream +{ +constexpr float fade_delay = 3; +constexpr float fade_duration = 0.25; + +// Dimming for the streamed video when the GUI is interactable +constexpr float dimming_scale = 0.7; +constexpr float dimming_bias = 0.15; +} // namespace constants::stream + namespace constants::style { constexpr ImVec2 window_padding = {20, 20}; diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index 9cc8a570..3153ec55 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -750,67 +750,7 @@ void scenes::lobby::gui_settings() imgui_ctx->tooltip(_("This feature is not supported by your headset")); ImGui::EndDisabled(); - if (ImGui::Checkbox(_S("Show performance metrics"), &config.show_performance_metrics)) - config.save(); - imgui_ctx->vibrate_on_hover(); - if (ImGui::IsItemHovered()) - imgui_ctx->tooltip(_("Overlay can be toggled by pressing both thumbsticks")); - - ImGui::PopStyleVar(); - - if (config.show_performance_metrics) - { - float win_width = ImGui::GetWindowSize().x; - float win_height = ImGui::GetWindowSize().y; - - ImVec2 plot_size{ - win_width / 2 - style.ItemSpacing.x / 2, - win_height / 2}; - - static std::array cpu_time; - static std::array gpu_time; - static int offset = 0; - - float min_v = 0; - float max_v = 20; - - cpu_time[offset] = application::get_cpu_time().count() * 1.0e-6; - gpu_time[offset] = renderer->get_gpu_time() * 1'000; - offset = (offset + 1) % cpu_time.size(); - - ImPlot::PushStyleColor(ImPlotCol_PlotBg, IM_COL32(32, 32, 32, 64)); - ImPlot::PushStyleColor(ImPlotCol_FrameBg, IM_COL32(0, 0, 0, 0)); - ImPlot::PushStyleColor(ImPlotCol_AxisBg, IM_COL32(0, 0, 0, 0)); - ImPlot::PushStyleColor(ImPlotCol_AxisBgActive, IM_COL32(0, 0, 0, 0)); - ImPlot::PushStyleColor(ImPlotCol_AxisBgHovered, IM_COL32(0, 0, 0, 0)); - - if (ImPlot::BeginPlot(_S("CPU time"), plot_size, ImPlotFlags_CanvasOnly)) - { - auto col = ImPlot::GetColormapColor(0); - - ImPlot::SetupAxes(nullptr, _S("CPU time [ms]"), ImPlotAxisFlags_NoDecorations, 0); - ImPlot::SetupAxesLimits(0, cpu_time.size() - 1, min_v, max_v, ImGuiCond_Always); - ImPlot::SetNextLineStyle(col); - ImPlot::SetNextFillStyle(col, 0.25); - ImPlot::PlotLine(_S("CPU time"), cpu_time.data(), cpu_time.size(), 1, 0, ImPlotLineFlags_Shaded, offset); - ImPlot::EndPlot(); - } - - ImGui::SameLine(); - - if (ImPlot::BeginPlot(_S("GPU time"), plot_size, ImPlotFlags_CanvasOnly)) - { - auto col = ImPlot::GetColormapColor(1); - - ImPlot::SetupAxes(nullptr, _S("GPU time [ms]"), ImPlotAxisFlags_NoDecorations, 0); - ImPlot::SetupAxesLimits(0, gpu_time.size() - 1, min_v, max_v, ImGuiCond_Always); - ImPlot::SetNextLineStyle(col); - ImPlot::SetNextFillStyle(col, 0.25); - ImPlot::PlotLine(_S("GPU time"), gpu_time.data(), gpu_time.size(), 1, 0, ImPlotLineFlags_Shaded, offset); - ImPlot::EndPlot(); - } - ImPlot::PopStyleColor(5); - } + ImGui::PopStyleVar(); // ImGuiStyleVar_ItemSpacing } void scenes::lobby::gui_post_processing() @@ -1024,6 +964,58 @@ void scenes::lobby::gui_debug() std::filesystem::remove(application::get_config_path() / "client.json"); imgui_ctx->vibrate_on_hover(); + float win_width = ImGui::GetWindowSize().x; + float win_height = ImGui::GetWindowSize().y; + + ImGuiStyle & style = ImGui::GetStyle(); + ImVec2 plot_size{ + win_width / 2 - style.ItemSpacing.x / 2, + win_height / 2}; + + static std::array cpu_time; + static std::array gpu_time; + static int offset = 0; + + float min_v = 0; + float max_v = 20; + + cpu_time[offset] = application::get_cpu_time().count() * 1.0e-6; + gpu_time[offset] = renderer->get_gpu_time() * 1'000; + offset = (offset + 1) % cpu_time.size(); + + ImPlot::PushStyleColor(ImPlotCol_PlotBg, IM_COL32(32, 32, 32, 64)); + ImPlot::PushStyleColor(ImPlotCol_FrameBg, IM_COL32(0, 0, 0, 0)); + ImPlot::PushStyleColor(ImPlotCol_AxisBg, IM_COL32(0, 0, 0, 0)); + ImPlot::PushStyleColor(ImPlotCol_AxisBgActive, IM_COL32(0, 0, 0, 0)); + ImPlot::PushStyleColor(ImPlotCol_AxisBgHovered, IM_COL32(0, 0, 0, 0)); + + if (ImPlot::BeginPlot(_S("CPU time"), plot_size, ImPlotFlags_CanvasOnly)) + { + auto col = ImPlot::GetColormapColor(0); + + ImPlot::SetupAxes(nullptr, _S("CPU time [ms]"), ImPlotAxisFlags_NoDecorations, 0); + ImPlot::SetupAxesLimits(0, cpu_time.size() - 1, min_v, max_v, ImGuiCond_Always); + ImPlot::SetNextLineStyle(col); + ImPlot::SetNextFillStyle(col, 0.25); + ImPlot::PlotLine(_S("CPU time"), cpu_time.data(), cpu_time.size(), 1, 0, ImPlotLineFlags_Shaded, offset); + ImPlot::EndPlot(); + } + + ImGui::SameLine(); + + if (ImPlot::BeginPlot(_S("GPU time"), plot_size, ImPlotFlags_CanvasOnly)) + { + auto col = ImPlot::GetColormapColor(1); + + ImPlot::SetupAxes(nullptr, _S("GPU time [ms]"), ImPlotAxisFlags_NoDecorations, 0); + ImPlot::SetupAxesLimits(0, gpu_time.size() - 1, min_v, max_v, ImGuiCond_Always); + ImPlot::SetNextLineStyle(col); + ImPlot::SetNextFillStyle(col, 0.25); + ImPlot::PlotLine(_S("GPU time"), gpu_time.data(), gpu_time.size(), 1, 0, ImPlotLineFlags_Shaded, offset); + ImPlot::EndPlot(); + } + ImPlot::PopStyleColor(5); + ImGui::PopStyleVar(); } #endif diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 6bcfc973..049bd65d 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -16,6 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#include "constants.h" #include "utils/overloaded.h" #include "xr/fb_body_tracker.h" #include "xr/fb_face_tracker2.h" @@ -384,92 +385,91 @@ std::shared_ptr scenes::stream::create(std::unique_ptrget_default_material()); + + std::string profile = controller_name(); + input.emplace( + *this, + "controllers/" + profile + "/profile.json", + layer_controllers, + layer_rays); + + spdlog::info("Loaded input profile {}", input->id); + + for (auto i: {xr::spaces::aim_left, xr::spaces::aim_right, xr::spaces::grip_left, xr::spaces::grip_right}) { - auto views = system.view_configuration_views(viewconfig); - // stream_view = override_view(views[0], guess_model()); - width = views[0].recommendedImageRectWidth; - height = views[0].recommendedImageRectHeight; + auto [p, q] = input->offset[i] = controller_offset(controller_name(), i); - renderer.emplace(device, physical_device, queue, commandpool); - loader.emplace(device, physical_device, queue, application::queue_family_index(), renderer->get_default_material()); - - std::string profile = controller_name(); - input.emplace( - *this, - "controllers/" + profile + "/profile.json", - layer_controllers, - layer_rays); - - spdlog::info("Loaded input profile {}", input->id); - - for (auto i: {xr::spaces::aim_left, xr::spaces::aim_right, xr::spaces::grip_left, xr::spaces::grip_right}) - { - auto [p, q] = input->offset[i] = controller_offset(controller_name(), i); - - auto rot = glm::degrees(glm::eulerAngles(q)); - spdlog::info("Initializing offset of space {} to ({}, {}, {}) mm, ({}, {}, {})°", - magic_enum::enum_name(i), - 1000 * p.x, - 1000 * p.y, - 1000 * p.z, - rot.x, - rot.y, - rot.z); - } - - std::array imgui_inputs{ - imgui_context::controller{ - .aim = get_action_space("left_aim"), - .offset = input->offset[xr::spaces::aim_left], - .trigger = get_action("left_trigger").first, - .squeeze = get_action("left_squeeze").first, - .scroll = get_action("left_scroll").first, - .haptic_output = get_action("left_haptic").first, - }, - imgui_context::controller{ - .aim = get_action_space("right_aim"), - .offset = input->offset[xr::spaces::aim_right], - .trigger = get_action("right_trigger").first, - .squeeze = get_action("right_squeeze").first, - .scroll = get_action("right_scroll").first, - .haptic_output = get_action("right_haptic").first, - }, - }; - - swapchain_imgui = xr::swapchain( - session, - device, - swapchain_format, - 1800, - 1000); - - imgui_context::viewport vp{ - .space = xr::spaces::world, - // Position and orientation are set at each frame - .size = {1.2, 0.6666}, - .vp_origin = {0, 0}, - .vp_size = {1800, 1000}, - }; - - imgui_ctx.emplace(physical_device, - device, - queue_family_index, - queue, - imgui_inputs, - swapchain_imgui, - std::vector{vp}); - - plots_toggle_1 = get_action("plots_toggle_1").first; - plots_toggle_2 = get_action("plots_toggle_2").first; - recenter_left = get_action("recenter_left").first; - recenter_right = get_action("recenter_right").first; - foveation_pitch = get_action("foveation_pitch").first; - foveation_distance = get_action("foveation_distance").first; - foveation_ok = get_action("foveation_ok").first; - foveation_cancel = get_action("foveation_cancel").first; + auto rot = glm::degrees(glm::eulerAngles(q)); + spdlog::info("Initializing offset of space {} to ({}, {}, {}) mm, ({}, {}, {})°", + magic_enum::enum_name(i), + 1000 * p.x, + 1000 * p.y, + 1000 * p.z, + rot.x, + rot.y, + rot.z); } + std::array imgui_inputs{ + imgui_context::controller{ + .aim = get_action_space("left_aim"), + .offset = input->offset[xr::spaces::aim_left], + .trigger = get_action("left_trigger").first, + .squeeze = get_action("left_squeeze").first, + .scroll = get_action("left_scroll").first, + .haptic_output = get_action("left_haptic").first, + }, + imgui_context::controller{ + .aim = get_action_space("right_aim"), + .offset = input->offset[xr::spaces::aim_right], + .trigger = get_action("right_trigger").first, + .squeeze = get_action("right_squeeze").first, + .scroll = get_action("right_scroll").first, + .haptic_output = get_action("right_haptic").first, + }, + }; + + swapchain_imgui = xr::swapchain( + session, + device, + swapchain_format, + 1800, + 1000); + + imgui_context::viewport vp{ + .space = xr::spaces::world, + // Position and orientation are set at each frame + .size = {1.2, 0.6666}, + .vp_origin = {0, 0}, + .vp_size = {1800, 1000}, + }; + + imgui_ctx.emplace(physical_device, + device, + queue_family_index, + queue, + imgui_inputs, + swapchain_imgui, + std::vector{vp}); + + plots_toggle_1 = get_action("plots_toggle_1").first; + plots_toggle_2 = get_action("plots_toggle_2").first; + recenter_left = get_action("recenter_left").first; + recenter_right = get_action("recenter_right").first; + foveation_pitch = get_action("foveation_pitch").first; + foveation_distance = get_action("foveation_distance").first; + foveation_ok = get_action("foveation_ok").first; + foveation_cancel = get_action("foveation_cancel").first; + assert(video_stream_description); std::unique_lock lock(decoder_mutex); setup_reprojection_swapchain( @@ -479,9 +479,8 @@ void scenes::stream::on_focused() void scenes::stream::on_unfocused() { - if (renderer) - renderer->wait_idle(); // Must be before the scene data because the renderer uses its descriptor sets - world.clear(); // Must be cleared before the renderer so that the descriptor sets are freed before their pools + renderer->wait_idle(); // Must be before the scene data because the renderer uses its descriptor sets + world.clear(); // Must be cleared before the renderer so that the descriptor sets are freed before their pools input.reset(); loader.reset(); renderer.reset(); @@ -658,27 +657,6 @@ std::shared_ptr scenes::stream::accumulator_imag return nullptr; } -// Return the vector v such that dot(v, x) > 0 iff x is on the side where the composition layer is visible -static glm::vec4 compute_ray_limits(const XrPosef & pose, float margin = 0) -{ - glm::quat q{ - pose.orientation.w, - pose.orientation.x, - pose.orientation.y, - pose.orientation.z, - }; - - glm::vec3 p{ - pose.position.x, - pose.position.y, - pose.position.z, - }; - - glm::vec3 normal = glm::column(glm::mat3_cast(q), 2); - - return glm::vec4(normal, -glm::dot(p, normal) - margin); -} - void scenes::stream::update_gui_position(xr::spaces controller) { auto aim = application::locate_controller( @@ -734,9 +712,6 @@ void scenes::stream::update_gui_position(xr::spaces controller) bool scenes::stream::is_gui_interactable() const { - if (not imgui_ctx) - return false; - switch (gui_status) { case gui_status::stats: @@ -1160,126 +1135,29 @@ void scenes::stream::render(const XrFrameState & frame_state) application::space(xr::spaces::world), std::move(layer_view)); - bool interactable = is_gui_interactable(); + if (composition_layer_color_scale_bias_supported) + { + if (is_gui_interactable()) + dimming = dimming + frame_state.predictedDisplayPeriod / (1e9 * constants::stream::fade_duration); + else + dimming = dimming - frame_state.predictedDisplayPeriod / (1e9 * constants::stream::fade_duration); - if (composition_layer_color_scale_bias_supported and imgui_ctx and interactable) - set_color_scale_bias({0.7, 0.7, 0.7, 1}, {0.15, 0.15, 0.15, 0}); + dimming = std::clamp(dimming, 0, 1); + float x = dimming * dimming * (3 - 2 * dimming); // Easing function + + float scale = std::lerp(1, constants::stream::dimming_scale, x); + float bias = std::lerp(0, constants::stream::dimming_bias, x); + + set_color_scale_bias({scale, scale, scale, 1}, {bias, bias, bias, 0}); + } if (const configuration::openxr_post_processing_settings openxr_post_processing = application::get_config().openxr_post_processing; (openxr_post_processing.sharpening | openxr_post_processing.super_sampling) > 0) set_layer_settings(openxr_post_processing.sharpening | openxr_post_processing.super_sampling); - if (imgui_ctx) - { - XrSpace world_space = application::space(xr::spaces::world); - std::vector imgui_layers; - accumulate_metrics(frame_state.predictedDisplayTime, current_blit_handles, timestamps); - auto views = session.locate_views(viewconfig, frame_state.predictedDisplayTime, world_space).second; + accumulate_metrics(frame_state.predictedDisplayTime, current_blit_handles, timestamps); - imgui_ctx->set_controllers_enabled(interactable); - if (gui_status != gui_status::hidden) - imgui_layers = draw_gui(frame_state.predictedDisplayTime, frame_state.predictedDisplayPeriod); - - // Display controllers and handle recentering - if (interactable) - { - if (recentering_context) - { - xr::spaces controller = std::get<0>(*recentering_context); - bool state; - switch (controller) - { - case xr::spaces::aim_left: - state = application::read_action_bool(recenter_left).value_or(std::pair{0, false}).second; - break; - case xr::spaces::aim_right: - state = application::read_action_bool(recenter_right).value_or(std::pair{0, false}).second; - break; - default: - state = false; - break; - } - - if (state) - update_gui_position(controller); - else - recentering_context.reset(); - } - else if (auto state = application::read_action_bool(recenter_left); state and state->second) - update_gui_position(xr::spaces::aim_left); - else if (auto state = application::read_action_bool(recenter_right); state and state->second) - update_gui_position(xr::spaces::aim_right); - else - recentering_context.reset(); - - std::vector ray_limits; - for (auto & layer: imgui_layers) - { - ray_limits.push_back(compute_ray_limits(layer.pose)); - } - - input->apply(world, world_space, frame_state.predictedDisplayTime, false, false, ray_limits); - - // Add the layer with the controllers - if (composition_layer_depth_test_supported) - { - render_world(XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT, - world_space, - views, - width, - height, - true, - layer_controllers, - {}); - set_depth_test(true, XR_COMPARE_OP_ALWAYS_FB); - } - } - - // Display the GUI - if (gui_status != gui_status::hidden) - { - // Lock the GUI position to the head - std::optional> head_position = application::locate_controller(application::space(xr::spaces::view), world_space, frame_state.predictedDisplayTime); - if (head_position) - { - if (gui_status == gui_status::foveation_settings) - { - glm::mat3 M = glm::mat3_cast(head_position->second); - imgui_ctx->layers()[0].orientation = head_position->second; - imgui_ctx->layers()[0].position = head_position->first + M * glm::vec3{0, -override_foveation_distance * sin(override_foveation_pitch), -override_foveation_distance}; - } - else - { - glm::mat3 M = glm::mat3_cast(head_position->second); - imgui_ctx->layers()[0].orientation = head_position->second * head_gui_orientation; - imgui_ctx->layers()[0].position = head_position->first + M * head_gui_position; - } - } - - // Add the layer with the GUI - for (auto & layer: imgui_layers) - { - add_quad_layer(layer.layerFlags, layer.space, layer.eyeVisibility, layer.subImage, layer.pose, layer.size); - if (composition_layer_depth_test_supported) - set_depth_test(true, XR_COMPARE_OP_LESS_FB); - } - } - - // Display the controller rays - if (interactable) - { - render_world(XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT, - world_space, - views, - width, - height, - composition_layer_depth_test_supported, - composition_layer_depth_test_supported ? layer_rays : layer_controllers | layer_rays, - {}); - if (composition_layer_depth_test_supported) - set_depth_test(true, XR_COMPARE_OP_LESS_FB); - } - } + draw_gui(frame_state.predictedDisplayTime, frame_state.predictedDisplayPeriod); try { @@ -1350,10 +1228,8 @@ void scenes::stream::render(const XrFrameState & frame_state) case gui_status::stats: case gui_status::settings: - gui_status = gui_status::overlay_only; - break; - case gui_status::foveation_settings: + gui_status = gui_status::hidden; break; } } @@ -1637,9 +1513,15 @@ void scenes::stream::on_xr_event(const xr::event & event) }); break; case XR_TYPE_EVENT_DATA_SESSION_STATE_CHANGED: - network_session->send_control(from_headset::session_state_changed{ - .state = event.state_changed.state, - }); + // Override session state if the GUI is interactable + if (event.state_changed.state == XR_SESSION_STATE_FOCUSED and is_gui_interactable()) + network_session->send_control(from_headset::session_state_changed{ + .state = XR_SESSION_STATE_VISIBLE, + }); + else + network_session->send_control(from_headset::session_state_changed{ + .state = event.state_changed.state, + }); break; case XR_TYPE_EVENT_DATA_USER_PRESENCE_CHANGED_EXT: network_session->send_control(from_headset::user_presence_changed{ diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 19ac1a4c..b1826560 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -145,7 +145,11 @@ private: bool is_gui_interactable() const; - std::atomic gui_status = gui_status::stats; + std::atomic gui_status = gui_status::hidden; + enum gui_status last_gui_status = gui_status::hidden; + XrTime gui_status_last_change; + float dimming = 0; + XrAction plots_toggle_1 = XR_NULL_HANDLE; XrAction plots_toggle_2 = XR_NULL_HANDLE; XrAction recenter_left = XR_NULL_HANDLE; @@ -293,6 +297,6 @@ private: void gui_compact_view(); void gui_settings(); void gui_foveation_settings(float predicted_display_period); - std::vector draw_gui(XrTime predicted_display_time, XrDuration predicted_display_period); + void draw_gui(XrTime predicted_display_time, XrDuration predicted_display_period); }; } // namespace scenes diff --git a/client/scenes/stream_gui.cpp b/client/scenes/stream_gui.cpp index 17e80362..aa2f1311 100644 --- a/client/scenes/stream_gui.cpp +++ b/client/scenes/stream_gui.cpp @@ -31,9 +31,11 @@ #include #include #include +#include #include #include #include +#include namespace { @@ -393,7 +395,7 @@ void scenes::stream::gui_performance_metrics() if (is_gui_interactable()) ImGui::Text("%s", _S("Press the grip button to move the window")); else - ImGui::Text("%s", _S("Press both thumbsticks to interact")); + ImGui::Text("%s", _S("Press both thumbsticks to display the GUI")); } } @@ -590,47 +592,133 @@ void scenes::stream::gui_foveation_settings(float predicted_display_period) }); } -std::vector scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicted_display_period) +// Return the vector v such that dot(v, x) > 0 iff x is on the side where the composition layer is visible +static glm::vec4 compute_ray_limits(const XrPosef & pose, float margin = 0) { - const float TabWidth = 300; - const ImVec2 MarginAroundWindow{50, 50}; + glm::quat q{ + pose.orientation.w, + pose.orientation.x, + pose.orientation.y, + pose.orientation.z, + }; + + glm::vec3 p{ + pose.position.x, + pose.position.y, + pose.position.z, + }; + + glm::vec3 normal = glm::column(glm::mat3_cast(q), 2); + + return glm::vec4(normal, -glm::dot(p, normal) - margin); +} + +void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicted_display_period) +{ + bool interactable = is_gui_interactable(); + XrSpace world_space = application::space(xr::spaces::world); + auto views = session.locate_views(viewconfig, predicted_display_time, world_space).second; + + imgui_ctx->set_controllers_enabled(interactable); + + if (gui_status != last_gui_status) + { + last_gui_status = gui_status; + gui_status_last_change = predicted_display_time; + + // Override session state if the GUI is interactable + if (not is_gui_interactable()) + network_session->send_control(from_headset::session_state_changed{ + .state = application::get_session_state(), + }); + else if (application::get_session_state() == XR_SESSION_STATE_FOCUSED) + network_session->send_control(from_headset::session_state_changed{ + .state = XR_SESSION_STATE_VISIBLE, + }); + } + + float alpha = 1; + if (gui_status == gui_status::hidden) + { + float t = (predicted_display_time - gui_status_last_change) * 1.e-9f; + + alpha = std::clamp(1 - (t - constants::stream::fade_delay) / constants::stream::fade_duration, 0, 1); + + if (alpha == 0) + return; + } + + // Lock the GUI position to the head, do it before displaying the GUI to avoid being off by one frame when gui_status changes + std::optional> head_position = application::locate_controller(application::space(xr::spaces::view), world_space, predicted_display_time); + if (head_position) + { + glm::mat3 M = glm::mat3_cast(head_position->second); + switch (gui_status) + { + case gui_status::foveation_settings: + imgui_ctx->layers()[0].orientation = head_position->second; + imgui_ctx->layers()[0].position = head_position->first + M * glm::vec3{0, -override_foveation_distance * sin(override_foveation_pitch), -override_foveation_distance}; + break; + + case gui_status::hidden: + // Always use the same position for the GUI shortcut tip + imgui_ctx->layers()[0].orientation = head_position->second; + imgui_ctx->layers()[0].position = head_position->first + M * glm::vec3{0.0, -0.3, -1.2}; + break; + + case gui_status::overlay_only: + case gui_status::compact: + case gui_status::stats: + case gui_status::settings: + imgui_ctx->layers()[0].orientation = head_position->second * head_gui_orientation; + imgui_ctx->layers()[0].position = head_position->first + M * head_gui_position; + break; + } + } + + const float tab_width = 300; + const ImVec2 margin_around_window{50, 50}; const ImGuiStyle & style = ImGui::GetStyle(); imgui_ctx->new_frame(predicted_display_time); - bool interactable = gui_status == gui_status::stats or gui_status == gui_status::settings; - bool compact = gui_status == gui_status::compact or gui_status == gui_status::foveation_settings; - - ImVec2 content_size{ImGui::GetMainViewport()->Size - ImVec2{TabWidth, 0} - MarginAroundWindow * 2}; - ImVec2 content_center = MarginAroundWindow + content_size / 2 + ImVec2{TabWidth, 0}; + ImVec2 content_size{ImGui::GetMainViewport()->Size - ImVec2{tab_width, 0} - margin_around_window * 2}; + ImVec2 content_center = margin_around_window + content_size / 2 + ImVec2{tab_width, 0}; + bool display_tabs, always_auto_resize; switch (gui_status) { - case gui_status::hidden: - break; - case gui_status::overlay_only: ImGui::SetNextWindowPos(content_center, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); ImGui::SetNextWindowSize(content_size); + always_auto_resize = false; + display_tabs = false; break; + case gui_status::hidden: case gui_status::foveation_settings: ImGui::SetNextWindowPos(ImGui::GetMainViewport()->Size / 2, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); + always_auto_resize = true; + display_tabs = false; break; case gui_status::compact: ImGui::SetNextWindowPos(content_center, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); + always_auto_resize = true; + display_tabs = false; break; case gui_status::stats: case gui_status::settings: - ImGui::SetNextWindowPos(MarginAroundWindow); - ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size - MarginAroundWindow * 2); + ImGui::SetNextWindowPos(margin_around_window); + ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size - margin_around_window * 2); + always_auto_resize = false; + display_tabs = true; break; } ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, 0); - if (compact) + if (always_auto_resize) { ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {8, 8}); ImGui::Begin("Compact view", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize); @@ -647,6 +735,9 @@ std::vector scenes::stream::draw_gui(XrTime predicted_di switch (gui_status) { case gui_status::hidden: + ImGui::PushFont(nullptr, constants::gui::font_size_large); + ImGui::Text("%s", _S("Press both thumbsticks to display the GUI")); + ImGui::PopFont(); break; case gui_status::overlay_only: @@ -661,14 +752,14 @@ std::vector scenes::stream::draw_gui(XrTime predicted_di break; case gui_status::stats: - ImGui::SetCursorPos({TabWidth + 20, 20}); + ImGui::SetCursorPos({tab_width + 20, 20}); ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX(), 0)); gui_performance_metrics(); ImGui::EndChild(); break; case gui_status::settings: - ImGui::SetCursorPos({TabWidth + 20, 20}); + ImGui::SetCursorPos({tab_width + 20, 20}); ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX(), 0)); gui_settings(); ImGui::EndChild(); @@ -681,34 +772,34 @@ std::vector scenes::stream::draw_gui(XrTime predicted_di ImGui::PopStyleVar(2); // ImGuiStyleVar_WindowPadding, ImGuiStyleVar_FrameRounding - if (interactable) + if (display_tabs) { ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(0, 0, 0, 255)); ImGui::SetCursorPos(style.WindowPadding); { - ImGui::BeginChild("Tabs", {TabWidth, ImGui::GetContentRegionMax().y - ImGui::GetWindowContentRegionMin().y}); + ImGui::BeginChild("Tabs", {tab_width, ImGui::GetContentRegionMax().y - ImGui::GetWindowContentRegionMin().y}); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(10, 10)); - RadioButtonWithoutCheckBox(ICON_FA_COMPUTER " " + _("Stats"), gui_status, gui_status::stats, {TabWidth, 0}); + RadioButtonWithoutCheckBox(ICON_FA_COMPUTER " " + _("Stats"), gui_status, gui_status::stats, {tab_width, 0}); imgui_ctx->vibrate_on_hover(); - RadioButtonWithoutCheckBox(ICON_FA_GEARS " " + _("Settings"), gui_status, gui_status::settings, {TabWidth, 0}); + RadioButtonWithoutCheckBox(ICON_FA_GEARS " " + _("Settings"), gui_status, gui_status::settings, {tab_width, 0}); imgui_ctx->vibrate_on_hover(); int n_items_at_end = 4; ImGui::SetCursorPosY(ImGui::GetContentRegionMax().y - n_items_at_end * ImGui::GetCurrentContext()->FontSize - (n_items_at_end * 2) * style.FramePadding.y - (n_items_at_end - 1) * style.ItemSpacing.y - style.WindowPadding.y); - RadioButtonWithoutCheckBox(ICON_FA_EYE_SLASH " " + _("Hide"), gui_status, gui_status::hidden, {TabWidth, 0}); + RadioButtonWithoutCheckBox(ICON_FA_EYE_SLASH " " + _("Hide"), gui_status, gui_status::hidden, {tab_width, 0}); imgui_ctx->vibrate_on_hover(); - RadioButtonWithoutCheckBox(ICON_FA_COMPUTER " " + _("Overlay only"), gui_status, gui_status::overlay_only, {TabWidth, 0}); + RadioButtonWithoutCheckBox(ICON_FA_COMPUTER " " + _("Overlay only"), gui_status, gui_status::overlay_only, {tab_width, 0}); imgui_ctx->vibrate_on_hover(); - RadioButtonWithoutCheckBox(ICON_FA_MINIMIZE " " + _("Compact view"), gui_status, gui_status::compact, {TabWidth, 0}); + RadioButtonWithoutCheckBox(ICON_FA_MINIMIZE " " + _("Compact view"), gui_status, gui_status::compact, {tab_width, 0}); imgui_ctx->vibrate_on_hover(); bool dummy = false; - if (RadioButtonWithoutCheckBox(ICON_FA_DOOR_OPEN " " + _("Disconnect"), dummy, true, {TabWidth, 0})) + if (RadioButtonWithoutCheckBox(ICON_FA_DOOR_OPEN " " + _("Disconnect"), dummy, true, {tab_width, 0})) exit(); imgui_ctx->vibrate_on_hover(); @@ -720,9 +811,99 @@ std::vector scenes::stream::draw_gui(XrTime predicted_di ImGui::End(); ImGui::PopStyleVar(2); // ImGuiStyleVar_ChildBorderSize, ImGuiStyleVar_WindowPadding - std::vector layers; - for (auto & layer: imgui_ctx->end_frame()) - layers.push_back(layer.second); + auto layers = imgui_ctx->end_frame(); - return layers; + // Display controllers and handle recentering + if (interactable) + { + if (recentering_context) + { + xr::spaces controller = std::get<0>(*recentering_context); + bool state; + switch (controller) + { + case xr::spaces::aim_left: + state = application::read_action_bool(recenter_left).value_or(std::pair{0, false}).second; + break; + case xr::spaces::aim_right: + state = application::read_action_bool(recenter_right).value_or(std::pair{0, false}).second; + break; + default: + state = false; + break; + } + + if (state) + update_gui_position(controller); + else + recentering_context.reset(); + } + else if (auto state = application::read_action_bool(recenter_left); state and state->second) + update_gui_position(xr::spaces::aim_left); + else if (auto state = application::read_action_bool(recenter_right); state and state->second) + update_gui_position(xr::spaces::aim_right); + else + recentering_context.reset(); + + std::vector ray_limits; + + for (auto [_, layer]: layers) + ray_limits.push_back(compute_ray_limits(layer.pose)); + + bool hide_left_controller = false; + bool hide_right_controller = false; + + if (application::get_hand_tracking_supported()) + { + auto left = application::get_left_hand().locate(world_space, predicted_display_time); + auto right = application::get_right_hand().locate(world_space, predicted_display_time); + + if (left and xr::hand_tracker::check_flags(*left, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0)) + hide_left_controller = true; + + if (right and xr::hand_tracker::check_flags(*right, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0)) + hide_right_controller = true; + } + + input->apply(world, world_space, predicted_display_time, hide_left_controller, hide_right_controller, ray_limits); + + // Add the layer with the controllers + if (composition_layer_depth_test_supported) + { + render_world(XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT, + world_space, + views, + width, + height, + true, + layer_controllers, + {}); + set_depth_test(true, XR_COMPARE_OP_ALWAYS_FB); + } + } + + // Add the layer with the GUI + for (auto [_, layer]: layers) + { + add_quad_layer(layer.layerFlags, layer.space, layer.eyeVisibility, layer.subImage, layer.pose, layer.size); + if (composition_layer_depth_test_supported) + set_depth_test(true, XR_COMPARE_OP_LESS_FB); + if (alpha < 1 and composition_layer_color_scale_bias_supported) + set_color_scale_bias({alpha, alpha, alpha, alpha}, {}); + } + + // Display the controller rays + if (interactable) + { + render_world(XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT, + world_space, + views, + width, + height, + composition_layer_depth_test_supported, + composition_layer_depth_test_supported ? layer_rays : layer_controllers | layer_rays, + {}); + if (composition_layer_depth_test_supported) + set_depth_test(true, XR_COMPARE_OP_LESS_FB); + } }