From e4ae14435fa93ac1bcb13c6838b0ad3c923c8fde Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 22 Nov 2021 13:34:00 +0200 Subject: [PATCH] Reuse previously allocated command buffers --- source/backends/vulkan/commands_backend.cpp | 20 ++++++++++++++------ source/backends/vulkan/commands_backend.h | 2 ++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/source/backends/vulkan/commands_backend.cpp b/source/backends/vulkan/commands_backend.cpp index 54282c36..6f5703f6 100644 --- a/source/backends/vulkan/commands_backend.cpp +++ b/source/backends/vulkan/commands_backend.cpp @@ -41,13 +41,20 @@ void VulkanCommands::begin_buffer() current_pool->in_use = true; } - VkCommandBufferAllocateInfo alloc_info = { }; - alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; - alloc_info.commandPool = handle_cast<::VkCommandPool>(current_pool->pool); - alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; - alloc_info.commandBufferCount = 1; + if(current_pool->next_buffer>=current_pool->buffers.size()) + { + VkCommandBufferAllocateInfo alloc_info = { }; + alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; + alloc_info.commandPool = handle_cast<::VkCommandPool>(current_pool->pool); + alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; + alloc_info.commandBufferCount = 1; + + VkCommandBuffer buffer; + vk.AllocateCommandBuffers(alloc_info, &buffer); + current_pool->buffers.push_back(buffer); + } - vk.AllocateCommandBuffers(alloc_info, ¤t_buffer); + current_buffer = current_pool->buffers[current_pool->next_buffer++]; VkCommandBufferBeginInfo begin_info = { }; begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; @@ -158,6 +165,7 @@ void VulkanCommands::begin_frame(unsigned index) current_pool->fence.wait(); vk.ResetCommandPool(current_pool->pool, 0); current_pool->in_use = false; + current_pool->next_buffer = 0; } } diff --git a/source/backends/vulkan/commands_backend.h b/source/backends/vulkan/commands_backend.h index b87d73e0..a89225a6 100644 --- a/source/backends/vulkan/commands_backend.h +++ b/source/backends/vulkan/commands_backend.h @@ -25,6 +25,8 @@ protected: { Device &device; VkCommandPool pool = 0; + std::vector buffers; + unsigned next_buffer = 0; Fence fence; bool in_use = false; -- 2.45.2