From f154ea852d4d6d920d22e62b9074a3c442b5cbb4 Mon Sep 17 00:00:00 2001 From: Johannes Schneider Date: Sat, 20 May 2023 19:29:57 +0200 Subject: [PATCH] [image|render-graph] Improve image wrapper further --- .../inexor/vulkan-renderer/wrapper/image.hpp | 22 ++++++++- src/vulkan-renderer/render_graph.cpp | 45 ++++++------------- src/vulkan-renderer/wrapper/image.cpp | 27 ++++++++++- 3 files changed, 61 insertions(+), 33 deletions(-) diff --git a/include/inexor/vulkan-renderer/wrapper/image.hpp b/include/inexor/vulkan-renderer/wrapper/image.hpp index 8dd866e92..26ed457fa 100644 --- a/include/inexor/vulkan-renderer/wrapper/image.hpp +++ b/include/inexor/vulkan-renderer/wrapper/image.hpp @@ -47,11 +47,17 @@ class Image { std::string name); /// Constructor 3 (calls constructor 2 internally) - /// @note This constructor does not expose image aspect flags (use ``VK_IMAGE_ASPECT_COLOR_BIT`` as default) + /// @param device The device wrapper /// @param img_ci The image create info + /// @param aspect_flags The image aspect flags + /// @param name The internal debug name of the image and the image view (must not be empty) + /// @exception std::invalid_argument The internal debug name is empty + /// @exception VulkanException vmaCreateImage call failed + /// @exception VulkanException vkCreateImageView call failed Image(const Device &device, const VkImageCreateInfo &img_ci, VkImageAspectFlags aspect_flags, std::string name); /// Constructor 4 (calls constructor 3 internally) + /// @note This constructor does not expose image aspect flags (use ``VK_IMAGE_ASPECT_COLOR_BIT`` as default) /// @param device The device wrapper /// @param img_ci The image create info /// @param name The internal debug name of the image and the image view (must not be empty) @@ -60,6 +66,20 @@ class Image { /// @exception VulkanException vkCreateImageView call failed Image(const Device &device, const VkImageCreateInfo &img_ci, std::string name); + /// Constructor 5 (calls constructor 3 (not 4!) internally) + /// @param device The device wrapper + /// @param format The image format + /// @param width The image width in pixels + /// @param height The image height in pixels + /// @param usage The image usage + /// @param aspect_flags The image aspect flags + /// @param name The internal debug name of the image and the image view (must not be empty) + /// @exception std::invalid_argument The internal debug name is empty + /// @exception VulkanException vmaCreateImage call failed + /// @exception VulkanException vkCreateImageView call failed + Image(const Device &device, VkFormat format, std::uint32_t width, std::uint32_t height, VkImageUsageFlags usage, + VkImageAspectFlags aspect_flags, std::string name); + Image(const Image &) = delete; Image(Image &&) noexcept; ~Image(); diff --git a/src/vulkan-renderer/render_graph.cpp b/src/vulkan-renderer/render_graph.cpp index b5c5abf93..9fb3c36a5 100644 --- a/src/vulkan-renderer/render_graph.cpp +++ b/src/vulkan-renderer/render_graph.cpp @@ -358,38 +358,21 @@ void RenderGraph::compile(const RenderResource *target) { if (texture_resource->m_usage == TextureUsage::BACK_BUFFER) { // TODO: Move image views from wrapper::Swapchain to PhysicalBackBuffer. texture_resource->m_physical = std::make_shared(m_device, m_swapchain); - continue; + } else { + auto physical = std::make_shared(m_device); + texture_resource->m_physical = physical; + + physical->m_img = std::make_unique( + m_device, texture_resource->m_format, m_swapchain.extent().width, m_swapchain.extent().height, + texture_resource->m_usage == TextureUsage::DEPTH_STENCIL_BUFFER + ? static_cast(VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) + : static_cast(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT), + static_cast(texture_resource->m_usage == TextureUsage::DEPTH_STENCIL_BUFFER + ? VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT + : VK_IMAGE_ASPECT_COLOR_BIT), + // TODO: Apply internal debug name to the images + "Rendergraph image"); } - - auto physical = std::make_shared(m_device); - texture_resource->m_physical = physical; - - physical->m_img = std::make_unique( - m_device, - wrapper::make_info({ - .imageType = VK_IMAGE_TYPE_2D, - .format = texture_resource->m_format, - .extent{ - // TODO: Support textures with dimensions not equal to back buffer size. - .width = m_swapchain.extent().width, - .height = m_swapchain.extent().height, - .depth = 1, - }, - .mipLevels = 1, - .arrayLayers = 1, - .samples = VK_SAMPLE_COUNT_1_BIT, - .tiling = VK_IMAGE_TILING_OPTIMAL, - .usage = texture_resource->m_usage == TextureUsage::DEPTH_STENCIL_BUFFER - ? static_cast(VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) - : static_cast(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT), - .sharingMode = VK_SHARING_MODE_EXCLUSIVE, - .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, - }), - static_cast(texture_resource->m_usage == TextureUsage::DEPTH_STENCIL_BUFFER - ? VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT - : VK_IMAGE_ASPECT_COLOR_BIT), - // TODO: Apply internal debug name to the images - "Rendergraph image"); } // Create physical stages. Each render stage maps to a vulkan pipeline (either compute or graphics) and a list of diff --git a/src/vulkan-renderer/wrapper/image.cpp b/src/vulkan-renderer/wrapper/image.cpp index e4e85a110..09024bb15 100644 --- a/src/vulkan-renderer/wrapper/image.cpp +++ b/src/vulkan-renderer/wrapper/image.cpp @@ -8,6 +8,7 @@ namespace inexor::vulkan_renderer::wrapper { +// Constructor 1 (the most powerful constructor which exposes all parameters, rarely used) Image::Image(const Device &device, const VkImageCreateInfo &img_ci, const VkImageViewCreateInfo &img_view_ci, const VmaAllocationCreateInfo &alloc_ci, std::string name) : m_device(device), m_format(img_ci.format), m_name(std::move(name)) { @@ -34,6 +35,7 @@ Image::Image(const Device &device, const VkImageCreateInfo &img_ci, const VkImag m_device.set_debug_marker_name(&m_img_view, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT, m_name); } +// Constructor 2 (calls constructor 1 internally) Image::Image(const Device &device, const VkImageCreateInfo &img_ci, const VkImageViewCreateInfo &img_view_ci, std::string name) : Image(device, img_ci, img_view_ci, @@ -43,11 +45,11 @@ Image::Image(const Device &device, const VkImageCreateInfo &img_ci, const VkImag }, name) {} +// Constructor 3 (calls constructor 2 internally) Image::Image(const Device &device, const VkImageCreateInfo &img_ci, const VkImageAspectFlags aspect_flags, std::string name) : Image(device, img_ci, make_info({ - .image = m_img, .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = img_ci.format, .subresourceRange{ @@ -58,9 +60,32 @@ Image::Image(const Device &device, const VkImageCreateInfo &img_ci, const VkImag }), std::move(name)) {} +// Constructor 4 (calls constructor 3 internally) Image::Image(const Device &device, const VkImageCreateInfo &img_ci, std::string name) : Image(device, img_ci, VK_IMAGE_ASPECT_COLOR_BIT, std::move(name)) {} +// Constructor 5 (calls constructor 3 (not 4!) internally) +Image::Image(const Device &device, const VkFormat format, std::uint32_t width, std::uint32_t height, + const VkImageUsageFlags usage, const VkImageAspectFlags aspect_flags, std::string name) + : Image(device, + wrapper::make_info({ + .imageType = VK_IMAGE_TYPE_2D, + .format = format, + .extent{ + .width = width, + .height = height, + .depth = 1, + }, + .mipLevels = 1, + .arrayLayers = 1, + .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = usage, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE, + .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, + }), + aspect_flags, std::move(name)) {} + Image::Image(Image &&other) noexcept : m_device(other.m_device) { m_format = other.m_format; m_alloc = other.m_alloc;