From ce437ce2b40830c05c5baa0abcdd0ede84fa2bb6 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sun, 13 Jul 2025 22:08:42 +0200 Subject: [PATCH] Add compact view in the stream scene --- client/scenes/stream.cpp | 7 +- client/scenes/stream.h | 13 ++- client/scenes/stream_gui.cpp | 169 +++++++++++++++++++++++++++-------- 3 files changed, 145 insertions(+), 44 deletions(-) diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index bed1636a..20ed89fb 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -1138,6 +1138,7 @@ void scenes::stream::render(const XrFrameState & frame_state) if (gui_status != gui_status::hidden) imgui_layers = draw_gui(frame_state.predictedDisplayTime); + // Display controllers and handle recentering if (gui_status == gui_status::interactable) { if (recentering_context) @@ -1192,6 +1193,7 @@ void scenes::stream::render(const XrFrameState & frame_state) } } + // Display the GUI if (gui_status != gui_status::hidden) { // Lock the GUI position to the head @@ -1212,9 +1214,9 @@ void scenes::stream::render(const XrFrameState & frame_state) } } + // Display the controller rays if (gui_status == gui_status::interactable) { - // Add the layer with the controller rays render_world(XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT, world_space, views, @@ -1287,15 +1289,16 @@ void scenes::stream::render(const XrFrameState & frame_state) if (state_1.currentState and state_2.currentState and (state_1.changedSinceLastSync or state_2.changedSinceLastSync)) { + current_tab = tab::stats; switch (gui_status) { case gui_status::hidden: + case gui_status::compact: case gui_status::overlay_only: gui_status = gui_status::interactable; break; case gui_status::interactable: gui_status = gui_status::overlay_only; - current_tab = tab::stats; break; } } diff --git a/client/scenes/stream.h b/client/scenes/stream.h index dcc59e83..51cc707f 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -137,6 +137,7 @@ private: { hidden, overlay_only, + compact, interactable }; std::atomic gui_status = gui_status::interactable; @@ -154,8 +155,6 @@ private: stats, settings, disconnect, - - hide, }; tab current_tab; @@ -222,8 +221,9 @@ private: vk::raii::QueryPool query_pool = nullptr; bool query_pool_filled = false; - uint64_t bytes_sent = 0; + // Used for plots uint64_t bytes_received = 0; + uint64_t bytes_sent = 0; float bandwidth_rx = 0; float bandwidth_tx = 0; @@ -278,8 +278,15 @@ private: XrTime last_metric_time = 0; int metrics_offset = 0; + // Used for compact view + float compact_bandwidth_rx = 0; + float compact_bandwidth_tx = 0; + float compact_cpu_time = 0; + float compact_gpu_time = 0; + void accumulate_metrics(XrTime predicted_display_time, const std::vector> & blit_handles, const gpu_timestamps & timestamps); void gui_performance_metrics(); + void gui_compact_view(); void gui_settings(); std::vector draw_gui(XrTime predicted_display_time); }; diff --git a/client/scenes/stream_gui.cpp b/client/scenes/stream_gui.cpp index 2cfdff74..73ed371f 100644 --- a/client/scenes/stream_gui.cpp +++ b/client/scenes/stream_gui.cpp @@ -22,11 +22,14 @@ #include "stream.h" #include "application.h" +#include "constants.h" +#include "imgui.h" #include "imgui_internal.h" #include "implot.h" #include "utils/i18n.h" #include "utils/ranges.h" #include +#include #include #include #include @@ -95,6 +98,12 @@ void scenes::stream::accumulate_metrics(XrTime predicted_display_time, const std bandwidth_rx = 0.8 * bandwidth_rx + 0.2 * float(rx - bytes_received) / dt; bandwidth_tx = 0.8 * bandwidth_tx + 0.2 * float(tx - bytes_sent) / dt; + // Filter more aggressively for the compact view + compact_bandwidth_rx = 0.99 * compact_bandwidth_rx + 0.01 * float(rx - bytes_received) / dt; + compact_bandwidth_tx = 0.99 * compact_bandwidth_tx + 0.01 * float(tx - bytes_sent) / dt; + compact_cpu_time = 0.99 * compact_cpu_time + 0.01 * application::get_cpu_time().count() * 1e-9f; + compact_gpu_time = 0.99 * compact_gpu_time + 0.01 * timestamps.gpu_time; + last_metric_time = predicted_display_time; bytes_received = rx; bytes_sent = tx; @@ -191,8 +200,8 @@ static bool RadioButtonWithoutCheckBox(const std::string & label, bool active, I return pressed; } -template -static bool RadioButtonWithoutCheckBox(const std::string & label, T & v, T v_button, ImVec2 size_arg) +template +static bool RadioButtonWithoutCheckBox(const std::string & label, T & v, U v_button, ImVec2 size_arg) { const bool pressed = RadioButtonWithoutCheckBox(label, v == v_button, size_arg); if (pressed) @@ -200,6 +209,46 @@ static bool RadioButtonWithoutCheckBox(const std::string & label, T & v, T v_but return pressed; } +// TODO factorize with the tooltip function of the lobby +static void tooltip(std::string_view text) +{ + // FIXME: this is incorrect if we use the docking branch of imgui + ImGuiViewport * viewport = ImGui::GetMainViewport(); + + ImVec2 pos{ + (ImGui::GetItemRectMin().x + ImGui::GetItemRectMax().x) / 2, + ImGui::GetItemRectMin().y - constants::style::tooltip_distance, + }; + + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, constants::style::tooltip_padding); + ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, constants::style::tooltip_rounding); + + // Clamp position to avoid overflowing on the right + auto & style = ImGui::GetStyle(); + const ImVec2 text_size = ImGui::CalcTextSize(text.data(), text.data() + text.size(), true); + const ImVec2 size = {text_size.x + style.WindowPadding.x * 2.0f, text_size.y + style.WindowPadding.y * 2.0f}; + pos.x = std::min(pos.x, viewport->Pos.x + viewport->Size.x - size.x / 2); + ImVec2 pivot = {0.5, 1}; + + // Move tooltip below the item if it overflows on the top + if (pos.y - size.y <= viewport->Pos.y) + { + pos.y = ImGui::GetItemRectMax().y + constants::style::tooltip_distance; + pivot.y = 0; + } + + ImGui::SetNextWindowPos(pos, ImGuiCond_Always, pivot); + if (ImGui::BeginTooltip()) + { + ImGui::PushStyleColor(ImGuiCol_Text, 0xffffffff); + ImGui::TextUnformatted(text.data(), text.data() + text.size()); + ImGui::PopStyleColor(); + ImGui::EndTooltip(); + } + + ImGui::PopStyleVar(2); +} + void scenes::stream::gui_performance_metrics() { const ImGuiStyle & style = ImGui::GetStyle(); @@ -388,6 +437,29 @@ void scenes::stream::gui_performance_metrics() } } +void scenes::stream::gui_compact_view() +{ + const auto & metrics = global_metrics[(metrics_offset + global_metrics.size() - 1) % global_metrics.size()]; + + if (ImGui::BeginTable("metrics", 2)) + { + auto f = [&](const char * label, float value, const char * unit) { + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::Text("%s", label); + ImGui::TableNextColumn(); + ImGui::Text("%.1f %s", value, unit); + }; + + f(_S("Download"), 8 * compact_bandwidth_rx * 1e-6, "Mbit/s"); + f(_S("Upload"), 8 * compact_bandwidth_tx * 1e-6, "Mbit/s"); + f(_S("CPU time"), compact_cpu_time * 1000, "ms"); + f(_S("GPU time"), compact_gpu_time * 1000, "ms"); + f(_S("Motion to photon latency"), std::chrono::duration_cast(tracking_control.max_offset).count() * 1e-3f, "ms"); + ImGui::EndTable(); + } +} + std::string openxr_post_processing_flag_name(XrCompositionLayerSettingsFlagsFB flag); void scenes::stream::gui_settings() { @@ -422,7 +494,7 @@ void scenes::stream::gui_settings() imgui_ctx->vibrate_on_hover(); if (ImGui::IsItemHovered()) { - // tooltip(_("Reduce flicker for high contrast edges.\nUseful when the input resolution is high compared to the headset display")); + tooltip(_("Reduce flicker for high contrast edges.\nUseful when the input resolution is high compared to the headset display")); } } { @@ -448,7 +520,7 @@ void scenes::stream::gui_settings() imgui_ctx->vibrate_on_hover(); if (ImGui::IsItemHovered()) { - // tooltip(_("Improve clarity of high contrast edges and counteract blur.\nUseful when the input resolution is low compared to the headset display")); + tooltip(_("Improve clarity of high contrast edges and counteract blur.\nUseful when the input resolution is low compared to the headset display")); } } ImGui::Unindent(); @@ -459,54 +531,76 @@ void scenes::stream::gui_settings() std::vector scenes::stream::draw_gui(XrTime predicted_display_time) { const float TabWidth = 300; + const ImVec2 MarginAroundWindow{50, 50}; const ImGuiStyle & style = ImGui::GetStyle(); imgui_ctx->new_frame(predicted_display_time); bool interactable = gui_status == gui_status::interactable; + bool compact = gui_status == gui_status::compact; + + ImVec2 content_size{ImGui::GetMainViewport()->Size - ImVec2{TabWidth, 0} - MarginAroundWindow * 2}; + ImVec2 content_center = MarginAroundWindow + content_size / 2 + ImVec2{TabWidth, 0}; if (interactable) { - ImGui::SetNextWindowPos({50, 50}); - ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size - ImVec2{100, 100}); + ImGui::SetNextWindowPos(MarginAroundWindow); + ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size - MarginAroundWindow * 2); + } + else if (compact) + { + ImGui::SetNextWindowPos(content_center, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); } else { - ImGui::SetNextWindowPos({TabWidth + 50, 50}); - ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size - ImVec2{TabWidth + 100, 100}); + ImGui::SetNextWindowPos(content_center, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); + ImGui::SetNextWindowSize(content_size); } ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, 0); - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {0, 0}); - ImGui::Begin("Performance metrics", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove); + if (compact) + { + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {8, 8}); + ImGui::Begin("Compact view", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize); + } + else + { + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {0, 0}); + ImGui::Begin("Performance metrics", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove); + } if (interactable) ImGui::SetCursorPos({TabWidth + 20, 20}); - else + else if (not compact) ImGui::SetCursorPos({20, 20}); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {8, 8}); - ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX(), 0)); - switch (current_tab) + ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 10); + if (compact) { - case tab::stats: - gui_performance_metrics(); - break; - - case tab::settings: - gui_settings(); - break; - - case tab::disconnect: - // TODO disconnect correctly - // application::pop_scene(); - break; - - case tab::hide: - break; + gui_compact_view(); } - ImGui::EndChild(); - ImGui::PopStyleVar(); + else + { + ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX(), 0)); + switch (current_tab) + { + case tab::stats: + gui_performance_metrics(); + break; + + case tab::settings: + gui_settings(); + break; + + case tab::disconnect: + // TODO disconnect correctly + // application::pop_scene(); + break; + } + ImGui::EndChild(); + } + ImGui::PopStyleVar(2); // ImGuiStyleVar_WindowPadding, ImGuiStyleVar_FrameRounding if (interactable) { @@ -522,10 +616,13 @@ std::vector scenes::stream::draw_gui(XrTime predicted_di RadioButtonWithoutCheckBox(ICON_FA_GEARS " " + _("Settings"), current_tab, tab::settings, {TabWidth, 0}); imgui_ctx->vibrate_on_hover(); - int n_items_at_end = 2; + int n_items_at_end = 3; 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"), current_tab, tab::hide, {TabWidth, 0}); + RadioButtonWithoutCheckBox(ICON_FA_EYE_SLASH " " + _("Hide"), gui_status, gui_status::hidden, {TabWidth, 0}); + imgui_ctx->vibrate_on_hover(); + + RadioButtonWithoutCheckBox(ICON_FA_MINIMIZE " " + _("Compact view"), gui_status, gui_status::compact, {TabWidth, 0}); imgui_ctx->vibrate_on_hover(); RadioButtonWithoutCheckBox(ICON_FA_DOOR_OPEN " " + _("Disconnect"), current_tab, tab::disconnect, {TabWidth, 0}); @@ -537,13 +634,7 @@ std::vector scenes::stream::draw_gui(XrTime predicted_di ImGui::PopStyleColor(); // ImGuiCol_ChildBg } ImGui::End(); - ImGui::PopStyleVar(2); - - if (current_tab == tab::hide) - { - gui_status = gui_status::hidden; - current_tab = tab::stats; - } + ImGui::PopStyleVar(2); // ImGuiStyleVar_ChildBorderSize, ImGuiStyleVar_WindowPadding std::vector layers; for (auto & layer: imgui_ctx->end_frame())