Files
WiVRn/client/render/imgui_impl.cpp
T
Guillaume Meunier 252b2fd81e Update imgui to 1.91.3
Rework touch handling in lobby GUI

Add imgui patches

Support splitting the imgui viewport/swapchain in different composition layers
2024-11-01 11:40:33 +01:00

1011 lines
30 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <[email protected]>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "imgui_impl.h"
#include "implot.h"
#include "application.h"
#include "asset.h"
#include "image_loader.h"
#include "openxr/openxr.h"
#include "utils/ranges.h"
#include "vulkan/vulkan_enums.hpp"
#include "vulkan/vulkan_handles.hpp"
#include "vulkan/vulkan_to_string.hpp"
#include <algorithm>
#include <backends/imgui_impl_vulkan.h>
#include <boost/locale.hpp>
#include <cmath>
#include <cstddef>
#include <glm/gtc/matrix_access.hpp>
#include <imgui.h>
#include <imgui_internal.h>
#include <optional>
#include <ranges>
#include <spdlog/spdlog.h>
#include <string_view>
#include "IconsFontAwesome6.h"
#ifdef __ANDROID__
#include <android/font.h>
#include <android/font_matcher.h>
#include <android/system_fonts.h>
#else
#include <fontconfig/fontconfig.h>
#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<std::string, std::vector<int>> 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();
}
}
std::vector<std::pair<ImVec2, float>> imgui_context::ray_plane_intersection(const imgui_context::controller_state & in) const
{
if (!in.active)
return {};
std::vector<std::pair<ImVec2, float>> intersections;
for (const auto & i: layers_)
{
auto M = glm::transpose(glm::mat3_cast(i.orientation)); // world-to-plane transform
glm::quat q;
if (std::abs(glm::length(in.aim_orientation) - 1) < 0.01)
q = in.aim_orientation;
else
q = i.orientation;
glm::vec3 controller_direction = glm::column(glm::mat3_cast(q), 2);
// 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 < 0)
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;
}
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;
}
static const std::array pool_sizes =
{
vk::DescriptorPoolSize{
.type = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 100,
}};
static const vk::DescriptorSetLayoutBinding layout_bindings{
.binding = 0,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eFragment};
imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, uint32_t queue_family_index, vk::raii::Queue & queue, std::span<controller> controllers_, xr::swapchain & swapchain, std::vector<viewport> layers) :
physical_device(physical_device),
device(device),
queue_family_index(queue_family_index),
queue(queue),
descriptor_pool(device,
vk::DescriptorPoolCreateInfo{
.flags = vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet,
.maxSets = pool_sizes[0].descriptorCount,
.poolSizeCount = pool_sizes.size(),
.pPoolSizes = pool_sizes.data(),
}),
ds_layout(device, vk::DescriptorSetLayoutCreateInfo{.bindingCount = 1, .pBindings = &layout_bindings}),
renderpass(create_renderpass(device, swapchain.format(), true)),
command_pool(device,
vk::CommandPoolCreateInfo{
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient,
.queueFamilyIndex = queue_family_index,
}),
command_buffers(swapchain.images().size()),
size(swapchain.extent().width, swapchain.extent().height),
format(swapchain.format()),
layers_(std::move(layers)),
swapchain(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 = *application::get_physical_device(),
.Device = *application::get_device(),
.QueueFamily = application::queue_family_index(),
.Queue = *application::get_queue(),
.DescriptorPool = *descriptor_pool,
.RenderPass = *renderpass,
.MinImageCount = 2,
.ImageCount = (uint32_t)swapchain.images().size(), // used to cycle between VkBuffers in ImGui_ImplVulkan_RenderDrawData
.MSAASamples = VK_SAMPLE_COUNT_1_BIT,
.PipelineCache = *application::get_pipeline_cache(),
.Subpass = 0,
.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};
}
void imgui_context::add_chars(std::string_view sv)
{
for (auto it = sv.begin(); it != sv.end();)
{
ImWchar c = (ImWchar)*it;
if (c < 0x80)
++it;
else
it += ImTextCharFromUtf8(&c, it, sv.end());
if (!glyph_range_builder.GetBit(c))
{
glyph_range_builder.AddChar(c);
glyph_range_dirty = true;
}
}
}
#ifdef __ANDROID__
static std::vector<std::string> find_font(const ImFontGlyphRangesBuilder & glyph_range_builder, const std::string & locale)
{
std::u16string wtext;
std::u16string_view wtextview;
for (int i = 0; i < glyph_range_builder.UsedChars.Size; ++i)
{
std::uint32_t used_chars = glyph_range_builder.UsedChars[i];
if (used_chars == 0)
continue;
for (int j = 0; j < 32; ++j)
{
ImWchar c = i * 32 + j;
if (used_chars & (1 << j))
{
wtext += c;
}
}
}
wtextview = wtext;
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<std::string> fonts;
while (!wtextview.empty())
{
uint32_t runlength = 0;
AFont * font = AFontMatcher_match(font_matcher, "sans-serif", (uint16_t *)wtextview.data(), wtextview.size(), &runlength);
fonts.emplace_back(AFont_getFontFilePath(font));
AFont_close(font);
if (runlength == 0)
break;
wtextview = wtextview.substr(runlength);
}
AFontMatcher_destroy(font_matcher);
return fonts;
}
#else
static std::vector<std::string> find_font(ImFontGlyphRangesBuilder glyph_range_builder, const std::string & locale)
{
// See https://www.camconn.cc/post/how-to-fontconfig-lib-c/
static FcConfig * config = FcInitLoadConfigAndFonts();
FcPattern * pattern = FcNameParse((const FcChar8 *)"Noto Sans");
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 (int i = 8; i < glyph_range_builder.UsedChars.Size; ++i)
{
std::uint32_t used_chars = glyph_range_builder.UsedChars[i];
if (used_chars == 0)
continue;
for (int j = 0; j < 32; ++j)
{
ImWchar c = i * 32 + j;
if (used_chars & (1 << j))
{
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<std::string> 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;
for (int i = 0; i < glyph_range_builder.UsedChars.Size; ++i)
{
std::uint32_t & used_chars = glyph_range_builder.UsedChars[i];
if (used_chars == 0)
continue;
for (int j = 0; j < 32; ++j)
{
ImWchar c = i * 32 + j;
if (used_chars & (1 << j))
{
if (FcCharSetHasChar(font_charset, c))
{
keep_this_font = true;
used_chars &= ~(1 << j);
}
}
}
}
if (keep_this_font)
fonts.emplace_back((const char *)font_file);
}
FcFontSetDestroy(fs);
FcPatternDestroy(pattern);
return fonts;
}
#endif
void imgui_context::initialize_fonts()
{
glyph_ranges.clear();
// Always include Basic Latin and Latin-1 Supplement without control characters
ImWchar default_ranges[] = {0x20, 0x7f, 0xa0, 0xff, 0};
glyph_range_builder.AddRanges(default_ranges);
for (auto language = application::get_messages_info().language; language != ""; language = language.substr(0, language.size() - 1))
{
auto it = glyph_set_per_language.find(language);
if (it == glyph_set_per_language.end())
continue;
for (ImWchar c: it->second)
{
glyph_range_builder.AddChar(c);
}
break;
}
glyph_range_builder.BuildRanges(&glyph_ranges);
// Load Fonts
auto fonts = find_font(glyph_range_builder, application::get_messages_info().language);
for (auto & i: fonts)
{
spdlog::info("Font {}", i);
}
asset font_awesome_regular("Font Awesome 6 Free-Regular-400.otf");
asset font_awesome_solid("Font Awesome 6 Free-Solid-900.otf");
IM_DELETE(io.Fonts);
io.Fonts = IM_NEW(ImFontAtlas);
{
ImFontConfig config;
config.FontDataOwnedByAtlas = false;
for (auto & font: fonts)
{
spdlog::info("Using font {}", font, application::get_messages_info().language);
io.Fonts->AddFontFromFileTTF(font.c_str(), 30, &config, glyph_ranges.Data);
config.MergeMode = true;
}
config.MergeMode = true;
config.GlyphMinAdvanceX = 40; // Use if you want to make the icon monospaced
static const ImWchar icon_ranges[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
io.Fonts->AddFontFromMemoryTTF(const_cast<std::byte *>(font_awesome_regular.data()), font_awesome_regular.size(), 30, &config, icon_ranges);
io.Fonts->AddFontFromMemoryTTF(const_cast<std::byte *>(font_awesome_solid.data()), font_awesome_solid.size(), 30, &config, icon_ranges);
}
{
ImFontConfig config;
config.FontDataOwnedByAtlas = false;
for (auto & font: fonts)
{
large_font = io.Fonts->AddFontFromFileTTF(font.c_str(), 75, &config, glyph_ranges.Data);
config.MergeMode = true;
}
}
glyph_range_dirty = false;
ImGui_ImplVulkan_CreateFontsTexture();
}
std::optional<ImVec2> imgui_context::get_pointer_position_in_imgui_frame()
{
if (pointer_position.empty())
return {};
for (auto [pos, distance]: pointer_position)
{
for (ImGuiWindow * window: context->Windows)
{
if (window->Hidden or not window->Active)
continue;
if (window->Pos.x > pos.x)
continue;
if (window->Pos.y > pos.y)
continue;
if (window->Pos.x + window->Size.x < pos.x)
continue;
if (window->Pos.y + window->Size.y < pos.y)
continue;
return pos;
}
}
return pointer_position.back().first;
}
void imgui_context::new_frame(XrTime display_time)
{
ImGui::SetCurrentContext(context);
ImPlot::SetCurrentContext(plot_context);
if (last_display_time)
io.DeltaTime = std::min((display_time - last_display_time) * 1e-9f, 0.1f);
last_display_time = display_time;
float scroll_scale = io.DeltaTime * 3;
size_t new_focused_controller = focused_controller;
std::vector<controller_state> new_states;
for (auto && [index, controller]: utils::enumerate(controllers))
{
auto & [ctrl, state] = controller;
controller_state & new_state = new_states.emplace_back();
if (ctrl.hand)
{
if (auto joints = ctrl.hand->locate(world, display_time))
{
XrHandJointLocationEXT & index_tip = (*joints)[XR_HAND_JOINT_INDEX_TIP_EXT].first;
if (index_tip.locationFlags & XR_SPACE_LOCATION_POSITION_TRACKED_BIT)
{
new_state.aim_position = {
index_tip.pose.position.x,
index_tip.pose.position.y,
index_tip.pose.position.z};
// Use the GUI plane as orientation to have the finger point perpendicularly
// to the plane instead of following the finger direction.
// ray_plane_intersection() interprets a null quaternion as a request to use the layer
// orientation.
new_state.aim_orientation = glm::quat(0, 0, 0, 0);
new_state.active = true;
new_state.source = ImGuiMouseSource_VRHandTracking;
auto position_distance = ray_plane_intersection(new_state);
if (not position_distance.empty())
{
new_state.hover_distance = position_distance.front().second;
if (std::abs(position_distance.front().second) < 0.1f)
new_state.fingertip_hovered = true;
else
new_state.active = false;
if (new_state.fingertip_hovered && (new_state.hover_distance < 0.02))
new_state.fingertip_touching = true;
}
else
new_state.hover_distance = 1e10;
}
}
continue;
}
new_state.source = ImGuiMouseSource_VRController;
if (auto location = application::locate_controller(ctrl.aim, world, display_time))
{
new_state.active = true;
new_state.aim_position = location->first;
new_state.aim_orientation = location->second;
}
if (ctrl.squeeze)
{
auto squeeze = application::read_action_float(ctrl.squeeze).value_or(std::pair{0, 0});
new_state.squeeze_value = squeeze.second;
// TODO tunable
if (new_state.squeeze_value < 0.5)
new_state.squeeze_clicked = false;
else if (new_state.squeeze_value > 0.8)
new_state.squeeze_clicked = true;
}
if (ctrl.trigger)
{
auto trigger = application::read_action_float(ctrl.trigger).value_or(std::pair{0, 0});
new_state.trigger_value = trigger.second;
// TODO tunable
if (new_state.trigger_value < 0.5)
new_state.trigger_clicked = false;
else if (new_state.trigger_value > 0.8)
new_state.trigger_clicked = true;
}
if (ctrl.scroll)
{
if (auto act = application::read_action_vec2(ctrl.scroll); act)
new_state.scroll_value = {-act->second.x * scroll_scale, act->second.y * scroll_scale};
else
new_state.scroll_value = {0, 0};
}
}
float closest_hover_distance = 1e10;
for (auto && [index, new_state]: utils::enumerate(new_states))
{
if (std::abs(new_state.hover_distance) < closest_hover_distance && new_state.fingertip_hovered)
{
new_focused_controller = index;
closest_hover_distance = std::abs(new_state.hover_distance);
}
else if (new_state.squeeze_clicked || new_state.trigger_clicked || glm::length(new_state.scroll_value) > 0.01f)
{
new_focused_controller = index;
}
}
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 (focused_change && controllers[focused_controller].second.trigger_clicked)
{
// Focused controller changed: end the current click
io.AddMouseButtonEvent(0, false);
button_pressed = false;
fingertip_touching = false;
}
if (new_focused_controller != (size_t)-1)
{
pointer_position = ray_plane_intersection(new_states[new_focused_controller]);
auto scroll = new_states[new_focused_controller].scroll_value;
bool last_trigger = controllers[new_focused_controller].second.trigger_clicked;
button_pressed = new_states[new_focused_controller].trigger_clicked;
bool last_touching = controllers[new_focused_controller].second.fingertip_touching;
fingertip_touching = new_states[new_focused_controller].fingertip_touching;
if (auto position = get_pointer_position_in_imgui_frame())
{
io.AddMousePosEvent(position->x, position->y);
if (focused_change || (last_trigger || last_touching) != (button_pressed || fingertip_touching))
{
io.AddMouseButtonEvent(0, button_pressed || fingertip_touching);
}
if (glm::length(scroll) > 0.01f)
io.AddMouseWheelEvent(scroll.x, scroll.y);
}
else
{
if ((last_trigger || last_touching) && !(button_pressed || fingertip_touching))
{
io.AddMouseButtonEvent(0, button_pressed || fingertip_touching);
}
}
}
else
{
pointer_position.clear();
}
focused_controller = new_focused_controller;
for (auto && [controller, next_state]: std::views::zip(controllers, new_states))
{
controller.second = next_state;
}
// Force recreation of the font atlas
if (glyph_range_dirty)
{
// Wait for fences before ImGui_ImplVulkan_DestroyFontsTexture is called
std::vector<vk::Fence> fences;
for (auto & f: command_buffers)
{
fences.push_back(*f.fence);
}
if (auto result = device.waitForFences(fences, true, 1'000'000'000); result != vk::Result::eSuccess)
spdlog::error("vkWaitForfences: {}", vk::to_string(result));
ImGui_ImplVulkan_DestroyFontsTexture();
initialize_fonts();
ImGui_ImplVulkan_CreateFontsTexture();
}
// 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();
}
std::vector<XrCompositionLayerQuad> imgui_context::end_frame()
{
vk::Image destination = swapchain.images()[image_index].image;
ImGui::SetCurrentContext(context);
ImPlot::SetCurrentContext(plot_context);
if (auto position = get_pointer_position_in_imgui_frame())
{
// Clip in the right plane
ImVec2 clip_rect_min(0, 0);
ImVec2 clip_rect_max(size.width, size.height);
for (auto & i: layers_)
{
if (position->x < i.vp_origin.x)
continue;
if (position->y < i.vp_origin.y)
continue;
if (position->x > i.vp_origin.x + i.vp_size.x)
continue;
if (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;
}
// TODO fix according to layers
float distance_to_border = std::min({position->x,
size.width - position->x,
position->y,
size.height - position->y});
float radius = 10; // std::clamp<float>(distance_to_border / 4, 0, 10);
float alpha = std::clamp<float>((distance_to_border - 10) / 50, 0, 0.8);
ImU32 color_pressed = ImGui::GetColorU32(ImVec4(0, 0.2, 1, alpha));
ImU32 color_unpressed = ImGui::GetColorU32(ImVec4(1, 1, 1, alpha));
bool pressed = button_pressed || fingertip_touching;
ImDrawList * draw_list = ImGui::GetForegroundDrawList();
draw_list->PushClipRect(clip_rect_min, clip_rect_max);
draw_list->AddCircleFilled(*position, radius, pressed ? color_pressed : color_unpressed);
draw_list->AddCircle(*position, radius * 1.2, ImGui::GetColorU32(ImVec4(0, 0, 0, alpha)), 0, radius * 0.4);
draw_list->PopClipRect();
}
if (not context->OpenPopupStack.empty())
{
// TODO dim main window
}
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);
ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), *cb);
cb.endRenderPass();
cb.end();
queue.submit(vk::SubmitInfo{
.commandBufferCount = 1,
.pCommandBuffers = &*cb,
},
*fence);
swapchain.release();
std::vector<XrCompositionLayerQuad> quads;
quads.reserve(layers_.size());
for (auto & i: layers_)
{
quads.push_back(XrCompositionLayerQuad{
.type = XR_TYPE_COMPOSITION_LAYER_QUAD,
.layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT,
.space = world,
.eyeVisibility = XrEyeVisibility::XR_EYE_VISIBILITY_BOTH,
.subImage = {
.swapchain = swapchain,
.imageRect = {
.offset = {
.x = i.vp_origin.x,
.y = i.vp_origin.y,
},
.extent = {
.width = i.vp_size.x,
.height = i.vp_size.y,
}},
},
.pose = {
.orientation = {
.x = i.orientation.x,
.y = i.orientation.y,
.z = i.orientation.z,
.w = i.orientation.w,
},
.position = {
.x = i.position.x,
.y = i.position.y,
.z = i.position.z,
},
},
.size = {
.width = i.size.x,
.height = i.size.y,
},
});
}
return quads;
}
imgui_context::~imgui_context()
{
ImGui::SetCurrentContext(context);
ImPlot::SetCurrentContext(plot_context);
std::vector<vk::Fence> 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_context::load_texture(const std::string & filename, vk::raii::Sampler && sampler)
{
bool srgb = true;
image_loader loader(physical_device, device, queue, command_pool);
loader.load(asset{filename}, srgb);
vk::raii::DescriptorSet ds = std::move(device.allocateDescriptorSets({
.descriptorPool = *descriptor_pool,
.descriptorSetCount = 1,
.pSetLayouts = &*ds_layout,
})[0]);
vk::DescriptorImageInfo image_info{
.sampler = *sampler,
.imageView = **loader.image_view,
.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
};
vk::WriteDescriptorSet ds_write{
.dstSet = *ds,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.pImageInfo = &image_info};
device.updateDescriptorSets(ds_write, nullptr);
ImTextureID id = *ds;
textures.emplace(
id,
texture_data{
.sampler = std::move(sampler),
// .image = std::move(loader.image),
.image_view = std::move(loader.image_view),
.descriptor_set = std::move(ds),
});
return id;
}
ImTextureID imgui_context::load_texture(const std::string & filename)
{
return load_texture(
filename,
vk::raii::Sampler{
device,
vk::SamplerCreateInfo{
.magFilter = vk::Filter::eLinear,
.minFilter = vk::Filter::eLinear,
.mipmapMode = vk::SamplerMipmapMode::eLinear,
.borderColor = vk::BorderColor::eFloatTransparentBlack,
},
});
}
void imgui_context::free_texture(ImTextureID texture)
{
textures.erase(texture);
}
void imgui_context::set_current()
{
ImGui::SetCurrentContext(context);
ImPlot::SetCurrentContext(plot_context);
}