Skip to content

feat(receiver): add validated native Metal renderer - #174

Draft
Betafer wants to merge 2 commits into
swellweb:maint-3.5from
Betafer:codex/feat-native-metal-renderer-v3.5
Draft

Betafer wants to merge 2 commits into
swellweb:maint-3.5from
Betafer:codex/feat-native-metal-renderer-v3.5

Conversation

@Betafer

@Betafer Betafer commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Add an optional native CAMetalLayer renderer for VideoToolbox NV12 frames, while retaining the existing SDL/OpenGL renderer as the default and immediate fallback.

The native path wraps the decoder CVPixelBuffer planes through CVMetalTextureCache, avoiding the full-frame CPU upload performed by SDL_UpdateNVTexture. It also renders the remote cursor in the same Metal pass, preserves the display's Display P3 color space and accounts for Retina backing scale.

Safety

  • OpenGL remains the default backend
  • native Metal is enabled only through TB_RECEIVER_RENDER_DRIVER=metal-native or auto
  • auto mode evaluates real submitted, completed and dropped frames before keeping Metal
  • renderer creation or frame submission failure immediately exposes the existing OpenGL path
  • software-decoded/raw frames continue through OpenGL
  • the current normal-size macOS cursor behavior is preserved

Structure

  1. add the opt-in zero-copy renderer and focused bridge test
  2. add adaptive health policy, Display P3 handling and reproducible Metal/OpenGL benchmarks

Verification

  • rebased cleanly onto maint-3.5 at v3.5.1-rc.3
  • Receiver make test: 664 logic checks pass; the platform test builds successfully
  • full Receiver build succeeds
  • Apple M4, 4096 x 2304 at 60 fps: Metal 180/180 frames, 0 drops, 0.223 ms average submission, 1.306 ms average GPU time, Display P3
  • same run with SDL/OpenGL: 180/180 frames, 7.553 ms average frame call, 2430 MiB CPU upload in three seconds
  • extended 2017 Intel Retina iMac test: 600/600 Metal frames, 0 drops, hardware VideoToolbox HEVC and Display P3

This is intentionally a Draft PR because the production change is larger than the maintenance fixes and benefits from maintainer review before enabling it outside an opt-in setting.

@swellweb

Copy link
Copy Markdown
Owner

Maintenance review: this stays experimental and outside 3.6.1. Before integration it needs a current-base rebase and measured checks for color management, native cursor behavior, fullscreen/wake, fallback and sustained frame pacing on Intel and Apple Silicon. The open color issues are not being marked fixed by this draft.

@ziyadasghar-GIT

Copy link
Copy Markdown

Thanks for this, @Betafer. I ported it onto maint-3.6 and have been running it on a 2017 5K iMac under Ventura. Here's the data in case it helps take it out of draft.

Branch: ziyadasghar-GIT:port/pr174-on-maint-3.6

  1. fix(receiver): present only the newest decoded frame under backlog: my receiver PR (fix(receiver): present only the newest decoded frame under backlog #230), which this sits on.
  2. and 3. Your two commits, authorship kept. The only conflict was with that newest-frame path. present_pending_frame() now tries tb_disp_render_native_nv12() first and falls back to the CPU-plane OpenGL upload, so stale frames are still skipped while Metal is active. Plus trivial Makefile and include merges with the 3.6.1 status-layout test.
  3. (optional, for discussion) Default to auto when TB_RECEIVER_RENDER_DRIVER is unset, with =opengl restoring the old default. Finder and Dock launches never see environment variables, so without something like this normal users can't reach the native path. Feel free to drop it.

Tests: all 7 receiver suites pass, including your renderer-policy (22) and Metal shader tests. The Metal source compiles with -mmacosx-version-min=13.0 -Wunguarded-availability-new with no warnings except the deprecated-but-working CVBufferGetAttachment.

Hardware: MacBook Air M5 → iMac18,3 (Radeon Pro, Ventura 13.7.8), Thunderbolt Bridge. x86_64 receiver cross-built against the official 3.6.0 bundle's dylibs.

Adaptive mode on real use: adaptive renderer selected native Metal: completed=120 gpuAvg=1.957ms drops=0.0%. No fallback in any session.

5K 48, OpenGL vs native Metal, same receiver binary:

OpenGL Metal
Receiver CPU 25–27% 12–14%
GPU rail power 48–50 W 41–43 W
GPU utilisation 67–80% 35–46%
GPU die temperature 61 °C 57–58 °C
Receiver RSS 352–407 MB 227–280 MB
Frames shown ~47 fps ~47 fps

5K 60, Metal: a median of 58 fps (range 49–61) shown on the iMac, against 45–50 with OpenGL, with receiver CPU at 15.5%. On this hardware the OpenGL upload was what held 5K 60 back. The user also reports the block artifacts are gone at 5K 60.

Latency (Air paint → iMac present, measured with a timestamp barcode decoded in the receiver, clock offset tracked every 2 s over the Thunderbolt link): OpenGL 67.6 / 67.4 ms vs Metal 72.7 / 66.8 and 63.6 / 62.9 ms median, in interleaved sessions. That's no significant difference. On a discrete Radeon the CPU→GPU copy is cheap, so the win here is efficiency and 5K 60 frame rate, not latency. The rest of the latency is capture cadence plus 5K HEVC encode and decode.


Port, measurements and write-up done with Claude Opus 5.5 (Anthropic), credited in the commit trailers.

mjfrank added a commit to mjfrank/targetBridge that referenced this pull request Nov 16, 2026
…mental) [skip ci]

Inspired by upstream PR swellweb#174, built into our own Metal renderer and the
virtual display instead of a second renderer.

Sender:
- New per-session setting "Colour space (experimental)": Automatic, sRGB,
  Display P3. Automatic picks Display P3 when the Receiver reports a P3
  panel, otherwise sRGB.
- The chosen colour space sets the virtual display's primaries, the
  ScreenCaptureKit / CGDisplayStream colour space (BT.709 matrix), the
  encoder's colour tags, and is announced in the session ack
  ("colorSpace": "srgb" / "displayP3") before the first frame.
- Receivers without colour management (older versions, SDL renderers) get
  the previous behaviour: wide-gamut virtual display, unlabelled video.

Receiver:
- Display profile reports supportsColorSpaceTags (TargetBridge Metal
  renderer) and panelColorSpace (NSScreen P3 gamut).
- Tags its Metal layer from the ack; untagged when nothing is announced.
- Expert setting "Zero-copy H.264/HEVC frames": decoded IOSurface frames
  become Metal textures via CVMetalTextureCache, kept for 4 frames; other
  frames are copied as before.
- Stats overlay shows zero-copy/copy and the colour space.

Tests: color_space.h (Receiver), TBVideoColorSpaceTests (Sender).
Docs: changelog, Performance. Translations in all five languages.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015AiywzXyRsfrH6x9nyNXRQ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants