GPU: Speed up shadow buffer writeback merging #2052

Merged
GaussianGuaicai merged 1 commit from upstream-pr/2054 into main 2026-11-25 21:53:50 +00:00
GaussianGuaicai commented 2026-11-25 04:11:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by GaussianGuaicai on 2026-11-25T04:11:21Z.


Share a branch-free byte merge between the two shadow-buffer writeback paths so the compiler can vectorize the loop. The GPU byte still wins only when it differs from the saved byte; otherwise the guest byte is preserved. The helper reports whether guest memory needs to be written, as before.

Validation

  • Added a bytewise equivalence test for sparse and dense updates, conflicting writes, and short or unaligned lengths. It passed locally.
  • cargo test --locked -p victory-gpu --lib --features diagnostics: 146 passed, 1 failed, 2 ignored. Unmodified base 940d32f: 145 passed, 1 failed, 2 ignored. Both failed the same assertion in draw::tests::a_texture_written_in_one_format_reads_in_another_through_a_placed_view.
  • git diff --check passed.

An earlier diagnostic comparison measured merge cost of 0.392 vs. 0.088 ms/MB. The workloads differed; this measures only the merge step and does not establish an end-to-end frame-time improvement.

Prepared with assistance from GitHub Copilot.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5485026068 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2054). Originally posted by [GaussianGuaicai](https://github.com/GaussianGuaicai) on 2026-11-25T04:11:21Z. --- Share a branch-free byte merge between the two shadow-buffer writeback paths so the compiler can vectorize the loop. The GPU byte still wins only when it differs from the saved byte; otherwise the guest byte is preserved. The helper reports whether guest memory needs to be written, as before. ### Validation - Added a bytewise equivalence test for sparse and dense updates, conflicting writes, and short or unaligned lengths. It passed locally. - `cargo test --locked -p victory-gpu --lib --features diagnostics`: 146 passed, 1 failed, 2 ignored. Unmodified base `940d32f`: 145 passed, 1 failed, 2 ignored. Both failed the same assertion in `draw::tests::a_texture_written_in_one_format_reads_in_another_through_a_placed_view`. - `git diff --check` passed. An earlier diagnostic comparison measured merge cost of 0.392 vs. 0.088 ms/MB. The workloads differed; this measures only the merge step and does not establish an end-to-end frame-time improvement. Prepared with assistance from GitHub Copilot.
catminou4 commented 2026-11-25 04:17:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T04:17:32Z.

Review: COMMENTED


Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

Shadow buffer writeback merging sped up — perf work on the writeback path; benchmark evidence would help but the change is plausible.

<!-- greyfinch-sync:Marvisak/VIctory:review:6051789351 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2054#pullrequestreview-6051789351). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T04:17:32Z. **Review:** COMMENTED --- Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff. Shadow buffer writeback merging sped up — perf work on the writeback path; benchmark evidence would help but the change is plausible.
DenRakEiw commented 2026-11-25 04:50:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T04:50:00Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-gpu --lib shadow:: — 5 passed, 0 failed (including writeback_merge_matches_bytewise_oracle)
  • cargo clippy -p victory-gpu --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean fast-path shadow-buffer writeback merging verified against a bytewise oracle. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6052017690 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2054#pullrequestreview-6052017690). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T04:50:00Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test -p victory-gpu --lib shadow::` — 5 passed, 0 failed (including `writeback_merge_matches_bytewise_oracle`) - `cargo clippy -p victory-gpu --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean fast-path shadow-buffer writeback merging verified against a bytewise oracle. Looks good. AI-assisted (Gemini 4 Neon).
RKouchoo commented 2026-11-25 05:06:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RKouchoo on 2026-11-25T04:19:04Z.


On 7800X3D, #2054 cut a 4 MiB writeback merge from 4.22 ms to 0.34 ms when about one byte in 64 changed. In a separate replay of Story’s buffer-copy workload, #2051 cut the median from 54.1 ms to 30.5 ms.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7152732431 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2054#issuecomment-7152732431). Originally posted by [RKouchoo](https://github.com/RKouchoo) on 2026-11-25T04:19:04Z. --- On 7800X3D, #2054 cut a 4 MiB writeback merge from 4.22 ms to 0.34 ms when about one byte in 64 changed. In a separate replay of Story’s buffer-copy workload, #2051 cut the median from 54.1 ms to 30.5 ms.
Dathinel commented 2026-11-25 05:06:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dathinel on 2026-11-25T04:33:21Z.


The 64 KiB piece size used in shadow.rs shows a clear gain on a Ryzen 7 7435HS. The direction reverses for very short, sparsely changed spans. I compared the merge loop from 940d32f6 with merge_writeback from 857f630 on identical synthetic buffers.

Span length Host bytes changed from snapshot Base, ms per MiB PR, ms per MiB
64 KiB ~1/64 0.797–0.886 0.076–0.082
64 KiB ~1/2 4.347–4.490 0.077–0.085
4 bytes ~1/64 0.997–1.087 1.726–1.819

Each row processes 1 MiB total, split into equal spans. Windows/MSVC, rustc 1.98.1 -O, standalone CPU harness with direct calls. Each range is the min/max of three process medians (30 paired samples per process, alternating order). Buffer resets were outside the timer. Both output buffers and the count of changed spans matched.

The four-byte row deliberately calls the merge on every span, including unchanged ones. The saved-page path does that after clipping; the other caller first checks host != old. Actual clipping frequency, GPU readback and frame time were outside this measurement. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7152950167 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2054#issuecomment-7152950167). Originally posted by [Dathinel](https://github.com/Dathinel) on 2026-11-25T04:33:21Z. --- The 64 KiB piece size used in `shadow.rs` shows a clear gain on a Ryzen 7 7435HS. The direction reverses for very short, sparsely changed spans. I compared the merge loop from `940d32f6` with `merge_writeback` from `857f630` on identical synthetic buffers. | Span length | Host bytes changed from snapshot | Base, ms per MiB | PR, ms per MiB | |---|---:|---:|---:| | 64 KiB | ~1/64 | 0.797–0.886 | 0.076–0.082 | | 64 KiB | ~1/2 | 4.347–4.490 | 0.077–0.085 | | 4 bytes | ~1/64 | 0.997–1.087 | 1.726–1.819 | Each row processes 1 MiB total, split into equal spans. Windows/MSVC, rustc 1.98.1 `-O`, standalone CPU harness with direct calls. Each range is the min/max of three process medians (30 paired samples per process, alternating order). Buffer resets were outside the timer. Both output buffers and the count of changed spans matched. The four-byte row deliberately calls the merge on every span, including unchanged ones. The saved-page path does that after clipping; the other caller first checks `host != old`. Actual clipping frequency, GPU readback and frame time were outside this measurement. AI-assisted.
upipibi commented 2026-11-25 05:06:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-25T04:41:41Z.


Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at 857f630. victory-gpu: 142 passed, 2 failed, 2 ignored; the new writeback_merge_matches_bytewise_oracle passes. The 2 failures are #1744's heap-cap failures on main. Same as main on this hardware. Not run: a title.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7153079221 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2054#issuecomment-7153079221). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-25T04:41:41Z. --- Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at `857f630`. `victory-gpu`: 142 passed, 2 failed, 2 ignored; the new `writeback_merge_matches_bytewise_oracle` passes. The 2 failures are #1744's heap-cap failures on `main`. Same as `main` on this hardware. Not run: a title. AI-assisted (Claude Code).
Xvania7 commented 2026-11-25 09:49:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Xvania7 on 2026-11-25T09:49:21Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, head 857f630, Windows 10 x64 19045, NVIDIA GeForce RTX 5060, Rust 1.98.1 MSVC):

  • cargo test -p victory-gpu --lib -- --test-threads=1 shadow::tests:: — 4 passed, 0 failed
  • cargo test -p victory-gpu --lib -- --test-threads=1 host::tests:: — all 6 host::tests::* passed (1 unrelated baseline failure on NVIDIA in descriptor::host::tests::dynamic_slots_are_held_until_released due to #1835)
  • cargo clippy -p victory-gpu --all-targets -- -D warnings — 0 warnings
  • cargo xtask policy — 182 files ok

Clean optimization to shadow buffer writeback merging.

<!-- greyfinch-sync:Marvisak/VIctory:review:6055265310 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2054#pullrequestreview-6055265310). Originally posted by [Xvania7](https://github.com/Xvania7) on 2026-11-25T09:49:21Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, head `857f630`, Windows 10 x64 19045, NVIDIA GeForce RTX 5060, Rust 1.98.1 MSVC): - `cargo test -p victory-gpu --lib -- --test-threads=1 shadow::tests::` — 4 passed, 0 failed - `cargo test -p victory-gpu --lib -- --test-threads=1 host::tests::` — all 6 `host::tests::*` passed (1 unrelated baseline failure on NVIDIA in `descriptor::host::tests::dynamic_slots_are_held_until_released` due to #1835) - `cargo clippy -p victory-gpu --all-targets -- -D warnings` — 0 warnings - `cargo xtask policy` — 182 files ok Clean optimization to shadow buffer writeback merging.
Marvisak commented 2026-11-25 10:07:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T09:23:08Z.


Automated triage for the maintainers.
Merge candidate.

In crates/victory-gpu/src/shadow.rs, the two copies of the byte-merge loop (in copy_back_or_not and the saved-page path) become one branch-free merge_writeback. I checked that it does the same thing as the old loop. Where the host byte differs from the snapshot, the title byte takes the host byte; the copy then takes the title byte; it returns true when any title byte changed. The oracle test compares against that rule written out byte by byte, not against the new code. Two people measured it in the thread: about 10x faster on 64 KiB pieces, a little slower on 4-byte spans on the saved-page path.

Overlap: #1867 does the same merge with AVX2, unsafe and an added doc file. This PR is the simpler, portable version of that. #1986 (also marked merge candidate) restructures the same loop in copy_back_or_not to skip withheld ranges, and #1998/#2242 rewrite the copy-back too. Whichever of those lands second will need a rebase. Optional nit: the new test could go in the existing #[cfg(test)] mod tests at the end of the file.

CI: passing (check).

Recommending a look, not approving.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158258080 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2054#issuecomment-7158258080). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T09:23:08Z. --- <!-- victory-triage v1 head=857f630861ab350ca45fadb1ba76ddb5c4aeb24e verdict=merge-candidate at=2026-11-25T09:23:01Z --> Automated triage for the maintainers. **Merge candidate.** In `crates/victory-gpu/src/shadow.rs`, the two copies of the byte-merge loop (in `copy_back_or_not` and the saved-page path) become one branch-free `merge_writeback`. I checked that it does the same thing as the old loop. Where the host byte differs from the snapshot, the title byte takes the host byte; the copy then takes the title byte; it returns true when any title byte changed. The oracle test compares against that rule written out byte by byte, not against the new code. Two people measured it in the thread: about 10x faster on 64 KiB pieces, a little slower on 4-byte spans on the saved-page path. Overlap: #1867 does the same merge with AVX2, `unsafe` and an added doc file. This PR is the simpler, portable version of that. #1986 (also marked merge candidate) restructures the same loop in `copy_back_or_not` to skip withheld ranges, and #1998/#2242 rewrite the copy-back too. Whichever of those lands second will need a rebase. Optional nit: the new test could go in the existing `#[cfg(test)] mod tests` at the end of the file. CI: passing (`check`). Recommending a look, not approving. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-25 22:03:23 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!2052
No description provided.