Accelerating ray tracing using Metal
At a glance
| Item | Summary |
|---|---|
| Purpose | Implement ray-traced rendering using GPU-based parallel processing. |
| App architecture | A C/Objective-C header, Metal, Objective-C, Objective-C++ sample with the source-visible chain main → ViewController → Renderer → Metal APIs. |
| Main patterns | View-controller organization, Delegate or data-source callbacks |
| Project style | 15 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 | simd, Metal, UIKit, Cocoa, MetalKit; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── Application/
│ ├── main.m
│ ├── AppDelegate.m
│ ├── AppDelegate.h
│ ├── ViewController.h
│ ├── ViewController.mm
│ ├── WindowSceneDelegate.h
│ └── WindowSceneDelegate.m
└── Renderer/
├── Scene.h
├── Scene.mm
├── ShaderTypes.h
├── Shaders.metal
└── Renderer.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 22 source declaration(s).
Overall architecture
flowchart LR
N1["main"]
N2["ViewController"]
N3["Renderer"]
N4["Metal APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
Application/main.m:20 — architecture anchor
#if !TARGET_OS_IPHONE
int main(int argc, const char * argv[]) {
return NSApplicationMain(argc, argv);
}
#endifInterpretation
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
BoundingBox o-- id : device
BoundingBox o-- NSString : intersectionFunctionName
BoundingBox o-- Geometry : geometry
BoundingBox o-- matrix_float4x4 : transform
Ownership evidence
Renderer/Scene.h:40 — stored dependency or nearest verified ownership anchor
#ifndef Scene_h
@property (nonatomic, readonly) id <MTLDevice> device;
#endif| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
BoundingBox |
id (device) |
stores or receives | The declaring implementation writes; property clients read |
BoundingBox |
NSString (intersectionFunctionName) |
stores or receives | The declaring implementation writes; property clients read |
BoundingBox |
Geometry (geometry) |
stores or receives | The declaring implementation writes; property clients read |
BoundingBox |
matrix_float4x4 (transform) |
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 | simd |
The cited file imports this module; runtime use and architectural role are not inferred. | Renderer/Renderer.mm:8 |
| Source import | Metal |
The cited file imports this module; runtime use and architectural role are not inferred. | Application/ViewController.h:7 |
| Source import | UIKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Application/ViewController.h:14 |
| Source import | Cocoa |
The cited file imports this module; runtime use and architectural role are not inferred. | Application/ViewController.h:12 |
| Source import | MetalKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Application/ViewController.h:8 |
| Source import | TargetConditionals.h |
The cited file imports this module; runtime use and architectural role are not inferred. | Application/ViewController.h:9 |
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/Scene.h:110 — representative type boundary
#ifndef Scene_h
@interface Scene : NSObject
@end
#endif| Type | Responsibility | Depends on or conforms to |
|---|---|---|
Scene |
Scene lifecycle or scene-level composition | NSObject |
AAPLWindowSceneDelegate |
Receives callback-driven events | UIResponder, UIWindowSceneDelegate |
AAPLAppDelegate |
Receives callback-driven events | PlatformAppDelegate |
ViewController |
View lifecycle, callbacks, and feature coordination | NSViewController |
WindowSceneDelegate |
Receives callback-driven events | UIResponder, UIWindowSceneDelegate |
Renderer |
Owns drawing, GPU, or presentation processing | NSObject, MTKViewDelegate |
BoundingBox |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
Geometry |
Defines a feature-specific type boundary | NSObject |
TriangleGeometry |
Defines a feature-specific type boundary | Geometry |
SphereGeometry |
Defines a feature-specific type boundary | Geometry |
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 |
|---|---|---|---|
window (Application/AppDelegate.h:21) |
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/AppDelegate.m:12) |
implementation |
Visibility follows header/implementation and language linkage rules. | Inference: keep the declaration in the Objective-C implementation boundary. |
window (Application/WindowSceneDelegate.h:13) |
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. |
device (Renderer/Scene.h:40) |
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/AppDelegate.h:21 — representative boundary
#if !TARGET_IOS
@property (strong, nonatomic) PlatformWindow *window;
#endifSwift 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 | ViewController |
The source’s Controller suffix makes this role explicit. |
| Receives callback-driven events | AAPLAppDelegate, AAPLWindowSceneDelegate, WindowSceneDelegate |
The source’s Delegate suffix makes this role explicit. |
| Owns drawing, GPU, or presentation processing | Renderer |
The source’s Renderer suffix makes this role explicit. |
| Scene lifecycle or scene-level composition | Scene |
The source’s Scene suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | Application/ViewController.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/AppDelegate.h:18 |
Callback protocols invert event delivery back into the sample’s owner. |
Main application flow
sequenceDiagram
participant Renderer
participant _device
participant _queue
participant commandBuffer
participant commandEncoder
Renderer->>_device: accelerationStructureSizesWithDescriptor()
Renderer->>_device: newAccelerationStructureWithSize()
Renderer->>_device: newBufferWithLength()
Renderer->>_queue: commandBuffer()
Renderer->>commandBuffer: accelerationStructureCommandEncoder()
Renderer->>commandEncoder: buildAccelerationStructure()
Renderer->>commandBuffer: commit()
Renderer->>commandBuffer: commit()
Reference code
Renderer/Renderer.mm:424 — newAccelerationStructureWithDescriptor()
- (id <MTLAccelerationStructure>)newAccelerationStructureWithDescriptor:(MTLAccelerationStructureDescriptor *)descriptor
{
// ...
MTLAccelerationStructureSizes accelSizes = [_device accelerationStructureSizesWithDescriptor:descriptor];
id <MTLAccelerationStructure> accelerationStructure = [_device newAccelerationStructureWithSize:accelSizes.accelerationStructureSize];
id <MTLBuffer> scratchBuffer = [_device newBufferWithLength:accelSizes.buildScratchBufferSize options:MTLResourceStorageModePrivate];
id <MTLCommandBuffer> commandBuffer = [_queue commandBuffer];
id <MTLAccelerationStructureCommandEncoder> commandEncoder = [commandBuffer accelerationStructureCommandEncoder];
id <MTLBuffer> compactedSizeBuffer = [_device newBufferWithLength:sizeof(uint32_t) options:MTLResourceStorageModeShared];
[commandEncoder buildAccelerationStructure:accelerationStructure
descriptor:descriptor
scratchBuffer:scratchBuffer
scratchBufferOffset:0];
[commandEncoder writeCompactedAccelerationStructureSize:accelerationStructure
toBuffer:compactedSizeBuffer
offset:0];
[commandEncoder endEncoding];
[commandBuffer commit];
[commandBuffer waitUntilCompleted];
uint32_t compactedSize = *(uint32_t *)compactedSizeBuffer.contents;
id <MTLAccelerationStructure> compactedAccelerationStructure = [_device newAccelerationStructureWithSize:compactedSize];
commandBuffer = [_queue commandBuffer];
commandEncoder = [commandBuffer accelerationStructureCommandEncoder];
[commandEncoder copyAndCompactAccelerationStructure:accelerationStructure
toAccelerationStructure:compactedAccelerationStructure];
[commandEncoder endEncoding];
[commandBuffer commit];
return compactedAccelerationStructure;
}
Naming conventions
- Types: Controller: ViewController; Delegate: AAPLAppDelegate, AAPLWindowSceneDelegate, WindowSceneDelegate; Renderer: Renderer; Scene: Scene.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
initWithDevice,clear,uploadToBuffers,geometryDescriptor,resources,addCubeWithFaces,addSphereWithOrigin,initWithGeometry. - Files:
Renderer/Scene.h,Renderer/Scene.mm,Application/ViewController.h,Application/ViewController.mm,Application/WindowSceneDelegate.h,Application/WindowSceneDelegate.m.
Architecture takeaways
mainis the main source-visible entry or composition anchor for this sample.- Framework work reaches simd, Metal, UIKit, Cocoa 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/Scene.h |
Cited implementation, Scene, device, BoundingBox, Geometry, TriangleGeometry, SphereGeometry, GeometryInstance |
Application/AppDelegate.h |
window, Cited implementation, AAPLAppDelegate |
Application/AppDelegate.m |
window, AAPLWindowSceneDelegate, AAPLAppDelegate |
Application/WindowSceneDelegate.h |
window, WindowSceneDelegate |
Application/ViewController.h |
ViewController, Metal, UIKit, Cocoa, MetalKit, TargetConditionals.h |
Renderer/Renderer.mm |
simd, Renderer |
Renderer/Scene.mm |
Geometry, TriangleGeometry, SphereGeometry, GeometryInstance, Scene |
Renderer/ShaderTypes.h |
packed_float3, Camera, AreaLight, Uniforms, Sphere, Triangle |
Renderer/Shaders.metal |
BoundingBoxIntersection, TriangleResources, SphereResources, ray, CopyVertexOut |
Application/ViewController.mm |
ViewController |
Application/WindowSceneDelegate.m |
WindowSceneDelegate |
Renderer/Renderer.h |
Renderer |
Renderer/Transforms.h |
Feature implementation |
Renderer/Transforms.mm |
Feature implementation |