Renderer: Blit texture to window instead of using a full renderpass

This may improve performance and allow for render targets to customize how they blit the final texture (allowing for render-to-texture)
This commit is contained in:
SirLynix
2023-11-17 16:59:31 +01:00
parent f2e77fb8a5
commit 97d5640967
38 changed files with 448 additions and 265 deletions

View File

@@ -11,48 +11,45 @@
#include <Nazara/VulkanRenderer/Wrapper/DeviceObject.hpp>
#include <Nazara/VulkanRenderer/Wrapper/ImageView.hpp>
namespace Nz
namespace Nz::Vk
{
namespace Vk
class Swapchain : public DeviceObject<Swapchain, VkSwapchainKHR, VkSwapchainCreateInfoKHR, VK_OBJECT_TYPE_SWAPCHAIN_KHR>
{
class Swapchain : public DeviceObject<Swapchain, VkSwapchainKHR, VkSwapchainCreateInfoKHR, VK_OBJECT_TYPE_SWAPCHAIN_KHR>
{
friend DeviceObject;
friend DeviceObject;
public:
struct Image;
public:
struct Image;
Swapchain() = default;
Swapchain(const Swapchain&) = delete;
Swapchain(Swapchain&&) = default;
~Swapchain() = default;
Swapchain() = default;
Swapchain(const Swapchain&) = delete;
Swapchain(Swapchain&&) = default;
~Swapchain() = default;
inline bool AcquireNextImage(Nz::UInt64 timeout, VkSemaphore semaphore, VkFence fence, UInt32* imageIndex) const;
inline bool AcquireNextImage(Nz::UInt64 timeout, VkSemaphore semaphore, VkFence fence, UInt32* imageIndex) const;
inline bool Create(Device& device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator = nullptr);
inline bool Create(Device& device, const VkSwapchainCreateInfoKHR& createInfo, const VkAllocationCallbacks* allocator = nullptr);
inline const Image& GetImage(UInt32 index) const;
inline const std::vector<Image>& GetImages() const;
inline UInt32 GetImageCount() const;
inline const Image& GetImage(UInt32 index) const;
inline const std::vector<Image>& GetImages() const;
inline UInt32 GetImageCount() const;
inline bool IsSupported() const;
inline bool IsSupported() const;
Swapchain& operator=(const Swapchain&) = delete;
Swapchain& operator=(Swapchain&&) = default;
Swapchain& operator=(const Swapchain&) = delete;
Swapchain& operator=(Swapchain&&) = default;
struct Image
{
VkImage image;
ImageView view;
};
struct Image
{
VkImage image;
ImageView view;
};
private:
static inline VkResult CreateHelper(Device& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle);
static inline void DestroyHelper(Device& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator);
private:
static inline VkResult CreateHelper(Device& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle);
static inline void DestroyHelper(Device& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator);
std::vector<Image> m_images;
};
}
std::vector<Image> m_images;
};
}
#include <Nazara/VulkanRenderer/Wrapper/Swapchain.inl>

View File

@@ -7,117 +7,114 @@
#include <Nazara/VulkanRenderer/Wrapper/Device.hpp>
#include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz
namespace Nz::Vk
{
namespace Vk
inline bool Swapchain::AcquireNextImage(Nz::UInt64 timeout, VkSemaphore semaphore, VkFence fence, UInt32* imageIndex) const
{
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)
{
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;
case VkResult::VK_SUBOPTIMAL_KHR:
case VkResult::VK_SUCCESS:
return true;
default:
{
NazaraErrorFmt("failed to acquire next swapchain image: {0}", TranslateVulkanError(m_lastErrorCode));
return false;
default:
{
NazaraErrorFmt("failed to acquire next swapchain image: {0}", TranslateVulkanError(m_lastErrorCode));
return false;
}
}
}
inline bool Swapchain::Create(Device& 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)
{
NazaraErrorFmt("failed to query swapchain image count: {0}", TranslateVulkanError(m_lastErrorCode));
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)
{
NazaraErrorFmt("failed to query swapchain images: {0}", TranslateVulkanError(m_lastErrorCode));
return false;
}
m_images.resize(imageCount);
for (UInt32 i = 0; i < imageCount; ++i)
{
m_images[i].image = images[i];
VkImageViewCreateInfo imageViewCreateInfo = {
VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkImageViewCreateFlags flags;
m_images[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;
}
}
}
};
inline bool Swapchain::Create(Device& 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)
if (!m_images[i].view.Create(*m_device, imageViewCreateInfo))
{
NazaraErrorFmt("failed to query swapchain image count: {0}", TranslateVulkanError(m_lastErrorCode));
NazaraErrorFmt("failed to create image view for image #{0}", i);
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)
{
NazaraErrorFmt("failed to query swapchain images: {0}", TranslateVulkanError(m_lastErrorCode));
return false;
}
m_images.resize(imageCount);
for (UInt32 i = 0; i < imageCount; ++i)
{
m_images[i].image = images[i];
VkImageViewCreateInfo imageViewCreateInfo = {
VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkImageViewCreateFlags flags;
m_images[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_images[i].view.Create(*m_device, imageViewCreateInfo))
{
NazaraErrorFmt("failed to create image view for image #{0}", i);
return false;
}
}
return true;
}
inline const Swapchain::Image& Swapchain::GetImage(UInt32 index) const
{
return m_images[index];
}
return true;
}
inline const std::vector<Swapchain::Image>& Swapchain::GetImages() const
{
return m_images;
}
inline const Swapchain::Image& Swapchain::GetImage(UInt32 index) const
{
return m_images[index];
}
inline UInt32 Swapchain::GetImageCount() const
{
return static_cast<UInt32>(m_images.size());
}
inline const std::vector<Swapchain::Image>& Swapchain::GetImages() const
{
return m_images;
}
inline bool Swapchain::IsSupported() const
{
if (!m_device->IsExtensionLoaded(VK_KHR_SWAPCHAIN_EXTENSION_NAME))
return false;
inline UInt32 Swapchain::GetImageCount() const
{
return static_cast<UInt32>(m_images.size());
}
return true;
}
inline bool Swapchain::IsSupported() const
{
if (!m_device->IsExtensionLoaded(VK_KHR_SWAPCHAIN_EXTENSION_NAME))
return false;
inline VkResult Swapchain::CreateHelper(Device& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle)
{
return device.vkCreateSwapchainKHR(device, createInfo, allocator, handle);
}
return true;
}
inline void Swapchain::DestroyHelper(Device& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator)
{
return device.vkDestroySwapchainKHR(device, handle, allocator);
}
inline VkResult Swapchain::CreateHelper(Device& device, const VkSwapchainCreateInfoKHR* createInfo, const VkAllocationCallbacks* allocator, VkSwapchainKHR* handle)
{
return device.vkCreateSwapchainKHR(device, createInfo, allocator, handle);
}
inline void Swapchain::DestroyHelper(Device& device, VkSwapchainKHR handle, const VkAllocationCallbacks* allocator)
{
return device.vkDestroySwapchainKHR(device, handle, allocator);
}
}