Rendering a scene with deferred lighting in Objective-C
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 |
Objective-C host code with Metal shaders; AAPLViewController hands work to AAPLRenderer, which owns Objective-C deferred renderer before G-buffer and lighting passes. |
| Main patterns |
Renderer boundary, Diagnostic manager, Host-shader data contract |
| Scope |
High-level review of 29 scanned source files and 33 detected declarations; build assets are omitted. |
Project structure
Source bundle/
├── Application/
│ ├── AAPLViewController.m
│ ├── main.m
│ ├── WindowSceneDelegate.m
│ ├── AAPLAppDelegate.h
│ └── AAPLAppDelegate.m
└── Renderer/
├── Shaders/
│ ├── AAPLBufferExamination.metal
│ └── AAPLShaderCommon.h
├── AAPLRenderer.m
└── AAPLMesh.h
Structure observations
- The entry/composition boundary and renderer or operation boundary are separate in the source;
AAPLRenderer 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
flowchart LR
N1["AAPLViewController"]
N2["AAPLRenderer"]
N3["Objective-C deferred renderer"]
N4["G-buffer and lighting passes"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
Application/AAPLViewController.m:113 — feature handoff or setup anchor
@implementation AAPLViewController
// ...
- (void)drawInMTKView:(MTKView *)view
// ...
@end
Interpretation
This is the dominant control/data path: platform code composes AAPLRenderer; that object controls Objective-C deferred renderer; GPU-visible work ends in G-buffer and lighting passes. The arrows summarize responsibility transfer, not a claim that every node directly calls the next.
Ownership and state
classDiagram
AAPLViewController *-- AAPLRenderer_SinglePassDeferred : _renderer
Ownership evidence
Application/AAPLViewController.m:74 — AAPLViewController creates and stores _renderer
@implementation AAPLViewController
// ...
_renderer = [[AAPLRenderer_SinglePassDeferred alloc] initWithMetalKitView:_view];
// ...
@end
| Owner |
Object or state |
Relationship |
Mutation authority |
AAPLViewController |
_renderer / AAPLRenderer_SinglePassDeferred |
Creates and stores; the diagram uses composition because construction is source-visible. |
The declaring scope performs setup and replacement. |
AAPLRenderer |
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 |
AAPLViewController |
selects/configures the view and composes the feature objects. |
PlatformViewController, MTKViewDelegate |
Application/AAPLViewController.h:21 |
AAPLRenderer |
owns pipeline/resource setup and per-frame command encoding. |
NSObject |
Renderer/AAPLRenderer.h:18 |
AAPLBufferExaminationManager |
centralizes a long-lived resource or instrumentation concern. |
NSObject |
Renderer/AAPLBufferExaminationManager.h:29 |
AAPLAppDelegate |
handles application/window lifecycle callbacks. |
UIResponder, UIApplicationDelegate, NSObject |
Application/AAPLAppDelegate.h:11 |
WindowSceneDelegate |
handles application/window lifecycle callbacks. |
UIResponder, UIWindowSceneDelegate |
Application/WindowSceneDelegate.h:15 |
AAPLSubmesh |
loads and retains the material textures used to draw one Model I/O submesh in the deferred passes. |
NSObject, Model I/O material, Metal textures |
Renderer/AAPLMesh.m:15 |
Framework delegate conformance is a callback seam; the source does not justify calling the whole app protocol-oriented.
Access control
| Symbol |
Access |
Verified effect |
Design reason |
AAPLAppDelegate |
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. (Application/AAPLAppDelegate.h:11) |
AAPLRenderer implementation details |
implementation-only |
native helpers, stored state, or registration code stays out of the imported header contract. |
Hide native implementation details from importing translation units. (Renderer/AAPLRenderer.m:1) |
AAPLBufferExamination 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/AAPLBufferExamination.metal:24) |
This sample does not use Swift private, fileprivate, or public for its native boundary. Objective-C/Objective-C++ use header versus implementation placement, C++ uses access specifiers/linkage, Python uses module conventions, and Metal entry points cross a compiled-library boundary; these are not Swift access levels.
Logic ownership and placement
| Logic |
Owning type or file |
Why it lives there |
| Selects/configures the view and composes the feature objects |
AAPLViewController — Application/AAPLViewController.h:21 |
The type’s callbacks and stored state align with this responsibility. |
| Owns pipeline/resource setup and per-frame command encoding |
AAPLRenderer — Renderer/AAPLRenderer.h:18 |
The type’s callbacks and stored state align with this responsibility. |
| Centralizes a long-lived resource or instrumentation concern |
AAPLBufferExaminationManager — Renderer/AAPLBufferExaminationManager.h:29 |
The type’s callbacks and stored state align with this responsibility. |
| Handles application/window lifecycle callbacks |
AAPLAppDelegate — Application/AAPLAppDelegate.h:11 |
The type’s callbacks and stored state align with this responsibility. |
| Objective-C deferred renderer |
AAPLRenderer / Application/AAPLViewController.m:113 |
Keeps Metal/framework setup and encoding out of entry or lifecycle code. |
| G-buffer and lighting passes |
Renderer/Shaders/AAPLBufferExamination.metal:24 |
GPU-parallel code remains in the Metal compilation boundary. |
Shader boundary reference
Renderer/Shaders/AAPLBufferExamination.metal:24 — representative GPU entry/helper
vertex LightInfoData
Design patterns
| Pattern |
Source evidence |
Purpose or tradeoff |
| Renderer boundary |
Application/AAPLViewController.m:113 |
UI/lifecycle code composes a renderer while GPU state and encoding stay together. |
| Diagnostic manager |
Application/AAPLViewController.m:74 |
Makes the sample’s Objective-C deferred renderer an explicit, reviewable boundary. |
| Host-shader data contract |
Renderer/AAPLRenderer.m:18 |
Shared indices/structs and matching bindings couple host encoding to shader signatures deliberately. |
Naming conventions
- Role suffixes make ownership visible:
AAPLViewController, AAPLRenderer, AAPLBufferExaminationManager, AAPLAppDelegate, WindowSceneDelegate.
- Method names describe setup or encoding actions:
initWithMetalKitView, shaderLibrary, loadMetal, loadScene, loadAssets, populateLights, updateLights.
- Shader entry points use stage/operation names:
gbuffer_vertex, gbuffer_fragment, skybox_vertex, skybox_fragment, fairy_vertex, fairy_fragment.
AAPL is a sample namespace prefix, not a recommendation for production module naming; retain semantic suffixes such as Renderer, Manager, Scene, or Adapter.
Architecture takeaways
- Keep
AAPLViewController focused on composition; AAPLRenderer is the owner of Objective-C deferred renderer.
- Treat G-buffer and lighting passes as a separate execution/compilation boundary with explicit resource and data-layout contracts.
- The verified ownership edge is
AAPLViewController → _renderer; 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/AAPLViewController.m |
AAPLViewController |
Renderer/Shaders/AAPLBufferExamination.metal |
LightInfoData, RenderTextureData |
Application/main.m |
entry point or feature implementation |
Renderer/AAPLRenderer.m |
AAPLRenderer |
Application/WindowSceneDelegate.m |
WindowSceneDelegate |
Renderer/Shaders/AAPLShaderCommon.h |
GBufferData, AccumLightBuffer |
Application/AAPLAppDelegate.h |
AAPLAppDelegate, AAPLAppDelegate |
Application/AAPLAppDelegate.m |
AAPLAppDelegate, AAPLAppDelegate |
Renderer/AAPLMesh.h |
AAPLSubmesh, AAPLMesh |