1
$\begingroup$

There are four kinds of memory barriers in DirectX. Their names and definitions are:

  • DeviceMemoryBarrier: Blocks execution of all threads in a group until all device memory accesses have been completed.
  • DeviceMemoryBarrierWithGroupSync: Blocks execution of all threads in a group until all device memory accesses have been completed and all threads in the group have reached this call.
  • GroupMemoryBarrier: Blocks execution of all threads in a group until all group shared accesses have been completed.
  • GroupMemoryBarrierWithGroupSync: Blocks execution of all threads in a group until all group shared accesses have been completed and all threads in the group have reached this call.

From those definitions, it looks like the only differences are the types of memories accesses they are blocking and whether the command call is synchronized. Taking the following shader code, which use groupshared memory to process texture data, as an example:

Texture2D gInput;
RWTexture2D<float4> gOutput;
groupshared float4 gCache[256];
[numthreads(256,1,1)]
void HorzBlurCS(int3 groupThreadID : SV_GroupThreadID,
                int3 dispatchThreadID : SV_DispatchThreadID)
{
  gCache[groupThreadID.x] = gInput[dispatchThreadID.xy];

  // ! synchronize threads here
  
  // blur
  float4 blurColor = 0.25*gCache[groupThreadID.x-1] + 0.5*gCache[groupThreadID.x] + 0.25*gCache[groupThreadID.x+1];
  
  gInput[dispatchThreadID.xy] = blurColor;
}

Here are my questions:

  1. In the first line gCache[groupThreadID.x] = gInput[dispatchThreadID.xy], I am accessing both device and group memory - reading from the first and writing to the second. In that case, should I choose GroupMemoryBarrier or DeviceMemoryBarrier?
  2. Should I use WithGroupSync version here? If not, what's the use case of that version?
$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.