Files
NazaraEngine/include/Nazara/VulkanRenderer/VkSwapchain.inl
Lynix bdedd05032 Add new Renderer architecture (far from complete)
Former-commit-id: 52226793d7a087dfe0523315d3303934daffee49 [formerly 9de1c04df6b371f861a2eee8bba38902ee5041cd] [formerly ecd3099df5498722f6390447f40bd3907b8e40c4 [formerly 3076df585565fc9759ab3e718270f2e5ef620840]]
Former-commit-id: 92d52f967d0b088d1271afef26069e08cacd6b0f [formerly 5fe27e2ead104278951c772c2121a7b677f88d4d]
Former-commit-id: fb6c2456d8edd3ec022d5d953f79fecdd4f2b8f4
2016-08-23 12:52:34 +02:00

122 lines
4.3 KiB
C++

// Copyright (C) 2016 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 <Nazara/VulkanRenderer/VkSwapchain.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/VulkanRenderer/VkDevice.hpp>
#include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz
{
namespace Vk
{
inline bool Swapchain::AcquireNextImage(Nz::UInt64 timeout, VkSemaphore semaphore, VkFence fence, UInt32* imageIndex) const
{
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 DeviceHandle& device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator)
{
if (!DeviceObject::Create(device, 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;
}
std::vector<VkImage> images(imageCount);
m_lastErrorCode = m_device->vkGetSwapchainImagesKHR(*m_device, m_handle, &imageCount, images.data());
if (m_lastErrorCode != VkResult::VK_SUCCESS)
{
NazaraError("Failed to query swapchain images");
return false;
}
m_buffers.resize(imageCount);
for (UInt32 i = 0; i < imageCount; ++i)
{
m_buffers[i].image = images[i];
VkImageViewCreateInfo imageViewCreateInfo = {
VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkImageViewCreateFlags flags;
m_buffers[i].image, // VkImage image;
VK_IMAGE_VIEW_TYPE_2D, // VkImageViewType viewType;
createInfo.imageFormat, // VkFormat format;
{ // VkComponentMapping components;
VK_COMPONENT_SWIZZLE_R, // VkComponentSwizzle .r;
VK_COMPONENT_SWIZZLE_G, // VkComponentSwizzle .g;
VK_COMPONENT_SWIZZLE_B, // VkComponentSwizzle .b;
VK_COMPONENT_SWIZZLE_A // VkComponentSwizzle .a;
},
{ // VkImageSubresourceRange subresourceRange;
VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags .aspectMask;
0, // uint32_t .baseMipLevel;
1, // uint32_t .levelCount;
0, // uint32_t .baseArrayLayer;
1 // uint32_t .layerCount;
}
};
if (!m_buffers[i].view.Create(m_device, imageViewCreateInfo))
{
NazaraError("Failed to create image view for image #" + String::Number(i));
return false;
}
}
return true;
}
inline const Swapchain::Buffer& Swapchain::GetBuffer(UInt32 index) const
{
return m_buffers[index];
}
inline const std::vector<Swapchain::Buffer>& Swapchain::GetBuffers() const
{
return m_buffers;
}
inline UInt32 Swapchain::GetBufferCount() const
{
return static_cast<UInt32>(m_buffers.size());
}
inline bool Swapchain::IsSupported() const
{
if (!m_device->IsExtensionLoaded("VK_KHR_swapchain"))
return false;
return true;
}
inline VkResult Swapchain::CreateHelper(const DeviceHandle& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle)
{
return device->vkCreateSwapchainKHR(*device, createInfo, allocator, handle);
}
inline void Swapchain::DestroyHelper(const DeviceHandle& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator)
{
return device->vkDestroySwapchainKHR(*device, handle, allocator);
}
}
}
#include <Nazara/VulkanRenderer/DebugOff.hpp>