Vulkan: Use helper for image transitions.

This commit is contained in:
BearOso 2024-10-17 15:15:21 -05:00
parent 39c0f8418b
commit 79f6e911f3
7 changed files with 96 additions and 187 deletions

View File

@ -2,6 +2,7 @@
#include "vulkan_pipeline_image.hpp"
#include "slang_helpers.hpp"
#include "vulkan_common.hpp"
namespace Vulkan
{
@ -63,25 +64,14 @@ void PipelineImage::generate_mipmaps(vk::CommandBuffer cmd)
if (!mipmap)
return;
auto srr = [](unsigned int i) { return vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, i, 1, 0, 1); };
auto srl = [](unsigned int i) { return vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, i, 0, 1); };
auto range = [](unsigned int i) { return vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, i, 1, 0, 1); };
auto level = [](unsigned int i) { return vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, i, 0, 1); };
auto image_memory_barrier = vk::ImageMemoryBarrier{}.setImage(image);
auto mipmap_levels = mipmap ? mipmap_levels_for_size(image_width, image_height) : 1;
image_layout_transition(cmd, image, vk::ImageLayout::eShaderReadOnlyOptimal, vk::ImageLayout::eTransferSrcOptimal);
// Transition base layer to readable format.
image_memory_barrier
.setImage(image)
.setOldLayout(vk::ImageLayout::eShaderReadOnlyOptimal)
.setNewLayout(vk::ImageLayout::eTransferSrcOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eInputAttachmentRead)
.setDstAccessMask(vk::AccessFlagBits::eTransferRead)
.setSubresourceRange(srr(0));
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eVertexShader | vk::PipelineStageFlagBits::eFragmentShader,
vk::PipelineStageFlagBits::eTransfer,
{}, {}, {}, image_memory_barrier);
int base_width = image_width;
int base_height = image_height;
int base_level = 0;
@ -90,75 +80,52 @@ void PipelineImage::generate_mipmaps(vk::CommandBuffer cmd)
// Transition base layer to readable format.
if (base_level > 0)
{
image_memory_barrier
.setImage(image)
.setOldLayout(vk::ImageLayout::eTransferDstOptimal)
.setNewLayout(vk::ImageLayout::eTransferSrcOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eTransferWrite)
.setDstAccessMask(vk::AccessFlagBits::eTransferRead)
.setSubresourceRange(srr(base_level));
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eTransfer,
{}, {}, {}, image_memory_barrier);
image_layout_transition(cmd,
image,
vk::ImageLayout::eTransferDstOptimal,
vk::ImageLayout::eTransferSrcOptimal,
range(base_level));
}
// Transition mipmap layer to writable
image_memory_barrier
.setImage(image)
.setOldLayout(vk::ImageLayout::eUndefined)
.setNewLayout(vk::ImageLayout::eTransferDstOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eNone)
.setDstAccessMask(vk::AccessFlagBits::eTransferWrite)
.setSubresourceRange(srr(base_level + 1));
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eTransfer,
{}, {}, {}, image_memory_barrier);
image_layout_transition(cmd,
image,
vk::ImageLayout::eUndefined,
vk::ImageLayout::eTransferDstOptimal,
range(base_level + 1));
// Blit base layer to mipmap layer
int mipmap_width = base_width >> 1;
int mipmap_height = base_height >> 1;
if (mipmap_width < 1)
mipmap_width = 1;
if (mipmap_height < 1)
mipmap_height = 1;
int mipmap_width = std::max(base_width >> 1, 1);
int mipmap_height = std::max(base_height >> 1, 1);
auto blit = vk::ImageBlit{}
.setSrcOffsets({ vk::Offset3D(0, 0, 0), vk::Offset3D(base_width, base_height, 1) })
.setDstOffsets({ vk::Offset3D(0, 0, 0), vk::Offset3D(mipmap_width, mipmap_height, 1) })
.setSrcSubresource(srl(base_level))
.setDstSubresource(srl(base_level + 1));
.setSrcSubresource(level(base_level))
.setDstSubresource(level(base_level + 1));
base_width = mipmap_width;
base_height = mipmap_height;
cmd.blitImage(image, vk::ImageLayout::eTransferSrcOptimal, image, vk::ImageLayout::eTransferDstOptimal, blit, vk::Filter::eLinear);
cmd.blitImage(image,
vk::ImageLayout::eTransferSrcOptimal,
image,
vk::ImageLayout::eTransferDstOptimal,
blit,
vk::Filter::eLinear);
// Transition base layer to shader readable
image_memory_barrier
.setOldLayout(vk::ImageLayout::eTransferSrcOptimal)
.setNewLayout(vk::ImageLayout::eShaderReadOnlyOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eTransferRead)
.setDstAccessMask(vk::AccessFlagBits::eShaderRead)
.setSubresourceRange(srr(base_level));
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eFragmentShader,
{}, {}, {}, image_memory_barrier);
image_layout_transition(cmd,
image,
vk::ImageLayout::eTransferSrcOptimal,
vk::ImageLayout::eShaderReadOnlyOptimal,
range(base_level));
}
// Transition final layer to shader readable
image_memory_barrier
.setOldLayout(vk::ImageLayout::eTransferDstOptimal)
.setNewLayout(vk::ImageLayout::eShaderReadOnlyOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eTransferWrite)
.setDstAccessMask(vk::AccessFlagBits::eShaderRead)
.setSubresourceRange(srr(base_level));
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eFragmentShader,
{}, {}, {}, image_memory_barrier);
image_layout_transition(cmd,
image,
vk::ImageLayout::eTransferDstOptimal,
vk::ImageLayout::eShaderReadOnlyOptimal,
range(base_level));
}
void PipelineImage::barrier(vk::CommandBuffer cmd)
@ -172,18 +139,11 @@ void PipelineImage::clear(vk::CommandBuffer cmd)
{
vk::ImageSubresourceRange subresource_range(vk::ImageAspectFlagBits::eColor, 0, VK_REMAINING_MIP_LEVELS, 0, 1);
auto image_memory_barrier = vk::ImageMemoryBarrier{}
.setImage(image)
.setSubresourceRange(subresource_range)
.setOldLayout(vk::ImageLayout::eUndefined)
.setNewLayout(vk::ImageLayout::eTransferDstOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eShaderRead)
.setDstAccessMask(vk::AccessFlagBits::eTransferWrite);
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eFragmentShader,
vk::PipelineStageFlagBits::eTransfer,
{}, {}, {},
image_memory_barrier);
image_layout_transition(cmd,
image,
vk::ImageLayout::eUndefined,
vk::ImageLayout::eTransferDstOptimal,
subresource_range);
vk::ClearColorValue color{};
@ -194,16 +154,11 @@ void PipelineImage::clear(vk::CommandBuffer cmd)
color,
subresource_range);
image_memory_barrier
.setOldLayout(vk::ImageLayout::eTransferDstOptimal)
.setNewLayout(vk::ImageLayout::eShaderReadOnlyOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eTransferWrite)
.setDstAccessMask(vk::AccessFlagBits::eShaderRead);
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eFragmentShader,
{}, {}, {},
image_memory_barrier);
image_layout_transition(cmd,
image,
vk::ImageLayout::eTransferDstOptimal,
vk::ImageLayout::eShaderReadOnlyOptimal,
subresource_range);
current_layout = vk::ImageLayout::eShaderReadOnlyOptimal;
}

View File

@ -4,6 +4,7 @@
#include "slang_helpers.hpp"
#include "stb_image.h"
#include "vulkan/vulkan_enums.hpp"
#include "vulkan_common.hpp"
namespace Vulkan
{
@ -464,25 +465,12 @@ bool ShaderChain::do_frame_without_swap(uint8_t *data, int width, int height, in
if (preset->last_pass_uses_feedback && is_last_pass)
{
std::array<vk::ImageMemoryBarrier, 2> image_memory_barrier{};
image_memory_barrier[0]
.setImage(frame.image.image)
.setOldLayout(vk::ImageLayout::eUndefined)
.setNewLayout(vk::ImageLayout::eTransferDstOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eColorAttachmentWrite)
.setDstAccessMask(vk::AccessFlagBits::eTransferWrite)
.setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1));
image_memory_barrier[1]
.setImage(context->swapchain->get_image())
.setOldLayout(vk::ImageLayout::ePresentSrcKHR)
.setNewLayout(vk::ImageLayout::eTransferSrcOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eNone)
.setDstAccessMask(vk::AccessFlagBits::eTransferRead)
.setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1));
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eColorAttachmentOutput,
vk::PipelineStageFlagBits::eTransfer,
{}, {}, {}, image_memory_barrier);
image_layout_transition(cmd, frame.image.image,
vk::ImageLayout::eUndefined,
vk::ImageLayout::eTransferDstOptimal);
image_layout_transition(cmd, context->swapchain->get_image(),
vk::ImageLayout::ePresentSrcKHR,
vk::ImageLayout::eTransferSrcOptimal);
auto image_blit = vk::ImageBlit{}
.setSrcOffsets({ vk::Offset3D(viewport_x, viewport_y, 0), vk::Offset3D(viewport_x + viewport_width, viewport_y + viewport_height, 1) })
@ -492,25 +480,12 @@ bool ShaderChain::do_frame_without_swap(uint8_t *data, int width, int height, in
cmd.blitImage(context->swapchain->get_image(), vk::ImageLayout::eTransferSrcOptimal, frame.image.image, vk::ImageLayout::eTransferDstOptimal, image_blit, vk::Filter::eNearest);
image_memory_barrier[0]
.setOldLayout(vk::ImageLayout::eTransferDstOptimal)
.setNewLayout(vk::ImageLayout::eShaderReadOnlyOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eTransferWrite)
.setDstAccessMask(vk::AccessFlagBits::eShaderRead)
.setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1));
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eVertexShader | vk::PipelineStageFlagBits::eFragmentShader,
{}, {}, {}, image_memory_barrier[0]);
image_memory_barrier[1]
.setOldLayout(vk::ImageLayout::eTransferSrcOptimal)
.setNewLayout(vk::ImageLayout::ePresentSrcKHR)
.setSrcAccessMask(vk::AccessFlagBits::eTransferRead)
.setDstAccessMask(vk::AccessFlagBits::eNone)
.setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1));
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eBottomOfPipe,
{}, {}, {}, image_memory_barrier[1]);
image_layout_transition(cmd, frame.image.image,
vk::ImageLayout::eTransferDstOptimal,
vk::ImageLayout::eShaderReadOnlyOptimal);
image_layout_transition(cmd, context->swapchain->get_image(),
vk::ImageLayout::eTransferSrcOptimal,
vk::ImageLayout::ePresentSrcKHR);
frame.image.current_layout = vk::ImageLayout::eTransferDstOptimal;
}

View File

@ -1,8 +1,8 @@
#include <cassert>
#include "vulkan_texture.hpp"
#include "vulkan/vulkan_enums.hpp"
#include "slang_helpers.hpp"
#include "vulkan_common.hpp"
namespace Vulkan
{
@ -103,20 +103,13 @@ void Texture::from_buffer(vk::CommandBuffer cmd,
allocator.unmapMemory(buffer_allocation);
allocator.flushAllocation(buffer_allocation, 0, width * height * pixel_size);
auto srr = [](unsigned int i) { return vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, i, 1, 0, 1); };
auto srl = [](unsigned int i) { return vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, i, 0, 1); };
auto range = [](unsigned int i) { return vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, i, 1, 0, 1); };
auto level = [](unsigned int i) { return vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, i, 0, 1); };
auto barrier = vk::ImageMemoryBarrier{}
.setImage(image)
.setOldLayout(vk::ImageLayout::eUndefined)
.setNewLayout(vk::ImageLayout::eTransferDstOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eShaderRead)
.setDstAccessMask(vk::AccessFlagBits::eTransferWrite)
.setSubresourceRange(srr(0));
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eFragmentShader,
vk::PipelineStageFlagBits::eTransfer,
{}, {}, {}, barrier);
image_layout_transition(cmd, image,
vk::ImageLayout::eUndefined,
vk::ImageLayout::eTransferDstOptimal,
range(0));
auto buffer_image_copy = vk::BufferImageCopy{}
.setBufferOffset(0)
@ -124,7 +117,7 @@ void Texture::from_buffer(vk::CommandBuffer cmd,
.setBufferImageHeight(height)
.setImageExtent(vk::Extent3D(width, height, 1))
.setImageOffset(vk::Offset3D(0, 0, 0))
.setImageSubresource(srl(0));
.setImageSubresource(level(0));
cmd.copyBufferToImage(this->buffer, image, vk::ImageLayout::eTransferDstOptimal, buffer_image_copy);
auto mipmap_levels = mipmap ? mipmap_levels_for_size(image_width, image_height) : 1;
@ -135,30 +128,16 @@ void Texture::from_buffer(vk::CommandBuffer cmd,
for (; base_level + 1 < mipmap_levels; base_level++)
{
// Transition base layer to readable format.
barrier
.setImage(image)
.setOldLayout(vk::ImageLayout::eTransferDstOptimal)
.setNewLayout(vk::ImageLayout::eTransferSrcOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eTransferWrite)
.setDstAccessMask(vk::AccessFlagBits::eTransferRead)
.setSubresourceRange(srr(base_level));
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eTransfer,
{}, {}, {}, barrier);
image_layout_transition(cmd, image,
vk::ImageLayout::eTransferDstOptimal,
vk::ImageLayout::eTransferSrcOptimal,
range(base_level));
// Transition mipmap layer to writable
barrier
.setImage(image)
.setOldLayout(vk::ImageLayout::eUndefined)
.setNewLayout(vk::ImageLayout::eTransferDstOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eTransferRead)
.setDstAccessMask(vk::AccessFlagBits::eTransferWrite)
.setSubresourceRange(srr(base_level + 1));
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eTransfer,
{}, {}, {}, barrier);
image_layout_transition(cmd, image,
vk::ImageLayout::eUndefined,
vk::ImageLayout::eTransferDstOptimal,
range(base_level + 1));
// Blit base layer to mipmap layer
int mipmap_width = base_width >> 1;
@ -171,8 +150,8 @@ void Texture::from_buffer(vk::CommandBuffer cmd,
auto blit = vk::ImageBlit{}
.setSrcOffsets({ vk::Offset3D(0, 0, 0), vk::Offset3D(base_width, base_height, 1) })
.setDstOffsets({ vk::Offset3D(0, 0, 0), vk::Offset3D(mipmap_width, mipmap_height, 1)})
.setSrcSubresource(srl(base_level))
.setDstSubresource(srl(base_level + 1));
.setSrcSubresource(level(base_level))
.setDstSubresource(level(base_level + 1));
base_width = mipmap_width;
base_height = mipmap_height;
@ -180,29 +159,17 @@ void Texture::from_buffer(vk::CommandBuffer cmd,
cmd.blitImage(image, vk::ImageLayout::eTransferSrcOptimal, image, vk::ImageLayout::eTransferDstOptimal, blit, vk::Filter::eLinear);
// Transition base layer to shader readable
barrier
.setOldLayout(vk::ImageLayout::eTransferSrcOptimal)
.setNewLayout(vk::ImageLayout::eShaderReadOnlyOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eTransferWrite)
.setDstAccessMask(vk::AccessFlagBits::eShaderRead)
.setSubresourceRange(srr(base_level));
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eFragmentShader,
{}, {}, {}, barrier);
image_layout_transition(cmd, image,
vk::ImageLayout::eTransferSrcOptimal,
vk::ImageLayout::eShaderReadOnlyOptimal,
range(base_level));
}
// Transition final layer to shader readable
barrier
.setOldLayout(vk::ImageLayout::eTransferDstOptimal)
.setNewLayout(vk::ImageLayout::eShaderReadOnlyOptimal)
.setSrcAccessMask(vk::AccessFlagBits::eTransferWrite)
.setDstAccessMask(vk::AccessFlagBits::eShaderRead)
.setSubresourceRange(srr(base_level));
cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eFragmentShader,
{}, {}, {}, barrier);
image_layout_transition(cmd, image,
vk::ImageLayout::eTransferDstOptimal,
vk::ImageLayout::eShaderReadOnlyOptimal,
range(base_level));
}
void Texture::from_buffer(uint8_t *buffer, int width, int height, int byte_stride)

View File

@ -124,6 +124,8 @@ if(USE_SLANG)
../common/video/vulkan/vk_mem_alloc_implementation.cpp
../common/video/vulkan/vulkan_context.cpp
../common/video/vulkan/vulkan_context.hpp
../common/video/vulkan/vulkan_common.cpp
../common/video/vulkan/vulkan_common.hpp
../common/video/vulkan/vulkan_texture.cpp
../common/video/vulkan/vulkan_texture.hpp
../common/video/vulkan/vulkan_swapchain.cpp

View File

@ -263,6 +263,8 @@ list(APPEND SOURCES
../common/video/vulkan/vk_mem_alloc_implementation.cpp
../common/video/vulkan/vulkan_context.cpp
../common/video/vulkan/vulkan_context.hpp
../common/video/vulkan/vulkan_common.cpp
../common/video/vulkan/vulkan_common.hpp
../common/video/vulkan/vulkan_texture.cpp
../common/video/vulkan/vulkan_texture.hpp
../common/video/vulkan/vulkan_swapchain.cpp

View File

@ -454,6 +454,7 @@
<ClInclude Include="..\common\video\vulkan\slang_preset_ini.hpp" />
<ClInclude Include="..\common\video\vulkan\slang_shader.hpp" />
<ClInclude Include="..\common\video\vulkan\vulkan_context.hpp" />
<ClInclude Include="..\common\video\vulkan\vulkan_common.hpp" />
<ClInclude Include="..\common\video\vulkan\vulkan_pipeline_image.hpp" />
<ClInclude Include="..\common\video\vulkan\vulkan_shader_chain.hpp" />
<ClInclude Include="..\common\video\vulkan\vulkan_simple_output.hpp" />
@ -625,6 +626,7 @@
<ClCompile Include="..\common\video\vulkan\slang_shader.cpp" />
<ClCompile Include="..\common\video\vulkan\vk_mem_alloc_implementation.cpp" />
<ClCompile Include="..\common\video\vulkan\vulkan_context.cpp" />
<ClCompile Include="..\common\video\vulkan\vulkan_common.cpp" />
<ClCompile Include="..\common\video\vulkan\vulkan_hpp_storage.cpp" />
<ClCompile Include="..\common\video\vulkan\vulkan_pipeline_image.cpp" />
<ClCompile Include="..\common\video\vulkan\vulkan_shader_chain.cpp" />

View File

@ -315,6 +315,9 @@
<ClInclude Include="..\common\video\vulkan\vulkan_context.hpp">
<Filter>GUI\VideoDriver\Vulkan</Filter>
</ClInclude>
<ClInclude Include="..\common\video\vulkan\vulkan_common.hpp">
<Filter>GUI\VideoDriver\Vulkan</Filter>
</ClInclude>
<ClInclude Include="..\common\video\vulkan\vulkan_pipeline_image.hpp">
<Filter>GUI\VideoDriver\Vulkan</Filter>
</ClInclude>
@ -737,6 +740,9 @@
<ClCompile Include="..\common\video\vulkan\vulkan_context.cpp">
<Filter>GUI\VideoDriver\Vulkan</Filter>
</ClCompile>
<ClCompile Include="..\common\video\vulkan\vulkan_common.cpp">
<Filter>GUI\VideoDriver\Vulkan</Filter>
</ClCompile>
<ClCompile Include="..\common\video\vulkan\vulkan_hpp_storage.cpp">
<Filter>GUI\VideoDriver\Vulkan</Filter>
</ClCompile>