Vulkan: Add support for CommandPool and CommandBuffer

Former-commit-id: 0ed1e8d09300577be3d4c52b94d6a8f594178a8b
This commit is contained in:
Lynix 2016-05-15 00:08:05 +02:00
parent e5528abb0f
commit bb945d773e
8 changed files with 275 additions and 1 deletions

View File

@ -0,0 +1,50 @@
// Copyright (C) 2016 Jérôme Leclercq
// This file is part of the "Nazara Engine - Vulkan"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_VULKAN_VKCOMMANDBUFFER_HPP
#define NAZARA_VULKAN_VKCOMMANDBUFFER_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Vulkan/VkCommandPool.hpp>
#include <vulkan/vulkan.h>
namespace Nz
{
namespace Vk
{
class NAZARA_VULKAN_API CommandBuffer
{
friend CommandPool;
public:
CommandBuffer(const CommandBuffer&) = delete;
CommandBuffer(CommandBuffer&& commandBuffer);
inline ~CommandBuffer();
inline void Free();
inline VkResult GetLastErrorCode() const;
CommandBuffer& operator=(const CommandBuffer&) = delete;
CommandBuffer& operator=(CommandBuffer&&) = delete;
inline operator VkCommandBuffer();
private:
inline CommandBuffer(CommandPool& pool, VkCommandBuffer handle);
CommandPoolHandle m_pool;
VkAllocationCallbacks m_allocator;
VkCommandBuffer m_handle;
VkResult m_lastErrorCode;
};
}
}
#include <Nazara/Vulkan/VkCommandBuffer.inl>
#endif // NAZARA_VULKAN_VKCOMMANDBUFFER_HPP

View File

@ -0,0 +1,51 @@
// Copyright (C) 2016 Jérôme Leclercq
// This file is part of the "Nazara Engine - Vulkan"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Vulkan/VkSurface.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Vulkan/VkInstance.hpp>
#include <Nazara/Vulkan/Debug.hpp>
namespace Nz
{
namespace Vk
{
inline CommandBuffer::CommandBuffer(CommandPool& pool, VkCommandBuffer handle) :
m_pool(&pool),
m_handle(handle)
{
}
inline CommandBuffer::CommandBuffer(CommandBuffer&& commandBuffer) :
m_pool(std::move(commandBuffer.m_pool)),
m_allocator(commandBuffer.m_allocator),
m_handle(commandBuffer.m_handle),
m_lastErrorCode(commandBuffer.m_lastErrorCode)
{
commandBuffer.m_handle = VK_NULL_HANDLE;
}
inline CommandBuffer::~CommandBuffer()
{
Free();
}
inline void CommandBuffer::Free()
{
m_pool->GetDevice().vkFreeCommandBuffers(m_pool->GetDevice(), *m_pool, 1, &m_handle);
}
inline VkResult CommandBuffer::GetLastErrorCode() const
{
return m_lastErrorCode;
}
inline CommandBuffer::operator VkCommandBuffer()
{
return m_handle;
}
}
}
#include <Nazara/Vulkan/DebugOff.hpp>

View File

@ -0,0 +1,53 @@
// Copyright (C) 2016 Jérôme Leclercq
// This file is part of the "Nazara Engine - Vulkan"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_VULKAN_VKCOMMANDPOOL_HPP
#define NAZARA_VULKAN_VKCOMMANDPOOL_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/HandledObject.hpp>
#include <Nazara/Vulkan/VkDeviceObject.hpp>
namespace Nz
{
namespace Vk
{
class CommandBuffer;
class CommandPool;
using CommandPoolHandle = ObjectHandle<CommandPool>;
class CommandPool : public DeviceObject<CommandPool, VkCommandPool, VkCommandPoolCreateInfo>, public HandledObject<CommandPool>
{
friend DeviceObject;
public:
inline CommandPool(Device& instance);
CommandPool(const CommandPool&) = delete;
CommandPool(CommandPool&&) = default;
~CommandPool() = default;
CommandBuffer AllocateCommandBuffer(VkCommandBufferLevel level);
std::vector<CommandBuffer> AllocateCommandBuffers(UInt32 commandBufferCount, VkCommandBufferLevel level);
using DeviceObject::Create;
inline bool Create(UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr);
inline bool Reset(VkCommandPoolResetFlags flags);
CommandPool& operator=(const CommandPool&) = delete;
CommandPool& operator=(CommandPool&&) = delete;
private:
static VkResult CreateHelper(Device& device, const VkCommandPoolCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkCommandPool* handle);
static void DestroyHelper(Device& device, VkCommandPool handle, const VkAllocationCallbacks* allocator);
};
}
}
#include <Nazara/Vulkan/VkCommandPool.inl>
#endif // NAZARA_VULKAN_VKCOMMANDPOOL_HPP

View File

@ -0,0 +1,53 @@
// Copyright (C) 2016 Jérôme Leclercq
// This file is part of the "Nazara Engine - Vulkan"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Vulkan/VkCommandPool.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Vulkan/VkDevice.hpp>
#include <Nazara/Vulkan/Debug.hpp>
namespace Nz
{
namespace Vk
{
inline CommandPool::CommandPool(Device& device) :
DeviceObject(device)
{
}
inline bool CommandPool::Create(UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags, const VkAllocationCallbacks* allocator)
{
VkCommandPoolCreateInfo createInfo =
{
VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
nullptr,
flags,
queueFamilyIndex
};
return Create(createInfo, allocator);
}
inline bool CommandPool::Reset(VkCommandPoolResetFlags flags)
{
m_lastErrorCode = m_device.vkResetCommandPool(m_device, m_handle, flags);
if (m_lastErrorCode != VkResult::VK_SUCCESS)
return false;
return true;
}
VkResult CommandPool::CreateHelper(Device& device, const VkCommandPoolCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkCommandPool* handle)
{
return device.vkCreateCommandPool(device, createInfo, allocator, handle);
}
void CommandPool::DestroyHelper(Device& device, VkCommandPool handle, const VkAllocationCallbacks* allocator)
{
return device.vkDestroyCommandPool(device, handle, allocator);
}
}
}
#include <Nazara/Vulkan/DebugOff.hpp>

View File

@ -27,6 +27,8 @@ namespace Nz
inline bool Create(const CreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr);
inline void Destroy();
inline Device& GetDevice();
inline const Device& GetDevice() const;
inline VkResult GetLastErrorCode() const;
DeviceObject& operator=(const DeviceObject&) = delete;

View File

@ -60,6 +60,18 @@ namespace Nz
C::DestroyHelper(m_device, m_handle, (m_allocator.pfnAllocation) ? &m_allocator : nullptr);
}
template<typename C, typename VkType, typename CreateInfo>
inline Device& DeviceObject<C, VkType, CreateInfo>::GetDevice()
{
return m_device;
}
template<typename C, typename VkType, typename CreateInfo>
inline const Device& DeviceObject<C, VkType, CreateInfo>::GetDevice() const
{
return m_device;
}
template<typename C, typename VkType, typename CreateInfo>
inline VkResult DeviceObject<C, VkType, CreateInfo>::GetLastErrorCode() const
{

View File

@ -18,7 +18,7 @@ namespace Nz
{
class Instance;
class NAZARA_VULKAN_API Surface
class Surface
{
public:
inline Surface(Instance& instance);

View File

@ -0,0 +1,53 @@
// Copyright (C) 2016 Jérôme Leclercq
// This file is part of the "Nazara Engine - Vulkan"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Vulkan/VkCommandPool.hpp>
#include <Nazara/Vulkan/VkCommandBuffer.hpp>
#include <Nazara/Vulkan/Debug.hpp>
namespace Nz
{
namespace Vk
{
CommandBuffer CommandPool::AllocateCommandBuffer(VkCommandBufferLevel level)
{
VkCommandBufferAllocateInfo createInfo =
{
VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
nullptr,
m_handle,
level,
1U
};
VkCommandBuffer handle = VK_NULL_HANDLE;
m_lastErrorCode = m_device.vkAllocateCommandBuffers(m_device, &createInfo, &handle);
return CommandBuffer(*this, handle);
}
std::vector<CommandBuffer> CommandPool::AllocateCommandBuffers(UInt32 commandBufferCount, VkCommandBufferLevel level)
{
VkCommandBufferAllocateInfo createInfo =
{
VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
nullptr,
m_handle,
level,
1U
};
std::vector<VkCommandBuffer> handles(commandBufferCount, VK_NULL_HANDLE);
m_lastErrorCode = m_device.vkAllocateCommandBuffers(m_device, &createInfo, handles.data());
if (m_lastErrorCode != VkResult::VK_SUCCESS)
return std::vector<CommandBuffer>();
std::vector<CommandBuffer> commandBuffers;
for (UInt32 i = 0; i < commandBufferCount; ++i)
commandBuffers.emplace_back(CommandBuffer(*this, handles[i]));
return commandBuffers;
}
}
}