Rendering a scene with deferred lighting in 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 |
C++, Objective-C, Objective-C++ host code with Metal shaders; Python is build/tooling support; AAPLViewController hands work to AAPLViewAdapter, which owns Objective-C++ view adapter and metal-cpp renderer before deferred G-buffer and lighting passes. |
| Main patterns |
Adapter, Renderer hierarchy, Host-shader data contract |
| Scope |
High-level review of 33 scanned source files and 40 detected declarations; build assets are omitted. |
Project structure
Source bundle/
├── Application/
│ ├── AAPLViewController.mm
│ ├── main.m
│ ├── WindowSceneDelegate.m
│ └── AAPLAppDelegate.h
└── Renderer/
├── Shaders/
│ ├── AAPLBufferExamination.metal
│ └── AAPLShaderTypes.h
├── AAPLViewAdapter.mm
├── AAPLRenderer.cpp
└── AAPLViewAdapter.h
Structure observations
- The entry/composition boundary and renderer or operation boundary are separate in the source;
AAPLViewAdapter 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["AAPLViewAdapter"]
N3["Objective-C++ view adapter and metal-cpp renderer"]
N4["deferred G-buffer and lighting passes"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
Application/AAPLViewController.mm:107 — feature handoff or setup anchor
@implementation AAPLViewController
// ...
AAPLViewAdapter rendererView( (__bridge void *)_view );
// ...
@end
Interpretation
This is the dominant control/data path: platform code composes AAPLViewAdapter; that object controls Objective-C++ view adapter and metal-cpp renderer; GPU-visible work ends in deferred 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 *-- Renderer_SinglePassDeferred : _pRenderer
Ownership evidence
Application/AAPLViewController.mm:73 — AAPLViewController creates and stores _pRenderer
@implementation AAPLViewController
// ...
_pRenderer = new Renderer_SinglePassDeferred( _pDevice );
// ...
@end
| Owner |
Object or state |
Relationship |
Mutation authority |
AAPLViewController |
_pRenderer / Renderer_SinglePassDeferred |
Creates and stores; the diagram uses composition because construction is source-visible. |
The declaring scope performs setup and replacement. |
AAPLViewAdapter |
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:19 |
AAPLViewAdapter |
bridges UI/framework callbacks into a differently shaped renderer API. |
concrete Metal/framework collaborators |
Renderer/AAPLViewAdapter.h:25 |
Renderer |
owns pipeline/resource setup and per-frame command encoding. |
concrete Metal/framework collaborators |
Renderer/AAPLBufferExaminationManager.h:18 |
BufferExaminationManager |
centralizes a long-lived resource or instrumentation concern. |
concrete Metal/framework collaborators |
Renderer/AAPLBufferExaminationManager.h:34 |
AAPLAppDelegate |
handles application/window lifecycle callbacks. |
UIResponder, UIApplicationDelegate, NSObject |
Application/AAPLAppDelegate.h:12 |
WindowSceneDelegate |
handles application/window lifecycle callbacks. |
UIResponder, UIWindowSceneDelegate |
Application/WindowSceneDelegate.h: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:12) |
main 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. (Application/main.m:1) |
AAPLViewAdapter C++ members |
C++ access specifier |
public/protected/private defines member reachability. |
Keep the usable surface no wider than the collaboration requires. (Renderer/AAPLViewAdapter.h:27) |
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:19 |
The type’s callbacks and stored state align with this responsibility. |
| Bridges UI/framework callbacks into a differently shaped renderer API |
AAPLViewAdapter — Renderer/AAPLViewAdapter.h:25 |
The type’s callbacks and stored state align with this responsibility. |
| Owns pipeline/resource setup and per-frame command encoding |
Renderer — Renderer/AAPLBufferExaminationManager.h:18 |
The type’s callbacks and stored state align with this responsibility. |
| Centralizes a long-lived resource or instrumentation concern |
BufferExaminationManager — Renderer/AAPLBufferExaminationManager.h:34 |
The type’s callbacks and stored state align with this responsibility. |
| Objective-C++ view adapter and metal-cpp renderer |
AAPLViewAdapter / Application/AAPLViewController.mm:107 |
Keeps Metal/framework setup and encoding out of entry or lifecycle code. |
| Deferred 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 |
| Adapter |
Renderer/AAPLViewAdapter.h:5 |
An adapter translates platform view callbacks or Objective-C objects into C++/metal-cpp calls. |
| Renderer hierarchy |
Application/AAPLViewController.mm:73 |
Makes the sample’s Objective-C++ view adapter and metal-cpp renderer an explicit, reviewable boundary. |
| Host-shader data contract |
Renderer/AAPLRenderer.cpp:35 |
Shared indices/structs and matching bindings couple host encoding to shader signatures deliberately. |
Naming conventions
- Role suffixes make ownership visible:
AAPLViewController, AAPLViewAdapter, Renderer, BufferExaminationManager, AAPLAppDelegate, WindowSceneDelegate.
- Method names describe setup or encoding actions:
loadMetal, loadScene, populateLights, updateLights, updateWorldState, drawableSizeWillChange, drawMeshes.
- 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; AAPLViewAdapter is the owner of Objective-C++ view adapter and metal-cpp renderer.
- Treat deferred G-buffer and lighting passes as a separate execution/compilation boundary with explicit resource and data-layout contracts.
- The verified ownership edge is
AAPLViewController → _pRenderer; 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.mm |
AAPLViewController |
Renderer/Shaders/AAPLBufferExamination.metal |
LightInfoData, RenderTextureData |
Application/main.m |
entry point or feature implementation |
Renderer/AAPLViewAdapter.mm |
entry point or feature implementation |
Renderer/AAPLRenderer.cpp |
entry point or feature implementation |
Application/WindowSceneDelegate.m |
WindowSceneDelegate |
Renderer/Shaders/AAPLShaderTypes.h |
FrameData, PointLight, SimpleVertex, ShadowVertex |
Application/AAPLAppDelegate.h |
AAPLAppDelegate, AAPLAppDelegate |
Renderer/AAPLViewAdapter.h |
MetalDrawable, Texture, RenderPassDescriptor, AAPLViewAdapter |