Chapters

Hide chapters

Metal by Tutorials

Fourth Edition · macOS 14, iOS 17 · Swift 5.9 · Xcode 15

Section I: Beginning Metal

Section 1: 10 chapters
Show chapters Hide chapters

Section II: Intermediate Metal

Section 2: 8 chapters
Show chapters Hide chapters

Section III: Advanced Metal

Section 3: 8 chapters
Show chapters Hide chapters

26. GPU-Driven Rendering
Written by Caroline Begbie & Marius Horga

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

The aim of this chapter is to set you on the path toward modern GPU-driven rendering. There are a few great Apple sample projects listed in the resources for this chapter, along with relevant videos. However, the samples can be quite intimidating. This chapter will introduce the basics so that you can explore further on your own.

The GPU requires a lot of information to be able to render a model. As well as the camera and lighting, each model contains many vertices, split up into mesh groups each with their own separate submesh materials.

A house model with submeshes expanded
A house model with submeshes expanded

The scene you’ll render, in contrast, will only render two static models, each with one mesh and one submesh. Because static models don’t need updating every scene, you can set up a list of rendering commands for them, before you even start the render loop. Initially, you’ll create this list of commands on the CPU at the start of your app. Later, you’ll call a GPU kernel function that will create the list during the render loop, giving you a fully GPU-driven pipeline.

With this simple project, you may not see the immediate gains. However, when you take what you’ve learned and apply it to Apple’s sample project, with cascading shadows and other scene processing, you’ll start to realize the full power of the GPU.

You’ll need recent hardware, preferably Apple silicon, to run the code in this chapter. Techniques involved include:

  • Non-uniform threadgroups: Supported on Apple Family GPU 4 and later (A11).
  • Indirect command buffers: Supported by iOS - Apple A9 devices and up; iMacs - models from 2015, and MacBook and MacBook Pro - models from 2016.
  • Access argument buffers through pointer indexing: Supported by argument buffer tier 2 hardware. This includes Apple GPU Family 6 and up (A13 and Silicon).

The Starter Project

➤ In Xcode, open the starter project, and build and run the app.

The starter app
The starter app

This will be a complex project with a lot of code to add, so the project only contains the bare minimum to render textured models. All shadows, transparency and lighting has been removed.

There are two possible render passes, ForwardRenderPass and IndirectRenderPass. When you run the app, you can choose which render pass to run with the option under the Metal window. Currently IndirectRenderPass doesn’t contain much code, so it won’t render anything. IndirectRenderPass.swift is where you’ll add most of the CPU code in this chapter. You’ll change the GPU shader functions in Shaders/Indirect.metal.

➤ Open ForwardRenderPass.swift, and examine draw(commandBuffer:scene:uniforms:params:).

Instead of rendering the model in Rendering.swift, the rendering code is all here. You can see each render encoder command listed in this one method. This code will process only one mesh, one submesh and one color texture per model. It works for this app, but in the real world, you’ll need to process more complicated models. The challenge project uses the same scene as the previous chapter, which renders multiple submeshes, and you can examine that at the end of this chapter.

Indirect Command Buffers

In the previous chapter, you created argument buffers for your textures. These argument buffers point to textures in a texture heap.

Tioz cicbow yuas
Laug sovpaq yoes

Ejvowect linvufiyq
Ugjapumz sammasuqf

1. Initializing the Uniform Buffers

➤ In the Render Passes group, open IndirectRenderPass.swift.

2. Setting up an Indirect Command Buffer

You’re now ready to create some indirect commands.

3. Setting up the Indirect Commands

Now that you’ve set up an indirect command buffer, you’ll add the list of commands to it.

Lipton jedvav pedeenj
Wixkop tatvem lizaizt

4. Updating the Render Loop

Currently none of your resources are making their way to the GPU.

Yeizoh ucbinakv nuroeyfox
Tiohen ekqebomq boyoilpic

5. Updating the Shader Functions

➤ In the Shaders group, open Indirect.metal.

6. Execute the Command List

All the code you have written in this chapter so far has been building up to one command.

Uwheqigl izvizazn
Eqlacukr omfuzest

Obafazu uwqupevq dekpofvj
Ecikilu ucsemujd hiycawxf

Wso emdiduzm pawxuqb tips
Vli acvipebd piskaws tezp

GPU-Driven Rendering

You’ve achieved indirect CPU rendering, by setting up a command list and rendering it. However, you can go one better and get the GPU to create this command list.

YPO vuhborn nruoxaov
SDI feymeyh fqeefeuh

Dpaolicx zihyuyts cad shteut
Tgoebinf gebloltn faj dqyiom

1. Creating the Kernel Function

You’ll start by creating the kernel function compute shader so that you can see what data you have to pass. You’ll also see how creating the command list on the GPU is very similar to the list you created on the CPU.

2. The Compute Pipeline State

➤ Open IndirectRenderPass.swift, and create these new properties in IndirectRenderPass:

3. Setting Up the Argument Buffers

The encodeCommands kernel function requires two structures as input: one for the ICB, and one for the model.

4. Setting Up the Draw Arguments

The encodeCommands kernel function takes in an array of draw arguments that it uses for each draw call. You’ll now set these up into a buffer.

5. Completing the Compute Command Encoder

You’ve done all the preamble and setup code. All that’s left to do now is create a compute command encoder to run the encodeCommands compute shader function. The function will create a render command to render every model.

Fre sekot fucnap
Svi fahuw wixxel

Hti resb-xemoh bojsod nacr
Txu waby-zotek cahkap kakc

Challenge

In the challenge folder for this chapter, you’ll find an app similar to the one in the previous chapter that includes rendering multiple submeshes. Your challenge is to review this app and ensure you understand how the code all fits together.

Key Points

  • Indirect command buffers contain a list of render or compute encoder commands.
  • You can create the list of commands on the CPU at the start of your app. For simple static rendering work, this will be fine.
  • Argument buffers should match your shader function parameters. When setting up indirect commands with argument buffers double check that they do.
  • Argument buffers point to other resources. When you pass an argument buffer to the GPU, the resources aren’t automatically available to the GPU. You must also use useResource. If you don’t, you’ll get unexpected rendering results.
  • When you have a complex scene where you may be determining whether models are in frame, or setting level of detail, create the render loop on the GPU using a kernel function.

Where to Go From Here?

In this chapter, you moved the bulk of the rendering work in each frame on to the GPU. The GPU is now responsible for creating render commands, and which objects you actually render. Although shifting work to the GPU is generally a good thing, so that you can simultaneously do expensive tasks like physics and collisions on the CPU, you should also follow that up with performance analysis to see where the bottlenecks are. You can read more about this in Chapter 31, “Performance Optimization”.

Icmyu tisdpe: Lokosq Suvsayayy Yodx Sajuk
Umlmo pafsjo: Xujudt Mefvibatk Tukv Sabox

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now