Skip to content

Commit

Permalink
cleanup on image layout transition functions
Browse files Browse the repository at this point in the history
  • Loading branch information
asuessenbach committed Aug 14, 2023
1 parent fe63a1e commit 824adae
Show file tree
Hide file tree
Showing 20 changed files with 506 additions and 742 deletions.
66 changes: 30 additions & 36 deletions framework/api_vulkan_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1025,12 +1025,11 @@ Texture ApiVulkanSample::load_texture(const std::string &file, vkb::sg::Image::C

// Image barrier for optimal image (target)
// Optimal image will be used as destination for the copy
vkb::set_image_layout(
command_buffer,
texture.image->get_vk_image().get_handle(),
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
subresource_range);
vkb::image_layout_transition(command_buffer,
texture.image->get_vk_image().get_handle(),
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
subresource_range);

// Copy mip levels from staging buffer
vkCmdCopyBufferToImage(
Expand All @@ -1042,12 +1041,11 @@ Texture ApiVulkanSample::load_texture(const std::string &file, vkb::sg::Image::C
bufferCopyRegions.data());

// Change texture image layout to shader read after all mip levels have been copied
vkb::set_image_layout(
command_buffer,
texture.image->get_vk_image().get_handle(),
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
subresource_range);
vkb::image_layout_transition(command_buffer,
texture.image->get_vk_image().get_handle(),
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
subresource_range);

device->flush_command_buffer(command_buffer, queue.get_handle());

Expand Down Expand Up @@ -1129,12 +1127,11 @@ Texture ApiVulkanSample::load_texture_array(const std::string &file, vkb::sg::Im

// Image barrier for optimal image (target)
// Optimal image will be used as destination for the copy
vkb::set_image_layout(
command_buffer,
texture.image->get_vk_image().get_handle(),
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
subresource_range);
vkb::image_layout_transition(command_buffer,
texture.image->get_vk_image().get_handle(),
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
subresource_range);

// Copy mip levels from staging buffer
vkCmdCopyBufferToImage(
Expand All @@ -1146,12 +1143,11 @@ Texture ApiVulkanSample::load_texture_array(const std::string &file, vkb::sg::Im
buffer_copy_regions.data());

// Change texture image layout to shader read after all mip levels have been copied
vkb::set_image_layout(
command_buffer,
texture.image->get_vk_image().get_handle(),
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
subresource_range);
vkb::image_layout_transition(command_buffer,
texture.image->get_vk_image().get_handle(),
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
subresource_range);

device->flush_command_buffer(command_buffer, queue.get_handle());

Expand Down Expand Up @@ -1230,12 +1226,11 @@ Texture ApiVulkanSample::load_texture_cubemap(const std::string &file, vkb::sg::

// Image barrier for optimal image (target)
// Optimal image will be used as destination for the copy
vkb::set_image_layout(
command_buffer,
texture.image->get_vk_image().get_handle(),
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
subresource_range);
vkb::image_layout_transition(command_buffer,
texture.image->get_vk_image().get_handle(),
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
subresource_range);

// Copy mip levels from staging buffer
vkCmdCopyBufferToImage(
Expand All @@ -1247,12 +1242,11 @@ Texture ApiVulkanSample::load_texture_cubemap(const std::string &file, vkb::sg::
buffer_copy_regions.data());

// Change texture image layout to shader read after all mip levels have been copied
vkb::set_image_layout(
command_buffer,
texture.image->get_vk_image().get_handle(),
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
subresource_range);
vkb::image_layout_transition(command_buffer,
texture.image->get_vk_image().get_handle(),
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
subresource_range);

device->flush_command_buffer(command_buffer, queue.get_handle());

Expand Down
41 changes: 24 additions & 17 deletions framework/common/hpp_vk_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ inline vk::ShaderModule load_shader(const std::string &filename, vk::Device devi
return static_cast<vk::ShaderModule>(vkb::load_shader(filename, device, static_cast<VkShaderStageFlagBits>(stage)));
}

inline void image_layout_transition(vk::CommandBuffer command_buffer,
vk::Image image,
vk::ImageLayout old_layout,
vk::ImageLayout new_layout)
{
vkb::image_layout_transition(command_buffer,
static_cast<VkImage>(image),
static_cast<VkImageLayout>(old_layout),
static_cast<VkImageLayout>(new_layout));
}

inline void image_layout_transition(vk::CommandBuffer command_buffer,
vk::Image image,
vk::ImageLayout old_layout,
vk::ImageLayout new_layout,
vk::ImageSubresourceRange subresource_range)
{
vkb::image_layout_transition(command_buffer,
static_cast<VkImage>(image),
static_cast<VkImageLayout>(old_layout),
static_cast<VkImageLayout>(new_layout),
static_cast<VkImageSubresourceRange>(subresource_range));
}

inline vk::SurfaceFormatKHR select_surface_format(vk::PhysicalDevice gpu,
vk::SurfaceKHR surface,
std::vector<vk::Format> const &preferred_formats = {
Expand All @@ -127,23 +151,6 @@ inline vk::SurfaceFormatKHR select_surface_format(vk::PhysicalDevice
return it != supported_surface_formats.end() ? *it : supported_surface_formats[0];
}

inline void set_image_layout(vk::CommandBuffer command_buffer,
vk::Image image,
vk::ImageLayout old_layout,
vk::ImageLayout new_layout,
vk::ImageSubresourceRange subresource_range,
vk::PipelineStageFlags src_mask = vk::PipelineStageFlagBits::eAllCommands,
vk::PipelineStageFlags dst_mask = vk::PipelineStageFlagBits::eAllCommands)
{
vkb::set_image_layout(command_buffer,
static_cast<VkImage>(image),
static_cast<VkImageLayout>(old_layout),
static_cast<VkImageLayout>(new_layout),
static_cast<VkImageSubresourceRange>(subresource_range),
static_cast<VkPipelineStageFlags>(src_mask),
static_cast<VkPipelineStageFlags>(dst_mask));
}

// helper functions not backed by vk_common.h
inline vk::CommandBuffer allocate_command_buffer(vk::Device device, vk::CommandPool command_pool, vk::CommandBufferLevel level = vk::CommandBufferLevel::ePrimary)
{
Expand Down
Loading

0 comments on commit 824adae

Please sign in to comment.