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

Customizing shaders using function pointers and stitching

At a glance

Item Summary
Purpose Define custom shader behavior at runtime by creating functions from existing ones and preferentially linking to others in a dynamic library.
App architecture Objective-C host code with Metal shaders; AAPLConfigurationViewController hands work to AAPLRenderer, which owns function-table and stitched-function construction before indirectly selected shader functions.
Main patterns Runtime shader composition, Strategy by function selection, Renderer boundary
Scope High-level review of 18 scanned source files and 17 detected declarations; build assets are omitted.

Project structure

Source bundle/
├── Renderer/
│   ├── AAPLRenderer.m
│   ├── AAPLShaders.metal
│   └── AAPLShaderTypes.h
└── Application/
    ├── AAPLRenderViewController.m
    ├── main.m
    ├── iOS/
    │   └── AAPLSplitViewController.m
    ├── WindowSceneDelegate.m
    ├── AAPLAppDelegate.m
    └── AAPLAppDelegate.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:116 — feature handoff or setup anchor

@implementation AAPLRenderer
// ...
    MTLFunctionStitchingInputNode *zInput = [[MTLFunctionStitchingInputNode alloc] initWithArgumentIndex:0];
// ...
@end

Interpretation

This is the dominant control/data path: platform code composes AAPLRenderer; that object controls function-table and stitched-function construction; GPU-visible work ends in indirectly selected shader functions. The arrows summarize responsibility transfer, not a claim that every node directly calls the next.

Ownership and state

Ownership evidence

Application/AAPLRenderViewController.m:53AAPLRenderViewController creates and stores _renderer

@implementation AAPLRenderViewController
// ...
    _renderer = [[AAPLRenderer alloc] initWithMetalKitView:_view];
// ...
@end
Owner Object or state Relationship Mutation authority
AAPLRenderViewController _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
AAPLConfigurationViewController selects/configures the view and composes the feature objects. PlatformViewController Application/AAPLConfigurationViewController.h:20
AAPLRenderViewController selects/configures the view and composes the feature objects. PlatformViewController Application/AAPLRenderViewController.h:18
AAPLSplitViewController selects/configures the view and composes the feature objects. UISplitViewController Application/iOS/AAPLSplitViewController.h:12
AAPLRenderer owns pipeline/resource setup and per-frame command encoding. NSObject, MTKViewDelegate Renderer/AAPLRenderer.h:13
AAPLAppDelegate handles application/window lifecycle callbacks. PlatformAppDelegate Application/AAPLAppDelegate.h:18
AAPLWindowSceneDelegate handles application/window lifecycle callbacks. UIResponder, UIWindowSceneDelegate Application/AAPLAppDelegate.m:12
WindowSceneDelegate handles application/window lifecycle callbacks. UIResponder, UIWindowSceneDelegate Application/WindowSceneDelegate.h:11

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/AAPLAppDelegate.h:18)
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 callable functions Metal library boundary [[stitchable]] and [[visible]] functions expose only the symbols runtime composition requires. Keep the composable function surface explicit. (Renderer/AAPLShaders.metal:29)

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 AAPLConfigurationViewControllerApplication/AAPLConfigurationViewController.h:20 The type’s callbacks and stored state align with this responsibility.
Selects/configures the view and composes the feature objects AAPLRenderViewControllerApplication/AAPLRenderViewController.h:18 The type’s callbacks and stored state align with this responsibility.
Selects/configures the view and composes the feature objects AAPLSplitViewControllerApplication/iOS/AAPLSplitViewController.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:13 The type’s callbacks and stored state align with this responsibility.
Function-table and stitched-function construction AAPLRenderer / Renderer/AAPLRenderer.m:116 Keeps Metal/framework setup and encoding out of entry or lifecycle code.
Indirectly selected shader functions Renderer/AAPLShaders.metal:29 Composable GPU functions remain in the Metal compilation boundary.

Shader boundary reference

Renderer/AAPLShaders.metal:29 — stitchable arithmetic functions

[[stitchable]] float2 add(float2 a, float2 b)
{
    return a + b;
}

Design patterns

Pattern Source evidence Purpose or tradeoff
Runtime shader composition Renderer/AAPLRenderer.m:116 Makes the sample’s function-table and stitched-function construction an explicit, reviewable boundary.
Strategy by function selection Application/AAPLRenderViewController.m:53 Makes the sample’s function-table and stitched-function construction an explicit, reviewable boundary.
Renderer boundary Renderer/AAPLShaders.metal:29 UI/lifecycle code composes a renderer while stitchable functions stay in the GPU library.

Naming conventions

  • Role suffixes make ownership visible: AAPLConfigurationViewController, AAPLRenderViewController, AAPLSplitViewController, AAPLRenderer, AAPLAppDelegate, AAPLWindowSceneDelegate, WindowSceneDelegate.
  • Method names describe setup or encoding actions: scene, sceneDidDisconnect, application, applicationShouldTerminateAfterLastWindowClosed, viewDidLoad, selectVisualizationType, selectOperation.
  • Shader and callable-function names state their role: mandlebrotFragment, colorEscaped, add, subtract, multiply, and negate.
  • 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 AAPLConfigurationViewController focused on composition; AAPLRenderer is the owner of function-table and stitched-function construction.
  • Treat indirectly selected shader functions as a separate execution/compilation boundary with explicit resource and data-layout contracts.
  • The verified ownership edge is AAPLRenderViewController_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/AAPLRenderViewController.m AAPLRenderViewController
Renderer/AAPLShaders.metal RasterizerData
Application/main.m entry point or feature implementation
Application/iOS/AAPLSplitViewController.m AAPLSplitViewController, AAPLSplitViewController
Application/WindowSceneDelegate.m WindowSceneDelegate
Renderer/AAPLShaderTypes.h entry point or feature implementation
Application/AAPLAppDelegate.m AAPLWindowSceneDelegate, AAPLWindowSceneDelegate, AAPLAppDelegate, AAPLAppDelegate
Application/AAPLAppDelegate.h AAPLAppDelegate