From 63547fcd4e57c81afeea9760ddc7f4c0a1c0dfb0 Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 13 Mar 2020 18:38:26 +0100 Subject: [PATCH] Replace DeviceHandle by references and keep device alive until Vulkan is freed --- examples/VulkanTest/main.cpp | 14 ++--- include/Nazara/VulkanRenderer/Vulkan.hpp | 2 +- .../Nazara/VulkanRenderer/VulkanBuffer.hpp | 6 +- .../Nazara/VulkanRenderer/VulkanBuffer.inl | 2 +- .../VulkanRenderer/VulkanRenderPipeline.hpp | 5 +- .../VulkanRenderPipelineLayout.hpp | 4 +- .../VulkanRenderer/VulkanShaderStage.hpp | 2 +- .../Nazara/VulkanRenderer/Wrapper/Buffer.hpp | 6 +- .../Nazara/VulkanRenderer/Wrapper/Buffer.inl | 12 ++-- .../VulkanRenderer/Wrapper/CommandPool.hpp | 6 +- .../VulkanRenderer/Wrapper/CommandPool.inl | 12 ++-- .../VulkanRenderer/Wrapper/DescriptorPool.hpp | 8 +-- .../VulkanRenderer/Wrapper/DescriptorPool.inl | 16 ++--- .../Wrapper/DescriptorSetLayout.hpp | 8 +-- .../Wrapper/DescriptorSetLayout.inl | 16 ++--- .../Nazara/VulkanRenderer/Wrapper/Device.hpp | 12 ++-- .../Nazara/VulkanRenderer/Wrapper/Device.inl | 24 -------- .../VulkanRenderer/Wrapper/DeviceMemory.hpp | 8 +-- .../VulkanRenderer/Wrapper/DeviceMemory.inl | 18 +++--- .../VulkanRenderer/Wrapper/DeviceObject.hpp | 7 ++- .../VulkanRenderer/Wrapper/DeviceObject.inl | 10 ++-- .../Nazara/VulkanRenderer/Wrapper/Fence.hpp | 6 +- .../Nazara/VulkanRenderer/Wrapper/Fence.inl | 12 ++-- .../VulkanRenderer/Wrapper/Framebuffer.hpp | 4 +- .../VulkanRenderer/Wrapper/Framebuffer.inl | 8 +-- .../Nazara/VulkanRenderer/Wrapper/Image.hpp | 4 +- .../Nazara/VulkanRenderer/Wrapper/Image.inl | 8 +-- .../VulkanRenderer/Wrapper/ImageView.hpp | 4 +- .../VulkanRenderer/Wrapper/ImageView.inl | 8 +-- .../VulkanRenderer/Wrapper/Instance.hpp | 3 + .../VulkanRenderer/Wrapper/Instance.inl | 15 ++++- .../VulkanRenderer/Wrapper/Pipeline.hpp | 10 ++-- .../VulkanRenderer/Wrapper/Pipeline.inl | 16 ++--- .../VulkanRenderer/Wrapper/PipelineCache.hpp | 4 +- .../VulkanRenderer/Wrapper/PipelineCache.inl | 8 +-- .../VulkanRenderer/Wrapper/PipelineLayout.hpp | 8 +-- .../VulkanRenderer/Wrapper/PipelineLayout.inl | 16 ++--- .../Nazara/VulkanRenderer/Wrapper/Queue.hpp | 6 +- .../Nazara/VulkanRenderer/Wrapper/Queue.inl | 25 ++------ .../VulkanRenderer/Wrapper/RenderPass.hpp | 4 +- .../VulkanRenderer/Wrapper/RenderPass.inl | 8 +-- .../VulkanRenderer/Wrapper/Semaphore.hpp | 6 +- .../VulkanRenderer/Wrapper/Semaphore.inl | 12 ++-- .../VulkanRenderer/Wrapper/ShaderModule.hpp | 6 +- .../VulkanRenderer/Wrapper/ShaderModule.inl | 12 ++-- .../VulkanRenderer/Wrapper/Swapchain.hpp | 6 +- .../VulkanRenderer/Wrapper/Swapchain.inl | 14 ++--- src/Nazara/VulkanRenderer/VkRenderWindow.cpp | 17 +++--- src/Nazara/VulkanRenderer/Vulkan.cpp | 18 +----- src/Nazara/VulkanRenderer/VulkanDevice.cpp | 10 ++-- .../VulkanRenderer/VulkanRenderPipeline.cpp | 6 +- .../VulkanRenderPipelineLayout.cpp | 8 +-- src/Nazara/VulkanRenderer/VulkanRenderer.cpp | 4 +- .../VulkanRenderer/VulkanShaderStage.cpp | 2 +- src/Nazara/VulkanRenderer/Wrapper/Device.cpp | 60 ++++++++++++++++++- .../VulkanRenderer/Wrapper/Instance.cpp | 15 +++++ 56 files changed, 303 insertions(+), 268 deletions(-) diff --git a/examples/VulkanTest/main.cpp b/examples/VulkanTest/main.cpp index bd121a9b1..34333abb5 100644 --- a/examples/VulkanTest/main.cpp +++ b/examples/VulkanTest/main.cpp @@ -154,7 +154,7 @@ int main() Nz::VulkanDevice& vulkanDevice = vulkanWindow.GetDevice(); Nz::Vk::DescriptorPool descriptorPool; - if (!descriptorPool.Create(vulkanDevice.shared_from_this(), 1, poolSize, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) + if (!descriptorPool.Create(vulkanDevice, 1, poolSize, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) { NazaraError("Failed to create descriptor pool"); return __LINE__; @@ -165,7 +165,7 @@ int main() Nz::RenderBuffer* renderBufferUB = static_cast(uniformBuffer.GetBuffer()->GetImpl()); if (!renderBufferUB->Synchronize(&vulkanDevice)) { - NazaraError("Failed to synchronize render buffer"); + NazaraError("Failed to create uniform buffer"); return __LINE__; } Nz::VulkanBuffer* uniformBufferImpl = static_cast(renderBufferUB->GetHardwareBuffer(&vulkanDevice)); @@ -188,7 +188,7 @@ int main() pipelineCreateInfo.pipelineInfo.renderPass = vulkanWindow.GetRenderPass(); Nz::Vk::CommandPool cmdPool; - if (!cmdPool.Create(vulkanDevice.shared_from_this(), 0, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT)) + if (!cmdPool.Create(vulkanDevice, 0, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT)) { NazaraError("Failed to create rendering cmd pool"); return __LINE__; @@ -198,7 +198,7 @@ int main() clearValues[0].color = {1.0f, 0.8f, 0.4f, 0.0f}; clearValues[1].depthStencil = {1.f, 0}; - Nz::Vk::Queue graphicsQueue(vulkanDevice.shared_from_this(), vulkanDevice.GetEnabledQueues()[0].queues[0].queue); + Nz::Vk::Queue graphicsQueue(vulkanDevice, vulkanDevice.GetEnabledQueues()[0].queues[0].queue); Nz::UInt32 imageCount = vulkanWindow.GetFramebufferCount(); std::vector renderCmds = cmdPool.AllocateCommandBuffers(imageCount, VK_COMMAND_BUFFER_LEVEL_PRIMARY); @@ -286,10 +286,10 @@ int main() std::vector frameSync(MaxConcurrentImage); for (ImageSync& syncData : frameSync) { - syncData.imageAvailableSemaphore.Create(vulkanDevice.shared_from_this()); - syncData.renderFinishedSemaphore.Create(vulkanDevice.shared_from_this()); + syncData.imageAvailableSemaphore.Create(vulkanDevice); + syncData.renderFinishedSemaphore.Create(vulkanDevice); - syncData.inflightFence.Create(vulkanDevice.shared_from_this(), VK_FENCE_CREATE_SIGNALED_BIT); + syncData.inflightFence.Create(vulkanDevice, VK_FENCE_CREATE_SIGNALED_BIT); } std::vector inflightFences(imageCount, nullptr); diff --git a/include/Nazara/VulkanRenderer/Vulkan.hpp b/include/Nazara/VulkanRenderer/Vulkan.hpp index 2898dca94..ef88e3d10 100644 --- a/include/Nazara/VulkanRenderer/Vulkan.hpp +++ b/include/Nazara/VulkanRenderer/Vulkan.hpp @@ -54,7 +54,7 @@ namespace Nz static void Uninitialize(); private: - static std::vector> s_devices; + static std::vector> s_devices; static std::vector s_physDevices; static Vk::Instance s_instance; static ParameterList s_initializationParameters; diff --git a/include/Nazara/VulkanRenderer/VulkanBuffer.hpp b/include/Nazara/VulkanRenderer/VulkanBuffer.hpp index 04ee709dd..52adcf253 100644 --- a/include/Nazara/VulkanRenderer/VulkanBuffer.hpp +++ b/include/Nazara/VulkanRenderer/VulkanBuffer.hpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include namespace Nz @@ -21,7 +23,7 @@ namespace Nz class NAZARA_VULKANRENDERER_API VulkanBuffer : public AbstractBuffer { public: - inline VulkanBuffer(const Vk::DeviceHandle& device, Buffer* parent, BufferType type); + inline VulkanBuffer(Vk::Device& device, Buffer* parent, BufferType type); VulkanBuffer(const VulkanBuffer&) = delete; VulkanBuffer(VulkanBuffer&&) = delete; ///TODO virtual ~VulkanBuffer(); @@ -43,8 +45,8 @@ namespace Nz Buffer* m_parent; BufferType m_type; Nz::Vk::Buffer m_buffer; - Nz::Vk::DeviceHandle m_device; Nz::Vk::DeviceMemory m_memory; + Vk::Device& m_device; }; } diff --git a/include/Nazara/VulkanRenderer/VulkanBuffer.inl b/include/Nazara/VulkanRenderer/VulkanBuffer.inl index cc699a10d..2e210c149 100644 --- a/include/Nazara/VulkanRenderer/VulkanBuffer.inl +++ b/include/Nazara/VulkanRenderer/VulkanBuffer.inl @@ -7,7 +7,7 @@ namespace Nz { - inline VulkanBuffer::VulkanBuffer(const Vk::DeviceHandle& device, Buffer* parent, BufferType type) : + inline VulkanBuffer::VulkanBuffer(Vk::Device& device, Buffer* /*parent*/, BufferType type) : m_device(device), m_parent(parent), m_type(type) diff --git a/include/Nazara/VulkanRenderer/VulkanRenderPipeline.hpp b/include/Nazara/VulkanRenderer/VulkanRenderPipeline.hpp index baa644c60..6fa836a47 100644 --- a/include/Nazara/VulkanRenderer/VulkanRenderPipeline.hpp +++ b/include/Nazara/VulkanRenderer/VulkanRenderPipeline.hpp @@ -8,6 +8,7 @@ #define NAZARA_VULKANRENDERER_VULKANRENDERPIPELINE_HPP #include +#include #include #include #include @@ -22,7 +23,7 @@ namespace Nz public: struct CreateInfo; - VulkanRenderPipeline(Vk::DeviceHandle device, RenderPipelineInfo pipelineInfo); + VulkanRenderPipeline(Vk::Device& device, RenderPipelineInfo pipelineInfo); ~VulkanRenderPipeline() = default; VkPipeline Get(const Vk::RenderPass& renderPass); @@ -69,7 +70,7 @@ namespace Nz private: std::unordered_map m_pipelines; - Vk::DeviceHandle m_device; + MovablePtr m_device; CreateInfo m_pipelineCreateInfo; RenderPipelineInfo m_pipelineInfo; }; diff --git a/include/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp b/include/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp index beac99148..98911226c 100644 --- a/include/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp +++ b/include/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp @@ -23,13 +23,13 @@ namespace Nz VulkanRenderPipelineLayout() = default; ~VulkanRenderPipelineLayout() = default; - bool Create(Vk::DeviceHandle device, RenderPipelineLayoutInfo layoutInfo); + bool Create(Vk::Device& device, RenderPipelineLayoutInfo layoutInfo); inline const Vk::DescriptorSetLayout& GetDescriptorSetLayout() const; inline const Vk::PipelineLayout& GetPipelineLayout() const; private: - Vk::DeviceHandle m_device; + MovablePtr m_device; Vk::DescriptorSetLayout m_descriptorSetLayout; Vk::PipelineLayout m_pipelineLayout; RenderPipelineLayoutInfo m_layoutInfo; diff --git a/include/Nazara/VulkanRenderer/VulkanShaderStage.hpp b/include/Nazara/VulkanRenderer/VulkanShaderStage.hpp index 533592a45..5809ba4e7 100644 --- a/include/Nazara/VulkanRenderer/VulkanShaderStage.hpp +++ b/include/Nazara/VulkanRenderer/VulkanShaderStage.hpp @@ -23,7 +23,7 @@ namespace Nz VulkanShaderStage(VulkanShaderStage&&) noexcept = default; ~VulkanShaderStage() = default; - bool Create(const Vk::DeviceHandle& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize); + bool Create(Vk::Device& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize); inline const Vk::ShaderModule& GetHandle() const; inline ShaderStageType GetStageType() const; diff --git a/include/Nazara/VulkanRenderer/Wrapper/Buffer.hpp b/include/Nazara/VulkanRenderer/Wrapper/Buffer.hpp index a846c922e..ad3573f34 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Buffer.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/Buffer.hpp @@ -27,7 +27,7 @@ namespace Nz bool BindBufferMemory(VkDeviceMemory memory, VkDeviceSize offset = 0); using DeviceObject::Create; - inline bool Create(DeviceHandle device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(Device& device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator = nullptr); VkMemoryRequirements GetMemoryRequirements() const; @@ -35,8 +35,8 @@ namespace Nz Buffer& operator=(Buffer&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkBufferCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkBuffer* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkBuffer handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkBufferCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkBuffer* handle); + static inline void DestroyHelper(Device& device, VkBuffer handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/Buffer.inl b/include/Nazara/VulkanRenderer/Wrapper/Buffer.inl index e5b79d6c7..15762a7b0 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Buffer.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/Buffer.inl @@ -21,7 +21,7 @@ namespace Nz return true; } - inline bool Buffer::Create(DeviceHandle device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator) + inline bool Buffer::Create(Device& device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator) { VkBufferCreateInfo createInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType; @@ -34,7 +34,7 @@ namespace Nz nullptr // const uint32_t* pQueueFamilyIndices; }; - return Create(std::move(device), createInfo, allocator); + return Create(device, createInfo, allocator); } inline VkMemoryRequirements Buffer::GetMemoryRequirements() const @@ -47,14 +47,14 @@ namespace Nz return memoryRequirements; } - inline VkResult Buffer::CreateHelper(const DeviceHandle& device, const VkBufferCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkBuffer* handle) + inline VkResult Buffer::CreateHelper(Device& device, const VkBufferCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkBuffer* handle) { - return device->vkCreateBuffer(*device, createInfo, allocator, handle); + return device.vkCreateBuffer(device, createInfo, allocator, handle); } - inline void Buffer::DestroyHelper(const DeviceHandle& device, VkBuffer handle, const VkAllocationCallbacks* allocator) + inline void Buffer::DestroyHelper(Device& device, VkBuffer handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroyBuffer(*device, handle, allocator); + return device.vkDestroyBuffer(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/CommandPool.hpp b/include/Nazara/VulkanRenderer/Wrapper/CommandPool.hpp index ff88dc378..807f26f19 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/CommandPool.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/CommandPool.hpp @@ -30,7 +30,7 @@ namespace Nz std::vector AllocateCommandBuffers(UInt32 commandBufferCount, VkCommandBufferLevel level); using DeviceObject::Create; - inline bool Create(DeviceHandle device, UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(Device& device, UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); inline bool Reset(VkCommandPoolResetFlags flags); @@ -38,8 +38,8 @@ namespace Nz CommandPool& operator=(CommandPool&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkCommandPoolCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkCommandPool* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkCommandPool handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkCommandPoolCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkCommandPool* handle); + static inline void DestroyHelper(Device& device, VkCommandPool handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/CommandPool.inl b/include/Nazara/VulkanRenderer/Wrapper/CommandPool.inl index 96b1f2abd..615db5898 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/CommandPool.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/CommandPool.inl @@ -11,7 +11,7 @@ namespace Nz { namespace Vk { - inline bool CommandPool::Create(DeviceHandle device, UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags, const VkAllocationCallbacks* allocator) + inline bool CommandPool::Create(Device& device, UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags, const VkAllocationCallbacks* allocator) { VkCommandPoolCreateInfo createInfo = { @@ -21,7 +21,7 @@ namespace Nz queueFamilyIndex }; - return Create(std::move(device), createInfo, allocator); + return Create(device, createInfo, allocator); } inline bool CommandPool::Reset(VkCommandPoolResetFlags flags) @@ -33,14 +33,14 @@ namespace Nz return true; } - inline VkResult CommandPool::CreateHelper(const DeviceHandle& device, const VkCommandPoolCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkCommandPool* handle) + inline VkResult CommandPool::CreateHelper(Device& device, const VkCommandPoolCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkCommandPool* handle) { - return device->vkCreateCommandPool(*device, createInfo, allocator, handle); + return device.vkCreateCommandPool(device, createInfo, allocator, handle); } - inline void CommandPool::DestroyHelper(const DeviceHandle& device, VkCommandPool handle, const VkAllocationCallbacks* allocator) + inline void CommandPool::DestroyHelper(Device& device, VkCommandPool handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroyCommandPool(*device, handle, allocator); + return device.vkDestroyCommandPool(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/DescriptorPool.hpp b/include/Nazara/VulkanRenderer/Wrapper/DescriptorPool.hpp index bf7da5556..06f64d185 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/DescriptorPool.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/DescriptorPool.hpp @@ -30,15 +30,15 @@ namespace Nz std::vector AllocateDescriptorSets(UInt32 descriptorSetCount, const VkDescriptorSetLayout* setLayouts); using DeviceObject::Create; - inline bool Create(DeviceHandle device, UInt32 maxSets, const VkDescriptorPoolSize& poolSize, VkDescriptorPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); - inline bool Create(DeviceHandle device, UInt32 maxSets, UInt32 poolSizeCount, const VkDescriptorPoolSize* poolSize, VkDescriptorPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(Device& device, UInt32 maxSets, const VkDescriptorPoolSize& poolSize, VkDescriptorPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(Device& device, UInt32 maxSets, UInt32 poolSizeCount, const VkDescriptorPoolSize* poolSize, VkDescriptorPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); DescriptorPool& operator=(const DescriptorPool&) = delete; DescriptorPool& operator=(DescriptorPool&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkDescriptorPoolCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkDescriptorPool* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkDescriptorPool handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkDescriptorPoolCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkDescriptorPool* handle); + static inline void DestroyHelper(Device& device, VkDescriptorPool handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/DescriptorPool.inl b/include/Nazara/VulkanRenderer/Wrapper/DescriptorPool.inl index a4161b367..46392a326 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/DescriptorPool.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/DescriptorPool.inl @@ -9,7 +9,7 @@ namespace Nz { namespace Vk { - inline bool DescriptorPool::Create(DeviceHandle device, UInt32 maxSets, const VkDescriptorPoolSize& poolSize, VkDescriptorPoolCreateFlags flags, const VkAllocationCallbacks* allocator) + inline bool DescriptorPool::Create(Device& device, UInt32 maxSets, const VkDescriptorPoolSize& poolSize, VkDescriptorPoolCreateFlags flags, const VkAllocationCallbacks* allocator) { VkDescriptorPoolCreateInfo createInfo = { @@ -21,10 +21,10 @@ namespace Nz &poolSize // const VkDescriptorPoolSize* pPoolSizes; }; - return Create(std::move(device), createInfo, allocator); + return Create(device, createInfo, allocator); } - inline bool DescriptorPool::Create(DeviceHandle device, UInt32 maxSets, UInt32 poolSizeCount, const VkDescriptorPoolSize* poolSize, VkDescriptorPoolCreateFlags flags, const VkAllocationCallbacks* allocator) + inline bool DescriptorPool::Create(Device& device, UInt32 maxSets, UInt32 poolSizeCount, const VkDescriptorPoolSize* poolSize, VkDescriptorPoolCreateFlags flags, const VkAllocationCallbacks* allocator) { VkDescriptorPoolCreateInfo createInfo = { @@ -36,17 +36,17 @@ namespace Nz poolSize // const VkDescriptorPoolSize* pPoolSizes; }; - return Create(std::move(device), createInfo, allocator); + return Create(device, createInfo, allocator); } - inline VkResult DescriptorPool::CreateHelper(const DeviceHandle& device, const VkDescriptorPoolCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkDescriptorPool* handle) + inline VkResult DescriptorPool::CreateHelper(Device& device, const VkDescriptorPoolCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkDescriptorPool* handle) { - return device->vkCreateDescriptorPool(*device, createInfo, allocator, handle); + return device.vkCreateDescriptorPool(device, createInfo, allocator, handle); } - inline void DescriptorPool::DestroyHelper(const DeviceHandle& device, VkDescriptorPool handle, const VkAllocationCallbacks* allocator) + inline void DescriptorPool::DestroyHelper(Device& device, VkDescriptorPool handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroyDescriptorPool(*device, handle, allocator); + return device.vkDestroyDescriptorPool(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/DescriptorSetLayout.hpp b/include/Nazara/VulkanRenderer/Wrapper/DescriptorSetLayout.hpp index fd2b09a22..81ef4849e 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/DescriptorSetLayout.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/DescriptorSetLayout.hpp @@ -25,15 +25,15 @@ namespace Nz ~DescriptorSetLayout() = default; using DeviceObject::Create; - inline bool Create(DeviceHandle device, const VkDescriptorSetLayoutBinding& binding, VkDescriptorSetLayoutCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); - inline bool Create(DeviceHandle device, UInt32 bindingCount, const VkDescriptorSetLayoutBinding* binding, VkDescriptorSetLayoutCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(Device& device, const VkDescriptorSetLayoutBinding& binding, VkDescriptorSetLayoutCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(Device& device, UInt32 bindingCount, const VkDescriptorSetLayoutBinding* binding, VkDescriptorSetLayoutCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); DescriptorSetLayout& operator=(const DescriptorSetLayout&) = delete; DescriptorSetLayout& operator=(DescriptorSetLayout&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkDescriptorSetLayoutCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkDescriptorSetLayout* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkDescriptorSetLayout handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkDescriptorSetLayoutCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkDescriptorSetLayout* handle); + static inline void DestroyHelper(Device& device, VkDescriptorSetLayout handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/DescriptorSetLayout.inl b/include/Nazara/VulkanRenderer/Wrapper/DescriptorSetLayout.inl index 20b74400a..3e25fe591 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/DescriptorSetLayout.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/DescriptorSetLayout.inl @@ -9,12 +9,12 @@ namespace Nz { namespace Vk { - inline bool DescriptorSetLayout::Create(DeviceHandle device, const VkDescriptorSetLayoutBinding& binding, VkDescriptorSetLayoutCreateFlags flags, const VkAllocationCallbacks* allocator) + inline bool DescriptorSetLayout::Create(Device& device, const VkDescriptorSetLayoutBinding& binding, VkDescriptorSetLayoutCreateFlags flags, const VkAllocationCallbacks* allocator) { - return Create(std::move(device), 1U, &binding, flags, allocator); + return Create(device, 1U, &binding, flags, allocator); } - inline bool DescriptorSetLayout::Create(DeviceHandle device, UInt32 bindingCount, const VkDescriptorSetLayoutBinding* binding, VkDescriptorSetLayoutCreateFlags flags, const VkAllocationCallbacks* allocator) + inline bool DescriptorSetLayout::Create(Device& device, UInt32 bindingCount, const VkDescriptorSetLayoutBinding* binding, VkDescriptorSetLayoutCreateFlags flags, const VkAllocationCallbacks* allocator) { VkDescriptorSetLayoutCreateInfo createInfo = { @@ -25,17 +25,17 @@ namespace Nz binding // const VkDescriptorSetLayoutBinding* pBindings; }; - return Create(std::move(device), createInfo, allocator); + return Create(device, createInfo, allocator); } - inline VkResult DescriptorSetLayout::CreateHelper(const DeviceHandle& device, const VkDescriptorSetLayoutCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkDescriptorSetLayout* handle) + inline VkResult DescriptorSetLayout::CreateHelper(Device& device, const VkDescriptorSetLayoutCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkDescriptorSetLayout* handle) { - return device->vkCreateDescriptorSetLayout(*device, createInfo, allocator, handle); + return device.vkCreateDescriptorSetLayout(device, createInfo, allocator, handle); } - inline void DescriptorSetLayout::DestroyHelper(const DeviceHandle& device, VkDescriptorSetLayout handle, const VkAllocationCallbacks* allocator) + inline void DescriptorSetLayout::DestroyHelper(Device& device, VkDescriptorSetLayout handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroyDescriptorSetLayout(*device, handle, allocator); + return device.vkDestroyDescriptorSetLayout(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/Device.hpp b/include/Nazara/VulkanRenderer/Wrapper/Device.hpp index 1e381d16d..b2a4cf70b 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Device.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/Device.hpp @@ -19,12 +19,9 @@ namespace Nz { namespace Vk { - class Device; class Instance; class Queue; - using DeviceHandle = std::shared_ptr; - class NAZARA_VULKANRENDERER_API Device : public std::enable_shared_from_this { public: @@ -32,10 +29,10 @@ namespace Nz struct QueueInfo; using QueueList = std::vector; - inline Device(Instance& instance); + Device(Instance& instance); Device(const Device&) = delete; Device(Device&&) = delete; - inline ~Device(); + ~Device(); bool Create(const Vk::PhysicalDevice& deviceInfo, const VkDeviceCreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr); inline void Destroy(); @@ -63,7 +60,7 @@ namespace Nz // Vulkan functions #define NAZARA_VULKANRENDERER_DEVICE_EXT_BEGIN(ext) #define NAZARA_VULKANRENDERER_DEVICE_EXT_END() -#define NAZARA_VULKANRENDERER_DEVICE_FUNCTION(func) PFN_##func func; +#define NAZARA_VULKANRENDERER_DEVICE_FUNCTION(func) PFN_##func func = nullptr; #include @@ -88,6 +85,9 @@ namespace Nz }; private: + void WaitAndDestroyDevice(); + void ResetPointers(); + inline PFN_vkVoidFunction GetProcAddr(const char* name); Instance& m_instance; diff --git a/include/Nazara/VulkanRenderer/Wrapper/Device.inl b/include/Nazara/VulkanRenderer/Wrapper/Device.inl index 5affb6bc2..e3a069745 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Device.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/Device.inl @@ -12,30 +12,6 @@ namespace Nz { namespace Vk { - inline Device::Device(Instance& instance) : - m_instance(instance), - m_physicalDevice(nullptr), - m_device(VK_NULL_HANDLE) - { - } - - inline Device::~Device() - { - Destroy(); - } - - inline void Device::Destroy() - { - if (m_device != VK_NULL_HANDLE) - { - vkDeviceWaitIdle(m_device); - vkDestroyDevice(m_device, (m_allocator.pfnAllocation) ? &m_allocator : nullptr); - - m_device = VK_NULL_HANDLE; - m_physicalDevice = nullptr; - } - } - inline const std::vector& Device::GetEnabledQueues() const { return m_enabledQueuesInfos; diff --git a/include/Nazara/VulkanRenderer/Wrapper/DeviceMemory.hpp b/include/Nazara/VulkanRenderer/Wrapper/DeviceMemory.hpp index d6f32e80a..8b2d57ada 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/DeviceMemory.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/DeviceMemory.hpp @@ -25,8 +25,8 @@ namespace Nz ~DeviceMemory() = default; using DeviceObject::Create; - inline bool Create(DeviceHandle device, VkDeviceSize size, UInt32 memoryType, const VkAllocationCallbacks* allocator = nullptr); - inline bool Create(DeviceHandle device, VkDeviceSize size, UInt32 typeBits, VkFlags properties, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(Device& device, VkDeviceSize size, UInt32 memoryType, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(Device& device, VkDeviceSize size, UInt32 typeBits, VkFlags properties, const VkAllocationCallbacks* allocator = nullptr); inline void* GetMappedPointer(); @@ -38,8 +38,8 @@ namespace Nz DeviceMemory& operator=(DeviceMemory&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkMemoryAllocateInfo* allocInfo, const VkAllocationCallbacks* allocator, VkDeviceMemory* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkDeviceMemory handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkMemoryAllocateInfo* allocInfo, const VkAllocationCallbacks* allocator, VkDeviceMemory* handle); + static inline void DestroyHelper(Device& device, VkDeviceMemory handle, const VkAllocationCallbacks* allocator); void* m_mappedPtr; }; diff --git a/include/Nazara/VulkanRenderer/Wrapper/DeviceMemory.inl b/include/Nazara/VulkanRenderer/Wrapper/DeviceMemory.inl index 26bee7012..65fd619d4 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/DeviceMemory.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/DeviceMemory.inl @@ -24,7 +24,7 @@ namespace Nz memory.m_mappedPtr = nullptr; } - inline bool DeviceMemory::Create(DeviceHandle device, VkDeviceSize size, UInt32 memoryType, const VkAllocationCallbacks* allocator) + inline bool DeviceMemory::Create(Device& device, VkDeviceSize size, UInt32 memoryType, const VkAllocationCallbacks* allocator) { VkMemoryAllocateInfo allocInfo = { @@ -34,12 +34,12 @@ namespace Nz memoryType // uint32_t memoryTypeIndex; }; - return Create(std::move(device), allocInfo, allocator); + return Create(device, allocInfo, allocator); } - inline bool DeviceMemory::Create(DeviceHandle device, VkDeviceSize size, UInt32 typeBits, VkFlags properties, const VkAllocationCallbacks* allocator) + inline bool DeviceMemory::Create(Device& device, VkDeviceSize size, UInt32 typeBits, VkFlags properties, const VkAllocationCallbacks* allocator) { - const Vk::PhysicalDevice& deviceInfo = Vulkan::GetPhysicalDeviceInfo(device->GetPhysicalDevice()); + const Vk::PhysicalDevice& deviceInfo = Vulkan::GetPhysicalDeviceInfo(device.GetPhysicalDevice()); UInt32 typeMask = 1; for (UInt32 i = 0; i < VK_MAX_MEMORY_TYPES; ++i) @@ -47,7 +47,7 @@ namespace Nz if (typeBits & typeMask) { if ((deviceInfo.memoryProperties.memoryTypes[i].propertyFlags & properties) == properties) - return Create(std::move(device), size, i, allocator); + return Create(device, size, i, allocator); } typeMask <<= 1; @@ -82,14 +82,14 @@ namespace Nz m_mappedPtr = nullptr; } - inline VkResult DeviceMemory::CreateHelper(const DeviceHandle& device, const VkMemoryAllocateInfo* allocInfo, const VkAllocationCallbacks* allocator, VkDeviceMemory* handle) + inline VkResult DeviceMemory::CreateHelper(Device& device, const VkMemoryAllocateInfo* allocInfo, const VkAllocationCallbacks* allocator, VkDeviceMemory* handle) { - return device->vkAllocateMemory(*device, allocInfo, allocator, handle); + return device.vkAllocateMemory(device, allocInfo, allocator, handle); } - inline void DeviceMemory::DestroyHelper(const DeviceHandle& device, VkDeviceMemory handle, const VkAllocationCallbacks* allocator) + inline void DeviceMemory::DestroyHelper(Device& device, VkDeviceMemory handle, const VkAllocationCallbacks* allocator) { - return device->vkFreeMemory(*device, handle, allocator); + return device.vkFreeMemory(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/DeviceObject.hpp b/include/Nazara/VulkanRenderer/Wrapper/DeviceObject.hpp index 4062cb5c8..887b05563 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/DeviceObject.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/DeviceObject.hpp @@ -8,6 +8,7 @@ #define NAZARA_VULKANRENDERER_VKDEVICEOBJECT_HPP #include +#include #include #include @@ -24,12 +25,12 @@ namespace Nz DeviceObject(DeviceObject&& object); ~DeviceObject(); - bool Create(DeviceHandle device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr); + bool Create(Device& device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr); void Destroy(); bool IsValid() const; - const DeviceHandle& GetDevice() const; + Device* GetDevice() const; VkResult GetLastErrorCode() const; DeviceObject& operator=(const DeviceObject&) = delete; @@ -38,7 +39,7 @@ namespace Nz operator VkType() const; protected: - DeviceHandle m_device; + MovablePtr m_device; VkAllocationCallbacks m_allocator; VkType m_handle; mutable VkResult m_lastErrorCode; diff --git a/include/Nazara/VulkanRenderer/Wrapper/DeviceObject.inl b/include/Nazara/VulkanRenderer/Wrapper/DeviceObject.inl index def0ebc0a..91c671ce4 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/DeviceObject.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/DeviceObject.inl @@ -36,10 +36,10 @@ namespace Nz } template - bool DeviceObject::Create(DeviceHandle device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator) + bool DeviceObject::Create(Device& device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator) { - m_device = std::move(device); - m_lastErrorCode = C::CreateHelper(m_device, &createInfo, allocator, &m_handle); + m_device = &device; + m_lastErrorCode = C::CreateHelper(*m_device, &createInfo, allocator, &m_handle); if (m_lastErrorCode != VkResult::VK_SUCCESS) { NazaraError("Failed to create Vulkan object: " + TranslateVulkanError(m_lastErrorCode)); @@ -60,7 +60,7 @@ namespace Nz { if (IsValid()) { - C::DestroyHelper(m_device, m_handle, (m_allocator.pfnAllocation) ? &m_allocator : nullptr); + C::DestroyHelper(*m_device, m_handle, (m_allocator.pfnAllocation) ? &m_allocator : nullptr); m_handle = VK_NULL_HANDLE; } } @@ -72,7 +72,7 @@ namespace Nz } template - const DeviceHandle& DeviceObject::GetDevice() const + Device* DeviceObject::GetDevice() const { return m_device; } diff --git a/include/Nazara/VulkanRenderer/Wrapper/Fence.hpp b/include/Nazara/VulkanRenderer/Wrapper/Fence.hpp index a78b4a597..054083a3a 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Fence.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/Fence.hpp @@ -25,7 +25,7 @@ namespace Nz ~Fence() = default; using DeviceObject::Create; - inline bool Create(DeviceHandle device, VkFenceCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(Device& device, VkFenceCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); inline bool Reset(); @@ -36,8 +36,8 @@ namespace Nz Fence& operator=(Fence&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkFenceCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkFence* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkFence handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkFenceCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkFence* handle); + static inline void DestroyHelper(Device& device, VkFence handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/Fence.inl b/include/Nazara/VulkanRenderer/Wrapper/Fence.inl index 7a02c20b5..e89f31ed1 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Fence.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/Fence.inl @@ -9,7 +9,7 @@ namespace Nz { namespace Vk { - inline bool Fence::Create(DeviceHandle device, VkFenceCreateFlags flags, const VkAllocationCallbacks* allocator) + inline bool Fence::Create(Device& device, VkFenceCreateFlags flags, const VkAllocationCallbacks* allocator) { VkFenceCreateInfo createInfo = { @@ -18,7 +18,7 @@ namespace Nz flags }; - return Create(std::move(device), createInfo, allocator); + return Create(device, createInfo, allocator); } inline bool Fence::Reset() @@ -53,14 +53,14 @@ namespace Nz return true; } - inline VkResult Fence::CreateHelper(const DeviceHandle& device, const VkFenceCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkFence* handle) + inline VkResult Fence::CreateHelper(Device& device, const VkFenceCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkFence* handle) { - return device->vkCreateFence(*device, createInfo, allocator, handle); + return device.vkCreateFence(device, createInfo, allocator, handle); } - inline void Fence::DestroyHelper(const DeviceHandle& device, VkFence handle, const VkAllocationCallbacks* allocator) + inline void Fence::DestroyHelper(Device& device, VkFence handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroyFence(*device, handle, allocator); + return device.vkDestroyFence(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/Framebuffer.hpp b/include/Nazara/VulkanRenderer/Wrapper/Framebuffer.hpp index 297b69b93..014d1e205 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Framebuffer.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/Framebuffer.hpp @@ -28,8 +28,8 @@ namespace Nz Framebuffer& operator=(Framebuffer&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkFramebufferCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkFramebuffer* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkFramebuffer handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkFramebufferCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkFramebuffer* handle); + static inline void DestroyHelper(Device& device, VkFramebuffer handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/Framebuffer.inl b/include/Nazara/VulkanRenderer/Wrapper/Framebuffer.inl index a13b52539..7b486c952 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Framebuffer.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/Framebuffer.inl @@ -9,14 +9,14 @@ namespace Nz { namespace Vk { - inline VkResult Framebuffer::CreateHelper(const DeviceHandle& device, const VkFramebufferCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkFramebuffer* handle) + inline VkResult Framebuffer::CreateHelper(Device& device, const VkFramebufferCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkFramebuffer* handle) { - return device->vkCreateFramebuffer(*device, createInfo, allocator, handle); + return device.vkCreateFramebuffer(device, createInfo, allocator, handle); } - inline void Framebuffer::DestroyHelper(const DeviceHandle& device, VkFramebuffer handle, const VkAllocationCallbacks* allocator) + inline void Framebuffer::DestroyHelper(Device& device, VkFramebuffer handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroyFramebuffer(*device, handle, allocator); + return device.vkDestroyFramebuffer(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/Image.hpp b/include/Nazara/VulkanRenderer/Wrapper/Image.hpp index f63910501..3c6cdc0e1 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Image.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/Image.hpp @@ -32,8 +32,8 @@ namespace Nz Image& operator=(Image&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkImageCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkImage* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkImage handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkImageCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkImage* handle); + static inline void DestroyHelper(Device& device, VkImage handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/Image.inl b/include/Nazara/VulkanRenderer/Wrapper/Image.inl index bafaf1c62..575aab441 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Image.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/Image.inl @@ -32,14 +32,14 @@ namespace Nz return memoryRequirements; } - inline VkResult Image::CreateHelper(const DeviceHandle& device, const VkImageCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkImage* handle) + inline VkResult Image::CreateHelper(Device& device, const VkImageCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkImage* handle) { - return device->vkCreateImage(*device, createInfo, allocator, handle); + return device.vkCreateImage(device, createInfo, allocator, handle); } - inline void Image::DestroyHelper(const DeviceHandle& device, VkImage handle, const VkAllocationCallbacks* allocator) + inline void Image::DestroyHelper(Device& device, VkImage handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroyImage(*device, handle, allocator); + return device.vkDestroyImage(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/ImageView.hpp b/include/Nazara/VulkanRenderer/Wrapper/ImageView.hpp index 8e3b00031..957453774 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/ImageView.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/ImageView.hpp @@ -28,8 +28,8 @@ namespace Nz ImageView& operator=(ImageView&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkImageViewCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkImageView* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkImageView handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkImageViewCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkImageView* handle); + static inline void DestroyHelper(Device& device, VkImageView handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/ImageView.inl b/include/Nazara/VulkanRenderer/Wrapper/ImageView.inl index cf107865b..d49988586 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/ImageView.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/ImageView.inl @@ -9,14 +9,14 @@ namespace Nz { namespace Vk { - inline VkResult ImageView::CreateHelper(const DeviceHandle& device, const VkImageViewCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkImageView* handle) + inline VkResult ImageView::CreateHelper(Device& device, const VkImageViewCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkImageView* handle) { - return device->vkCreateImageView(*device, createInfo, allocator, handle); + return device.vkCreateImageView(device, createInfo, allocator, handle); } - inline void ImageView::DestroyHelper(const DeviceHandle& device, VkImageView handle, const VkAllocationCallbacks* allocator) + inline void ImageView::DestroyHelper(Device& device, VkImageView handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroyImageView(*device, handle, allocator); + return device.vkDestroyImageView(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/Instance.hpp b/include/Nazara/VulkanRenderer/Wrapper/Instance.hpp index 3a97e5b0b..5ce955bca 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Instance.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/Instance.hpp @@ -64,6 +64,9 @@ namespace Nz #undef NAZARA_VULKANRENDERER_INSTANCE_FUNCTION private: + inline void DestroyInstance(); + void ResetPointers(); + inline PFN_vkVoidFunction GetProcAddr(const char* name); VkAllocationCallbacks m_allocator; diff --git a/include/Nazara/VulkanRenderer/Wrapper/Instance.inl b/include/Nazara/VulkanRenderer/Wrapper/Instance.inl index 646f3d4c9..309a01fb2 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Instance.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/Instance.inl @@ -5,6 +5,7 @@ #include #include #include +#include #include namespace Nz @@ -18,7 +19,7 @@ namespace Nz inline Instance::~Instance() { - Destroy(); + DestroyInstance(); } inline bool Instance::Create(const String& appName, UInt32 appVersion, const String& engineName, UInt32 engineVersion, const std::vector& layers, const std::vector& extensions, const VkAllocationCallbacks* allocator) @@ -52,8 +53,8 @@ namespace Nz { if (m_instance) { - vkDestroyInstance(m_instance, (m_allocator.pfnAllocation) ? &m_allocator : nullptr); - m_instance = nullptr; + DestroyInstance(); + ResetPointers(); } } @@ -135,6 +136,14 @@ 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); diff --git a/include/Nazara/VulkanRenderer/Wrapper/Pipeline.hpp b/include/Nazara/VulkanRenderer/Wrapper/Pipeline.hpp index e97a50509..d63abadac 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Pipeline.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/Pipeline.hpp @@ -22,11 +22,11 @@ namespace Nz Pipeline(Pipeline&&); inline ~Pipeline(); - inline bool CreateCompute(const DeviceHandle& device, const VkComputePipelineCreateInfo& createInfo, VkPipelineCache cache = VK_NULL_HANDLE, const VkAllocationCallbacks* allocator = nullptr); - inline bool CreateGraphics(const DeviceHandle& device, const VkGraphicsPipelineCreateInfo& createInfo, VkPipelineCache cache = VK_NULL_HANDLE, const VkAllocationCallbacks* allocator = nullptr); + inline bool CreateCompute(Device& device, const VkComputePipelineCreateInfo& createInfo, VkPipelineCache cache = VK_NULL_HANDLE, const VkAllocationCallbacks* allocator = nullptr); + inline bool CreateGraphics(Device& device, const VkGraphicsPipelineCreateInfo& createInfo, VkPipelineCache cache = VK_NULL_HANDLE, const VkAllocationCallbacks* allocator = nullptr); inline void Destroy(); - inline const DeviceHandle& GetDevice() const; + inline Device& GetDevice() const; inline VkResult GetLastErrorCode() const; Pipeline& operator=(const Pipeline&) = delete; @@ -35,9 +35,9 @@ namespace Nz inline operator VkPipeline() const; protected: - inline bool Create(DeviceHandle device, VkResult result, const VkAllocationCallbacks* allocator); + inline bool Create(Device& device, VkResult result, const VkAllocationCallbacks* allocator); - DeviceHandle m_device; + MovablePtr m_device; VkAllocationCallbacks m_allocator; VkPipeline m_handle; mutable VkResult m_lastErrorCode; diff --git a/include/Nazara/VulkanRenderer/Wrapper/Pipeline.inl b/include/Nazara/VulkanRenderer/Wrapper/Pipeline.inl index 010437000..e91fe8107 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Pipeline.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/Pipeline.inl @@ -29,14 +29,14 @@ namespace Nz Destroy(); } - inline bool Pipeline::CreateCompute(const DeviceHandle& device, const VkComputePipelineCreateInfo& createInfo, VkPipelineCache cache, const VkAllocationCallbacks* allocator) + inline bool Pipeline::CreateCompute(Device& device, const VkComputePipelineCreateInfo& createInfo, VkPipelineCache cache, const VkAllocationCallbacks* allocator) { - return Create(std::move(device), device->vkCreateComputePipelines(*device, cache, 1U, &createInfo, allocator, &m_handle), allocator); + return Create(device, device.vkCreateComputePipelines(device, cache, 1U, &createInfo, allocator, &m_handle), allocator); } - inline bool Pipeline::CreateGraphics(const DeviceHandle& device, const VkGraphicsPipelineCreateInfo& createInfo, VkPipelineCache cache, const VkAllocationCallbacks* allocator) + inline bool Pipeline::CreateGraphics(Device& device, const VkGraphicsPipelineCreateInfo& createInfo, VkPipelineCache cache, const VkAllocationCallbacks* allocator) { - return Create(std::move(device), device->vkCreateGraphicsPipelines(*device, cache, 1U, &createInfo, allocator, &m_handle), allocator); + return Create(device, device.vkCreateGraphicsPipelines(device, cache, 1U, &createInfo, allocator, &m_handle), allocator); } inline void Pipeline::Destroy() @@ -48,9 +48,9 @@ namespace Nz } } - inline const DeviceHandle& Pipeline::GetDevice() const + inline Device& Pipeline::GetDevice() const { - return m_device; + return *m_device; } inline VkResult Pipeline::GetLastErrorCode() const @@ -63,9 +63,9 @@ namespace Nz return m_handle; } - inline bool Pipeline::Create(DeviceHandle device, VkResult result, const VkAllocationCallbacks* allocator) + inline bool Pipeline::Create(Device& device, VkResult result, const VkAllocationCallbacks* allocator) { - m_device = device; + m_device = &device; m_lastErrorCode = result; if (m_lastErrorCode != VkResult::VK_SUCCESS) { diff --git a/include/Nazara/VulkanRenderer/Wrapper/PipelineCache.hpp b/include/Nazara/VulkanRenderer/Wrapper/PipelineCache.hpp index 8fbd900eb..8c923c311 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/PipelineCache.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/PipelineCache.hpp @@ -28,8 +28,8 @@ namespace Nz PipelineCache& operator=(PipelineCache&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkPipelineCacheCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkPipelineCache* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkPipelineCache handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkPipelineCacheCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkPipelineCache* handle); + static inline void DestroyHelper(Device& device, VkPipelineCache handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/PipelineCache.inl b/include/Nazara/VulkanRenderer/Wrapper/PipelineCache.inl index a23d2c509..c16d8dbd8 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/PipelineCache.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/PipelineCache.inl @@ -9,14 +9,14 @@ namespace Nz { namespace Vk { - inline VkResult PipelineCache::CreateHelper(const DeviceHandle& device, const VkPipelineCacheCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkPipelineCache* handle) + inline VkResult PipelineCache::CreateHelper(Device& device, const VkPipelineCacheCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkPipelineCache* handle) { - return device->vkCreatePipelineCache(*device, createInfo, allocator, handle); + return device.vkCreatePipelineCache(device, createInfo, allocator, handle); } - inline void PipelineCache::DestroyHelper(const DeviceHandle& device, VkPipelineCache handle, const VkAllocationCallbacks* allocator) + inline void PipelineCache::DestroyHelper(Device& device, VkPipelineCache handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroyPipelineCache(*device, handle, allocator); + return device.vkDestroyPipelineCache(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/PipelineLayout.hpp b/include/Nazara/VulkanRenderer/Wrapper/PipelineLayout.hpp index 1a27d40a4..ad97ca925 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/PipelineLayout.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/PipelineLayout.hpp @@ -25,15 +25,15 @@ namespace Nz ~PipelineLayout() = default; using DeviceObject::Create; - bool Create(DeviceHandle device, VkDescriptorSetLayout layout, VkPipelineLayoutCreateFlags flags = 0); - bool Create(DeviceHandle device, UInt32 layoutCount, const VkDescriptorSetLayout* layouts, VkPipelineLayoutCreateFlags flags = 0); + bool Create(Device& device, VkDescriptorSetLayout layout, VkPipelineLayoutCreateFlags flags = 0); + bool Create(Device& device, UInt32 layoutCount, const VkDescriptorSetLayout* layouts, VkPipelineLayoutCreateFlags flags = 0); PipelineLayout& operator=(const PipelineLayout&) = delete; PipelineLayout& operator=(PipelineLayout&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkPipelineLayoutCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkPipelineLayout* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkPipelineLayout handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkPipelineLayoutCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkPipelineLayout* handle); + static inline void DestroyHelper(Device& device, VkPipelineLayout handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/PipelineLayout.inl b/include/Nazara/VulkanRenderer/Wrapper/PipelineLayout.inl index 3f0f52df1..96e1c57ac 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/PipelineLayout.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/PipelineLayout.inl @@ -9,12 +9,12 @@ namespace Nz { namespace Vk { - inline bool PipelineLayout::Create(DeviceHandle device, VkDescriptorSetLayout layout, VkPipelineLayoutCreateFlags flags) + inline bool PipelineLayout::Create(Device& device, VkDescriptorSetLayout layout, VkPipelineLayoutCreateFlags flags) { - return Create(std::move(device), 1U, &layout, flags); + return Create(device, 1U, &layout, flags); } - inline bool PipelineLayout::Create(DeviceHandle device, UInt32 layoutCount, const VkDescriptorSetLayout* layouts, VkPipelineLayoutCreateFlags flags) + inline bool PipelineLayout::Create(Device& device, UInt32 layoutCount, const VkDescriptorSetLayout* layouts, VkPipelineLayoutCreateFlags flags) { VkPipelineLayoutCreateInfo createInfo = { VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, @@ -26,17 +26,17 @@ namespace Nz nullptr }; - return Create(std::move(device), createInfo); + return Create(device, createInfo); } - inline VkResult PipelineLayout::CreateHelper(const DeviceHandle& device, const VkPipelineLayoutCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkPipelineLayout* handle) + inline VkResult PipelineLayout::CreateHelper(Device& device, const VkPipelineLayoutCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkPipelineLayout* handle) { - return device->vkCreatePipelineLayout(*device, createInfo, allocator, handle); + return device.vkCreatePipelineLayout(device, createInfo, allocator, handle); } - inline void PipelineLayout::DestroyHelper(const DeviceHandle& device, VkPipelineLayout handle, const VkAllocationCallbacks* allocator) + inline void PipelineLayout::DestroyHelper(Device& device, VkPipelineLayout handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroyPipelineLayout(*device, handle, allocator); + return device.vkDestroyPipelineLayout(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/Queue.hpp b/include/Nazara/VulkanRenderer/Wrapper/Queue.hpp index 9256441e7..9de7e73e2 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Queue.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/Queue.hpp @@ -19,12 +19,12 @@ namespace Nz { public: inline Queue(); - inline Queue(const DeviceHandle& device, VkQueue queue); + inline Queue(Device& device, VkQueue queue); inline Queue(const Queue& queue); inline Queue(Queue&& queue); inline ~Queue() = default; - inline const DeviceHandle& GetDevice() const; + inline Device& GetDevice() const; inline VkResult GetLastErrorCode() const; inline bool Present(const VkPresentInfoKHR& presentInfo) const; @@ -43,7 +43,7 @@ namespace Nz inline operator VkQueue(); protected: - DeviceHandle m_device; + Device* m_device; VkQueue m_handle; mutable VkResult m_lastErrorCode; }; diff --git a/include/Nazara/VulkanRenderer/Wrapper/Queue.inl b/include/Nazara/VulkanRenderer/Wrapper/Queue.inl index c22e73ab4..96465e993 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Queue.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/Queue.inl @@ -12,34 +12,21 @@ namespace Nz namespace Vk { inline Queue::Queue() : - Queue(DeviceHandle(), VK_NULL_HANDLE) + m_handle(VK_NULL_HANDLE), + m_lastErrorCode(VkResult::VK_SUCCESS) { } - inline Queue::Queue(const DeviceHandle& device, VkQueue queue) : - m_device(device), + inline Queue::Queue(Device& device, VkQueue queue) : + m_device(&device), m_handle(queue), m_lastErrorCode(VkResult::VK_SUCCESS) { } - inline Queue::Queue(const Queue& queue) : - m_device(queue.m_device), - m_handle(queue.m_handle), - m_lastErrorCode(queue.m_lastErrorCode) + inline Device& Queue::GetDevice() const { - } - - inline Queue::Queue(Queue&& queue) : - m_device(queue.m_device), - m_handle(queue.m_handle), - m_lastErrorCode(queue.m_lastErrorCode) - { - } - - inline const DeviceHandle& Queue::GetDevice() const - { - return m_device; + return *m_device; } inline VkResult Queue::GetLastErrorCode() const diff --git a/include/Nazara/VulkanRenderer/Wrapper/RenderPass.hpp b/include/Nazara/VulkanRenderer/Wrapper/RenderPass.hpp index b5b6ef58d..de95bfcca 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/RenderPass.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/RenderPass.hpp @@ -28,8 +28,8 @@ namespace Nz RenderPass& operator=(RenderPass&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkRenderPassCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkRenderPass* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkRenderPass handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkRenderPassCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkRenderPass* handle); + static inline void DestroyHelper(Device& device, VkRenderPass handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/RenderPass.inl b/include/Nazara/VulkanRenderer/Wrapper/RenderPass.inl index 16b27c5e7..583dfa248 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/RenderPass.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/RenderPass.inl @@ -9,14 +9,14 @@ namespace Nz { namespace Vk { - inline VkResult RenderPass::CreateHelper(const DeviceHandle& device, const VkRenderPassCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkRenderPass* handle) + inline VkResult RenderPass::CreateHelper(Device& device, const VkRenderPassCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkRenderPass* handle) { - return device->vkCreateRenderPass(*device, createInfo, allocator, handle); + return device.vkCreateRenderPass(device, createInfo, allocator, handle); } - inline void RenderPass::DestroyHelper(const DeviceHandle& device, VkRenderPass handle, const VkAllocationCallbacks* allocator) + inline void RenderPass::DestroyHelper(Device& device, VkRenderPass handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroyRenderPass(*device, handle, allocator); + return device.vkDestroyRenderPass(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/Semaphore.hpp b/include/Nazara/VulkanRenderer/Wrapper/Semaphore.hpp index f8f1592bb..b42cd8c27 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Semaphore.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/Semaphore.hpp @@ -25,14 +25,14 @@ namespace Nz ~Semaphore() = default; using DeviceObject::Create; - inline bool Create(DeviceHandle device, VkSemaphoreCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(Device& device, VkSemaphoreCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); Semaphore& operator=(const Semaphore&) = delete; Semaphore& operator=(Semaphore&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkSemaphoreCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSemaphore* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkSemaphore handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkSemaphoreCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSemaphore* handle); + static inline void DestroyHelper(Device& device, VkSemaphore handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/Semaphore.inl b/include/Nazara/VulkanRenderer/Wrapper/Semaphore.inl index 7a17c6d5e..334af2fe6 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Semaphore.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/Semaphore.inl @@ -9,7 +9,7 @@ namespace Nz { namespace Vk { - inline bool Semaphore::Create(DeviceHandle device, VkSemaphoreCreateFlags flags, const VkAllocationCallbacks* allocator) + inline bool Semaphore::Create(Device& device, VkSemaphoreCreateFlags flags, const VkAllocationCallbacks* allocator) { VkSemaphoreCreateInfo createInfo = { @@ -18,17 +18,17 @@ namespace Nz flags }; - return Create(std::move(device), createInfo, allocator); + return Create(device, createInfo, allocator); } - inline VkResult Semaphore::CreateHelper(const DeviceHandle& device, const VkSemaphoreCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSemaphore* handle) + inline VkResult Semaphore::CreateHelper(Device& device, const VkSemaphoreCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSemaphore* handle) { - return device->vkCreateSemaphore(*device, createInfo, allocator, handle); + return device.vkCreateSemaphore(device, createInfo, allocator, handle); } - inline void Semaphore::DestroyHelper(const DeviceHandle& device, VkSemaphore handle, const VkAllocationCallbacks* allocator) + inline void Semaphore::DestroyHelper(Device& device, VkSemaphore handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroySemaphore(*device, handle, allocator); + return device.vkDestroySemaphore(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/ShaderModule.hpp b/include/Nazara/VulkanRenderer/Wrapper/ShaderModule.hpp index a815db5fd..caacaccea 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/ShaderModule.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/ShaderModule.hpp @@ -25,14 +25,14 @@ namespace Nz ~ShaderModule() = default; using DeviceObject::Create; - inline bool Create(DeviceHandle device, const UInt32* code, std::size_t size, VkShaderModuleCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(Device& device, const UInt32* code, std::size_t size, VkShaderModuleCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); ShaderModule& operator=(const ShaderModule&) = delete; ShaderModule& operator=(ShaderModule&&) = delete; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkShaderModuleCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkShaderModule* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkShaderModule handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkShaderModuleCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkShaderModule* handle); + static inline void DestroyHelper(Device& device, VkShaderModule handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/ShaderModule.inl b/include/Nazara/VulkanRenderer/Wrapper/ShaderModule.inl index bce7a49b8..851e85916 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/ShaderModule.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/ShaderModule.inl @@ -9,7 +9,7 @@ namespace Nz { namespace Vk { - inline bool ShaderModule::Create(DeviceHandle device, const UInt32* code, std::size_t size, VkShaderModuleCreateFlags flags, const VkAllocationCallbacks* allocator) + inline bool ShaderModule::Create(Device& device, const UInt32* code, std::size_t size, VkShaderModuleCreateFlags flags, const VkAllocationCallbacks* allocator) { VkShaderModuleCreateInfo createInfo = { @@ -20,17 +20,17 @@ namespace Nz code }; - return Create(std::move(device), createInfo, allocator); + return Create(device, createInfo, allocator); } - inline VkResult ShaderModule::CreateHelper(const DeviceHandle& device, const VkShaderModuleCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkShaderModule* handle) + inline VkResult ShaderModule::CreateHelper(Device& device, const VkShaderModuleCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkShaderModule* handle) { - return device->vkCreateShaderModule(*device, createInfo, allocator, handle); + return device.vkCreateShaderModule(device, createInfo, allocator, handle); } - inline void ShaderModule::DestroyHelper(const DeviceHandle& device, VkShaderModule handle, const VkAllocationCallbacks* allocator) + inline void ShaderModule::DestroyHelper(Device& device, VkShaderModule handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroyShaderModule(*device, handle, allocator); + return device.vkDestroyShaderModule(device, handle, allocator); } } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/Swapchain.hpp b/include/Nazara/VulkanRenderer/Wrapper/Swapchain.hpp index 893f74913..d8d611585 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Swapchain.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/Swapchain.hpp @@ -29,7 +29,7 @@ namespace Nz inline bool AcquireNextImage(Nz::UInt64 timeout, VkSemaphore semaphore, VkFence fence, UInt32* imageIndex) const; - inline bool Create(DeviceHandle device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(Device& device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator = nullptr); inline const Buffer& GetBuffer(UInt32 index) const; inline const std::vector& GetBuffers() const; @@ -47,8 +47,8 @@ namespace Nz }; private: - static inline VkResult CreateHelper(const DeviceHandle& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle); - static inline void DestroyHelper(const DeviceHandle& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator); + static inline VkResult CreateHelper(Device& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle); + static inline void DestroyHelper(Device& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator); std::vector m_buffers; }; diff --git a/include/Nazara/VulkanRenderer/Wrapper/Swapchain.inl b/include/Nazara/VulkanRenderer/Wrapper/Swapchain.inl index c0e146d40..fff7991d6 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/Swapchain.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/Swapchain.inl @@ -29,9 +29,9 @@ namespace Nz } } - inline bool Swapchain::Create(DeviceHandle device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator) + inline bool Swapchain::Create(Device& device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator) { - if (!DeviceObject::Create(std::move(device), createInfo, allocator)) + if (!DeviceObject::Create(device, createInfo, allocator)) return false; UInt32 imageCount = 0; @@ -77,7 +77,7 @@ namespace Nz } }; - if (!m_buffers[i].view.Create(m_device, imageViewCreateInfo)) + if (!m_buffers[i].view.Create(*m_device, imageViewCreateInfo)) { NazaraError("Failed to create image view for image #" + String::Number(i)); return false; @@ -110,14 +110,14 @@ namespace Nz return true; } - inline VkResult Swapchain::CreateHelper(const DeviceHandle& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle) + inline VkResult Swapchain::CreateHelper(Device& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle) { - return device->vkCreateSwapchainKHR(*device, createInfo, allocator, handle); + return device.vkCreateSwapchainKHR(device, createInfo, allocator, handle); } - inline void Swapchain::DestroyHelper(const DeviceHandle& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator) + inline void Swapchain::DestroyHelper(Device& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator) { - return device->vkDestroySwapchainKHR(*device, handle, allocator); + return device.vkDestroySwapchainKHR(device, handle, allocator); } } } diff --git a/src/Nazara/VulkanRenderer/VkRenderWindow.cpp b/src/Nazara/VulkanRenderer/VkRenderWindow.cpp index 08b9b651e..f1c367d6e 100644 --- a/src/Nazara/VulkanRenderer/VkRenderWindow.cpp +++ b/src/Nazara/VulkanRenderer/VkRenderWindow.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -43,7 +44,7 @@ namespace Nz return true; } - bool VkRenderWindow::Create(RendererImpl* renderer, RenderSurface* surface, const Vector2ui& size, const RenderWindowParameters& parameters) + bool VkRenderWindow::Create(RendererImpl* /*renderer*/, RenderSurface* surface, const Vector2ui& size, const RenderWindowParameters& parameters) { m_physicalDevice = Vulkan::GetPhysicalDevices()[0].device; @@ -74,8 +75,6 @@ namespace Nz if (!parameters.depthFormats.empty()) { - const Vk::PhysicalDevice& deviceInfo = Vulkan::GetPhysicalDeviceInfo(m_physicalDevice); - for (PixelFormatType format : parameters.depthFormats) { switch (format) @@ -163,7 +162,7 @@ namespace Nz 1U // uint32_t layers; }; - if (!m_frameBuffers[i].Create(m_device, frameBufferCreate)) + if (!m_frameBuffers[i].Create(*m_device, frameBufferCreate)) { NazaraError("Failed to create framebuffer for image #" + String::Number(i)); return false; @@ -195,14 +194,14 @@ namespace Nz VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout initialLayout; }; - if (!m_depthBuffer.Create(m_device, imageCreateInfo)) + if (!m_depthBuffer.Create(*m_device, imageCreateInfo)) { NazaraError("Failed to create depth buffer"); return false; } VkMemoryRequirements memoryReq = m_depthBuffer.GetMemoryRequirements(); - if (!m_depthBufferMemory.Create(m_device, memoryReq.size, memoryReq.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) + if (!m_depthBufferMemory.Create(*m_device, memoryReq.size, memoryReq.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) { NazaraError("Failed to allocate depth buffer memory"); return false; @@ -236,7 +235,7 @@ namespace Nz } }; - if (!m_depthBufferView.Create(m_device, imageViewCreateInfo)) + if (!m_depthBufferView.Create(*m_device, imageViewCreateInfo)) { NazaraError("Failed to create depth buffer view"); return false; @@ -330,7 +329,7 @@ namespace Nz dependencies.data() // const VkSubpassDependency* pDependencies; }; - return m_renderPass.Create(m_device, createInfo); + return m_renderPass.Create(*m_device, createInfo); } bool VkRenderWindow::SetupSwapchain(Vk::Surface& surface, const Vector2ui& size) @@ -395,7 +394,7 @@ namespace Nz 0 }; - if (!m_swapchain.Create(m_device, swapchainInfo)) + if (!m_swapchain.Create(*m_device, swapchainInfo)) { NazaraError("Failed to create swapchain"); return false; diff --git a/src/Nazara/VulkanRenderer/Vulkan.cpp b/src/Nazara/VulkanRenderer/Vulkan.cpp index 344f50103..cb211ad01 100644 --- a/src/Nazara/VulkanRenderer/Vulkan.cpp +++ b/src/Nazara/VulkanRenderer/Vulkan.cpp @@ -444,13 +444,7 @@ namespace Nz { for (auto it = s_devices.begin(); it != s_devices.end();) { - auto devicePtr = it->lock(); - if (!devicePtr) - { - it = s_devices.erase(it); - continue; - } - + const auto& devicePtr = *it; if (devicePtr->GetPhysicalDevice() == gpu) return devicePtr; } @@ -463,13 +457,7 @@ namespace Nz // First, try to find a device compatible with that surface for (auto it = s_devices.begin(); it != s_devices.end();) { - auto devicePtr = it->lock(); - if (!devicePtr) - { - it = s_devices.erase(it); - continue; - } - + const auto& devicePtr = *it; if (devicePtr->GetPhysicalDevice() == gpu) { const std::vector& queueFamilyInfo = devicePtr->GetEnabledQueues(); @@ -511,7 +499,7 @@ namespace Nz Vk::Loader::Uninitialize(); } - std::vector> Vulkan::s_devices; + std::vector> Vulkan::s_devices; std::vector Vulkan::s_physDevices; Vk::Instance Vulkan::s_instance; ParameterList Vulkan::s_initializationParameters; diff --git a/src/Nazara/VulkanRenderer/VulkanDevice.cpp b/src/Nazara/VulkanRenderer/VulkanDevice.cpp index f1c532846..252d7436b 100644 --- a/src/Nazara/VulkanRenderer/VulkanDevice.cpp +++ b/src/Nazara/VulkanRenderer/VulkanDevice.cpp @@ -12,20 +12,20 @@ namespace Nz { VulkanDevice::~VulkanDevice() = default; - std::unique_ptr VulkanDevice::InstantiateBuffer(Buffer* parent, BufferType type) + std::unique_ptr VulkanDevice::InstantiateBuffer(BufferType type) { - return std::make_unique(shared_from_this(), parent, type); + return std::make_unique(*this, type); } std::unique_ptr VulkanDevice::InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) { - return std::make_unique(shared_from_this(), std::move(pipelineInfo)); + return std::make_unique(*this, std::move(pipelineInfo)); } std::shared_ptr VulkanDevice::InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo) { auto pipelineLayout = std::make_shared(); - if (!pipelineLayout->Create(shared_from_this(), std::move(pipelineLayoutInfo))) + if (!pipelineLayout->Create(*this, std::move(pipelineLayoutInfo))) return {}; return pipelineLayout; @@ -34,7 +34,7 @@ namespace Nz std::shared_ptr VulkanDevice::InstantiateShaderStage(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize) { auto stage = std::make_shared(); - if (!stage->Create(shared_from_this(), type, lang, source, sourceSize)) + if (!stage->Create(*this, type, lang, source, sourceSize)) return {}; return stage; diff --git a/src/Nazara/VulkanRenderer/VulkanRenderPipeline.cpp b/src/Nazara/VulkanRenderer/VulkanRenderPipeline.cpp index 93a05ebcc..d7b790aec 100644 --- a/src/Nazara/VulkanRenderer/VulkanRenderPipeline.cpp +++ b/src/Nazara/VulkanRenderer/VulkanRenderPipeline.cpp @@ -12,8 +12,8 @@ namespace Nz { - VulkanRenderPipeline::VulkanRenderPipeline(Vk::DeviceHandle device, RenderPipelineInfo pipelineInfo) : - m_device(std::move(device)), + VulkanRenderPipeline::VulkanRenderPipeline(Vk::Device& device, RenderPipelineInfo pipelineInfo) : + m_device(&device), m_pipelineInfo(std::move(pipelineInfo)) { m_pipelineCreateInfo = BuildCreateInfo(m_pipelineInfo); @@ -29,7 +29,7 @@ namespace Nz pipelineCreateInfo.renderPass = renderPass; Vk::Pipeline newPipeline; - if (!newPipeline.CreateGraphics(m_device, pipelineCreateInfo)) + if (!newPipeline.CreateGraphics(*m_device, pipelineCreateInfo)) return VK_NULL_HANDLE; auto it = m_pipelines.emplace(renderPass, std::move(newPipeline)).first; diff --git a/src/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.cpp b/src/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.cpp index d509ff043..b8d4704de 100644 --- a/src/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.cpp +++ b/src/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.cpp @@ -12,9 +12,9 @@ namespace Nz { - bool VulkanRenderPipelineLayout::Create(Vk::DeviceHandle device, RenderPipelineLayoutInfo layoutInfo) + bool VulkanRenderPipelineLayout::Create(Vk::Device& device, RenderPipelineLayoutInfo layoutInfo) { - m_device = std::move(device); + m_device = &device; m_layoutInfo = std::move(layoutInfo); StackVector layoutBindings = NazaraStackVector(VkDescriptorSetLayoutBinding, m_layoutInfo.bindings.size()); @@ -28,10 +28,10 @@ namespace Nz layoutBinding.stageFlags = ToVulkan(bindingInfo.shaderStageFlags); } - if (!m_descriptorSetLayout.Create(m_device, layoutBindings.size(), layoutBindings.data())) + if (!m_descriptorSetLayout.Create(*m_device, UInt32(layoutBindings.size()), layoutBindings.data())) return false; - if (!m_pipelineLayout.Create(m_device, m_descriptorSetLayout)) + if (!m_pipelineLayout.Create(*m_device, m_descriptorSetLayout)) return false; return true; diff --git a/src/Nazara/VulkanRenderer/VulkanRenderer.cpp b/src/Nazara/VulkanRenderer/VulkanRenderer.cpp index 7e543c256..bfacf89bc 100644 --- a/src/Nazara/VulkanRenderer/VulkanRenderer.cpp +++ b/src/Nazara/VulkanRenderer/VulkanRenderer.cpp @@ -73,7 +73,7 @@ namespace Nz for (const Vk::PhysicalDevice& physDevice : m_physDevices) { - RenderDeviceInfo device; + RenderDeviceInfo& device = devices.emplace_back(); device.name = physDevice.properties.deviceName; switch (physDevice.properties.deviceType) @@ -100,8 +100,6 @@ namespace Nz device.type = RenderDeviceType::Unknown; break; } - - devices.emplace_back(std::move(device)); } return devices; diff --git a/src/Nazara/VulkanRenderer/VulkanShaderStage.cpp b/src/Nazara/VulkanRenderer/VulkanShaderStage.cpp index ea95dd0ac..238bb6468 100644 --- a/src/Nazara/VulkanRenderer/VulkanShaderStage.cpp +++ b/src/Nazara/VulkanRenderer/VulkanShaderStage.cpp @@ -7,7 +7,7 @@ namespace Nz { - bool VulkanShaderStage::Create(const Vk::DeviceHandle& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize) + bool VulkanShaderStage::Create(Vk::Device& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize) { if (lang != ShaderLanguage::SpirV) { diff --git a/src/Nazara/VulkanRenderer/Wrapper/Device.cpp b/src/Nazara/VulkanRenderer/Wrapper/Device.cpp index 8f15e73ba..65aa4c463 100644 --- a/src/Nazara/VulkanRenderer/Wrapper/Device.cpp +++ b/src/Nazara/VulkanRenderer/Wrapper/Device.cpp @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include #include #include @@ -12,6 +13,28 @@ namespace Nz { namespace Vk { + Device::Device(Instance& instance) : + m_instance(instance), + m_physicalDevice(nullptr), + m_device(VK_NULL_HANDLE) + { + } + + Device::~Device() + { + if (m_device != VK_NULL_HANDLE) + WaitAndDestroyDevice(); + } + + void Device::Destroy() + { + if (m_device != VK_NULL_HANDLE) + { + WaitAndDestroyDevice(); + ResetPointers(); + } + } + bool Device::Create(const Vk::PhysicalDevice& deviceInfo, const VkDeviceCreateInfo& createInfo, const VkAllocationCallbacks* allocator) { m_lastErrorCode = m_instance.vkCreateDevice(deviceInfo.device, &createInfo, allocator, &m_device); @@ -21,6 +44,8 @@ namespace Nz return false; } + CallOnExit destroyOnFailure([this] { Destroy(); }); + m_physicalDevice = &deviceInfo; // Store the allocator to access them when needed @@ -53,7 +78,7 @@ namespace Nz } catch (const std::exception& e) { - NazaraError(String("Failed to query device function: ") + e.what()); + NazaraError(std::string("Failed to query device function: ") + e.what()); return false; } @@ -88,6 +113,8 @@ namespace Nz for (const QueueFamilyInfo& familyInfo : m_enabledQueuesInfos) m_queuesByFamily[familyInfo.familyIndex] = &familyInfo.queues; + destroyOnFailure.Reset(); + return true; } @@ -96,8 +123,37 @@ namespace Nz VkQueue queue; vkGetDeviceQueue(m_device, queueFamilyIndex, queueIndex, &queue); - return Queue(shared_from_this(), queue); + return Queue(*this, queue); } + void Device::WaitAndDestroyDevice() + { + assert(m_device != VK_NULL_HANDLE); + + if (vkDeviceWaitIdle) + vkDeviceWaitIdle(m_device); + + m_internalData.reset(); + + if (vkDestroyDevice) + vkDestroyDevice(m_device, (m_allocator.pfnAllocation) ? &m_allocator : nullptr); + } + + void Device::ResetPointers() + { + m_device = VK_NULL_HANDLE; + m_physicalDevice = nullptr; + + // Reset functions pointers +#define NAZARA_VULKANRENDERER_DEVICE_EXT_BEGIN(ext) +#define NAZARA_VULKANRENDERER_DEVICE_EXT_END() +#define NAZARA_VULKANRENDERER_DEVICE_FUNCTION(func) func = nullptr; + +#include + +#undef NAZARA_VULKANRENDERER_DEVICE_EXT_BEGIN +#undef NAZARA_VULKANRENDERER_DEVICE_EXT_END +#undef NAZARA_VULKANRENDERER_DEVICE_FUNCTION + } } } diff --git a/src/Nazara/VulkanRenderer/Wrapper/Instance.cpp b/src/Nazara/VulkanRenderer/Wrapper/Instance.cpp index c46028b15..e7b0330d7 100644 --- a/src/Nazara/VulkanRenderer/Wrapper/Instance.cpp +++ b/src/Nazara/VulkanRenderer/Wrapper/Instance.cpp @@ -103,5 +103,20 @@ namespace Nz return true; } + void Instance::ResetPointers() + { + assert(m_instance != VK_NULL_HANDLE); + m_instance = VK_NULL_HANDLE; + +#define NAZARA_VULKANRENDERER_INSTANCE_EXT_BEGIN(ext) +#define NAZARA_VULKANRENDERER_INSTANCE_EXT_END() +#define NAZARA_VULKANRENDERER_INSTANCE_FUNCTION(func) func = nullptr; + +#include + +#undef NAZARA_VULKANRENDERER_INSTANCE_EXT_BEGIN +#undef NAZARA_VULKANRENDERER_INSTANCE_EXT_END +#undef NAZARA_VULKANRENDERER_INSTANCE_FUNCTION + } } }