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.
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 creating a game scene. 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.
In this chapter, you'll learn how to add professional lighting to your scenes.
Section II: Intermediate Metal
With the basics under your belt, you can move on to multi-pass rendering. You’ll add shadows and learn several new rendering techniques. Programming the GPU using compute shaders can be intimidating, so you’ll create particle systems to learn how fast multi-threaded solutions can be.
In this chapter, you’ll learn how to move the bulk of the rendering work to the GPU.
Section IV: Ray Tracing
In this section, you’ll trace rays to render objects with more realism than the rasterization techniques you’ve used up to now. As a bonus, you’ll also do some post-processing with Metal Performance Shaders. To wrap up, you’ll consider how best to profile and optimize your game.
In this chapter, you’ll dive a bit deeper into the world of the Metal Performance Shaders (MPS) framework. 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 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.
In this chapter, you’ll review the best practices to follow when creating your Metal apps — from choosing the right resolution to compressing textures, no best practice stone is unturned in this chapter.