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 A C/Objective-C header, Metal, Objective-C, Objective-C++ sample with the source-visible chain mainAAPLWindowControllerAAPLRendererMetalKit APIs.
Main patterns View-controller organization, Delegate or data-source callbacks
Project style 22 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, MetalKit, UIKit, Cocoa, Foundation; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── Application/
│   ├── main.m
│   ├── iOS/
│   │   ├── AAPLAppDelegate.h
│   │   ├── AAPLAppDelegate.m
│   │   ├── WindowSceneDelegate.h
│   │   ├── WindowSceneDelegate.m
│   │   ├── AAPLViewControllerIOS.h
│   │   └── AAPLViewControllerIOS.m
│   └── macOS/
│       ├── AAPLWindowController.m
│       └── AAPLWindowController.h
└── Renderer/
    ├── AAPLRenderer.m
    ├── AAPLShaders.metal
    └── AAPLRenderer.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 9 project/configuration file(s) and 14 source declaration(s).

Overall architecture

Reference code

Application/main.m:18 — architecture anchor

#if defined(TARGET_IOS) || defined(TARGET_TVOS)
int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AAPLAppDelegate class]));
    }
}
#endif

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

Application/iOS/WindowSceneDelegate.h:13 — stored dependency or nearest verified ownership anchor

@interface WindowSceneDelegate : UIResponder <UIWindowSceneDelegate>

@property (nonatomic, strong) UIWindow *window;

@end
Owner Object or state Relationship Mutation authority
WindowSceneDelegate UIWindow (window) retains or copies an assigned value Header-visible collaborators
AAPLRenderer float (bloomIntensity) stores or receives Header-visible collaborators
AAPLRenderer float (bloomThreshold) stores or receives Header-visible collaborators
AAPLRenderer float (bloomRange) 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 simd The cited file imports this module; runtime use and architectural role are not inferred. Renderer/AAPLMathUtilities.h:9
Source import MetalKit The cited file imports this module; runtime use and architectural role are not inferred. Application/iOS/AAPLViewControllerIOS.m:8
Source import UIKit The cited file imports this module; runtime use and architectural role are not inferred. Application/iOS/AAPLAppDelegate.h:8
Source import Cocoa The cited file imports this module; runtime use and architectural role are not inferred. Application/macOS/AAPLWindowController.h:8
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. Application/UIDefaults.h:11
Source import stdlib.h The cited file imports this module; runtime use and architectural role are not inferred. Renderer/AAPLMathUtilities.h:8

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/iOS/AAPLAppDelegate.h:10 — representative type boundary

@interface AAPLAppDelegate : UIResponder <UIApplicationDelegate>

@end
Type Responsibility Depends on or conforms to
AAPLAppDelegate Receives callback-driven events UIResponder, UIApplicationDelegate
WindowSceneDelegate Receives callback-driven events UIResponder, UIWindowSceneDelegate
AAPLWindowController View lifecycle, callbacks, and feature coordination NSWindowController
AAPLRenderer Owns drawing, GPU, or presentation processing NSObject, MTKViewDelegate
GaussSample Represents a feature value or composable behavior Concrete collaborators/imported frameworks
VertexIn Represents a feature value or composable behavior Concrete collaborators/imported frameworks
GeometryVertexOut Represents a feature value or composable behavior Concrete collaborators/imported frameworks
SkyDomeVertexOut Represents a feature value or composable behavior Concrete collaborators/imported frameworks
FSQVertexOut Represents a feature value or composable behavior Concrete collaborators/imported frameworks
BloomVertexOut Represents a feature value or composable behavior Concrete collaborators/imported frameworks

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
isUIDisplayed (Application/iOS/AAPLViewControllerIOS.h:12) 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/iOS/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.
isUIDisplayed (Application/macOS/AAPLViewControllerMac.h:14) 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.
bloomIntensity (Renderer/AAPLRenderer.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.

Reference code

Application/iOS/AAPLViewControllerIOS.h:12 — representative boundary

@interface AAPLViewControllerIOS : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
// For menu bar interaction
@property (nonatomic) BOOL isUIDisplayed;
@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 AAPLWindowController The source’s Controller suffix makes this role explicit.
Receives callback-driven events AAPLAppDelegate, WindowSceneDelegate The source’s Delegate suffix makes this role explicit.
Owns drawing, GPU, or presentation processing AAPLRenderer The source’s Renderer suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
View-controller organization Application/macOS/AAPLWindowController.h:12 A controller is the verified coordination boundary; this is MVC-style only where a separate model is present.
Delegate or data-source callbacks Application/iOS/AAPLAppDelegate.h:10 Callback protocols invert event delivery back into the sample’s owner.

Main application flow

Reference code

Renderer/AAPLRenderer.m:1022encodeSceneExposureCalculationWithCommandBuffer()

- (void)encodeSceneExposureCalculationWithCommandBuffer:(id<MTLCommandBuffer>)commandBuffer
{
    // ...
    if (_exposureType == kExposureControlTypeKey)
    {
        id<MTLCommandBuffer> averageLuminanceCommandBuffer = [_commandQueue commandBuffer];
        averageLuminanceCommandBuffer.label = [NSString stringWithFormat:@"Avg Luminance CommandBuffer %lu", _cameraAnimationFrameIndex];
        __weak AAPLRenderer * weakSelf = self;
        [averageLuminanceCommandBuffer addCompletedHandler:^(id<MTLCommandBuffer> cb)
        {
            AAPLRenderer * strongSelf = weakSelf;
            strongSelf->_averageLuminanceDuration = cb.GPUEndTime - cb.GPUStartTime;
            return;
        }];
        MTLRenderPassDescriptor * rpd = [MTLRenderPassDescriptor renderPassDescriptor];
        rpd.colorAttachments[0].texture = _logLuminanceTexture;
        rpd.colorAttachments[0].loadAction = MTLLoadActionClear;
        rpd.colorAttachments[0].storeAction = MTLStoreActionStore;
        rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0);
        id<MTLRenderCommandEncoder> rce = [averageLuminanceCommandBuffer renderCommandEncoderWithDescriptor:rpd];
        rce.label = @"Log Luminance";
        [rce setDepthStencilState:_depthStateDisabled];
        [rce setCullMode:MTLCullModeBack];
        [rce setRenderPipelineState:_logLuminancePipeline];
        [rce setFragmentTexture:_sceneLinearColorTexture atIndex:0];
        [rce drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3];
        [rce endEncoding];
        id<MTLBlitCommandEncoder> bce = [averageLuminanceCommandBuffer blitCommandEncoder];
        bce.label = @"Mipmap Gen: Avg Luminance";
        [bce generateMipmapsForTexture:_logLuminanceTexture];
        [bce endEncoding];
        [averageLuminanceCommandBuffer commit];
    }
}

Naming conventions

  • Types: Controller: AAPLWindowController; Delegate: AAPLAppDelegate, WindowSceneDelegate; Renderer: AAPLRenderer.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: initWithMetalKitView, setTonemapWhitepoint, cameraAnimationStepCount, minimumResolutionScale, maximumResolutionScale, setResolutionScale, updateWithDevice, updateWithSize.
  • Files: Application/iOS/AAPLAppDelegate.h, Renderer/AAPLRenderer.m, Application/macOS/AAPLWindowController.m, Application/iOS/AAPLAppDelegate.m, Application/iOS/WindowSceneDelegate.h, Application/iOS/WindowSceneDelegate.m.

Architecture takeaways

  • main is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches simd, MetalKit, 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
Application/iOS/WindowSceneDelegate.h Cited implementation, window, WindowSceneDelegate
Application/iOS/AAPLAppDelegate.h AAPLAppDelegate, Cited implementation, UIKit
Application/iOS/AAPLViewControllerIOS.h isUIDisplayed, AAPLViewControllerIOS
Application/macOS/AAPLViewControllerMac.h isUIDisplayed, AAPLViewControllerMac
Renderer/AAPLRenderer.h bloomIntensity, AAPLRenderer
Application/macOS/AAPLWindowController.h AAPLWindowController, Cocoa
Renderer/AAPLMathUtilities.h simd, stdlib.h, Feature implementation
Application/iOS/AAPLViewControllerIOS.m MetalKit, AAPLViewControllerIOS
Application/UIDefaults.h Foundation, Feature implementation
Renderer/AAPLRenderer.m AAPLRenderer
Renderer/AAPLShaders.metal GaussSample, VertexIn, GeometryVertexOut, SkyDomeVertexOut, FSQVertexOut, BloomVertexOut
Application/macOS/AAPLWindowController.m AAPLWindowController
Application/iOS/AAPLAppDelegate.m AAPLAppDelegate
Application/iOS/WindowSceneDelegate.m WindowSceneDelegate
Application/macOS/AAPLViewControllerMac.m AAPLViewControllerMac