diff --git a/sandbox/01_offscreen/renderer.cpp b/sandbox/01_offscreen/renderer.cpp index 67075d7..ec8b696 100644 --- a/sandbox/01_offscreen/renderer.cpp +++ b/sandbox/01_offscreen/renderer.cpp @@ -192,7 +192,7 @@ void Renderer::draw() { }; if (frame_count % 30 < 10) { - draw_to_storage_image(red); + draw_to_storage_image(red); } else if (frame_count % 30 > 20) { draw_to_storage_image(green); } else { @@ -225,7 +225,7 @@ void Renderer::draw() { image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; image_memory_barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; - image_memory_barrier.image = swapchain_image; + image_memory_barrier.image = swapchain_image.get_handle(); vkCmdPipelineBarrier(m_command_buffers[m_current_frame].get_handle(), VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, @@ -244,7 +244,7 @@ void Renderer::draw() { vkCmdCopyImage(m_command_buffers[m_current_frame].get_handle(), m_storage_image->get_handle(), - VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, swapchain_image, + VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, swapchain_image.get_handle(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &image_copy); // transition swapchain image layout @@ -252,7 +252,7 @@ void Renderer::draw() { image_memory_barrier.dstAccessMask = 0; image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; image_memory_barrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; - image_memory_barrier.image = swapchain_image; + image_memory_barrier.image = swapchain_image.get_handle(); vkCmdPipelineBarrier(m_command_buffers[m_current_frame].get_handle(), VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, diff --git a/src/prism/vulkan/image.h b/src/prism/vulkan/image.h index 6834282..872a862 100644 --- a/src/prism/vulkan/image.h +++ b/src/prism/vulkan/image.h @@ -69,7 +69,7 @@ namespace prism void upload(const CommandPool &command_pool, const void *data, VkDeviceSize size, VkImageLayout target_layout = VK_IMAGE_LAYOUT_GENERAL); - private: + protected: const Device &m_device; VkImage m_handle{}; diff --git a/src/prism/vulkan/swapchain.cpp b/src/prism/vulkan/swapchain.cpp index e777236..b6818e0 100644 --- a/src/prism/vulkan/swapchain.cpp +++ b/src/prism/vulkan/swapchain.cpp @@ -23,6 +23,24 @@ VkExtent2D choose_extent(VkExtent2D required, const VkSurfaceCapabilitiesKHR &ca uint32_t choose_image_count(uint32_t required, const VkSurfaceCapabilitiesKHR &capabilities); +SwapchainImage::SwapchainImage(const Device &device, VkImage handle) + : Image(device, handle) +{ +} + +SwapchainImage::SwapchainImage(SwapchainImage &&other) noexcept + : Image(std::move(other)) +{ +} + +SwapchainImage::~SwapchainImage() +{ + if (m_handle != VK_NULL_HANDLE) + { + m_handle = VK_NULL_HANDLE; + } +} + SwapchainCreateInfo::SwapchainCreateInfo() { @@ -62,24 +80,22 @@ Swapchain::Swapchain(const Device &device, const Surface &surface, const Propert unsigned int image_count; VK_CHECK(vkGetSwapchainImagesKHR(m_device.get_handle(), m_handle, &image_count, nullptr)); - m_images.resize(image_count); - VK_CHECK(vkGetSwapchainImagesKHR(m_device.get_handle(), m_handle, &image_count, m_images.data())); - - VkImageViewCreateInfo image_view_create_info{}; - image_view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; - image_view_create_info.viewType = VK_IMAGE_VIEW_TYPE_2D; - image_view_create_info.format = m_properties.surface_format.format; - image_view_create_info.subresourceRange.levelCount = 1; - image_view_create_info.subresourceRange.layerCount = 1; - image_view_create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; - - for (auto &image : m_images) + std::vector vk_images(image_count); + VK_CHECK(vkGetSwapchainImagesKHR(m_device.get_handle(), m_handle, &image_count, vk_images.data())); + + ImageViewCreateInfo image_view_ci{}; + image_view_ci.set_view_type(VK_IMAGE_VIEW_TYPE_2D) + .set_format(m_properties.surface_format.format) + .set_level_count(1) + .set_layer_count(1) + .set_aspect_mask(VK_IMAGE_ASPECT_COLOR_BIT); + + for (auto &vk_image : vk_images) { - image_view_create_info.image = image; + m_images.emplace_back(m_device, vk_image); - VkImageView image_view; - VK_CHECK(vkCreateImageView(m_device.get_handle(), &image_view_create_info, nullptr, &image_view)); - m_image_views.push_back(image_view); + image_view_ci.image = vk_image; + m_image_views.emplace_back(m_images.back(), image_view_ci); } } @@ -99,14 +115,6 @@ Swapchain::~Swapchain() { vkDestroySwapchainKHR(m_device.get_handle(), m_handle, nullptr); } - - for (auto& image_view : m_image_views) - { - if (image_view != VK_NULL_HANDLE) - { - vkDestroyImageView(m_device.get_handle(), image_view, nullptr); - } - } } VkSwapchainKHR Swapchain::get_handle() const @@ -124,7 +132,7 @@ const VkExtent2D &Swapchain::get_extent() const return m_properties.extent; } -const std::vector &Swapchain::get_images() const +const std::vector &Swapchain::get_images() const { return m_images; } diff --git a/src/prism/vulkan/swapchain.h b/src/prism/vulkan/swapchain.h index 42ce1ab..bd8a793 100644 --- a/src/prism/vulkan/swapchain.h +++ b/src/prism/vulkan/swapchain.h @@ -2,12 +2,30 @@ #include "prism/vulkan/semaphore.h" #include "prism/vulkan/fence.h" +#include "prism/vulkan/image.h" +#include "prism/vulkan/image_view.h" namespace prism { class Device; class Surface; + class SwapchainImage : public Image + { + public: + SwapchainImage(const Device &device, VkImage handle); + + SwapchainImage(const SwapchainImage &) = delete; + + SwapchainImage(SwapchainImage &&other) noexcept; + + ~SwapchainImage() override; + + SwapchainImage &operator=(const SwapchainImage &) = delete; + + SwapchainImage &operator=(SwapchainImage &&) = delete; + }; + class SwapchainCreateInfo : public VkSwapchainCreateInfoKHR { public: @@ -45,7 +63,7 @@ namespace prism const VkExtent2D &get_extent() const; - const std::vector &get_images() const; + const std::vector &get_images() const; VkResult acquire_next_image(uint64_t time_out, const Semaphore& semaphore, const std::optional& fence, uint32_t &image_index) const; @@ -56,8 +74,8 @@ namespace prism const Surface &m_surface; - std::vector m_images; - std::vector m_image_views; + std::vector m_images; + std::vector m_image_views; Properties m_properties;