Skip to content

perf(encoder): read the bitstream back from cached memory - #67

Open
porkloin wants to merge 1 commit into
hgaiser:mainfrom
porkloin:bitstream-host-cached
Open

porkloin wants to merge 1 commit into
hgaiser:mainfrom
porkloin:bitstream-host-cached

Conversation

@porkloin

Copy link
Copy Markdown
Contributor

Currently, the encoder's bitstream buffer takes the first host-visible, coherent memory type. On RADV that's uncached, write-combined memory, and the CPU copies every encoded frame out of it slowly: at 4K120 on an RX 7900 XTX the copy takes 0.5–0.7 ms per frame, and over 3 ms while the GPU is also decoding.

This PR prefers a cached memory type for buffers the encoder writes into (VIDEO_ENCODE_DST). Other buffers, and devices without a coherent cached type, keep the current choice. The copy drops to about 0.1 ms, and Moonshine's capture-to-send latency falls 7–12% on an RX 7900 XTX, with GPU encode time unchanged.

TL;DR: does the same thing as 878db34 (the fix for #59), but for encode instead of decode.

Testing

pixelforge's tests

RX 7900 XTX (Mesa 26.2.7): 65 passed, 12 skipped, 2 failed. RX 9070 XT: 65 passed, 13 skipped, 1 failed. The failures, rate_control_changes on both and roundtrip_h265_top_quality_level on the RX 7900 XTX, happen the same way on main. cargo fmt and clippy -D warnings are clean.

Integration testing in Moonshine

pixelforge's tests don't measure latency, so both builds (this branch caf6dfd, main efa5780) were built into Moonshine 934c5c5 and streamed with moonshine-test-headless, which streams a test app to a headless Moonlight client on the same machine and records Moonshine's timing for every frame. The apps were vkcube and framestamp, a full-motion test pattern whose frames the client checks against the expected image. Each codec streamed for 20 s, 3 times per build.

RX 7900 XTX (RDNA3). 3840x2160 at 120 fps and 113 Mb/s, and 1920x1080 at 60 fps and 20 Mb/s. Host latency p50 (capture to packets sent), framestamp:

Mode, codec main this branch
4K120 AV1 5.40 ms 4.81 ms
4K120 H.264 6.14 ms 5.53 ms
4K120 HEVC 5.93 ms 5.52 ms
1080p60 AV1 1.78 ms 1.57 ms
1080p60 H.264 2.02 ms 1.80 ms
1080p60 HEVC 1.98 ms 1.82 ms

vkcube moved the same way (−6.8% to −12.2%). GPU encode time was identical; from encode finished to frame copied out took 480–740 µs on main and 70–130 µs here at 4K120, and 220–300 µs against 63–81 µs at 1080p60. When the client decodes on the same GPU, as in the harness output below, host latency p99 at 4K120 fell from 8.32 ms to 5.77 ms.

RX 9070 XT (RDNA4). 5 streams per codec. Against main at 1080p60 (vkcube), host latency p50 fell 4.3% for H.264 and 6.3% for HEVC, with GPU encode time within 1%. At 4K120 H.264 it was compared with an SSE4.1 streaming-load copy from the same buffer (5–7.5% faster than main on this GPU), in two runs in opposite order: the two matched, GPU encode 5866 µs against 5870 µs p50 in the second.

Not tested: NVIDIA and Intel. Problems there are unlikely: a driver without a host-visible, coherent and cached type for this buffer gets the current memory type, and the GPU's writes into cached memory cost nothing measurable on either AMD card.

Harness output

The full moonshine-test-headless summaries for the RX 7900 XTX runs above, one per mode. Their report cards come from streams the client also decodes, so their host latency is higher than in the table.

pixelforge caf6dfd vs main efa5780, in Moonshine 934c5c5 · 3840x2160 at 120 fps

Hardware

Host Client
GPU AMD Radeon RX 7900 XTX (RADV NAVI31) AMD Radeon RX 7900 XTX
Driver radv Mesa 26.2.7-arch1.1 (Vulkan) radeonsi Mesa 26.2.7-arch1.1 (VAAPI)
Other GPUs N/A N/A
CPU AMD Ryzen 9 5950X 16-Core Processor AMD Ryzen 9 5950X 16-Core Processor
Memory 126 GB 126 GB
OS Fedora Linux 44 (Container Image) Arch Linux
Kernel 7.2.11-200.fc44.x86_64 7.2.11-200.fc44.x86_64
Network N/A N/A
Machine X570D4U-2L2T X570D4U-2L2T

Stream

  • Moonshine 934c5c5, 3840x2160 at 120 fps, 113 Mb/s, streams of 20 s each, route loopback, chaos drop one frame in 120
  • workloads (streams): framestamp-full (15), vkcube (12); 64510 frames decoded; clock sync ±0.02 ms
  • tools: moonshine-test-headless 4a6594e, gamestream-test-client e0a987a, moonshine-otel v1.4.0

Report card

Metric test baseline Target
Decode errors 🟢 0 🟢 0 ≤ 0
Corrupt pictures 🟢 0 🟢 0 ≤ 0
Corrupt after recovery 🟢 0 🟢 0 ≤ 0
Lost frames 🟢 0 🟢 0 ≤ 0
Frame pacing p99 🟢 1.02 frames (8.5 ms) 🟢 1.03 frames (8.5 ms) ≤ 1.10 frames (9.2 ms)
Delivered frame rate 🟢 100.0% 🟢 100.0% ≥ 99.0%
Host latency p99 🟡 5.77 ms 🔴 8.32 ms ≤ 3.00 ms
Recovery time p95 🟡 4.04 frames (33.6 ms) 🟡 4.05 frames (33.7 ms) ≤ 3.00 frames (25.0 ms)
RFI recovery rate 🟡 66.7% 🟡 66.7% ≥ 95.0%
Input latency p50 no click test no click test ≤ 2.00 frames (16.7 ms)
HDR switch keyframes no HDR switch test no HDR switch test ≤ 0.70
FPS under GPU load no GPU-load test no GPU-load test ≥ 99.0%
Peak bitrate 🟢 101.9% 🟢 101.9% ≤ 110.0%

pixelforge caf6dfd vs main efa5780, in Moonshine 934c5c5 · 1920x1080 at 60 fps

Stream

  • Moonshine 934c5c5, 1920x1080 at 60 fps, 20 Mb/s, streams of 20 s each, route loopback, chaos drop one frame in 120
  • workloads (streams): framestamp-full (15), vkcube (12); 32283 frames decoded; clock sync ±0.02 ms
  • tools: moonshine-test-headless 4a6594e, gamestream-test-client e0a987a, moonshine-otel v1.4.0

Report card

Metric test baseline Target
Decode errors 🟢 0 🟢 0 ≤ 0
Corrupt pictures 🟢 0 🟢 0 ≤ 0
Corrupt after recovery 🟢 0 🟢 0 ≤ 0
Lost frames 🟢 0 🟢 0 ≤ 0
Frame pacing p99 🟢 1.01 frames (16.9 ms) 🟢 1.02 frames (17.0 ms) ≤ 1.10 frames (18.3 ms)
Delivered frame rate 🟢 100.0% 🟢 100.0% ≥ 99.0%
Host latency p99 🟢 2.39 ms 🟢 2.60 ms ≤ 3.00 ms
Recovery time p95 🟡 4.00 frames (66.7 ms) 🟡 4.01 frames (66.8 ms) ≤ 3.00 frames (50.0 ms)
RFI recovery rate 🟡 66.7% 🟡 66.7% ≥ 95.0%
Input latency p50 no click test no click test ≤ 2.00 frames (33.3 ms)
HDR switch keyframes no HDR switch test no HDR switch test ≤ 0.70
FPS under GPU load no GPU-load test no GPU-load test ≥ 99.0%
Peak bitrate 🟡 115.0% 🟡 115.0% ≤ 110.0%

Results produced by moonshine-test-headless. Results file attached.

To run the same test on your hardware:

podman container runlabel run ghcr.io/porkloin/moonshine-test-headless -- \
    --pixelforge porkloin@bitstream-host-cached --baseline --iterations 3 \
    --app vkcube --app framestamp-full --mode 4k120 --mode 1080p60

To view the attached results:

podman container runlabel run ghcr.io/porkloin/moonshine-test-headless:view \
    path/to/results-compact-no-traces.tar.gz

@porkloin

Copy link
Copy Markdown
Contributor Author

@porkloin
porkloin marked this pull request as ready for review November 19, 2026 20:50
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.

1 participant