Protocol

MTLResource

An allocation of memory that is accessible to a GPU.

Declaration

protocol MTLResource

Overview

When you execute commands on the GPU, those commands can only affect memory allocated as MTLResource objects. These Metal resources can only be modified by the MTLDevice that created them. Different resource types have different uses. The most common resource types are buffers (MTLBuffer), which are linear allocations of memory, and textures (MTLTexture), which hold structured image data. Don’t implement this protocol yourself; instead, create resources by calling methods on MTLDevice, MTLBuffer, or MTLTexture.

Topics

Identifying the Resource

var device: MTLDevice

The device object that created the resource.

Required.

var label: String?

A string that identifies the resource.

Required.

Reading Memory and Storage Properties

var cpuCacheMode: MTLCPUCacheMode

The CPU cache mode that defines the CPU mapping of the resource.

Required.

var storageMode: MTLStorageMode

The location and access permissions of the resource.

Required.

var hazardTrackingMode: MTLHazardTrackingMode

A mode that determines whether Metal tracks and synchronizes resource access.

Required.

var resourceOptions: MTLResourceOptions

The storage mode, CPU cache mode, and hazard tracking mode of the resource.

Required.

enum MTLCPUCacheMode

Options for the CPU cache mode that define the CPU mapping of the resource.

enum MTLStorageMode

Options for the memory location and access permissions for a resource.

enum MTLHazardTrackingMode

The options you use to specify the hazard tracking mode.

Setting the Purgeable State of the Resource

func setPurgeableState(MTLPurgeableState) -> MTLPurgeableState

Specifies or queries the resource’s purgeable state.

Required.

enum MTLPurgeableState

The purgeable state of the resource.

Managing Heap Resources

var heapOffset: Int

The distance, in bytes, from the beginning of the heap to the first byte of the resource, if you allocated the resource on a heap.

Required.

var heap: MTLHeap?

The heap on which the resource is allocated, if any.

Required.

func makeAliasable()

Allows future heap resource allocations to alias against the resource’s memory.

Required.

func isAliasable() -> Bool

A Boolean value that indicates whether future heap resource allocations may alias against the resource’s memory.

Required.

Querying the Allocated Size

var allocatedSize: Int

The size of the resource, in bytes.

Required.

Setting the Behavior of a New Resource

struct MTLResourceOptions

Optional arguments used to set the behavior of a resource.

Relationships

Inherits From

See Also

Resources

Setting Resource Storage Modes

Set a storage mode that defines the memory location and access permissions of a resource.

Copying Data to a Private Resource

Use a blit command encoder to copy buffer or texture data to a private resource.

Synchronizing a Managed Resource

Synchronize the contents of a managed resource for the CPU or GPU.

Transferring Data Between Connected GPUs

Use high-speed connections between GPUs to transfer data quickly.

Reducing the Memory Footprint of Metal Apps

Learn best practices for using memory efficiently in iOS and tvOS.

protocol MTLBlitCommandEncoder

An encoder that encodes memory copying, filtering, and fill commands.

protocol MTLResourceStateCommandEncoder

An encoder that encodes commands that modify resource configurations.

Buffers

Create and manipulate unstructured GPU resources.

Textures

Create and manipulate structured GPU resources.

Indirect Command Buffers

Recoup encoding time by reusing commands, or create a GPU-driven rendering pipeline by generating commands on the GPU.

Heaps

Create a single allocation of memory from which you can suballocate resources.

Synchronization

Manage access to resources in your app to avoid data hazards.