From d87b0587d7aadbdf1e2053571eab64820ca2763c Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 30 Apr 2016 11:43:48 +0200 Subject: [PATCH] Vulkan: Add support for Swapchains Former-commit-id: 7eafa8f4d38fb3202c63bc42d5d5bb4febfd459a --- include/Nazara/Vulkan/VkSwapchain.hpp | 52 +++++++++++++++++++++ include/Nazara/Vulkan/VkSwapchain.inl | 67 +++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 include/Nazara/Vulkan/VkSwapchain.hpp create mode 100644 include/Nazara/Vulkan/VkSwapchain.inl diff --git a/include/Nazara/Vulkan/VkSwapchain.hpp b/include/Nazara/Vulkan/VkSwapchain.hpp new file mode 100644 index 000000000..a0f23f8ac --- /dev/null +++ b/include/Nazara/Vulkan/VkSwapchain.hpp @@ -0,0 +1,52 @@ +// Copyright (C) 2016 Jérôme Leclercq +// This file is part of the "Nazara Engine - Vulkan" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_VULKAN_VKSWAPCHAIN_HPP +#define NAZARA_VULKAN_VKSWAPCHAIN_HPP + +#include +#include +#include +#include + +namespace Nz +{ + namespace Vk + { + class Device; + + class NAZARA_VULKAN_API Swapchain + { + public: + inline Swapchain(Device& instance); + Swapchain(const Swapchain&) = delete; + Swapchain(Swapchain&&) = delete; + inline ~Swapchain(); + + bool Create(const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator = nullptr); + inline void Destroy(); + + inline bool IsSupported() const; + + inline VkResult GetLastErrorCode() const; + + Swapchain& operator=(const Swapchain&) = delete; + Swapchain& operator=(Swapchain&&) = delete; + + inline operator VkSwapchainKHR(); + + private: + Device& m_device; + VkAllocationCallbacks m_allocator; + VkSwapchainKHR m_swapchain; + VkResult m_lastErrorCode; + }; + } +} + +#include + +#endif // NAZARA_VULKAN_VKSWAPCHAIN_HPP diff --git a/include/Nazara/Vulkan/VkSwapchain.inl b/include/Nazara/Vulkan/VkSwapchain.inl new file mode 100644 index 000000000..7ed08b902 --- /dev/null +++ b/include/Nazara/Vulkan/VkSwapchain.inl @@ -0,0 +1,67 @@ +// Copyright (C) 2016 Jérôme Leclercq +// 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 + +namespace Nz +{ + namespace Vk + { + inline Swapchain::Swapchain(Device& device) : + m_device(device), + m_swapchain(VK_NULL_HANDLE) + { + } + + inline Swapchain::~Swapchain() + { + Destroy(); + } + + inline bool Swapchain::Create(const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator) + { + m_lastErrorCode = m_device.vkCreateSwapchainKHR(m_device, &createInfo, allocator, &m_swapchain); + if (m_lastErrorCode != VkResult::VK_SUCCESS) + { + NazaraError("Failed to create Vulkan swapchain"); + return false; + } + + // Store the allocator to access them when needed + if (allocator) + m_allocator = *allocator; + else + m_allocator.pfnAllocation = nullptr; + + return true; + } + + inline void Swapchain::Destroy() + { + if (m_swapchain != VK_NULL_HANDLE) + m_device.vkDestroySwapchainKHR(m_device, m_swapchain, (m_allocator.pfnAllocation) ? &m_allocator : nullptr); + } + + inline VkResult Swapchain::GetLastErrorCode() const + { + return m_lastErrorCode; + } + + inline bool Swapchain::IsSupported() const + { + if (!m_device.IsExtensionLoaded("VK_KHR_swapchain")) + return false; + } + + inline Swapchain::operator VkSwapchainKHR() + { + return m_swapchain; + } + } +} + +#include