Version Second Edition
Platform iOS 13
Language Swift 5.1
Editor Xcode 11

Build your own low-level game engine in Metal!

Metal is a unified application programming interface (API) for the graphics processing unit, or GPU. It’s unified because it applies to both 3D graphics and data-parallel computation paradigms. Metal is a low-level API because it provides programmers near-direct access to the GPU. Finally, Metal is a low-overhead API because it reduces the central processing unit (CPU) cost by multi-threading and pre-compiling of resources.

But beyond the technical definition, Metal is the most appropriate way to use the GPU’s parallel processing power to visualize data or solve numerical challenges. It’s also tailored to be used for machine learning, image/video processing or, as this book describes, graphics rendering.

This book will introduce you to low-level graphics programming in Metal — Apple’s framework for programming on the graphics processing unit (GPU). As you progress through this book, you’ll learn many of the fundamentals that go into making a game engine and gradually put together your own engine. Once your game engine is complete, you’ll be able to put together 3D scenes and program your own simple 3D games. Because you’ll have built your 3D game engine from scratch, you’ll be able to customize every aspect of what you see on your screen.

This book is for intermediate Swift developers interested in learning 3D graphics or gaining a deeper understanding of how game engines work.

Before You Begin

This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more.

i

iii

Section I: The Player

It takes a wealth of knowledge to render a simple triangle on the screen or animate game characters. This section will guide you through the necessary basics of vertex wrangling, lighting, textures and character animation; and if you’re worried about the math, don’t be! Although computer graphics is highly math-intensive, each chapter explains everything you need, and you’ll get experience creating and rendering models.

Learn about Metal, why you would use it over other graphics frameworks, and get a quick start by rendering your first primitive object!

1

3D Models

Free

Go into depth with 3D models, render them on the screen, and learn how to work with models in Blender.

2

To this point, you’ve been working in playgrounds. You’ll go further with Metal by creating a macOS app from scratch, where you’ll learn how the rendering pipeline works to create the graphics on screen.

3

We'd all love to be math geniuses but some of us lost the opportunity early in life. Fortunately to use math, you don't always have to know exactly what's under the hood. In this chapter you're going to become a matrix master and you will learn what matrices can do for you and how to manipulate them painlessly.

4

Lighting and beyond! In this chapter you'll learn basic lighting; but more importantly, you'll learn how to craft data in shaders, and be on the path to mastering shader artistry. Lighting, shadows, non-photorealistic rendering - these are all techniques that start with the methods that you'll learn in this chapter.

5

Now that you have light in your scene, the next step is to add color to it. In this chapter you'll learn about UV coordinates, texturing a model, samplers, mipmaps, and the asset catalog.

6

This is the final chapter on how to render still models. In the previous chapter, you rendered a simple house with a single color texture imported using Model I/O. In this chapter you'll find out how to use material groups to describe a surface, and how to design textures for micro detail.

7

Rendering still models is a great achievement, but rendering models that move is even more fun. So far your models have been simple inanimate props. You'll now render characters with body movement and give them personality. In this chapter, you'll start off by bouncing a ball, and then move on to rendering a friendly skeleton.

8

Section II: The Scene

With the basics under your belt, you can move on to creating game scenes with scenery, skies and terrains. You’ll be able to add multiple models to a scene, and move around the scene and explore it.

In this chapter you'll take your rendering engine and put together a simple game engine by abstracting the rendering code away from scene management. This takes you to the next level where you can start creating your own games.

9

Before embarking on huge features such as tessellation and instancing, you'll learn some simple techniques to improve your render quality. A series of operations run on the GPU after the fragments have been processed in the pipeline, including alpha testing, depth testing, stencil testing, scissor testing, blending, and anti-aliasing. You’ll touch on most of these operations in this chapter.

10

So far, you’ve used normal map trickery in the fragment function to show the fine details of your low poly models. To achieve a similar level of detail without using normal maps requires a change of model geometry by adding more vertices, which can problematically choke up the pipeline. In this chapter, you’re going to create a detailed terrain using a small number of points. You’ll send a flat ground plane with a grayscale texture describing the height, and the tessellator will create as many vertices as needed.

11

Learn about skyboxes, cube maps, image-based lighting and more as you add some surrounding features to your virtual environment.

12

Many of the best features in games aren’t coded manually; they’re procedurally generated instead. Learn how to generate vertices for grass, how constant and argument buffers work, how to use L-system trees, and then add the grass and random trees to your scene.

13

Up to this point, you have been running projects and playgrounds that only had one pass. In other words, you used just one render command encoder to submit all your draw calls to the GPU. In more complex apps, you would often need the result of one pass used in a following pass before presenting the texture to the screen. Sometimes you need to render content off-screen and use it in subsequent stages of your app. When you finish this chapter, you will be able to render a scene with shadows and multiple lights.

14

To finish off this section, you’ll learn about triple buffering, how to manage resource contention, CPU-GPU synchronization, how to achieve multithreaded encoding, and how to use the GPU profiler.

15

Section III: The Effects

In this section, you’ll use compute shaders to create many different effects. You’ll trace rays to render objects with more realism than the rasterization techniques you’ve used up to now.

Learn about generators, emitters, particle life, the compute kernel, and more.

16

You’ve learned how to create particles, now see how to control their behavior with forces, swarming and flocking behavior, predator-prey behavior, and crowd behavior.

17

This chapter covers all you’ll need to know on how to do detailed ray tracing, ray casting, path tracing and ray marching.

18

Learn about hard and soft shadows, ambient occlusion, screen space rendering and more.

19

Get incredibly detailed with the lighting effects in your scene. Learn about the rendering equation, reflection and refraction, the Fresnel effect, lens flare, subsurface scattering and more lighting details!

20

MPS consists of low-level, fine-tuned, high-performance kernels that run off the shelf with minimal configuration. The MPS kernels make use of data-parallel primitives that are written in such a way that they can take advantage of each GPU family’s characteristics. In this chapter, you’ll dive a bit deeper into the world of MPS.

21

Bring all you’ve learned together and learn how to integrate your game engine with SceneKit and SpriteKit. You’ll learn how to import Metal shaders, perform toon rendering, and more.

22

Whether it’s objects in the scene you seem to have accidentally lost, or maybe more lights and shadows you want to render, you’ll get to use some of Xcode’s excellent tools to find out what’s happening on the GPU, as well as find ways to optimize your game to run at blazing speeds.

23

In this final chapter, you’ll learn more ways to optimize the performance of your game. You’ll understand how to categorize GPUs, how to manage memory, how to synchronize resources between the CPU and GPU, how to move your game on multiple threads and what are the best practices for a smooth rendering.

24

Meet the team

Contributors

Who is this book for

This book is for intermediate Swift developers interested in learning 3D graphics or gaining a deeper understanding of how game engines work.

Concepts covered in this book

  • The Rendering Pipeline
  • 3D Models
  • Coordinate Spaces
  • Lighting
  • Textures & Materials
  • Character Animation
  • Tessellation
  • Environment
  • Instancing & Procedural Generation
  • Multipass & Deferred Rendering
  • Performance Optimization:
  • Particle Systems
  • Raytracing
  • Advanced Lighting & Shadows
  • Integration with SpriteKit & SceneKit

Version history

Second Edition · iOS 13, Swift 5.1, Xcode 11

v2.0 · Jan 16 2020 · Caroline Begbie & M Horga
Pro

Metal by Tutorials

By Caroline Begbie & M Horga

This book will introduce you to graphics programming in Metal — Apple’s framework for programming on the GPU. Build a complete game engine in Metal!

Read for Free with an Ultimate Pro Subscription* * Includes this and all other books in our online library See all benefits
Buy Individually $59.99* *Includes access to all of our online reading features.
See buying options
Hide buying options

All videos. All books.
One low price.

A raywenderlich.com subscription is the best way to learn and master mobile development — plans start at just $19.99/month!

Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.