Article

Choosing a Resource Storage Mode in macOS

Choose an appropriate storage mode for your macOS resources.

Overview

macOS devices can have multiple GPUs, each with a unified or discrete memory model. In a unified memory model, the CPU and the GPU share system memory. However, CPU and GPU access to that memory depends on the storage mode you choose for your resources. In a discrete memory model, system memory is separate from video memory. System memory is accessible to both the CPU and the GPU, but video memory is accessible only to the GPU.

Despite the differences between unified and discrete memory models, you don't need to write code that specifically targets one or the other. Metal’s resource storage mode API works for both.

Understand the Shared Mode

In both memory models, a resource with a MTLStorageModeShared mode resides in system memory accessible to both the CPU and the GPU.

A system diagram showing the memory location and access permissions for a shared resource in macOS.

Understand the Private Mode

A resource with a MTLStorageModePrivate mode is accessible only to the GPU. In a unified memory model, this resource resides in system memory. In a discrete memory model, it resides in video memory. In both memory models, Metal optimizes GPU access to private resources.

Two system diagrams showing the memory locations and access permissions for a private resource in macOS. The top diagram shows a unified memory model and the bottom diagram shows a discrete memory model.

In a discrete memory model, Metal always attempts to store private resources in video memory. However, under certain memory constraints, Metal may evict a private resource into system memory. When you use a private resource that Metal previously evicted, Metal attempts to copy it back into video memory before you use it.

Understand the Managed Mode

In a unified memory model, a resource with a MTLStorageModeManaged mode resides in system memory accessible to both the CPU and the GPU.

In a discrete memory model, a managed resource exists as a synchronized pair of memory allocations. One copy of the resource resides in system memory accessible only to the CPU; the other resides in video memory accessible only to the GPU. However, you don’t manage the copies separately; Metal creates a single MTLResource object for both copies.

In both memory models, Metal optimizes CPU and GPU access to managed resources. However, you must explicitly synchronize a managed resource after modifying its contents with the CPU or the GPU. For information about synchronizing a managed resource, see Synchronizing a Managed Resource.

Two system diagrams showing the memory locations and access permissions for a managed resource in macOS. The top diagram shows a unified memory model and the bottom diagram shows a discrete memory model.

Choose a Resource Storage Mode for Buffers

For information about setting a storage mode, see Setting Resource Storage Modes. Several options are available, depending on your buffer access needs.

Accessed exclusively by the GPU. Choose the MTLStorageModePrivate mode if you populate your buffer with the GPU through a compute, render, or blit pass. This case is common for intermediary buffers used between passes.

Populated once by the CPU and accessed frequently by the GPU. Choose the MTLStorageModeManaged mode. First, populate the buffer’s data with the CPU and then synchronize the buffer. Finally, access the buffer's data with the GPU.

Changes frequently, is relatively small, and is accessed by both the CPU and the GPU. Choose the MTLStorageModeShared mode.

Changes frequently , is relatively large, and is accessed by both the CPU and the GPU. Choose the MTLStorageModeManaged mode. Always synchronize the buffer after modifying its contents with the CPU or the GPU.

For more information, see Synchronizing a Managed Resource.

Choose a Resource Storage Mode for Textures

For information about setting a storage mode, see Setting Resource Storage Modes. You can create textures with a MTLStorageModeManaged or MTLStorageModePrivate mode only, but not with MTLStorageModeShared. Several options are available, depending on your texture access needs.

Accessed exclusively by the GPU. Choose the MTLStorageModePrivate mode if you populate your texture with the GPU through a compute, render, or blit pass. This case is common for render targets and drawables.

Populated once by the CPU and accessed frequently by the GPU. Use the CPU to create a buffer with MTLStorageModeShared mode and populate its contents with your texture data. Then, use the GPU to copy the buffer’s contents into a texture with a MTLStorageModePrivate mode.

Accessed frequently by both the CPU and the GPU. Choose the MTLStorageModeManaged mode. Always synchronize the texture after modifying its contents with the CPU or the GPU.

For more information, see Copying Data to a Private Resource and Synchronizing a Managed Resource.

See Also

Resource Storage Modes by Platform

Choosing a Resource Storage Mode in iOS and tvOS

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