<!--
{
  "availability" : [
    "iOS: 10.0.0 -",
    "iPadOS: 10.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.13.0 -",
    "tvOS: 10.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Metal",
  "identifier" : "/documentation/Metal/MTLFence",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Metal"
    ],
    "preciseIdentifier" : "c:objc(pl)MTLFence"
  },
  "title" : "MTLFence"
}
-->

# MTLFence

A synchronization mechanism that orders memory operations between GPU passes.

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

## Overview

Create a fence by calling the [`makeFence()`](/documentation/Metal/MTLDevice/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:

1. Encode the producing pass and update a fence after the commands that modify the texture.
2. 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(_:)`](/documentation/Metal/MTLDevice/supportsFamily(_:)) method, and
the [Metal feature set tables PDF](https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf)
or the equivalent [Metal feature set tables spreadsheet](https://developer.apple.com/metal/Metal-Feature-Set-Tables.zip).

The following encoder types support the [`updateFence(_:afterEncoderStages:)`](/documentation/Metal/MTL4CommandEncoder/updateFence(_:afterEncoderStages:)) and
[`waitForFence(_:beforeEncoderStages:)`](/documentation/Metal/MTL4CommandEncoder/waitForFence(_:beforeEncoderStages:)) methods by conforming to the
[`MTL4CommandEncoder`](/documentation/Metal/MTL4CommandEncoder) protocol:

- [`MTL4RenderCommandEncoder`](/documentation/Metal/MTL4RenderCommandEncoder)
- [`MTL4ComputeCommandEncoder`](/documentation/Metal/MTL4ComputeCommandEncoder)
- [`MTL4MachineLearningCommandEncoder`](/documentation/Metal/MTL4MachineLearningCommandEncoder)

The encoder types that inherit the [`MTLCommandEncoder`](/documentation/Metal/MTLCommandEncoder) protocol each have methods
for updating and waiting for fences.

|Encoder types                                                                       |Update fence methods                                                                                |Wait for fence methods                                                                               |
|------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------|
|``doc://com.apple.metal/documentation/Metal/MTLRenderCommandEncoder``               |``doc://com.apple.metal/documentation/Metal/MTLRenderCommandEncoder/updateFence(_:after:)``         |``doc://com.apple.metal/documentation/Metal/MTLRenderCommandEncoder/waitForFence(_:before:)``        |
|``doc://com.apple.metal/documentation/Metal/MTLComputeCommandEncoder``              |``doc://com.apple.metal/documentation/Metal/MTLComputeCommandEncoder/updateFence(_:)``              |``doc://com.apple.metal/documentation/Metal/MTLComputeCommandEncoder/waitForFence(_:)``              |
|``doc://com.apple.metal/documentation/Metal/MTLBlitCommandEncoder``                 |``doc://com.apple.metal/documentation/Metal/MTLBlitCommandEncoder/updateFence(_:)``                 |``doc://com.apple.metal/documentation/Metal/MTLBlitCommandEncoder/waitForFence(_:)``                 |
|``doc://com.apple.metal/documentation/Metal/MTLAccelerationStructureCommandEncoder``|``doc://com.apple.metal/documentation/Metal/MTLAccelerationStructureCommandEncoder/updateFence(_:)``|``doc://com.apple.metal/documentation/Metal/MTLAccelerationStructureCommandEncoder/waitForFence(_:)``|
|``doc://com.apple.metal/documentation/Metal/MTLResourceStateCommandEncoder``        |``doc://com.apple.metal/documentation/Metal/MTLResourceStateCommandEncoder/update(_:)``             |``doc://com.apple.metal/documentation/Metal/MTLResourceStateCommandEncoder/wait(for:)``              |

> Note:
> Earlier versions of Metal support hazard tracking for work you encode and commit
> with ``doc://com.apple.metal/documentation/Metal/MTLCommandEncoder``, ``doc://com.apple.metal/documentation/Metal/MTLCommandBuffer``, and ``doc://com.apple.metal/documentation/Metal/MTLCommandQueue`` instances, which means you don’t need
> to synchronize memory operations for resources with a ``doc://com.apple.metal/documentation/Metal/MTLResource/hazardTrackingMode`` property
> that’s equal to ``doc://com.apple.metal/documentation/Metal/MTLResourceOptions/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 ``doc://com.apple.metal/documentation/Metal/MTL4CommandQueue`` at the same time,
> such as with its ``doc://com.apple.metal/documentation/Metal/MTL4CommandQueue/commit:count:options:`` method,
> the method commits the command buffers in array order.

Fences can synchronize passes you submit to different queues, including
[`MTL4CommandQueue`](/documentation/Metal/MTL4CommandQueue), [`MTLCommandQueue`](/documentation/Metal/MTLCommandQueue), or a combination of both.

> Tip:
> Consider synchronizing passes that you submit to different queues with an ``doc://com.apple.metal/documentation/Metal/MTLEvent`` instance instead.

## Topics

### Identifying a fence

[`device`](/documentation/Metal/MTLFence/device)

The device object that created the fence.

[`label`](/documentation/Metal/MTLFence/label)

A string that identifies the fence.

### Selecting render stages

[`MTLRenderStages`](/documentation/Metal/MTLRenderStages)

The stages in a render pass that triggers a synchronization command.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)