Move Vulkan debug callback to module

This commit is contained in:
Lynix
2020-04-13 15:12:58 +02:00
parent 7447875753
commit e905c3a004
11 changed files with 366 additions and 95 deletions

View File

@@ -0,0 +1,41 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Vulkan Renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_VULKANRENDERER_VKDEBUGUTILSMESSENGEREXT_HPP
#define NAZARA_VULKANRENDERER_VKDEBUGUTILSMESSENGEREXT_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/VulkanRenderer/Wrapper/InstanceObject.hpp>
namespace Nz
{
namespace Vk
{
class DebugUtilsMessengerEXT : public InstanceObject<DebugUtilsMessengerEXT, VkDebugUtilsMessengerEXT, VkDebugUtilsMessengerCreateInfoEXT, VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT>
{
friend InstanceObject;
public:
DebugUtilsMessengerEXT() = default;
DebugUtilsMessengerEXT(const DebugUtilsMessengerEXT&) = delete;
DebugUtilsMessengerEXT(DebugUtilsMessengerEXT&&) = default;
~DebugUtilsMessengerEXT() = default;
DebugUtilsMessengerEXT& operator=(const DebugUtilsMessengerEXT&) = delete;
DebugUtilsMessengerEXT& operator=(DebugUtilsMessengerEXT&&) = delete;
static inline bool IsSupported(Instance& instance);
private:
static inline VkResult CreateHelper(Instance& instance, const VkDebugUtilsMessengerCreateInfoEXT* createInfo, const VkAllocationCallbacks* allocator, VkDebugUtilsMessengerEXT* handle);
static inline void DestroyHelper(Instance& instance, VkDebugUtilsMessengerEXT handle, const VkAllocationCallbacks* allocator);
};
}
}
#include <Nazara/VulkanRenderer/Wrapper/DebugUtilsMessengerEXT.inl>
#endif // NAZARA_VULKANRENDERER_VKDEBUGUTILSMESSENGEREXT_HPP

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Vulkan Renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/VulkanRenderer/Wrapper/DebugUtilsMessengerEXT.hpp>
#include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz
{
namespace Vk
{
inline bool DebugUtilsMessengerEXT::IsSupported(Instance& instance)
{
return instance.vkCreateDebugUtilsMessengerEXT != nullptr;
}
inline VkResult DebugUtilsMessengerEXT::CreateHelper(Instance& instance, const VkDebugUtilsMessengerCreateInfoEXT* createInfo, const VkAllocationCallbacks* allocator, VkDebugUtilsMessengerEXT* handle)
{
return instance.vkCreateDebugUtilsMessengerEXT(instance, createInfo, allocator, handle);
}
inline void DebugUtilsMessengerEXT::DestroyHelper(Instance& instance, VkDebugUtilsMessengerEXT handle, const VkAllocationCallbacks* allocator)
{
return instance.vkDestroyDebugUtilsMessengerEXT(instance, handle, allocator);
}
}
}
#include <Nazara/VulkanRenderer/DebugOff.hpp>

View File

@@ -115,17 +115,17 @@ namespace Nz
static constexpr std::size_t QueueCount = static_cast<std::size_t>(QueueType::Max) + 1;
std::unique_ptr<InternalData> m_internalData;
std::array<UInt32, QueueCount> m_defaultQueues;
std::unordered_set<std::string> m_loadedExtensions;
std::unordered_set<std::string> m_loadedLayers;
std::vector<QueueFamilyInfo> m_enabledQueuesInfos;
std::vector<const QueueList*> m_queuesByFamily;
Instance& m_instance;
const Vk::PhysicalDevice* m_physicalDevice;
VkAllocationCallbacks m_allocator;
VkDevice m_device;
VkResult m_lastErrorCode;
VmaAllocator m_memAllocator;
std::array<UInt32, QueueCount> m_defaultQueues;
std::unordered_set<std::string> m_loadedExtensions;
std::unordered_set<std::string> m_loadedLayers;
std::vector<QueueFamilyInfo> m_enabledQueuesInfos;
std::vector<const QueueList*> m_queuesByFamily;
};
}
}

View File

@@ -5,8 +5,6 @@
#include <Nazara/VulkanRenderer/Wrapper/DeviceObject.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/VulkanRenderer/Utils.hpp>
#include <Nazara/VulkanRenderer/Wrapper/CommandPool.hpp>
#include <Nazara/VulkanRenderer/Wrapper/Device.hpp>
#include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz

View File

@@ -21,10 +21,10 @@ namespace Nz
class NAZARA_VULKANRENDERER_API Instance
{
public:
inline Instance();
Instance();
Instance(const Instance&) = delete;
Instance(Instance&&) = delete;
inline ~Instance();
~Instance();
bool Create(const VkInstanceCreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr);
inline bool Create(const std::string& appName, UInt32 appVersion, const std::string& engineName, UInt32 engineVersion, const std::vector<const char*>& layers, const std::vector<const char*>& extensions, const VkAllocationCallbacks* allocator = nullptr);
@@ -45,6 +45,8 @@ namespace Nz
inline VkPhysicalDeviceProperties GetPhysicalDeviceProperties(VkPhysicalDevice device);
bool GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice device, std::vector<VkQueueFamilyProperties>* queueFamilyProperties);
void InstallDebugMessageCallback();
inline bool IsExtensionLoaded(const std::string& extensionName) const;
inline bool IsLayerLoaded(const std::string& layerName) const;
inline bool IsValid() const;
@@ -68,17 +70,20 @@ namespace Nz
#undef NAZARA_VULKANRENDERER_INSTANCE_FUNCTION
private:
inline void DestroyInstance();
void DestroyInstance();
void ResetPointers();
inline PFN_vkVoidFunction GetProcAddr(const char* name);
struct InternalData;
std::unique_ptr<InternalData> m_internalData;
std::unordered_set<std::string> m_loadedExtensions;
std::unordered_set<std::string> m_loadedLayers;
VkAllocationCallbacks m_allocator;
VkInstance m_instance;
VkResult m_lastErrorCode;
UInt32 m_apiVersion;
std::unordered_set<std::string> m_loadedExtensions;
std::unordered_set<std::string> m_loadedLayers;
};
}
}

View File

@@ -12,17 +12,6 @@ namespace Nz
{
namespace Vk
{
inline Instance::Instance() :
m_instance(nullptr)
{
}
inline Instance::~Instance()
{
if (m_instance)
DestroyInstance();
}
inline bool Instance::Create(const std::string& appName, UInt32 appVersion, const std::string& engineName, UInt32 engineVersion, const std::vector<const char*>& layers, const std::vector<const char*>& extensions, const VkAllocationCallbacks* allocator)
{
VkApplicationInfo appInfo =
@@ -142,14 +131,6 @@ namespace Nz
return properties;
}
inline void Instance::DestroyInstance()
{
assert(m_instance != VK_NULL_HANDLE);
if (vkDestroyInstance)
vkDestroyInstance(m_instance, (m_allocator.pfnAllocation) ? &m_allocator : nullptr);
}
inline PFN_vkVoidFunction Instance::GetProcAddr(const char* name)
{
PFN_vkVoidFunction func = Loader::GetInstanceProcAddr(m_instance, name);

View File

@@ -0,0 +1,53 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Vulkan Renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_VULKANRENDERER_VKINSTANCEOBJECT_HPP
#define NAZARA_VULKANRENDERER_VKINSTANCEOBJECT_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/MovablePtr.hpp>
#include <Nazara/VulkanRenderer/Wrapper/Instance.hpp>
#include <vulkan/vulkan.h>
#include <string>
namespace Nz
{
namespace Vk
{
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
class InstanceObject
{
public:
InstanceObject();
InstanceObject(const InstanceObject&) = delete;
InstanceObject(InstanceObject&& object) noexcept;
~InstanceObject();
bool Create(Instance& instance, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr);
void Destroy();
bool IsValid() const;
Instance* GetInstance() const;
VkResult GetLastErrorCode() const;
InstanceObject& operator=(const InstanceObject&) = delete;
InstanceObject& operator=(InstanceObject&& object) noexcept;
operator VkType() const;
protected:
MovablePtr<Instance> m_instance;
VkAllocationCallbacks m_allocator;
VkType m_handle;
mutable VkResult m_lastErrorCode;
};
}
}
#include <Nazara/VulkanRenderer/Wrapper/InstanceObject.inl>
#endif // NAZARA_VULKANRENDERER_VKINSTANCEOBJECT_HPP

View File

@@ -0,0 +1,103 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Vulkan Renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/VulkanRenderer/Wrapper/InstanceObject.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/VulkanRenderer/Utils.hpp>
#include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz
{
namespace Vk
{
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
InstanceObject<C, VkType, CreateInfo, ObjectType>::InstanceObject() :
m_handle(VK_NULL_HANDLE)
{
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
InstanceObject<C, VkType, CreateInfo, ObjectType>::InstanceObject(InstanceObject&& object) noexcept :
m_instance(std::move(object.m_instance)),
m_allocator(object.m_allocator),
m_handle(object.m_handle),
m_lastErrorCode(object.m_lastErrorCode)
{
object.m_handle = VK_NULL_HANDLE;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
InstanceObject<C, VkType, CreateInfo, ObjectType>::~InstanceObject()
{
Destroy();
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
bool InstanceObject<C, VkType, CreateInfo, ObjectType>::Create(Instance& instance, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator)
{
m_instance = &instance;
m_lastErrorCode = C::CreateHelper(*m_instance, &createInfo, allocator, &m_handle);
if (m_lastErrorCode != VkResult::VK_SUCCESS)
{
NazaraError("Failed to create Vulkan object: " + TranslateVulkanError(m_lastErrorCode));
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, VkObjectType ObjectType>
void InstanceObject<C, VkType, CreateInfo, ObjectType>::Destroy()
{
if (IsValid())
{
C::DestroyHelper(*m_instance, m_handle, (m_allocator.pfnAllocation) ? &m_allocator : nullptr);
m_handle = VK_NULL_HANDLE;
}
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
bool InstanceObject<C, VkType, CreateInfo, ObjectType>::IsValid() const
{
return m_handle != VK_NULL_HANDLE;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
Instance* InstanceObject<C, VkType, CreateInfo, ObjectType>::GetInstance() const
{
return m_instance;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
VkResult InstanceObject<C, VkType, CreateInfo, ObjectType>::GetLastErrorCode() const
{
return m_lastErrorCode;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
auto InstanceObject<C, VkType, CreateInfo, ObjectType>::operator=(InstanceObject&& object) noexcept -> InstanceObject&
{
std::swap(m_allocator, object.m_allocator);
std::swap(m_instance, object.m_instance);
std::swap(m_handle, object.m_handle);
std::swap(m_lastErrorCode, object.m_lastErrorCode);
return *this;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
InstanceObject<C, VkType, CreateInfo, ObjectType>::operator VkType() const
{
return m_handle;
}
}
}
#include <Nazara/VulkanRenderer/DebugOff.hpp>