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

Merged
Sheffy007 merged 1 commit from upstream-pr/326 into main 2026-11-27 16:14:20 +00:00
Sheffy007 commented 2026-11-27 11:42:56 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T11:42:56Z.


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
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5526035160 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/326). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T11:42:56Z. --- 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`
Olima34 commented 2026-11-27 13:29:17 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T13:29:17Z.


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

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7219537392 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/326#issuecomment-7219537392). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-27T13:29:17Z. --- 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).
Sheffy007 commented 2026-11-27 13:56:48 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T13:56:48Z.


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!

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7220138892 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/326#issuecomment-7220138892). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T13:56:48Z. --- 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!
Olima34 commented 2026-11-27 14:14:00 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T14:14:00Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7220520217 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/326#issuecomment-7220520217). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-27T14:14:00Z. --- 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.
Sheffy007 commented 2026-11-27 14:43:24 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T14:43:24Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7221144773 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/326#issuecomment-7221144773). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T14:43:24Z. --- 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`.
LakshanSivakumar commented 2026-11-27 16:15:29 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T16:15:29Z.


Merged in 4cf84c83. It overlapped #348 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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7223045340 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/326#issuecomment-7223045340). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T16:15:29Z. --- Merged in 4cf84c83. It overlapped #348 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.
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
LakshanSivakumar/Native-Victory-macOS!326
No description provided.