Batch contiguous GPU readback of guest memory #247

Manually merged
github merged 4 commits from github-bot/gh-247 into main 2026-12-03 17:20:15 +00:00 AGit
Owner

@⁠tommiehansen on GitHub · 2026-12-02 20:55 UTC · original
tommiehansen:perf/batch-card-readback → main · +156 −22 · 2 files · 4 commits · merged 2026-12-03 17:15 UTC

Problem

The GPU readback path submitted a separate copy and completed a fence for every 4 MiB of title memory read by the host. A streaming burst can therefore serialize texture preparation with many CPU-to-GPU round trips. Existing diagnostics in pull.rs document 20–60 MiB of card-memory reads during frame spikes, although these reads may comprise several textures and the reported spike includes other costs.

Change and correctness

Use one contiguous copy up to 16 MiB when a single existing mirror covers the whole group. If growth fails, the extra allocation budget is exhausted, or the larger interval is unavailable, the original 4 MiB progression stays available. A 128 MiB process-wide budget bounds additional readback capacity. The original source interval, destination byte order, copy-queue synchronization and completion-before-CPU-read rule are preserved. Timeout allowance scales with bytes, and source and destination retain their lifetimes when completion cannot be proved. Set VICTORY_CARD_PULL_BATCH=0 to compare with the original grouping.

A contiguous 20 MiB read uses two submissions instead of five. A 60 MiB read uses four instead of fifteen. These are command counts, not hardware timings. This helps configurations that run GPU readback; the CPU BAR path is outside the change.

Quantified improvements — submission counts

For contiguous reads covered by a single mirror, when enlarged staging capacity is available:

Read size Original 4 MiB grouping Up to 16 MiB grouping Fewer submissions
20 MiB 5 2 60.0%
60 MiB 15 4 73.3%

These are copy submission counts, not reductions in readback bytes, GPU time or whole-frame time. The larger grouping avoids intermediate completion waits; the final completion-before-read requirement is unchanged.

Upstream #256 reports an RX 7900 XTX workload with CPU card reads at 42–44 MB/s and GPU copies at 75 MB/s, and texture-refresh spikes of 250–820 ms after the previous automatic policy selected CPU reads. Its fence-polling changes reported:

Readback size Before #256 After #256
32 KiB 0.12 ms 0.05 ms
128 KiB 0.24 ms 0.19 ms
512 KiB 0.20 ms 0.10 ms
≥4 MiB Unchanged Unchanged

This demonstrates why synchronization overhead matters, but is a different optimization. #256 also reports that automatic GPU copying still disables itself on an RX 9070 XT configuration with faster CPU reads. This PR therefore has no direct benefit where the CPU BAR path is selected.

Why this should be merged

Where GPU readback is active, fewer intermediate submissions and fences can reduce CPU/GPU round trips during large reads. The patch preserves the updated fence-wait policy, bounds extra staging capacity and retains the original progression when batching is unavailable. Test together with #248 (which can remove certain unnecessary reads entirely), measuring remaining readback traffic and frame-time tails rather than assuming additive gains.

Validation

Windows-target cargo check passed with and without diagnostics and with test targets enabled, using a check-only mimalloc override and pure Rust BLAKE3 on macOS. A portable differential test exercised 8,192 randomized source intervals, three enlarged capacities and original mirror-boundary behavior. git diff --check passed. Hardware benchmarking was unavailable.

Please compare both grouping settings on RX 9060 XT 16 GB at native 1920×1080 and RX 9070 XT at native 2560×1440, with RT enabled, identical settings and no frame generation. Where GPU readback is active, capture readback time, copy-queue occupancy, median and P99 frame time and image parity on a reproducible streaming route. The existing ignored GPU readback test also checks returned bytes against the original data.

**@⁠tommiehansen** on GitHub · 2026-12-02 20:55 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/247) `tommiehansen:perf/batch-card-readback` → `main` · +156 −22 · 2 files · 4 commits · merged 2026-12-03 17:15 UTC ## Problem The GPU readback path submitted a separate copy and completed a fence for every 4 MiB of title memory read by the host. A streaming burst can therefore serialize texture preparation with many CPU-to-GPU round trips. Existing diagnostics in `pull.rs` document 20–60 MiB of card-memory reads during frame spikes, although these reads may comprise several textures and the reported spike includes other costs. ## Change and correctness Use one contiguous copy up to 16 MiB when a single existing mirror covers the whole group. If growth fails, the extra allocation budget is exhausted, or the larger interval is unavailable, the original 4 MiB progression stays available. A 128 MiB process-wide budget bounds additional readback capacity. The original source interval, destination byte order, copy-queue synchronization and completion-before-CPU-read rule are preserved. Timeout allowance scales with bytes, and source and destination retain their lifetimes when completion cannot be proved. Set `VICTORY_CARD_PULL_BATCH=0` to compare with the original grouping. A contiguous 20 MiB read uses two submissions instead of five. A 60 MiB read uses four instead of fifteen. These are command counts, not hardware timings. This helps configurations that run GPU readback; the CPU BAR path is outside the change. ## Quantified improvements — submission counts For contiguous reads covered by a single mirror, when enlarged staging capacity is available: | Read size | Original 4 MiB grouping | Up to 16 MiB grouping | Fewer submissions | |---:|---:|---:|---:| | 20 MiB | 5 | 2 | **60.0%** | | 60 MiB | 15 | 4 | **73.3%** | These are copy submission counts, not reductions in readback bytes, GPU time or whole-frame time. The larger grouping avoids intermediate completion waits; the final completion-before-read requirement is unchanged. ## Evidence from related implementations [Upstream #256](https://git.gtahub.st/github/lobomfz--VIctory-xmem/pulls/256) reports an RX 7900 XTX workload with CPU card reads at 42–44 MB/s and GPU copies at 75 MB/s, and texture-refresh spikes of 250–820 ms after the previous automatic policy selected CPU reads. Its fence-polling changes reported: | Readback size | Before #256 | After #256 | |---:|---:|---:| | 32 KiB | 0.12 ms | 0.05 ms | | 128 KiB | 0.24 ms | 0.19 ms | | 512 KiB | 0.20 ms | 0.10 ms | | ≥4 MiB | Unchanged | Unchanged | This demonstrates why synchronization overhead matters, but is a different optimization. #256 also reports that automatic GPU copying still disables itself on an RX 9070 XT configuration with faster CPU reads. This PR therefore has no direct benefit where the CPU BAR path is selected. ## Why this should be merged Where GPU readback is active, fewer intermediate submissions and fences can reduce CPU/GPU round trips during large reads. The patch preserves the updated fence-wait policy, bounds extra staging capacity and retains the original progression when batching is unavailable. Test together with #248 (which can remove certain unnecessary reads entirely), measuring remaining readback traffic and frame-time tails rather than assuming additive gains. ## Validation Windows-target `cargo check` passed with and without diagnostics and with test targets enabled, using a check-only mimalloc override and pure Rust BLAKE3 on macOS. A portable differential test exercised 8,192 randomized source intervals, three enlarged capacities and original mirror-boundary behavior. `git diff --check` passed. Hardware benchmarking was unavailable. Please compare both grouping settings on RX 9060 XT 16 GB at native 1920×1080 and RX 9070 XT at native 2560×1440, with RT enabled, identical settings and no frame generation. Where GPU readback is active, capture readback time, copy-queue occupancy, median and P99 frame time and image parity on a reproducible streaming route. The existing ignored GPU readback test also checks returned bytes against the original data.
Reading a large device-local title range issued a copy and waited on a fence every 4 MiB. Grow the thread-local readback buffer when useful and use one copy for up to 16 MiB when one mirror covers it. This removes repeated submissions, host waits and command allocator resets without changing the bytes read.

Keep the original piece size for small reads, failed growth, and ranges a larger mirror lookup cannot cover. Bound extra readback storage, retain the source and destination on uncertain completion, and provide VICTORY_CARD_PULL_BATCH=0 for comparing with the original copy size.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 20:56:57 +00:00
Keep the upstream README layout and move this branch’s setting to the corresponding documentation page.
Remove the duplicate partial wait left by the upstream merge. Retain the short completion poll and switch to the event with the length-scaled timeout for a batched copy. Leave failed and uncertain submission handling intact.

The standard and diagnostics Windows-target checks and the portable 8192-case readback planner differential test passed. The D3D12 readback test requires hardware.
github manually merged commit 4aa5e9c2cf into main 2026-12-03 17:20:15 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.