perf(gpu): sample reverse-mip chains in texture probes and coalesce pool reads #326

Manually merged
github merged 1 commit from github-bot/gh-326 into main 2026-11-27 16:23:12 +00:00 AGit
Owner

@⁠Sheffy007 on GitHub · 2026-11-27 11:42 UTC · original
Sheffy007:perf/texture-sync-mip-probes-and-pool-reads → main · +1021 −98 · 4 files · 1 commits · merged 2026-11-27 16:14 UTC

Refs #179

Addresses the two remaining world texture-sync bottlenecks noted in #179: unchanged streamed textures being re-read whole (~410 MiB/s), and partial mip-streamed textures walking page-by-page across scattered 64 KiB mappings.

Root Causes & Changes

  1. Mip-aware probe_nonzero and probe_read (crates/victory-gpu/src/native/textures.rs):
    • In GFX10 Addr2 layouts (Layout::tiled and Layout::linear), mips are packed smallest-first: [0..64 KiB) holds the mip tail (with bytes 0..1024 permanently zero-padded on 2D surfaces), followed by coarse-to-fine standard mips, and mip 0 occupies the top ~75% of the buffer.
    • Previously, probe_nonzero checked only (size / 2) & !4095 (inside mip 0) and probe_read sampled 0..256 (unused tail padding) plus 7 windows mostly inside mip 1 and mip 0. Whenever DirectStorage streamed coarse or mid mips while mip 0 remained NO_ACCESS, all 8 samples read zeros before and after (sampled_sees_writes = false), forcing probe_round into every = 1 immediate whole-texture re-reads (up to WHOLE_BUDGET = 8 MiB per submission) — and skipped was not reset when a whole read ran, causing back-to-back whole reads right after an upload.
    • probe_nonzero now also checks the coarsest and first-tail mip center micro-tiles on multi-mip layouts, and probe_read samples one 256-byte center window per mip level (Layout::offset(mip, w / 2, h / 2, 0), accounting for XOR and pipe-bank XOR) while resetting skipped = 0 whenever a whole read completes.
  2. Coalesced partial reads via PoolTable::read_mapped_with (crates/victory-gpu/src/native/memory.rs, textures.rs):
    • Placed streamed textures map 64 KiB physical pages one at a time in scattered pool order, producing separate 64 KiB vm_map_entry regions in XNU. PoolTable::read_mapped_with coalesces adjacent mapped title-address pages across pools and out-of-order pool pages into a single fault-checked safe_read per contiguous resident span and zeroes unmapped fine-mip holes without mach_vm_region traps (read_texture_chunk also coalesces adjacent readable VM regions for non-pool memory).
  3. Skip detiling & uploading never-uploaded zero mips, and avoid duplicate scanner first-reads (textures.rs):
    • Fresh MTLStorageMode::Shared textures start zeroed. detile_changed (Tracked::uploaded_mips) skips staging allocations, CPU detiling, and under-the-lock replaceRegion uploads for never-uploaded blank textures and all-zero non-tail mips (checking !layout.addressable() first so unsupported layouts are still counted).
    • Sampled textures enter WATCH once their first submission read populates seen in sync_apply, preventing the background scanner thread from racing prepare to hash brand-new textures a second time.

Benchmark (Apple M5 Pro, 48 GB, macOS 27.2.1, --release)

cargo test --release -p victory-gpu --lib bench_streamed_texture_sync_pool_reads_and_mip_probes -- --ignored --nocapture (32 x 1.375 MiB 512x512 SW_64KB_D_X 10-mip textures, each with 6 scattered mach_vm_remap 64 KiB pages for mips 1..9 and 1 MiB NO_ACCESS mip 0):

  • Initial blank sync (32 textures, 44 MiB): 5.17 ms (0 staging allocations / replaceRegion calls).
  • 320 partial texture reads (440 MiB): page-by-page VM walk 89.72 ms (4,160 kernel calls) -> PoolTable coalesced 24.49 ms (320 kernel calls, 3.66x faster, 13x fewer kernel calls).
  • Streamed mips 1..9 sync (32 textures, unmapped 1 MiB mip 0 skipped): 8.39 ms total.
  • 30 idle submissions (~1 world frame) while mip 0 remains unmapped: 31.62 MiB of periodic WHOLE_SPARE checks (backing off to 0 across subsequent frames), down from ~240 MiB per 30-submission frame when sampled_sees_writes was false.

Verification

  • cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo xtask policy
**@⁠Sheffy007** on GitHub · 2026-11-27 11:42 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/326) `Sheffy007:perf/texture-sync-mip-probes-and-pool-reads` → `main` · +1021 −98 · 4 files · 1 commits · merged 2026-11-27 16:14 UTC Refs #179 Addresses the two remaining world texture-sync bottlenecks noted in #179: unchanged streamed textures being re-read whole (~410 MiB/s), and partial mip-streamed textures walking page-by-page across scattered 64 KiB mappings. ### Root Causes & Changes 1. **Mip-aware `probe_nonzero` and `probe_read` (`crates/victory-gpu/src/native/textures.rs`):** - In GFX10 `Addr2` layouts (`Layout::tiled` and `Layout::linear`), mips are packed smallest-first: `[0..64 KiB)` holds the mip tail (with bytes `0..1024` permanently zero-padded on 2D surfaces), followed by coarse-to-fine standard mips, and **mip 0 occupies the top ~75% of the buffer**. - Previously, `probe_nonzero` checked only `(size / 2) & !4095` (inside mip 0) and `probe_read` sampled `0..256` (unused tail padding) plus 7 windows mostly inside mip 1 and mip 0. Whenever DirectStorage streamed coarse or mid mips while mip 0 remained `NO_ACCESS`, all 8 samples read zeros before and after (`sampled_sees_writes = false`), forcing `probe_round` into `every = 1` immediate whole-texture re-reads (up to `WHOLE_BUDGET = 8 MiB` per submission) — and `skipped` was not reset when a whole read ran, causing back-to-back whole reads right after an upload. - `probe_nonzero` now also checks the coarsest and first-tail mip center micro-tiles on multi-mip layouts, and `probe_read` samples one `256`-byte center window per mip level (`Layout::offset(mip, w / 2, h / 2, 0)`, accounting for XOR and pipe-bank XOR) while resetting `skipped = 0` whenever a whole read completes. 2. **Coalesced partial reads via `PoolTable::read_mapped_with` (`crates/victory-gpu/src/native/memory.rs`, `textures.rs`):** - Placed streamed textures map 64 KiB physical pages one at a time in scattered pool order, producing separate 64 KiB `vm_map_entry` regions in XNU. `PoolTable::read_mapped_with` coalesces adjacent mapped title-address pages across pools and out-of-order pool pages into a single fault-checked `safe_read` per contiguous resident span and zeroes unmapped fine-mip holes without `mach_vm_region` traps (`read_texture_chunk` also coalesces adjacent readable VM regions for non-pool memory). 3. **Skip detiling & uploading never-uploaded zero mips, and avoid duplicate scanner first-reads (`textures.rs`):** - Fresh `MTLStorageMode::Shared` textures start zeroed. `detile_changed` (`Tracked::uploaded_mips`) skips staging allocations, CPU detiling, and under-the-lock `replaceRegion` uploads for never-uploaded blank textures and all-zero non-tail mips (checking `!layout.addressable()` first so unsupported layouts are still counted). - Sampled textures enter `WATCH` once their first submission read populates `seen` in `sync_apply`, preventing the background scanner thread from racing `prepare` to hash brand-new textures a second time. ### Benchmark (Apple M5 Pro, 48 GB, macOS 27.2.1, `--release`) `cargo test --release -p victory-gpu --lib bench_streamed_texture_sync_pool_reads_and_mip_probes -- --ignored --nocapture` (`32` x `1.375 MiB` `512x512` `SW_64KB_D_X` 10-mip textures, each with `6` scattered `mach_vm_remap` 64 KiB pages for mips `1..9` and `1 MiB` `NO_ACCESS` mip 0): - **Initial blank sync (32 textures, 44 MiB):** `5.17 ms` (0 staging allocations / `replaceRegion` calls). - **320 partial texture reads (440 MiB):** page-by-page VM walk `89.72 ms` (`4,160` kernel calls) -> `PoolTable` coalesced `24.49 ms` (`320` kernel calls, **`3.66x` faster**, **`13x` fewer kernel calls**). - **Streamed mips `1..9` sync (32 textures, unmapped 1 MiB mip 0 skipped):** `8.39 ms` total. - **30 idle submissions (~1 world frame) while mip 0 remains unmapped:** `31.62 MiB` of periodic `WHOLE_SPARE` checks (backing off to 0 across subsequent frames), down from **~240 MiB** per 30-submission frame when `sampled_sees_writes` was `false`. ### Verification - `cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask` - `cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo xtask policy`
In GFX10 Addr2 layouts, mips are packed smallest-first: the 64 KiB mip
tail sits at byte 0 (with 0..1024 zero-padded on 2D surfaces), followed
by coarse-to-fine standard mips and mip 0 in the top ~75% of the buffer.
When world streaming maps coarse mips while fine mips stay NO_ACCESS:

- probe_nonzero checked only (size / 2) & !4095 inside mip 0, missing
  coarse-mip writes to blank textures until the scanner round; probe_read
  sampled 0..256 (zero tail padding) plus offsets in mip 1 and mip 0,
  leaving sampled_sees_writes = false and forcing immediate whole-texture
  re-reads (up to WHOLE_BUDGET = 8 MiB per submission). Sample mip center
  micro-tiles across the mip chain via Layout::offset and reset skipped
  whenever a whole read runs.
- Coalesce adjacent mapped title-address pages across pools and scattered
  64 KiB physical pages via PoolTable::read_mapped_with (and coalesce
  adjacent readable VM regions in read_texture_chunk) so partial reads
  issue one safe_read per resident span and zero unmapped fine-mip holes
  without per-page mach_vm_region traps.
- Skip staging allocations, CPU detile, and replaceRegion uploads for
  never-uploaded blank textures and all-zero non-tail mips, and register
  sampled textures in WATCH once their first submission read populates
  seen.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 12:02:42 +00:00
Author
Owner

@⁠Olima34 on GitHub · 2026-11-27 13:29 UTC · original

Check report: 94d27ba2 merged onto main c5d7f4d0 passed every check here (M5 Pro 48 GB, arm64 with the converter: victory-gpu 332 passed, victory-shader 360, Windows clippy clean, policy ok). Since then main has moved to 60baf130 (the maintainer's integration, including 'GPU-written textures not refilled'), and this now conflicts in crates/victory-gpu/src/native/texture_tests.rs (textures.rs itself merges cleanly). @⁠Sheffy007, could you rebase it on main? Mention @⁠Olima34 then, and I'll re-run the checks and a game run against main (seconds alive, frame reached, GPU errors, frame rate in the world).

**@⁠Olima34** on GitHub · 2026-11-27 13:29 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/326#issuecomment-7219537392) Check report: `94d27ba2` merged onto main `c5d7f4d0` passed every check here (M5 Pro 48 GB, arm64 with the converter: victory-gpu 332 passed, victory-shader 360, Windows clippy clean, policy ok). Since then main has moved to `60baf130` (the maintainer's integration, including 'GPU-written textures not refilled'), and this now conflicts in `crates/victory-gpu/src/native/texture_tests.rs` (textures.rs itself merges cleanly). @⁠Sheffy007, could you rebase it on main? Mention @⁠Olima34 then, and I'll re-run the checks and a game run against main (seconds alive, frame reached, GPU errors, frame rate in the world).
github-bot force-pushed github-bot/gh-326 from 94d27ba244 to 3f08b8471b 2026-11-27 14:08:10 +00:00 Compare
Author
Owner

@⁠Sheffy007 on GitHub · 2026-11-27 13:56 UTC · original

Rebased onto main (b639c175 -> 3f08b847), integrating cleanly with #252's hot_until serial cooling and 95627c66's kept_from_memory / gpu_written tracking (the initial zero-skip path still increments uploads, so kept_from_memory continues to release GPU-written UAVs and render targets from CPU refills and WATCH), and using sync_dirty_without_probe_deadline / sync_jobs_within(None) in streamed_mips_across_scattered_pool_pages_upload_each_step_without_whole_rereads.

All workspace tests, both Windows clippy passes, and cargo xtask policy pass on 3f08b847. Ready for re-check and the world game run whenever convenient, @⁠Olima34!

**@⁠Sheffy007** on GitHub · 2026-11-27 13:56 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/326#issuecomment-7220138892) Rebased onto `main` (`b639c175` -> `3f08b847`), integrating cleanly with #252's `hot_until` serial cooling and `95627c66`'s `kept_from_memory` / `gpu_written` tracking (the initial zero-skip path still increments `uploads`, so `kept_from_memory` continues to release GPU-written UAVs and render targets from CPU refills and `WATCH`), and using `sync_dirty_without_probe_deadline` / `sync_jobs_within(None)` in `streamed_mips_across_scattered_pool_pages_upload_each_step_without_whole_rereads`. All workspace tests, both Windows `clippy` passes, and `cargo xtask policy` pass on `3f08b847`. Ready for re-check and the world game run whenever convenient, @⁠Olima34!
Author
Owner

@⁠Olima34 on GitHub · 2026-11-27 14:14 UTC · original

Check report: 3f08b847 merged onto main 728845d1 (M5 Pro 48 GB, arm64, with the converter).

  • cargo test -p victory-hle -p victory -p victory-gpu -p xtask: passes (victory-gpu 364 passed).
  • Windows clippy is clean and cargo xtask policy passes.
  • Flaky: the new native::tests::texture_tests::streamed_mips_across_scattered_pool_pages_upload_each_step_without_whole_rereads failed once in 4 full runs of the victory-gpu lib tests (texture_tests.rs:1746, 'idle submission {sub} after mip 1'). The other 3 runs passed. So in one run, an idle sync_jobs_within(None) after step 3 returned a job for key. That looks like the cross-test interference behind #307 and #252's earlier flake: the dirty set the scanner fills (watch) is process-wide, so another test running alongside can put a key into this test's sync. @⁠Sheffy007, could you make the idle-submission checks independent of other tests, e.g. by holding the title lock for the whole test as 5ba3cce6 does, or by draining watch before each idle loop? Then 3 clean runs in a row should do.
  • No game run: world runs currently hang the GPU on this Mac (#79).

Result: not ready yet, only because of the flaky test. Everything else passes.

**@⁠Olima34** on GitHub · 2026-11-27 14:14 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/326#issuecomment-7220520217) Check report: `3f08b847` merged onto main `728845d1` (M5 Pro 48 GB, arm64, with the converter). - `cargo test -p victory-hle -p victory -p victory-gpu -p xtask`: passes (victory-gpu 364 passed). - Windows clippy is clean and `cargo xtask policy` passes. - **Flaky:** the new `native::tests::texture_tests::streamed_mips_across_scattered_pool_pages_upload_each_step_without_whole_rereads` failed once in 4 full runs of the victory-gpu lib tests (`texture_tests.rs:1746`, 'idle submission {sub} after mip 1'). The other 3 runs passed. So in one run, an idle `sync_jobs_within(None)` after step 3 returned a job for `key`. That looks like the cross-test interference behind #307 and #252's earlier flake: the dirty set the scanner fills (`watch`) is process-wide, so another test running alongside can put a key into this test's sync. @⁠Sheffy007, could you make the idle-submission checks independent of other tests, e.g. by holding the title lock for the whole test as `5ba3cce6` does, or by draining `watch` before each idle loop? Then 3 clean runs in a row should do. - No game run: world runs currently hang the GPU on this Mac (#79). Result: not ready yet, only because of the flaky test. Everything else passes.
github-bot force-pushed github-bot/gh-326 from 3f08b8471b to 317487d707 2026-11-27 14:46:08 +00:00 Compare
Author
Owner

@⁠Sheffy007 on GitHub · 2026-11-27 14:43 UTC · original

Good catch, @⁠Olima34! Traced the exact 1-in-4 race at Step 3 (after mip 1), which had two parts:

  1. In-flight WATCH scan over a multi-page mapping (textures.rs): Step 3 maps 4 scattered 64 KiB pages in a loop (k = 0..4). When the background scan_round thread woke up mid-loop (hashing an intermediate H_torn), Title::jobs / prepare then read the completed H3, and scan_round committed w.seen[key] = H_torn; w.dirty.insert(key) right before sync_apply ran. In sync_apply, w.seen.insert(key, H3) == Some(H3) then saw Some(H_torn) != Some(H3), leaving key stuck in w.dirty for the first idle submission (and in real streaming, causing an extra redundant whole-texture read). Watch now tracks a per-key epoch bumped when Title::jobs begins or sync_apply finishes a submission read, so any scan_round that started earlier discards its older hash.
  2. Shared Title lock (texture_tests.rs): streamed_mips_across_scattered_pool_pages_upload_each_step_without_whole_rereads now also holds with_title across all steps (matching 5ba3cce6), so parallel tests on the shared Title cannot drain the process-wide WATCH.dirty set mid-step.

Rebased onto main (728845d1 -> 317487d7) and verified with 5 consecutive full cargo test -p victory-gpu --lib runs (5/5 green), full workspace tests, both Windows clippy passes, and cargo xtask policy.

**@⁠Sheffy007** on GitHub · 2026-11-27 14:43 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/326#issuecomment-7221144773) Good catch, @⁠Olima34! Traced the exact 1-in-4 race at Step 3 (`after mip 1`), which had two parts: 1. **In-flight `WATCH` scan over a multi-page mapping (`textures.rs`):** Step 3 maps 4 scattered 64 KiB pages in a loop (`k = 0..4`). When the background `scan_round` thread woke up mid-loop (hashing an intermediate `H_torn`), `Title::jobs` / `prepare` then read the completed `H3`, and `scan_round` committed `w.seen[key] = H_torn; w.dirty.insert(key)` right before `sync_apply` ran. In `sync_apply`, `w.seen.insert(key, H3) == Some(H3)` then saw `Some(H_torn) != Some(H3)`, leaving `key` stuck in `w.dirty` for the first idle submission (and in real streaming, causing an extra redundant whole-texture read). `Watch` now tracks a per-key `epoch` bumped when `Title::jobs` begins or `sync_apply` finishes a submission read, so any `scan_round` that started earlier discards its older hash. 2. **Shared `Title` lock (`texture_tests.rs`):** `streamed_mips_across_scattered_pool_pages_upload_each_step_without_whole_rereads` now also holds `with_title` across all steps (matching `5ba3cce6`), so parallel tests on the shared `Title` cannot drain the process-wide `WATCH.dirty` set mid-step. Rebased onto `main` (`728845d1` -> `317487d7`) and verified with **5 consecutive full `cargo test -p victory-gpu --lib` runs** (5/5 green), full workspace tests, both Windows `clippy` passes, and `cargo xtask policy`.
github-bot referenced this pull request from a commit 2026-11-27 16:23:10 +00:00
github manually merged commit 061ba4dac4 into main 2026-11-27 16:23:12 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 16:15 UTC · original

Merged in 4cf84c83. It overlapped #350 in detile, so I merged the two by hand (the dropped levels are skipped in the new detile_changed too), and marked fresh Metal textures reading as zero as a guess in the code and docs, since Metal does not document it. Before pushing I ran the tests (native arm64), Windows clippy, the policy check, a release build, and one headless game run of the exact tree (170 s, Series S): menus at 30 fps, the world entered at about 81 s and kept presenting to the end at about 1 frame a second, 0 GPU errors, no crash. Thanks.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 16:15 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/326#issuecomment-7223045340) Merged in 4cf84c83. It overlapped #350 in detile, so I merged the two by hand (the dropped levels are skipped in the new detile_changed too), and marked fresh Metal textures reading as zero as a guess in the code and docs, since Metal does not document it. Before pushing I ran the tests (native arm64), Windows clippy, the policy check, a release build, and one headless game run of the exact tree (170 s, Series S): menus at 30 fps, the world entered at about 81 s and kept presenting to the end at about 1 frame a second, 0 GPU errors, no crash. Thanks.
This discussion has been locked. Commenting is limited to contributors.
No description provided.