Framework

Metal

Render advanced 3D graphics and perform data-parallel computations using graphics processors.

Overview

Graphics processors (GPUs) are designed to quickly render graphics and perform data-parallel calculations. Use the Metal framework when you need to communicate directly with the GPUs available on a device. Apps that render complex scenes or that perform advanced scientific calculations can use this power to achieve maximum performance. Such apps include:

  • Games that render sophisticated 3D environments

  • Video processing apps, like Final Cut Pro

  • Data-crunching apps, such as those used to perform scientific research

Metal works hand-in-hand with other frameworks that supplement its capability. Use MetalKit to simplify the task of getting your Metal content onscreen. Use Metal Performance Shaders to implement custom rendering functions or to take advantage of a large library of existing functions.

Many high level Apple frameworks are built on top of Metal to take advantage of its performance, including Core Image, SpriteKit, and SceneKit. Using one of these high-level frameworks shields you from the details of GPU programming, but writing custom Metal code enables you to achieve the highest level of performance.

Topics

Essentials

Basic Tasks and Concepts

Get familiar with Metal through a series of sample code projects.

GPUs

Access GPU device(s) at runtime. GPUs form the basis of Metal development.

Getting the Default GPU

Select the system's default GPU device on which to run your Metal code.

GPU Selection in macOS

Select one or more GPUs on which to run your Metal code by considering GPU capabilities, power, or performance characteristics.

MTLDevice

The Metal interface to a GPU that you use to draw graphics or do parallel computation.

GPU Features

Find feature information for specific GPU families.

Command Setup

Set up infrastructure to execute your custom code on the GPU.

Setting Up a Command Structure

Discover how Metal executes commands on a GPU.

MTLCommandQueue

A queue that organizes command buffers to be executed by a GPU.

MTLCommandBuffer

A container that stores encoded commands for the GPU to execute.

MTLCommandEncoder

An encoder that writes GPU commands into a command buffer.

Counter Sampling

Retrieve information about how the GPU executed your commands.

Rendering

Render graphics by issuing draw calls, and choose a presentation object if you're drawing to the screen.

Creating and Sampling Textures

Load image data into a texture and apply it to a quadrangle.

Render Pipelines

Specify how graphics primitives should be rendered.

Render Passes

Update a set of render targets.

MTLRenderCommandEncoder

The object to use for encoding commands for a render pass.

MTLParallelRenderCommandEncoder

An object that splits up a single render pass so that it can be simultaneously encoded from multiple threads.

Model I/O

Specify precise locations within the textures associated with graphics processing.

Presentation Objects

Use user interface elements to display your Metal content.

Parallel Computation

Process arbitrary calculations in parallel on the GPU.

Hello Compute

Demonstrates how to perform data-parallel computations using the GPU.

Creating Threads and Threadgroups

Learn how Metal organizes compute-processing workloads.

Calculating Threadgroup and Grid Sizes

Calculate the optimum sizes for threadgroups and grids when dispatching compute-processing workloads.

MTLComputePipelineDescriptor

An object used to customize how a new compute pipeline state object is compiled.

MTLComputePipelineState

An object that contains a compiled compute pipeline.

MTLComputeCommandEncoder

An object used to encode commands in a compute pass.

Shaders

Libraries

Organize your shaders into libraries.

Functions

Retrieve information about rendering and compute functions.

Resources

Create objects to hold GPU data.

Setting Resource Storage Modes

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

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.

MTLResource

An allocation of memory that is accessible to a GPU.

MTLBlitCommandEncoder

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

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.

Object Sizing and Positioning

Set the sizes and positions of many Metal objects.

MTLOrigin

The coordinates for the front upper-left corner of a region.

MTLRegion

The bounds for a subset of an object's elements.

MTLSize

The dimensions of an object.

Tools

Developing Metal Apps that Run in Simulator

Prototype and test your Metal apps in Simulator.

Supporting Simulator in a Metal App

Modify Metal Apps to Run in Simulator.

Frame Capture Debugging Tools

Analyze and optimize your app performance at runtime.

Using Metal System Trace in Instruments to Profile Your App

Smooth out your frame rate by checking for issues in your app's CPU and GPU utilization.

GPU Programming Techniques

Learn a variety of strategies for executing code efficiently on GPUs, and experiment with their companion sample code.

Forward Plus Lighting with Tile Shading

Implement a forward plus renderer with Metal’s latest features for Apple GPUs.

Deferred Lighting

Demonstrates how to implement a deferred lighting renderer that takes advantage of unique Metal features.

Reflections with Layer Selection

Demonstrates how to use layer selection to reduce the number of render passes needed for a reflective object.

LOD with Function Specialization

Demonstrates how to use specialized functions to select a level of detail based on dynamic conditions.

Dynamic Terrain with Argument Buffers

Demonstrates how to use argument buffers to render a dynamic terrain in real time with a GPU-driven pipeline.

Mixing Metal and OpenGL Rendering in a View

Draw with Metal and OpenGL in the same view using an interoperable texture.

See Also

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software