Contents

waitForFence(_:before:)

Encodes a command that instructs the GPU to pause before starting one or more stages of the render pass until a pass updates a fence.

Declaration

func waitForFence(_ fence: any MTLFence, before stages: MTLRenderStages)

Parameters

  • fence:

    A fence that the pass waits for before running the stages you pass to stages.

  • stages:

    The render stages that need to wait for another pass to update fence before they run.

Discussion

Synchronize memory operations of a render pass that access resources with an MTLFence. This method instructs the GPU to wait until another pass updates fence before running the stages you pass to the stages parameter. The fence indicates when the pass can access those resources without a race condition.

For more information about synchronization with fences, see:

Reuse a fence by waiting first and updating second

When encoding a render pass that reuses a fence, wait for other passes to update the fence before repurposing that fence to notify subsequent passes with an update:

  1. Call the waitForFence(_:before:) method before encoding commands that need to wait for other passes.

  2. Call the updateFence(_:after:) method after encoding commands that later passes depend on.

The GPU driver evaluates the fences that apply to the pass and the commands that depend on those fences when your app commits the enclosing MTLCommandBuffer.

To synchronize different stages within a single pass, create an intrapass barrier because a fence can only synchronize memory operations between different passes. For more information, see Synchronizing stages within a pass.

See Also

Preventing resource access conflicts