Skip to content

Commit

Permalink
Vulkan: Cleanup image barrier code (#988)
Browse files Browse the repository at this point in the history
  • Loading branch information
goeiecool9999 authored Oct 16, 2023
1 parent db44a2d commit d4a2a8e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 56 deletions.
53 changes: 12 additions & 41 deletions src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2801,47 +2801,18 @@ void VulkanRenderer::ClearColorImageRaw(VkImage image, uint32 sliceIndex, uint32
{
draw_endRenderPass();

VkImageMemoryBarrier barrier = {};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.oldLayout = inputLayout;
barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = image;
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
barrier.subresourceRange.baseMipLevel = mipIndex;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.baseArrayLayer = sliceIndex;
barrier.subresourceRange.layerCount = 1;

VkPipelineStageFlags srcStages = 0;
VkPipelineStageFlags dstStages = 0;
barrier.srcAccessMask = 0;
barrier.dstAccessMask = 0;
barrier_calcStageAndMask<SYNC_OP::ANY_TRANSFER | SYNC_OP::IMAGE_READ | SYNC_OP::IMAGE_WRITE>(srcStages, barrier.srcAccessMask);
barrier_calcStageAndMask<SYNC_OP::ANY_TRANSFER>(dstStages, barrier.dstAccessMask);

vkCmdPipelineBarrier(m_state.currentCommandBuffer, srcStages, dstStages, 0, 0, nullptr, 0, nullptr, 1, &barrier);

VkImageSubresourceRange imageRange{};
imageRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imageRange.baseArrayLayer = sliceIndex;
imageRange.layerCount = 1;
imageRange.baseMipLevel = mipIndex;
imageRange.levelCount = 1;

vkCmdClearColorImage(m_state.currentCommandBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &color, 1, &imageRange);

barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
barrier.newLayout = outputLayout;

srcStages = 0;
dstStages = 0;
barrier.srcAccessMask = 0;
barrier.dstAccessMask = 0;
barrier_calcStageAndMask<SYNC_OP::ANY_TRANSFER>(srcStages, barrier.srcAccessMask);
barrier_calcStageAndMask<SYNC_OP::ANY_TRANSFER | SYNC_OP::IMAGE_READ | SYNC_OP::IMAGE_WRITE>(dstStages, barrier.dstAccessMask);
vkCmdPipelineBarrier(m_state.currentCommandBuffer, srcStages, dstStages, 0, 0, nullptr, 0, nullptr, 1, &barrier);
VkImageSubresourceRange subresourceRange{};
subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
subresourceRange.baseMipLevel = mipIndex;
subresourceRange.levelCount = 1;
subresourceRange.baseArrayLayer = sliceIndex;
subresourceRange.layerCount = 1;

barrier_image<SYNC_OP::ANY_TRANSFER | SYNC_OP::IMAGE_READ | SYNC_OP::IMAGE_WRITE, SYNC_OP::ANY_TRANSFER>(image, subresourceRange, inputLayout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);

vkCmdClearColorImage(m_state.currentCommandBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &color, 1, &subresourceRange);

barrier_image<ANY_TRANSFER, SYNC_OP::ANY_TRANSFER | SYNC_OP::IMAGE_READ | SYNC_OP::IMAGE_WRITE>(image, subresourceRange, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, outputLayout);
}

void VulkanRenderer::ClearColorImage(LatteTextureVk* vkTexture, uint32 sliceIndex, uint32 mipIndex, const VkClearColorValue& color, VkImageLayout outputLayout)
Expand Down
39 changes: 24 additions & 15 deletions src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -906,10 +906,8 @@ class VulkanRenderer : public Renderer
}

template<uint32 TSrcSyncOp, uint32 TDstSyncOp>
void barrier_image(LatteTextureVk* vkTexture, VkImageSubresourceLayers& subresourceLayers, VkImageLayout newLayout)
void barrier_image(VkImage imageVk, VkImageSubresourceRange& subresourceRange, VkImageLayout oldLayout, VkImageLayout newLayout)
{
VkImage imageVk = vkTexture->GetImageObj()->m_image;

VkPipelineStageFlags srcStages = 0;
VkPipelineStageFlags dstStages = 0;

Expand All @@ -922,22 +920,33 @@ class VulkanRenderer : public Renderer
barrier_calcStageAndMask<TSrcSyncOp>(srcStages, imageMemBarrier.srcAccessMask);
barrier_calcStageAndMask<TDstSyncOp>(dstStages, imageMemBarrier.dstAccessMask);
imageMemBarrier.image = imageVk;
imageMemBarrier.subresourceRange.aspectMask = subresourceLayers.aspectMask;
imageMemBarrier.subresourceRange.baseArrayLayer = subresourceLayers.baseArrayLayer;
imageMemBarrier.subresourceRange.layerCount = subresourceLayers.layerCount;
imageMemBarrier.subresourceRange.baseMipLevel = subresourceLayers.mipLevel;
imageMemBarrier.subresourceRange.levelCount = 1;
imageMemBarrier.oldLayout = vkTexture->GetImageLayout(imageMemBarrier.subresourceRange);
imageMemBarrier.subresourceRange = subresourceRange;
imageMemBarrier.oldLayout = oldLayout;
imageMemBarrier.newLayout = newLayout;

vkCmdPipelineBarrier(m_state.currentCommandBuffer,
srcStages, dstStages,
0,
0, NULL,
0, NULL,
1, &imageMemBarrier);
srcStages, dstStages,
0,
0, NULL,
0, NULL,
1, &imageMemBarrier);
}

template<uint32 TSrcSyncOp, uint32 TDstSyncOp>
void barrier_image(LatteTextureVk* vkTexture, VkImageSubresourceLayers& subresourceLayers, VkImageLayout newLayout)
{
VkImage imageVk = vkTexture->GetImageObj()->m_image;

VkImageSubresourceRange subresourceRange;
subresourceRange.aspectMask = subresourceLayers.aspectMask;
subresourceRange.baseArrayLayer = subresourceLayers.baseArrayLayer;
subresourceRange.layerCount = subresourceLayers.layerCount;
subresourceRange.baseMipLevel = subresourceLayers.mipLevel;
subresourceRange.levelCount = 1;

barrier_image<TSrcSyncOp, TDstSyncOp>(imageVk, subresourceRange, vkTexture->GetImageLayout(subresourceRange), newLayout);

vkTexture->SetImageLayout(imageMemBarrier.subresourceRange, newLayout);
vkTexture->SetImageLayout(subresourceRange, newLayout);
}


Expand Down

0 comments on commit d4a2a8e

Please sign in to comment.