MTLFence
A synchronization mechanism that orders memory operations between GPU passes.
Declaration
protocol MTLFence : NSObjectProtocol, SendableMentioned in
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:
The encoder types that inherit the MTLCommandEncoder protocol each have methods for updating and waiting for fences.
Encoder types | Update fence methods | Wait for fence methods |
|---|---|---|
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.
Fences can synchronize passes you submit to different queues, including MTL4CommandQueue, MTLCommandQueue, or a combination of both.
Topics
Identifying a fence
Selecting render stages
See Also
Synchronizing with barriers and fences
Synchronizing stages within a passSynchronizing passes with a fenceSynchronizing passes with consumer barriersSynchronizing passes with producer barriersSynchronizing CPU and GPU workImplementing a multistage image filter using heaps and fencesMTLStagesMTLRenderStagesMTLBarrierScopeMTL4VisibilityOptions