umem: skip sparse GPU binds and lookup records for CPU-only host_range views #186

Open
github-bot wants to merge 1 commit from github-bot/gh-186 into main AGit
Owner

@⁠Mhyster on GitHub · 2026-12-02 06:21 UTC · original
Mhyster:opt/umem-skip-host-range-gpu-binds → main · +130 −54 · 3 files · 1 commits · open

What

Overlap with open PRs: #168 (umem: run on NVIDIA's driver) and #178 (umem: driver.rs) touch greenfield/umem/src/lib.rs (device setup) and 3 lines of greenfield/umem/src/mapping.rs; this change touches Mapping, insert_mapping, carve, set_runs_before, and map_pieces.

Addresses docs/TASKS.md ("Small items": "About 9 % of sparse binds go to a CPU-only pool window the GPU never reads", docs/learnings/known-wrong.md "Pages above 2^40"):

  • Adds gpu: bool to umem::Mapping and State::mapping_uses_gpu(start, len) (!self.in_host(start, len)).
  • When map_pieces enters a pool view inside a declared Memory::host_range (victory-hle's CPU-only pool write window above 2^41, which vkback-unix/umem-unix already register via m.host_range when own = true), insert_mapping maps it with gpu = false:
    • Skips creating an unused sparse Vulkan buffer window above 2^41 (self.window).
    • Skips inserting records into the lock-free GPU lookup tree (self.lookup.insert).
    • Skips queueing vkQueueBindSparse binds (self.queue_bind) and timeline events.
    • Skips set_runs_before quadratic run-rewriting walks across 64 KiB pool_window pages.
    • Keeps the mapping in st.maps and st.blocks[block].mappings so pool_release, destroy_pool, unview, protect, read_backing, check_sample (stray-write detection against DirectStorage writes), and Drop continue to track and unmap its CPU pages cleanly.
  • Fails loudly (umem::loud::fail + Error::Invalid) if a caller ever requests a GPU-only view (cpu == false) inside a declared host_range.
  • In carve, skips lookup.remove, timeline::unmapped, st.stale / queue_bind(null), and set_runs_before for gpu == false mappings, and preserves m.gpu on any carved remainders.
  • In set_runs_before and insert_mapping's run merge, ignores gpu == false mappings so GPU runs never merge across non-GPU aliases.

The reference

No D3D12 / vkd3d-proton counterpart (this is our own Linux umem pool-window mechanism, documented in docs/learnings/known-wrong.md "Pages above 2^40" and crates/victory-hle/src/modules/umem.rs pool_window / reserve_window).

Evidence

Checks run (all passed on Linux x86_64)

  • Unit test in greenfield/umem/src/lib.rs (host_range_tests::host_ranges_merge_and_cover_only_what_was_declared): verifies State::mapping_uses_gpu returns false inside a declared host_range and true for title addresses.
  • Added GPU integration test host_range_pool_view_skips_gpu_binds_and_lookup in greenfield/umem/tests/gpu.rs (compiled and clippy-checked; not executed on GPU in this session).
  • cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked: passed (victory-core 13, victory-formats 49 passed / 5 ignored, victory-shader 240 passed / 2 ignored, xtask 6).
  • (cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked): passed (frame 13, umem 22, vkback 168 passed / 3 ignored, policy 6/6).
  • cargo clippy --all-targets --locked -- -D warnings: passed cleanly across root (-p victory-shader -p victory-formats -p victory-core -p xtask), greenfield, greenfield/wine, and greenfield/vkback/translate.

Checks NOT run (live game / GPU unavailable in this session)

The host GPU was not touched in this session:

  • NOT run: cargo test -p umem --test gpu host_range_pool_view_skips_gpu_binds_and_lookup (requires GPU).
  • NOT run: A B B A boat-scene performance measurement with VKB_LAT + greenfield/tools/frec/frames.py.
  • NOT run: 60 s house-scene Vulkan validation run (docs/tools.md, "Vulkan validation run").
  • NOT run: Cross-compiled Windows PE clippy (cargo xwin clippy ..., verified by GitHub Actions CI).

Checklist (docs/AGENTS.md)

  • One topic (rule 31); docs/TASKS.md not edited, the fixed item named above (rule 29)
  • "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits)
  • No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9)
  • Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b)
  • Every switch is listed, read in one place, and a bad value stops the start (rule 2)
  • Queues and other externally synchronized Vulkan objects are used under their lock (rule 35)
  • Every unsafe block has a // SAFETY: comment; no raw pointer around the borrow checker (rule 36)
  • Clippy clean; tests call the production code; TRANSLATOR_VERSION bumped if the translator's output changes (rule 27)
  • Comments say what is true now and name functions, not line numbers (rule 3)
  • Nothing from the title, and no pointers to where non-free data can be found (hard constraints)
**@⁠Mhyster** on GitHub · 2026-12-02 06:21 UTC · [original](https://github.com/vktory6/VKtory/pull/186) `Mhyster:opt/umem-skip-host-range-gpu-binds` → `main` · +130 −54 · 3 files · 1 commits · open ## What Overlap with open PRs: `#168` (`umem: run on NVIDIA's driver`) and `#178` (`umem: driver.rs`) touch `greenfield/umem/src/lib.rs` (device setup) and 3 lines of `greenfield/umem/src/mapping.rs`; this change touches `Mapping`, `insert_mapping`, `carve`, `set_runs_before`, and `map_pieces`. Addresses `docs/TASKS.md` ("Small items": "About 9 % of sparse binds go to a CPU-only pool window the GPU never reads", `docs/learnings/known-wrong.md` "Pages above 2^40"): - Adds `gpu: bool` to `umem::Mapping` and `State::mapping_uses_gpu(start, len)` (`!self.in_host(start, len)`). - When `map_pieces` enters a pool view inside a declared `Memory::host_range` (victory-hle's CPU-only pool write window above 2^41, which `vkback-unix`/`umem-unix` already register via `m.host_range` when `own = true`), `insert_mapping` maps it with `gpu = false`: - Skips creating an unused sparse Vulkan buffer window above 2^41 (`self.window`). - Skips inserting records into the lock-free GPU lookup tree (`self.lookup.insert`). - Skips queueing `vkQueueBindSparse` binds (`self.queue_bind`) and timeline events. - Skips `set_runs_before` quadratic run-rewriting walks across 64 KiB `pool_window` pages. - Keeps the mapping in `st.maps` and `st.blocks[block].mappings` so `pool_release`, `destroy_pool`, `unview`, `protect`, `read_backing`, `check_sample` (stray-write detection against DirectStorage writes), and `Drop` continue to track and unmap its CPU pages cleanly. - Fails loudly (`umem::loud::fail` + `Error::Invalid`) if a caller ever requests a GPU-only view (`cpu == false`) inside a declared `host_range`. - In `carve`, skips `lookup.remove`, `timeline::unmapped`, `st.stale` / `queue_bind(null)`, and `set_runs_before` for `gpu == false` mappings, and preserves `m.gpu` on any carved remainders. - In `set_runs_before` and `insert_mapping`'s `run` merge, ignores `gpu == false` mappings so GPU runs never merge across non-GPU aliases. ## The reference No D3D12 / vkd3d-proton counterpart (this is our own Linux `umem` pool-window mechanism, documented in `docs/learnings/known-wrong.md` "Pages above 2^40" and `crates/victory-hle/src/modules/umem.rs` `pool_window` / `reserve_window`). ## Evidence ### Checks run (all passed on Linux x86_64) - Unit test in `greenfield/umem/src/lib.rs` (`host_range_tests::host_ranges_merge_and_cover_only_what_was_declared`): verifies `State::mapping_uses_gpu` returns `false` inside a declared `host_range` and `true` for title addresses. - Added GPU integration test `host_range_pool_view_skips_gpu_binds_and_lookup` in `greenfield/umem/tests/gpu.rs` (compiled and clippy-checked; not executed on GPU in this session). - `cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked`: passed (`victory-core` 13, `victory-formats` 49 passed / 5 ignored, `victory-shader` 240 passed / 2 ignored, `xtask` 6). - `(cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked)`: passed (`frame` 13, `umem` 22, `vkback` 168 passed / 3 ignored, `policy` 6/6). - `cargo clippy --all-targets --locked -- -D warnings`: passed cleanly across root (`-p victory-shader -p victory-formats -p victory-core -p xtask`), `greenfield`, `greenfield/wine`, and `greenfield/vkback/translate`. ### Checks NOT run (live game / GPU unavailable in this session) The host GPU was not touched in this session: - **NOT run**: `cargo test -p umem --test gpu host_range_pool_view_skips_gpu_binds_and_lookup` (requires GPU). - **NOT run**: A B B A boat-scene performance measurement with `VKB_LAT` + `greenfield/tools/frec/frames.py`. - **NOT run**: 60 s house-scene Vulkan validation run (`docs/tools.md`, "Vulkan validation run"). - **NOT run**: Cross-compiled Windows PE clippy (`cargo xwin clippy ...`, verified by GitHub Actions CI). ## Checklist (docs/AGENTS.md) - [x] One topic (rule 31); `docs/TASKS.md` not edited, the fixed item named above (rule 29) - [x] "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits) - [x] No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9) - [x] Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b) - [x] Every switch is listed, read in one place, and a bad value stops the start (rule 2) - [x] Queues and other externally synchronized Vulkan objects are used under their lock (rule 35) - [x] Every `unsafe` block has a `// SAFETY:` comment; no raw pointer around the borrow checker (rule 36) - [x] Clippy clean; tests call the production code; `TRANSLATOR_VERSION` bumped if the translator's output changes (rule 27) - [x] Comments say what is true now and name functions, not line numbers (rule 3) - [x] Nothing from the title, and no pointers to where non-free data can be found (hard constraints)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 06:26:11 +00:00
Author
Owner

@⁠firefloc on GitHub · 2026-12-02 07:02 UTC · original

I tested #186 locally on Linux/RADV because a streaming trace showed a large cost in sparse bind submission and in set_runs_before. I also tried a separate change to the tree traversal so I could distinguish the two effects.

Setup: RX 7900 XTX (RADV NAVI31), Ryzen 7 7800X3D, 32 GiB RAM, CachyOS (kernel 7.3.6), KDE/Xwayland at 3440×1440. The fork base used upstream cc16c64. Each run started from a fresh clone of the same save, with the live guard filter from #141, VICTORY_VIEW_GUARD=1, valid BCPack tables, per-frame MangoHud logging, and VKB_LAT. The PE and tools were identical across builds. The scene was the load into the saved world, so these numbers do not characterize the standard boat route.

Exploratory passes, world seconds 1–80

Build FPS p50 / p90 / p99 / max (ms) Frames >100 ms Sparse-bind call time over run
Baseline 11.40 44 / 208 / 510 / 973 235 25.90 s
Baseline + #186 10.44 65 / 199 / 449 / 631 246 18.71 s
Baseline + #186 + traversal change 10.38 62 / 202 / 504 / 826 237 17.06 s

With #186 alone the number of sparse-bind flushes dropped from 142,857 to 129,088. Its p99 and max improved in this pass, while mean cadence and the count of frames over 100 ms went the other way. The game lock was held, but a local config overrode the build lock setting in this series. These were single passes and the sparse-bind totals are summed timings over the reported run segments, across threads. They are useful for attribution, not an FPS verdict.

Independent follow-up for set_runs_before

The extra change uses one descending BTreeMap::range_mut(..at).rev() traversal in place of a predecessor search and a second mutable lookup per changed entry. The stop conditions, published run lengths and Vulkan calls are unchanged. On top of #186, the measured time in that function went from 2.80 to 0.44 s with 64.23M and 64.53M run updates. In a separate pair with both the GPU and build locks confirmed and without #186 in either build, it went from 2.99 to 0.93 s (−69%) for 66.15M and 66.10M run updates. This is summed time in the function, not a per-frame saving. The standalone traversal change passed the 22 umem unit tests plus the GPU tests for span changes and GPU-only pool views. The host-range CPU/GPU alias coherence test also passed with #186 and the traversal change together.

In the locked pair, over world seconds 1–60, MangoHud recorded 7.92 → 4.51 FPS, 487 → 2,106 ms p99, and 206/467 → 167/266 frames over 100 ms. There was active swap in both runs, with spot samples above 100 MiB/s in both directions in the slower run. This pair does not establish a frame pacing benefit or attribute the slowdown to the traversal. Neither run recorded a remap applied ahead of its queue position or a read of pages mapped from the future. I did not run the in-game Vulkan validation or picture gate for #186.

The result worth retesting is the reduction in sparse-bind work from #186 and the separate reduction in tree maintenance. A warm, interleaved boat run without swap pressure would show whether either reduction carries through to frame times.

**@⁠firefloc** on GitHub · 2026-12-02 07:02 UTC · [original](https://github.com/vktory6/VKtory/pull/186#issuecomment-7337697274) I tested #186 locally on Linux/RADV because a streaming trace showed a large cost in sparse bind submission and in `set_runs_before`. I also tried a separate change to the tree traversal so I could distinguish the two effects. Setup: RX 7900 XTX (RADV NAVI31), Ryzen 7 7800X3D, 32 GiB RAM, CachyOS (kernel 7.3.6), KDE/Xwayland at 3440×1440. The fork base used upstream `cc16c64`. Each run started from a fresh clone of the same save, with the live guard filter from #141, `VICTORY_VIEW_GUARD=1`, valid BCPack tables, per-frame MangoHud logging, and `VKB_LAT`. The PE and tools were identical across builds. The scene was the load into the saved world, so these numbers do not characterize the standard boat route. **Exploratory passes, world seconds 1–80** | Build | FPS | p50 / p90 / p99 / max (ms) | Frames >100 ms | Sparse-bind call time over run | | --- | ---: | ---: | ---: | ---: | | Baseline | 11.40 | 44 / 208 / 510 / 973 | 235 | 25.90 s | | Baseline + #186 | 10.44 | 65 / 199 / 449 / 631 | 246 | 18.71 s | | Baseline + #186 + traversal change | 10.38 | 62 / 202 / 504 / 826 | 237 | 17.06 s | With #186 alone the number of sparse-bind flushes dropped from 142,857 to 129,088. Its p99 and max improved in this pass, while mean cadence and the count of frames over 100 ms went the other way. The game lock was held, but a local config overrode the build lock setting in this series. These were single passes and the sparse-bind totals are summed timings over the reported run segments, across threads. They are useful for attribution, not an FPS verdict. **Independent follow-up for `set_runs_before`** The extra change uses one descending `BTreeMap::range_mut(..at).rev()` traversal in place of a predecessor search and a second mutable lookup per changed entry. The stop conditions, published run lengths and Vulkan calls are unchanged. On top of #186, the measured time in that function went from 2.80 to 0.44 s with 64.23M and 64.53M run updates. In a separate pair with both the GPU and build locks confirmed and **without #186 in either build**, it went from **2.99 to 0.93 s (−69%)** for **66.15M and 66.10M** run updates. This is summed time in the function, not a per-frame saving. The standalone traversal change passed the 22 umem unit tests plus the GPU tests for span changes and GPU-only pool views. The host-range CPU/GPU alias coherence test also passed with #186 and the traversal change together. In the locked pair, over world seconds 1–60, MangoHud recorded 7.92 → 4.51 FPS, 487 → 2,106 ms p99, and 206/467 → 167/266 frames over 100 ms. There was active swap in both runs, with spot samples above 100 MiB/s in both directions in the slower run. This pair does not establish a frame pacing benefit or attribute the slowdown to the traversal. Neither run recorded a remap applied ahead of its queue position or a read of pages mapped from the future. I did not run the in-game Vulkan validation or picture gate for #186. The result worth retesting is the reduction in sparse-bind work from #186 and the separate reduction in tree maintenance. A warm, interleaved boat run without swap pressure would show whether either reduction carries through to frame times.
Author
Owner

@⁠firefloc on GitHub · 2026-12-02 17:33 UTC · original

I checked the lookup changes at c3516dd. Keeping the CPU-only alias out of the GPU sparse window and making locate return no GPU buffer makes sense. However, the if gpu around lookup.insert also removes the alias from backing_of and kinds_in, which still read that same tree. The existing backing_of contract explicitly covers equality between a texture view and the pool alias that DirectStorage writes through. cpu_read_ok also uses the reported memory kinds.

Could we keep the backing identity and kind visible to those CPU queries while excluding the alias from GPU lookup and binds? I compiled a regression test with two views of one write-combined pool page. It asserts equal backing identity, write-combined placement and cpu_read_ok == false, checks that the alias has no GPU location, then checks the partial unview and removal. Vulkan execution of that test remains pending. The physical DirectStorage notification path I checked translates its notifications to title addresses, so this lookup change does not by itself establish an in-game stale-texture regression.

**@⁠firefloc** on GitHub · 2026-12-02 17:33 UTC · [original](https://github.com/vktory6/VKtory/pull/186#issuecomment-7353412647) I checked the lookup changes at `c3516dd`. Keeping the CPU-only alias out of the GPU sparse window and making `locate` return no GPU buffer makes sense. However, the `if gpu` around `lookup.insert` also removes the alias from `backing_of` and `kinds_in`, which still read that same tree. The existing `backing_of` contract explicitly covers equality between a texture view and the pool alias that DirectStorage writes through. `cpu_read_ok` also uses the reported memory kinds. Could we keep the backing identity and kind visible to those CPU queries while excluding the alias from GPU lookup and binds? I compiled a regression test with two views of one write-combined pool page. It asserts equal backing identity, write-combined placement and `cpu_read_ok == false`, checks that the alias has no GPU location, then checks the partial unview and removal. Vulkan execution of that test remains pending. The physical DirectStorage notification path I checked translates its notifications to title addresses, so this lookup change does not by itself establish an in-game stale-texture regression.
Author
Owner

@⁠firefloc on GitHub · 2026-12-02 18:50 UTC · original

The targeted alias test now reproduces the lookup change on Navi 31 / RADV. I used the same test setup on d59b4f8 and c3516ddb, with two views of the same write-combined pool page and one declared as host_range.

  • d59b4f8: 1 pass. Both views report the same backing, kinds_in reports GttWriteCombined, and cpu_read_ok is false. Both sides of a partial unview keep the expected backing and placement.
  • c3516ddb: the same test fails on backing identity. The host alias has no backing, kinds_in is empty, and cpu_read_ok becomes true. Both sides still lack that metadata after partial unview. The alias has no GPU buffer, as intended.

The test uses views inside one sparse window, so it checks the lookup contract independently of the real pool's high address. This is native Vulkan test evidence. The DirectStorage path we traced reports title addresses, and a visible game regression remains unverified.

**@⁠firefloc** on GitHub · 2026-12-02 18:50 UTC · [original](https://github.com/vktory6/VKtory/pull/186#issuecomment-7355240541) The targeted alias test now reproduces the lookup change on Navi 31 / RADV. I used the same test setup on `d59b4f8` and `c3516ddb`, with two views of the same write-combined pool page and one declared as `host_range`. - `d59b4f8`: 1 pass. Both views report the same backing, `kinds_in` reports `GttWriteCombined`, and `cpu_read_ok` is false. Both sides of a partial unview keep the expected backing and placement. - `c3516ddb`: the same test fails on backing identity. The host alias has no backing, `kinds_in` is empty, and `cpu_read_ok` becomes true. Both sides still lack that metadata after partial unview. The alias has no GPU buffer, as intended. The test uses views inside one sparse window, so it checks the lookup contract independently of the real pool's high address. This is native Vulkan test evidence. The DirectStorage path we traced reports title addresses, and a visible game regression remains unverified.
Author
Owner

@⁠vktory6 on GitHub · 2026-12-02 20:08 UTC · original

Thanks. We ran it on the reference card (RX 9070 XT) in the boat scene: binds sent fell by 2.6 % and sparse flushes by 8 %, both within the noise of that scene, so there is no measured gain yet. That alone would not block it (rule 24). The correctness point firefloc raised does.

The if gpu around lookup.insert keeps the alias records of CPU-only host_range views out of the lookup altogether. That hides them from the CPU-side queries too: backing_of, kinds_in and cpu_read_ok. One concrete path is dstorage.rs's zlib decode destination (cpu_read_ok before it reads back). A write-combined alias page then counts as fine, and its slow read goes uncounted, silently (rule 5).

What we'd merge:

  • Keep the alias records in the lookup with their backing and kind, so the CPU queries see them.
  • Leave them out of the GPU queries only (locate, locate_span, span_of, gpu_address_of): no window and no sparse bind for them. That keeps the bind saving.
  • Add firefloc's test with two views of the same pages, one of them write-combined, so cpu_read_ok and kinds_in stay right for the alias. Please credit firefloc in the commit.

The sparse-bind cost on RADV is measured and discussed in https://github.com/vktory6/VKtory/discussions/260, if you want numbers on what a bind costs.

**@⁠vktory6** on GitHub · 2026-12-02 20:08 UTC · [original](https://github.com/vktory6/VKtory/pull/186#issuecomment-7357269343) Thanks. We ran it on the reference card (RX 9070 XT) in the boat scene: binds sent fell by 2.6 % and sparse flushes by 8 %, both within the noise of that scene, so there is no measured gain yet. That alone would not block it (rule 24). The correctness point firefloc raised does. The `if gpu` around `lookup.insert` keeps the alias records of CPU-only `host_range` views out of the lookup altogether. That hides them from the CPU-side queries too: `backing_of`, `kinds_in` and `cpu_read_ok`. One concrete path is dstorage.rs's zlib decode destination (`cpu_read_ok` before it reads back). A write-combined alias page then counts as fine, and its slow read goes uncounted, silently (rule 5). What we'd merge: - Keep the alias records in the lookup with their backing and kind, so the CPU queries see them. - Leave them out of the GPU queries only (`locate`, `locate_span`, `span_of`, `gpu_address_of`): no window and no sparse bind for them. That keeps the bind saving. - Add firefloc's test with two views of the same pages, one of them write-combined, so `cpu_read_ok` and `kinds_in` stay right for the alias. Please credit firefloc in the commit. The sparse-bind cost on RADV is measured and discussed in https://github.com/vktory6/VKtory/discussions/260, if you want numbers on what a bind costs.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/186/head:github-bot/gh-186'
git switch 'github-bot/gh-186'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-186'
git switch 'github-bot/gh-186'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-186'
git switch 'github-bot/gh-186'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-186'
git switch 'main'
git merge --squash 'github-bot/gh-186'
git switch 'main'
git merge --ff-only 'github-bot/gh-186'
git switch 'main'
git merge 'github-bot/gh-186'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.