From 9107897b76a8cd2fa0495d7d1131b7063c8da3d6 Mon Sep 17 00:00:00 2001 From: Wojciech Figat Date: Tue, 22 Mar 2022 12:56:21 +0100 Subject: [PATCH] Add missing pipeline barriers after Dispatch on Vulkan to prevent race-conditions with UAVs --- Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp b/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp index e82cc6e24..8e28da63a 100644 --- a/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp +++ b/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp @@ -1107,6 +1107,10 @@ void GPUContextVulkan::Dispatch(GPUShaderProgramCS* shader, uint32 threadGroupCo vkCmdDispatch(cmdBuffer->GetHandle(), threadGroupCountX, threadGroupCountY, threadGroupCountZ); RENDER_STAT_DISPATCH_CALL(); + // Place a barrier between dispatches, so that UAVs can be read+write in subsequent passes + // TODO: optimize it by moving inputs/outputs into higher-layer so eg. Global SDF can manually optimize it + vkCmdPipelineBarrier(cmdBuffer->GetHandle(), VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 0, nullptr); + #if VK_ENABLE_BARRIERS_DEBUG LOG(Warning, "Dispatch"); #endif @@ -1141,6 +1145,10 @@ void GPUContextVulkan::DispatchIndirect(GPUShaderProgramCS* shader, GPUBuffer* b vkCmdDispatchIndirect(cmdBuffer->GetHandle(), bufferForArgsVulkan->GetHandle(), offsetForArgs); RENDER_STAT_DISPATCH_CALL(); + // Place a barrier between dispatches, so that UAVs can be read+write in subsequent passes + // TODO: optimize it by moving inputs/outputs into higher-layer so eg. Global SDF can manually optimize it + vkCmdPipelineBarrier(cmdBuffer->GetHandle(), VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 0, nullptr); + #if VK_ENABLE_BARRIERS_DEBUG LOG(Warning, "DispatchIndirect"); #endif