diff --git a/client/constants.h b/client/constants.h index 69d817d8..9e8cb099 100644 --- a/client/constants.h +++ b/client/constants.h @@ -28,9 +28,6 @@ namespace constants::gui // Minimum distance between a GUI layer and a fingertip/controller to register a click constexpr float min_pointer_distance = -0.1; -// Number of combined image samplers in ImGui descriptor pool -constexpr unsigned int nb_combined_image_samplers = 100; - // Font to use (desktop only) constexpr const char font_name[] = "Noto Sans"; diff --git a/client/render/growable_descriptor_pool.cpp b/client/render/growable_descriptor_pool.cpp index 2cccd9d5..7d9bf52d 100644 --- a/client/render/growable_descriptor_pool.cpp +++ b/client/render/growable_descriptor_pool.cpp @@ -23,13 +23,41 @@ struct descriptor_set { - growable_descriptor_pool & growable_pool; + growable_descriptor_pool * growable_pool; vk::DescriptorPool pool; vk::raii::DescriptorSet ds; + descriptor_set(const descriptor_set &) = delete; + descriptor_set & operator=(const descriptor_set &) = delete; + + descriptor_set(growable_descriptor_pool * growable_pool, + vk::DescriptorPool pool, + vk::raii::DescriptorSet ds) : + growable_pool(growable_pool), + pool(pool), + ds(std::move(ds)) + { + } + + descriptor_set(descriptor_set && other) : + growable_pool(std::exchange(other.growable_pool, nullptr)), + pool(other.pool), + ds(std::move(other.ds)) + { + } + + descriptor_set & operator=(descriptor_set && other) + { + std::swap(*this, other); + return *this; + } + ~descriptor_set() { - for (auto & i: growable_pool.pools) + if (not growable_pool) + return; + + for (auto & i: growable_pool->pools) { if (*i.descriptor_pool == pool) { @@ -43,7 +71,7 @@ struct descriptor_set growable_descriptor_pool::growable_descriptor_pool( vk::raii::Device & device, vk::raii::DescriptorSetLayout & layout, - std::span bindings, + std::span bindings, int descriptorsets_per_pool) : device(device), layout_(nullptr), @@ -84,7 +112,7 @@ std::shared_ptr growable_descriptor_pool::allocate() i.free_count--; alloc_info.descriptorPool = *i.descriptor_pool; - auto ds = std::make_shared(*this, *i.descriptor_pool, std::move(device.allocateDescriptorSets(alloc_info)[0])); + auto ds = std::make_shared(this, *i.descriptor_pool, std::move(device.allocateDescriptorSets(alloc_info)[0])); return std::shared_ptr(ds, &ds->ds); } @@ -103,7 +131,7 @@ std::shared_ptr growable_descriptor_pool::allocate() alloc_info.descriptorPool = *pools.back().descriptor_pool; - auto ds = std::make_shared(*this, *pools.back().descriptor_pool, std::move(device.allocateDescriptorSets(alloc_info)[0])); + auto ds = std::make_shared(this, *pools.back().descriptor_pool, std::move(device.allocateDescriptorSets(alloc_info)[0])); return std::shared_ptr(ds, &ds->ds); } diff --git a/client/render/growable_descriptor_pool.h b/client/render/growable_descriptor_pool.h index 56a93412..9ade4c24 100644 --- a/client/render/growable_descriptor_pool.h +++ b/client/render/growable_descriptor_pool.h @@ -40,7 +40,7 @@ class growable_descriptor_pool std::vector pools; public: - growable_descriptor_pool(vk::raii::Device & device, vk::raii::DescriptorSetLayout & layout, std::span bindings, int descriptorsets_per_pool = 100); + growable_descriptor_pool(vk::raii::Device & device, vk::raii::DescriptorSetLayout & layout, std::span bindings, int descriptorsets_per_pool = 100); std::shared_ptr allocate(); }; diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index 238e6827..8428a2bc 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -289,32 +289,21 @@ imgui_context::imgui_frame & imgui_context::get_frame(vk::Image destination) return frame; } -static const std::array pool_sizes = - { - vk::DescriptorPoolSize{ - .type = vk::DescriptorType::eCombinedImageSampler, - .descriptorCount = constants::gui::nb_combined_image_samplers, - }}; - -static const vk::DescriptorSetLayoutBinding layout_bindings{ - .binding = 0, - .descriptorType = vk::DescriptorType::eCombinedImageSampler, - .descriptorCount = 1, - .stageFlags = vk::ShaderStageFlagBits::eFragment}; +static const std::array layout_bindings = { + vk::DescriptorSetLayoutBinding{ + .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 controllers_, xr::swapchain & swapchain, std::vector 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}), + ds_layout(device, vk::DescriptorSetLayoutCreateInfo{.bindingCount = layout_bindings.size(), .pBindings = layout_bindings.data()}), + descriptor_pool(device, ds_layout, layout_bindings), renderpass(create_renderpass(device, swapchain.format(), true)), command_pool(device, vk::CommandPoolCreateInfo{ @@ -353,13 +342,13 @@ imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii: .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, + .DescriptorPoolSize = 100, .Allocator = nullptr, .CheckVkResultFn = check_vk_result, }; @@ -1028,11 +1017,7 @@ ImTextureID imgui_context::load_texture(const std::span & bytes image_loader loader(physical_device, device, queue, command_pool); loader.load(bytes, srgb); - vk::raii::DescriptorSet ds = std::move(device.allocateDescriptorSets({ - .descriptorPool = *descriptor_pool, - .descriptorSetCount = 1, - .pSetLayouts = &*ds_layout, - })[0]); + std::shared_ptr ds = descriptor_pool.allocate(); vk::DescriptorImageInfo image_info{ .sampler = *sampler, @@ -1041,14 +1026,14 @@ ImTextureID imgui_context::load_texture(const std::span & bytes }; vk::WriteDescriptorSet ds_write{ - .dstSet = *ds, + .dstSet = **ds, .descriptorCount = 1, .descriptorType = vk::DescriptorType::eCombinedImageSampler, .pImageInfo = &image_info}; device.updateDescriptorSets(ds_write, nullptr); - ImTextureID id = reinterpret_cast(VkDescriptorSet(*ds)); + ImTextureID id = reinterpret_cast(VkDescriptorSet(**ds)); textures.emplace( id, diff --git a/client/render/imgui_impl.h b/client/render/imgui_impl.h index 483526f6..025661ad 100644 --- a/client/render/imgui_impl.h +++ b/client/render/imgui_impl.h @@ -18,6 +18,7 @@ #pragma once +#include "render/growable_descriptor_pool.h" #include "wivrn_config.h" #include "xr/hand_tracker.h" #include "xr/space.h" @@ -46,7 +47,7 @@ class imgui_context { vk::raii::Sampler sampler; std::shared_ptr image_view; - vk::raii::DescriptorSet descriptor_set; + std::shared_ptr descriptor_set; }; public: @@ -121,8 +122,8 @@ private: vk::raii::Queue & queue; vk::raii::Pipeline pipeline = nullptr; - vk::raii::DescriptorPool descriptor_pool; vk::raii::DescriptorSetLayout ds_layout; + growable_descriptor_pool descriptor_pool; vk::raii::RenderPass renderpass; vk::raii::CommandPool command_pool;