From a8fa5e1d124ff0e4cb84df74c8e6a76cc3e3961e Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 14 May 2016 14:46:15 +0200 Subject: [PATCH] Vulkan: Use a generic class helper for all device objects This greatly reduce the amount of code required for wrappers around Vulkan Objects Former-commit-id: 885d0b39197ba41fd856c45571dbf06d8ae27d8c --- include/Nazara/Vulkan/VkDeviceObject.hpp | 49 +++++++++++++++++ include/Nazara/Vulkan/VkDeviceObject.inl | 67 ++++++++++++++++++++++++ include/Nazara/Vulkan/VkSemaphore.hpp | 23 +++----- include/Nazara/Vulkan/VkSemaphore.inl | 42 ++------------- include/Nazara/Vulkan/VkSwapchain.hpp | 22 ++------ include/Nazara/Vulkan/VkSwapchain.inl | 42 +-------------- 6 files changed, 133 insertions(+), 112 deletions(-) create mode 100644 include/Nazara/Vulkan/VkDeviceObject.hpp create mode 100644 include/Nazara/Vulkan/VkDeviceObject.inl diff --git a/include/Nazara/Vulkan/VkDeviceObject.hpp b/include/Nazara/Vulkan/VkDeviceObject.hpp new file mode 100644 index 000000000..efad29c30 --- /dev/null +++ b/include/Nazara/Vulkan/VkDeviceObject.hpp @@ -0,0 +1,49 @@ +// 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_VKDEVICEOBJECT_HPP +#define NAZARA_VULKAN_VKDEVICEOBJECT_HPP + +#include +#include +#include +#include + +namespace Nz +{ + namespace Vk + { + template + class DeviceObject + { + public: + inline DeviceObject(Device& instance); + DeviceObject(const DeviceObject&) = delete; + DeviceObject(DeviceObject&&) = delete; + inline ~DeviceObject(); + + inline bool Create(const CreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr); + inline void Destroy(); + + inline VkResult GetLastErrorCode() const; + + DeviceObject& operator=(const DeviceObject&) = delete; + DeviceObject& operator=(DeviceObject&&) = delete; + + inline operator VkType(); + + protected: + Device& m_device; + VkAllocationCallbacks m_allocator; + VkType m_handle; + VkResult m_lastErrorCode; + }; + } +} + +#include + +#endif // NAZARA_VULKAN_VKDEVICEOBJECT_HPP diff --git a/include/Nazara/Vulkan/VkDeviceObject.inl b/include/Nazara/Vulkan/VkDeviceObject.inl new file mode 100644 index 000000000..7aa313ca9 --- /dev/null +++ b/include/Nazara/Vulkan/VkDeviceObject.inl @@ -0,0 +1,67 @@ +// 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 + { + template + inline DeviceObject::DeviceObject(Device& device) : + m_device(device), + m_handle(VK_NULL_HANDLE) + { + } + + template + inline DeviceObject::~DeviceObject() + { + Destroy(); + } + + template + inline bool DeviceObject::Create(const CreateInfo& createInfo, const VkAllocationCallbacks* allocator) + { + m_lastErrorCode = C::CreateHelper(m_device, &createInfo, allocator, &m_handle); + if (m_lastErrorCode != VkResult::VK_SUCCESS) + { + NazaraError("Failed to create Vulkan object"); + return false; + } + + // Store the allocator to access them when needed + if (allocator) + m_allocator = *allocator; + else + m_allocator.pfnAllocation = nullptr; + + return true; + } + + template + inline void DeviceObject::Destroy() + { + if (m_handle != VK_NULL_HANDLE) + C::DestroyHelper(m_device, m_handle, (m_allocator.pfnAllocation) ? &m_allocator : nullptr); + } + + template + inline VkResult DeviceObject::GetLastErrorCode() const + { + return m_lastErrorCode; + } + + template + inline DeviceObject::operator VkType() + { + return m_handle; + } + } +} + +#include diff --git a/include/Nazara/Vulkan/VkSemaphore.hpp b/include/Nazara/Vulkan/VkSemaphore.hpp index 0cf871d5d..b7983ee0b 100644 --- a/include/Nazara/Vulkan/VkSemaphore.hpp +++ b/include/Nazara/Vulkan/VkSemaphore.hpp @@ -9,39 +9,32 @@ #include #include -#include +#include #include namespace Nz { namespace Vk { - class Device; - - class NAZARA_VULKAN_API Semaphore + class Semaphore : public DeviceObject { + friend DeviceObject; + public: inline Semaphore(Device& instance); Semaphore(const Semaphore&) = delete; Semaphore(Semaphore&&) = delete; - inline ~Semaphore(); + ~Semaphore() = default; - inline bool Create(const VkSemaphoreCreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr); + using DeviceObject::Create; inline bool Create(VkSemaphoreCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); - inline void Destroy(); - - inline VkResult GetLastErrorCode() const; Semaphore& operator=(const Semaphore&) = delete; Semaphore& operator=(Semaphore&&) = delete; - inline operator VkSemaphore(); - private: - Device& m_device; - VkAllocationCallbacks m_allocator; - VkSemaphore m_semaphore; - VkResult m_lastErrorCode; + static VkResult CreateHelper(Device& device, const VkSemaphoreCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSemaphore* handle); + static void DestroyHelper(Device& device, VkSemaphore handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/Vulkan/VkSemaphore.inl b/include/Nazara/Vulkan/VkSemaphore.inl index 0fb61c426..fd2bf3427 100644 --- a/include/Nazara/Vulkan/VkSemaphore.inl +++ b/include/Nazara/Vulkan/VkSemaphore.inl @@ -3,8 +3,6 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include -#include -#include #include namespace Nz @@ -12,34 +10,10 @@ namespace Nz namespace Vk { inline Semaphore::Semaphore(Device& device) : - m_device(device), - m_semaphore(VK_NULL_HANDLE) + DeviceObject(device) { } - inline Semaphore::~Semaphore() - { - Destroy(); - } - - inline bool Semaphore::Create(const VkSemaphoreCreateInfo& createInfo, const VkAllocationCallbacks* allocator) - { - m_lastErrorCode = m_device.vkCreateSemaphore(m_device, &createInfo, allocator, &m_semaphore); - if (m_lastErrorCode != VkResult::VK_SUCCESS) - { - NazaraError("Failed to create Vulkan semaphore"); - return false; - } - - // Store the allocator to access them when needed - if (allocator) - m_allocator = *allocator; - else - m_allocator.pfnAllocation = nullptr; - - return true; - } - inline bool Semaphore::Create(VkSemaphoreCreateFlags flags, const VkAllocationCallbacks* allocator) { VkSemaphoreCreateInfo createInfo = @@ -52,20 +26,14 @@ namespace Nz return Create(createInfo, allocator); } - inline void Semaphore::Destroy() + VkResult Semaphore::CreateHelper(Device& device, const VkSemaphoreCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSemaphore* handle) { - if (m_semaphore != VK_NULL_HANDLE) - m_device.vkDestroySemaphore(m_device, m_semaphore, (m_allocator.pfnAllocation) ? &m_allocator : nullptr); + return device.vkCreateSemaphore(device, createInfo, allocator, handle); } - inline VkResult Semaphore::GetLastErrorCode() const + void Semaphore::DestroyHelper(Device& device, VkSemaphore handle, const VkAllocationCallbacks* allocator) { - return m_lastErrorCode; - } - - inline Semaphore::operator VkSemaphore() - { - return m_semaphore; + return device.vkDestroySemaphore(device, handle, allocator); } } } diff --git a/include/Nazara/Vulkan/VkSwapchain.hpp b/include/Nazara/Vulkan/VkSwapchain.hpp index a0f23f8ac..71ab1a853 100644 --- a/include/Nazara/Vulkan/VkSwapchain.hpp +++ b/include/Nazara/Vulkan/VkSwapchain.hpp @@ -9,40 +9,24 @@ #include #include -#include -#include +#include namespace Nz { namespace Vk { - class Device; - - class NAZARA_VULKAN_API Swapchain + class Swapchain : public DeviceObject { public: inline Swapchain(Device& instance); Swapchain(const Swapchain&) = delete; Swapchain(Swapchain&&) = delete; - inline ~Swapchain(); - - bool Create(const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator = nullptr); - inline void Destroy(); + ~Swapchain() = default; inline bool IsSupported() const; - inline VkResult GetLastErrorCode() const; - Swapchain& operator=(const Swapchain&) = delete; Swapchain& operator=(Swapchain&&) = delete; - - inline operator VkSwapchainKHR(); - - private: - Device& m_device; - VkAllocationCallbacks m_allocator; - VkSwapchainKHR m_swapchain; - VkResult m_lastErrorCode; }; } } diff --git a/include/Nazara/Vulkan/VkSwapchain.inl b/include/Nazara/Vulkan/VkSwapchain.inl index 7ed08b902..8c05c0284 100644 --- a/include/Nazara/Vulkan/VkSwapchain.inl +++ b/include/Nazara/Vulkan/VkSwapchain.inl @@ -12,55 +12,15 @@ namespace Nz namespace Vk { inline Swapchain::Swapchain(Device& device) : - m_device(device), - m_swapchain(VK_NULL_HANDLE) + DeviceObject(device) { } - inline Swapchain::~Swapchain() - { - Destroy(); - } - - inline bool Swapchain::Create(const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator) - { - m_lastErrorCode = m_device.vkCreateSwapchainKHR(m_device, &createInfo, allocator, &m_swapchain); - if (m_lastErrorCode != VkResult::VK_SUCCESS) - { - NazaraError("Failed to create Vulkan swapchain"); - return false; - } - - // Store the allocator to access them when needed - if (allocator) - m_allocator = *allocator; - else - m_allocator.pfnAllocation = nullptr; - - return true; - } - - inline void Swapchain::Destroy() - { - if (m_swapchain != VK_NULL_HANDLE) - m_device.vkDestroySwapchainKHR(m_device, m_swapchain, (m_allocator.pfnAllocation) ? &m_allocator : nullptr); - } - - inline VkResult Swapchain::GetLastErrorCode() const - { - return m_lastErrorCode; - } - inline bool Swapchain::IsSupported() const { if (!m_device.IsExtensionLoaded("VK_KHR_swapchain")) return false; } - - inline Swapchain::operator VkSwapchainKHR() - { - return m_swapchain; - } } }