/* * WiVRn VR streaming * Copyright (C) 2024 Guillaume Meunier * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "imgui_impl.h" #include "implot.h" #include "application.h" #include "constants.h" #include "image_loader.h" #include "openxr/openxr.h" #include "ui_theme.h" #include "utils/mapped_file.h" #include "utils/ranges.h" #include "utils/strings.h" #include "vulkan/vulkan_enums.hpp" #include "vulkan/vulkan_handles.hpp" #include "vulkan/vulkan_to_string.hpp" #include "xr/space.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __ANDROID__ #include #include #include #else #include #endif /* Do not use: * * ImGui_ImplVulkanH_SelectSurfaceFormat * ImGui_ImplVulkanH_SelectPresentMode * ImGui_ImplVulkanH_GetMinImageCountFromPresentMode * ImGui_ImplVulkanH_CreateWindowSwapChain * ImGui_ImplVulkanH_CreateOrResizeWindow * ImGui_ImplVulkanH_DestroyWindow * * struct ImGui_ImplVulkanH_Window */ // Automatically generated by CMakeLists extern std::map> glyph_set_per_language; static vk::raii::RenderPass create_renderpass(vk::raii::Device & device, vk::Format format, bool clear) { vk::AttachmentDescription attachment{ .format = format, .samples = vk::SampleCountFlagBits::e1, .loadOp = clear ? vk::AttachmentLoadOp::eClear : vk::AttachmentLoadOp::eDontCare, .storeOp = vk::AttachmentStoreOp::eStore, .stencilLoadOp = vk::AttachmentLoadOp::eDontCare, .stencilStoreOp = vk::AttachmentStoreOp::eDontCare, .initialLayout = vk::ImageLayout::eUndefined, .finalLayout = vk::ImageLayout::eColorAttachmentOptimal, }; vk::AttachmentReference color_attachment{ .attachment = 0, .layout = vk::ImageLayout::eColorAttachmentOptimal, }; vk::SubpassDescription subpass = { .pipelineBindPoint = vk::PipelineBindPoint::eGraphics, .colorAttachmentCount = 1, .pColorAttachments = &color_attachment, }; vk::SubpassDependency dependency = { .srcSubpass = VK_SUBPASS_EXTERNAL, .dstSubpass = 0, .srcStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput, .dstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput, .srcAccessMask = {}, .dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite, }; return vk::raii::RenderPass( device, vk::RenderPassCreateInfo{ .attachmentCount = 1, .pAttachments = &attachment, .subpassCount = 1, .pSubpasses = &subpass, .dependencyCount = 1, .pDependencies = &dependency, }); } static void check_vk_result(VkResult result) { if (result < 0) { spdlog::error("Vulkan error in Dear ImGui: {}", vk::to_string((vk::Result)result)); abort(); } } static bool in_window(ImGuiWindow * window, ImVec2 position) { if (window->Hidden or not window->Active) return false; if (window->Pos.x > position.x or window->Pos.y > position.y or window->Pos.x + window->Size.x < position.x or window->Pos.y + window->Size.y < position.y) return false; return true; } static bool in_viewport(const imgui_context::viewport & viewport, ImVec2 position) { if (viewport.vp_origin.x > position.x or viewport.vp_origin.y > position.y or viewport.vp_origin.x + viewport.vp_size.x < position.x or viewport.vp_origin.y + viewport.vp_size.y < position.y) return false; return true; } static bool window_intersects_viewport(ImGuiWindow * window, const imgui_context::viewport & viewport) { ImRect w{window->Pos.x, window->Pos.y, window->Pos.x + window->Size.x, window->Pos.y + window->Size.y}; ImRect v(viewport.vp_origin.x, viewport.vp_origin.y, viewport.vp_origin.x + viewport.vp_size.x, viewport.vp_origin.y + viewport.vp_size.y); return w.Overlaps(v); } static float distance_to_window(ImGuiWindow * window, ImVec2 position) { if (window->Hidden or not window->Active) return std::numeric_limits::max(); float dx; if (position.x < window->Pos.x) dx = window->Pos.x - position.x; else if (position.x > window->Pos.x + window->Size.x) dx = position.x - (window->Pos.x + window->Size.x); else dx = 0; float dy; if (position.y < window->Pos.y) dy = window->Pos.y - position.y; else if (position.y > window->Pos.y + window->Size.y) dy = position.y - (window->Pos.y + window->Size.y); else dy = 0; return std::hypot(dx, dy); } static const std::array layout_bindings = { vk::DescriptorSetLayoutBinding{ .binding = 0, .descriptorType = vk::DescriptorType::eSampledImage, .descriptorCount = 1, .stageFlags = vk::ShaderStageFlagBits::eFragment, }}; imgui_textures::imgui_textures( vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, uint32_t queue_family_index, thread_safe & queue, std::shared_ptr image_cache) : physical_device(physical_device), device(device), queue(queue), ds_layout(device, vk::DescriptorSetLayoutCreateInfo{.bindingCount = layout_bindings.size(), .pBindings = layout_bindings.data()}), command_pool(device, vk::CommandPoolCreateInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient, .queueFamilyIndex = queue_family_index, }), image_cache(image_cache), descriptor_pool(device, ds_layout, layout_bindings) { if (not image_cache) this->image_cache = std::make_shared(device, physical_device, queue, queue_family_index); } imgui_textures::~imgui_textures() { device.waitIdle(); } std::vector> imgui_context::ray_plane_intersection(const imgui_context::controller_state & in) const { std::vector> intersections; for (const auto & i: layers_) { if (i.space != xr::spaces::world or i.tooltip_viewport) continue; auto M = glm::transpose(glm::mat3_cast(i.orientation)); // world-to-plane transform glm::quat q = (in.source == ImGuiMouseSource_VRHandTracking) ? i.orientation : in.aim_orientation; glm::vec3 controller_direction = -glm::column(glm::mat3_cast(q), 2); // The aim direction is -Z // Compute all vectors in the reference frame of the GUI plane glm::vec3 ray_start = M * (in.aim_position - i.position); glm::vec3 ray_dir = M * controller_direction; if (ray_dir.z < -0.0001f) { glm::vec2 coord; // ray_start + distance × ray_dir ∈ imgui plane // => ray_start.z + distance × ray_dir.z = 0 float distance = -ray_start.z / ray_dir.z; if (distance < constants::gui::min_controller_distance and in.source == ImGuiMouseSource_VRController) continue; if (distance < constants::gui::fingertip_distance_touching_thd_lo and in.source == ImGuiMouseSource_VRHandTracking) continue; coord.x = ray_start.x + distance * ray_dir.x; coord.y = ray_start.y + distance * ray_dir.y; // Convert from mesh coordinates to imgui coordinates coord = coord / i.size; if (fabs(coord.x) > 0.5 or fabs(coord.y) > 0.5) continue; intersections.emplace_back(ImVec2( (0.5 + coord.x) * i.vp_size.x + i.vp_origin.x, (0.5 - coord.y) * i.vp_size.y + i.vp_origin.y), distance); } } std::ranges::sort(intersections, [](auto & a, auto & b) { return a.second < b.second; }); return intersections; } glm::vec3 imgui_context::rw_from_vp(const ImVec2 & position) { for (auto & i: layers_) { if (not in_viewport(i, position)) continue; return i.position + glm::mat3_cast(i.orientation) * glm::vec3{ ((position.x - i.vp_origin.x) / i.vp_size.x - 0.5) * i.size.x, (-(position.y - i.vp_origin.y) / i.vp_size.y + 0.5) * i.size.y, 0}; } return {}; } imgui_context::imgui_frame & imgui_context::get_frame(vk::Image destination) { for (auto & i: frames) { if (i.destination == destination) return i; } auto & frame = frames.emplace_back(); frame.destination = destination; // Only 1 mipmap level for the framebuffer view frame.image_view_framebuffer = vk::raii::ImageView( device, vk::ImageViewCreateInfo{ .image = destination, .viewType = vk::ImageViewType::e2D, .format = format, .subresourceRange = { .aspectMask = vk::ImageAspectFlagBits::eColor, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1, }, }); frame.framebuffer = vk::raii::Framebuffer( device, vk::FramebufferCreateInfo{ .renderPass = *renderpass, .attachmentCount = 1, .pAttachments = &*frame.image_view_framebuffer, .width = size.width, .height = size.height, .layers = 1, }); return frame; } imgui_context::imgui_context( vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, uint32_t queue_family_index, thread_safe & queue, std::span controllers_, xr::swapchain && swapchain_, std::vector layers, std::shared_ptr image_cache) : imgui_textures( physical_device, device, queue_family_index, queue, image_cache), queue_family_index(queue_family_index), renderpass(create_renderpass(device, swapchain_.format(), true)), command_buffers(swapchain_.images().size()), size(swapchain_.extent().width, swapchain_.extent().height), format(swapchain_.format()), layers_(std::move(layers)), swapchain(std::move(swapchain_)), context(ImGui::CreateContext()), plot_context(ImPlot::CreateContext()), io((ImGui::SetCurrentContext(context), ImGui::GetIO())), world(application::space(xr::spaces::world)) { controllers.reserve(controllers_.size()); for (const auto & i: controllers_) controllers.emplace_back(i, controller_state{}); io.IniFilename = nullptr; for (command_buffer & cb: command_buffers) { cb.command_buffer = std::move(device.allocateCommandBuffers({ .commandPool = *command_pool, .commandBufferCount = 1, })[0]); cb.fence = device.createFence(vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled}); } ImGui_ImplVulkan_InitInfo init_info = { .Instance = *application::get_vulkan_instance(), .PhysicalDevice = *physical_device, .Device = *device, .QueueFamily = queue_family_index, .Queue = *queue.get_unsafe(), .DescriptorPoolSize = 100, .MinImageCount = 2, .ImageCount = (uint32_t)swapchain.images().size(), // used to cycle between VkBuffers in ImGui_ImplVulkan_RenderDrawData .PipelineCache = *application::get_pipeline_cache(), .PipelineInfoMain = { .RenderPass = *renderpass, .Subpass = 0, .MSAASamples = VK_SAMPLE_COUNT_1_BIT, }, .Allocator = nullptr, .CheckVkResultFn = check_vk_result, }; ImGui::SetCurrentContext(context); ImPlot::SetCurrentContext(plot_context); ImGui_ImplVulkan_Init(&init_info); initialize_fonts(); // Setup Dear ImGui style ImGui::StyleColorsDark(); // ImGui::StyleColorsClassic(); // ImGui::StyleColorsLight(); ImGuiStyle & style = ImGui::GetStyle(); style.WindowBorderSize = 0; style.DisabledAlpha = 0.2; style.Colors[ImGuiCol_ModalWindowDimBg] = {0, 0, 0, 0}; } #ifdef __ANDROID__ static std::vector find_font(std::u16string_view sample_text, const std::string & locale) { AFontMatcher * font_matcher = AFontMatcher_create(); if (!font_matcher) throw std::runtime_error("AFontMatcher_create"); AFontMatcher_setFamilyVariant(font_matcher, AFAMILY_VARIANT_DEFAULT); AFontMatcher_setLocales(font_matcher, locale.c_str()); AFontMatcher_setStyle(font_matcher, AFONT_WEIGHT_NORMAL, false); std::vector fonts; while (!sample_text.empty()) { uint32_t runlength = 0; AFont * font = AFontMatcher_match(font_matcher, "sans-serif", (uint16_t *)sample_text.data(), sample_text.size(), &runlength); fonts.emplace_back(AFont_getFontFilePath(font)); AFont_close(font); if (runlength == 0) break; sample_text = sample_text.substr(runlength); } AFontMatcher_destroy(font_matcher); return fonts; } #else static std::vector find_font(std::u16string sample_text, const std::string & locale) { // See https://www.camconn.cc/post/how-to-fontconfig-lib-c/ static FcConfig * config = FcInitLoadConfigAndFonts(); FcPattern * pattern = FcNameParse((const FcChar8 *)constants::gui::font_name); if (!pattern) throw std::runtime_error("Failed to create Fontconfig pattern"); FcConfigSubstitute(config, pattern, FcMatchPattern); FcDefaultSubstitute(pattern); // Add the locale/language to the pattern FcPatternAddString(pattern, FC_LANG, (const FcChar8 *)locale.c_str()); // Add the text to the pattern (to determine appropriate characters) FcCharSet * charset = FcCharSetCreate(); for (char16_t c: sample_text) FcCharSetAddChar(charset, c); FcPatternAddCharSet(pattern, FC_CHARSET, charset); FcCharSetDestroy(charset); auto fs = FcFontSetCreate(); FcResult result; FcFontSet * font_patterns = FcFontSort(config, pattern, FcTrue, nullptr, &result); if (!font_patterns || font_patterns->nfont == 0) { FcFontSetDestroy(fs); FcPatternDestroy(pattern); throw std::runtime_error("Fontconfig could not find ANY fonts on the system?"); } std::vector fonts; for (int i = 0; i < font_patterns->nfont; i++) { FcChar8 * font_file; if (FcPatternGetString(font_patterns->fonts[i], FC_FILE, 0, &font_file) != FcResultMatch) continue; FcCharSet * font_charset; if (FcPatternGetCharSet(font_patterns->fonts[i], FC_CHARSET, 0, &font_charset) != FcResultMatch) continue; bool keep_this_font = false; std::u16string tmp = sample_text; for (int i = 0; i < tmp.size();) { char16_t c = tmp[i]; if (FcCharSetHasChar(font_charset, c)) { keep_this_font = true; tmp[i] = tmp[tmp.size() - 1]; tmp.resize(tmp.size() - 1); } else ++i; } if (keep_this_font) fonts.emplace_back((const char *)font_file); break; } FcFontSetDestroy(fs); FcPatternDestroy(pattern); return fonts; } #endif void imgui_context::initialize_fonts() { std::vector fonts; for (auto & [code, glyphs]: glyph_set_per_language) { std::u16string sample_text = u"Hello world"; for (char16_t c: glyphs) sample_text += c; auto language = code.substr(0, code.find("_")); // Load Fonts for (auto && i: find_font(sample_text, language)) { spdlog::info("Font for {} {}", code, i); if (not std::ranges::contains(fonts, i)) fonts.push_back(std::move(i)); } } assert(font_awesome.empty()); font_awesome.emplace_back("assets://Font Awesome 7 Free-Regular-400.otf"); font_awesome.emplace_back("assets://Font Awesome 7 Free-Solid-900.otf"); font_awesome.emplace_back("assets://Font Awesome 7 Brands-Regular-400.otf"); ImFontConfig config; config.FontDataOwnedByAtlas = true; for (auto & font: fonts) { spdlog::info("Using font {}", font, application::get_messages_info().language); io.Fonts->AddFontFromFileTTF(font.c_str(), constants::gui::font_size_small, &config); config.MergeMode = true; } config.FontDataOwnedByAtlas = false; config.MergeMode = true; config.GlyphMinAdvanceX = 40; // Use if you want to make the icon monospaced for (auto & fa: font_awesome) io.Fonts->AddFontFromMemoryTTF(const_cast(fa.data()), fa.size(), constants::gui::font_size_small, &config); } std::vector imgui_context::read_controllers_state(XrTime display_time) { float scroll_scale = io.DeltaTime * constants::gui::scroll_ratio; std::vector new_states; new_states.resize(controllers.size()); if (not controllers_enabled) return new_states; aim_interaction = {1, 1}; // Get the hand/controller state from OpenXR for (const auto & [controller, state, current_aim_interaction]: std::ranges::zip_view(controllers, new_states, aim_interaction)) { const auto & ctrl = controller.first; std::pair, float> index_tip_position{{}, std::numeric_limits::max()}; std::pair, float> palm_position{{}, std::numeric_limits::max()}; std::pair, float> controller_position{{}, std::numeric_limits::max()}; // First hands, so we can set aim_interaction current_aim_interaction = 1; if (ctrl.hand) { if (auto hand = ctrl.hand->locate(world, display_time).joints) { XrHandJointLocationEXT & index_tip = (*hand)[XR_HAND_JOINT_INDEX_TIP_EXT].first; XrHandJointLocationEXT & palm = (*hand)[XR_HAND_JOINT_PALM_EXT].first; if (index_tip.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) { index_tip_position = compute_pointer_position(controller_state{ .aim_position = { index_tip.pose.position.x, index_tip.pose.position.y, index_tip.pose.position.z, }, // aim_orientation is ignored by ray_plane_intersection() for hands .source = ImGuiMouseSource_VRHandTracking, }); } if (palm.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) { palm_position = compute_pointer_position(controller_state{ .aim_position = { palm.pose.position.x, palm.pose.position.y, palm.pose.position.z, }, .source = ImGuiMouseSource_VRHandTracking, }); } if (index_tip_position.second < constants::gui::fingertip_distance_touching_thd_hi and index_tip_position.second > constants::gui::fingertip_distance_touching_thd_lo) state.fingertip_touching = true; if (palm_position.second < constants::gui::palm_distance_close_thd_lo) current_aim_interaction = 0; else if (palm_position.second > constants::gui::palm_distance_close_thd_hi) current_aim_interaction = 1; else current_aim_interaction = (palm_position.second - constants::gui::palm_distance_close_thd_lo) / (constants::gui::palm_distance_close_thd_hi - constants::gui::palm_distance_close_thd_lo); } } // Then controllers if (auto location = application::locate_controller(ctrl.aim, world, display_time)) { controller_position = compute_pointer_position(controller_state{ .aim_position = location->first + glm::mat3_cast(location->second * ctrl.offset.second) * ctrl.offset.first, .aim_orientation = location->second * ctrl.offset.second, .source = ImGuiMouseSource_VRController, }); if (current_aim_interaction == 1) { if (ctrl.trigger) { auto trigger = application::read_action_float(ctrl.trigger).value_or(std::pair{0, 0}); state.trigger_value = trigger.second; if (state.trigger_value > constants::gui::trigger_click_thd) state.trigger_clicked = true; } if (ctrl.scroll) { if (auto act = application::read_action_vec2(ctrl.scroll); act) state.scroll_value = {-act->second.x * scroll_scale, act->second.y * scroll_scale}; else state.scroll_value = {0, 0}; } } } if (current_aim_interaction == 0 or not controller_position.first) state.pointer_position = index_tip_position.first; else if (current_aim_interaction == 1 or not index_tip_position.first) state.pointer_position = controller_position.first; else { assert(index_tip_position.first); assert(controller_position.first); state.pointer_position = *index_tip_position.first + (*controller_position.first - *index_tip_position.first) * current_aim_interaction; } if (current_aim_interaction < 1) state.source = ImGuiMouseSource_VRHandTracking; else state.source = ImGuiMouseSource_VRController; } return new_states; } std::pair, float> imgui_context::compute_pointer_position(const imgui_context::controller_state & state) const { auto intersections = ray_plane_intersection(state); if (intersections.empty()) return {std::nullopt, std::numeric_limits::max()}; if (ImGuiWindow * modal_popup = ImGui::GetTopMostAndVisiblePopupModal()) { // If there is a popup window, use the viewport of that window or the virtual keyboard for (auto & i: layers_) { if (i.space != xr::spaces::world) continue; if (window_intersects_viewport(modal_popup, i) or i.always_show_cursor) { for (auto [position, distance]: intersections) { if (in_viewport(i, position)) return {position, distance}; } } } return {std::nullopt, std::numeric_limits::max()}; } else { // Intersections are sorted by distance: take the closest one for (auto [position, distance]: intersections) { for (ImGuiWindow * window: context->Windows) { if (not window->Active or window->Hidden) continue; if (in_window(window, position)) return {position, distance}; } } // If the pointer isn't in any window, take the farthest one return intersections.back(); } } size_t imgui_context::choose_focused_controller(const std::vector & new_states) const { // If only one controller points at a GUI layer, select it size_t new_focused_controller = (size_t)-1; size_t count = 0; for (auto && [index, state]: utils::enumerate(new_states)) { if (state.pointer_position) { count++; new_focused_controller = index; } } if (count == 1) return new_focused_controller; // If there is a rising edge on a trigger, select the controller assert(controllers.size() == new_states.size()); for (size_t index = 0, size = controllers.size(); index < size; ++index) { const auto & old_state = controllers[index].second; const auto & new_state = new_states[index]; bool old_click = (old_state.source == ImGuiMouseSource_VRController and old_state.trigger_clicked) or (old_state.source == ImGuiMouseSource_VRHandTracking and old_state.fingertip_touching); bool new_click = (new_state.source == ImGuiMouseSource_VRController and new_state.trigger_clicked) or (new_state.source == ImGuiMouseSource_VRHandTracking and new_state.fingertip_touching); if (not old_click and new_click) return index; } // scrolling? for (auto && [index, state]: utils::enumerate(new_states)) { if (glm::length(state.scroll_value) > constants::gui::scroll_value_thd) return index; } // Else, keep the last controller active return focused_controller; } void imgui_context::new_frame(XrTime display_time) { ImGui::SetCurrentContext(context); ImPlot::SetCurrentContext(plot_context); if (last_display_time) io.DeltaTime = std::clamp((display_time - last_display_time) * 1e-9f, 0.001f, 0.0166f); last_display_time = display_time; // Uses the window list from last frame auto new_states = read_controllers_state(display_time); // Set the currently active controller size_t new_focused_controller = choose_focused_controller(new_states); bool focused_change = new_focused_controller != focused_controller && focused_controller != (size_t)-1; // Set the source for the following events if (focused_controller != (size_t)-1) io.AddMouseSourceEvent(new_states[focused_controller].source); if (new_focused_controller != (size_t)-1) { auto scroll = new_states[new_focused_controller].scroll_value; button_pressed = new_states[new_focused_controller].trigger_clicked; fingertip_touching = new_states[new_focused_controller].fingertip_touching; // Focused controller changed: end the current click for this frame io.AddMouseButtonEvent(0, (button_pressed or fingertip_touching) and not focused_change); if (auto position = new_states[new_focused_controller].pointer_position) { io.AddMousePosEvent(position->x, position->y); if (glm::length(scroll) > constants::gui::scroll_value_thd) { io.MouseWheel = scroll.y; io.MouseWheelH = scroll.x; } } } focused_controller = new_focused_controller; for (auto && [controller, next_state]: std::views::zip(controllers, new_states)) { controller.second = next_state; } // Start the Dear ImGui frame ImGui_ImplVulkan_NewFrame(); io.DisplaySize = ImVec2(size.width, size.height); io.DisplayFramebufferScale = ImVec2(1, 1); // See ImGui_ImplSDL2_ProcessEvent ImGui::NewFrame(); #if WIVRN_SHOW_IMGUI_DEMO_WINDOW if (show_demo_window) ImGui::ShowDemoWindow(&show_demo_window); #endif image_index = swapchain.acquire(); swapchain.wait(); hovered_item_prev = hovered_item; hovered_item = 0; } std::vector> imgui_context::end_frame() { if (hovered_item != hovered_item_prev && hovered_item != 0) { size_t controller = get_focused_controller(); if (auto haptic_output = controllers[controller].first.haptic_output; haptic_output != XR_NULL_HANDLE) application::haptic_start(haptic_output, XR_NULL_PATH, 10'000'000, 1000, 1); } vk::Image destination = swapchain.image(image_index); ImGui::SetCurrentContext(context); ImPlot::SetCurrentContext(plot_context); for (auto & controller: controllers) { if (auto position = controller.second.pointer_position) { // Clip in the right plane ImVec2 clip_rect_min(0, 0); ImVec2 clip_rect_max(0, 0); // If there is a modal popup, only display the cursor in the viewport of the popup or on the virtual keyboard if (ImGuiWindow * modal_popup = ImGui::GetTopMostAndVisiblePopupModal()) { for (auto & i: layers_) { if (i.space != xr::spaces::world) continue; // The cursor is over the virtual keyboard if (i.always_show_cursor and position->x >= i.vp_origin.x and position->y >= i.vp_origin.y and position->x <= i.vp_origin.x + i.vp_size.x and position->y <= i.vp_origin.y + i.vp_size.y) { clip_rect_min = ImVec2(i.vp_origin.x + 1, i.vp_origin.y + 1); clip_rect_max = ImVec2(i.vp_origin.x + i.vp_size.x, i.vp_origin.y + i.vp_size.y); break; } // The cursor is in the same viewport as the popup if (window_intersects_viewport(modal_popup, i) and position->x >= i.vp_origin.x and position->y >= i.vp_origin.y and position->x <= i.vp_origin.x + i.vp_size.x and position->y <= i.vp_origin.y + i.vp_size.y) { clip_rect_min = ImVec2(i.vp_origin.x + 1, i.vp_origin.y + 1); clip_rect_max = ImVec2(i.vp_origin.x + i.vp_size.x, i.vp_origin.y + i.vp_size.y); break; } } } else { for (auto & i: layers_) { if (position->x < i.vp_origin.x or position->y < i.vp_origin.y or position->x > i.vp_origin.x + i.vp_size.x or position->y > i.vp_origin.y + i.vp_size.y) continue; clip_rect_min = ImVec2(i.vp_origin.x + 1, i.vp_origin.y + 1); clip_rect_max = ImVec2(i.vp_origin.x + i.vp_size.x, i.vp_origin.y + i.vp_size.y); break; } } // Compute the distance to the closest window float distance = std::numeric_limits::max(); for (ImGuiWindow * window: context->Windows) { distance = std::min(distance, distance_to_window(window, *position)); } float alpha = std::clamp(1 - distance / constants::gui::pointer_fading_distance, 0, 1); if (focused_controller != (size_t)-1 and &controller == &controllers[focused_controller]) alpha *= constants::gui::pointer_alpha; else alpha *= constants::gui::pointer_alpha_disabled; float radius_in = constants::gui::pointer_radius_in; float radius_out = radius_in + constants::gui::pointer_thickness * 0.5; ImDrawList * draw_list = ImGui::GetForegroundDrawList(); draw_list->PushClipRect(clip_rect_min, clip_rect_max); draw_list->AddCircleFilled(*position, radius_in, ImGui::GetColorU32(constants::gui::pointer_color, alpha)); draw_list->AddCircle(*position, radius_out, ImGui::GetColorU32(constants::gui::pointer_color_border, alpha), 0, constants::gui::pointer_thickness); draw_list->PopClipRect(); } } if (auto modal_popup = ImGui::GetTopMostAndVisiblePopupModal(); modal_popup != nullptr) { ImDrawList * draw_list = ImGui::GetForegroundDrawList(); for (ImGuiWindow * window: context->Windows) { if (window == modal_popup or window->ParentWindow != nullptr or not window->Active or window->Hidden or window->IsFallbackWindow) continue; static const auto virtual_keyboard_id = ImHashStr("VirtualKeyboard"); if (window->ID == virtual_keyboard_id) continue; ImRect window_rect{ window->Pos.x, window->Pos.y, window->Pos.x + window->Size.x, window->Pos.y + window->Size.y}; draw_list->AddRectFilled(window_rect.Min, window_rect.Max, ImGui::ColorConvertFloat4ToU32(wivrn::ui::current().dimming), window->WindowRounding); } } ImGui::Render(); current_command_buffer = (current_command_buffer + 1) % command_buffers.size(); auto & f = get_frame(destination); auto & cb = get_command_buffer().command_buffer; auto & fence = get_command_buffer().fence; if (auto result = device.waitForFences(*fence, true, 1'000'000'000); result != vk::Result::eSuccess) throw std::runtime_error("vkWaitForfences: " + vk::to_string(result)); device.resetFences(*fence); cb.begin({.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit}); vk::ClearValue clear{vk::ClearColorValue(0, 0, 0, 0)}; cb.beginRenderPass(vk::RenderPassBeginInfo{ .renderPass = *renderpass, .framebuffer = *f.framebuffer, .renderArea = { .extent = size, }, .clearValueCount = 1, .pClearValues = &clear}, vk::SubpassContents::eInline); { auto _ = queue.lock(); // ImGui_ImplVulkan_RenderDrawData uses the queue internally (in ImGui_ImplVulkan_UpdateTexture) // TODO: patch imgui_impl_vulkan to accept a mutex ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), *cb); } cb.endRenderPass(); cb.end(); queue.lock()->submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*cb, }, *fence); swapchain.release(); std::vector> quads; quads.reserve(layers_.size()); for (auto & i: layers_) { bool visible = false; ImRect layer_rect( i.vp_origin.x, i.vp_origin.y, i.vp_origin.x + i.vp_size.x, i.vp_origin.y + i.vp_size.y); ImRect all_windows_rect{ std::numeric_limits::max(), std::numeric_limits::max(), std::numeric_limits::lowest(), std::numeric_limits::lowest(), }; for (ImGuiWindow * window: context->Windows) { ImRect window_rect{ window->Pos.x, window->Pos.y, window->Pos.x + window->Size.x, window->Pos.y + window->Size.y}; if (window->Active and not window->Hidden and layer_rect.Overlaps(window_rect)) { visible = true; // Take the union of all visible windows in this layer all_windows_rect.Add(window_rect); } } // Take the intersection with the layer all_windows_rect.ClipWithFull(layer_rect); glm::vec2 pixel_size = i.size / glm::vec2(i.vp_size); glm::vec2 all_windows_center(all_windows_rect.GetCenter().x, all_windows_rect.GetCenter().y); glm::vec2 layer_center(layer_rect.GetCenter().x, layer_rect.GetCenter().y); glm::vec3 new_position = i.position + i.orientation * glm::vec3((all_windows_center - layer_center) * pixel_size, 0); glm::vec2 new_size = glm::vec2(all_windows_rect.GetWidth(), all_windows_rect.GetHeight()) * pixel_size; if (not visible) continue; quads.emplace_back( i.z_index, XrCompositionLayerQuad{ .type = XR_TYPE_COMPOSITION_LAYER_QUAD, .layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT, .space = application::space(i.space), .eyeVisibility = XrEyeVisibility::XR_EYE_VISIBILITY_BOTH, .subImage = { .swapchain = swapchain, .imageRect = { .offset = { .x = int(all_windows_rect.Min.x), .y = int(all_windows_rect.Min.y), }, .extent = { .width = int(all_windows_rect.Max.x - all_windows_rect.Min.x), .height = int(all_windows_rect.Max.y - all_windows_rect.Min.y), }}, }, .pose = { .orientation = { .x = i.orientation.x, .y = i.orientation.y, .z = i.orientation.z, .w = i.orientation.w, }, .position = { .x = new_position.x, .y = new_position.y, .z = new_position.z, }, }, .size = { .width = new_size.x, .height = new_size.y, }, }); } return quads; } std::vector imgui_context::windows() { std::vector w; for (const imgui_context::viewport & layer: layers_) { if (layer.space != xr::spaces::world) continue; ImRect v(layer.vp_origin.x, layer.vp_origin.y, layer.vp_origin.x + layer.vp_size.x, layer.vp_origin.y + layer.vp_size.y); for (ImGuiWindow * window: context->Windows) { if (not window->Active or window->Hidden or (window->Flags & ImGuiWindowFlags_ChildWindow)) continue; if (not window_intersects_viewport(window, layer)) continue; // The window intersects the viewport: compute the window position in the real world ImVec2 max = ImMin(v.Max, window->Pos + window->Size); ImVec2 min = ImMax(v.Min, window->Pos); ImVec2 center = (min + max) / 2; w.push_back(window_viewport{ .space = layer.space, .position = layer.position + glm::mat3_cast(layer.orientation) * glm::vec3( ((center.x - layer.vp_origin.x) / layer.vp_size.x - 0.5) * layer.size.x, (-(center.y - layer.vp_origin.y) / layer.vp_size.y + 0.5) * layer.size.y, 0), .orientation = layer.orientation, .size = { (max.x - min.x) * layer.size.x / layer.vp_size.x, (max.y - min.y) * layer.size.y / layer.vp_size.y, }, }); } } return w; } void imgui_context::vibrate_on_hover() { if (ImGui::IsItemHovered()) hovered_item = ImGui::GetItemID(); } void imgui_context::set_hovered_item() { hovered_item = ImGui::GetItemID(); } imgui_context::~imgui_context() { ImGui::SetCurrentContext(context); ImPlot::SetCurrentContext(plot_context); std::vector fences; // Release the command buffers without freing them, they will be destroyed with the command pool for (auto & f: command_buffers) { f.command_buffer.release(); fences.push_back(*f.fence); } // Wait for fences before ImGui_ImplVulkan_Shutdown is called if (auto result = device.waitForFences(fences, true, 1'000'000'000); result != vk::Result::eSuccess) spdlog::error("vkWaitForfences: {}", vk::to_string(result)); ImGui_ImplVulkan_Shutdown(); ImPlot::DestroyContext(); ImGui::DestroyContext(context); } ImTextureID imgui_textures::load_texture(const std::string & filename) { return load_texture(utils::mapped_file{filename}, filename); } ImTextureID imgui_textures::load_texture(const std::span & bytes, const std::string & name) { bool srgb = true; std::shared_ptr image; if (name == "") image = image_cache->load_uncached(bytes, srgb, "", true /* premultiply alpha */); else image = image_cache->load(name, bytes, srgb, name, true /* premultiply alpha */); std::shared_ptr ds = descriptor_pool.allocate(); vk::DescriptorImageInfo image_info{ .imageView = *image->image_view, .imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal, }; vk::WriteDescriptorSet ds_write{ .dstSet = **ds, .descriptorCount = 1, .descriptorType = vk::DescriptorType::eSampledImage, .pImageInfo = &image_info}; device.updateDescriptorSets(ds_write, nullptr); ImTextureID id = reinterpret_cast(VkDescriptorSet(**ds)); textures.emplace( id, texture_data{ .image = image, .descriptor_set = std::move(ds), }); return id; } void imgui_textures::free_texture(ImTextureID texture) { textures.erase(texture); } void imgui_context::set_current() { ImGui::SetCurrentContext(context); ImPlot::SetCurrentContext(plot_context); } void imgui_context::set_controllers_enabled(bool value) { controllers_enabled = value; } imgui_context::viewport & imgui_context::layer(ImVec2 position) { for (auto & layer: layers_) { if (in_viewport(layer, position)) return layer; } return layers_.front(); } void imgui_context::place_layer_relative(size_t layer, size_t base, glm::vec3 offset, glm::quat extra_rotation) { const viewport & b = layers_[base]; viewport & l = layers_[layer]; l.orientation = b.orientation * extra_rotation; l.position = b.position + glm::mat3_cast(b.orientation) * offset; } void imgui_context::tooltip(std::string_view text, std::optional anchor) { // FIXME: this is incorrect if we use the docking branch of imgui ImGuiViewport * viewport = ImGui::GetMainViewport(); auto & current_layer = layer(anchor.value_or(ImGui::GetMousePos())); assert(std::ranges::contains(layers_, true, &viewport::tooltip_viewport)); auto & tooltip_layer = *std::ranges::find(layers_, true, &viewport::tooltip_viewport); // anchor above the given display point, or above the last item's rect (top center) ImVec2 item_position = anchor.value_or(ImVec2{ (ImGui::GetItemRectMin().x + ImGui::GetItemRectMax().x) / 2, ImGui::GetItemRectMin().y, }); auto pos_backup = viewport->Pos; auto size_backup = viewport->Size; viewport->Pos = ImVec2(tooltip_layer.vp_origin.x, tooltip_layer.vp_origin.y); viewport->Size = ImVec2(tooltip_layer.vp_size.x, tooltip_layer.vp_size.y); // Draw the tooltip in the tooltip layer, themed // Clamp position to avoid overflowing on the left or the right const wivrn::ui::theme & t = wivrn::ui::current(); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, wivrn::ui::metrics::tooltip_padding); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, t.card_rounding); ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, t.border_size); ImGui::PushStyleColor(ImGuiCol_PopupBg, t.card); ImGui::PushStyleColor(ImGuiCol_Border, t.border); auto & style = ImGui::GetStyle(); const ImVec2 text_size = ImGui::CalcTextSize(text.data(), text.data() + text.size(), true); const ImVec2 tooltip_size = {text_size.x + style.WindowPadding.x * 2.0f, text_size.y + style.WindowPadding.y * 2.0f}; ImGui::SetNextWindowPos(viewport->Pos + viewport->Size * 0.5, ImGuiCond_Always, {0.5, 0.5}); ImGui::SetNextWindowSizeConstraints({0, 0}, viewport->Size); if (ImGui::BeginTooltip()) { ImGui::PushStyleColor(ImGuiCol_Text, t.col(t.text)); ImGui::TextUnformatted(text.data(), text.data() + text.size()); ImGui::PopStyleColor(); ImGui::EndTooltip(); } ImGui::PopStyleColor(2); ImGui::PopStyleVar(3); viewport->Pos = pos_backup; viewport->Size = size_backup; // Compute the tooltip position glm::quat tooltip_orientation = current_layer.orientation; auto M = glm::mat3_cast(tooltip_orientation); float pixel_size = current_layer.size.y / current_layer.vp_size.y; glm::vec3 tooltip_position_centre = rw_from_vp(item_position) + M * (glm::vec3(0, tooltip_size.y / 2, 0) * pixel_size + constants::gui::tooltip_distance); // Position the tooltip layer tooltip_layer.position = tooltip_position_centre; tooltip_layer.orientation = tooltip_orientation; } // https://github.com/ocornut/imgui/issues/3379#issuecomment-2943903877 void ScrollWhenDragging() { ImVec2 delta{0.0f, -ImGui::GetIO().MouseDelta.y}; const ImGuiMouseButton mouse_button = ImGuiMouseButton_Left; ImGuiContext & g = *ImGui::GetCurrentContext(); ImGuiWindow * window = g.CurrentWindow; ImGuiID id = window->GetID("##scrolldraggingoverlay"); ImGui::KeepAliveID(id); static int active_id; static ImVec2 cumulated_delta; // don't drag while a popup is open or just closed: the ray snaps back and synthesises a click static bool popup_was_open; const bool popup_open = ImGui::IsPopupOpen(nullptr, ImGuiPopupFlags_AnyPopupId | ImGuiPopupFlags_AnyPopupLevel); bool HoveredIdAllowOverlap_backup = std::exchange(g.HoveredIdAllowOverlap, true); bool ActiveIdAllowOverlap_backup = std::exchange(g.ActiveIdAllowOverlap, true); // start only over this window or a child card, and only with no active widget const bool over = ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows); if (active_id == 0 and g.ActiveId == 0 and not popup_open and not popup_was_open and over and ImGui::IsMouseClicked(mouse_button, ImGuiInputFlags_None, /*id*/ ImGuiKeyOwner_Any)) { active_id = id; // Don't scroll on the first step, in case the active controller just changed delta = {}; cumulated_delta = {}; } if (not g.IO.MouseDown[mouse_button]) active_id = 0; if (active_id == id) { if (delta.x != 0.0f) ImGui::SetScrollX(window, window->Scroll.x + delta.x); if (delta.y != 0.0f) ImGui::SetScrollY(window, window->Scroll.y + delta.y); cumulated_delta += delta; if (std::max(std::abs(cumulated_delta.x), std::abs(cumulated_delta.y)) > 50) ImGui::ClearActiveID(); } g.HoveredIdAllowOverlap = HoveredIdAllowOverlap_backup; g.ActiveIdAllowOverlap = ActiveIdAllowOverlap_backup; popup_was_open = popup_open; } void CenterTextH(const std::string & text) { float win_width = ImGui::GetWindowSize().x; std::vector lines = utils::split(text); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {0, 0}); for (const auto & i: lines) { float text_width = ImGui::CalcTextSize(i.c_str()).x; ImGui::SetCursorPosX((win_width - text_width) / 2); ImGui::Text("%s", i.c_str()); } ImGui::PopStyleVar(); ImGui::Dummy({}); // Make sure the original vertical ItemSpacing is respected } void CenterTextHV(const std::string & text) { ImVec2 size = ImGui::GetWindowSize(); std::vector lines = utils::split(text); float text_height = 0; for (const auto & i: lines) text_height += ImGui::CalcTextSize(i.c_str()).y; ImGui::SetCursorPosY((size.y - text_height) / 2); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {0, 0}); for (const auto & i: lines) { float text_width = ImGui::CalcTextSize(i.c_str()).x; ImGui::SetCursorPosX((size.x - text_width) / 2); ImGui::Text("%s", i.c_str()); } ImGui::PopStyleVar(); }