Sample CodeiOS, iPadOS, Mac Catalyst, macOS, tvOSReviewed 2026-07-21View on Apple Developer

Rendering a scene with deferred lighting in Swift

At a glance

Item Summary
Purpose Avoid expensive lighting calculations by implementing a deferred lighting renderer optimized for immediate mode and tile-based deferred renderer GPUs.
App architecture Swift host code with Metal shaders; ViewController hands work to Renderer, which owns base renderer with two concrete strategies before single-pass or traditional deferred shaders.
Main patterns Strategy, Renderer inheritance, Local protocol abstraction
Scope High-level review of 31 scanned source files and 28 detected declarations; build assets are omitted.

Project structure

Source bundle/
├── Application/
│   ├── ViewController.swift
│   ├── AppDelegate.swift
│   └── WindowSceneDelegate.swift
└── Renderer/
    ├── Shaders/
    │   ├── AAPLPointLights.metal
    │   └── AAPLShaderCommon.h
    ├── Renderer.swift
    ├── Scene.swift
    ├── SinglePassDeferredRenderer.swift
    └── TraditionalDeferredRenderer.swift

Structure observations

  • The entry/composition boundary and renderer or operation boundary are separate in the source; Renderer is the principal feature coordinator.
  • GPU-specific logic stays in Metal shader files; shared headers bridge host/shader layouts where present.
  • The tree above is intentionally pruned to composition, resource, and shader files.

Overall architecture

Reference code

Application/ViewController.swift:48 — feature handoff or setup anchor

class ViewController: PlatformViewController {
    // ...
}

Interpretation

This is the dominant control/data path: platform code composes Renderer; that object controls base renderer with two concrete strategies; GPU-visible work ends in single-pass or traditional deferred shaders. The arrows summarize responsibility transfer, not a claim that every node directly calls the next.

Ownership and state

Ownership evidence

Application/ViewController.swift:48ViewController creates and stores renderer

class ViewController: PlatformViewController {
    // ...
}
Owner Object or state Relationship Mutation authority
ViewController renderer / SinglePassDeferredRenderer Creates and stores; the diagram uses composition because construction is source-visible. The declaring scope performs setup and replacement.
Renderer Feature-specific framework and Metal resources Operation-local calls or stored state; exclusive lifetime is not assumed beyond cited evidence. Feature setup/encoding code controls mutation and command submission.

Class and protocol design

Type Responsibility Depends on or conforms to Source
ViewController selects/configures the view and composes the feature objects. PlatformViewController Application/ViewController.swift:18
Renderer owns pipeline/resource setup and per-frame command encoding. NSObject, MTKViewDelegate Renderer/Renderer.swift:13
SinglePassDeferredRenderer owns pipeline/resource setup and per-frame command encoding. Renderer Renderer/SinglePassDeferredRenderer.swift:11
TraditionalDeferredRenderer owns pipeline/resource setup and per-frame command encoding. Renderer Renderer/TraditionalDeferredRenderer.swift:12
Scene holds scene geometry, instances, camera, or lighting data. concrete Metal/framework collaborators Renderer/Scene.swift:14
BufferView owns presentation timing/drawable behavior and forwards callbacks. concrete Metal/framework collaborators Renderer/Utilities/BufferView.swift:11
AppDelegate handles application/window lifecycle callbacks. UIResponder, UIApplicationDelegate, NSObject Application/AppDelegate.swift:17

A local protocol/conformance boundary is present, so protocol-oriented collaboration is claimed only for that seam.

Access control

Symbol Access Verified effect Design reason
private let inFlightSemaphore: DispatchSemaphore private the enclosing declaration and Swift-permitted same-file extensions can use it. Keep mutable GPU/resource state under one lexical owner. (Renderer/Renderer.swift:23)
fileprivate let buffer: MTLBuffer fileprivate all declarations in that Swift file may use it. Allow same-file helpers without exposing the symbol target-wide. (Renderer/Utilities/BufferView.swift:14)
AppDelegate implicit internal visible inside the sample target, not exported as public API. Keep the usable surface no wider than the collaboration requires. (Application/AppDelegate.swift:17)
GBufferData header-visible available to translation units that import the header; this is not Swift public. Keep the usable surface no wider than the collaboration requires. (Renderer/Shaders/AAPLShaderCommon.h:15)
AAPLPointLights entry points Metal library boundary host code resolves named shader entry points; Swift access modifiers do not apply. Expose only named shader entry points needed by pipeline creation. (Renderer/Shaders/AAPLPointLights.metal:25)

Swift’s default is internal; private and fileprivate above use Swift semantics. No public or open app API is inferred when the sample only needs one target. Objective-C/Objective-C++ instead use header versus implementation placement, C++ uses access specifiers/linkage, Python uses module conventions, and Metal entry points cross a compiled-library boundary—none is interchangeable with Swift public.

Logic ownership and placement

Logic Owning type or file Why it lives there
Selects/configures the view and composes the feature objects ViewControllerApplication/ViewController.swift:18 The type’s callbacks and stored state align with this responsibility.
Owns pipeline/resource setup and per-frame command encoding RendererRenderer/Renderer.swift:13 The type’s callbacks and stored state align with this responsibility.
Owns pipeline/resource setup and per-frame command encoding SinglePassDeferredRendererRenderer/SinglePassDeferredRenderer.swift:11 The type’s callbacks and stored state align with this responsibility.
Owns pipeline/resource setup and per-frame command encoding TraditionalDeferredRendererRenderer/TraditionalDeferredRenderer.swift:12 The type’s callbacks and stored state align with this responsibility.
Base renderer with two concrete strategies Renderer / Application/ViewController.swift:48 Keeps Metal/framework setup and encoding out of entry or lifecycle code.
Single-pass or traditional deferred shaders Renderer/Shaders/AAPLPointLights.metal:25 GPU-parallel code remains in the Metal compilation boundary.

Shader boundary reference

Renderer/Shaders/AAPLPointLights.metal:25 — representative GPU entry/helper

vertex LightMaskOut

Design patterns

Pattern Source evidence Purpose or tradeoff
Strategy Renderer/Renderer.swift:5 A common caller selects between concrete rendering implementations.
Renderer inheritance Application/ViewController.swift:48 Makes the sample’s base renderer with two concrete strategies an explicit, reviewable boundary.
Local protocol abstraction Renderer/Shaders/AAPLPointLights.metal:25 Makes the sample’s base renderer with two concrete strategies an explicit, reviewable boundary.

Naming conventions

  • Role suffixes make ownership visible: ViewController, Renderer, SinglePassDeferredRenderer, TraditionalDeferredRenderer, Scene, BufferView, AppDelegate.
  • Method names describe setup or encoding actions: application, applicationDidFinishLaunching, applicationWillTerminate, applicationShouldTerminateAfterLastWindowClosed, beginFrame, beginDrawableCommands, endFrame.
  • Shader entry points use stage/operation names: skybox_vertex, skybox_fragment, fairy_vertex, fairy_fragment, shadow_vertex, gbuffer_vertex.
  • Names favor concrete domain roles and target-local types; no broad public library namespace is introduced.

Architecture takeaways

  • Keep ViewController focused on composition; Renderer is the owner of base renderer with two concrete strategies.
  • Treat single-pass or traditional deferred shaders as a separate execution/compilation boundary with explicit resource and data-layout contracts.
  • The verified ownership edge is ViewControllerrenderer; broader exclusive ownership is not inferred.
  • Access-control rationale follows concrete language boundaries rather than translating every header or shader symbol into Swift terms.

Source map

Source file Architectural role
Application/ViewController.swift ViewController
Renderer/Shaders/AAPLPointLights.metal LightMaskOut, LightInOut
Application/AppDelegate.swift AppDelegate, AppDelegate
Renderer/Renderer.swift Renderer
Application/WindowSceneDelegate.swift WindowSceneDelegate
Renderer/Shaders/AAPLShaderCommon.h GBufferData, AccumLightBuffer
Renderer/Scene.swift Scene
Renderer/SinglePassDeferredRenderer.swift SinglePassDeferredRenderer
Renderer/TraditionalDeferredRenderer.swift TraditionalDeferredRenderer