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 | A C/Objective-C header, Metal, Objective-C sample with the source-visible chain main → AAPLConfigurationViewController → AAPLRenderer → metal_stdlib APIs. |
| Main patterns | View-controller organization, Delegate or data-source callbacks |
| Project style | 18 scanned source file(s) across C/Objective-C header, Metal, 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 | metal_stdlib, UIKit, Cocoa, simd, TargetConditionals.h; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── Application/
│ ├── main.m
│ ├── AAPLAppDelegate.m
│ ├── AAPLAppDelegate.h
│ ├── iOS/
│ │ ├── AAPLSplitViewController.m
│ │ └── AAPLSplitViewController.h
│ ├── AAPLConfigurationViewController.h
│ ├── AAPLConfigurationViewController.m
│ ├── AAPLRenderViewController.h
│ ├── AAPLRenderViewController.m
│ ├── WindowSceneDelegate.h
│ └── WindowSceneDelegate.m
└── Renderer/
└── 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.
- The verified tree contains 7 project/configuration file(s) and 8 source declaration(s).
Overall architecture
flowchart LR
N1["main"]
N2["AAPLConfigurationViewController"]
N3["AAPLRenderer"]
N4["metal_stdlib APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
Application/main.m:17 — architecture anchor
#if defined(TARGET_IOS)
int main(int argc, char * argv[]) {
// ...
#error No simulator support for Metal API for this SDK version. Build for a device instead.
// ...
}
#endifInterpretation
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
classDiagram
AAPLAppDelegate o-- UIWindow : window
AAPLConfigurationViewController o-- AAPLRenderViewController : renderViewController
WindowSceneDelegate o-- UIWindow : window
Ownership evidence
Application/AAPLAppDelegate.m:13 — stored dependency or nearest verified ownership anchor
#if TARGET_IOS
@property (nonatomic, strong) UIWindow *window;
#endif| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
AAPLAppDelegate |
UIWindow (window) |
retains or copies an assigned value | Owning Objective-C implementation |
AAPLConfigurationViewController |
AAPLRenderViewController (renderViewController) |
stores or receives | Header-visible collaborators |
WindowSceneDelegate |
UIWindow (window) |
retains or copies an assigned value | 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 | metal_stdlib |
The cited file imports this module; runtime use and architectural role are not inferred. | Renderer/AAPLShaders.metal:8 |
| Source import | UIKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Application/WindowSceneDelegate.h:8 |
| Source import | Cocoa |
The cited file imports this module; runtime use and architectural role are not inferred. | Application/main.m:13 |
| Source import | simd |
The cited file imports this module; runtime use and architectural role are not inferred. | Renderer/AAPLShaderTypes.h:11 |
| Source import | TargetConditionals.h |
The cited file imports this module; runtime use and architectural role are not inferred. | Application/main.m:10 |
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/AAPLAppDelegate.m:12 — representative type boundary
#if TARGET_IOS
@interface AAPLWindowSceneDelegate : UIResponder <UIWindowSceneDelegate>
@property (nonatomic, strong) UIWindow *window;
@end
#endif| Type | Responsibility | Depends on or conforms to |
|---|---|---|
AAPLWindowSceneDelegate |
Receives callback-driven events | UIResponder, UIWindowSceneDelegate |
AAPLAppDelegate |
Receives callback-driven events | PlatformAppDelegate |
AAPLConfigurationViewController |
View lifecycle, callbacks, and feature coordination | PlatformViewController |
AAPLRenderViewController |
View lifecycle, callbacks, and feature coordination | PlatformViewController |
WindowSceneDelegate |
Receives callback-driven events | UIResponder, UIWindowSceneDelegate |
AAPLSplitViewController |
View lifecycle, callbacks, and feature coordination | UISplitViewController |
AAPLRenderer |
Owns drawing, GPU, or presentation processing | NSObject, MTKViewDelegate |
RasterizerData |
Represents feature data | 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 |
|---|---|---|---|
window (Application/AAPLAppDelegate.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. |
window (Application/AAPLAppDelegate.m:13) |
implementation |
Visibility follows header/implementation and language linkage rules. | Inference: keep the declaration in the Objective-C implementation boundary. |
renderViewController (Application/AAPLConfigurationViewController.h:22) |
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/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. |
Reference code
Application/AAPLAppDelegate.h:21 — representative boundary
#if !defined(TARGET_IOS)
@property (strong, nonatomic) PlatformWindow *window;
#endifSwift 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 | AAPLConfigurationViewController, AAPLRenderViewController, AAPLSplitViewController |
The source’s Controller suffix makes this role explicit. |
| Receives callback-driven events | AAPLAppDelegate, AAPLWindowSceneDelegate, 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/AAPLConfigurationViewController.h:20 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Delegate or data-source callbacks | Application/AAPLAppDelegate.h:18 |
Callback protocols invert event delivery back into the sample’s owner. |
Main application flow
sequenceDiagram
participant AAPLRenderer
participant _commandQueue
participant NSString
participant renderEncoder
participant commandBuffer
AAPLRenderer->>_commandQueue: commandBuffer()
AAPLRenderer->>NSString: stringWithFormat()
AAPLRenderer->>renderEncoder: pushDebugGroup()
AAPLRenderer->>renderEncoder: setRenderPipelineState()
AAPLRenderer->>renderEncoder: setVertexBytes()
AAPLRenderer->>renderEncoder: setVertexBytes()
AAPLRenderer->>commandBuffer: presentDrawable()
AAPLRenderer->>commandBuffer: commit()
Reference code
Renderer/AAPLRenderer.m:371 — drawInMTKView()
@implementation AAPLRenderer
// ...
[commandBuffer commit];
// ...
@endNaming conventions
- Types: Controller: AAPLConfigurationViewController, AAPLRenderViewController, AAPLSplitViewController; Delegate: AAPLAppDelegate, AAPLWindowSceneDelegate, WindowSceneDelegate; Renderer: AAPLRenderer.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
scene,sceneDidDisconnect,application,applicationShouldTerminateAfterLastWindowClosed,viewDidLoad,selectVisualizationType,selectOperation,selectIterations. - Files:
Application/AAPLAppDelegate.m,Application/AAPLAppDelegate.h,Application/iOS/AAPLSplitViewController.m,Application/AAPLConfigurationViewController.h,Application/AAPLConfigurationViewController.m,Application/AAPLRenderViewController.h.
Architecture takeaways
mainis the main source-visible entry or composition anchor for this sample.- Framework work reaches UIKit, metal_stdlib, Cocoa, 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.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
Application/main.m |
Cited implementation, Cocoa, TargetConditionals.h, Feature implementation |
Application/AAPLAppDelegate.m |
Cited implementation, AAPLWindowSceneDelegate, window, AAPLAppDelegate |
Application/AAPLAppDelegate.h |
window, Cited implementation, AAPLAppDelegate |
Application/AAPLConfigurationViewController.h |
renderViewController, AAPLConfigurationViewController |
Application/WindowSceneDelegate.h |
window, UIKit, WindowSceneDelegate |
Renderer/AAPLShaders.metal |
metal_stdlib, RasterizerData |
Renderer/AAPLShaderTypes.h |
simd, Feature implementation |
Application/iOS/AAPLSplitViewController.m |
AAPLSplitViewController |
Application/AAPLConfigurationViewController.m |
AAPLConfigurationViewController |
Application/AAPLRenderViewController.h |
AAPLRenderViewController |
Application/AAPLRenderViewController.m |
AAPLRenderViewController |
Application/WindowSceneDelegate.m |
WindowSceneDelegate |
Application/iOS/AAPLSplitViewController.h |
AAPLSplitViewController |
Renderer/AAPLRenderer.h |
AAPLRenderer |
Renderer/AAPLRenderer.m |
AAPLRenderer |
Renderer/AAPLUserCompiledFunction.metal |
Feature implementation |