9 changed files with 307 additions and 75 deletions
@ -0,0 +1,70 @@ |
|||||
|
// Copyright 2022 Simon Boyé
|
||||
|
|
||||
|
#include <vk/CommandBuffer.h> |
||||
|
#include <vk/Context.h> |
||||
|
|
||||
|
#include <cassert> |
||||
|
|
||||
|
|
||||
|
namespace vk { |
||||
|
|
||||
|
|
||||
|
CommandBuffer::CommandBuffer() noexcept { |
||||
|
} |
||||
|
|
||||
|
CommandBuffer::CommandBuffer(Context& context, VkCommandPool command_pool, VkCommandBufferLevel level) |
||||
|
: m_context(&context) |
||||
|
, m_command_pool(command_pool) |
||||
|
{ |
||||
|
assert(m_context); |
||||
|
assert(m_command_pool != VK_NULL_HANDLE); |
||||
|
|
||||
|
VkCommandBufferAllocateInfo allocate_info { |
||||
|
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, |
||||
|
.commandPool = m_command_pool, |
||||
|
.level = level, |
||||
|
.commandBufferCount = 1, |
||||
|
}; |
||||
|
if(vkAllocateCommandBuffers( |
||||
|
context.device(), |
||||
|
&allocate_info, |
||||
|
&m_command_buffer |
||||
|
) != VK_SUCCESS) |
||||
|
throw std::runtime_error("failed to allocate command pool"); |
||||
|
} |
||||
|
|
||||
|
CommandBuffer::CommandBuffer(CommandBuffer&& other) noexcept |
||||
|
{ |
||||
|
swap(*this, other); |
||||
|
} |
||||
|
|
||||
|
CommandBuffer::~CommandBuffer() noexcept { |
||||
|
if(!is_null()) |
||||
|
destroy(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
CommandBuffer& CommandBuffer::operator=(CommandBuffer&& other) noexcept { |
||||
|
swap(*this, other); |
||||
|
if(other) |
||||
|
other.destroy(); |
||||
|
return *this; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void CommandBuffer::destroy() noexcept { |
||||
|
assert(!is_null()); |
||||
|
assert(m_context); |
||||
|
|
||||
|
vkFreeCommandBuffers( |
||||
|
m_context->device(), |
||||
|
m_command_pool, |
||||
|
1, |
||||
|
&m_command_buffer |
||||
|
); |
||||
|
|
||||
|
m_command_buffer = nullptr; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,71 @@ |
|||||
|
// Copyright 2022 Simon Boyé
|
||||
|
#pragma once |
||||
|
|
||||
|
#include <vk/forward.h> |
||||
|
|
||||
|
#include <vulkan/vulkan.h> |
||||
|
|
||||
|
|
||||
|
namespace vk { |
||||
|
|
||||
|
|
||||
|
class CommandBuffer { |
||||
|
public: |
||||
|
CommandBuffer() noexcept; |
||||
|
CommandBuffer(Context& context, VkCommandPool command_pool, VkCommandBufferLevel level=VK_COMMAND_BUFFER_LEVEL_PRIMARY); |
||||
|
CommandBuffer(const CommandBuffer&) = delete; |
||||
|
CommandBuffer(CommandBuffer&& other) noexcept; |
||||
|
~CommandBuffer() noexcept; |
||||
|
|
||||
|
CommandBuffer& operator=(const CommandBuffer&) = delete; |
||||
|
CommandBuffer& operator=(CommandBuffer&& other) noexcept; |
||||
|
|
||||
|
explicit inline operator bool() const noexcept { |
||||
|
return !is_null(); |
||||
|
} |
||||
|
|
||||
|
inline bool is_null() const noexcept { |
||||
|
return m_command_buffer == VK_NULL_HANDLE; |
||||
|
} |
||||
|
|
||||
|
inline const Context* context() const noexcept { |
||||
|
return m_context; |
||||
|
} |
||||
|
|
||||
|
inline Context* context() noexcept { |
||||
|
return m_context; |
||||
|
} |
||||
|
|
||||
|
inline const VkCommandPool command_pool() const noexcept { |
||||
|
return m_command_pool; |
||||
|
} |
||||
|
|
||||
|
inline VkCommandPool command_pool() noexcept { |
||||
|
return m_command_pool; |
||||
|
} |
||||
|
|
||||
|
inline operator VkCommandBuffer() noexcept { |
||||
|
return m_command_buffer; |
||||
|
} |
||||
|
|
||||
|
inline VkCommandBuffer command_buffer() noexcept { |
||||
|
return m_command_buffer; |
||||
|
} |
||||
|
|
||||
|
friend inline void swap(CommandBuffer& command_buffer_0, CommandBuffer& command_buffer_1) noexcept { |
||||
|
using std::swap; |
||||
|
swap(command_buffer_0.m_context, command_buffer_1.m_context); |
||||
|
swap(command_buffer_0.m_command_pool, command_buffer_1.m_command_pool); |
||||
|
swap(command_buffer_0.m_command_buffer, command_buffer_1.m_command_buffer); |
||||
|
} |
||||
|
|
||||
|
void destroy() noexcept; |
||||
|
|
||||
|
private: |
||||
|
Context* m_context = nullptr; |
||||
|
VkCommandPool m_command_pool = VK_NULL_HANDLE; |
||||
|
VkCommandBuffer m_command_buffer = VK_NULL_HANDLE; |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,67 @@ |
|||||
|
// Copyright 2022 Simon Boyé
|
||||
|
|
||||
|
#include <vk/CommandPool.h> |
||||
|
#include <vk/Context.h> |
||||
|
|
||||
|
#include <cassert> |
||||
|
|
||||
|
|
||||
|
namespace vk { |
||||
|
|
||||
|
|
||||
|
CommandPool::CommandPool() noexcept { |
||||
|
} |
||||
|
|
||||
|
CommandPool::CommandPool(Context& context, uint32_t queue_family, VkCommandPoolCreateFlags flags) |
||||
|
: m_context(&context) |
||||
|
{ |
||||
|
assert(m_context); |
||||
|
|
||||
|
VkCommandPoolCreateInfo create_info { |
||||
|
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, |
||||
|
.flags = flags, |
||||
|
.queueFamilyIndex = queue_family, |
||||
|
}; |
||||
|
if(vkCreateCommandPool( |
||||
|
context.device(), |
||||
|
&create_info, |
||||
|
nullptr, |
||||
|
&m_command_pool |
||||
|
) != VK_SUCCESS) |
||||
|
throw std::runtime_error("failed to create command pool"); |
||||
|
} |
||||
|
|
||||
|
CommandPool::CommandPool(CommandPool&& other) noexcept |
||||
|
{ |
||||
|
swap(*this, other); |
||||
|
} |
||||
|
|
||||
|
CommandPool::~CommandPool() noexcept { |
||||
|
if(!is_null()) |
||||
|
destroy(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
CommandPool& CommandPool::operator=(CommandPool&& other) noexcept { |
||||
|
swap(*this, other); |
||||
|
if(other) |
||||
|
other.destroy(); |
||||
|
return *this; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void CommandPool::destroy() noexcept { |
||||
|
assert(!is_null()); |
||||
|
assert(m_context); |
||||
|
|
||||
|
vkDestroyCommandPool( |
||||
|
m_context->device(), |
||||
|
m_command_pool, |
||||
|
nullptr |
||||
|
); |
||||
|
|
||||
|
m_command_pool = nullptr; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
// Copyright 2022 Simon Boyé
|
||||
|
#pragma once |
||||
|
|
||||
|
#include <vk/forward.h> |
||||
|
|
||||
|
#include <vulkan/vulkan.h> |
||||
|
|
||||
|
|
||||
|
namespace vk { |
||||
|
|
||||
|
|
||||
|
class CommandPool { |
||||
|
public: |
||||
|
CommandPool() noexcept; |
||||
|
CommandPool(Context& context, uint32_t queue_family, VkCommandPoolCreateFlags flags=0); |
||||
|
CommandPool(const CommandPool&) = delete; |
||||
|
CommandPool(CommandPool&& other) noexcept; |
||||
|
~CommandPool() noexcept; |
||||
|
|
||||
|
CommandPool& operator=(const CommandPool&) = delete; |
||||
|
CommandPool& operator=(CommandPool&& other) noexcept; |
||||
|
|
||||
|
explicit inline operator bool() const noexcept { |
||||
|
return !is_null(); |
||||
|
} |
||||
|
|
||||
|
inline bool is_null() const noexcept { |
||||
|
return m_command_pool == VK_NULL_HANDLE; |
||||
|
} |
||||
|
|
||||
|
inline const Context* context() const noexcept { |
||||
|
return m_context; |
||||
|
} |
||||
|
|
||||
|
inline Context* context() noexcept { |
||||
|
return m_context; |
||||
|
} |
||||
|
|
||||
|
inline operator VkCommandPool() noexcept { |
||||
|
return m_command_pool; |
||||
|
} |
||||
|
|
||||
|
inline VkCommandPool command_pool() noexcept { |
||||
|
return m_command_pool; |
||||
|
} |
||||
|
|
||||
|
friend inline void swap(CommandPool& command_pool_0, CommandPool& command_pool_1) noexcept { |
||||
|
using std::swap; |
||||
|
swap(command_pool_0.m_context, command_pool_1.m_context); |
||||
|
swap(command_pool_0.m_command_pool, command_pool_1.m_command_pool); |
||||
|
} |
||||
|
|
||||
|
void destroy() noexcept; |
||||
|
|
||||
|
private: |
||||
|
Context* m_context = nullptr; |
||||
|
VkCommandPool m_command_pool = VK_NULL_HANDLE; |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue