Supporting Simulator in a Metal app
At a glance
| Item | Summary |
|---|---|
| Purpose | Configure alternative render paths in your Metal app to enable running your app in Simulator. |
| App architecture | A C/Objective-C header, Metal, Objective-C, Swift sample bundle with entry-bearing project variants Objective-C, Swift, each leading to MetalKit APIs. |
| Main patterns | View-controller organization, Delegate or data-source callbacks |
| Project style | 19 scanned source file(s) across C/Objective-C header, Metal, Objective-C, Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: DispatchSemaphore; none alone proves a background thread. |
| State/event model | No structured observation or publisher-scheduling marker indexed. |
| Key frameworks/packages | simd, UIKit, Foundation, MetalKit, Cocoa; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── Objective-C/
│ ├── Application/
│ │ ├── main.m
│ │ ├── AppDelegate.h
│ │ ├── AppDelegate.m
│ │ ├── ViewController.h
│ │ ├── ViewController.m
│ │ ├── WindowSceneDelegate.h
│ │ └── WindowSceneDelegate.m
│ └── Renderer/
│ ├── Renderer.h
│ └── Renderer.m
└── Swift/
├── Application/
│ ├── AppDelegate.swift
│ └── ViewController.swift
└── Renderer/
└── Renderer.swift
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, Swift.
- The verified tree contains 19 project/configuration file(s) and 16 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["Objective-C"]
V2["Swift"]
Boundary["MetalKit APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
Objective-C/Application/main.m:19 — architecture anchor
#if defined(TARGET_IOS) || defined(TARGET_TVOS)
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
#endifInterpretation
The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.
Ownership and state
classDiagram
Renderer o-- MTLDevice : device
Renderer o-- MTLCommandQueue : commandQueue
Renderer o-- MTLBuffer : dynamicUniformBuffer
Renderer *-- Array : constantData
Ownership evidence
Swift/Renderer/Renderer.swift:31 — stored dependency or nearest verified ownership anchor
class Renderer: NSObject, MTKViewDelegate {
// ...
public let device: MTLDevice
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
Renderer |
MTLDevice (device) |
stores or receives | Initialized by the owner; the binding is immutable |
Renderer |
MTLCommandQueue (commandQueue) |
stores or receives | Initialized by the owner; the binding is immutable |
Renderer |
MTLBuffer (dynamicUniformBuffer) |
stores or receives | App/module collaborators |
Renderer |
Array (constantData) |
owns value state | App/module 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.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Synchronization | DispatchSemaphore |
The source references a synchronization primitive; the protected state requires surrounding review. | Swift/Renderer/Renderer.swift:46 |
@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.
Reference code
Swift/Renderer/Renderer.swift:46 — representative execution boundary
class Renderer: NSObject, MTKViewDelegate {
// ...
let inFlightSemaphore = DispatchSemaphore(value: maxBuffersInFlight)
// ...
}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 | simd |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/Renderer/Renderer.m:8 |
| Source import | UIKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/Application/AppDelegate.h:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/Renderer/ShaderTypes.h:15 |
| Source import | MetalKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/Renderer/Renderer.h:8 |
| Source import | Cocoa |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/Application/main.m:14 |
| Source import | TargetConditionals.h |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/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
Swift/Application/AppDelegate.swift:12 — representative type boundary
#if os(macOS)
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
// ...
}
#endif| Type | Responsibility | Depends on or conforms to |
|---|---|---|
AppDelegate |
Receives callback-driven events | NSObject, NSApplicationDelegate |
AppDelegate |
Receives callback-driven events | UIResponder, UIApplicationDelegate |
Renderer |
Owns drawing, GPU, or presentation processing | NSObject, MTKViewDelegate |
AppDelegate |
Receives callback-driven events | UIResponder, UIApplicationDelegate |
ViewController |
View lifecycle, callbacks, and feature coordination | PlatformViewController |
WindowSceneDelegate |
Receives callback-driven events | UIResponder, UIWindowSceneDelegate |
Renderer |
Owns drawing, GPU, or presentation processing | NSObject, MTKViewDelegate |
ViewController |
View lifecycle, callbacks, and feature coordination | PlatformViewController |
WindowSceneDelegate |
Receives callback-driven events | UIResponder, UIWindowSceneDelegate |
RendererError |
Represents feature failure conditions | Error |
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 |
|---|---|---|---|
transparencySlider (Objective-C/Application/ViewController.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. |
blendMode (Objective-C/Application/ViewController.h:23) |
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 (Objective-C/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. |
device (Swift/Renderer/Renderer.swift:31) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
Reference code
Objective-C/Application/ViewController.h:22 — representative boundary
#ifdef TARGET_IOS
@property (weak, nonatomic) IBOutlet UISlider *transparencySlider;
#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 | ViewController |
The source’s Controller suffix makes this role explicit. |
| Receives callback-driven events | AppDelegate, WindowSceneDelegate |
The source’s Delegate suffix makes this role explicit. |
| Owns drawing, GPU, or presentation processing | Renderer |
The source’s Renderer suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | Objective-C/Application/ViewController.h:19 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Delegate or data-source callbacks | Objective-C/Application/AppDelegate.h:10 |
Callback protocols invert event delivery back into the sample’s owner. |
Main application flow
sequenceDiagram
participant Renderer
participant _commandQueue
participant commandBuffer
participant renderEncoder
Renderer->>_commandQueue: commandBuffer()
Renderer->>commandBuffer: addCompletedHandler()
Renderer->>Renderer: updateState()
Renderer->>commandBuffer: renderCommandEncoderWithDescriptor()
Renderer->>renderEncoder: setFrontFacingWinding()
Renderer->>renderEncoder: setCullMode()
Renderer->>commandBuffer: presentDrawable()
Renderer->>commandBuffer: commit()
Reference code
Objective-C/Renderer/Renderer.m:539 — drawInMTKView()
@implementation Renderer
// ...
[commandBuffer commit];
// ...
@endNaming conventions
- Types: Controller: ViewController; Delegate: AppDelegate, WindowSceneDelegate; Renderer: Renderer.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
applicationShouldTerminateAfterLastWindowClosed,application,buildMetalVertexDescriptor,loadTexture,updateDynamicBufferState,uniformsForObject,updateGameState,bindVertexDescriptorsForMesh. - Files:
Swift/Application/AppDelegate.swift,Swift/Renderer/Renderer.swift,Objective-C/Application/AppDelegate.h,Objective-C/Application/AppDelegate.m,Objective-C/Application/ViewController.h,Objective-C/Application/ViewController.m.
Architecture takeaways
mainis the main source-visible entry or composition anchor for this sample.- Framework work reaches simd, UIKit, MetalKit, Cocoa 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 |
|---|---|
Objective-C/Application/main.m |
Cited implementation, Cocoa, TargetConditionals.h, Feature implementation |
Swift/Renderer/Renderer.swift |
Cited implementation, DispatchSemaphore, RendererError, Renderer, func |
Swift/Application/AppDelegate.swift |
AppDelegate |
Objective-C/Application/ViewController.h |
transparencySlider, blendMode, ViewController |
Objective-C/Application/WindowSceneDelegate.h |
window, WindowSceneDelegate |
Objective-C/Application/AppDelegate.h |
Cited implementation, UIKit, AppDelegate |
Objective-C/Renderer/Renderer.m |
simd, Renderer |
Objective-C/Renderer/ShaderTypes.h |
Foundation, Feature implementation |
Objective-C/Renderer/Renderer.h |
MetalKit, Renderer |
Objective-C/Application/AppDelegate.m |
AppDelegate |
Objective-C/Application/ViewController.m |
ViewController |
Objective-C/Application/WindowSceneDelegate.m |
WindowSceneDelegate |
Swift/Application/ViewController.swift |
ViewController |
Swift/Application/WindowSceneDelegate.swift |
WindowSceneDelegate |
Objective-C/Renderer/Shaders.metal |
Vertex, ColorInOut, ColorOut |
Swift/Renderer/Shaders.metal |
Vertex, ColorInOut, ColorOut |