From ce3c61365233409b0da45a48e9879d4b359e765c Mon Sep 17 00:00:00 2001 From: Sylvain Doremus Date: Sat, 27 May 2023 02:16:00 +0200 Subject: [PATCH] Added a way to force memory barriers. --- include/RenderGraph/RecordContext.hpp | 24 ++++++++---- source/RenderGraph/RecordContext.cpp | 53 ++++++++++++++++++--------- 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/include/RenderGraph/RecordContext.hpp b/include/RenderGraph/RecordContext.hpp index 3e1ae1c..d2c7732 100644 --- a/include/RenderGraph/RecordContext.hpp +++ b/include/RenderGraph/RecordContext.hpp @@ -93,30 +93,36 @@ namespace crg CRG_API void memoryBarrier( VkCommandBuffer commandBuffer , ImageViewId const & view , VkImageLayout initialLayout - , LayoutState const & wantedState ); + , LayoutState const & wantedState + , bool force = false ); CRG_API void memoryBarrier( VkCommandBuffer commandBuffer , ImageId const & image , VkImageSubresourceRange const & subresourceRange , VkImageLayout initialLayout - , LayoutState const & wantedState ); + , LayoutState const & wantedState + , bool force = false ); CRG_API void memoryBarrier( VkCommandBuffer commandBuffer , ImageId const & image , VkImageViewType viewType , VkImageSubresourceRange const & subresourceRange , VkImageLayout initialLayout - , LayoutState const & wantedState ); + , LayoutState const & wantedState + , bool force = false ); CRG_API void memoryBarrier( VkCommandBuffer commandBuffer , ImageViewId const & view - , LayoutState const & wantedState ); + , LayoutState const & wantedState + , bool force = false ); CRG_API void memoryBarrier( VkCommandBuffer commandBuffer , ImageId const & image , VkImageSubresourceRange const & subresourceRange - , LayoutState const & wantedState ); + , LayoutState const & wantedState + , bool force = false ); CRG_API void memoryBarrier( VkCommandBuffer commandBuffer , ImageId const & image , VkImageViewType viewType , VkImageSubresourceRange const & subresourceRange - , LayoutState const & wantedState ); + , LayoutState const & wantedState + , bool force = false ); //@} /** *\name Buffers @@ -127,11 +133,13 @@ namespace crg , BufferSubresourceRange const & subresourceRange , VkAccessFlags initialMask , VkPipelineStageFlags initialStage - , AccessState const & wantedState ); + , AccessState const & wantedState + , bool force = false ); CRG_API void memoryBarrier( VkCommandBuffer commandBuffer , VkBuffer buffer , BufferSubresourceRange const & subresourceRange - , AccessState const & wantedState ); + , AccessState const & wantedState + , bool force = false ); //@} //@} CRG_API GraphContext & getContext()const; diff --git a/source/RenderGraph/RecordContext.cpp b/source/RenderGraph/RecordContext.cpp index 138b269..d6a839d 100644 --- a/source/RenderGraph/RecordContext.cpp +++ b/source/RenderGraph/RecordContext.cpp @@ -189,28 +189,32 @@ namespace crg void RecordContext::memoryBarrier( VkCommandBuffer commandBuffer , ImageViewId const & view , VkImageLayout initialLayout - , LayoutState const & wantedState ) + , LayoutState const & wantedState + , bool force ) { memoryBarrier( commandBuffer , view.data->image , view.data->info.viewType , view.data->info.subresourceRange , initialLayout - , wantedState ); + , wantedState + , force ); } void RecordContext::memoryBarrier( VkCommandBuffer commandBuffer , ImageId const & image , VkImageSubresourceRange const & subresourceRange , VkImageLayout initialLayout - , LayoutState const & wantedState ) + , LayoutState const & wantedState + , bool force ) { memoryBarrier( commandBuffer , image , VkImageViewType( image.data->info.imageType ) , subresourceRange , initialLayout - , wantedState ); + , wantedState + , force ); } void RecordContext::memoryBarrier( VkCommandBuffer commandBuffer @@ -218,7 +222,8 @@ namespace crg , VkImageViewType viewType , VkImageSubresourceRange const & subresourceRange , VkImageLayout initialLayout - , LayoutState const & wantedState ) + , LayoutState const & wantedState + , bool force ) { auto & resources = getResources(); @@ -241,8 +246,10 @@ namespace crg , getStageMask( initialLayout ) }; } - if ( from.layout != wantedState.layout - && wantedState.layout != VK_IMAGE_LAYOUT_UNDEFINED ) + if ( force + || ( ( from.layout != wantedState.layout + || from.state.pipelineStage != wantedState.state.pipelineStage ) + && wantedState.layout != VK_IMAGE_LAYOUT_UNDEFINED ) ) { VkImageMemoryBarrier barrier{ VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER , nullptr @@ -273,41 +280,47 @@ namespace crg void RecordContext::memoryBarrier( VkCommandBuffer commandBuffer , ImageViewId const & view - , LayoutState const & wantedState ) + , LayoutState const & wantedState + , bool force ) { memoryBarrier( commandBuffer , view.data->image , view.data->info.viewType , view.data->info.subresourceRange , VK_IMAGE_LAYOUT_UNDEFINED - , wantedState ); + , wantedState + , force ); } void RecordContext::memoryBarrier( VkCommandBuffer commandBuffer , ImageId const & image , VkImageSubresourceRange const & subresourceRange - , LayoutState const & wantedState ) + , LayoutState const & wantedState + , bool force ) { memoryBarrier( commandBuffer , image , VkImageViewType( image.data->info.imageType ) , subresourceRange , VK_IMAGE_LAYOUT_UNDEFINED - , wantedState ); + , wantedState + , force ); } void RecordContext::memoryBarrier( VkCommandBuffer commandBuffer , ImageId const & image , VkImageViewType viewType , VkImageSubresourceRange const & subresourceRange - , LayoutState const & wantedState ) + , LayoutState const & wantedState + , bool force ) { memoryBarrier( commandBuffer , image , viewType , subresourceRange , VK_IMAGE_LAYOUT_UNDEFINED - , wantedState ); + , wantedState + , force ); } void RecordContext::memoryBarrier( VkCommandBuffer commandBuffer @@ -315,7 +328,8 @@ namespace crg , BufferSubresourceRange const & subresourceRange , VkAccessFlags initialMask , VkPipelineStageFlags initialStage - , AccessState const & wantedState ) + , AccessState const & wantedState + , bool force ) { auto & resources = getResources(); @@ -332,8 +346,9 @@ namespace crg from = { initialMask, initialStage }; } - if ( from.access != wantedState.access - || from.pipelineStage != wantedState.pipelineStage ) + if ( force + || ( from.access != wantedState.access + || from.pipelineStage != wantedState.pipelineStage ) ) { VkBufferMemoryBarrier barrier{ VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER , nullptr @@ -363,14 +378,16 @@ namespace crg void RecordContext::memoryBarrier( VkCommandBuffer commandBuffer , VkBuffer buffer , BufferSubresourceRange const & subresourceRange - , AccessState const & wantedState ) + , AccessState const & wantedState + , bool force ) { memoryBarrier( commandBuffer , buffer , subresourceRange , 0u , VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT - , wantedState ); + , wantedState + , force ); } GraphContext & RecordContext::getContext()const