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
This commit is contained in:
Lynix 2016-05-14 14:46:15 +02:00
parent ef66c09719
commit a8fa5e1d12
6 changed files with 133 additions and 112 deletions

View File

@ -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 <Nazara/Prerequesites.hpp>
#include <Nazara/Vulkan/Config.hpp>
#include <Nazara/Vulkan/VkDevice.hpp>
#include <vulkan/vulkan.h>
namespace Nz
{
namespace Vk
{
template<typename C, typename VkType, typename CreateInfo>
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 <Nazara/Vulkan/VkDeviceObject.inl>
#endif // NAZARA_VULKAN_VKDEVICEOBJECT_HPP

View File

@ -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 <Nazara/Vulkan/VkCommandPool.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Vulkan/VkDevice.hpp>
#include <Nazara/Vulkan/Debug.hpp>
namespace Nz
{
namespace Vk
{
template<typename C, typename VkType, typename CreateInfo>
inline DeviceObject<C, VkType, CreateInfo>::DeviceObject(Device& device) :
m_device(device),
m_handle(VK_NULL_HANDLE)
{
}
template<typename C, typename VkType, typename CreateInfo>
inline DeviceObject<C, VkType, CreateInfo>::~DeviceObject()
{
Destroy();
}
template<typename C, typename VkType, typename CreateInfo>
inline bool DeviceObject<C, VkType, CreateInfo>::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<typename C, typename VkType, typename CreateInfo>
inline void DeviceObject<C, VkType, CreateInfo>::Destroy()
{
if (m_handle != VK_NULL_HANDLE)
C::DestroyHelper(m_device, m_handle, (m_allocator.pfnAllocation) ? &m_allocator : nullptr);
}
template<typename C, typename VkType, typename CreateInfo>
inline VkResult DeviceObject<C, VkType, CreateInfo>::GetLastErrorCode() const
{
return m_lastErrorCode;
}
template<typename C, typename VkType, typename CreateInfo>
inline DeviceObject<C, VkType, CreateInfo>::operator VkType()
{
return m_handle;
}
}
}
#include <Nazara/Vulkan/DebugOff.hpp>

View File

@ -9,39 +9,32 @@
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <Nazara/Vulkan/Config.hpp> #include <Nazara/Vulkan/Config.hpp>
#include <Nazara/Vulkan/VkLoader.hpp> #include <Nazara/Vulkan/VkDeviceObject.hpp>
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
namespace Nz namespace Nz
{ {
namespace Vk namespace Vk
{ {
class Device; class Semaphore : public DeviceObject<Semaphore, VkSemaphore, VkSemaphoreCreateInfo>
class NAZARA_VULKAN_API Semaphore
{ {
friend DeviceObject;
public: public:
inline Semaphore(Device& instance); inline Semaphore(Device& instance);
Semaphore(const Semaphore&) = delete; Semaphore(const Semaphore&) = delete;
Semaphore(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 bool Create(VkSemaphoreCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr);
inline void Destroy();
inline VkResult GetLastErrorCode() const;
Semaphore& operator=(const Semaphore&) = delete; Semaphore& operator=(const Semaphore&) = delete;
Semaphore& operator=(Semaphore&&) = delete; Semaphore& operator=(Semaphore&&) = delete;
inline operator VkSemaphore();
private: private:
Device& m_device; static VkResult CreateHelper(Device& device, const VkSemaphoreCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSemaphore* handle);
VkAllocationCallbacks m_allocator; static void DestroyHelper(Device& device, VkSemaphore handle, const VkAllocationCallbacks* allocator);
VkSemaphore m_semaphore;
VkResult m_lastErrorCode;
}; };
} }
} }

View File

@ -3,8 +3,6 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Vulkan/VkSemaphore.hpp> #include <Nazara/Vulkan/VkSemaphore.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Vulkan/VkDevice.hpp>
#include <Nazara/Vulkan/Debug.hpp> #include <Nazara/Vulkan/Debug.hpp>
namespace Nz namespace Nz
@ -12,34 +10,10 @@ namespace Nz
namespace Vk namespace Vk
{ {
inline Semaphore::Semaphore(Device& device) : inline Semaphore::Semaphore(Device& device) :
m_device(device), DeviceObject(device)
m_semaphore(VK_NULL_HANDLE)
{ {
} }
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) inline bool Semaphore::Create(VkSemaphoreCreateFlags flags, const VkAllocationCallbacks* allocator)
{ {
VkSemaphoreCreateInfo createInfo = VkSemaphoreCreateInfo createInfo =
@ -52,20 +26,14 @@ namespace Nz
return Create(createInfo, allocator); 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) return device.vkCreateSemaphore(device, createInfo, allocator, handle);
m_device.vkDestroySemaphore(m_device, m_semaphore, (m_allocator.pfnAllocation) ? &m_allocator : nullptr);
} }
inline VkResult Semaphore::GetLastErrorCode() const void Semaphore::DestroyHelper(Device& device, VkSemaphore handle, const VkAllocationCallbacks* allocator)
{ {
return m_lastErrorCode; return device.vkDestroySemaphore(device, handle, allocator);
}
inline Semaphore::operator VkSemaphore()
{
return m_semaphore;
} }
} }
} }

View File

@ -9,40 +9,24 @@
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <Nazara/Vulkan/Config.hpp> #include <Nazara/Vulkan/Config.hpp>
#include <Nazara/Vulkan/VkLoader.hpp> #include <Nazara/Vulkan/VkDeviceObject.hpp>
#include <vulkan/vulkan.h>
namespace Nz namespace Nz
{ {
namespace Vk namespace Vk
{ {
class Device; class Swapchain : public DeviceObject<Swapchain, VkSwapchainKHR, VkSwapchainCreateInfoKHR>
class NAZARA_VULKAN_API Swapchain
{ {
public: public:
inline Swapchain(Device& instance); inline Swapchain(Device& instance);
Swapchain(const Swapchain&) = delete; Swapchain(const Swapchain&) = delete;
Swapchain(Swapchain&&) = delete; Swapchain(Swapchain&&) = delete;
inline ~Swapchain(); ~Swapchain() = default;
bool Create(const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator = nullptr);
inline void Destroy();
inline bool IsSupported() const; inline bool IsSupported() const;
inline VkResult GetLastErrorCode() const;
Swapchain& operator=(const Swapchain&) = delete; Swapchain& operator=(const Swapchain&) = delete;
Swapchain& operator=(Swapchain&&) = delete; Swapchain& operator=(Swapchain&&) = delete;
inline operator VkSwapchainKHR();
private:
Device& m_device;
VkAllocationCallbacks m_allocator;
VkSwapchainKHR m_swapchain;
VkResult m_lastErrorCode;
}; };
} }
} }

View File

@ -12,55 +12,15 @@ namespace Nz
namespace Vk namespace Vk
{ {
inline Swapchain::Swapchain(Device& device) : inline Swapchain::Swapchain(Device& device) :
m_device(device), DeviceObject(device)
m_swapchain(VK_NULL_HANDLE)
{ {
} }
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 inline bool Swapchain::IsSupported() const
{ {
if (!m_device.IsExtensionLoaded("VK_KHR_swapchain")) if (!m_device.IsExtensionLoaded("VK_KHR_swapchain"))
return false; return false;
} }
inline Swapchain::operator VkSwapchainKHR()
{
return m_swapchain;
}
} }
} }