# Passing data between compute shader passes using a RWStructuredBuffer

**URL:** <https://forums.unrealengine.com/t/passing-data-between-compute-shader-passes-using-a-rwstructuredbuffer/1320353>\
**Category:** Rendering\
**Tags:** rhi, graphics-programming, unreal-engine, hlsl\
**Created:** [October 2, 2023, 8:37pm UTC](https://forums.unrealengine.com/t/passing-data-between-compute-shader-passes-using-a-rwstructuredbuffer/1320353 "2023-10-02T20:37:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![alecdutch](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@alecdutch](https://forums.unrealengine.com/u/alecdutch)\
**Post date:** [October 2, 2023, 8:37pm UTC](https://forums.unrealengine.com/t/passing-data-between-compute-shader-passes-using-a-rwstructuredbuffer/1320353/1 "2023-10-02T20:37:49Z")

</div>

Hi,

I’d like to share data between two passes in a compute shader. The first pass writes data to a RWStructuredBuffer while the second pass reads data from the buffer.

This appears to create a race condition in some cases. How can I allow the first pass to finish before the second pass starts?

C++ snippet:

```auto
////////// Pass1 //////////

//Fill the shader parameters structure with the cached data supplied by the client
FPass1ComputeShaderDeclaration::FParameters Pass1_Params;
Pass1_Params.DataBuffer = DataBufferUAV;

//Get a reference to our shader type from global shader map
TShaderMapRef<FPass1ComputeShaderDeclaration> Pass1(GetGlobalShaderMap(GMaxRHIFeatureLevel));

//Dispatch the compute shader (used for a 256x256 texture = 65536 )
FComputeShaderUtils::Dispatch(RHICmdList, Pass1, Pass1_Params, FIntVector(65536 / NUM_THREADS_PER_GROUP_DIMENSION_X, 1, 1));

////////// Transition the buffer?? ////////// 
//I gave this a shot, but it doesnt seem to prevent the race condition
RHICmdList.TransitionResource(ERHIAccess::ERWBarrier, EResourceTransitionPipeline::EComputeToCompute, DataBufferUAV);

////////// Pass2 //////////

//Fill the shader parameters structure with the cached data supplied by the client
FPass2ComputeShaderDeclaration::FParameters Pass2_Params;
Pass2_Params.DataBuffer = DataBufferUAV;

//Get a reference to our shader type from global shader map
TShaderMapRef<FPass2ComputeShaderDeclaration> Pass2(GetGlobalShaderMap(GMaxRHIFeatureLevel));

//Dispatch the compute shader (used for a 256x256 texture = 65536 )
FComputeShaderUtils::Dispatch(RHICmdList, Pass2, Pass2_Params,
FIntVector(65536 / NUM_THREADS_PER_GROUP_DIMENSION_X, 1, 1));

```
