Rendering hover effects in Metal immersive apps
At a glance
| Item | Summary |
|---|---|
| Purpose | Change the appearance of a rendered onscreen element when a player gazes at it. |
| App architecture | A C/Objective-C header, Metal, Swift sample with the source-visible chain CompositorServicesHoverEffectApp → SettingsView → AppModel → TileResolvePipeline → CompositorServices APIs. |
| Main patterns | Binding-based state propagation, Actor isolation |
| Project style | 18 scanned source file(s) across C/Objective-C header, Metal, Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: Task, await suspension point, Sendable or @Sendable, actor; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, @Observable. |
| Key frameworks/packages | CompositorServices, MetalKit, SwiftUI, ARKit, ModelIO; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── CS_HoverEffect/
└── CS_HoverEffect/
├── App/
│ ├── CompositorServicesHoverEffectApp.swift
│ ├── CompositorServicesHoverEffectApp+CompositorLayer.swift
│ └── CompositorServicesHoverEffectApp+Render.swift
├── Data/
│ ├── DataStructures.swift
│ ├── AppModel.swift
│ ├── RenderData+Animate.swift
│ ├── RenderData.swift
│ └── RenderData+AssetLoad.swift
├── TileResolver/
│ └── TileResolvePipeline.swift
├── Views/
│ └── SettingsView.swift
├── Shaders/
│ └── Shaders.metal
└── Extensions/
└── CompositorLayer+FunctionInit.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 3 project/configuration file(s) and 16 source declaration(s).
Overall architecture
flowchart LR
N1["CompositorServicesHoverEffectApp"]
N2["SettingsView"]
N3["AppModel"]
N4["TileResolvePipeline"]
N5["CompositorServices APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
Reference code
CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp.swift:19 — architecture anchor
@main
struct CompositorServicesHoverEffectApp: App {
// ...
@State var appModel = AppModel()
// ...
}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 Compositor Services.
Ownership and state
classDiagram
CompositorServicesHoverEffectApp *-- AppModel : appModel
AppCompositorContent o-- AppModel : appModel
AppCompositorContent o-- RemoteDeviceIdentifier : remoteDeviceIdentifier
DrawCallMaterial o-- MTLTexture : texture
Ownership evidence
CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp.swift:22 — stored dependency or nearest verified ownership anchor
@main
struct CompositorServicesHoverEffectApp: App {
// ...
@State var appModel = AppModel()
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
CompositorServicesHoverEffectApp |
AppModel (appModel) |
owns wrapper-managed state | App/module collaborators |
AppCompositorContent |
AppModel (appModel) |
stores or receives | App/module collaborators |
AppCompositorContent |
RemoteDeviceIdentifier (remoteDeviceIdentifier) |
stores or receives | Owning lexical scope |
DrawCallMaterial |
MTLTexture (texture) |
stores or receives | 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 |
|---|---|---|---|
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp+Render.swift:23 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp+Render.swift:29 |
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | CS_HoverEffect/CS_HoverEffect/Data/DataStructures.swift:19 |
| Actor isolation | actor |
The cited type is actor-isolated; this does not select a background thread. | CS_HoverEffect/CS_HoverEffect/Data/DataStructures.swift:30 |
@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
CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp+Render.swift:23 — representative execution boundary
Task(priority: .high) {
// ...
layerRenderer: renderer,
remoteDeviceIdentifier: remoteDeviceIdentifier,
appModel: appModel
// ...
}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. | CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp.swift:22 |
| State propagation | @Observable |
Observation macro publishes source-visible changes. | CS_HoverEffect/CS_HoverEffect/Data/AppModel.swift:20 |
| Source import | CompositorServices |
The cited file imports this module; runtime use and architectural role are not inferred. | CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp+CompositorLayer.swift:11 |
| Source import | MetalKit |
The cited file imports this module; runtime use and architectural role are not inferred. | CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp+CompositorLayer.swift:15 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp+CompositorLayer.swift:8 |
| Source import | ARKit |
The cited file imports this module; runtime use and architectural role are not inferred. | CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp+CompositorLayer.swift:14 |
| Source import | ModelIO |
The cited file imports this module; runtime use and architectural role are not inferred. | CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp+CompositorLayer.swift:13 |
| Source import | RealityKit |
The cited file imports this module; runtime use and architectural role are not inferred. | CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp+CompositorLayer.swift:9 |
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
CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp.swift:20 — representative type boundary
@main
struct CompositorServicesHoverEffectApp: App {
// ...
@State var appModel = AppModel()
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
CompositorServicesHoverEffectApp |
Application entry and top-level composition | App |
Scene |
Scene lifecycle or scene-level composition | Concrete collaborators/imported frameworks |
AppModel |
Feature data or observable state | Concrete collaborators/imported frameworks |
TileResolvePipeline |
Coordinates a multi-stage processing flow | Concrete collaborators/imported frameworks |
SettingsView |
User-interface presentation and input forwarding | View |
AppCompositorContent |
Represents a feature value or composable behavior | CompositorContent |
DrawCallMaterial |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
DrawCall |
Represents a feature value or composable behavior | Sendable |
ShaderConstants |
Represents a feature value or composable behavior | Hashable |
TextureCache |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
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 |
|---|---|---|---|
remoteDeviceIdentifier (CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp.swift:76) |
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. |
loadSkybox (CS_HoverEffect/CS_HoverEffect/Data/RenderData+AssetLoad.swift:38) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: hide an implementation step that is not part of the collaboration surface. |
loadSkyboxShader (CS_HoverEffect/CS_HoverEffect/Data/RenderData+AssetLoad.swift:54) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: hide an implementation step that is not part of the collaboration surface. |
load (CS_HoverEffect/CS_HoverEffect/Data/RenderData+AssetLoad.swift:92) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: hide an implementation step that is not part of the collaboration surface. |
Reference code
CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp.swift:76 — representative boundary
struct AppCompositorContent: CompositorContent {
// ...
private var remoteDeviceIdentifier: RemoteDeviceIdentifier?
// ...
}Swift 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 | CompositorServicesHoverEffectApp |
The source’s App suffix makes this role explicit. |
| Feature data or observable state | AppModel |
The source’s Model suffix makes this role explicit. |
| Coordinates a multi-stage processing flow | TileResolvePipeline |
The source’s Pipeline suffix makes this role explicit. |
| Scene lifecycle or scene-level composition | Scene |
The source’s Scene suffix makes this role explicit. |
| User-interface presentation and input forwarding | SettingsView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Binding-based state propagation | CS_HoverEffect/CS_HoverEffect/Views/SettingsView.swift:12 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
| Actor isolation | CS_HoverEffect/CS_HoverEffect/Data/DataStructures.swift:30 |
A declared actor creates an explicit isolation boundary; its executor is not described as a background thread. |
Main application flow
sequenceDiagram
participant RenderData
participant device
participant appModel
participant library
RenderData->>RenderData: withTaskGroup()
RenderData->>RenderData: setUpShaderVariantsArray()
RenderData->>device: makeDefaultLibrary()
RenderData->>appModel: enableHoverEffects()
RenderData->>library: makeFunction()
RenderData->>library: makeFunction()
RenderData->>appModel: enableMSAA()
RenderData->>RenderData: updatePipelineState()
Reference code
CS_HoverEffect/CS_HoverEffect/Data/RenderData.swift:223 — setUpShaderPipeline()
constants.setConstantValue(&debugColors, type: .bool, index: Int(FunctionConstantDebugColors.rawValue))
pDesc.vertexFunction = try! await library.makeFunction(
name: "vertexShader",
constantValues: constants
)
pDesc.fragmentFunction = try! await library.makeFunction(
name: "fragmentShader",
constantValues: constants
)
pDesc.vertexDescriptor = vDesc
pDesc.maxVertexAmplificationCount = layerRenderer.properties.viewCount
if await appModel.enableMSAA {
pDesc.rasterSampleCount = 4
}Naming conventions
- Types: App: CompositorServicesHoverEffectApp; Model: AppModel; Pipeline: TileResolvePipeline; Scene: Scene; View: SettingsView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
add,easeInOutQuad,easeOutSine,animate,tap,updatePipelineState,getDepthState,getRenderTexture. - Files:
CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp.swift,CS_HoverEffect/CS_HoverEffect/Data/AppModel.swift,CS_HoverEffect/CS_HoverEffect/TileResolver/TileResolvePipeline.swift,CS_HoverEffect/CS_HoverEffect/Views/SettingsView.swift,CS_HoverEffect/CS_HoverEffect/Data/RenderData.swift.
Architecture takeaways
CompositorServicesHoverEffectAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches CompositorServices, MetalKit, SwiftUI, ARKit 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 |
|---|---|
CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp.swift |
Cited implementation, CompositorServicesHoverEffectApp, SwiftUI state property wrapper, AppCompositorContent |
CS_HoverEffect/CS_HoverEffect/Data/RenderData+AssetLoad.swift |
Cited implementation, Feature implementation |
CS_HoverEffect/CS_HoverEffect/Views/SettingsView.swift |
Cited implementation, SettingsView |
CS_HoverEffect/CS_HoverEffect/Data/DataStructures.swift |
Scene, Sendable or @Sendable, actor, DrawCallMaterial, DrawCall, ShaderConstants, TextureCache |
CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp+Render.swift |
Task, await suspension point, Feature implementation |
CS_HoverEffect/CS_HoverEffect/Data/AppModel.swift |
@Observable, AppModel |
CS_HoverEffect/CS_HoverEffect/App/CompositorServicesHoverEffectApp+CompositorLayer.swift |
CompositorServices, MetalKit, SwiftUI, ARKit, ModelIO, RealityKit, Feature implementation |
CS_HoverEffect/CS_HoverEffect/TileResolver/TileResolvePipeline.swift |
TileResolvePipeline |
CS_HoverEffect/CS_HoverEffect/Shaders/Shaders.metal |
VertexIn, VertexOut, FragmentOut |
CS_HoverEffect/CS_HoverEffect/Data/RenderData+Animate.swift |
AnimationState |
CS_HoverEffect/CS_HoverEffect/Data/RenderData.swift |
RenderData |
CS_HoverEffect/CS_HoverEffect/Extensions/CompositorLayer+FunctionInit.swift |
Configuration |
CS_HoverEffect/CS_HoverEffect/Data/RenderData+Render.swift |
Feature implementation |
CS_HoverEffect/CS_HoverEffect/Data/Scene+Animate.swift |
Feature implementation |
CS_HoverEffect/CS_HoverEffect/Extensions/LayerRenderer-Clock-Instant-Duration+timeInterval.swift |
Feature implementation |
CS_HoverEffect/CS_HoverEffect/Extensions/SIMD+Utilities.swift |
Feature implementation |