perf(gpu): sample reverse-mip chains in texture probes and coalesce pool reads #326
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/326"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
probe_nonzeroandprobe_read(crates/victory-gpu/src/native/textures.rs):Addr2layouts (Layout::tiledandLayout::linear), mips are packed smallest-first:[0..64 KiB)holds the mip tail (with bytes0..1024permanently zero-padded on 2D surfaces), followed by coarse-to-fine standard mips, and mip 0 occupies the top ~75% of the buffer.probe_nonzerochecked only(size / 2) & !4095(inside mip 0) andprobe_readsampled0..256(unused tail padding) plus 7 windows mostly inside mip 1 and mip 0. Whenever DirectStorage streamed coarse or mid mips while mip 0 remainedNO_ACCESS, all 8 samples read zeros before and after (sampled_sees_writes = false), forcingprobe_roundintoevery = 1immediate whole-texture re-reads (up toWHOLE_BUDGET = 8 MiBper submission) — andskippedwas not reset when a whole read ran, causing back-to-back whole reads right after an upload.probe_nonzeronow also checks the coarsest and first-tail mip center micro-tiles on multi-mip layouts, andprobe_readsamples one256-byte center window per mip level (Layout::offset(mip, w / 2, h / 2, 0), accounting for XOR and pipe-bank XOR) while resettingskipped = 0whenever a whole read completes.PoolTable::read_mapped_with(crates/victory-gpu/src/native/memory.rs,textures.rs):vm_map_entryregions in XNU.PoolTable::read_mapped_withcoalesces adjacent mapped title-address pages across pools and out-of-order pool pages into a single fault-checkedsafe_readper contiguous resident span and zeroes unmapped fine-mip holes withoutmach_vm_regiontraps (read_texture_chunkalso coalesces adjacent readable VM regions for non-pool memory).textures.rs):MTLStorageMode::Sharedtextures start zeroed.detile_changed(Tracked::uploaded_mips) skips staging allocations, CPU detiling, and under-the-lockreplaceRegionuploads for never-uploaded blank textures and all-zero non-tail mips (checking!layout.addressable()first so unsupported layouts are still counted).WATCHonce their first submission read populatesseeninsync_apply, preventing the background scanner thread from racingprepareto 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(32x1.375 MiB512x512SW_64KB_D_X10-mip textures, each with6scatteredmach_vm_remap64 KiB pages for mips1..9and1 MiBNO_ACCESSmip 0):5.17 ms(0 staging allocations /replaceRegioncalls).89.72 ms(4,160kernel calls) ->PoolTablecoalesced24.49 ms(320kernel calls,3.66xfaster,13xfewer kernel calls).1..9sync (32 textures, unmapped 1 MiB mip 0 skipped):8.39 mstotal.31.62 MiBof periodicWHOLE_SPAREchecks (backing off to 0 across subsequent frames), down from ~240 MiB per 30-submission frame whensampled_sees_writeswasfalse.Verification
cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtaskcargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warningscargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warningscargo xtask policyImported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T13:29:17Z.
Check report:
94d27ba2merged onto mainc5d7f4d0passed 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 to60baf130(the maintainer's integration, including 'GPU-written textures not refilled'), and this now conflicts incrates/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).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'shot_untilserial cooling and95627c66'skept_from_memory/gpu_writtentracking (the initial zero-skip path still incrementsuploads, sokept_from_memorycontinues to release GPU-written UAVs and render targets from CPU refills andWATCH), and usingsync_dirty_without_probe_deadline/sync_jobs_within(None)instreamed_mips_across_scattered_pool_pages_upload_each_step_without_whole_rereads.All workspace tests, both Windows
clippypasses, andcargo xtask policypass on3f08b847. Ready for re-check and the world game run whenever convenient, @Olima34!Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T14:14:00Z.
Check report:
3f08b847merged onto main728845d1(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).cargo xtask policypasses.native::tests::texture_tests::streamed_mips_across_scattered_pool_pages_upload_each_step_without_whole_rereadsfailed 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 idlesync_jobs_within(None)after step 3 returned a job forkey. 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 as5ba3cce6does, or by drainingwatchbefore each idle loop? Then 3 clean runs in a row should do.Result: not ready yet, only because of the flaky test. Everything else passes.
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:WATCHscan over a multi-page mapping (textures.rs): Step 3 maps 4 scattered 64 KiB pages in a loop (k = 0..4). When the backgroundscan_roundthread woke up mid-loop (hashing an intermediateH_torn),Title::jobs/preparethen read the completedH3, andscan_roundcommittedw.seen[key] = H_torn; w.dirty.insert(key)right beforesync_applyran. Insync_apply,w.seen.insert(key, H3) == Some(H3)then sawSome(H_torn) != Some(H3), leavingkeystuck inw.dirtyfor the first idle submission (and in real streaming, causing an extra redundant whole-texture read).Watchnow tracks a per-keyepochbumped whenTitle::jobsbegins orsync_applyfinishes a submission read, so anyscan_roundthat started earlier discards its older hash.Titlelock (texture_tests.rs):streamed_mips_across_scattered_pool_pages_upload_each_step_without_whole_rereadsnow also holdswith_titleacross all steps (matching5ba3cce6), so parallel tests on the sharedTitlecannot drain the process-wideWATCH.dirtyset mid-step.Rebased onto
main(728845d1->317487d7) and verified with 5 consecutive fullcargo test -p victory-gpu --libruns (5/5 green), full workspace tests, both Windowsclippypasses, andcargo xtask policy.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.