Implementing order-independent transparency with image blocks
At a glance
| Item |
Summary |
| Purpose |
Draw overlapping, transparent surfaces in any order by using tile shaders and image blocks. |
| App architecture |
Objective-C host code with Metal shaders; AAPLViewController hands work to AAPLRenderer, which owns image-block render-pass setup before tile-local transparency resolve. |
| Main patterns |
Renderer boundary, Tile-local accumulation, Host-shader data contract |
| Scope |
High-level review of 15 scanned source files and 14 detected declarations; build assets are omitted. |
Project structure
Source bundle/
├── Renderer/
│ ├── AAPLRenderer.m
│ └── Shaders/
│ ├── AAPLShaders.metal
│ └── AAPLShaderTypes.h
└── Application/
├── AAPLViewController.m
├── main.m
├── WindowSceneDelegate.m
├── AAPLAppDelegate.h
├── AAPLAppDelegate.m
└── AAPLViewController.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
flowchart LR
N1["AAPLViewController"]
N2["AAPLRenderer"]
N3["image-block render-pass setup"]
N4["tile-local transparency resolve"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
Renderer/AAPLRenderer.m:480 — tile initialization, transparent draws, and final blend
id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:_forwardRenderPassDescriptor];
renderEncoder.label = @"Forward Pass";
// Initialize the image block's memory before rendering.
[renderEncoder pushDebugGroup:@"Init Image Block"];
[renderEncoder setRenderPipelineState:_initImageBlockPipeline];
[renderEncoder dispatchThreadsPerTile:_optimalTileSize];
[renderEncoder popDebugGroup];
id<MTLBuffer> _actorParamsBuffers [MaxBuffersInFlight];
id<MTLBuffer> _cameraParamsBuffers [MaxBuffersInFlight];
Interpretation
This is the dominant control/data path: platform code composes AAPLRenderer; that object controls image-block render-pass setup; GPU-visible work ends in tile-local transparency resolve. The arrows summarize responsibility transfer, not a claim that every node directly calls the next.
Ownership and state
classDiagram
AAPLViewController *-- AAPLRenderer : _renderer
Ownership evidence
Application/AAPLViewController.m:36 — AAPLViewController creates and stores _renderer
_renderer = [[AAPLRenderer alloc] initWithMetalKitView:_mtkView];
NSAssert(_renderer, @"Renderer failed to initialize");
[_renderer mtkView:_mtkView drawableSizeWillChange:_mtkView.drawableSize];
_mtkView.delegate = _renderer;
| 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. |
PlatformViewController |
Application/AAPLViewController.h:21 |
AAPLRenderer |
owns pipeline/resource setup and per-frame command encoding. |
NSObject, MTKViewDelegate |
Renderer/AAPLRenderer.h:12 |
AAPLAppDelegate |
handles application/window lifecycle callbacks. |
UIResponder, UIApplicationDelegate, NSObject |
Application/AAPLAppDelegate.h:14 |
WindowSceneDelegate |
handles application/window lifecycle callbacks. |
UIResponder, UIWindowSceneDelegate |
Application/WindowSceneDelegate.h:16 |
AAPLActor |
stores color and transform state for a quad-mesh instance. |
NSObject |
Renderer/AAPLActor.m:10 |
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:14) |
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/Shaders/AAPLShaders.metal:100) |
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 |
AAPLViewController — Application/AAPLViewController.h:21 |
The type’s callbacks and stored state align with this responsibility. |
| Owns pipeline/resource setup and per-frame command encoding |
AAPLRenderer — Renderer/AAPLRenderer.h:12 |
The type’s callbacks and stored state align with this responsibility. |
| Handles application/window lifecycle callbacks |
AAPLAppDelegate — Application/AAPLAppDelegate.h:14 |
The type’s callbacks and stored state align with this responsibility. |
| Handles application/window lifecycle callbacks |
WindowSceneDelegate — Application/WindowSceneDelegate.h:16 |
The type’s callbacks and stored state align with this responsibility. |
| Image-block render-pass setup |
AAPLRenderer / Renderer/AAPLRenderer.m:480 |
Keeps tile initialization, transparent draws, and blending in one render-encoding owner. |
| Tile-local transparency resolve |
Renderer/Shaders/AAPLShaders.metal:100 |
Per-pixel-layer insertion remains in the Metal compilation boundary. |
Shader boundary reference
Renderer/Shaders/AAPLShaders.metal:100 — image-block transparent-fragment insertion
fragment TransparentFragmentStore processTransparentFragment
Design patterns
| Pattern |
Source evidence |
Purpose or tradeoff |
| Renderer boundary |
Renderer/AAPLRenderer.m:480 |
UI/lifecycle code composes a renderer while image-block encoding stays together. |
| Tile-local accumulation |
Application/AAPLViewController.m:36 |
Makes the sample’s image-block render-pass setup an explicit, reviewable boundary. |
| Host-shader data contract |
Renderer/AAPLRenderer.m:9 |
Shared indices/structs and matching bindings couple host encoding to shader signatures deliberately. |
Naming conventions
- Role suffixes make ownership visible:
AAPLViewController, AAPLRenderer, AAPLAppDelegate, WindowSceneDelegate.
- Method names describe setup or encoding actions:
application, applicationShouldTerminateAfterLastWindowClosed, viewDidLoad, acceptsFirstResponder, updateTransparencyMethod, toggleRotation, viewDidAppear.
- Shader entry points use stage/operation names:
quadPassVertex.
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 image-block render-pass setup.
- Treat tile-local transparency resolve 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/Shaders/AAPLShaders.metal |
TransparentFragmentValues, TransparentFragmentStore |
Application/main.m |
entry point or feature implementation |
Application/WindowSceneDelegate.m |
WindowSceneDelegate |
Renderer/Shaders/AAPLShaderTypes.h |
entry point or feature implementation |
Application/AAPLAppDelegate.h |
AAPLAppDelegate, AAPLAppDelegate |
Application/AAPLAppDelegate.m |
AAPLAppDelegate, AAPLAppDelegate |
Application/AAPLViewController.h |
AAPLViewController |