Use a growable descriptor pool in imgui
This commit is contained in:
@@ -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";
|
||||
|
||||
|
||||
@@ -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<vk::DescriptorSetLayoutBinding> bindings,
|
||||
std::span<const vk::DescriptorSetLayoutBinding> bindings,
|
||||
int descriptorsets_per_pool) :
|
||||
device(device),
|
||||
layout_(nullptr),
|
||||
@@ -84,7 +112,7 @@ std::shared_ptr<vk::raii::DescriptorSet> growable_descriptor_pool::allocate()
|
||||
i.free_count--;
|
||||
alloc_info.descriptorPool = *i.descriptor_pool;
|
||||
|
||||
auto ds = std::make_shared<descriptor_set>(*this, *i.descriptor_pool, std::move(device.allocateDescriptorSets(alloc_info)[0]));
|
||||
auto ds = std::make_shared<descriptor_set>(this, *i.descriptor_pool, std::move(device.allocateDescriptorSets(alloc_info)[0]));
|
||||
|
||||
return std::shared_ptr<vk::raii::DescriptorSet>(ds, &ds->ds);
|
||||
}
|
||||
@@ -103,7 +131,7 @@ std::shared_ptr<vk::raii::DescriptorSet> growable_descriptor_pool::allocate()
|
||||
|
||||
alloc_info.descriptorPool = *pools.back().descriptor_pool;
|
||||
|
||||
auto ds = std::make_shared<descriptor_set>(*this, *pools.back().descriptor_pool, std::move(device.allocateDescriptorSets(alloc_info)[0]));
|
||||
auto ds = std::make_shared<descriptor_set>(this, *pools.back().descriptor_pool, std::move(device.allocateDescriptorSets(alloc_info)[0]));
|
||||
|
||||
return std::shared_ptr<vk::raii::DescriptorSet>(ds, &ds->ds);
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ class growable_descriptor_pool
|
||||
std::vector<pool> pools;
|
||||
|
||||
public:
|
||||
growable_descriptor_pool(vk::raii::Device & device, vk::raii::DescriptorSetLayout & layout, std::span<vk::DescriptorSetLayoutBinding> bindings, int descriptorsets_per_pool = 100);
|
||||
growable_descriptor_pool(vk::raii::Device & device, vk::raii::DescriptorSetLayout & layout, std::span<const vk::DescriptorSetLayoutBinding> bindings, int descriptorsets_per_pool = 100);
|
||||
|
||||
std::shared_ptr<vk::raii::DescriptorSet> allocate();
|
||||
};
|
||||
|
||||
@@ -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<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}),
|
||||
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<const std::byte> & 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<vk::raii::DescriptorSet> ds = descriptor_pool.allocate();
|
||||
|
||||
vk::DescriptorImageInfo image_info{
|
||||
.sampler = *sampler,
|
||||
@@ -1041,14 +1026,14 @@ ImTextureID imgui_context::load_texture(const std::span<const std::byte> & 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<ImTextureID>(VkDescriptorSet(*ds));
|
||||
ImTextureID id = reinterpret_cast<ImTextureID>(VkDescriptorSet(**ds));
|
||||
|
||||
textures.emplace(
|
||||
id,
|
||||
|
||||
@@ -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<vk::raii::ImageView> image_view;
|
||||
vk::raii::DescriptorSet descriptor_set;
|
||||
std::shared_ptr<vk::raii::DescriptorSet> 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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user