GPU: sparse copies snapshot and copy back their tiles on several threads #18

Closed
github-bot wants to merge 1 commit from github-bot/gh-18 into main AGit
Owner

@⁠PikPakPik on GitHub · 2026-11-25 12:03 UTC · original
PikPakPik:perf-copies-paralleles → main · +130 −61 · 1 files · 2 commits · closed without merging

Each world frame snapshots about 620 MB of shader-written tiles before a submission and compares them after, all on the submitting thread. The diagnostics counters (sparse writes since) show about 160 GB of ranges declared a frame for those 620 MB of distinct tiles, of which about 65 MB differ. The sampling profile (VICTORY_PROFILE) put Shadow::writing at 20 % and Shadow::copy_back at 18 % of the submitting thread's samples, most of it memmove and one lock of the tiles' map per 64 KiB.

The change is in shadow.rs only. A range inside one already declared for the submission returns at once: its tiles' snapshots are taken, as or_insert_with left them before. The views of a range's tiles are found under one lock (tiles_of). New snapshots and the copy-back of a range's tiles run through host::in_parallel, one tile per item, so no two threads touch the same memory. What is copied back and how is unchanged (merge_gpu_writes_mut, watch::writing, write_mapped).

Measured on this machine (RX 9060 XT, Ryzen 7 9700X, Series X profile, diagnostics build, standing still in the world): copying back went from about 48 to 32 ms a frame, and Shadow::writing from 20 % to 8.5 % of the submitting thread's samples. cargo test -p victory-gpu: 109 passed, 4 ignored; none of them reaches this path (sparse returns None under cfg(test)), so it was checked in the world only.

The frame rate barely moves, from about 4.1 to 4.3 fps, and that comparison is not clean: the numbers before are from 4cbe9d7 with VICTORY_SKIP_PIPELINES, those after from 71652a2 without it, where the refresh copies grew from about 10 to 30 ms a frame. The GPU's ~95 ms a frame is untouched. in_parallel still creates its threads on each call, about 3 % of the samples; a pool would save that.

**@⁠PikPakPik** on GitHub · 2026-11-25 12:03 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/18) `PikPakPik:perf-copies-paralleles` → `main` · +130 −61 · 1 files · 2 commits · closed without merging **Each world frame snapshots about 620 MB of shader-written tiles before a submission and compares them after, all on the submitting thread.** The diagnostics counters (`sparse writes since`) show about 160 GB of ranges declared a frame for those 620 MB of distinct tiles, of which about 65 MB differ. The sampling profile (`VICTORY_PROFILE`) put `Shadow::writing` at 20 % and `Shadow::copy_back` at 18 % of the submitting thread's samples, most of it `memmove` and one lock of the tiles' map per 64 KiB. The change is in `shadow.rs` only. A range inside one already declared for the submission returns at once: its tiles' snapshots are taken, as `or_insert_with` left them before. The views of a range's tiles are found under one lock (`tiles_of`). New snapshots and the copy-back of a range's tiles run through `host::in_parallel`, one tile per item, so no two threads touch the same memory. What is copied back and how is unchanged (`merge_gpu_writes_mut`, `watch::writing`, `write_mapped`). Measured on this machine (RX 9060 XT, Ryzen 7 9700X, Series X profile, diagnostics build, standing still in the world): copying back went from about 48 to 32 ms a frame, and `Shadow::writing` from 20 % to 8.5 % of the submitting thread's samples. `cargo test -p victory-gpu`: 109 passed, 4 ignored; none of them reaches this path (`sparse` returns `None` under `cfg(test)`), so it was checked in the world only. **The frame rate barely moves, from about 4.1 to 4.3 fps, and that comparison is not clean:** the numbers before are from `4cbe9d7` with `VICTORY_SKIP_PIPELINES`, those after from `71652a2` without it, where the refresh copies grew from about 10 to 30 ms a frame. The GPU's ~95 ms a frame is untouched. `in_parallel` still creates its threads on each call, about 3 % of the samples; a pool would save that.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:33:13 +00:00
Author
Owner

@⁠Ekabusto on GitHub · 2026-11-25 12:47 UTC · original

Checked locally: builds, and cargo test -p victory-gpu --lib passes (109 passed, 4 ignored). As the description says, the tests do not reach this path. No conflicts with main or the other open PRs.

What I checked in the code:

  • Each work item is one tile's part of the range, so no two threads write the same view, snapshot or title bytes. The snapshot pointers stay valid because before stays locked for the whole parallel section and nothing is inserted into it meanwhile.
  • The helpers that now run on worker threads look safe there: watch::writing takes the global GUARDS mutex, and read_into/write_mapped recover from faults through the process-wide exception handler (or fall back to Read/WriteProcessMemory).
  • The covered early return in sparse_writing skips work only when an enclosing range is still pending in the same submission, so its snapshots are already taken.

One nit: clippy reports one new undocumented_unsafe_blocks warning at shadow.rs:2038. One // SAFETY: comment covers two unsafe blocks (host and old); the second needs its own.

Reviewed on Windows 11 (build 26300), Rust 1.98.1, each PR in its own worktree; no game data, not run in the game. AI-assisted (Claude Code).

**@⁠Ekabusto** on GitHub · 2026-11-25 12:47 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/18#issuecomment-7162863858) Checked locally: builds, and `cargo test -p victory-gpu --lib` passes (109 passed, 4 ignored). As the description says, the tests do not reach this path. No conflicts with `main` or the other open PRs. What I checked in the code: - Each work item is one tile's part of the range, so no two threads write the same view, snapshot or title bytes. The snapshot pointers stay valid because `before` stays locked for the whole parallel section and nothing is inserted into it meanwhile. - The helpers that now run on worker threads look safe there: `watch::writing` takes the global `GUARDS` mutex, and `read_into`/`write_mapped` recover from faults through the process-wide exception handler (or fall back to `Read/WriteProcessMemory`). - The `covered` early return in `sparse_writing` skips work only when an enclosing range is still pending in the same submission, so its snapshots are already taken. One nit: clippy reports one new `undocumented_unsafe_blocks` warning at `shadow.rs:2038`. One `// SAFETY:` comment covers two `unsafe` blocks (`host` and `old`); the second needs its own. Reviewed on Windows 11 (build 26300), Rust 1.98.1, each PR in its own worktree; no game data, not run in the game. AI-assisted (Claude Code).
Author
Owner

@⁠gut0to on GitHub · 2026-11-25 15:09 UTC · original

Could this snapshot/copy-back path get a small deterministic regression test? The current test configuration never reaches sparse shadows (as the PR notes), while this change adds parallel raw tile copies and pooled-buffer length updates. A test with multiple tiles, including one unchanged tile and one GPU-modified range, could check that only the modified bytes are copied back and no tile is lost or duplicated. AI-assisted review.

**@⁠gut0to** on GitHub · 2026-11-25 15:09 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/18#discussion_r4696906547) Could this snapshot/copy-back path get a small deterministic regression test? The current test configuration never reaches sparse shadows (as the PR notes), while this change adds parallel raw tile copies and pooled-buffer length updates. A test with multiple tiles, including one unchanged tile and one GPU-modified range, could check that only the modified bytes are copied back and no tile is lost or duplicated. AI-assisted review.
Author
Owner

@⁠gut0to on GitHub · 2026-11-25 15:09 UTC · original · reviewed

AI-assisted review: I checked the test gating and the new parallel copy-back path.

**@⁠gut0to** on GitHub · 2026-11-25 15:09 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/18#pullrequestreview-6060195276) · **reviewed** AI-assisted review: I checked the test gating and the new parallel copy-back path.
github-bot closed this pull request 2026-11-26 13:38:02 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.