screen-vlm-stream
Zero-cgo (CGO_ENABLED=0) IOSurface to Metal VLM preprocessing stream with a
GPU tile-diff change gate on macOS using x/screentensor, iosurface, metal,
and (optionally) screencapturekit.
Pipeline
IOSurface (3840x2160 BGRA8)
-> NewTextureWithDescriptorIosurfacePlane (zero-copy IOSurface -> MTLTexture)
-> GPU tile-diff change gate (32x32 tiles vs. previous keyframe MTLTexture)
├─ max tile diff < threshold -> skip VLM preprocess (~90% static frames)
└─ max tile diff >= threshold -> Metal compute bilinear resize + CLIP normalize
-> [3, 384, 384] FP16/FP32 NCHW MTLBuffer
+ GPU blit update of keyframe texture
By default, the command generates a headless 4K (3840x2160) IOSurface stream
requiring no Screen Recording TCC permission: frame 0 is the initial desktop
keyframe, 90% of frames are static, and 10% apply localized moving-cursor and
window-region updates. Each frame is also processed through an FP32 CPU-copy
control path (IOSurfaceLock -> row copy -> ReplaceRegion -> Metal compute)
to verify numerical parity and measure speedup.
Pass -live-screen to attach directly to ScreenCaptureKit when Screen
Recording permission is granted.
Usage
CGO_ENABLED=0 go run ./examples/media/screen-vlm-stream
CGO_ENABLED=0 go run ./examples/media/screen-vlm-stream -dtype fp32
CGO_ENABLED=0 go run ./examples/media/screen-vlm-stream -live-screen -duration 5s -fps 30
Flags:
-width (default 3840): synthetic surface width
-height (default 2160): synthetic surface height
-vlm-size (default 384): square VLM tensor size ([3, N, N])
-frames (default 60): number of synthetic frames
-dtype (default fp16): output tensor dtype (fp16 or fp32)
-threshold (default 0.005): GPU tile-diff threshold in [0, 1]
-live-screen (default false): capture live desktop frames via ScreenCaptureKit
-duration (default 5s): capture duration with -live-screen
-fps (default 30): target capture rate with -live-screen
Tests
CGO_ENABLED=0 go test -v -count=1 ./x/screentensor ./examples/media/screen-vlm-stream
Verifies both FP16 (max_abs_diff <= 1e-3) and FP32 (max_abs_diff <= 1e-5)
numerical parity against the CPU-copy control, static frame skipping
(max_static_diff == 0), and localized cursor-update detection.
Measured Results
Measured on Apple M4 Max (macOS 26.6.2, CGO_ENABLED=0, 3840x2160 BGRA ->
[3, 384, 384] CLIP-normalized NCHW tensor, 60 frames: 6 changed / 54 static):
| DType |
Tensor Size |
Max Abs Diff vs FP32 Control |
Zero-Copy Changed (wall / GPU) |
Zero-Copy Static Skip (wall / GPU) |
Effective Gated Stream (wall / FPS) |
CPU-Copy Control (wall / GPU) |
Speedup (Changed / Stream) |
fp16 |
0.84 MiB |
0.000488 |
2451.2 µs / 710.0 µs |
2098.8 µs / 333.0 µs |
2134.0 µs / 468.6 FPS |
6074.9 µs / 80.0 µs |
2.48x / 2.85x |
fp32 |
1.69 MiB |
0 |
2285.0 µs / 686.7 µs |
1670.6 µs / 351.7 µs |
1732.1 µs / 577.3 FPS |
5903.2 µs / 81.4 µs |
2.58x / 3.41x |
On both runs, static frames scored max_static_diff = 0.0000 and localized
cursor/window updates scored min_changed_diff = 0.1918.