From b45c9c310f65e2393526683d4b1426efd38e020e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 28 Sep 2020 19:09:40 +0200 Subject: [PATCH] Vk: command pool and buffer reset. --- doc/vulkan-mapping.dox | 4 +- src/Magnum/Vk/CommandBuffer.cpp | 5 +++ src/Magnum/Vk/CommandBuffer.h | 39 ++++++++++++++++++- src/Magnum/Vk/CommandPool.cpp | 4 ++ src/Magnum/Vk/CommandPool.h | 45 ++++++++++++++++++++-- src/Magnum/Vk/Test/CommandBufferVkTest.cpp | 19 ++++++++- src/Magnum/Vk/Test/CommandPoolVkTest.cpp | 12 ++++++ 7 files changed, 120 insertions(+), 8 deletions(-) diff --git a/doc/vulkan-mapping.dox b/doc/vulkan-mapping.dox index 4a5cd8fdd..67988d118 100644 --- a/doc/vulkan-mapping.dox +++ b/doc/vulkan-mapping.dox @@ -251,8 +251,8 @@ Vulkan function | Matching API Vulkan function | Matching API --------------------------------------- | ------------ -@fn_vk{ResetCommandBuffer} | | -@fn_vk{ResetCommandPool} | | +@fn_vk{ResetCommandBuffer} | @ref CommandBuffer::reset() +@fn_vk{ResetCommandPool} | @ref CommandPool::reset() @fn_vk{ResetDescriptorPool} | | @fn_vk{ResetFences} | | @fn_vk{ResetQueryPool} @m_class{m-label m-flat m-success} **EXT, 1.2** | | diff --git a/src/Magnum/Vk/CommandBuffer.cpp b/src/Magnum/Vk/CommandBuffer.cpp index 0e4244c67..ba563a636 100644 --- a/src/Magnum/Vk/CommandBuffer.cpp +++ b/src/Magnum/Vk/CommandBuffer.cpp @@ -27,6 +27,7 @@ #include "Magnum/Vk/Device.h" #include "Magnum/Vk/Handle.h" +#include "Magnum/Vk/Result.h" namespace Magnum { namespace Vk { @@ -59,6 +60,10 @@ CommandBuffer& CommandBuffer::operator=(CommandBuffer&& other) noexcept { return *this; } +void CommandBuffer::reset(const CommandBufferResetFlags flags) { + MAGNUM_VK_INTERNAL_ASSERT_RESULT((**_device).ResetCommandBuffer(_handle, VkCommandBufferResetFlags(flags))); +} + VkCommandBuffer CommandBuffer::release() { const VkCommandBuffer handle = _handle; _handle = nullptr; diff --git a/src/Magnum/Vk/CommandBuffer.h b/src/Magnum/Vk/CommandBuffer.h index adc7c3153..f673dccb1 100644 --- a/src/Magnum/Vk/CommandBuffer.h +++ b/src/Magnum/Vk/CommandBuffer.h @@ -26,7 +26,7 @@ */ /** @file - * @brief Class @ref Magnum::Vk::CommandBuffer + * @brief Class @ref Magnum::Vk::CommandBuffer, enum @ref Magnum::Vk::CommandPoolResetFlag, enum set @ref Magnum::Vk::CommandPoolResetFlags * @m_since_latest */ @@ -41,6 +41,32 @@ namespace Magnum { namespace Vk { +/** +@brief Command buffer reset flag +@m_since_latest + +Wraps @type_vk_keyword{CommandBufferResetFlagBits}. +@m_enum_values_as_keywords +@see @ref CommandBufferResetFlags, @ref CommandBuffer::reset(), + @ref CommandPoolResetFlag, @ref CommandPool::reset() +*/ +enum class CommandBufferResetFlag: UnsignedInt { + /** Recycle all resources from the command pool back to the system */ + ReleaseResources = VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT +}; + +/** +@brief Command buffer reset flags +@m_since_latest + +Wraps @type_vk_keyword{CommandBufferResetFlags}. +@see @ref CommandBuffer::reset(), @ref CommandPoolResetFlags, + @ref CommandPool::reset() +*/ +typedef Containers::EnumSet CommandBufferResetFlags; + +CORRADE_ENUMSET_OPERATORS(CommandBufferResetFlags) + /** @brief Command buffer @m_since_latest @@ -103,6 +129,17 @@ class MAGNUM_VK_EXPORT CommandBuffer { /** @brief Handle flags */ HandleFlags handleFlags() const { return _flags; } + /** + * @brief Reset the command buffer + * + * This operation is allowed only if the originating @ref CommandPool + * was created with @ref CommandPoolCreateInfo::Flag::ResetCommandBuffer. + * If not, the only way to reset is to reset the whole command pool + * using @ref CommandPool::reset(). + * @see @fn_vk_keyword{ResetCommandBuffer} + */ + void reset(CommandBufferResetFlags flags = {}); + /** * @brief Release the underlying Vulkan command buffer * diff --git a/src/Magnum/Vk/CommandPool.cpp b/src/Magnum/Vk/CommandPool.cpp index c921bed95..625ebc841 100644 --- a/src/Magnum/Vk/CommandPool.cpp +++ b/src/Magnum/Vk/CommandPool.cpp @@ -92,6 +92,10 @@ CommandBuffer CommandPool::allocate(const CommandBufferLevel level) { return out; } +void CommandPool::reset(const CommandPoolResetFlags flags) { + MAGNUM_VK_INTERNAL_ASSERT_RESULT((**_device).ResetCommandPool(*_device, _handle, VkCommandPoolResetFlags(flags))); +} + VkCommandPool CommandPool::release() { const VkCommandPool handle = _handle; _handle = {}; diff --git a/src/Magnum/Vk/CommandPool.h b/src/Magnum/Vk/CommandPool.h index 7abacbe77..329fdbca1 100644 --- a/src/Magnum/Vk/CommandPool.h +++ b/src/Magnum/Vk/CommandPool.h @@ -26,7 +26,7 @@ */ /** @file - * @brief Class @ref Magnum::Vk::CommandPoolCreateInfo, @ref Magnum::Vk::CommandPool, enum @ref Magnum::Vk::CommandBufferLevel + * @brief Class @ref Magnum::Vk::CommandPoolCreateInfo, @ref Magnum::Vk::CommandPool, enum @ref Magnum::Vk::CommandBufferLevel, @ref Magnum::Vk::CommandPoolResetFlag, enum set @ref Magnum::Vk::CommandPoolResetFlags * @m_since_latest */ @@ -44,8 +44,8 @@ namespace Magnum { namespace Vk { @brief Command pool creation info @m_since_latest -Wraps a @type_vk_keyword{CommandPoolCreateInfo}. -@see @ref CommandPool +Wraps a @type_vk_keyword{CommandPoolCreateInfo}. See @ref CommandPool for usage +information. */ class MAGNUM_VK_EXPORT CommandPoolCreateInfo { public: @@ -62,7 +62,8 @@ class MAGNUM_VK_EXPORT CommandPoolCreateInfo { /** * Allow individual command buffers to be reset to initial state - * instead of just the whole pool. + * using @ref CommandBuffer::reset() instead of just the whole pool + * using @ref CommandPool::reset(). * * @m_class{m-note m-success} * @@ -146,6 +147,32 @@ enum class CommandBufferLevel: Int { Secondary = VK_COMMAND_BUFFER_LEVEL_SECONDARY }; +/** +@brief Command buffer reset flag +@m_since_latest + +Wraps @type_vk_keyword{CommandPoolResetFlagBits}. +@m_enum_values_as_keywords +@see @ref CommandPoolResetFlags, @ref CommandPool::reset(), + @ref CommandBufferResetFlag, @ref CommandBuffer::reset() +*/ +enum class CommandPoolResetFlag: UnsignedInt { + /** Recycle all resources from the command pool back to the system */ + ReleaseResources = VK_COMMAND_POOL_RESET_RELEASE_RESOURCES_BIT +}; + +/** +@brief Command pool reset flags +@m_since_latest + +Wraps @type_vk_keyword{CommandPoolResetFlags}. +@see @ref CommandPool::reset(), @ref CommandBufferResetFlags, + @ref CommandBuffer::reset() +*/ +typedef Containers::EnumSet CommandPoolResetFlags; + +CORRADE_ENUMSET_OPERATORS(CommandPoolResetFlags) + /** @brief Command pool @m_since_latest @@ -222,6 +249,16 @@ class MAGNUM_VK_EXPORT CommandPool { */ CommandBuffer allocate(CommandBufferLevel level = CommandBufferLevel::Primary); + /** + * @brief Reset the command pool + * + * All command buffers allocated from this command pool are reset as + * well. See @ref CommandBuffer::reset() for a way to reset a single + * command buffer. + * @see @fn_vk_keyword{ResetCommandPool} + */ + void reset(CommandPoolResetFlags flags = {}); + /** * @brief Release the underlying Vulkan command pool * diff --git a/src/Magnum/Vk/Test/CommandBufferVkTest.cpp b/src/Magnum/Vk/Test/CommandBufferVkTest.cpp index fb637ca4a..623f950a9 100644 --- a/src/Magnum/Vk/Test/CommandBufferVkTest.cpp +++ b/src/Magnum/Vk/Test/CommandBufferVkTest.cpp @@ -38,12 +38,16 @@ struct CommandBufferVkTest: VulkanTester { void construct(); void constructMove(); void wrap(); + + void reset(); }; CommandBufferVkTest::CommandBufferVkTest() { addTests({&CommandBufferVkTest::construct, &CommandBufferVkTest::constructMove, - &CommandBufferVkTest::wrap}); + &CommandBufferVkTest::wrap, + + &CommandBufferVkTest::reset}); } void CommandBufferVkTest::construct() { @@ -105,6 +109,19 @@ void CommandBufferVkTest::wrap() { device()->FreeCommandBuffers(device(), pool, 1, &buffer); } +void CommandBufferVkTest::reset() { + CommandPool pool{device(), CommandPoolCreateInfo{ + deviceProperties().pickQueueFamily(QueueFlag::Graphics), + CommandPoolCreateInfo::Flag::ResetCommandBuffer}}; + + CommandBuffer a = pool.allocate(); + + a.reset(CommandBufferResetFlag::ReleaseResources); + + /* Does not do anything visible, so just test that it didn't blow up */ + CORRADE_VERIFY(true); +} + }}}} CORRADE_TEST_MAIN(Magnum::Vk::Test::CommandBufferVkTest) diff --git a/src/Magnum/Vk/Test/CommandPoolVkTest.cpp b/src/Magnum/Vk/Test/CommandPoolVkTest.cpp index 3278e4d07..be8c36eaf 100644 --- a/src/Magnum/Vk/Test/CommandPoolVkTest.cpp +++ b/src/Magnum/Vk/Test/CommandPoolVkTest.cpp @@ -39,6 +39,7 @@ struct CommandPoolVkTest: VulkanTester { void constructMove(); void wrap(); + void reset(); void allocate(); }; @@ -47,6 +48,7 @@ CommandPoolVkTest::CommandPoolVkTest() { &CommandPoolVkTest::constructMove, &CommandPoolVkTest::wrap, + &CommandPoolVkTest::reset, &CommandPoolVkTest::allocate}); } @@ -102,6 +104,16 @@ void CommandPoolVkTest::wrap() { device()->DestroyCommandPool(device(), pool, nullptr); } +void CommandPoolVkTest::reset() { + CommandPool pool{device(), CommandPoolCreateInfo{ + deviceProperties().pickQueueFamily(QueueFlag::Graphics)}}; + + pool.reset(CommandPoolResetFlag::ReleaseResources); + + /* Does not do anything visible, so just test that it didn't blow up */ + CORRADE_VERIFY(true); +} + void CommandPoolVkTest::allocate() { CommandPool pool{device(), CommandPoolCreateInfo{ deviceProperties().pickQueueFamily(QueueFlag::Graphics)}};