diff --git a/include/Nazara/VulkanRenderer/Wrapper.hpp b/include/Nazara/VulkanRenderer/Wrapper.hpp index 1ae7ed140..846542a50 100644 --- a/include/Nazara/VulkanRenderer/Wrapper.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper.hpp @@ -5,6 +5,7 @@ #ifndef NAZARA_GLOBAL_VULKANRENDERER_WRAPPER_HPP #define NAZARA_GLOBAL_VULKANRENDERER_WRAPPER_HPP +#include #include #include #include diff --git a/include/Nazara/VulkanRenderer/Wrapper/AutoFree.hpp b/include/Nazara/VulkanRenderer/Wrapper/AutoFree.hpp new file mode 100644 index 000000000..68ce8b48a --- /dev/null +++ b/include/Nazara/VulkanRenderer/Wrapper/AutoFree.hpp @@ -0,0 +1,42 @@ +// 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_VKAUTOFREE_HPP +#define NAZARA_VULKANRENDERER_VKAUTOFREE_HPP + +#include + +namespace Nz::Vk +{ + template + class AutoFree + { + public: + template AutoFree(Args&&... args); + AutoFree(const AutoFree&) = default; + AutoFree(AutoFree&&) = default; + ~AutoFree(); + + T& Get(); + const T& Get() const; + + T* operator->(); + const T* operator->() const; + + operator T&(); + operator const T&() const; + + AutoFree& operator=(const AutoFree&) = delete; + AutoFree& operator=(AutoFree&&) = default; + + private: + T m_object; + }; +} + +#include + +#endif // NAZARA_VULKANRENDERER_VKAUTOFREE_HPP diff --git a/include/Nazara/VulkanRenderer/Wrapper/AutoFree.inl b/include/Nazara/VulkanRenderer/Wrapper/AutoFree.inl new file mode 100644 index 000000000..8e0781406 --- /dev/null +++ b/include/Nazara/VulkanRenderer/Wrapper/AutoFree.inl @@ -0,0 +1,60 @@ +// 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 +#include + +namespace Nz::Vk +{ + template + template + AutoFree::AutoFree(Args&&... args) : + m_object(std::forward(args)...) + { + } + + template + AutoFree::~AutoFree() + { + m_object.Free(); + } + + template + T& AutoFree::Get() + { + return m_object; + } + + template + const T& AutoFree::Get() const + { + return m_object; + } + + template + T* AutoFree::operator->() + { + return &m_object; + } + + template + const T* AutoFree::operator->() const + { + return &m_object; + } + + template + AutoFree::operator T&() + { + return Get(); + } + + template + AutoFree::operator const T&() const + { + return Get(); + } +} + +#include diff --git a/include/Nazara/VulkanRenderer/Wrapper/CommandBuffer.hpp b/include/Nazara/VulkanRenderer/Wrapper/CommandBuffer.hpp index 52e457236..8e058256a 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/CommandBuffer.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/CommandBuffer.hpp @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -24,7 +25,7 @@ namespace Nz inline CommandBuffer(); CommandBuffer(const CommandBuffer&) = delete; inline CommandBuffer(CommandBuffer&& commandBuffer); - inline ~CommandBuffer(); + ~CommandBuffer() = default; inline bool Begin(const VkCommandBufferBeginInfo& info); inline bool Begin(VkCommandBufferUsageFlags flags = 0); @@ -90,10 +91,16 @@ namespace Nz inline CommandBuffer(CommandPool& pool, VkCommandBuffer handle); CommandPool* m_pool; - VkAllocationCallbacks m_allocator; VkCommandBuffer m_handle; VkResult m_lastErrorCode; + }; + class AutoCommandBuffer : public AutoFree + { + public: + using AutoFree::AutoFree; + + operator VkCommandBuffer() const { return Get(); } }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/CommandBuffer.inl b/include/Nazara/VulkanRenderer/Wrapper/CommandBuffer.inl index 00f3c9f31..f504a0df8 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/CommandBuffer.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/CommandBuffer.inl @@ -32,11 +32,6 @@ namespace Nz commandBuffer.m_handle = VK_NULL_HANDLE; } - inline CommandBuffer::~CommandBuffer() - { - Free(); - } - inline bool CommandBuffer::Begin(const VkCommandBufferBeginInfo& info) { m_lastErrorCode = m_pool->GetDevice()->vkBeginCommandBuffer(m_handle, &info); diff --git a/include/Nazara/VulkanRenderer/Wrapper/DescriptorSet.hpp b/include/Nazara/VulkanRenderer/Wrapper/DescriptorSet.hpp index 24eb8c6ed..55da61cf5 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/DescriptorSet.hpp +++ b/include/Nazara/VulkanRenderer/Wrapper/DescriptorSet.hpp @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -24,7 +25,7 @@ namespace Nz inline DescriptorSet(); DescriptorSet(const DescriptorSet&) = delete; inline DescriptorSet(DescriptorSet&& descriptorSet) noexcept; - inline ~DescriptorSet(); + ~DescriptorSet() = default; inline void Free(); @@ -56,6 +57,15 @@ namespace Nz DescriptorPool* m_pool; VkDescriptorSet m_handle; }; + + class AutoDescriptorSet : public AutoFree + { + public: + using AutoFree::AutoFree; + + explicit operator bool() const { return Get(); } + operator VkDescriptorSet() const { return Get(); } + }; } } diff --git a/include/Nazara/VulkanRenderer/Wrapper/DescriptorSet.inl b/include/Nazara/VulkanRenderer/Wrapper/DescriptorSet.inl index 3932fdfcf..d6712c1b4 100644 --- a/include/Nazara/VulkanRenderer/Wrapper/DescriptorSet.inl +++ b/include/Nazara/VulkanRenderer/Wrapper/DescriptorSet.inl @@ -31,11 +31,6 @@ namespace Nz descriptorSet.m_handle = VK_NULL_HANDLE; } - inline DescriptorSet::~DescriptorSet() - { - Free(); - } - inline void DescriptorSet::Free() { if (m_handle)