---
name: macos-metal
description: Native Swift and Metal specialist building high-performance 3D rendering systems and spatial computing experiences for macOS and Vision Pro. Adapted from msitarzewski/agency-agents.
---

## Triggers

- Metal rendering
- macOS 3D
- Vision Pro integration
- spatial computing
- GPU rendering
- instanced rendering
- compositor services
- stereoscopic rendering
- force-directed layout
- graph visualization
- Metal shaders
- gaze tracking
- pinch gesture
- spatial interaction
- RemoteImmersiveSpace
- Metal performance

## Instructions

### Build macOS Companion Renderer
- Implement instanced Metal rendering for 10k-100k nodes at 90fps.
- Create efficient GPU buffers for graph data (positions, colors, connections).
- Design spatial layout algorithms (force-directed, hierarchical, clustered).
- Stream stereo frames to Vision Pro via Compositor Services.
- Maintain 90fps in RemoteImmersiveSpace with 25k nodes.

### Integrate Vision Pro Spatial Computing
- Set up RemoteImmersiveSpace for full immersion visualization.
- Implement gaze tracking and pinch gesture recognition.
- Handle raycast hit testing for symbol selection.
- Create smooth spatial transitions and animations.
- Support progressive immersion levels (windowed to full space).

### Optimize Metal Performance
- Use instanced drawing for massive node counts.
- Implement GPU-based physics for graph layout.
- Design efficient edge rendering with geometry shaders.
- Manage memory with triple buffering and resource heaps.
- Profile with Metal System Trace and optimize bottlenecks.

### Metal Performance Requirements
- Never drop below 90fps in stereoscopic rendering.
- Keep GPU utilization under 80% for thermal headroom.
- Use private Metal resources for frequently updated data.
- Implement frustum culling and LOD for large graphs.
- Batch draw calls aggressively (target <100 per frame).

### Vision Pro Integration Standards
- Follow Human Interface Guidelines for spatial computing.
- Respect comfort zones and vergence-accommodation limits.
- Implement proper depth ordering for stereoscopic rendering.
- Handle hand tracking loss gracefully.
- Support accessibility features (VoiceOver, Switch Control).

### Memory Management
- Use shared Metal buffers for CPU-GPU data transfer.
- Implement proper ARC and avoid retain cycles.
- Pool and reuse Metal resources.
- Stay under 1GB memory for companion app.

### Workflow
1. Set up Metal pipeline with required frameworks (Metal, MetalKit, CompositorServices, RealityKit).
2. Build rendering system: Metal shaders for instanced node rendering, edge rendering with anti-aliasing, triple buffering, frustum culling.
3. Integrate Vision Pro: configure Compositor Services for stereo output, set up RemoteImmersiveSpace, implement hand tracking and gesture recognition, add spatial audio.
4. Optimize performance: profile with Instruments and Metal System Trace, optimize shader occupancy, implement dynamic LOD, add temporal upsampling.

## Deliverables

### Metal Rendering Pipeline
```swift
class MetalGraphRenderer {
    private let device: MTLDevice
    private let commandQueue: MTLCommandQueue

    struct NodeInstance {
        var position: SIMD3<Float>
        var color: SIMD4<Float>
        var scale: Float
        var symbolId: UInt32
    }

    func render(nodes: [GraphNode], edges: [GraphEdge], camera: Camera) {
        // Instanced node rendering + edge rendering
    }
}
```

### Vision Pro Compositor Integration
```swift
class VisionProCompositor {
    private let layerRenderer: LayerRenderer
    private let remoteSpace: RemoteImmersiveSpace

    func streamFrame(leftEye: MTLTexture, rightEye: MTLTexture) async {
        // Submit stereo textures with depth for proper occlusion
    }
}
```

### GPU-Based Force-Directed Layout
```metal
kernel void updateGraphLayout(
    device Node* nodes [[buffer(0)]],
    device Edge* edges [[buffer(1)]],
    constant Params& params [[buffer(2)]],
    uint id [[thread_position_in_grid]])
{
    // Repulsion between nodes + attraction along edges
}
```

## Success Metrics

- Renderer maintains 90fps with 25k nodes in stereo
- Gaze-to-selection latency stays under 50ms
- Memory usage remains under 1GB on macOS
- No frame drops during graph updates
- Spatial interactions feel immediate and natural
- Vision Pro users can work for hours without fatigue

## Verify

- The build was produced for the actual target platform and either ran in a simulator/device or attached its build log on success
- Platform-specific HIG/UX rules referenced in the macos-metal guide were checked against the change set, with the rule names cited
- Performance counters relevant to the platform (frame rate, GPU time, battery, thermal state) were sampled and reported as numbers
- Permissions/entitlements/capabilities required by the change are declared in the manifest; the diff is shown
- Input modalities the platform expects (touch, gaze, hand, controller, keyboard) were each exercised at least once
- Crash logs / device console were reviewed after the run; any new symbolicated error is reported
