Allocate command buffers from pools
This commit is contained in:
parent
cbdac32f5f
commit
7c9dcdfbe4
|
|
@ -22,12 +22,14 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
class OpenGLCommandPool;
|
||||
class OpenGLFramebuffer;
|
||||
|
||||
class NAZARA_OPENGLRENDERER_API OpenGLCommandBuffer final : public CommandBuffer
|
||||
{
|
||||
public:
|
||||
OpenGLCommandBuffer() = default;
|
||||
inline OpenGLCommandBuffer();
|
||||
inline OpenGLCommandBuffer(OpenGLCommandPool& owner, std::size_t poolIndex, std::size_t bindingIndex);
|
||||
OpenGLCommandBuffer(const OpenGLCommandBuffer&) = delete;
|
||||
OpenGLCommandBuffer(OpenGLCommandBuffer&&) noexcept = default;
|
||||
~OpenGLCommandBuffer() = default;
|
||||
|
|
@ -49,6 +51,10 @@ namespace Nz
|
|||
|
||||
void Execute();
|
||||
|
||||
inline std::size_t GetBindingIndex() const;
|
||||
inline std::size_t GetPoolIndex() const;
|
||||
inline const OpenGLCommandPool& GetOwner() const;
|
||||
|
||||
inline void SetFramebuffer(const OpenGLFramebuffer& framebuffer, const RenderPass& renderPass, std::initializer_list<CommandBufferBuilder::ClearValues> clearValues);
|
||||
inline void SetScissor(Nz::Recti scissorRegion);
|
||||
inline void SetViewport(Nz::Recti viewportRegion);
|
||||
|
|
@ -60,6 +66,7 @@ namespace Nz
|
|||
struct DrawStates;
|
||||
|
||||
void ApplyStates(const GL::Context& context, const DrawStates& states);
|
||||
void Release();
|
||||
|
||||
struct BeginDebugRegionData
|
||||
{
|
||||
|
|
@ -139,7 +146,10 @@ namespace Nz
|
|||
>;
|
||||
|
||||
DrawStates m_currentStates;
|
||||
std::size_t m_bindingIndex;
|
||||
std::size_t m_poolIndex;
|
||||
std::vector<CommandData> m_commands;
|
||||
OpenGLCommandPool* m_owner;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,11 +4,24 @@
|
|||
|
||||
#include <Nazara/OpenGLRenderer/OpenGLCommandBuffer.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLFramebuffer.hpp>
|
||||
#include <cassert>
|
||||
#include <stdexcept>
|
||||
#include <Nazara/OpenGLRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline OpenGLCommandBuffer::OpenGLCommandBuffer() :
|
||||
m_owner(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
inline OpenGLCommandBuffer::OpenGLCommandBuffer(OpenGLCommandPool& owner, std::size_t poolIndex, std::size_t bindingIndex) :
|
||||
m_bindingIndex(bindingIndex),
|
||||
m_poolIndex(poolIndex),
|
||||
m_owner(&owner)
|
||||
{
|
||||
}
|
||||
|
||||
inline void OpenGLCommandBuffer::BeginDebugRegion(const std::string_view& regionName, const Nz::Color& color)
|
||||
{
|
||||
BeginDebugRegionData beginDebugRegion;
|
||||
|
|
@ -104,6 +117,22 @@ namespace Nz
|
|||
m_commands.emplace_back(EndDebugRegionData{});
|
||||
}
|
||||
|
||||
inline std::size_t Nz::OpenGLCommandBuffer::GetBindingIndex() const
|
||||
{
|
||||
return m_bindingIndex;
|
||||
}
|
||||
|
||||
inline std::size_t Nz::OpenGLCommandBuffer::GetPoolIndex() const
|
||||
{
|
||||
return m_poolIndex;
|
||||
}
|
||||
|
||||
inline const OpenGLCommandPool& OpenGLCommandBuffer::GetOwner() const
|
||||
{
|
||||
assert(m_owner);
|
||||
return *m_owner;
|
||||
}
|
||||
|
||||
inline void OpenGLCommandBuffer::SetFramebuffer(const OpenGLFramebuffer& framebuffer, const RenderPass& /*renderPass*/, std::initializer_list<CommandBufferBuilder::ClearValues> clearValues)
|
||||
{
|
||||
SetFrameBufferData setFramebuffer;
|
||||
|
|
|
|||
|
|
@ -8,23 +8,45 @@
|
|||
#define NAZARA_OPENGLRENDERER_OPENGLCOMMANDPOOL_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/Bitset.hpp>
|
||||
#include <Nazara/Renderer/CommandPool.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Config.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLCommandBuffer.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_OPENGLRENDERER_API OpenGLCommandPool final : public CommandPool
|
||||
{
|
||||
friend OpenGLCommandBuffer;
|
||||
|
||||
public:
|
||||
OpenGLCommandPool() = default;
|
||||
OpenGLCommandPool(const OpenGLCommandPool&) = delete;
|
||||
OpenGLCommandPool(OpenGLCommandPool&&) noexcept = default;
|
||||
~OpenGLCommandPool() = default;
|
||||
|
||||
std::unique_ptr<CommandBuffer> BuildCommandBuffer(const std::function<void(CommandBufferBuilder& builder)>& callback) override;
|
||||
CommandBufferPtr BuildCommandBuffer(const std::function<void(CommandBufferBuilder& builder)>& callback) override;
|
||||
|
||||
OpenGLCommandPool& operator=(const OpenGLCommandPool&) = delete;
|
||||
OpenGLCommandPool& operator=(OpenGLCommandPool&&) = delete;
|
||||
|
||||
private:
|
||||
struct CommandPool;
|
||||
|
||||
CommandPool& AllocatePool();
|
||||
CommandBufferPtr AllocateFromPool(std::size_t poolIndex);
|
||||
void Release(CommandBuffer& commandBuffer);
|
||||
inline void TryToShrink();
|
||||
|
||||
struct CommandPool
|
||||
{
|
||||
using BindingStorage = std::aligned_storage_t<sizeof(OpenGLCommandBuffer), alignof(OpenGLCommandBuffer)>;
|
||||
|
||||
Bitset<UInt64> freeCommands;
|
||||
std::unique_ptr<BindingStorage[]> storage;
|
||||
};
|
||||
|
||||
std::vector<CommandPool> m_commandPools;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,22 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
inline void OpenGLCommandPool::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/OpenGLRenderer/DebugOff.hpp>
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ namespace Nz
|
|||
case PixelFormat_A8: return GLTextureFormat { GL_R8, GL_RED, GL_UNSIGNED_BYTE, GL_ZERO, GL_ZERO, GL_ZERO, GL_RED };
|
||||
case PixelFormat_RGB8: return GLTextureFormat { GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE, GL_RED, GL_GREEN, GL_BLUE, GL_ZERO };
|
||||
case PixelFormat_RGBA8: return GLTextureFormat { GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
|
||||
default: break;
|
||||
}
|
||||
|
||||
NazaraError("Unhandled PixelFormat 0x" + String::Number(UnderlyingCast(pixelFormat), 16));
|
||||
|
|
|
|||
|
|
@ -9,19 +9,36 @@
|
|||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Renderer/Config.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class CommandBuffer;
|
||||
class CommandBufferDeleter;
|
||||
|
||||
using CommandBufferPtr = std::unique_ptr<CommandBuffer, CommandBufferDeleter>;
|
||||
|
||||
class NAZARA_RENDERER_API CommandBuffer
|
||||
{
|
||||
friend CommandBufferDeleter;
|
||||
|
||||
public:
|
||||
CommandBuffer() = default;
|
||||
CommandBuffer(const CommandBuffer&) = delete;
|
||||
CommandBuffer(CommandBuffer&&) = default;
|
||||
CommandBuffer(CommandBuffer&&) = delete;
|
||||
virtual ~CommandBuffer();
|
||||
|
||||
CommandBuffer& operator=(const CommandBuffer&) = delete;
|
||||
CommandBuffer& operator=(CommandBuffer&&) = default;
|
||||
CommandBuffer& operator=(CommandBuffer&&) = delete;
|
||||
|
||||
protected:
|
||||
virtual void Release() = 0;
|
||||
};
|
||||
|
||||
class CommandBufferDeleter
|
||||
{
|
||||
public:
|
||||
inline void operator()(CommandBuffer* commandBuffer);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,10 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
inline void CommandBufferDeleter::operator()(CommandBuffer* commandBuffer)
|
||||
{
|
||||
commandBuffer->Release();
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Renderer/DebugOff.hpp>
|
||||
|
|
|
|||
|
|
@ -9,12 +9,11 @@
|
|||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Renderer/Config.hpp>
|
||||
#include <Nazara/Renderer/CommandBuffer.hpp>
|
||||
#include <functional>
|
||||
#include <memory> //< temporary
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class CommandBuffer;
|
||||
class CommandBufferBuilder;
|
||||
|
||||
class NAZARA_RENDERER_API CommandPool
|
||||
|
|
@ -25,7 +24,7 @@ namespace Nz
|
|||
CommandPool(CommandPool&&) = default;
|
||||
virtual ~CommandPool();
|
||||
|
||||
virtual std::unique_ptr<CommandBuffer> BuildCommandBuffer(const std::function<void(CommandBufferBuilder& builder)>& callback) = 0;
|
||||
virtual CommandBufferPtr BuildCommandBuffer(const std::function<void(CommandBufferBuilder& builder)>& callback) = 0;
|
||||
|
||||
CommandPool& operator=(const CommandPool&) = delete;
|
||||
CommandPool& operator=(CommandPool&&) = default;
|
||||
|
|
|
|||
|
|
@ -30,10 +30,15 @@ namespace Nz
|
|||
struct Binding;
|
||||
|
||||
ShaderBinding() = default;
|
||||
ShaderBinding(const ShaderBinding&) = delete;
|
||||
ShaderBinding(ShaderBinding&&) = delete;
|
||||
virtual ~ShaderBinding();
|
||||
|
||||
virtual void Update(std::initializer_list<Binding> bindings) = 0;
|
||||
|
||||
ShaderBinding& operator=(const ShaderBinding&) = delete;
|
||||
ShaderBinding& operator=(ShaderBinding&&) = delete;
|
||||
|
||||
struct TextureBinding
|
||||
{
|
||||
Texture* texture;
|
||||
|
|
@ -55,9 +60,6 @@ namespace Nz
|
|||
|
||||
protected:
|
||||
virtual void Release() = 0;
|
||||
|
||||
ShaderBinding(const ShaderBinding&) = delete;
|
||||
ShaderBinding(ShaderBinding&&) = default;
|
||||
};
|
||||
|
||||
class ShaderBindingDeleter
|
||||
|
|
|
|||
|
|
@ -15,22 +15,34 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
class VulkanCommandPool;
|
||||
|
||||
class NAZARA_VULKANRENDERER_API VulkanCommandBuffer final : public CommandBuffer
|
||||
{
|
||||
public:
|
||||
inline VulkanCommandBuffer(Vk::AutoCommandBuffer commandBuffer);
|
||||
inline VulkanCommandBuffer(std::vector<Vk::AutoCommandBuffer> commandBuffers);
|
||||
inline VulkanCommandBuffer(VulkanCommandPool& owner, std::size_t poolIndex, std::size_t bindingIndex, Vk::AutoCommandBuffer commandBuffer);
|
||||
inline VulkanCommandBuffer(VulkanCommandPool& owner, std::size_t poolIndex, std::size_t bindingIndex, std::vector<Vk::AutoCommandBuffer> commandBuffers);
|
||||
VulkanCommandBuffer(const VulkanCommandBuffer&) = delete;
|
||||
VulkanCommandBuffer(VulkanCommandBuffer&&) noexcept = default;
|
||||
~VulkanCommandBuffer() = default;
|
||||
|
||||
inline std::size_t GetBindingIndex() const;
|
||||
inline Vk::CommandBuffer& GetCommandBuffer(std::size_t imageIndex = 0);
|
||||
inline std::size_t GetPoolIndex() const;
|
||||
inline const VulkanCommandPool& GetOwner() const;
|
||||
|
||||
VulkanCommandBuffer& operator=(const VulkanCommandBuffer&) = delete;
|
||||
VulkanCommandBuffer& operator=(VulkanCommandBuffer&&) = delete;
|
||||
|
||||
private:
|
||||
inline VulkanCommandBuffer(VulkanCommandPool& owner, std::size_t poolIndex, std::size_t bindingIndex);
|
||||
|
||||
void Release() override;
|
||||
|
||||
std::size_t m_bindingIndex;
|
||||
std::size_t m_poolIndex;
|
||||
std::vector<Vk::AutoCommandBuffer> m_commandBuffers;
|
||||
VulkanCommandPool& m_owner;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,20 +7,44 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
inline VulkanCommandBuffer::VulkanCommandBuffer(Vk::AutoCommandBuffer commandBuffer)
|
||||
inline VulkanCommandBuffer::VulkanCommandBuffer(VulkanCommandPool& owner, std::size_t poolIndex, std::size_t bindingIndex, Vk::AutoCommandBuffer commandBuffer) :
|
||||
VulkanCommandBuffer(owner, poolIndex, bindingIndex)
|
||||
{
|
||||
m_commandBuffers.push_back(std::move(commandBuffer));
|
||||
}
|
||||
|
||||
inline VulkanCommandBuffer::VulkanCommandBuffer(std::vector<Vk::AutoCommandBuffer> commandBuffers) :
|
||||
m_commandBuffers(std::move(commandBuffers))
|
||||
inline VulkanCommandBuffer::VulkanCommandBuffer(VulkanCommandPool& owner, std::size_t poolIndex, std::size_t bindingIndex, std::vector<Vk::AutoCommandBuffer> commandBuffers) :
|
||||
VulkanCommandBuffer(owner, poolIndex, bindingIndex)
|
||||
{
|
||||
m_commandBuffers = std::move(commandBuffers);
|
||||
}
|
||||
|
||||
inline VulkanCommandBuffer::VulkanCommandBuffer(VulkanCommandPool& owner, std::size_t poolIndex, std::size_t bindingIndex) :
|
||||
m_bindingIndex(bindingIndex),
|
||||
m_poolIndex(poolIndex),
|
||||
m_owner(owner)
|
||||
{
|
||||
}
|
||||
|
||||
inline std::size_t VulkanCommandBuffer::GetBindingIndex() const
|
||||
{
|
||||
return m_bindingIndex;
|
||||
}
|
||||
|
||||
inline Vk::CommandBuffer& VulkanCommandBuffer::GetCommandBuffer(std::size_t imageIndex)
|
||||
{
|
||||
return m_commandBuffers[imageIndex].Get();
|
||||
}
|
||||
|
||||
inline std::size_t VulkanCommandBuffer::GetPoolIndex() const
|
||||
{
|
||||
return m_poolIndex;
|
||||
}
|
||||
|
||||
inline const VulkanCommandPool& VulkanCommandBuffer::GetOwner() const
|
||||
{
|
||||
return m_owner;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/VulkanRenderer/DebugOff.hpp>
|
||||
|
|
|
|||
|
|
@ -8,14 +8,19 @@
|
|||
#define NAZARA_VULKANRENDERER_VULKANCOMMANDPOOL_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/Bitset.hpp>
|
||||
#include <Nazara/Renderer/CommandPool.hpp>
|
||||
#include <Nazara/VulkanRenderer/Config.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanCommandBuffer.hpp>
|
||||
#include <Nazara/VulkanRenderer/Wrapper/CommandPool.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_VULKANRENDERER_API VulkanCommandPool final : public CommandPool
|
||||
{
|
||||
friend VulkanCommandBuffer;
|
||||
|
||||
public:
|
||||
inline VulkanCommandPool(Vk::Device& device, QueueType queueType);
|
||||
inline VulkanCommandPool(Vk::Device& device, UInt32 queueFamilyIndex);
|
||||
|
|
@ -23,12 +28,29 @@ namespace Nz
|
|||
VulkanCommandPool(VulkanCommandPool&&) noexcept = default;
|
||||
~VulkanCommandPool() = default;
|
||||
|
||||
std::unique_ptr<CommandBuffer> BuildCommandBuffer(const std::function<void(CommandBufferBuilder& builder)>& callback) override;
|
||||
CommandBufferPtr BuildCommandBuffer(const std::function<void(CommandBufferBuilder& builder)>& callback) override;
|
||||
|
||||
VulkanCommandPool& operator=(const VulkanCommandPool&) = delete;
|
||||
VulkanCommandPool& operator=(VulkanCommandPool&&) = delete;
|
||||
|
||||
private:
|
||||
struct CommandPool;
|
||||
|
||||
CommandPool& AllocatePool();
|
||||
template<typename... Args> CommandBufferPtr AllocateFromPool(std::size_t poolIndex, Args&&... args);
|
||||
void Release(CommandBuffer& commandBuffer);
|
||||
inline void TryToShrink();
|
||||
|
||||
struct CommandPool
|
||||
{
|
||||
using BindingStorage = std::aligned_storage_t<sizeof(VulkanCommandBuffer), alignof(VulkanCommandBuffer)>;
|
||||
|
||||
Bitset<UInt64> freeCommands;
|
||||
std::unique_ptr<BindingStorage[]> storage;
|
||||
};
|
||||
|
||||
MovablePtr<Vk::Device> m_device;
|
||||
std::vector<CommandPool> m_commandPools;
|
||||
Vk::CommandPool m_commandPool;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/OpenGLRenderer/OpenGLCommandBuffer.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLCommandPool.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLVaoCache.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Wrapper/VertexArray.hpp>
|
||||
|
|
@ -139,4 +140,10 @@ namespace Nz
|
|||
const GL::VertexArray& vao = context.GetVaoCache().Get(vaoSetup);
|
||||
context.BindVertexArray(vao.GetObjectId(), true);
|
||||
}
|
||||
|
||||
void OpenGLCommandBuffer::Release()
|
||||
{
|
||||
assert(m_owner);
|
||||
m_owner->Release(*this);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,19 +3,82 @@
|
|||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/OpenGLRenderer/OpenGLCommandPool.hpp>
|
||||
#include <Nazara/Core/MemoryHelper.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLCommandBuffer.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLCommandBufferBuilder.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
std::unique_ptr<CommandBuffer> OpenGLCommandPool::BuildCommandBuffer(const std::function<void(CommandBufferBuilder& builder)>& callback)
|
||||
CommandBufferPtr OpenGLCommandPool::BuildCommandBuffer(const std::function<void(CommandBufferBuilder& builder)>& callback)
|
||||
{
|
||||
std::unique_ptr<OpenGLCommandBuffer> commandBuffer = std::make_unique<OpenGLCommandBuffer>();
|
||||
CommandBufferPtr commandBuffer;
|
||||
for (std::size_t i = 0; i < m_commandPools.size(); ++i)
|
||||
{
|
||||
commandBuffer = AllocateFromPool(i);
|
||||
if (commandBuffer)
|
||||
break;
|
||||
}
|
||||
|
||||
OpenGLCommandBufferBuilder builder(*commandBuffer);
|
||||
if (!commandBuffer)
|
||||
{
|
||||
// No allocation could be made, time to allocate a new pool
|
||||
std::size_t newPoolIndex = m_commandPools.size();
|
||||
AllocatePool();
|
||||
|
||||
commandBuffer = AllocateFromPool(newPoolIndex);
|
||||
assert(commandBuffer);
|
||||
}
|
||||
|
||||
OpenGLCommandBufferBuilder builder(static_cast<OpenGLCommandBuffer&>(*commandBuffer.get()));
|
||||
callback(builder);
|
||||
|
||||
return commandBuffer;
|
||||
}
|
||||
|
||||
auto OpenGLCommandPool::AllocatePool() -> CommandPool&
|
||||
{
|
||||
constexpr UInt32 MaxSet = 128;
|
||||
|
||||
CommandPool pool;
|
||||
pool.freeCommands.Resize(MaxSet, true);
|
||||
pool.storage = std::make_unique<CommandPool::BindingStorage[]>(MaxSet);
|
||||
|
||||
return m_commandPools.emplace_back(std::move(pool));
|
||||
}
|
||||
|
||||
CommandBufferPtr OpenGLCommandPool::AllocateFromPool(std::size_t poolIndex)
|
||||
{
|
||||
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);
|
||||
|
||||
OpenGLCommandBuffer* freeBindingMemory = reinterpret_cast<OpenGLCommandBuffer*>(&pool.storage[freeBindingId]);
|
||||
return CommandBufferPtr(PlacementNew(freeBindingMemory, *this, poolIndex, freeBindingId));
|
||||
}
|
||||
|
||||
void OpenGLCommandPool::Release(CommandBuffer& binding)
|
||||
{
|
||||
OpenGLCommandBuffer& openglBinding = static_cast<OpenGLCommandBuffer&>(binding);
|
||||
|
||||
std::size_t poolIndex = openglBinding.GetPoolIndex();
|
||||
std::size_t bindingIndex = openglBinding.GetBindingIndex();
|
||||
|
||||
assert(poolIndex < m_commandPools.size());
|
||||
auto& pool = m_commandPools[poolIndex];
|
||||
assert(!pool.freeCommands.Test(bindingIndex));
|
||||
|
||||
OpenGLCommandBuffer* bindingMemory = reinterpret_cast<OpenGLCommandBuffer*>(&pool.storage[bindingIndex]);
|
||||
PlacementDestroy(bindingMemory);
|
||||
|
||||
pool.freeCommands.Set(bindingIndex);
|
||||
|
||||
// Try to free pool if it's one of the last one
|
||||
if (poolIndex >= m_commandPools.size() - 1 && poolIndex <= m_commandPools.size())
|
||||
TryToShrink();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ namespace Nz
|
|||
|
||||
void OpenGLRenderImage::Execute(const std::function<void(CommandBufferBuilder& builder)>& callback, QueueTypeFlags /*queueTypeFlags*/)
|
||||
{
|
||||
OpenGLCommandBuffer commandBuffer;
|
||||
OpenGLCommandBuffer commandBuffer; //< TODO: Use a pool and remove default constructor
|
||||
OpenGLCommandBufferBuilder builder(commandBuffer);
|
||||
callback(builder);
|
||||
|
||||
|
|
|
|||
|
|
@ -3,8 +3,13 @@
|
|||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/VulkanRenderer/VulkanCommandBuffer.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanCommandPool.hpp>
|
||||
#include <Nazara/VulkanRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
void VulkanCommandBuffer::Release()
|
||||
{
|
||||
m_owner.Release(*this);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/VulkanRenderer/VulkanCommandPool.hpp>
|
||||
#include <Nazara/Core/StackVector.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanCommandBuffer.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanCommandBufferBuilder.hpp>
|
||||
#include <Nazara/VulkanRenderer/Wrapper/CommandBuffer.hpp>
|
||||
|
|
@ -10,14 +11,14 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
std::unique_ptr<CommandBuffer> VulkanCommandPool::BuildCommandBuffer(const std::function<void(CommandBufferBuilder& builder)>& callback)
|
||||
CommandBufferPtr VulkanCommandPool::BuildCommandBuffer(const std::function<void(CommandBufferBuilder& builder)>& callback)
|
||||
{
|
||||
std::vector<Vk::AutoCommandBuffer> commandBuffers;
|
||||
auto BuildCommandBuffer = [&](std::size_t imageIndex)
|
||||
{
|
||||
Vk::AutoCommandBuffer& commandBuffer = commandBuffers.emplace_back(m_commandPool.AllocateCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY));
|
||||
|
||||
if (!commandBuffer->Begin(VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT))
|
||||
if (!commandBuffer->Begin())
|
||||
throw std::runtime_error("failed to begin command buffer: " + TranslateVulkanError(commandBuffer->GetLastErrorCode()));
|
||||
|
||||
VulkanCommandBufferBuilder builder(commandBuffer.Get(), imageIndex);
|
||||
|
|
@ -33,6 +34,50 @@ namespace Nz
|
|||
for (std::size_t i = 1; i < maxFramebufferCount; ++i)
|
||||
BuildCommandBuffer(i);
|
||||
|
||||
return std::make_unique<VulkanCommandBuffer>(std::move(commandBuffers));
|
||||
for (std::size_t i = 0; i < m_commandPools.size(); ++i)
|
||||
{
|
||||
if (m_commandPools[i].freeCommands.TestNone())
|
||||
continue;
|
||||
|
||||
return AllocateFromPool(i, std::move(commandBuffers));
|
||||
}
|
||||
|
||||
// No allocation could be made, time to allocate a new pool
|
||||
std::size_t newPoolIndex = m_commandPools.size();
|
||||
AllocatePool();
|
||||
|
||||
return AllocateFromPool(newPoolIndex, std::move(commandBuffers));
|
||||
}
|
||||
|
||||
auto VulkanCommandPool::AllocatePool() -> CommandPool&
|
||||
{
|
||||
constexpr UInt32 MaxSet = 128;
|
||||
|
||||
CommandPool pool;
|
||||
pool.freeCommands.Resize(MaxSet, true);
|
||||
pool.storage = std::make_unique<CommandPool::BindingStorage[]>(MaxSet);
|
||||
|
||||
return m_commandPools.emplace_back(std::move(pool));
|
||||
}
|
||||
|
||||
void VulkanCommandPool::Release(CommandBuffer& binding)
|
||||
{
|
||||
VulkanCommandBuffer& vulkanBinding = static_cast<VulkanCommandBuffer&>(binding);
|
||||
|
||||
std::size_t poolIndex = vulkanBinding.GetPoolIndex();
|
||||
std::size_t bindingIndex = vulkanBinding.GetBindingIndex();
|
||||
|
||||
assert(poolIndex < m_commandPools.size());
|
||||
auto& pool = m_commandPools[poolIndex];
|
||||
assert(!pool.freeCommands.Test(bindingIndex));
|
||||
|
||||
VulkanCommandBuffer* bindingMemory = reinterpret_cast<VulkanCommandBuffer*>(&pool.storage[bindingIndex]);
|
||||
PlacementDestroy(bindingMemory);
|
||||
|
||||
pool.freeCommands.Set(bindingIndex);
|
||||
|
||||
// Try to free pool if it's one of the last one
|
||||
if (poolIndex >= m_commandPools.size() - 1 && poolIndex <= m_commandPools.size())
|
||||
TryToShrink();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue