Skip to content

perf(gpu): stop recopying overlapping write snapshots - #436

Merged
pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
mikebuckets171:contrib/mac-shadow-snapshot-spans
Nov 27, 2026
Merged

pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
mikebuckets171:contrib/mac-shadow-snapshot-spans

Conversation

@mikebuckets171

@mikebuckets171 mikebuckets171 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Port of Marvisak/VIctory#3211 by Yumaste (d5128bce, ebd01cdd). Based on macOS bf99e728.

Why this matters on the Mac

In a retained M5 Pro Series X opening, the per-shadow diagnostics attributed up to 2,016 MB/frame of snapshot copying to one 64 MiB buffer. A separate 720p run reached 1,035 MB/frame on a buffer of the same size. Those are reporting-interval averages across submissions, not evidence that every copy was redundant. The specific waste we can reproduce is that declaring a write overlapping an earlier one copies the previous snapshot into a new buffer each time the covered range grows.

Change

Keep the bytes in capture order in one pooled buffer. Sorted spans map shadow offsets to those bytes. A declaration adds only missing bytes. Refreshes replace only the captured bytes they intersect. Copy-back compares a contiguous piece directly; it gathers the old bytes of a fragmented piece only if they changed. It still compares each whole 64 KiB piece only once and merges concurrent title writes.

The Mac adaptation uses the port’s sorted, age-trimmed pool and preserves its memory cap, widened-view updates, and tracked/untracked refresh paths. SnapshotBytes includes both newly saved bytes and saved bytes moved on pool growth so a reduction is not just a change in the definition of the counter.

Synthetic measurement

The first commit is a standalone ignored benchmark that also applies to bf99e728. The time covers Shadow::writing on a mapped D3D12 buffer, not command execution or copy-back. Each iteration changes a byte in the shadow and checks it in title memory after copy-back, outside the timer. M5 Pro / 48 GB, macOS 27.2.1, x86-64 release under Rosetta, private Wine 11.0 with #211, D3DMetal 4.0b3. Median of 9 warm captures after 2 warm-ups per case:

Declarations on a 64 MiB shadow Base Port
One 64 MiB range 1.472 ms 1.507 ms
17 overlapping ranges expanding from 32 to 64 MiB 18.686 ms 1.530 ms
Two disjoint 16 MiB ranges 0.710 ms 0.712 ms

The growing case is 12.2× faster in capture. The other two controls stayed close. To reproduce, on each commit run tools/macos/test-wine.sh --release -p victory-gpu --lib --locked -- --ignored --exact shadow::snapshot_bench::growing_write_ranges --test-threads=1 --nocapture.

A separate instrumented run in base → port → port → base order read SnapshotBytes around the same calls. On every warm iteration, the growing case copied 816 MiB → 64 MiB (92.2% less). One range copied 64 MiB on both, and the two disjoint ranges copied 32 MiB on both. The growing medians in that order were 17.855, 1.471, 1.514 and 17.238 ms. These are measured bytes, including moves during growth.

Tradeoff: a separate deliberately fragmented case declared 512 out-of-order 512-byte stripes across four pages, refreshed part of the buffer halfway through capture, then interleaved synthetic CPU and GPU writes. An independent per-byte model matched all 2,883,584 final bytes checked per tree run (11 submissions). Copy-back of the four changed pages alone took 31.5/31.8 µs on base versus 59.1/69.3 µs on the port. Gathering scattered originals costs time in that shape; the 12.2× result applies only to growing-range capture.

Title smoke: the port reached the changing Series X opening at 720p with the same recorded settings as the control. The visibility watchdog stopped the run at 217 s, after only 8 of the preselected 60 reporting intervals. No comparable frame-rate result or gameplay claim.

Validation

  • Standard native workspace: base and head 555 passed, 0 failed, 17 ignored. Standard Windows-target workspace under Wine/D3DMetal, serial: base 1,124/0/28, head 1,126/0/29 (passed/failed/ignored; the new timing fixture is intentionally ignored).
  • Release-mode Wine shadow:: tests: 34 passed, 0 failed, 4 ignored; the timing fixture then passed explicitly. Tests exercise overlapping and fragmented snapshots, copy-back across fragments, the actual copied-byte counter, and preserving a later CPU write after an untracked refresh crosses a fragment.
  • Native Clippy and both strict Windows-target workspace Clippy configurations passed, as did asset policy and git diff --check. cargo fmt --all -- --check flags the same 81 existing files on base and head; both new Rust files pass rustfmt.
  • As a separate release-profile baseline check, unmodified bf99e728 has two native test failures (UAV clear slot addresses and installation progress unregister) and one under Wine (UAV clear slots); the standard suites above use their documented debug profile. No full-release-suite pass is claimed.

Prepared and tested with GPT-6.2 Astra. No game data, captures or Apple components are included.

mikebuckets171 and others added 2 commits November 26, 2026 15:02
Port Yumaste’s Marvisak/VIctory#3211 (d5128bce, ebd01cdd) to the Mac tree. Keep the port’s recent-buffer pool, widened views and untracked refresh path. Count moves on growth as snapshot traffic. A 64 MiB growing-range capture on M5 Pro falls from 18.686 to 1.530 ms with the same copy-back result.

Co-authored-by: Yumaste <yumaste@outlook.fr>
@Nick233x

Copy link
Copy Markdown
Contributor

Independent run of the benchmark and the suites on an M5 Pro. MacBook Pro, Apple M5 Pro, 24 GB, macOS 26.6.2, Game Porting Toolkit 4.0 beta 3 (D3DMetal), Wine built by setup.sh (x86-64 through Rosetta). Synthetic only, no game files.

shadow::snapshot_bench::growing_write_ranges, --release, your command, run in base → port → port → base order (base = 512c379, the benchmark alone; port = 885d997). Medians of 9 warm captures:

Declarations on a 64 MiB shadow Base Port Port Base
One 64 MiB range 1.472 ms 1.344 ms 1.461 ms 1.327 ms
17 overlapping ranges, 32 → 64 MiB 17.343 ms 1.389 ms 1.372 ms 17.336 ms
Two disjoint 16 MiB ranges 0.655 ms 0.678 ms 0.720 ms 0.663 ms

The growing case is 12.6× faster here, in line with your 12.2× on macOS 27.2.1. The two controls stay within run-to-run spread, though the disjoint case read 3–9% slower in both port runs.

Suites at 885d997, default threads Result
victory-gpu --lib + victory-hle --lib 364 + 162 passed, 0 failed
victory-gpu --lib, 30 runs 30 of 30 passed (main bf99e72: 60 of 60 on the same Mac tonight)

AI-assisted, prepared with Claude Code.

@earpjonathan

earpjonathan commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Correction: this PR is not the cause. More runs show the dark first indoor area and grey, ashen skin come and go with the host pipeline library. The dark runs, with or without this PR, each had 35 to 42 frames that dropped work while about 2,000 pipelines were still being made after the library was replaced. The lit runs had 1 to 3. Switching between builds is what emptied the library in my comparison, so the earlier result here was an artifact of how I tested, and I'm sorry for the noise.

The likely mechanism: one-time setup work at the start of the scene, probably the lighting, gets dropped because its pipeline isn't ready yet, and the scene stays wrong afterwards. I'll test this PR again with a warm library.

Prepared with Claude Code.


Original comment, kept for the record:

In-game check on an M5 Pro (macOS 27.0.1, CrossOver 26.3 Wine with the repo's current patches, D3DMetal 4.0 beta 3, Series X). Runs with the same opening and the same scripted input, on main at bf99e72 plus #401, #435, #433, #439 and #419, the second run with this PR added as well.

The darkness points at write snapshots that come out incomplete. One guess: data written by the GPU into a range that an earlier snapshot already covers, which then never goes back to title memory. If a debug switch or more logging would help, I can run it again.

Prepared with Claude Code.

@mikebuckets171

Copy link
Copy Markdown
Contributor Author

@earpjonathan Thanks for running the control again. The lights returning without #436 is a correctness concern; I would hold this port until we understand it. Nick’s independent benchmark confirms the capture speedup, but neither that benchmark nor our existing byte fixtures tests this lighting pass. I’m not using the removed timing comparison.

I’ve started a local control on bf99e72 + #401 a436cc9, #435 9a4652e, #433 20feae0, #439 f80b128, and #419 c084675; the comparison adds only #436 885d997. I’ll compare the same indoor pose at two times a minute apart, using the ceiling fixtures and illuminated wall patches as the checks. Fast heap sync and queue coalescing are on. If your pinned revisions or switches differ, those details would help; no additional run or game captures are needed from you yet.

The old capture code also retained previously captured bytes when a declaration grew, so that fact alone doesn’t explain a difference. I’ll check the fragmented baseline, refresh and copy-back against the old path before proposing a fix.

Prepared by GPT-6.2 Astra.

@mikebuckets171

Copy link
Copy Markdown
Contributor Author

On this M5 Pro (48 GB, macOS 27.2.1, Wine 11.0, mapped D3DMetal 4.0 beta 3), I compared the first indoor room with and without #436. I used bf99e728 + #401 a436cc9, #435 9a4652e, #433 20feae0, #439 f80b128, #419 c084675. The second build adds only #436 885d997; neither includes #416, #437 or #438.

Both runs used the same Series X profile and retained caches, 720p, fast heap sync and queue coalescing on, fault tracking and diagnostics on. I checked the release binary against its source tree, checked the mapped graphics runtime, and compared the recorded launch settings. From the initial window I used the same bounded camera and movement holds to frame the blue door and overhead lights. This is a comparison of that room, not a frame-time benchmark.

I did not reproduce the missing lights in this pair. All three overhead fixtures and the illuminated wall beside the blue door were visible at both observations on both builds. With no input between observations, the captures were 64 seconds apart on the control and 70 seconds apart with #436. The character remained in shadow in both.

For a check beyond visual judgment, I selected three small patches on the first control capture and used the same normalized coordinates in all four captures after checking landmark alignment. Median 8-bit display luma (0–255; not linear scene lighting):

Patch Control first / later With #436 first / later
Middle ceiling fixture 241 / 249 243 / 248
Wall immediately right of door 148 / 146 148 / 146
Lower blue door panel 113 / 111 112 / 110

This local pair and the reporter's lit → dark → lit sequence differ. I would keep the report open rather than infer either a general lighting fix or an incomplete snapshot from the timings. Matching the reporter's exact combined revision, nondefault switches, and camera landmark is more useful than another unchanged run here. No game images or raw logs are needed.

Prepared and tested by GPT-6.2 Astra.

@pragyaangaur
pragyaangaur merged commit 82587a2 into pragyaangaur:main Nov 27, 2026
1 check passed
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.

4 participants