Allocate command buffers from pools

This commit is contained in:
Jérôme Leclercq
2020-08-27 18:31:26 +02:00
parent cbdac32f5f
commit 7c9dcdfbe4
18 changed files with 333 additions and 23 deletions

View File

@@ -23,6 +23,38 @@ namespace Nz
if (!m_commandPool.Create(device, queueFamilyIndex))
throw std::runtime_error("Failed to create command pool: " + TranslateVulkanError(m_commandPool.GetLastErrorCode()));
}
template<typename... Args>
CommandBufferPtr VulkanCommandPool::AllocateFromPool(std::size_t poolIndex, Args&&... args)
{
auto& pool = m_commandPools[poolIndex];
std::size_t freeBindingId = pool.freeCommands.FindFirst();
if (freeBindingId == pool.freeCommands.npos)
return {}; //< No free binding in this pool
pool.freeCommands.Reset(freeBindingId);
VulkanCommandBuffer* freeBindingMemory = reinterpret_cast<VulkanCommandBuffer*>(&pool.storage[freeBindingId]);
return CommandBufferPtr(PlacementNew(freeBindingMemory, *this, poolIndex, freeBindingId, std::forward<Args>(args)...));
}
inline void VulkanCommandPool::TryToShrink()
{
std::size_t poolCount = m_commandPools.size();
if (poolCount >= 2 && m_commandPools.back().freeCommands.TestAll())
{
for (std::size_t i = poolCount - 1; i > 0; --i)
{
if (!m_commandPools[i].freeCommands.TestAll())
break;
poolCount--;
}
m_commandPools.resize(poolCount);
}
}
}
#include <Nazara/VulkanRenderer/DebugOff.hpp>