Sample CodeiOS, iPadOS, Mac Catalyst, macOSReviewed 2026-07-21View on Apple Developer

Rendering terrain dynamically with argument buffers

At a glance

Item Summary
Purpose Use argument buffers to render terrain in real time with a GPU-driven pipeline.
App architecture A C/Objective-C header, Metal, Objective-C, Objective-C++ sample with the source-visible chain mainAAPLGameViewControllerAAPLDebugRenderermetal_stdlib / Metal APIs.
Main patterns View-controller organization, Delegate or data-source callbacks
Project style 37 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 metal_stdlib, simd, Metal, TargetConditionals.h, Foundation; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── Application/
│   ├── main.m
│   ├── AAPLAppDelegate.h
│   ├── AAPLGameViewController.h
│   ├── AAPLAppDelegate.mm
│   └── AAPLGameViewController.mm
└── Renderer/
    ├── AAPLParticleRenderer.metal
    ├── AAPLDebugRenderer.h
    ├── AAPLDebugRenderer.mm
    ├── AAPLMainRenderer.metal
    ├── AAPLObjLoader.h
    ├── AAPLObjLoader.mm
    └── AAPLTerrainRenderer.mm

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 38 source declaration(s).

Overall architecture

Reference code

Application/main.m:12 — architecture anchor

int main (int argc, char* argv[])
{
    // ...
        srandom(0);
    // ...
}

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

Ownership evidence

Renderer/AAPLDebugRenderer.h:18 — stored dependency or nearest verified ownership anchor

@interface AAPLDebugLine : NSObject
// ...
    @property simd::float3 to;
// ...
@end
Owner Object or state Relationship Mutation authority
AAPLDebugRenderer float3 (to) stores or receives Header-visible collaborators
AAPLDebugRenderer float3 (from) stores or receives Header-visible collaborators
AAPLDebugRenderer float4 (color) stores or receives Header-visible collaborators
AAPLObjLoader float (boundingRadius) stores or receives Header-visible collaborators

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 metal_stdlib The cited file imports this module; runtime use and architectural role are not inferred. Renderer/AAPLDebugRenderer.metal:8
Source import simd The cited file imports this module; runtime use and architectural role are not inferred. Renderer/AAPLAllocator.h:16
Source import Metal The cited file imports this module; runtime use and architectural role are not inferred. Application/AAPLGameViewController.h:18
Source import TargetConditionals.h The cited file imports this module; runtime use and architectural role are not inferred. Application/WindowSceneDelegate.h:8
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. Renderer/AAPLAllocator.h:14
Source import MetalKit The cited file imports this module; runtime use and architectural role are not inferred. Application/AAPLGameViewController.h:19

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

Application/AAPLAppDelegate.h:16 — representative type boundary

#if TARGET_OS_IOS
@interface AAPLAppDelegate : UIResponder <UIApplicationDelegate>

@end
#endif
Type Responsibility Depends on or conforms to
AAPLAppDelegate Receives callback-driven events UIResponder, UIApplicationDelegate
AAPLGameView User-interface presentation and input forwarding MTKView
AAPLGameViewController View lifecycle, callbacks, and feature coordination UIViewController, MTKViewDelegate
AAPLDebugRenderer Owns drawing, GPU, or presentation processing NSObject
AAPLObjLoader Loads and prepares feature data or resources NSObject
AAPLVegetationRenderer Owns drawing, GPU, or presentation processing NSObject
WindowSceneDelegate Receives callback-driven events UIResponder, UIWindowSceneDelegate
AAPLMainRenderer Owns drawing, GPU, or presentation processing NSObject
AAPLParticleRenderer Owns drawing, GPU, or presentation processing NSObject
AAPLTerrainRenderer Owns drawing, GPU, or presentation processing 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
window (Application/WindowSceneDelegate.h:17) 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.
uniforms (Renderer/AAPLCamera.h:79) 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.
left (Renderer/AAPLCamera.h:82) 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.
right (Renderer/AAPLCamera.h:85) 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/WindowSceneDelegate.h:17 — representative boundary

#if TARGET_OS_IOS
@property (nonatomic, strong) UIWindow *window;
#endif

Swift 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 AAPLGameViewController The source’s Controller suffix makes this role explicit.
Receives callback-driven events AAPLAppDelegate, WindowSceneDelegate The source’s Delegate suffix makes this role explicit.
Loads and prepares feature data or resources AAPLObjLoader The source’s Loader suffix makes this role explicit.
Owns drawing, GPU, or presentation processing AAPLDebugRenderer, AAPLMainRenderer, AAPLParticleRenderer, AAPLTerrainRenderer The source’s Renderer suffix makes this role explicit.
User-interface presentation and input forwarding AAPLGameView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
View-controller organization Application/AAPLGameViewController.h:30 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:16 Callback protocols invert event delivery back into the sample’s owner.

Main application flow

Reference code

Application/AAPLGameViewController.mm:241drawInMTKView()

- (void) drawInMTKView:(nonnull MTKView*)view
{
    // ...
    @autoreleasepool
    {
        float translation_speed = 8.0f; // In meters
        float rotation_speed = 0.05f; // In radians
        if ([_pressedKeys containsObject: @(controlsFast)])         { translation_speed *= 10; }
        if ([_pressedKeys containsObject: @(controlsSlow)])         { translation_speed *= 0.1; rotation_speed *= 0.1f; }
        if ([_pressedKeys containsObject: @(controlsIncBrush)])     _renderer.brushSize *= 1.1f;
        if ([_pressedKeys containsObject: @(controlsDecBrush)])     _renderer.brushSize /= 1.1f;
        if ([_pressedKeys containsObject: @(controlsForward)])      _camera.position += _camera.forward * translation_speed;
        if ([_pressedKeys containsObject: @(controlsStrafeRight)])  _camera.position += _camera.right * translation_speed;
        if ([_pressedKeys containsObject: @(controlsStrafeLeft)])   _camera.position += _camera.left * translation_speed;
        if ([_pressedKeys containsObject: @(controlsStrafeUp)])     _camera.position += _camera.up * translation_speed;
        if ([_pressedKeys containsObject: @(controlsStrafeDown)])   _camera.position += _camera.down * translation_speed;
        if ([_pressedKeys containsObject: @(controlsBackward)])     _camera.position += _camera.backward * translation_speed;
        if ([_pressedKeys containsObject: @(controlsTurnLeft)])     [_camera rotateOnAxis:(float3) {0, 1, 0} radians: rotation_speed ];
        if ([_pressedKeys containsObject: @(controlsTurnRight)])    [_camera rotateOnAxis:(float3) {0, 1, 0} radians: -rotation_speed ];
        if ([_pressedKeys containsObject: @(controlsTurnUp)])       [_camera rotateOnAxis:_camera.right radians: rotation_speed ];
        if ([_pressedKeys containsObject: @(controlsTurnDown)])     [_camera rotateOnAxis:_camera.right radians: -rotation_speed ];
        if ([_pressedKeys containsObject: @(controlsRollLeft)])     [_camera rotateOnAxis:_camera.direction radians: -rotation_speed ];
        if ([_pressedKeys containsObject: @(controlsRollRight)])    [_camera rotateOnAxis:_camera.direction radians: rotation_speed ];
        [_camera rotateOnAxis:(float3) {0, 1, 0}   radians: _mouseDrag.x * -0.02f ];
        [_camera rotateOnAxis: _camera.right        radians: _mouseDrag.y * -0.02f ];
        _mouseDrag = (float2) { 0, 0 };
        id <MTLDrawable> drawable = _view.currentDrawable;
        MTLRenderPassDescriptor* renderPassDescriptor = _view.currentRenderPassDescriptor;
        if (drawable != NULL && renderPassDescriptor != NULL)
        {
            [_renderer UpdateWithDrawable: drawable
                     renderPassDescriptor: renderPassDescriptor
                        waitForCompletion: false ];
        }
    }
}

Naming conventions

  • Types: Controller: AAPLGameViewController; Delegate: AAPLAppDelegate, WindowSceneDelegate; Loader: AAPLObjLoader; Renderer: AAPLDebugRenderer, AAPLMainRenderer, AAPLParticleRenderer, AAPLTerrainRenderer, AAPLVegetationRenderer; View: AAPLGameView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: acceptsFirstResponder, acceptsFirstMouse, ModifyTerrain, application, applicationDidFinishLaunching, applicationWillTerminate, applicationShouldTerminateAfterLastWindowClosed, awakeFromNib.
  • Files: Application/AAPLAppDelegate.h, Application/AAPLGameViewController.h, Application/AAPLAppDelegate.mm, Application/AAPLGameViewController.mm, Renderer/AAPLDebugRenderer.h, Renderer/AAPLDebugRenderer.mm.

Architecture takeaways

  • main is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches metal_stdlib, simd, Metal, MetalKit 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/AAPLDebugRenderer.h Cited implementation, AAPLDebugLine, AAPLDebugRenderer
Application/AAPLAppDelegate.h AAPLAppDelegate, Cited implementation
Application/WindowSceneDelegate.h window, TargetConditionals.h, WindowSceneDelegate
Renderer/AAPLCamera.h uniforms, left, right, AAPLCamera
Application/AAPLGameViewController.h AAPLGameViewController, Metal, MetalKit, AAPLGameView
Renderer/AAPLDebugRenderer.metal metal_stdlib, Feature implementation
Renderer/AAPLAllocator.h simd, Foundation, AAPLAllocator, AAPLGpuBuffer
Renderer/AAPLParticleRenderer.metal ParticleVertexOut, ParticleVertexIn, ParticleData
Application/AAPLAppDelegate.mm AAPLAppDelegate
Application/AAPLGameViewController.mm AAPLGameView, AAPLGameViewController
Renderer/AAPLDebugRenderer.mm AAPLDebugLine, AAPLDebugRenderer
Renderer/AAPLMainRenderer.metal LightingVtxOut, LightingPsOut
Renderer/AAPLObjLoader.h AAPLObjMesh, AAPLObjLoader
Renderer/AAPLObjLoader.mm AAPLObjMesh, AAPLObjLoader
Renderer/AAPLTerrainRenderer.mm HabitatTextures, AAPLTerrainRenderer