Passing data between compute shader passes using a RWStructuredBuffer

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:

////////// 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));