Thirty Days of Metal—Day 1: Devices

Warren Moore
3 min readApr 1, 2022

--

This series of posts is my attempt to present the Metal graphics programming framework in small, bite-sized chunks for Swift app developers who haven’t done GPU programming before.

To run the sample code that accompanies these posts, you will need a fairly recent Mac with Xcode 13 installed. Most of the code can run on an iPhone with relatively little modification, but an iPhone is not required.

Welcome to Metal!

What exactly is Metal, and why should you care?

Metal is a framework—a collection of application programming interfaces (APIs) — that enables you to program the graphics processing unit (GPU) in your phone or computer.

One evocative use of GPU programming is to render 3D graphics for video games, so this series on Metal will cover topics in 3D graphics programming that are essential for real-time interactive apps like games. You should be aware, though, that GPUs are used for other things besides games, like machine learning, data visualization, and physical simulations.

Unlike other graphics frameworks (Core Graphics, SceneKit, SwiftUI Canvas, etc.), Metal requires you to have more low-level knowledge. This entails understanding how to write code that runs on the GPU and how to explicitly describe the work that goes into drawing your pictures. My task with this series is to make that job approachable and at least a little bit fun.

Over the next 30 articles, we will look at many aspects of graphics programming, culminating in sample code that renders an animated, interactive, three-dimensional scene. With the knowledge you gain, you will be able to add 3D graphics to your own apps and games and be well on your way to GPU mastery.

Devices

To get started, we will learn about devices.

A device is an abstraction of the graphics processing unit, or GPU, inside your iPhone or Mac. The GPU is a separate processor from the CPU and is specialized for different kinds of work. A big part of our job here is learning what GPUs are good at and how to program them.

In code, a device is an object that conforms to the MTLDevice protocol. This protocol includes methods for allocating GPU resources, as well as many other kinds of objects. We will see many of these as we progress through our exploration of Metal.

Getting a Device

I recommend creating a Playground in Xcode and running the code in this article as we go along.

The first thing you should do is import the Metal framework so the compiler can find Metal’s types and functions:

import Metal

The simplest way to get a Metal device is to call the MTLCreateSystemDefaultDevice() function. As its name suggests, this function returns the default device. Its return type is MTLDevice?, since it can return nil on systems that do not support Metal.

let device = MTLCreateSystemDefaultDevice()!

Once we have a device, we can print out its name:

print("Device name: \(device.name)")

On iOS devices, there is only one Metal device, while some Macs have several. Here are some possible device names you might see if you run this code on a Mac or iPhone:

AMD Radeon Pro 5500M
Intel(R) UHD Graphics 630
Apple A10 GPU
Apple A14 GPU

Asking for the name of a device may sometimes be useful, but that’s not the most interesting thing we can do by far. In the next article, we will start talking about resources, objects that hold the data that the GPU operates on.

--

--

Warren Moore

Real-time graphics engineer based in San Francisco, CA.