---
title: MTLFence
framework: metal
role: symbol
role_heading: Protocol
path: metal/mtlfence
---

# MTLFence

A synchronization mechanism that orders memory operations between GPU passes.

## Declaration

```swift
protocol MTLFence : NSObjectProtocol, Sendable
```

## Mentioned in

Synchronizing passes with a fence Tracking the resource residency of argument buffers Synchronizing stages within a pass

## Overview

Overview Create a fence by calling the makeFence() method. A fence instructs the GPU to finish running specific stages of a pass before starting stages from another pass. This is useful when a pass needs to wait before loading data from a resource until after another pass stores data to that resource. For example, to synchronize two passes where one modifies a texture and another reads it, use a fence with the following steps: Encode the producing pass and update a fence after the commands that modify the texture. Encode the consuming pass and wait for the same fence before the commands that read from that texture. Apple family GPUs can update and respond to fences on a per-stage basis. This means a GPU can delay running the commands for specific stages that need to wait for another pass while it runs other stages from the same pass. For example, a GPU can run the vertex stage of a pass while the fragment stage waits until another pass updates a fence. For more information about Apple family GPUs, see the supportsFamily(_:) method, and the Metal feature set tables PDF or the equivalent Metal feature set tables spreadsheet. The following encoder types support the updateFence(_:afterEncoderStages:) and waitForFence(_:beforeEncoderStages:) methods by conforming to the MTL4CommandEncoder protocol: MTL4RenderCommandEncoder MTL4ComputeCommandEncoder MTL4MachineLearningCommandEncoder The encoder types that inherit the MTLCommandEncoder protocol each have methods for updating and waiting for fences.  |  |   |  |   |  |   |  |   |  |   |  |  note: Earlier versions of Metal support hazard tracking for work you encode and commit with MTLCommandEncoder, MTLCommandBuffer, and MTLCommandQueue instances, which means you don’t need to synchronize memory operations for resources with a hazardTrackingMode property that’s equal to hazardTrackingModeTracked. Submit producing passes before consuming passes Send producing passes that update a fence to a queue before submitting consuming passes that wait for a fence. When encoding the producing and consuming passes into the same command buffer, encode the producing passes before the consuming passes. When submitting the producing and consuming passes in different command buffers, commit the command buffers with the producing passes before those with the consuming passes. note: When submitting multiple command buffers to an MTL4CommandQueue at the same time, such as with its commit:count:options: method, the method commits the command buffers in array order. Fences can synchronize passes you submit to different queues, including MTL4CommandQueue, MTLCommandQueue, or a combination of both. tip: Consider synchronizing passes that you submit to different queues with an MTLEvent instance instead.

## Topics

### Identifying a fence

- [device](metal/mtlfence/device.md)
- [label](metal/mtlfence/label.md)

### Selecting render stages

- [MTLRenderStages](metal/mtlrenderstages.md)

## Relationships

### Inherits From

- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Synchronizing with barriers and fences

- [Synchronizing stages within a pass](metal/synchronizing-stages-within-a-pass.md)
- [Synchronizing passes with a fence](metal/synchronizing-passes-with-a-fence.md)
- [Synchronizing passes with consumer barriers](metal/synchronizing-passes-with-consumer-barriers.md)
- [Synchronizing passes with producer barriers](metal/synchronizing-passes-with-producer-barriers.md)
- [Synchronizing CPU and GPU work](metal/synchronizing-cpu-and-gpu-work.md)
- [Implementing a multistage image filter using heaps and fences](metal/implementing-a-multistage-image-filter-using-heaps-and-fences.md)
- [MTLStages](metal/mtlstages.md)
- [MTLRenderStages](metal/mtlrenderstages.md)
- [MTLBarrierScope](metal/mtlbarrierscope.md)
- [MTL4VisibilityOptions](metal/mtl4visibilityoptions.md)
