Vulkan: Cleanup image barrier code (#988)

This commit is contained in:
goeiecool9999 2023-10-16 07:33:12 +02:00 committed by GitHub
parent db44a2d130
commit d4a2a8e8de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 53 deletions

View File

@ -2801,47 +2801,18 @@ void VulkanRenderer::ClearColorImageRaw(VkImage image, uint32 sliceIndex, uint32
{ {
draw_endRenderPass(); draw_endRenderPass();
VkImageMemoryBarrier barrier = {}; VkImageSubresourceRange subresourceRange{};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
barrier.oldLayout = inputLayout; subresourceRange.baseMipLevel = mipIndex;
barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; subresourceRange.levelCount = 1;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; subresourceRange.baseArrayLayer = sliceIndex;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; subresourceRange.layerCount = 1;
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; 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);
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); vkCmdClearColorImage(m_state.currentCommandBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &color, 1, &subresourceRange);
VkImageSubresourceRange imageRange{}; 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);
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);
} }
void VulkanRenderer::ClearColorImage(LatteTextureVk* vkTexture, uint32 sliceIndex, uint32 mipIndex, const VkClearColorValue& color, VkImageLayout outputLayout) void VulkanRenderer::ClearColorImage(LatteTextureVk* vkTexture, uint32 sliceIndex, uint32 mipIndex, const VkClearColorValue& color, VkImageLayout outputLayout)

View File

@ -906,10 +906,8 @@ private:
} }
template<uint32 TSrcSyncOp, uint32 TDstSyncOp> 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 srcStages = 0;
VkPipelineStageFlags dstStages = 0; VkPipelineStageFlags dstStages = 0;
@ -922,12 +920,8 @@ private:
barrier_calcStageAndMask<TSrcSyncOp>(srcStages, imageMemBarrier.srcAccessMask); barrier_calcStageAndMask<TSrcSyncOp>(srcStages, imageMemBarrier.srcAccessMask);
barrier_calcStageAndMask<TDstSyncOp>(dstStages, imageMemBarrier.dstAccessMask); barrier_calcStageAndMask<TDstSyncOp>(dstStages, imageMemBarrier.dstAccessMask);
imageMemBarrier.image = imageVk; imageMemBarrier.image = imageVk;
imageMemBarrier.subresourceRange.aspectMask = subresourceLayers.aspectMask; imageMemBarrier.subresourceRange = subresourceRange;
imageMemBarrier.subresourceRange.baseArrayLayer = subresourceLayers.baseArrayLayer; imageMemBarrier.oldLayout = oldLayout;
imageMemBarrier.subresourceRange.layerCount = subresourceLayers.layerCount;
imageMemBarrier.subresourceRange.baseMipLevel = subresourceLayers.mipLevel;
imageMemBarrier.subresourceRange.levelCount = 1;
imageMemBarrier.oldLayout = vkTexture->GetImageLayout(imageMemBarrier.subresourceRange);
imageMemBarrier.newLayout = newLayout; imageMemBarrier.newLayout = newLayout;
vkCmdPipelineBarrier(m_state.currentCommandBuffer, vkCmdPipelineBarrier(m_state.currentCommandBuffer,
@ -936,8 +930,23 @@ private:
0, NULL, 0, NULL,
0, NULL, 0, NULL,
1, &imageMemBarrier); 1, &imageMemBarrier);
}
vkTexture->SetImageLayout(imageMemBarrier.subresourceRange, newLayout); 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(subresourceRange, newLayout);
} }