-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce a MinAllocationSize field in ImGui_ImplVulkan_InitInfo that allows users to solve #4238 #7189
Conversation
…plVulkan_InitInfo
Merged as 4778560, thank you ! |
After further inspection, the code added by this PR is unsound: imgui/backends/imgui_impl_vulkan.cpp Line 407 in 4778560
The issue is that the size of the buffer and of the allocation are not the same, i thought it would be fine, no big deal, but then the allocation size is used as the size of the buffer when the buffer are in need to be resized. imgui/backends/imgui_impl_vulkan.cpp Line 498 in 4778560
The bug was already present but never occurred because The fix is quite simple: --- a/backends/imgui_impl_vulkan.cpp
+++ b/backends/imgui_impl_vulkan.cpp
@@ -395,7 +395,7 @@ static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory
if (buffer_memory != VK_NULL_HANDLE)
vkFreeMemory(v->Device, buffer_memory, v->Allocator);
- VkDeviceSize vertex_buffer_size_aligned = ((new_size - 1) / bd->BufferMemoryAlignment + 1) * bd->BufferMemoryAlignment;
+ VkDeviceSize vertex_buffer_size_aligned = ((IM_MAX(v->MinAllocationSize, new_size) - 1) / bd->BufferMemoryAlignment + 1) * bd->BufferMemoryAlignment;
VkBufferCreateInfo buffer_info = {};
buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
buffer_info.size = vertex_buffer_size_aligned;
@@ -407,17 +407,16 @@ static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory
VkMemoryRequirements req;
vkGetBufferMemoryRequirements(v->Device, buffer, &req);
bd->BufferMemoryAlignment = (bd->BufferMemoryAlignment > req.alignment) ? bd->BufferMemoryAlignment : req.alignment;
- VkDeviceSize size = IM_MAX(v->MinAllocationSize, req.size);
VkMemoryAllocateInfo alloc_info = {};
alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
- alloc_info.allocationSize = size;
+ alloc_info.allocationSize = req.size;
alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &buffer_memory);
check_vk_result(err);
err = vkBindBufferMemory(v->Device, buffer, buffer_memory, 0);
check_vk_result(err);
- p_buffer_size = size;
+ p_buffer_size = vertex_buffer_size_aligned;
}
static void ImGui_ImplVulkan_SetupRenderState(ImDrawData* draw_data, VkPipeline pipeline, VkCommandBuffer command_buffer, ImGui_ImplVulkanH_FrameRenderBuffers* rb, int fb_width, int fb_height) Should i have opened an issue for this? A PR? |
A new field
MinAllocationSize
is introduced inImGui_ImplVulkan_InitInfo
that controls the minimum size of a GPU allocation (for usage in buffers and images).Such a field can be used to silence some warnings arising from Vulkan's Validation Layers when Best Practices are enabled:
The magic number
1024*1024
comes from the vulkan's validation layers source codeIf
MinAllocationSize
is set to 0, the current behavior is preservedScreenshots
init_info.MinAllocationSize = 0;