Article

Choosing a Resource Storage Mode in macOS

Choose an appropriate storage mode for your macOS resources.

Overview

macOS devices can have multiple integrated, discrete, or external GPUs. These devices have (or emulate) a discrete memory model in which system memory is separate from video memory.

The MTLStorageMode.shared mode defines system memory accessible to both the CPU and a GPU, whereas the MTLStorageMode.private mode defines video memory accessible only to a GPU. Additionally, the MTLStorageMode.managed mode defines a synchronized memory storage pair for a resource, with one copy of the resource in system memory and another in video memory. This mode results in fast CPU and GPU access to each copy of the resource.

The MTLStorageMode.shared, MTLStorageMode.private, and MTLStorageMode.managed storage modes are available to both built-in (integrated or discrete) and external GPUs, but their performance and bandwidth varies. Built-in GPUs access system memory via an internal bus (higher bandwidth), and external GPUs access system memory via an external Thunderbolt 3 bus (lower bandwidth). Thus, access to system memory can be faster for a built-in GPU and slower for an external GPU.

A system diagram of resource storage modes in macOS, showing the memory location and access permissions for resources configured with a shared, private, or managed mode.

Choose a Resource Storage Mode for Buffers

Several options are available, depending on your buffer access needs:

Accessed exclusively by a GPU. Choose MTLStorageMode.private mode. This case is common for GPU-generated data, such as per-patch tessellation factors.

Accessed exclusively by the CPU. Choose MTLStorageMode.shared mode. This case is rare and is usually an intermediary step in a blit operation.

Initialized once by the CPU and accessed frequently by a GPU. Use the CPU to initialize a source buffer with MTLStorageMode.shared mode, and then use a GPU to blit its data into a destination buffer with MTLStorageMode.private mode.

Changes frequently and is accessed by both the CPU and a built-in GPU. Choose MTLStorageMode.shared mode. The cost of a built-in GPU accessing system memory directly may be less than the cost of copying data from system memory to video memory. 

Changes frequently and is accessed by both the CPU and an external GPU. Choose MTLStorageMode.managed or MTLStorageMode.private mode. Unless your data access is sparse, the cost of an external GPU accessing system memory directly (MTLStorageMode.shared) is far more than the cost of copying data from system memory to video memory (MTLStorageMode.managed or MTLStorageMode.private).

Changes infrequently and is accessed by both the CPU and a GPU. Choose MTLStorageMode.managed mode. Always call one of the following synchronization methods after modifying the contents of a managed buffer:

  • After performing a CPU write, call the didModifyRange(_:) method to notify Metal about the specific range of data that was modified in system memory. This method allows Metal to update only that specific range of data in the copy of the buffer in video memory.

  • After encoding a GPU write, encode a blit operation that includes a call to the synchronize(resource:) method. This method allows Metal to modify the buffer in video memory and then update the copy of the buffer in system memory.

Choose a Resource Storage Mode for Textures

Several options are available, depending on your texture access needs:

Accessed exclusively by a GPU. Choose MTLStorageMode.private mode. This case is common for GPU-populated textures, such as drawables.

Accessed exclusively by the CPU. Choose MTLStorageMode.managed mode. This case is rare and is usually an intermediary step in a blit operation.

Initialized once by the CPU and accessed frequently by a GPU. Use the CPU to initialize a source texture with MTLStorageMode.managed mode, and then use a GPU to blit its data into a destination texture with MTLStorageMode.private mode.

Accessed frequently by both the CPU and a GPU. Choose MTLStorageMode.managed mode. Always call one of the following synchronization methods after modifying the contents of a managed texture:

See Also

Resource Storage Modes per Platform

Choosing a Resource Storage Mode in iOS and tvOS

Choose an appropriate storage mode for your iOS and tvOS resources.