Rendering a windowed game in stereo
At a glance
| Item | Summary |
|---|---|
| Purpose | Bring an iOS or iPadOS game to visionOS and enhance it. |
| App architecture | A C/Objective-C header, Metal, Swift sample with the source-visible chain RealityKitStereoRenderingApp → WindowView → HeadPositionProvider → RealityKit APIs. |
| Main patterns | View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks, Binding-based state propagation, Actor isolation |
| Project style | 45 scanned source file(s) across C/Objective-C header, Metal, Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: async declaration or closure, Task, @MainActor, actor, MainActor.run; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, ObservableObject, @Observable. |
| Key frameworks/packages | MetalKit, SwiftUI, RealityKit, simd, Metal; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── RealityKit-Stereo-Rendering/
├── RealityKitStereoRenderingApp.swift
├── DeferredLighting/
│ ├── Renderer/
│ │ ├── Renderer.swift
│ │ ├── Scene.swift
│ │ ├── SinglePassDeferredRenderer.swift
│ │ ├── TraditionalDeferredRenderer.swift
│ │ └── Utilities/
│ │ └── BufferView.swift
│ ├── DeferredLightingView.swift
│ └── Application/
│ └── ViewController.swift
├── MetalVRREntity.swift
├── Settings.swift
├── HeadTracker.swift
└── ViewableArea.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, Swift.
- The verified tree contains 4 project/configuration file(s) and 57 source declaration(s).
Overall architecture
flowchart LR
N1["RealityKitStereoRenderingApp"]
N2["WindowView"]
N3["HeadPositionProvider"]
N4["RealityKit APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
RealityKit-Stereo-Rendering/RealityKitStereoRenderingApp.swift:12 — architecture anchor
@main
struct MetalVRRPortalSampleApp: App {
@Environment(\.openWindow) private var openWindow
// ...
}Interpretation
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 RealityKit.
Ownership and state
classDiagram
Renderer o-- MTLRenderPassDescriptor : shadowRenderPassDescriptor
Renderer o-- MTLCommandQueue : commandQueue
Renderer o-- Callback : didBeginFrame
Renderer o-- Callback : setGBufferTextures
Ownership evidence
RealityKit-Stereo-Rendering/DeferredLighting/Renderer/Renderer.swift:15 — stored dependency or nearest verified ownership anchor
lazy var shadowRenderPassDescriptor: MTLRenderPassDescriptor = {
let descriptor = MTLRenderPassDescriptor()
descriptor.depthAttachment.texture = scene.shadowMap
descriptor.depthAttachment.storeAction = .store
return descriptor
}()| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
Renderer |
MTLRenderPassDescriptor (shadowRenderPassDescriptor) |
stores or receives | App/module collaborators |
Renderer |
MTLCommandQueue (commandQueue) |
stores or receives | Initialized by the owner; the binding is immutable |
Renderer |
Callback (didBeginFrame) |
stores a callback | Initialized by the owner; the binding is immutable |
Renderer |
Callback (setGBufferTextures) |
stores a callback | Initialized by the owner; the binding is immutable |
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 |
|---|---|---|---|
| Suspension boundary | async declaration or closure |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | RealityKit-Stereo-Rendering/DeferredLighting/DeferredLightingView.swift:23 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | RealityKit-Stereo-Rendering/DeferredLighting/DeferredLightingView.swift:368 |
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | RealityKit-Stereo-Rendering/HeadTracker.swift:19 |
| Actor isolation | actor |
The cited type is actor-isolated; this does not select a background thread. | RealityKit-Stereo-Rendering/LazyAsync.swift:9 |
| Main isolation | MainActor.run |
The cited operation explicitly enters a main-actor-isolated region. | RealityKit-Stereo-Rendering/MetalVRREntity.swift:179 |
@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
RealityKit-Stereo-Rendering/DeferredLighting/DeferredLightingView.swift:23 — representative execution boundary
override func setup() async {
await super.setup()
self.components.set(DeferredLightingComponent())
}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 |
|---|---|---|---|
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | RealityKit-Stereo-Rendering/DeferredLighting/DeferredLightingView.swift:281 |
| State propagation | ObservableObject |
ObservableObject supplies an observation contract. | RealityKit-Stereo-Rendering/HeadTracker.swift:20 |
| State propagation | @Observable |
Observation macro publishes source-visible changes. | RealityKit-Stereo-Rendering/Settings.swift:11 |
| Source import | MetalKit |
The cited file imports this module; runtime use and architectural role are not inferred. | RealityKit-Stereo-Rendering/DeferredLighting/Application/ViewController.swift:16 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | RealityKit-Stereo-Rendering/DeferredLighting/DeferredLightingView.swift:8 |
| Source import | RealityKit |
The cited file imports this module; runtime use and architectural role are not inferred. | RealityKit-Stereo-Rendering/DeferredLighting/DeferredLightingView.swift:9 |
| Source import | simd |
The cited file imports this module; runtime use and architectural role are not inferred. | RealityKit-Stereo-Rendering/DeferredLighting/Renderer/Renderer.swift:8 |
| Source import | Metal |
The cited file imports this module; runtime use and architectural role are not inferred. | RealityKit-Stereo-Rendering/DeferredLighting/Renderer/DepthStencilStates.swift:8 |
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
RealityKit-Stereo-Rendering/DeferredLighting/Renderer/Renderer.swift:353 — representative type boundary
@objc protocol DrawableProviding {
var viewCount: Int { get }
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
MetalVRRPortalSampleApp |
Application entry and top-level composition | App |
WindowView |
User-interface presentation and input forwarding | View |
Renderer |
Owns drawing, GPU, or presentation processing | NSObject |
DeferredLightingComponent |
Stores entity-component data or behavior | TransientComponent |
DeferredLightingSystem |
Runs entity-component-system update logic | System |
DeferredLightingView |
User-interface presentation and input forwarding | View |
SettingsView |
User-interface presentation and input forwarding | View |
ViewController |
View lifecycle, callbacks, and feature coordination | PlatformViewController |
Scene |
Scene lifecycle or scene-level composition | Concrete collaborators/imported frameworks |
SinglePassDeferredRenderer |
Owns drawing, GPU, or presentation processing | Renderer |
The source explicitly defines local protocol relationships: ResolutionProbeGrid → RateFactorProviding, DeferredLightingEntity → DrawableProviding, DeferredLightingEntity → RenderDestination, MTKView → DrawableProviding, MTKView → RenderDestination.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
assetsBundle (Packages/RealityKit-Assets/Sources/RealityKit-Assets/RealityKit-Assets.swift:11) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
openWindow (RealityKit-Stereo-Rendering/DeferredLighting/DeferredLightingView.swift:285) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
openImmersiveSpace (RealityKit-Stereo-Rendering/DeferredLighting/DeferredLightingView.swift:286) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
dismissImmersiveSpace (RealityKit-Stereo-Rendering/DeferredLighting/DeferredLightingView.swift:287) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
Reference code
Packages/RealityKit-Assets/Sources/RealityKit-Assets/RealityKit-Assets.swift:11 — representative boundary
public let assetsBundle = Bundle.moduleSwift 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 |
|---|---|---|
| Application entry and top-level composition | MetalVRRPortalSampleApp |
The source’s App suffix makes this role explicit. |
| Stores entity-component data or behavior | DeferredLightingComponent |
The source’s Component suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | ViewController |
The source’s Controller suffix makes this role explicit. |
| Supplies a capability or framework resource | HeadPositionProvider |
The source’s Provider suffix makes this role explicit. |
| Owns drawing, GPU, or presentation processing | Renderer, SinglePassDeferredRenderer, TraditionalDeferredRenderer |
The source’s Renderer suffix makes this role explicit. |
| Scene lifecycle or scene-level composition | Scene |
The source’s Scene suffix makes this role explicit. |
| Runs entity-component-system update logic | DeferredLightingSystem, ResolutionProbeSystem |
The source’s System suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | RealityKit-Stereo-Rendering/DeferredLighting/Application/ViewController.swift:18 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Protocol-oriented abstraction | RealityKit-Stereo-Rendering/ResolutionProbeGrid.swift:47 |
A local protocol and concrete conformance create an explicit capability boundary. |
| Delegate or data-source callbacks | RealityKit-Stereo-Rendering/DeferredLighting/Renderer/Renderer.swift:339 |
Callback protocols invert event delivery back into the sample’s owner. |
| Binding-based state propagation | RealityKit-Stereo-Rendering/DeferredLighting/DeferredLightingView.swift:281 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
| Actor isolation | RealityKit-Stereo-Rendering/LazyAsync.swift:9 |
A declared actor creates an explicit isolation boundary; its executor is not described as a background thread. |
Naming conventions
- Types: App: MetalVRRPortalSampleApp; Component: DeferredLightingComponent; Controller: ViewController; Provider: HeadPositionProvider; Renderer: Renderer, SinglePassDeferredRenderer, TraditionalDeferredRenderer; Scene: Scene; System: DeferredLightingSystem, ResolutionProbeSystem; View: BufferView, DeferredLightingView, SettingsView, WindowView.
- Protocols:
DrawableProviding,RateFactorProviding,RenderDestination. - Methods:
beginFrame,beginDrawableCommands,endFrame,encodePass,encodeStage,encodeGBufferStage,encodeDirectionalLightingStage,encodeLightMaskStage. - Files:
RealityKit-Stereo-Rendering/DeferredLighting/Renderer/Renderer.swift,RealityKit-Stereo-Rendering/DeferredLighting/DeferredLightingView.swift,RealityKit-Stereo-Rendering/MetalVRREntity.swift,RealityKit-Stereo-Rendering/Settings.swift,RealityKit-Stereo-Rendering/DeferredLighting/Application/ViewController.swift,RealityKit-Stereo-Rendering/DeferredLighting/Renderer/Scene.swift.
Architecture takeaways
RealityKitStereoRenderingAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches MetalKit, SwiftUI, RealityKit, 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.
- Local protocol relationships provide an explicit substitution boundary.
Source map
| Source file | Relevant symbols |
|---|---|
RealityKit-Stereo-Rendering/RealityKitStereoRenderingApp.swift |
Cited implementation, MetalVRRPortalSampleApp, WindowView, OnDismiss |
RealityKit-Stereo-Rendering/DeferredLighting/Renderer/Renderer.swift |
Cited implementation, DrawableProviding, simd, Renderer |
Packages/RealityKit-Assets/Sources/RealityKit-Assets/RealityKit-Assets.swift |
Cited implementation, Feature implementation |
RealityKit-Stereo-Rendering/DeferredLighting/DeferredLightingView.swift |
Cited implementation, async declaration or closure, Task, SwiftUI state property wrapper, SwiftUI, RealityKit, DeferredLightingComponent, DeferredLightingEntity, DeferredLightingSystem, DeferredLightingView |
RealityKit-Stereo-Rendering/DeferredLighting/Application/ViewController.swift |
ViewController, MetalKit |
RealityKit-Stereo-Rendering/ResolutionProbeGrid.swift |
Cited implementation, ResolutionProbeGrid |
RealityKit-Stereo-Rendering/LazyAsync.swift |
LazyAsync, actor |
RealityKit-Stereo-Rendering/HeadTracker.swift |
@MainActor, ObservableObject, HeadPositionProvider |
RealityKit-Stereo-Rendering/MetalVRREntity.swift |
MainActor.run, RateFactors, RateFactorProviding, RateIndicator, RateMapIndicators, PercentageIndicator, RenderTarget, MetalVRREntity |
RealityKit-Stereo-Rendering/Settings.swift |
@Observable, Settings, Stereoscopy, OpenImmersiveSpace, WindowStyle, SettingsView, Parameter |
RealityKit-Stereo-Rendering/DeferredLighting/Renderer/DepthStencilStates.swift |
Metal, DepthStencilStates |
RealityKit-Stereo-Rendering/DeferredLighting/Renderer/Scene.swift |
Scene |
RealityKit-Stereo-Rendering/DeferredLighting/Renderer/SinglePassDeferredRenderer.swift |
SinglePassDeferredRenderer |
RealityKit-Stereo-Rendering/DeferredLighting/Renderer/TraditionalDeferredRenderer.swift |
TraditionalDeferredRenderer |
RealityKit-Stereo-Rendering/DeferredLighting/Renderer/Utilities/BufferView.swift |
BufferView |
RealityKit-Stereo-Rendering/ViewableArea.swift |
ViewableAreaBounds, ViewableAreaReader |