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

Implementing a multistage image filter using heaps and fences

At a glance

Item Summary
Purpose Use fences to synchronize access to resources allocated on a heap.
App architecture Objective-C host code with Metal shaders; AAPLViewController hands work to AAPLRenderer, which owns heap-backed intermediate image stages before fence-ordered compute filters.
Main patterns Filter pipeline, Fence-based synchronization, Heap resource reuse
Scope High-level review of 14 scanned source files and 14 detected declarations; build assets are omitted.

Project structure

Source bundle/
├── Renderer/
│   ├── AAPLRenderer.m
│   ├── AAPLShaders.metal
│   └── AAPLShaderTypes.h
└── Application/
    ├── AAPLViewController.m
    ├── main.m
    ├── WindowSceneDelegate.m
    ├── AAPLAppDelegate.h
    ├── AAPLViewController.h
    └── AAPLAppDelegate.m

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:402 — explicit render/compute ordering with a fence

        [renderEncoder waitForFence:_fence
                       beforeStages:MTLRenderStageFragment];

        [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle
                          vertexStart:0
                          vertexCount:6];

        [renderEncoder updateFence:_fence
                       afterStages:MTLRenderStageFragment];

Interpretation

This is the dominant control/data path: platform code composes AAPLRenderer; that object controls heap-backed intermediate image stages; GPU-visible work ends in fence-ordered compute filters. The arrows summarize responsibility transfer, not a claim that every node directly calls the next.

Ownership and state

Ownership evidence

Application/AAPLViewController.m:29AAPLViewController creates and stores _renderer

@implementation AAPLViewController
// ...
    _renderer = [[AAPLRenderer alloc] initWithMetalKitView:_view];
// ...
@end
Owner Object or state Relationship Mutation authority
AAPLViewController _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
AAPLViewController selects/configures the view and composes the feature objects. UIViewController, NSViewController Application/AAPLViewController.h:15
AAPLRenderer owns pipeline/resource setup and per-frame command encoding. NSObject, MTKViewDelegate Renderer/AAPLRenderer.h:11
AAPLAppDelegate handles application/window lifecycle callbacks. UIResponder, UIApplicationDelegate Application/AAPLAppDelegate.h:10
WindowSceneDelegate handles application/window lifecycle callbacks. UIResponder, UIWindowSceneDelegate Application/WindowSceneDelegate.h:11
AAPLFilter defines heap sizing and fence-synchronized texture-filter execution. concrete Metal/framework collaborators Renderer/AAPLFilter.h:14
AAPLDownsampleFilter allocates a heap texture, blits the input, generates mipmaps, and updates the fence. NSObject, AAPLFilter Renderer/AAPLFilter.m:14

A local protocol/conformance boundary is present, so protocol-oriented collaboration is claimed only for that seam.

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/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:20)

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 AAPLViewControllerApplication/AAPLViewController.h:15 The type’s callbacks and stored state align with this responsibility.
Owns pipeline/resource setup and per-frame command encoding AAPLRendererRenderer/AAPLRenderer.h:11 The type’s callbacks and stored state align with this responsibility.
Handles application/window lifecycle callbacks AAPLAppDelegateApplication/AAPLAppDelegate.h:10 The type’s callbacks and stored state align with this responsibility.
Handles application/window lifecycle callbacks WindowSceneDelegateApplication/WindowSceneDelegate.h:11 The type’s callbacks and stored state align with this responsibility.
Heap-backed intermediate image stages AAPLRenderer / Renderer/AAPLRenderer.m:402 Keeps transient-resource use and cross-stage ordering in one command-encoding owner.
Fence-ordered compute filters Renderer/AAPLShaders.metal:20 GPU-parallel code remains in the Metal compilation boundary.

Shader boundary reference

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

vertex RasterizerData texturedQuadVertex( uint         vertexID  [[ vertex_id ]],
                             const device AAPLVertex * vertices  [[ buffer(AAPLVertexBufferIndexVertices) ]],
                             constant     float2 &     quadScale [[ buffer(AAPLVertexBufferIndexScale) ]])
{
    // ...
}

Design patterns

Pattern Source evidence Purpose or tradeoff
Filter pipeline Renderer/AAPLRenderer.m:402 Makes the filter stages’ wait/draw/update dependency explicit.
Fence-based synchronization Application/AAPLViewController.m:29 Makes the sample’s heap-backed intermediate image stages an explicit, reviewable boundary.
Heap resource reuse Renderer/AAPLShaders.metal:20 Makes the sample’s heap-backed intermediate image stages an explicit, reviewable boundary.

Naming conventions

  • Role suffixes make ownership visible: AAPLViewController, AAPLRenderer, AAPLAppDelegate, WindowSceneDelegate.
  • Method names describe setup or encoding actions: application, viewDidLoad, prefersHomeIndicatorAutoHidden, scene, sceneDidDisconnect, initWithMetalKitView, loadImages.
  • Shader entry points use stage/operation names: texturedQuadVertex, texturedQuadFragment, gaussianblurHorizontal, gaussianblurVertical.
  • 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 AAPLViewController focused on composition; AAPLRenderer is the owner of heap-backed intermediate image stages.
  • Treat fence-ordered compute filters as a separate execution/compilation boundary with explicit resource and data-layout contracts.
  • The verified ownership edge is AAPLViewController_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/AAPLRenderer.m AAPLRenderer
Application/AAPLViewController.m AAPLViewController
Renderer/AAPLShaders.metal entry point or feature implementation
Application/main.m entry point or feature implementation
Application/WindowSceneDelegate.m WindowSceneDelegate
Renderer/AAPLShaderTypes.h entry point or feature implementation
Application/AAPLAppDelegate.h AAPLAppDelegate
Application/AAPLViewController.h AAPLViewController, AAPLViewController
Application/AAPLAppDelegate.m AAPLAppDelegate