From 602c39e8baf0309deee47b8ced5470a4e1478d0e Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 19 May 2016 09:07:31 +0200 Subject: [PATCH] Vulkan/Swapchain: Wrap images creation/getters Former-commit-id: 50e5890c7aeb16408a460e6734301c6393c435e5 --- include/Nazara/Vulkan/VkSwapchain.hpp | 10 +++++ include/Nazara/Vulkan/VkSwapchain.inl | 53 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/include/Nazara/Vulkan/VkSwapchain.hpp b/include/Nazara/Vulkan/VkSwapchain.hpp index ea47970d5..374ad88e6 100644 --- a/include/Nazara/Vulkan/VkSwapchain.hpp +++ b/include/Nazara/Vulkan/VkSwapchain.hpp @@ -24,6 +24,14 @@ namespace Nz 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 VkImage GetImage(UInt32 index) const; + inline const std::vector& GetImages() const; + inline UInt32 GetImageCount() const; + inline bool IsSupported() const; Swapchain& operator=(const Swapchain&) = delete; @@ -32,6 +40,8 @@ namespace Nz private: static VkResult CreateHelper(Device& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle); static void DestroyHelper(Device& 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 e05dfefd3..8865012e4 100644 --- a/include/Nazara/Vulkan/VkSwapchain.inl +++ b/include/Nazara/Vulkan/VkSwapchain.inl @@ -16,6 +16,59 @@ namespace Nz { } + 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); + switch (m_lastErrorCode) + { + case VkResult::VK_SUBOPTIMAL_KHR: + case VkResult::VK_SUCCESS: + return true; + + default: + return false; + } + } + + inline bool Swapchain::Create(const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator) + { + if (!DeviceObject::Create(createInfo, allocator)) + return false; + + UInt32 imageCount = 0; + 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"); + return false; + } + + m_images.resize(imageCount); + 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"); + return false; + } + + return true; + } + + inline VkImage Swapchain::GetImage(UInt32 index) const + { + return m_images[index]; + } + + inline const std::vector& Swapchain::GetImages() const + { + return m_images; + } + + inline UInt32 Swapchain::GetImageCount() const + { + return m_images.size(); + } + inline bool Swapchain::IsSupported() const { if (!m_device.IsExtensionLoaded("VK_KHR_swapchain"))