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

Drawing a triangle with Metal 4

At a glance

Item Summary
Purpose Render a colorful, rotating 2D triangle by running draw commands with a render pipeline on a GPU.
App architecture C, Objective-C host code with Metal shaders; ViewController hands work to MetalKitViewDelegate, which owns renderer selection behind a shared delegate before Metal 4 or legacy triangle path.
Main patterns Strategy, Delegate-driven frame loop, Shared renderer contract
Scope High-level review of 28 scanned source files and 24 detected declarations; build assets are omitted.

Project structure

Source bundle/
├── View controller/
│   ├── MetalKitViewDelegate.m
│   └── ViewController.m
├── Shaders/
│   ├── Shaders.metal
│   └── ShaderTypes.h
├── Application/
│   ├── main.m
│   ├── WindowSceneDelegate.m
│   ├── AppDelegate.h
│   └── AppDelegate.m
└── Metal 4 renderer/
    └── Metal4Renderer.m

Structure observations

  • The entry/composition boundary and renderer or operation boundary are separate in the source; MetalKitViewDelegate 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

View controller/MetalKitViewDelegate.m:37 — feature handoff or setup anchor

@implementation MetalKitViewDelegate
// ...
@end

Interpretation

This is the dominant control/data path: platform code composes MetalKitViewDelegate; that object controls renderer selection behind a shared delegate; GPU-visible work ends in Metal 4 or legacy triangle path. The arrows summarize responsibility transfer, not a claim that every node directly calls the next.

Ownership and state

Ownership evidence

View controller/MetalKitViewDelegate.m:39MetalKitViewDelegate creates and stores renderer

@implementation MetalKitViewDelegate
// ...
            renderer = [[Metal4Renderer alloc] initWithMetalKitView:view];
// ...
@end
Owner Object or state Relationship Mutation authority
MetalKitViewDelegate renderer / Metal4Renderer Creates and stores; the diagram uses composition because construction is source-visible. The declaring scope performs setup and replacement.
MetalKitViewDelegate 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
ViewController selects/configures the view and composes the feature objects. PlatformViewController View controller/ViewController.h:17
Metal4Renderer owns pipeline/resource setup and per-frame command encoding. NSObject, Renderer Metal 4 renderer/Metal4Renderer+Compilation.h:12
MetalRenderer owns pipeline/resource setup and per-frame command encoding. NSObject, Renderer Metal renderer/MetalRenderer+Compilation.h:10
Renderer owns pipeline/resource setup and per-frame command encoding. NSObject Renderer common/RendererProtocol.h:11
AppDelegate handles application/window lifecycle callbacks. UIResponder, UIApplicationDelegate Application/AppDelegate.h:11
MetalKitViewDelegate handles application/window lifecycle callbacks. NSObject, MTKViewDelegate View controller/MetalKitViewDelegate.h:12
WindowSceneDelegate handles application/window lifecycle callbacks. UIResponder, UIWindowSceneDelegate Application/WindowSceneDelegate.h:11

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
AppDelegate 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/AppDelegate.h:11)
Metal4Renderer 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. (Metal 4 renderer/Metal4Renderer.m:1)
Shaders 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. (Shaders/Shaders.metal:39)

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 ViewControllerView controller/ViewController.h:17 The type’s callbacks and stored state align with this responsibility.
Owns pipeline/resource setup and per-frame command encoding Metal4RendererMetal 4 renderer/Metal4Renderer+Compilation.h:12 The type’s callbacks and stored state align with this responsibility.
Owns pipeline/resource setup and per-frame command encoding MetalRendererMetal renderer/MetalRenderer+Compilation.h:10 The type’s callbacks and stored state align with this responsibility.
Owns pipeline/resource setup and per-frame command encoding RendererRenderer common/RendererProtocol.h:11 The type’s callbacks and stored state align with this responsibility.
Renderer selection behind a shared delegate MetalKitViewDelegate / View controller/MetalKitViewDelegate.m:37 Keeps Metal/framework setup and encoding out of entry or lifecycle code.
Metal 4 or legacy triangle path Shaders/Shaders.metal:39 GPU-parallel code remains in the Metal compilation boundary.

Shader boundary reference

Shaders/Shaders.metal:39 — representative GPU entry/helper

vertex RasterizerData

Design patterns

Pattern Source evidence Purpose or tradeoff
Strategy Metal 4 renderer/Metal4Renderer.h:5 A common caller selects between concrete rendering implementations.
Delegate-driven frame loop View controller/MetalKitViewDelegate.m:61 A view/display callback triggers updates and command encoding; timing is inverted into the renderer.
Shared renderer contract Shaders/Shaders.metal:39 Makes the sample’s renderer selection behind a shared delegate an explicit, reviewable boundary.

Naming conventions

  • Role suffixes make ownership visible: ViewController, Metal4Renderer, MetalRenderer, Renderer, AppDelegate, MetalKitViewDelegate, WindowSceneDelegate.
  • Method names describe setup or encoding actions: application, scene, sceneDidDisconnect, initWithMetalKitView, updateViewportSize, renderFrameToView, isMissingRequirementsFromView.
  • Shader entry points use stage/operation names: fragmentShader.
  • Names favor concrete domain roles and target-local types; no broad public library namespace is introduced.

Architecture takeaways

  • Keep ViewController focused on composition; MetalKitViewDelegate is the owner of renderer selection behind a shared delegate.
  • Treat Metal 4 or legacy triangle path as a separate execution/compilation boundary with explicit resource and data-layout contracts.
  • The verified ownership edge is MetalKitViewDelegaterenderer; 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
View controller/MetalKitViewDelegate.m MetalKitViewDelegate
Shaders/Shaders.metal RasterizerData
Application/main.m entry point or feature implementation
View controller/ViewController.m ViewController
Metal 4 renderer/Metal4Renderer.m Metal4Renderer
Application/WindowSceneDelegate.m WindowSceneDelegate
Shaders/ShaderTypes.h entry point or feature implementation
Application/AppDelegate.h AppDelegate
Application/AppDelegate.m AppDelegate