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

Processing HDR images with Metal

At a glance

Item Summary
Purpose Implement a post-processing pipeline using the latest features on Apple GPUs.
App architecture Objective-C, Objective-C++ host code with Metal shaders; AAPLWindowController hands work to AAPLRenderer, which owns HDR scene renderer and post processor before tone-map/bloom shader stages.
Main patterns Post-processing pipeline, Renderer boundary, Host-shader data contract
Scope High-level review of 22 scanned source files and 22 detected declarations; build assets are omitted.

Project structure

Source bundle/
├── Application/
│   ├── iOS/
│   │   ├── AAPLViewControllerIOS.m
│   │   ├── WindowSceneDelegate.m
│   │   ├── AAPLAppDelegate.h
│   │   └── AAPLAppDelegate.m
│   ├── main.m
│   └── macOS/
│       └── AAPLWindowController.m
└── Renderer/
    ├── AAPLShaders.metal
    ├── AAPLRenderer.m
    └── AAPLShaderTypes.h

Structure observations

  • The entry/composition boundary and renderer or operation boundary are separate in the source; AAPLRenderer 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

Reference code

Renderer/AAPLRenderer.m:808 — ordered bloom and tone-mapping stages

        [self encodeBloomSetupWithCommandBuffer:commandBuffer];
        [self encodeBloomSamplingFiltersWithCommandBuffer:commandBuffer];
        [self encodeBloomCompositeAndToneMappingWithCommandBuffer:commandBuffer view:view];

Interpretation

This is the dominant control/data path: platform code composes AAPLRenderer; that object controls HDR scene renderer and post processor; GPU-visible work ends in tone-map/bloom shader stages. The arrows summarize responsibility transfer, not a claim that every node directly calls the next.

Ownership and state

Ownership evidence

Application/iOS/AAPLViewControllerIOS.m:90AAPLViewControllerIOS creates and stores _renderer

    _renderer = [[AAPLRenderer alloc] initWithMetalKitView:_view cameraStepCount:kDefaultCameraStepCount resolutionScale:kDefaultResolutionScale];

    NSAssert(_renderer, @"Renderer failed initialization");

    [_renderer mtkView:_view drawableSizeWillChange:_view.drawableSize];

    _view.delegate = _renderer;
Owner Object or state Relationship Mutation authority
AAPLViewControllerIOS _renderer / AAPLRenderer Creates and stores; the diagram uses composition because construction is source-visible. The declaring scope performs setup and replacement.
AAPLRenderer 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
AAPLWindowController selects/configures the view and composes the feature objects. NSWindowController Application/macOS/AAPLWindowController.h:12
AAPLRenderer owns pipeline/resource setup and per-frame command encoding. NSObject, MTKViewDelegate Renderer/AAPLRenderer.h:14
AAPLAppDelegate handles application/window lifecycle callbacks. UIResponder, UIApplicationDelegate Application/iOS/AAPLAppDelegate.h:10
WindowSceneDelegate handles application/window lifecycle callbacks. UIResponder, UIWindowSceneDelegate Application/iOS/WindowSceneDelegate.h:11
AAPLViewControllerIOS owns the iOS Metal view and renderer, translating gestures, picker choices, and settings controls into HDR-rendering options. UIViewController, UIPickerViewDataSource, UIPickerViewDelegate Application/iOS/AAPLViewControllerIOS.m:26
AAPLViewControllerMac owns the macOS Metal view and renderer, mapping switches, sliders, and text fields to HDR and post-processing options. NSViewController Application/macOS/AAPLViewControllerMac.m:15

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/iOS/AAPLAppDelegate.h:10)
AAPLRenderer 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/AAPLRenderer.m:1)
AAPLShaders 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/AAPLShaders.metal:218)

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 AAPLWindowControllerApplication/macOS/AAPLWindowController.h:12 The type’s callbacks and stored state align with this responsibility.
Owns pipeline/resource setup and per-frame command encoding AAPLRendererRenderer/AAPLRenderer.h:14 The type’s callbacks and stored state align with this responsibility.
Handles application/window lifecycle callbacks AAPLAppDelegateApplication/iOS/AAPLAppDelegate.h:10 The type’s callbacks and stored state align with this responsibility.
Handles application/window lifecycle callbacks WindowSceneDelegateApplication/iOS/WindowSceneDelegate.h:11 The type’s callbacks and stored state align with this responsibility.
HDR scene renderer and post processor AAPLRenderer / Application/iOS/AAPLViewControllerIOS.m:18 Keeps Metal/framework setup and encoding out of entry or lifecycle code.
Tone-map/bloom shader stages Renderer/AAPLShaders.metal:218 GPU-parallel code remains in the Metal compilation boundary.

Shader boundary reference

Renderer/AAPLShaders.metal:218 — representative GPU entry/helper

vertex GeometryVertexOut GeometryVertex(const uint vertexID [[ vertex_id ]],
                                        const uint instanceID [[instance_id]],
                                        const VertexIn input [[stage_in]],
                                        const device AAPLUniforms& uniforms [[buffer(AAPLBufferIndexUniforms)]])
{
    // ...
}

Design patterns

Pattern Source evidence Purpose or tradeoff
Post-processing pipeline Application/iOS/AAPLViewControllerIOS.m:18 Makes the sample’s HDR scene renderer and post processor an explicit, reviewable boundary.
Renderer boundary Application/iOS/AAPLViewControllerIOS.m:90 UI/lifecycle code composes a renderer while GPU state and encoding stay together.
Host-shader data contract Renderer/AAPLRenderer.m:14 Shared indices/structs and matching bindings couple host encoding to shader signatures deliberately.

Naming conventions

  • Role suffixes make ownership visible: AAPLWindowController, AAPLRenderer, AAPLAppDelegate, WindowSceneDelegate.
  • Method names describe setup or encoding actions: initWithMetalKitView, setTonemapWhitepoint, cameraAnimationStepCount, minimumResolutionScale, maximumResolutionScale, setResolutionScale, updateWithDevice.
  • Shader entry points use stage/operation names: GeometryVertex, GeometryFragment, SkyDomeVertex, SkyDomeFragment, FSQVertex, LogLuminanceFragment.
  • 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 AAPLWindowController focused on composition; AAPLRenderer is the owner of HDR scene renderer and post processor.
  • Treat tone-map/bloom shader stages as a separate execution/compilation boundary with explicit resource and data-layout contracts.
  • The verified ownership edge is AAPLViewControllerIOS_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
Application/iOS/AAPLViewControllerIOS.m AAPLViewControllerIOS
Renderer/AAPLShaders.metal GaussSample, VertexIn, GeometryVertexOut, SkyDomeVertexOut, FSQVertexOut
Application/main.m entry point or feature implementation
Renderer/AAPLRenderer.m AAPLRenderer
Application/iOS/WindowSceneDelegate.m WindowSceneDelegate
Renderer/AAPLShaderTypes.h entry point or feature implementation
Application/iOS/AAPLAppDelegate.h AAPLAppDelegate
Application/macOS/AAPLWindowController.m AAPLWindowController, AAPLWindowController
Application/iOS/AAPLAppDelegate.m AAPLAppDelegate