Skip to content

Commit

Permalink
dev: improve swapchain image
Browse files Browse the repository at this point in the history
  • Loading branch information
jyxiong committed Sep 14, 2024
1 parent ccc8b43 commit 100b865
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 33 deletions.
8 changes: 4 additions & 4 deletions sandbox/01_offscreen/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -244,15 +244,15 @@ 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
image_memory_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
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,
Expand Down
2 changes: 1 addition & 1 deletion src/prism/vulkan/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand Down
58 changes: 33 additions & 25 deletions src/prism/vulkan/swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{

Expand Down Expand Up @@ -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<VkImage> 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);
}
}

Expand All @@ -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
Expand All @@ -124,7 +132,7 @@ const VkExtent2D &Swapchain::get_extent() const
return m_properties.extent;
}

const std::vector<VkImage> &Swapchain::get_images() const
const std::vector<SwapchainImage> &Swapchain::get_images() const
{
return m_images;
}
Expand Down
24 changes: 21 additions & 3 deletions src/prism/vulkan/swapchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -45,7 +63,7 @@ namespace prism

const VkExtent2D &get_extent() const;

const std::vector<VkImage> &get_images() const;
const std::vector<SwapchainImage> &get_images() const;

VkResult acquire_next_image(uint64_t time_out, const Semaphore& semaphore, const std::optional<Fence>& fence, uint32_t &image_index) const;

Expand All @@ -56,8 +74,8 @@ namespace prism

const Surface &m_surface;

std::vector<VkImage> m_images;
std::vector<VkImageView> m_image_views;
std::vector<SwapchainImage> m_images;
std::vector<ImageView> m_image_views;

Properties m_properties;

Expand Down

0 comments on commit 100b865

Please sign in to comment.