Rendering a curve primitive in a ray tracing scene
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 curve geometry and acceleration-structure setup before curve intersection/ray-tracing shader. |
| Main patterns |
Geometry hierarchy, Renderer boundary, Host-shader data contract |
| Scope |
High-level review of 15 scanned source files and 20 detected declarations; build assets are omitted. |
Project structure
Source bundle/
├── Renderer/
│ ├── Renderer.mm
│ ├── Shaders.metal
│ └── ShaderTypes.h
└── Application/
├── ViewController.mm
├── main.m
├── WindowSceneDelegate.m
├── AppDelegate.h
├── AppDelegate.m
└── ViewController.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["curve geometry and acceleration-structure setup"]
N4["curve intersection/ray-tracing shader"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
Renderer/Renderer.mm:339 — feature handoff or setup anchor
CurveGeometry *catmullCurveGeometry = [[CurveGeometry alloc] initWithDevice:_device];
MTLPrimitiveAccelerationStructureDescriptor *catmullCurveAccelDesc = [catmullCurveGeometry addCurveWithControlPoints:catmullControlPoints curveIndices:catmullIndices radii:radii];
// Build the acceleration structure.
id <MTLAccelerationStructure> catmullCurveAccelerationStructure = [self newAccelerationStructureWithDescriptor:catmullCurveAccelDesc];
// Add the acceleration structure to the array of primitive acceleration structures.
[_primitiveAccelerationStructures addObject:catmullCurveAccelerationStructure];
_controlPointBuffer = catmullCurveGeometry.controlPointBuffer;
Interpretation
This is the dominant control/data path: platform code composes Renderer; that object controls curve geometry and acceleration-structure setup; GPU-visible work ends in curve intersection/ray-tracing shader. 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:54 — ViewController creates and stores _renderer
@implementation ViewController
// ...
_renderer = [[Renderer alloc] initWithDevice:_view.device];
// ...
@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:19 |
Renderer |
owns pipeline/resource setup and per-frame command encoding. |
NSObject, MTKViewDelegate |
Renderer/Renderer.h:11 |
AppDelegate |
handles application/window lifecycle callbacks. |
PlatformAppDelegate |
Application/AppDelegate.h:20 |
WindowSceneDelegate |
handles application/window lifecycle callbacks. |
UIResponder, UIWindowSceneDelegate |
Application/WindowSceneDelegate.h:15 |
GeometryObject |
provides the Metal device and platform-appropriate resource storage mode shared by geometry builders. |
NSObject, MTLDevice |
Renderer/Geometry.mm:18 |
PlaneGeometry |
builds plane vertex/index buffers and returns the primitive acceleration-structure descriptor for the plane. |
GeometryObject, Metal buffers and acceleration structures |
Renderer/Geometry.mm:39 |
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 |
AppDelegate |
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:20) |
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:188) |
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:19 |
The type’s callbacks and stored state align with this responsibility. |
| Owns pipeline/resource setup and per-frame command encoding |
Renderer — Renderer/Renderer.h:11 |
The type’s callbacks and stored state align with this responsibility. |
| Handles application/window lifecycle callbacks |
AppDelegate — Application/AppDelegate.h:20 |
The type’s callbacks and stored state align with this responsibility. |
| Handles application/window lifecycle callbacks |
WindowSceneDelegate — Application/WindowSceneDelegate.h:15 |
The type’s callbacks and stored state align with this responsibility. |
| Curve geometry and acceleration-structure setup |
Renderer / Renderer/Renderer.mm:339 |
Keeps Metal/framework setup and encoding out of entry or lifecycle code. |
| Curve intersection/ray-tracing shader |
Renderer/Shaders.metal:136 |
GPU-parallel code remains in the Metal compilation boundary. |
Shader boundary reference
Renderer/Shaders.metal:136 — custom Catmull–Rom curve intersection function
[[intersection (curve, triangle_data, curve_data, instancing)]]
bool catmullRomCurveIntersectionFunction(// Ray parameters passed to the ray intersector.
float3 origin [[origin]],
float3 direction [[direction]],
float distance [[distance]],
// Information about the primitive.
uint segmentId [[primitive_id]],
float t [[curve_parameter]],
ray_data float3& normal [[payload]],
// Custom resources bound to the intersection function table.
constant float3 *controlPoints [[buffer(0)]],
constant uint16_t *indices [[buffer(1)]]
)
{
// ...
}
Design patterns
| Pattern |
Source evidence |
Purpose or tradeoff |
| Geometry hierarchy |
Renderer/Renderer.mm:339 |
Makes the sample’s curve geometry and acceleration-structure setup an explicit, reviewable boundary. |
| Renderer boundary |
Application/ViewController.mm:54 |
UI/lifecycle code composes a renderer while GPU state and encoding stay together. |
| Host-shader data contract |
Renderer/Renderer.mm:13 |
Shared indices/structs and matching bindings couple host encoding to shader signatures deliberately. |
Naming conventions
- Role suffixes make ownership visible:
ViewController, Renderer, AppDelegate, WindowSceneDelegate.
- Method names describe setup or encoding actions:
application, applicationShouldTerminateAfterLastWindowClosed, viewDidLoad, scene, sceneDidDisconnect, initWithDevice, loadMetal.
- Shader entry points use stage/operation names:
raytracingKernel, copyVertex, copyFragment.
- Names favor concrete domain roles and target-local types; no broad
public library namespace is introduced.
Architecture takeaways
- Keep
ViewController focused on composition; Renderer is the owner of curve geometry and acceleration-structure setup.
- Treat curve intersection/ray-tracing shader 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 |
CopyVertexOut |
Application/main.m |
entry point or feature implementation |
Application/WindowSceneDelegate.m |
WindowSceneDelegate |
Renderer/ShaderTypes.h |
packed_float3, Camera, Uniforms |
Application/AppDelegate.h |
AppDelegate |
Application/AppDelegate.m |
AppDelegate, AppDelegate |
Application/ViewController.h |
ViewController, ViewController |