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:
parent
ef66c09719
commit
a8fa5e1d12
|
|
@ -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
|
||||
|
|
@ -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>
|
||||
|
|
@ -9,39 +9,32 @@
|
|||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Vulkan/Config.hpp>
|
||||
#include <Nazara/Vulkan/VkLoader.hpp>
|
||||
#include <Nazara/Vulkan/VkDeviceObject.hpp>
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace Vk
|
||||
{
|
||||
class Device;
|
||||
|
||||
class NAZARA_VULKAN_API Semaphore
|
||||
class Semaphore : public DeviceObject<Semaphore, VkSemaphore, VkSemaphoreCreateInfo>
|
||||
{
|
||||
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);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,6 @@
|
|||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Vulkan/VkSemaphore.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Vulkan/VkDevice.hpp>
|
||||
#include <Nazara/Vulkan/Debug.hpp>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,40 +9,24 @@
|
|||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Vulkan/Config.hpp>
|
||||
#include <Nazara/Vulkan/VkLoader.hpp>
|
||||
#include <vulkan/vulkan.h>
|
||||
#include <Nazara/Vulkan/VkDeviceObject.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace Vk
|
||||
{
|
||||
class Device;
|
||||
|
||||
class NAZARA_VULKAN_API Swapchain
|
||||
class Swapchain : public DeviceObject<Swapchain, VkSwapchainKHR, VkSwapchainCreateInfoKHR>
|
||||
{
|
||||
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;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue