Synchronizing Metal's MTLRenderPassDescriptor Texture Read/Write on iOS

Hello! Here's my situation:

I am using an MTLRenderPassDescriptor to render to a 2D color attachment. My end goal is to be able to render this effect in the most efficient way possible:

Here's my current process:

  1. I draw to the output color attachment using the drawIndexedPrimitives(...) on the render encoder
  2. I now need to copy the output color attachment's color data to a separate texture such that I'll be able to sample it in the future. I'm using a custom tile shader that copies each imageblock to this auxilary texture
  3. I now run a fragment shader on the output color attachment which samples the auxilary texture at 3 separate offsets, once for the red value, once for the green value, and once for the blue value.

The Problem: The fragment shader is run concurrently with the tile shader and I'm not getting the desired effect since the fragment shader is sampling data that hasn't yet been written to by the tile shader

The Question: How can I tell the fragment shader to wait for the tile shader to finish working with all tiles before beginning its fragment work?

Current Solution: After calling dispatchThreadsPerTile(...) on the render encoder to run the tile shader, I end encoding. I then using a new MTLRenderCommandEncoder for the fragment shader. This is not ideal due to the overhead and memory bandwidth associated with ending/beginning a new render encoder. Also the debugger is telling me that these 2 encoders can be coalesced into 1. But how?

Here's what it looks like when I use 1 render pass (1 render command encoder). Strange artifacts can be seen on the edges of the letters. Note that this screenshot was taken during an animation so the artifacts are present from the previous frame of animation. This behavior is undesirable.

And here's what it looks like when I use 2 render passes. The artifacts are no longer present:

Final Note: I can't use the MTLRenderCommandEncoder's memoryBarrier(...) method since it's not available for iOS.

Synchronizing Metal's MTLRenderPassDescriptor Texture Read/Write on iOS
 
 
Q