Metal useResource vs. MTLFence

Hello, I'm tracking down a bug where useResource doesn't seem to apply proper synchronization when a resource is produced by the render pass then consumed by the compute pass, but when I use MTLFence between the to signal and wait between the render/compute encoders, the artifact goes away.

The resource is created with MTLHazardTrackingModeTracked and useResource is called on the compute encoder after the render pass. Metal API Validation doesn't report any warnings/errors.

Am I misunderstanding the difference between the two APIs? I dug through the Metal documentation and it looks like useResource should handle synchronization given the resource has MTLHazardTrackingModeTracked but on the other hand, MTLFence should be used to ensure proper synchronization between command encoders. Can someone can clarify the difference between the two APIs and when to use them.

Did you provide MTLResourceUsageWrite for useResource on render command encoder?

Hello,

For what you're trying to do, and results you're getting, using MTLFence to synchronize between the render and compute encoders is the most explicit way to avoid the artifacts.

See Implementing a Multistage Image Filter Using Heaps and Fences for an example that's similar.

It would be good know though if hazard tracking is sufficient and possibly more efficient (non-blocking). That said, will reach out for feedback and share back here as needed.

It turns out that the render command encoder's useResource(_:usage:) function doesn't do hazard tracking and has been deprecated.

Instead you can use useResource(_:usage:stages:).

Metal useResource vs. MTLFence
 
 
Q