Skip to content

Commit 61ab94d

Browse files
zeuxocornut
authored andcommitted
Backends: Vulkan: Make descriptor pool optional (ocornut#8172, ocornut#4867)
Comments/amends by ocornut
1 parent 9b27329 commit 61ab94d

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

backends/imgui_impl_vulkan.cpp

+27-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
// CHANGELOG
2828
// (minor and older changes stripped away, please see git history for details)
29+
// 2024-11-27: Vulkan: Make user-provided descriptor pool optional. As a convenience, when setting init_info->DescriptorPoolSize the backend will create one itself. (#8172, #4867)
2930
// 2024-10-07: Vulkan: Changed default texture sampler to Clamp instead of Repeat/Wrap.
3031
// 2024-10-07: Vulkan: Expose selected render state in ImGui_ImplVulkan_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
3132
// 2024-10-07: Vulkan: Compiling with '#define ImTextureID=ImU64' is unnecessary now that dear imgui defaults ImTextureID to u64 instead of void*.
@@ -131,6 +132,7 @@ static bool g_FunctionsLoaded = true;
131132
IMGUI_VULKAN_FUNC_MAP_MACRO(vkCmdSetViewport) \
132133
IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateBuffer) \
133134
IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateCommandPool) \
135+
IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateDescriptorPool) \
134136
IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateDescriptorSetLayout) \
135137
IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateFence) \
136138
IMGUI_VULKAN_FUNC_MAP_MACRO(vkCreateFramebuffer) \
@@ -221,6 +223,7 @@ struct ImGui_ImplVulkan_Data
221223
VkPipeline Pipeline;
222224
VkShaderModule ShaderModuleVert;
223225
VkShaderModule ShaderModuleFrag;
226+
VkDescriptorPool DescriptorPool;
224227

225228
// Font data
226229
VkSampler FontSampler;
@@ -1010,6 +1013,20 @@ bool ImGui_ImplVulkan_CreateDeviceObjects()
10101013
check_vk_result(err);
10111014
}
10121015

1016+
if (v->DescriptorPoolSize)
1017+
{
1018+
VkDescriptorPoolSize pool_size = { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, v->DescriptorPoolSize };
1019+
VkDescriptorPoolCreateInfo pool_info = {};
1020+
pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
1021+
pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
1022+
pool_info.maxSets = v->DescriptorPoolSize;
1023+
pool_info.poolSizeCount = 1;
1024+
pool_info.pPoolSizes = &pool_size;
1025+
1026+
err = vkCreateDescriptorPool(v->Device, &pool_info, v->Allocator, &bd->DescriptorPool);
1027+
check_vk_result(err);
1028+
}
1029+
10131030
if (!bd->PipelineLayout)
10141031
{
10151032
// Constants: we are using 'vec2 offset' and 'vec2 scale' instead of a full 3d projection matrix
@@ -1048,6 +1065,7 @@ void ImGui_ImplVulkan_DestroyDeviceObjects()
10481065
if (bd->DescriptorSetLayout) { vkDestroyDescriptorSetLayout(v->Device, bd->DescriptorSetLayout, v->Allocator); bd->DescriptorSetLayout = VK_NULL_HANDLE; }
10491066
if (bd->PipelineLayout) { vkDestroyPipelineLayout(v->Device, bd->PipelineLayout, v->Allocator); bd->PipelineLayout = VK_NULL_HANDLE; }
10501067
if (bd->Pipeline) { vkDestroyPipeline(v->Device, bd->Pipeline, v->Allocator); bd->Pipeline = VK_NULL_HANDLE; }
1068+
if (bd->DescriptorPool) { vkDestroyDescriptorPool(v->Device, bd->DescriptorPool, v->Allocator); bd->DescriptorPool = VK_NULL_HANDLE; }
10511069
}
10521070

10531071
bool ImGui_ImplVulkan_LoadFunctions(PFN_vkVoidFunction(*loader_func)(const char* function_name, void* user_data), void* user_data)
@@ -1110,7 +1128,10 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info)
11101128
IM_ASSERT(info->PhysicalDevice != VK_NULL_HANDLE);
11111129
IM_ASSERT(info->Device != VK_NULL_HANDLE);
11121130
IM_ASSERT(info->Queue != VK_NULL_HANDLE);
1113-
IM_ASSERT(info->DescriptorPool != VK_NULL_HANDLE);
1131+
if (info->DescriptorPool != VK_NULL_HANDLE) // Either DescriptorPool or DescriptorPoolSize must be set, not both!
1132+
IM_ASSERT(info->DescriptorPoolSize == 0);
1133+
else
1134+
IM_ASSERT(info->DescriptorPoolSize > 0);
11141135
IM_ASSERT(info->MinImageCount >= 2);
11151136
IM_ASSERT(info->ImageCount >= info->MinImageCount);
11161137
if (info->UseDynamicRendering == false)
@@ -1159,19 +1180,20 @@ void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count)
11591180
bd->VulkanInitInfo.MinImageCount = min_image_count;
11601181
}
11611182

1162-
// Register a texture
1183+
// Register a texture by creating a descriptor
11631184
// FIXME: This is experimental in the sense that we are unsure how to best design/tackle this problem, please post to https://github.com/ocornut/imgui/pull/914 if you have suggestions.
11641185
VkDescriptorSet ImGui_ImplVulkan_AddTexture(VkSampler sampler, VkImageView image_view, VkImageLayout image_layout)
11651186
{
11661187
ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
11671188
ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
1189+
VkDescriptorPool pool = bd->DescriptorPool ? bd->DescriptorPool : v->DescriptorPool;
11681190

11691191
// Create Descriptor Set:
11701192
VkDescriptorSet descriptor_set;
11711193
{
11721194
VkDescriptorSetAllocateInfo alloc_info = {};
11731195
alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
1174-
alloc_info.descriptorPool = v->DescriptorPool;
1196+
alloc_info.descriptorPool = pool;
11751197
alloc_info.descriptorSetCount = 1;
11761198
alloc_info.pSetLayouts = &bd->DescriptorSetLayout;
11771199
VkResult err = vkAllocateDescriptorSets(v->Device, &alloc_info, &descriptor_set);
@@ -1199,7 +1221,8 @@ void ImGui_ImplVulkan_RemoveTexture(VkDescriptorSet descriptor_set)
11991221
{
12001222
ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
12011223
ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
1202-
vkFreeDescriptorSets(v->Device, v->DescriptorPool, 1, &descriptor_set);
1224+
VkDescriptorPool pool = bd->DescriptorPool ? bd->DescriptorPool : v->DescriptorPool;
1225+
vkFreeDescriptorSets(v->Device, pool, 1, &descriptor_set);
12031226
}
12041227

12051228
void ImGui_ImplVulkan_DestroyFrameRenderBuffers(VkDevice device, ImGui_ImplVulkan_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator)

backends/imgui_impl_vulkan.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,23 @@
6262
#endif
6363

6464
// Initialization data, for ImGui_ImplVulkan_Init()
65-
// - VkDescriptorPool should be created with VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT,
66-
// and must contain a pool size large enough to hold an ImGui VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER descriptor.
67-
// - When using dynamic rendering, set UseDynamicRendering=true and fill PipelineRenderingCreateInfo structure.
6865
// [Please zero-clear before use!]
66+
// - About descriptor pool:
67+
// - A VkDescriptorPool should be created with VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT,
68+
// and must contain a pool size large enough to hold a small number of VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER descriptors.
69+
// - As an convenience, by setting DescriptorPoolSize > 0 the backend will create one for you.
70+
// - Current version of the backend use 1 descriptor for the font atlas + as many as additional calls done to ImGui_ImplVulkan_AddTexture().
71+
// - It is expected that as early as Q1 2025 the backend will use a few more descriptors, so aim at 10 + number of desierd calls to ImGui_ImplVulkan_AddTexture().
72+
// - About dynamic rendering:
73+
// - When using dynamic rendering, set UseDynamicRendering=true and fill PipelineRenderingCreateInfo structure.
6974
struct ImGui_ImplVulkan_InitInfo
7075
{
7176
VkInstance Instance;
7277
VkPhysicalDevice PhysicalDevice;
7378
VkDevice Device;
7479
uint32_t QueueFamily;
7580
VkQueue Queue;
76-
VkDescriptorPool DescriptorPool; // See requirements in note above
81+
VkDescriptorPool DescriptorPool; // See requirements in note above; ignored if using DescriptorPoolSize > 0
7782
VkRenderPass RenderPass; // Ignored if using dynamic rendering
7883
uint32_t MinImageCount; // >= 2
7984
uint32_t ImageCount; // >= MinImageCount
@@ -83,6 +88,9 @@ struct ImGui_ImplVulkan_InitInfo
8388
VkPipelineCache PipelineCache;
8489
uint32_t Subpass;
8590

91+
// (Optional) Set to create internal descriptor pool instead of using DescriptorPool
92+
uint32_t DescriptorPoolSize;
93+
8694
// (Optional) Dynamic Rendering
8795
// Need to explicitly enable VK_KHR_dynamic_rendering extension to use this, even for Vulkan 1.3.
8896
bool UseDynamicRendering;

docs/CHANGELOG.txt

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ Other changes:
5959
- Tools: binary_to_compressed_c: added -u8/-u32/-base85 export options.
6060
- Demo: example tree used by Property Editor & Selection demos properly freed
6161
on application closure. (#8158) [@Legulysse]
62+
- Backends: Vulkan: Make user-provided descriptor pool optional. As a convenience,
63+
when setting init_info->DescriptorPoolSize then the backend will create and manage
64+
one itself. (#8172, #4867) [@zeux]
6265
- Examples: Win32+DX12: Using a basic free-list allocator to manage multiple
6366
SRV descriptors.
6467

0 commit comments

Comments
 (0)