From bb945d773e5d45a6ecadce6af3a0346de5561429 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 15 May 2016 00:08:05 +0200 Subject: [PATCH] Vulkan: Add support for CommandPool and CommandBuffer Former-commit-id: 0ed1e8d09300577be3d4c52b94d6a8f594178a8b --- include/Nazara/Vulkan/VkCommandBuffer.hpp | 50 +++++++++++++++++++++ include/Nazara/Vulkan/VkCommandBuffer.inl | 51 ++++++++++++++++++++++ include/Nazara/Vulkan/VkCommandPool.hpp | 53 +++++++++++++++++++++++ include/Nazara/Vulkan/VkCommandPool.inl | 53 +++++++++++++++++++++++ include/Nazara/Vulkan/VkDeviceObject.hpp | 2 + include/Nazara/Vulkan/VkDeviceObject.inl | 12 +++++ include/Nazara/Vulkan/VkSurface.hpp | 2 +- src/Nazara/Vulkan/VkCommandPool.cpp | 53 +++++++++++++++++++++++ 8 files changed, 275 insertions(+), 1 deletion(-) create mode 100644 include/Nazara/Vulkan/VkCommandBuffer.hpp create mode 100644 include/Nazara/Vulkan/VkCommandBuffer.inl create mode 100644 include/Nazara/Vulkan/VkCommandPool.hpp create mode 100644 include/Nazara/Vulkan/VkCommandPool.inl create mode 100644 src/Nazara/Vulkan/VkCommandPool.cpp diff --git a/include/Nazara/Vulkan/VkCommandBuffer.hpp b/include/Nazara/Vulkan/VkCommandBuffer.hpp new file mode 100644 index 000000000..0399d4cf7 --- /dev/null +++ b/include/Nazara/Vulkan/VkCommandBuffer.hpp @@ -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 +#include +#include + +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 + +#endif // NAZARA_VULKAN_VKCOMMANDBUFFER_HPP diff --git a/include/Nazara/Vulkan/VkCommandBuffer.inl b/include/Nazara/Vulkan/VkCommandBuffer.inl new file mode 100644 index 000000000..ff4eaede4 --- /dev/null +++ b/include/Nazara/Vulkan/VkCommandBuffer.inl @@ -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 +#include +#include +#include + +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 diff --git a/include/Nazara/Vulkan/VkCommandPool.hpp b/include/Nazara/Vulkan/VkCommandPool.hpp new file mode 100644 index 000000000..8b99513e3 --- /dev/null +++ b/include/Nazara/Vulkan/VkCommandPool.hpp @@ -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 +#include +#include + +namespace Nz +{ + namespace Vk + { + class CommandBuffer; + class CommandPool; + + using CommandPoolHandle = ObjectHandle; + + class CommandPool : public DeviceObject, public HandledObject + { + friend DeviceObject; + + public: + inline CommandPool(Device& instance); + CommandPool(const CommandPool&) = delete; + CommandPool(CommandPool&&) = default; + ~CommandPool() = default; + + CommandBuffer AllocateCommandBuffer(VkCommandBufferLevel level); + std::vector 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 + +#endif // NAZARA_VULKAN_VKCOMMANDPOOL_HPP diff --git a/include/Nazara/Vulkan/VkCommandPool.inl b/include/Nazara/Vulkan/VkCommandPool.inl new file mode 100644 index 000000000..f7aef6978 --- /dev/null +++ b/include/Nazara/Vulkan/VkCommandPool.inl @@ -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 +#include +#include +#include + +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 diff --git a/include/Nazara/Vulkan/VkDeviceObject.hpp b/include/Nazara/Vulkan/VkDeviceObject.hpp index 999762098..dd9da38c9 100644 --- a/include/Nazara/Vulkan/VkDeviceObject.hpp +++ b/include/Nazara/Vulkan/VkDeviceObject.hpp @@ -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; diff --git a/include/Nazara/Vulkan/VkDeviceObject.inl b/include/Nazara/Vulkan/VkDeviceObject.inl index 58645983e..a3136156c 100644 --- a/include/Nazara/Vulkan/VkDeviceObject.inl +++ b/include/Nazara/Vulkan/VkDeviceObject.inl @@ -60,6 +60,18 @@ namespace Nz C::DestroyHelper(m_device, m_handle, (m_allocator.pfnAllocation) ? &m_allocator : nullptr); } + template + inline Device& DeviceObject::GetDevice() + { + return m_device; + } + + template + inline const Device& DeviceObject::GetDevice() const + { + return m_device; + } + template inline VkResult DeviceObject::GetLastErrorCode() const { diff --git a/include/Nazara/Vulkan/VkSurface.hpp b/include/Nazara/Vulkan/VkSurface.hpp index a27f3d357..7b7fbcdfd 100644 --- a/include/Nazara/Vulkan/VkSurface.hpp +++ b/include/Nazara/Vulkan/VkSurface.hpp @@ -18,7 +18,7 @@ namespace Nz { class Instance; - class NAZARA_VULKAN_API Surface + class Surface { public: inline Surface(Instance& instance); diff --git a/src/Nazara/Vulkan/VkCommandPool.cpp b/src/Nazara/Vulkan/VkCommandPool.cpp new file mode 100644 index 000000000..3ee53ab6f --- /dev/null +++ b/src/Nazara/Vulkan/VkCommandPool.cpp @@ -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 +#include +#include + +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 CommandPool::AllocateCommandBuffers(UInt32 commandBufferCount, VkCommandBufferLevel level) + { + VkCommandBufferAllocateInfo createInfo = + { + VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + nullptr, + m_handle, + level, + 1U + }; + + std::vector handles(commandBufferCount, VK_NULL_HANDLE); + m_lastErrorCode = m_device.vkAllocateCommandBuffers(m_device, &createInfo, handles.data()); + if (m_lastErrorCode != VkResult::VK_SUCCESS) + return std::vector(); + + std::vector commandBuffers; + for (UInt32 i = 0; i < commandBufferCount; ++i) + commandBuffers.emplace_back(CommandBuffer(*this, handles[i])); + + return commandBuffers; + } + } +}