Contents

MTLHazardTrackingMode

Options that control whether Metal automatically tracks and prevents memory hazards for resources.

Declaration

enum MTLHazardTrackingMode

Overview

Hazard tracking helps prevent race conditions by managing memory dependencies between commands. When you enable tracking for a resource, Metal automatically delays write operations until previous read operations finish, and prevents subsequent commands from running until write operations complete.

Metal applies hazard tracking to resources you create with MTLHazardTrackingMode.tracked, but only when you submit commands that use those resources to an MTLCommandQueue. Metal doesn’t track resources you create with MTLHazardTrackingMode.untracked.

Metal doesn’t apply hazard tracking to commands you submit to an MTL4CommandQueue, even when those commands use tracked resources.

Enable hazard tracking for a resource

You can create individual resources with tracking by calling the appropriate MTLDevice factory method (see Resource creation), or a factory method of an MTLHeap instance that you create with hazard tracking. Enable hazard tracking for an individual resource or heap by adding MTLHazardTrackingMode.tracked to an MTLResourceOptions instance.

Some factory methods have a parameter for resource options, such as makeBuffer(length:options:). Other factory methods have a parameter for a descriptor type, which has an MTLResourceOptions property. For example, to create an MTLTexture instance with hazard tracking:

  1. Create an MTLTextureDescriptor instance.

  2. Add the MTLHazardTrackingMode.tracked option to that descriptor’s resourceOptions property.

Choose between automatic safety and manual optimization

Hazard tracking provides automatic safety at the cost of some runtime overhead. You can improve the runtime performance of commands you send to an MTLCommandQueue by creating resources with MTLHazardTrackingMode.untracked and synchronizing access to those resources yourself.

For more information about synchronizing resources, see Resource synchronization.

Topics

Selecting the tracking mode

Initializers

See Also

Reading memory and storage properties