At a glance
| Item |
Summary |
| Purpose |
Implement ray-traced rendering using GPU-based parallel processing. |
| App architecture |
Objective-C, Objective-C++ host code with Metal shaders; ViewController hands work to Renderer, which owns acceleration-structure construction before ray-tracing shader work. |
| Main patterns |
Scene/renderer separation, Bounded frames in flight, Host-shader data contract |
| Scope |
High-level review of 15 scanned source files and 34 detected declarations; build assets are omitted. |
Project structure
Source bundle/
├── Renderer/
│ ├── Renderer.mm
│ ├── Shaders.metal
│ ├── Scene.mm
│ ├── ShaderTypes.h
│ └── Scene.h
└── Application/
├── ViewController.mm
├── main.m
├── AppDelegate.m
└── AppDelegate.h
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
flowchart LR
N1["ViewController"]
N2["Renderer"]
N3["acceleration-structure construction"]
N4["ray-tracing shader work"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
Renderer/Renderer.mm:347 — feature handoff or setup anchor
@implementation Renderer
// ...
- (id <MTLAccelerationStructure>)newAccelerationStructureWithDescriptor:(MTLAccelerationStructureDescriptor *)descriptor
// ...
@end
Interpretation
This is the dominant control/data path: platform code composes Renderer; that object controls acceleration-structure construction; GPU-visible work ends in ray-tracing shader work. The arrows summarize responsibility transfer, not a claim that every node directly calls the next.
Ownership and state
classDiagram
ViewController *-- Renderer : _renderer
Ownership evidence
Application/ViewController.mm:57 — ViewController creates and stores _renderer
@implementation ViewController
// ...
_renderer = [[Renderer alloc] initWithDevice:_view.device
scene:scene];
// ...
@end
| Owner |
Object or state |
Relationship |
Mutation authority |
ViewController |
_renderer / Renderer |
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. |
NSViewController, UIViewController |
Application/ViewController.h:18 |
Renderer |
owns pipeline/resource setup and per-frame command encoding. |
NSObject, MTKViewDelegate |
Renderer/Renderer.h:13 |
Scene |
holds scene geometry, instances, camera, or lighting data. |
NSObject |
Renderer/Scene.h:110 |
AAPLAppDelegate |
handles application/window lifecycle callbacks. |
PlatformAppDelegate |
Application/AppDelegate.h:18 |
AAPLWindowSceneDelegate |
handles application/window lifecycle callbacks. |
UIResponder, UIWindowSceneDelegate |
Application/AppDelegate.m:11 |
WindowSceneDelegate |
handles application/window lifecycle callbacks. |
UIResponder, UIWindowSceneDelegate |
Application/WindowSceneDelegate.h:11 |
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/AppDelegate.h:18) |
Renderer 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/Renderer.mm:1) |
Shaders 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.metal:264) |
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 |
ViewController — Application/ViewController.h:18 |
The type’s callbacks and stored state align with this responsibility. |
| Owns pipeline/resource setup and per-frame command encoding |
Renderer — Renderer/Renderer.h:13 |
The type’s callbacks and stored state align with this responsibility. |
| Holds scene geometry, instances, camera, or lighting data |
Scene — Renderer/Scene.h:110 |
The type’s callbacks and stored state align with this responsibility. |
| Handles application/window lifecycle callbacks |
AAPLAppDelegate — Application/AppDelegate.h:18 |
The type’s callbacks and stored state align with this responsibility. |
| Acceleration-structure construction |
Renderer / Renderer/Renderer.mm:347 |
Keeps Metal/framework setup and encoding out of entry or lifecycle code. |
| Ray-tracing shader work |
Renderer/Shaders.metal:264 |
GPU-parallel code remains in the Metal compilation boundary. |
Shader boundary reference
Renderer/Shaders.metal:264 — ray-tracing compute entry point and resource contract
kernel void raytracingKernel(
uint2 tid [[thread_position_in_grid]],
constant Uniforms & uniforms [[buffer(0)]],
texture2d<unsigned int> randomTex [[texture(0)]],
texture2d<float> prevTex [[texture(1)]],
texture2d<float, access::write> dstTex [[texture(2)]],
device void *resources [[buffer(1), function_constant(useResourcesBuffer)]],
constant MTLAccelerationStructureInstanceDescriptor *instances [[buffer(2)]],
constant AreaLight *areaLights [[buffer(3)]],
instance_acceleration_structure accelerationStructure [[buffer(4)]],
intersection_function_table<triangle_data, instancing> intersectionFunctionTable [[buffer(5)]]
)
{
// ...
}
Design patterns
| Pattern |
Source evidence |
Purpose or tradeoff |
| Scene/renderer separation |
Renderer/Renderer.mm:49 |
Makes the sample’s acceleration-structure construction an explicit, reviewable boundary. |
| Bounded frames in flight |
Renderer/Renderer.mm:42 |
A semaphore/ring limits CPU writes from overtaking GPU reads. |
| Host-shader data contract |
Renderer/Scene.h:14 |
Shared indices/structs and matching bindings couple host encoding to shader signatures deliberately. |
Naming conventions
- Role suffixes make ownership visible:
ViewController, Renderer, Scene, AAPLAppDelegate, AAPLWindowSceneDelegate, WindowSceneDelegate.
- Method names describe setup or encoding actions:
initWithDevice, clear, uploadToBuffers, geometryDescriptor, resources, addCubeWithFaces, addSphereWithOrigin.
- Shader entry points use stage/operation names:
raytracingKernel, copyVertex, copyFragment.
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
ViewController focused on composition; Renderer is the owner of acceleration-structure construction.
- Treat ray-tracing shader work as a separate execution/compilation boundary with explicit resource and data-layout contracts.
- The verified ownership edge is
ViewController → _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 |
Renderer/Renderer.mm |
Renderer |
Application/ViewController.mm |
ViewController |
Renderer/Shaders.metal |
BoundingBoxIntersection, TriangleResources, SphereResources, ray, CopyVertexOut |
Application/main.m |
entry point or feature implementation |
Renderer/Scene.mm |
Geometry, TriangleGeometry, SphereGeometry, GeometryInstance, Scene |
Renderer/ShaderTypes.h |
packed_float3, Camera, AreaLight, Uniforms, Sphere |
Renderer/Scene.h |
BoundingBox, Geometry, TriangleGeometry, SphereGeometry, GeometryInstance |
Application/AppDelegate.m |
AAPLWindowSceneDelegate, AAPLWindowSceneDelegate, AAPLAppDelegate, AAPLAppDelegate |
Application/AppDelegate.h |
AAPLAppDelegate |