mirror of https://github.com/mosra/magnum.git
11 changed files with 431 additions and 3 deletions
@ -0,0 +1,68 @@ |
|||||||
|
/*
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||||
|
2020 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a |
||||||
|
copy of this software and associated documentation files (the "Software"), |
||||||
|
to deal in the Software without restriction, including without limitation |
||||||
|
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||||
|
and/or sell copies of the Software, and to permit persons to whom the |
||||||
|
Software is furnished to do so, subject to the following conditions: |
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included |
||||||
|
in all copies or substantial portions of the Software. |
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||||
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||||
|
DEALINGS IN THE SOFTWARE. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "CommandBuffer.h" |
||||||
|
|
||||||
|
#include "Magnum/Vk/Device.h" |
||||||
|
#include "Magnum/Vk/Handle.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Vk { |
||||||
|
|
||||||
|
CommandBuffer CommandBuffer::wrap(Device& device, const VkCommandPool pool, const VkCommandBuffer handle, const HandleFlags flags) { |
||||||
|
CommandBuffer out{NoCreate}; |
||||||
|
out._device = &device; |
||||||
|
out._pool = pool; |
||||||
|
out._handle = handle; |
||||||
|
out._flags = flags; |
||||||
|
return out; |
||||||
|
} |
||||||
|
|
||||||
|
CommandBuffer::CommandBuffer(NoCreateT) noexcept: _device{}, _pool{}, _handle{} {} |
||||||
|
|
||||||
|
CommandBuffer::CommandBuffer(CommandBuffer&& other) noexcept: _device{other._device}, _pool{other._pool}, _handle{other._handle}, _flags{other._flags} { |
||||||
|
other._handle = nullptr; |
||||||
|
} |
||||||
|
|
||||||
|
CommandBuffer::~CommandBuffer() { |
||||||
|
if(_handle && (_flags & HandleFlag::DestroyOnDestruction)) |
||||||
|
(**_device).FreeCommandBuffers(*_device, _pool, 1, &_handle); |
||||||
|
} |
||||||
|
|
||||||
|
CommandBuffer& CommandBuffer::operator=(CommandBuffer&& other) noexcept { |
||||||
|
using std::swap; |
||||||
|
swap(other._device, _device); |
||||||
|
swap(other._pool, _pool); |
||||||
|
swap(other._handle, _handle); |
||||||
|
swap(other._flags, _flags); |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
VkCommandBuffer CommandBuffer::release() { |
||||||
|
const VkCommandBuffer handle = _handle; |
||||||
|
_handle = nullptr; |
||||||
|
return handle; |
||||||
|
} |
||||||
|
|
||||||
|
}} |
||||||
@ -0,0 +1,129 @@ |
|||||||
|
#ifndef Magnum_Vk_CommandBuffer_h |
||||||
|
#define Magnum_Vk_CommandBuffer_h |
||||||
|
/*
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||||
|
2020 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a |
||||||
|
copy of this software and associated documentation files (the "Software"), |
||||||
|
to deal in the Software without restriction, including without limitation |
||||||
|
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||||
|
and/or sell copies of the Software, and to permit persons to whom the |
||||||
|
Software is furnished to do so, subject to the following conditions: |
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included |
||||||
|
in all copies or substantial portions of the Software. |
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||||
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||||
|
DEALINGS IN THE SOFTWARE. |
||||||
|
*/ |
||||||
|
|
||||||
|
/** @file
|
||||||
|
* @brief Class @ref Magnum::Vk::CommandBuffer |
||||||
|
* @m_since_latest |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <Corrade/Containers/EnumSet.h> |
||||||
|
#include <Corrade/Containers/Reference.h> |
||||||
|
|
||||||
|
#include "Magnum/Tags.h" |
||||||
|
#include "Magnum/Magnum.h" |
||||||
|
#include "Magnum/Vk/Vk.h" |
||||||
|
#include "Magnum/Vk/Vulkan.h" |
||||||
|
#include "Magnum/Vk/visibility.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Vk { |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Command buffer |
||||||
|
@m_since_latest |
||||||
|
|
||||||
|
Wraps a @type_vk_keyword{CommandBuffer}. |
||||||
|
*/ |
||||||
|
class MAGNUM_VK_EXPORT CommandBuffer { |
||||||
|
public: |
||||||
|
/**
|
||||||
|
* @brief Wrap existing Vulkan handle |
||||||
|
* @param device Vulkan device the command buffer is created on |
||||||
|
* @param pool Command pool the buffer is allocated from |
||||||
|
* @param handle The @type_vk{CommandBuffer} handle |
||||||
|
* @param flags Handle flags |
||||||
|
* |
||||||
|
* The @p handle is expected to be of an existing Vulkan command |
||||||
|
* buffer. Unlike a command buffer allocated using |
||||||
|
* @ref CommandPool::allocate(), the Vulkan command buffer is by |
||||||
|
* default not deleted on destruction, use @p flags for different |
||||||
|
* behavior. |
||||||
|
*/ |
||||||
|
static CommandBuffer wrap(Device& device, VkCommandPool pool, VkCommandBuffer handle, HandleFlags flags = {}); |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Construct without creating the instance |
||||||
|
* |
||||||
|
* The constructed instance is equivalent to moved-from state. Useful |
||||||
|
* in cases where you will overwrite the instance later anyway. Move |
||||||
|
* another object over it to make it useful. |
||||||
|
*/ |
||||||
|
explicit CommandBuffer(NoCreateT) noexcept; |
||||||
|
|
||||||
|
/** @brief Copying is not allowed */ |
||||||
|
CommandBuffer(const CommandBuffer&) = delete; |
||||||
|
|
||||||
|
/** @brief Move constructor */ |
||||||
|
CommandBuffer(CommandBuffer&& other) noexcept; |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Destructor |
||||||
|
* |
||||||
|
* Frees associated @type_vk{CommandBuffer} handle, unless the |
||||||
|
* instance was created using @ref wrap() without @ref HandleFlag::DestroyOnDestruction |
||||||
|
* specified. |
||||||
|
* @see @fn_vk_keyword{FreeCommandBuffers}, @ref release() |
||||||
|
*/ |
||||||
|
~CommandBuffer(); |
||||||
|
|
||||||
|
/** @brief Copying is not allowed */ |
||||||
|
CommandBuffer& operator=(const CommandBuffer&) = delete; |
||||||
|
|
||||||
|
/** @brief Move assignment */ |
||||||
|
CommandBuffer& operator=(CommandBuffer&& other) noexcept; |
||||||
|
|
||||||
|
/** @brief Underlying @type_vk{CommandBuffer} handle */ |
||||||
|
VkCommandBuffer handle() { return _handle; } |
||||||
|
/** @overload */ |
||||||
|
operator VkCommandBuffer() { return _handle; } |
||||||
|
|
||||||
|
/** @brief Handle flags */ |
||||||
|
HandleFlags handleFlags() const { return _flags; } |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Release the underlying Vulkan command buffer |
||||||
|
* |
||||||
|
* Releases ownership of the Vulkan command buffer and returns its |
||||||
|
* handle so @fn_vk{FreeCommandBuffers} is not called on destruction. |
||||||
|
* The internal state is then equivalent to moved-from state. |
||||||
|
* @see @ref wrap() |
||||||
|
*/ |
||||||
|
VkCommandBuffer release(); |
||||||
|
|
||||||
|
private: |
||||||
|
friend CommandPool; |
||||||
|
|
||||||
|
/* Can't be a reference because of the NoCreate constructor */ |
||||||
|
Device* _device; |
||||||
|
|
||||||
|
VkCommandPool _pool; /* Used only for vkFreeCommandBuffers() */ |
||||||
|
VkCommandBuffer _handle; |
||||||
|
HandleFlags _flags; |
||||||
|
}; |
||||||
|
|
||||||
|
}} |
||||||
|
|
||||||
|
#endif |
||||||
@ -0,0 +1,62 @@ |
|||||||
|
/*
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||||
|
2020 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a |
||||||
|
copy of this software and associated documentation files (the "Software"), |
||||||
|
to deal in the Software without restriction, including without limitation |
||||||
|
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||||
|
and/or sell copies of the Software, and to permit persons to whom the |
||||||
|
Software is furnished to do so, subject to the following conditions: |
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included |
||||||
|
in all copies or substantial portions of the Software. |
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||||
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||||
|
DEALINGS IN THE SOFTWARE. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <new> |
||||||
|
#include <Corrade/TestSuite/Tester.h> |
||||||
|
|
||||||
|
#include "Magnum/Vk/CommandBuffer.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Vk { namespace Test { namespace { |
||||||
|
|
||||||
|
struct CommandBufferTest: TestSuite::Tester { |
||||||
|
explicit CommandBufferTest(); |
||||||
|
|
||||||
|
void constructNoCreate(); |
||||||
|
void constructCopy(); |
||||||
|
}; |
||||||
|
|
||||||
|
CommandBufferTest::CommandBufferTest() { |
||||||
|
addTests({&CommandBufferTest::constructNoCreate, |
||||||
|
&CommandBufferTest::constructCopy}); |
||||||
|
} |
||||||
|
|
||||||
|
void CommandBufferTest::constructNoCreate() { |
||||||
|
{ |
||||||
|
CommandBuffer buffer{NoCreate}; |
||||||
|
CORRADE_VERIFY(!buffer.handle()); |
||||||
|
} |
||||||
|
|
||||||
|
/* Implicit construction is not allowed */ |
||||||
|
CORRADE_VERIFY(!(std::is_convertible<NoCreateT, CommandBuffer>::value)); |
||||||
|
} |
||||||
|
|
||||||
|
void CommandBufferTest::constructCopy() { |
||||||
|
CORRADE_VERIFY(!(std::is_constructible<CommandBuffer, const CommandBuffer&>{})); |
||||||
|
CORRADE_VERIFY(!(std::is_assignable<CommandBuffer, const CommandBuffer&>{})); |
||||||
|
} |
||||||
|
|
||||||
|
}}}} |
||||||
|
|
||||||
|
CORRADE_TEST_MAIN(Magnum::Vk::Test::CommandBufferTest) |
||||||
@ -0,0 +1,110 @@ |
|||||||
|
/*
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||||
|
2020 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a |
||||||
|
copy of this software and associated documentation files (the "Software"), |
||||||
|
to deal in the Software without restriction, including without limitation |
||||||
|
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||||
|
and/or sell copies of the Software, and to permit persons to whom the |
||||||
|
Software is furnished to do so, subject to the following conditions: |
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included |
||||||
|
in all copies or substantial portions of the Software. |
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||||
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||||
|
DEALINGS IN THE SOFTWARE. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "Magnum/Vk/CommandBuffer.h" |
||||||
|
#include "Magnum/Vk/CommandPool.h" |
||||||
|
#include "Magnum/Vk/DeviceProperties.h" |
||||||
|
#include "Magnum/Vk/Handle.h" |
||||||
|
#include "Magnum/Vk/Result.h" |
||||||
|
#include "Magnum/Vk/VulkanTester.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Vk { namespace Test { namespace { |
||||||
|
|
||||||
|
struct CommandBufferVkTest: VulkanTester { |
||||||
|
explicit CommandBufferVkTest(); |
||||||
|
|
||||||
|
void construct(); |
||||||
|
void constructMove(); |
||||||
|
void wrap(); |
||||||
|
}; |
||||||
|
|
||||||
|
CommandBufferVkTest::CommandBufferVkTest() { |
||||||
|
addTests({&CommandBufferVkTest::construct, |
||||||
|
&CommandBufferVkTest::constructMove, |
||||||
|
&CommandBufferVkTest::wrap}); |
||||||
|
} |
||||||
|
|
||||||
|
void CommandBufferVkTest::construct() { |
||||||
|
CommandPool pool{device(), CommandPoolCreateInfo{ |
||||||
|
deviceProperties().pickQueueFamily(QueueFlag::Graphics)}}; |
||||||
|
|
||||||
|
{ |
||||||
|
CommandBuffer buffer = pool.allocate(); |
||||||
|
CORRADE_VERIFY(buffer.handle()); |
||||||
|
CORRADE_COMPARE(buffer.handleFlags(), HandleFlag::DestroyOnDestruction); |
||||||
|
} |
||||||
|
|
||||||
|
/* Shouldn't crash or anything */ |
||||||
|
CORRADE_VERIFY(true); |
||||||
|
} |
||||||
|
|
||||||
|
void CommandBufferVkTest::constructMove() { |
||||||
|
CommandPool pool{device(), CommandPoolCreateInfo{ |
||||||
|
deviceProperties().pickQueueFamily(QueueFlag::Graphics)}}; |
||||||
|
|
||||||
|
CommandBuffer a = pool.allocate(); |
||||||
|
VkCommandBuffer handle = a.handle(); |
||||||
|
|
||||||
|
CommandBuffer b = std::move(a); |
||||||
|
CORRADE_VERIFY(!a.handle()); |
||||||
|
CORRADE_COMPARE(b.handle(), handle); |
||||||
|
CORRADE_COMPARE(b.handleFlags(), HandleFlag::DestroyOnDestruction); |
||||||
|
|
||||||
|
CommandBuffer c{NoCreate}; |
||||||
|
c = std::move(b); |
||||||
|
CORRADE_VERIFY(!b.handle()); |
||||||
|
CORRADE_COMPARE(b.handleFlags(), HandleFlags{}); |
||||||
|
CORRADE_COMPARE(c.handle(), handle); |
||||||
|
CORRADE_COMPARE(c.handleFlags(), HandleFlag::DestroyOnDestruction); |
||||||
|
|
||||||
|
CORRADE_VERIFY(std::is_nothrow_move_constructible<CommandBuffer>::value); |
||||||
|
CORRADE_VERIFY(std::is_nothrow_move_assignable<CommandBuffer>::value); |
||||||
|
} |
||||||
|
|
||||||
|
void CommandBufferVkTest::wrap() { |
||||||
|
CommandPool pool{device(), CommandPoolCreateInfo{ |
||||||
|
deviceProperties().pickQueueFamily(QueueFlag::Graphics)}}; |
||||||
|
|
||||||
|
VkCommandBuffer buffer{}; |
||||||
|
VkCommandBufferAllocateInfo info{}; |
||||||
|
info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
||||||
|
info.commandPool = pool; |
||||||
|
info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
||||||
|
info.commandBufferCount = 1; |
||||||
|
CORRADE_COMPARE(Result(device()->AllocateCommandBuffers(device(), &info, &buffer)), Result::Success); |
||||||
|
CORRADE_VERIFY(buffer); |
||||||
|
|
||||||
|
auto wrapped = CommandBuffer::wrap(device(), pool, buffer, HandleFlag::DestroyOnDestruction); |
||||||
|
CORRADE_COMPARE(wrapped.handle(), buffer); |
||||||
|
|
||||||
|
/* Release the handle again, destroy by hand */ |
||||||
|
CORRADE_COMPARE(wrapped.release(), buffer); |
||||||
|
CORRADE_VERIFY(!wrapped.handle()); |
||||||
|
device()->FreeCommandBuffers(device(), pool, 1, &buffer); |
||||||
|
} |
||||||
|
|
||||||
|
}}}} |
||||||
|
|
||||||
|
CORRADE_TEST_MAIN(Magnum::Vk::Test::CommandBufferVkTest) |
||||||
Loading…
Reference in new issue