Skip to content

GPU: Speed up shadow buffer writeback merging - #2054

Open
GaussianGuaicai wants to merge 1 commit into
Marvisak:mainfrom
GaussianGuaicai:gpu/shadow-writeback-merge
Open

GaussianGuaicai wants to merge 1 commit into
Marvisak:mainfrom
GaussianGuaicai:gpu/shadow-writeback-merge

Conversation

@GaussianGuaicai

Copy link
Copy Markdown

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 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@RKouchoo

Copy link
Copy Markdown

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.

zhaibingye added a commit to zhaibingye/VIctory that referenced this pull request Nov 25, 2026
Keep bytewise guest/GPU reconciliation semantics unchanged.

Verified the source oracle with exact extracted code, measured sparse and dense synthetic merges, and cross-checked all workspace tests/features for Windows. Native source GPU test results report the same single unrelated failure on their baseline.
@Dathinel

Copy link
Copy Markdown
Contributor

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

upipibi commented Nov 25, 2026

Copy link
Copy Markdown

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).

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Owner

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

@Xvania7 Xvania7 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants