]> git.tdb.fi Git - libs/gl.git/commitdiff
Reuse previously allocated command buffers
authorMikko Rasa <tdb@tdb.fi>
Mon, 22 Nov 2021 11:34:00 +0000 (13:34 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 22 Nov 2021 11:34:00 +0000 (13:34 +0200)
source/backends/vulkan/commands_backend.cpp
source/backends/vulkan/commands_backend.h

index 54282c36b675235e9b642c099bf8b588adb40d0c..6f5703f60d1c98ac7e9563dc8360638d082b11fb 100644 (file)
@@ -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, &current_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;
        }
 }
 
index b87d73e0b71b5bffb22ff5c13133cf8e53ac2f21..a89225a6f6cdaa82279ec08e444e500b79789161 100644 (file)
@@ -25,6 +25,8 @@ protected:
        {
                Device &device;
                VkCommandPool pool = 0;
+               std::vector<VkCommandBuffer> buffers;
+               unsigned next_buffer = 0;
                Fence fence;
                bool in_use = false;