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 1, 2023
1 parent 7333a2b commit 56da06a
Show file tree
Hide file tree
Showing 20 changed files with 497 additions and 733 deletions.
66 changes: 30 additions & 36 deletions framework/api_vulkan_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,12 +1024,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 @@ -1041,12 +1040,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 @@ -1128,12 +1126,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 @@ -1145,12 +1142,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 @@ -1229,12 +1225,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 @@ -1246,12 +1241,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
37 changes: 22 additions & 15 deletions framework/common/hpp_vk_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,28 @@ 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 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));
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));
}

// helper functions not backed by vk_common.h
Expand Down
Loading

0 comments on commit 56da06a

Please sign in to comment.