Modern rendering with Metal
At a glance
| Item | Summary |
|---|---|
| Purpose | Use advanced Metal features such as indirect command buffers, sparse textures, and variable rate rasterization to implement complex rendering techniques. |
| App architecture | A C/Objective-C header, Metal, Objective-C, Objective-C++ sample with the source-visible chain main → AAPLCameraController → AAPLTextureManager → AAPLMeshRenderer → Metal APIs. |
| Main patterns | View-controller organization, Delegate or data-source callbacks |
| Project style | 66 scanned source file(s) across C/Objective-C header, Metal, Objective-C, Objective-C++, organized around ranked entry, type, and file boundaries. |
| Execution model | No structured execution marker indexed; callback threading requires source review. |
| State/event model | No structured observation or publisher-scheduling marker indexed. |
| Key frameworks/packages | Foundation, simd, Metal, vector, TargetConditionals.h; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── Application/
│ ├── main.m
│ ├── AAPLAppDelegate.h
│ ├── AAPLSettingsTableViewController.mm
│ └── AAPLViewController.h
├── Asset/
│ └── AAPLTextureManager.mm
└── Renderer/
├── Shaders/
│ ├── AAPLMeshRenderer.metal
│ └── AAPLShaderCommon.h
├── AAPLMesh.h
├── AAPLRenderer.h
├── AAPLRenderer.mm
├── AAPLScene.h
└── RenderTech/
└── AAPLMeshRenderer.h
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: C/Objective-C header, Metal, Objective-C, Objective-C++.
- The verified tree contains 7 project/configuration file(s) and 83 source declaration(s).
Overall architecture
flowchart LR
N1["main"]
N2["AAPLCameraController"]
N3["AAPLTextureManager"]
N4["AAPLMeshRenderer"]
N5["Metal APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
Reference code
Application/main.m:9 — architecture anchor
int main(int argc, char * argv[])
{
// ...
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AAPLAppDelegate class]));
// ...
}Interpretation
The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into Metal.
Ownership and state
classDiagram
AAPLMaterial o-- id : vertices
AAPLMaterial o-- id : normals
AAPLMaterial o-- id : tangents
AAPLMaterial o-- id : uvs
Ownership evidence
Renderer/AAPLMesh.h:27 — stored dependency or nearest verified ownership anchor
@interface AAPLMesh : NSObject <NSCopying>
// ...
@property (nonatomic, readonly) id<MTLBuffer> vertices;
// ...
@end| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
AAPLMaterial |
id (vertices) |
stores or receives | The declaring implementation writes; property clients read |
AAPLMaterial |
id (normals) |
stores or receives | The declaring implementation writes; property clients read |
AAPLMaterial |
id (tangents) |
stores or receives | The declaring implementation writes; property clients read |
AAPLMaterial |
id (uvs) |
stores or receives | The declaring implementation writes; property clients read |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
No source-visible execution, scheduling, or synchronization boundary was found in the indexed source.
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
State propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Application/AAPLAppDelegate.h:8 |
| Source import | simd |
The cited file imports this module; runtime use and architectural role are not inferred. | Application/AAPLCommon.h:9 |
| Source import | Metal |
The cited file imports this module; runtime use and architectural role are not inferred. | Application/AAPLCommon.h:8 |
| Source import | vector |
The cited file imports this module; runtime use and architectural role are not inferred. | Application/AAPLSettingsTableViewController.mm:10 |
| Source import | TargetConditionals.h |
The cited file imports this module; runtime use and architectural role are not inferred. | Renderer/AAPLConfig.h:8 |
| Source import | UIKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Application/AAPLAppDelegate.h:13 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
Renderer/AAPLMesh.h:18 — representative type boundary
@protocol MTLTexture;| Type | Responsibility | Depends on or conforms to |
|---|---|---|
AAPLAppDelegate |
Receives callback-driven events | UIResponder, UIApplicationDelegate |
AAPLView |
User-interface presentation and input forwarding | MTKView |
AAPLViewController |
View lifecycle, callbacks, and feature coordination | UIViewController, MTKViewDelegate |
AAPLRenderer |
Owns drawing, GPU, or presentation processing | NSObject |
AAPLScene |
Scene lifecycle or scene-level composition | NSObject |
AAPLMeshRenderer |
Owns drawing, GPU, or presentation processing | NSObject |
AAPLSettingsTableViewController |
View lifecycle, callbacks, and feature coordination | UITableViewController, UITableViewDelegate, UITableViewDataSource |
WindowSceneDelegate |
Receives callback-driven events | UIResponder, UIWindowSceneDelegate |
AAPLTextureManager |
Long-lived feature or framework coordination | NSObject |
AAPLCameraController |
View lifecycle, callbacks, and feature coordination | NSObject |
No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
pos (Application/AAPLInput.h:65) |
header-visible |
The declaration is exposed to translation units that import the header. | Inference: declare a contract needed by other Objective-C/C translation units. |
startPos (Application/AAPLInput.h:66) |
header-visible |
The declaration is exposed to translation units that import the header. | Inference: declare a contract needed by other Objective-C/C translation units. |
delta (Application/AAPLInput.h:67) |
header-visible |
The declaration is exposed to translation units that import the header. | Inference: declare a contract needed by other Objective-C/C translation units. |
window (Application/WindowSceneDelegate.h:18) |
header-visible |
The declaration is exposed to translation units that import the header. | Inference: declare a contract needed by other Objective-C/C translation units. |
Reference code
Application/AAPLInput.h:65 — representative boundary
@interface AAPLTouch : NSObject
// ...
@property simd::float2 pos; // Current position.
// ...
@endSwift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| View lifecycle, callbacks, and feature coordination | AAPLCameraController, AAPLSettingsTableViewController, AAPLViewController |
The source’s Controller suffix makes this role explicit. |
| Receives callback-driven events | AAPLAppDelegate, WindowSceneDelegate |
The source’s Delegate suffix makes this role explicit. |
| Long-lived feature or framework coordination | AAPLTextureManager |
The source’s Manager suffix makes this role explicit. |
| Owns drawing, GPU, or presentation processing | AAPLMeshRenderer, AAPLRenderer |
The source’s Renderer suffix makes this role explicit. |
| Scene lifecycle or scene-level composition | AAPLScene |
The source’s Scene suffix makes this role explicit. |
| User-interface presentation and input forwarding | AAPLView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | Renderer/AAPLCameraController.h:18 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Delegate or data-source callbacks | Application/AAPLAppDelegate.h:15 |
Callback protocols invert event delivery back into the sample’s owner. |
Main application flow
sequenceDiagram
participant AAPLRenderer
participant _commandQueue
participant MTLRenderPassDescriptor
participant AAPLCamera
participant spotCamera
participant commandBuffer
participant NSString
participant _device
AAPLRenderer->>_commandQueue: commandBuffer()
AAPLRenderer->>MTLRenderPassDescriptor: renderPassDescriptor()
AAPLRenderer->>AAPLCamera: initPerspectiveWithPosition()
AAPLRenderer->>spotCamera: updateState()
AAPLRenderer->>commandBuffer: renderCommandEncoderWithDescriptor()
AAPLRenderer->>NSString: stringWithFormat()
AAPLRenderer->>_device: newBufferWithLength()
AAPLRenderer->>commandBuffer: commit()
Reference code
Renderer/AAPLRenderer.mm:1812 — generateSpotShadowMaps()
#if USE_SPOT_LIGHT_SHADOWS
// ...
[commandBuffer commit];
#endif
Naming conventions
- Types: Controller: AAPLCameraController, AAPLSettingsTableViewController, AAPLViewController; Delegate: AAPLAppDelegate, WindowSceneDelegate; Manager: AAPLTextureManager; Renderer: AAPLMeshRenderer, AAPLRenderer; Scene: AAPLScene; View: AAPLView.
- Protocols:
MTLTexture,MTLBuffer,MTLDevice,MTLHeap,MTLDevice,MTLBuffer,MTLDevice,MTLRenderCommandEncoder. - Methods:
initWithDevice,update,getTexture,addTextures,request,setRequiredMip,loadMipsToTexture,addMips. - Files:
Asset/AAPLTextureManager.mm,Application/AAPLAppDelegate.h,Application/AAPLSettingsTableViewController.mm,Application/AAPLViewController.h,Renderer/AAPLMesh.h,Renderer/AAPLRenderer.h.
Architecture takeaways
mainis the main source-visible entry or composition anchor for this sample.- Framework work reaches simd, Metal, vector, UIKit through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
Application/main.m |
Cited implementation, Feature implementation |
Renderer/AAPLMesh.h |
Cited implementation, MTLTexture, AAPLMeshChunk, AAPLSubMesh, AAPLMaterial, MTLBuffer, MTLDevice, MTLHeap, AAPLMesh |
Application/AAPLInput.h |
pos, startPos, delta, AAPLTouch, AAPLVirtualJoystick, AAPLInput |
Application/WindowSceneDelegate.h |
window, WindowSceneDelegate |
Renderer/AAPLCameraController.h |
AAPLCameraController |
Application/AAPLAppDelegate.h |
Cited implementation, Foundation, UIKit, AAPLAppDelegate |
Application/AAPLCommon.h |
simd, Metal, Feature implementation |
Application/AAPLSettingsTableViewController.mm |
vector, AAPLWidget, AAPLSection, AAPLSettingsTableViewController |
Renderer/AAPLConfig.h |
TargetConditionals.h, Feature implementation |
Asset/AAPLTextureManager.mm |
TextureRequest, TextureEntry, TextureUpdate, StatsEntry, PendingBlit, AAPLTextureManager |
Renderer/Shaders/AAPLMeshRenderer.metal |
AAPLVertex, AAPLVertexOutput, AAPLDepthOnlyVertex, AAPLDepthOnlyVertexOutput, AAPLDepthOnlyAlphaMaskVertex, AAPLDepthOnlyAlphaMaskVertexOutput |
Application/AAPLViewController.h |
AAPLView, AAPLViewController |
Renderer/AAPLRenderer.h |
AAPLInput, AAPLFrameViewData, AAPLRenderer |
Renderer/AAPLRenderer.mm |
AAPLConfig, AAPLFrameData, AAPLRenderer |
Renderer/AAPLScene.h |
MTLDevice, MTLBuffer, AAPLScene |
Renderer/RenderTech/AAPLMeshRenderer.h |
AAPLICBData, AAPLCameraParams, AAPLMeshRenderer |