Sample CodemacOSReviewed 2026-07-21View on Apple Developer

Training a neural network to render irradiance in real time

At a glance

Item Summary
Purpose Train a small neural network on the GPU to approximate diffuse irradiance, and compare the result against Monte Carlo integration and a pre-trained ML model.
App architecture A C/Objective-C header, Metal, Objective-C, Python sample with the source-visible chain mainViewControllerRendererMetalKit / metal_stdlib APIs.
Main patterns Protocol-oriented abstraction, Delegate or data-source callbacks
Project style 18 scanned source file(s) across C/Objective-C header, Metal, Objective-C, Python, 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 Cocoa, metal_stdlib, MetalKit, simd, Metal; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── Application/
│   ├── main.m
│   ├── AppDelegate.h
│   ├── AppDelegate.m
│   ├── ViewController.h
│   └── ViewController.m
├── train_irradiance.py
└── Renderer/
    ├── IrradianceTechnique.h
    ├── Renderer.h
    ├── Renderer.m
    ├── IrradianceModel.h
    ├── IrradianceTechnique.m
    └── Composite.metal

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, Python.
  • The verified tree contains 5 project/configuration file(s) and 11 source declaration(s).

Overall architecture

Reference code

Application/main.m:10 — architecture anchor

int main(int argc, const char * argv[]) {
    return NSApplicationMain(argc, argv);
}

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/IrradianceTechnique.h:24 — stored dependency or nearest verified ownership anchor

@protocol IrradianceTechnique <NSObject>
// ...
@property (nonatomic, readonly) uint32_t totalSteps;
// ...
@end
Owner Object or state Relationship Mutation authority
IrradianceTechnique uint32_t (totalSteps) stores or receives The declaring implementation writes; property clients read
Renderer float (cameraYaw) stores or receives Header-visible collaborators
Renderer float (cameraPitch) stores or receives Header-visible collaborators
Renderer float (cameraDistance) 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 Cocoa The cited file imports this module; runtime use and architectural role are not inferred. Application/AppDelegate.h:8
Source import metal_stdlib The cited file imports this module; runtime use and architectural role are not inferred. Renderer/Composite.metal:8
Source import MetalKit The cited file imports this module; runtime use and architectural role are not inferred. Renderer/IrradianceTechnique.h:9
Source import simd The cited file imports this module; runtime use and architectural role are not inferred. Renderer/MathUtilities.h:11
Source import Metal The cited file imports this module; runtime use and architectural role are not inferred. Renderer/IrradianceModel.h:8
Source import metal_tensor The cited file imports this module; runtime use and architectural role are not inferred. Renderer/Composite.metal: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/IrradianceTechnique.h:12 — representative type boundary

@protocol IrradianceTechnique <NSObject>
// ...
@end
Type Responsibility Depends on or conforms to
AppDelegate Receives callback-driven events NSObject, NSApplicationDelegate
ViewController View lifecycle, callbacks, and feature coordination NSViewController
Renderer Owns drawing, GPU, or presentation processing NSObject, MTKViewDelegate
IrradianceTechnique Defines a capability or collaboration contract NSObject
IrradianceMLP Owns feature behavior and collaborator lifecycle nn.Module
_DecodedMLP Owns feature behavior and collaborator lifecycle nn.Module
IrradianceMonteCarloTechnique Defines a feature-specific type boundary NSObject, IrradianceTechnique
IrradianceMPPTechnique Defines a feature-specific type boundary NSObject, IrradianceTechnique
IrradianceMLEncoderTechnique Defines a feature-specific type boundary NSObject, IrradianceTechnique
MaterialParams Represents a feature value or composable behavior Concrete collaborators/imported frameworks

The source explicitly defines local protocol relationships: IrradianceMonteCarloTechniqueIrradianceTechnique, IrradianceMPPTechniqueIrradianceTechnique, IrradianceMLEncoderTechniqueIrradianceTechnique.

Access control

Symbol Access Verified effect Likely rationale
totalSteps (Renderer/IrradianceTechnique.h:24) 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.
totalSteps (Renderer/IrradianceTrainer.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.
cameraYaw (Renderer/Renderer.h:19) 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.
cameraPitch (Renderer/Renderer.h:20) 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

Renderer/IrradianceTechnique.h:24 — representative boundary

@protocol IrradianceTechnique <NSObject>
// ...
@property (nonatomic, readonly) uint32_t totalSteps;
// ...
@end

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 ViewController The source’s Controller suffix makes this role explicit.
Receives callback-driven events AppDelegate 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.

Design patterns

Pattern Source evidence Purpose or tradeoff
Protocol-oriented abstraction Renderer/IrradianceTechnique.h:29 A local protocol and concrete conformance create an explicit capability boundary.
Delegate or data-source callbacks Application/AppDelegate.h:10 Callback protocols invert event delivery back into the sample’s owner.

Main application flow

Reference code

Renderer/IrradianceTrainer.m:257dispatchKaimingInit()

- (void)dispatchKaimingInit
{
    // ...
    id<MTLBuffer> paramsBuffer = [_device newBufferWithLength:MLP_NUM_LAYERS * sizeof(KaimingInitParams)
                                                     options:MTLResourceStorageModeShared];
    KaimingInitParams *paramsPtr = (KaimingInitParams *)paramsBuffer.contents;
    for (uint32_t i = 0; i < MLP_NUM_LAYERS; i++) {
        uint32_t k = _model->layers[i].cols, n = _model->layers[i].rows;
        paramsPtr[i] = (KaimingInitParams){ .cols = k, .rows = n, .fanIn = (i == 0) ? 3 : k, .seed = arc4random() };
    }
    [_commandAllocator reset];
    [_commandBuffer beginCommandBufferWithAllocator:_commandAllocator];
    [_commandBuffer useResidencySet:_residencySet];
    id<MTL4ComputeCommandEncoder> enc = [_commandBuffer computeCommandEncoder];
    [enc setComputePipelineState:_kaimingInitPipelineState];
    MTL4ArgumentTableDescriptor *atd = [MTL4ArgumentTableDescriptor new];
    atd.maxBufferBindCount = 3;
    for (uint32_t i = 0; i < MLP_NUM_LAYERS; i++) {
        NSError *error;
        id<MTL4ArgumentTable> argTable = [_device newArgumentTableWithDescriptor:atd error:&error];
        [argTable setAddress:_masterWeightBuffers[i].gpuAddress atIndex:0];
        [argTable setResource:_model->layers[i].weights.gpuResourceID atBufferIndex:1];
        [argTable setAddress:paramsBuffer.gpuAddress + i * sizeof(KaimingInitParams) atIndex:2];
        [enc setArgumentTable:argTable];
        [enc dispatchThreads:MTLSizeMake(_model->layers[i].cols, _model->layers[i].rows, 1)
            threadsPerThreadgroup:MTLSizeMake(16, 2, 1)];
    }
    [enc endEncoding];
    [_commandBuffer endCommandBuffer];
    _eventValue++;
    [_commandQueue commit:&_commandBuffer count:1];
    [_commandQueue signalEvent:_event value:_eventValue];
    [_event waitUntilSignaledValue:_eventValue timeoutMS:UINT64_MAX];
}

Naming conventions

  • Types: Controller: ViewController; Delegate: AppDelegate; Renderer: Renderer.
  • Protocols: IrradianceTechnique.
  • Methods: load_hdr, _dirs_to_uv, _sample_env, _cosine_sample_hemisphere, _uniform_sphere, _compute_irradiance, __init__, forward.
  • Files: Application/AppDelegate.h, Application/AppDelegate.m, Application/ViewController.h, Application/ViewController.m, Renderer/IrradianceTechnique.h, Renderer/Renderer.h.

Architecture takeaways

  • main is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches Cocoa, MetalKit, metal_stdlib, simd 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.
  • Local protocol relationships provide an explicit substitution boundary.

Source map

Source file Relevant symbols
Application/main.m Cited implementation, Feature implementation
Renderer/IrradianceTechnique.h Cited implementation, IrradianceTechnique, totalSteps, MetalKit, IrradianceMonteCarloTechnique, IrradianceMPPTechnique, IrradianceMLEncoderTechnique
Renderer/IrradianceTrainer.h totalSteps, IrradianceTrainer
Renderer/Renderer.h cameraYaw, cameraPitch, Renderer
Application/AppDelegate.h Cited implementation, Cocoa, AppDelegate
Renderer/Composite.metal metal_stdlib, metal_tensor, MaterialParams
Renderer/MathUtilities.h simd, Feature implementation
Renderer/IrradianceModel.h Metal, Feature implementation
train_irradiance.py IrradianceMLP, _DecodedMLP
Application/AppDelegate.m AppDelegate
Application/ViewController.h ViewController
Application/ViewController.m ViewController
Renderer/Renderer.m Renderer
Renderer/IrradianceTechnique.m IrradianceMonteCarloTechnique, IrradianceMPPTechnique, IrradianceMLEncoderTechnique
Renderer/IrradianceTrainer.m IrradianceTrainer
Renderer/Geometry.metal Feature implementation