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

Rendering a scene with forward plus lighting using tile shaders

At a glance

Item Summary
Purpose Implement a forward plus renderer using the latest features on Apple GPUs.
App architecture Objective-C host code with Metal shaders; AAPLViewController hands work to AAPLRenderer, which owns forward-plus light culling and rendering before tile shader and raster lighting stages.
Main patterns Tiled lighting pipeline, Renderer boundary, Host-shader data contract
Scope High-level review of 19 scanned source files and 21 detected declarations; build assets are omitted.

Project structure

Source bundle/
├── Renderer/
│   ├── AAPLRenderer.m
│   ├── AAPLCulling.metal
│   ├── AAPLShaderCommon.h
│   └── AAPLMesh.h
└── Application/
    ├── AAPLViewController.m
    ├── main.m
    ├── WindowSceneDelegate.m
    ├── AAPLAppDelegate.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:694 — feature handoff or setup anchor

        [renderEncoder dispatchThreadsPerTile:MTLSizeMake(AAPLTileWidth, AAPLTileHeight, 1)];
        [renderEncoder popDebugGroup];
                // Perform tile culling, to minimize the number of lights rendered per tile.
        [renderEncoder pushDebugGroup:@"Prepare Light Lists"];
        [renderEncoder setRenderPipelineState:_lightCullingPipelineState];
        [renderEncoder setThreadgroupMemoryLength:AAPLThreadgroupBufferSize offset:0 atIndex:AAPLThreadgroupBufferIndexLightList];
        [renderEncoder setThreadgroupMemoryLength:AAPLTileDataSize offset:AAPLThreadgroupBufferSize atIndex:AAPLThreadgroupBufferIndexTileData];
        [renderEncoder setTileBuffer:_frameDataBuffers[_currentBufferIndex] offset:0 atIndex:AAPLBufferIndexFrameData];

Interpretation

This is the dominant control/data path: platform code composes AAPLRenderer; that object controls forward-plus light culling and rendering; GPU-visible work ends in tile shader and raster lighting stages. 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. PlatformViewController Application/AAPLViewController.h:19
AAPLRenderer owns pipeline/resource setup and per-frame command encoding. NSObject, MTKViewDelegate Renderer/AAPLRenderer.h:10
AAPLAppDelegate handles application/window lifecycle callbacks. UIResponder, UIApplicationDelegate, NSObject Application/AAPLAppDelegate.h:11
WindowSceneDelegate handles application/window lifecycle callbacks. UIResponder, UIWindowSceneDelegate Application/WindowSceneDelegate.h:13
AAPLSubmesh loads and retains the material textures associated with one Model I/O submesh. NSObject, Model I/O material, Metal textures Renderer/AAPLMesh.m:13
AAPLMesh converts a Model I/O mesh into MetalKit buffers and owns its texture-backed submesh wrappers. NSObject, MDLMesh, MTKMesh, AAPLSubmesh Renderer/AAPLMesh.m:147

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:11)
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)
AAPLCulling 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/AAPLCulling.metal:48)

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:19 The type’s callbacks and stored state align with this responsibility.
Owns pipeline/resource setup and per-frame command encoding AAPLRendererRenderer/AAPLRenderer.h:10 The type’s callbacks and stored state align with this responsibility.
Handles application/window lifecycle callbacks AAPLAppDelegateApplication/AAPLAppDelegate.h:11 The type’s callbacks and stored state align with this responsibility.
Handles application/window lifecycle callbacks WindowSceneDelegateApplication/WindowSceneDelegate.h:13 The type’s callbacks and stored state align with this responsibility.
Forward-plus light culling and rendering AAPLRenderer / Renderer/AAPLRenderer.m:694 Keeps Metal/framework setup and encoding out of entry or lifecycle code.
Tile shader and raster lighting stages Renderer/AAPLCulling.metal:48 GPU-parallel code remains in the Metal compilation boundary.

Shader boundary reference

Renderer/AAPLCulling.metal:48 — representative GPU entry/helper

kernel void create_bins(imageblock<ColorData,imageblock_layout_implicit> imageBlock,
                        constant AAPLFrameData & frameData    [[ buffer(AAPLBufferIndexFrameData) ]],
                        device vector_float4* light_positions [[ buffer(AAPLBufferIndexLightsPosition) ]],
                        threadgroup TileData *tile_data       [[ threadgroup(AAPLThreadgroupBufferIndexTileData) ]],
                        ushort2 thread_local_position         [[ thread_position_in_threadgroup ]],
                        uint thread_linear_id                 [[ thread_index_in_threadgroup ]],
                        uint quad_lane_id                     [[ thread_index_in_quadgroup ]])
{
    // ...
}

Design patterns

Pattern Source evidence Purpose or tradeoff
Tiled lighting pipeline Renderer/AAPLRenderer.m:694 Makes the sample’s forward-plus light culling and rendering an explicit, reviewable boundary.
Renderer boundary Application/AAPLViewController.m:29 UI/lifecycle code composes a renderer while GPU state and encoding stay together.
Host-shader data contract Renderer/AAPLRenderer.m:15 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, newMeshesFromURL, viewDidLoad, scene, sceneDidDisconnect, initWithMetalKitView.
  • Shader entry points use stage/operation names: create_bins, cull_lights, depth_pre_pass_vertex, depth_pre_pass_fragment, fairy_vertex, fairy_fragment.
  • 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 forward-plus light culling and rendering.
  • Treat tile shader and raster lighting stages 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/AAPLCulling.metal AAPLPlane
Application/main.m entry point or feature implementation
Application/WindowSceneDelegate.m WindowSceneDelegate
Renderer/AAPLShaderCommon.h TileData, Vertex, ColorData
Application/AAPLAppDelegate.h AAPLAppDelegate, AAPLAppDelegate
Application/AAPLAppDelegate.m AAPLAppDelegate, AAPLAppDelegate
Renderer/AAPLMesh.h AAPLSubmesh, AAPLMesh