diff --git a/include/Nazara/Vulkan/VkCommandBuffer.inl b/include/Nazara/Vulkan/VkCommandBuffer.inl index d2d6f50d7..c1d0ec9bc 100644 --- a/include/Nazara/Vulkan/VkCommandBuffer.inl +++ b/include/Nazara/Vulkan/VkCommandBuffer.inl @@ -2,7 +2,7 @@ // This file is part of the "Nazara Engine - Vulkan" // For conditions of distribution and use, see copyright notice in Config.hpp -#include +#include #include #include #include @@ -33,7 +33,7 @@ namespace Nz inline bool CommandBuffer::Begin(const VkCommandBufferBeginInfo& info) { - m_lastErrorCode = m_pool->GetDevice().vkBeginCommandBuffer(m_handle, &info); + m_lastErrorCode = m_pool->GetDevice()->vkBeginCommandBuffer(m_handle, &info); if (m_lastErrorCode != VkResult::VK_SUCCESS) { NazaraError("Failed to begin command buffer"); @@ -119,7 +119,7 @@ namespace Nz inline bool CommandBuffer::End() { - m_lastErrorCode = m_pool->GetDevice().vkEndCommandBuffer(m_handle); + m_lastErrorCode = m_pool->GetDevice()->vkEndCommandBuffer(m_handle); if (m_lastErrorCode != VkResult::VK_SUCCESS) { NazaraError("Failed to end command buffer"); @@ -132,7 +132,7 @@ namespace Nz inline void CommandBuffer::Free() { if (m_handle) - m_pool->GetDevice().vkFreeCommandBuffers(m_pool->GetDevice(), *m_pool, 1, &m_handle); + m_pool->GetDevice()->vkFreeCommandBuffers(*m_pool->GetDevice(), *m_pool, 1, &m_handle); } inline VkResult CommandBuffer::GetLastErrorCode() const diff --git a/include/Nazara/Vulkan/VkCommandPool.hpp b/include/Nazara/Vulkan/VkCommandPool.hpp index b51061bb2..1884e2b6c 100644 --- a/include/Nazara/Vulkan/VkCommandPool.hpp +++ b/include/Nazara/Vulkan/VkCommandPool.hpp @@ -25,7 +25,7 @@ namespace Nz friend DeviceObject; public: - inline CommandPool(Device& instance); + CommandPool() = default; CommandPool(const CommandPool&) = delete; CommandPool(CommandPool&&) = default; ~CommandPool() = default; @@ -34,7 +34,7 @@ namespace Nz std::vector AllocateCommandBuffers(UInt32 commandBufferCount, VkCommandBufferLevel level); using DeviceObject::Create; - inline bool Create(UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(const DeviceHandle& device, UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); inline bool Reset(VkCommandPoolResetFlags flags); @@ -42,8 +42,8 @@ namespace Nz CommandPool& operator=(CommandPool&&) = delete; private: - 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); + 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); }; } } diff --git a/include/Nazara/Vulkan/VkCommandPool.inl b/include/Nazara/Vulkan/VkCommandPool.inl index 61cfeaa43..b47b3481f 100644 --- a/include/Nazara/Vulkan/VkCommandPool.inl +++ b/include/Nazara/Vulkan/VkCommandPool.inl @@ -11,12 +11,7 @@ namespace Nz { namespace Vk { - inline CommandPool::CommandPool(Device& device) : - DeviceObject(device) - { - } - - inline bool CommandPool::Create(UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags, const VkAllocationCallbacks* allocator) + inline bool CommandPool::Create(const DeviceHandle& device, UInt32 queueFamilyIndex, VkCommandPoolCreateFlags flags, const VkAllocationCallbacks* allocator) { VkCommandPoolCreateInfo createInfo = { @@ -26,26 +21,26 @@ namespace Nz queueFamilyIndex }; - return Create(createInfo, allocator); + return Create(device, createInfo, allocator); } inline bool CommandPool::Reset(VkCommandPoolResetFlags flags) { - m_lastErrorCode = m_device.vkResetCommandPool(m_device, m_handle, flags); + m_lastErrorCode = m_device->vkResetCommandPool(*m_device, m_handle, flags); if (m_lastErrorCode != VkResult::VK_SUCCESS) return false; return true; } - inline VkResult CommandPool::CreateHelper(Device& device, const VkCommandPoolCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkCommandPool* handle) + inline VkResult CommandPool::CreateHelper(const DeviceHandle& 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(Device& device, VkCommandPool handle, const VkAllocationCallbacks* allocator) + inline void CommandPool::DestroyHelper(const DeviceHandle& device, VkCommandPool handle, const VkAllocationCallbacks* allocator) { - return device.vkDestroyCommandPool(device, handle, allocator); + return device->vkDestroyCommandPool(*device, handle, allocator); } } } diff --git a/include/Nazara/Vulkan/VkDevice.inl b/include/Nazara/Vulkan/VkDevice.inl index 81b784ae2..a42bd8a87 100644 --- a/include/Nazara/Vulkan/VkDevice.inl +++ b/include/Nazara/Vulkan/VkDevice.inl @@ -14,7 +14,7 @@ namespace Nz { inline Device::Device(Instance& instance) : m_instance(instance), - m_device(nullptr) + m_device(VK_NULL_HANDLE), { } @@ -25,12 +25,12 @@ namespace Nz inline void Device::Destroy() { - if (m_device) + if (m_device != VK_NULL_HANDLE) { vkDeviceWaitIdle(m_device); vkDestroyDevice(m_device, (m_allocator.pfnAllocation) ? &m_allocator : nullptr); - m_device = nullptr; + m_device = VK_NULL_HANDLE; } } @@ -39,7 +39,7 @@ namespace Nz VkQueue queue; vkGetDeviceQueue(m_device, queueFamilyIndex, queueIndex, &queue); - return Queue(*this, queue); + return Queue(CreateHandle(), queue); } inline Instance& Device::GetInstance() diff --git a/include/Nazara/Vulkan/VkDeviceObject.hpp b/include/Nazara/Vulkan/VkDeviceObject.hpp index dd9da38c9..49d70171f 100644 --- a/include/Nazara/Vulkan/VkDeviceObject.hpp +++ b/include/Nazara/Vulkan/VkDeviceObject.hpp @@ -19,16 +19,15 @@ namespace Nz class DeviceObject { public: - inline DeviceObject(Device& instance); + inline DeviceObject(); DeviceObject(const DeviceObject&) = delete; DeviceObject(DeviceObject&&); inline ~DeviceObject(); - inline bool Create(const CreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(const DeviceHandle& device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr); inline void Destroy(); - inline Device& GetDevice(); - inline const Device& GetDevice() const; + inline const DeviceHandle& GetDevice() const; inline VkResult GetLastErrorCode() const; DeviceObject& operator=(const DeviceObject&) = delete; @@ -37,7 +36,7 @@ namespace Nz inline operator VkType(); protected: - Device& m_device; + DeviceHandle m_device; VkAllocationCallbacks m_allocator; VkType m_handle; VkResult m_lastErrorCode; diff --git a/include/Nazara/Vulkan/VkDeviceObject.inl b/include/Nazara/Vulkan/VkDeviceObject.inl index 24499a7d0..8bd9eebe6 100644 --- a/include/Nazara/Vulkan/VkDeviceObject.inl +++ b/include/Nazara/Vulkan/VkDeviceObject.inl @@ -12,15 +12,14 @@ namespace Nz namespace Vk { template - inline DeviceObject::DeviceObject(Device& device) : - m_device(device), + inline DeviceObject::DeviceObject() : m_handle(VK_NULL_HANDLE) { } template inline DeviceObject::DeviceObject(DeviceObject&& object) : - m_device(object.m_device), + m_device(std::move(object.m_device)), m_allocator(object.m_allocator), m_handle(object.m_handle), m_lastErrorCode(object.m_lastErrorCode) @@ -35,8 +34,9 @@ namespace Nz } template - inline bool DeviceObject::Create(const CreateInfo& createInfo, const VkAllocationCallbacks* allocator) + inline bool DeviceObject::Create(const DeviceHandle& device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator) { + m_device = device; m_lastErrorCode = C::CreateHelper(m_device, &createInfo, allocator, &m_handle); if (m_lastErrorCode != VkResult::VK_SUCCESS) { @@ -64,13 +64,7 @@ namespace Nz } template - inline Device& DeviceObject::GetDevice() - { - return m_device; - } - - template - inline const Device& DeviceObject::GetDevice() const + inline const DeviceHandle& DeviceObject::GetDevice() const { return m_device; } diff --git a/include/Nazara/Vulkan/VkQueue.hpp b/include/Nazara/Vulkan/VkQueue.hpp index 35c8d16e0..b6253bc32 100644 --- a/include/Nazara/Vulkan/VkQueue.hpp +++ b/include/Nazara/Vulkan/VkQueue.hpp @@ -18,12 +18,12 @@ namespace Nz class Queue { public: - inline Queue(Device& device, VkQueue queue); + inline Queue(const DeviceHandle& device, VkQueue queue); inline Queue(const Queue& queue); inline Queue(Queue&& queue); inline ~Queue() = default; - inline Device& GetDevice(); + inline const DeviceHandle& GetDevice() const; inline VkResult GetLastErrorCode() const; inline bool Present(const VkPresentInfoKHR& presentInfo); @@ -40,7 +40,7 @@ namespace Nz inline operator VkQueue(); protected: - Device& m_device; + DeviceHandle m_device; VkQueue m_handle; VkResult m_lastErrorCode; }; diff --git a/include/Nazara/Vulkan/VkQueue.inl b/include/Nazara/Vulkan/VkQueue.inl index a47918da3..89273958f 100644 --- a/include/Nazara/Vulkan/VkQueue.inl +++ b/include/Nazara/Vulkan/VkQueue.inl @@ -2,7 +2,7 @@ // This file is part of the "Nazara Engine - Vulkan" // For conditions of distribution and use, see copyright notice in Config.hpp -#include +#include #include #include #include @@ -11,7 +11,7 @@ namespace Nz { namespace Vk { - inline Queue::Queue(Device& device, VkQueue queue) : + inline Queue::Queue(const DeviceHandle& device, VkQueue queue) : m_device(device), m_handle(queue), m_lastErrorCode(VkResult::VK_SUCCESS) @@ -32,7 +32,7 @@ namespace Nz { } - inline Device& Queue::GetDevice() + inline const DeviceHandle& Queue::GetDevice() const { return m_device; } @@ -44,7 +44,7 @@ namespace Nz inline bool Queue::Present(const VkPresentInfoKHR& presentInfo) { - m_lastErrorCode = m_device.vkQueuePresentKHR(m_handle, &presentInfo); + m_lastErrorCode = m_device->vkQueuePresentKHR(m_handle, &presentInfo); if (m_lastErrorCode != VkResult::VK_SUCCESS) return false; @@ -75,7 +75,7 @@ namespace Nz inline bool Queue::Submit(UInt32 submitCount, const VkSubmitInfo* submits, VkFence fence) { - m_lastErrorCode = m_device.vkQueueSubmit(m_handle, submitCount, submits, fence); + m_lastErrorCode = m_device->vkQueueSubmit(m_handle, submitCount, submits, fence); if (m_lastErrorCode != VkResult::VK_SUCCESS) return false; @@ -84,7 +84,7 @@ namespace Nz inline bool Queue::WaitIdle() { - m_lastErrorCode = m_device.vkQueueWaitIdle(m_handle); + m_lastErrorCode = m_device->vkQueueWaitIdle(m_handle); if (m_lastErrorCode != VkResult::VK_SUCCESS) return false; diff --git a/include/Nazara/Vulkan/VkSemaphore.hpp b/include/Nazara/Vulkan/VkSemaphore.hpp index 138d726be..9fdd7d191 100644 --- a/include/Nazara/Vulkan/VkSemaphore.hpp +++ b/include/Nazara/Vulkan/VkSemaphore.hpp @@ -19,20 +19,20 @@ namespace Nz friend DeviceObject; public: - inline Semaphore(Device& instance); + Semaphore() = default; Semaphore(const Semaphore&) = delete; Semaphore(Semaphore&&) = default; ~Semaphore() = default; using DeviceObject::Create; - inline bool Create(VkSemaphoreCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(const DeviceHandle& device, VkSemaphoreCreateFlags flags = 0, const VkAllocationCallbacks* allocator = nullptr); Semaphore& operator=(const Semaphore&) = delete; Semaphore& operator=(Semaphore&&) = delete; private: - static VkResult CreateHelper(Device& device, const VkSemaphoreCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSemaphore* handle); - static void DestroyHelper(Device& device, VkSemaphore handle, const VkAllocationCallbacks* allocator); + static VkResult CreateHelper(const DeviceHandle& device, const VkSemaphoreCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSemaphore* handle); + static void DestroyHelper(const DeviceHandle& device, VkSemaphore handle, const VkAllocationCallbacks* allocator); }; } } diff --git a/include/Nazara/Vulkan/VkSemaphore.inl b/include/Nazara/Vulkan/VkSemaphore.inl index fd2bf3427..b423fd4c0 100644 --- a/include/Nazara/Vulkan/VkSemaphore.inl +++ b/include/Nazara/Vulkan/VkSemaphore.inl @@ -9,12 +9,7 @@ namespace Nz { namespace Vk { - inline Semaphore::Semaphore(Device& device) : - DeviceObject(device) - { - } - - inline bool Semaphore::Create(VkSemaphoreCreateFlags flags, const VkAllocationCallbacks* allocator) + inline bool Semaphore::Create(const DeviceHandle& device, VkSemaphoreCreateFlags flags, const VkAllocationCallbacks* allocator) { VkSemaphoreCreateInfo createInfo = { @@ -23,17 +18,17 @@ namespace Nz flags }; - return Create(createInfo, allocator); + return Create(device, createInfo, allocator); } - VkResult Semaphore::CreateHelper(Device& device, const VkSemaphoreCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSemaphore* handle) + VkResult Semaphore::CreateHelper(const DeviceHandle& device, const VkSemaphoreCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSemaphore* handle) { - return device.vkCreateSemaphore(device, createInfo, allocator, handle); + return device->vkCreateSemaphore(*device, createInfo, allocator, handle); } - void Semaphore::DestroyHelper(Device& device, VkSemaphore handle, const VkAllocationCallbacks* allocator) + void Semaphore::DestroyHelper(const DeviceHandle& device, VkSemaphore handle, const VkAllocationCallbacks* allocator) { - return device.vkDestroySemaphore(device, handle, allocator); + return device->vkDestroySemaphore(*device, handle, allocator); } } } diff --git a/include/Nazara/Vulkan/VkSwapchain.hpp b/include/Nazara/Vulkan/VkSwapchain.hpp index 374ad88e6..fbe8d2118 100644 --- a/include/Nazara/Vulkan/VkSwapchain.hpp +++ b/include/Nazara/Vulkan/VkSwapchain.hpp @@ -19,14 +19,14 @@ namespace Nz friend DeviceObject; public: - inline Swapchain(Device& instance); + Swapchain() = default; Swapchain(const Swapchain&) = delete; Swapchain(Swapchain&&) = default; ~Swapchain() = default; inline bool AcquireNextImage(Nz::UInt64 timeout, VkSemaphore semaphore, VkFence fence, UInt32* imageIndex); - inline bool Create(const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator = nullptr); + inline bool Create(const DeviceHandle& device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator = nullptr); inline VkImage GetImage(UInt32 index) const; inline const std::vector& GetImages() const; @@ -38,8 +38,8 @@ namespace Nz Swapchain& operator=(Swapchain&&) = delete; private: - static VkResult CreateHelper(Device& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle); - static void DestroyHelper(Device& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator); + static VkResult CreateHelper(const DeviceHandle& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle); + static void DestroyHelper(const DeviceHandle& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator); std::vector m_images; }; diff --git a/include/Nazara/Vulkan/VkSwapchain.inl b/include/Nazara/Vulkan/VkSwapchain.inl index 8865012e4..e1f885045 100644 --- a/include/Nazara/Vulkan/VkSwapchain.inl +++ b/include/Nazara/Vulkan/VkSwapchain.inl @@ -11,14 +11,9 @@ namespace Nz { namespace Vk { - inline Swapchain::Swapchain(Device& device) : - DeviceObject(device) - { - } - inline bool Swapchain::AcquireNextImage(Nz::UInt64 timeout, VkSemaphore semaphore, VkFence fence, UInt32* imageIndex) { - m_lastErrorCode = m_device.vkAcquireNextImageKHR(m_device, m_handle, timeout, semaphore, fence, imageIndex); + m_lastErrorCode = m_device->vkAcquireNextImageKHR(*m_device, m_handle, timeout, semaphore, fence, imageIndex); switch (m_lastErrorCode) { case VkResult::VK_SUBOPTIMAL_KHR: @@ -30,13 +25,13 @@ namespace Nz } } - inline bool Swapchain::Create(const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator) + inline bool Swapchain::Create(const DeviceHandle& device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator) { - if (!DeviceObject::Create(createInfo, allocator)) + if (!DeviceObject::Create(device, createInfo, allocator)) return false; UInt32 imageCount = 0; - m_lastErrorCode = m_device.vkGetSwapchainImagesKHR(m_device, m_handle, &imageCount, nullptr); + m_lastErrorCode = m_device->vkGetSwapchainImagesKHR(*m_device, m_handle, &imageCount, nullptr); if (m_lastErrorCode != VkResult::VK_SUCCESS || imageCount == 0) { NazaraError("Failed to query swapchain image count"); @@ -44,7 +39,7 @@ namespace Nz } m_images.resize(imageCount); - m_lastErrorCode = m_device.vkGetSwapchainImagesKHR(m_device, m_handle, &imageCount, m_images.data()); + m_lastErrorCode = m_device->vkGetSwapchainImagesKHR(*m_device, m_handle, &imageCount, m_images.data()); if (m_lastErrorCode != VkResult::VK_SUCCESS) { NazaraError("Failed to query swapchain images"); @@ -71,18 +66,18 @@ namespace Nz inline bool Swapchain::IsSupported() const { - if (!m_device.IsExtensionLoaded("VK_KHR_swapchain")) + if (!m_device->IsExtensionLoaded("VK_KHR_swapchain")) return false; } - VkResult Swapchain::CreateHelper(Device& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle) + VkResult Swapchain::CreateHelper(const DeviceHandle& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle) { - return device.vkCreateSwapchainKHR(device, createInfo, allocator, handle); + return device->vkCreateSwapchainKHR(*device, createInfo, allocator, handle); } - void Swapchain::DestroyHelper(Device& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator) + void Swapchain::DestroyHelper(const DeviceHandle& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator) { - return device.vkDestroySwapchainKHR(device, handle, allocator); + return device->vkDestroySwapchainKHR(*device, handle, allocator); } } }