This repo contains code with examples on how to use Apple's Metal GPU APIs. Some sample code has been created entirely by me, while others have been picked from Github. The latter ones are in this repo because they have been heavily modified, not only to support the latest version of Swift and SwiftUI, but also to add support to macOS, iOS, and tvOS. Links to the source Github repos or websites are provided.
-
Playgrounds.
- Memory size, stride, and alignment.
MTKView
as a Playground live view.
-
Command-Line apps.
GPU Inspector.
Prints on
stdout
a brief description of all your available GPUs. For example:Apple M1 Max Built-in GPU Unified memory (shared with CPU) max recommended working set: 48 GB Feature set support family: apple 1, apple 2, apple 3, apple 4, apple 5, apple 6, apple 7, metal 3 General Purpose Computing max threadgroup memory: 32 kB max threads per threadgroup: [1024, 1024, 1024] threads execution width: 32 Intel(R) HD Graphics 530 Built-in GPU (low power) Unified memory (shared with CPU) max recommended working set: 1.61 GB Feature set support family: common 1, common 2, common 3, apple 1, apple 2, apple 3, apple 4 General Purpose Computing max threadgroup memory: 64 KB max threads per threadgroup: [1024, 1024, 1024] threads execution width: 32 AMD Radeon Pro 460 Built-in GPU Discrete memory max recommended working set: 4.29 GB Feature set support family: common 1, common 2, common 3, apple 1, apple 2, apple 3, apple 4 General Purpose Computing max threadgroup memory: 64 KB max threads per threadgroup: [1024, 1024, 1024] threads execution width: 64
Gray converter.
Transform an image into its gray-scale version.
This command-line utitily expects a single argument indicating the location of the image to transform and outputs the gray scale version to the
~/Desktop
with nameout.png
. You can run it from Xcode by pressing ⌘ + R. This project scheme has an argument where you can specify a different image from the default image.This utility is based on Safx' gray converter compute sample.
-
Basic Tessellation.
This sample shows how to tessellate a triangle or quad patch with adjustable per-patch tessellation factors.
-
Metal by Example.
Warren Moore wrote this book in 2015. It was and it remains one of the best introductions to Metal I have found. I've migrated most chapters to the latest Swift and I've added support to macOS.- Clear Screen.
- Draw 2D triangle.
- Draw spinning 3D cube.
- Draw spinning teapot with manual lighting.
- Draw spinning cow with manual lighting and custom texture.
-
Shader Exam.
Leon Denise wrote a tweet with a typical shader exam he gives to his students on SupInfo.com.- Page 1: pass, mirror, symmetry, rotation, zoom, zoomDistortion, repetition, spiral, thunder.
- Page 2: clamp, pli, color direction, pixelated, vague, column, crash, scanline, double frequency.
- Page 3: black & white, threshold, thresholds, sonar, grill, stamp, local negative, chromatic aberration, chroma key
- Page 4: vague, chubby, skinny, twist, glitch, voxel glitch, basic shading, toon shading, cell shading
- Page 5: sphere, sphere repeat, pyramid, polar module, tube twist, tube web, volumetric cloud, strangler fig, kirby