From d4a2a8e8de39821a22dd2ad82d2186796e566af5 Mon Sep 17 00:00:00 2001 From: goeiecool9999 <7033575+goeiecool9999@users.noreply.github.com> Date: Mon, 16 Oct 2023 07:33:12 +0200 Subject: [PATCH] Vulkan: Cleanup image barrier code (#988) --- .../Latte/Renderer/Vulkan/VulkanRenderer.cpp | 47 ++++--------------- .../HW/Latte/Renderer/Vulkan/VulkanRenderer.h | 39 +++++++++------ 2 files changed, 33 insertions(+), 53 deletions(-) diff --git a/src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp b/src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp index d084a399..052ca21a 100644 --- a/src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp +++ b/src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp @@ -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; + VkImageSubresourceRange subresourceRange{}; + subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + subresourceRange.baseMipLevel = mipIndex; + subresourceRange.levelCount = 1; + subresourceRange.baseArrayLayer = sliceIndex; + subresourceRange.layerCount = 1; - VkPipelineStageFlags srcStages = 0; - VkPipelineStageFlags dstStages = 0; - barrier.srcAccessMask = 0; - barrier.dstAccessMask = 0; - barrier_calcStageAndMask(srcStages, barrier.srcAccessMask); - barrier_calcStageAndMask(dstStages, barrier.dstAccessMask); + barrier_image(image, subresourceRange, inputLayout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); - 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{}; - 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(srcStages, barrier.srcAccessMask); - barrier_calcStageAndMask(dstStages, barrier.dstAccessMask); - vkCmdPipelineBarrier(m_state.currentCommandBuffer, srcStages, dstStages, 0, 0, nullptr, 0, nullptr, 1, &barrier); + barrier_image(image, subresourceRange, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, outputLayout); } void VulkanRenderer::ClearColorImage(LatteTextureVk* vkTexture, uint32 sliceIndex, uint32 mipIndex, const VkClearColorValue& color, VkImageLayout outputLayout) diff --git a/src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.h b/src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.h index 24008ee3..3d68f844 100644 --- a/src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.h +++ b/src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.h @@ -906,10 +906,8 @@ private: } template - 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; @@ -922,22 +920,33 @@ private: barrier_calcStageAndMask(srcStages, imageMemBarrier.srcAccessMask); barrier_calcStageAndMask(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); + } - vkTexture->SetImageLayout(imageMemBarrier.subresourceRange, newLayout); + template + 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(imageVk, subresourceRange, vkTexture->GetImageLayout(subresourceRange), newLayout); + + vkTexture->SetImageLayout(subresourceRange, newLayout); }