Skip to content

perf(gpu): opt-in fast bindless maps and per-page buffer-view resolution (VICTORY_QUICK_SYNC) - #538

Closed
gdemontalivet wants to merge 1 commit into
pragyaangaur:mainfrom
gdemontalivet:perf/macos-quick-sync-census-maps
Closed

gdemontalivet wants to merge 1 commit into
pragyaangaur:mainfrom
gdemontalivet:perf/macos-quick-sync-census-maps

Conversation

@gdemontalivet

Copy link
Copy Markdown
Contributor

Summary

Adds opt-in VICTORY_QUICK_SYNC=1 (off by default; also accepts maps, views, or maps,views) to speed up sync_heaps descriptor mirroring and bindless range bookkeeping on Apple Silicon / Wine:

  • crates/victory-gpu/src/census_maps.rs (new, portable std):
    • Implements a 128-bit multiply-and-fold hasher (Hashing::Fold) and RangesMap (Tree / Hash) replacing the BTreeMap and RandomState HashMap inside bindless::Viewed when maps is enabled, preserving #529 (VICTORY_CENSUS_FAST=1) UnorderedIndex and widened_writes_into semantics.
  • crates/victory-gpu/src/descriptor/host/quick.rs & descriptor/host.rs:
    • Integrates per-page deduplicated buffer-SRV/CBV resolution (quick::resolve) into #439's HostHeap::mirror_synced_batch: resolves each unique [tag, i, generation] buffer SRV, placed buffer SRV, or aligned dword-multiple CBV once per 64 KiB page outside HostHeap::made, then stamps all matching slots via StagingHeap + CopyDescriptors and Viewed::set_batch_resolved.
    • Leaves textures, UAVs, samplers, placed textures, unaligned/refused views, and concurrent rewrites on the standard mirror path.
  • crates/victory-gpu/src/descriptor.rs:
    • Moves set_view_gen(i, generation) from Views::entry_kept to record under VIEWS.write(), fixing a parallel-test isolation race.

Verification

  • cargo xtask policy: passed (297 files ok; host.rs 314,921 B, descriptor/host.rs 251,122 B).
  • cargo clippy --target x86_64-pc-windows-gnullvm --workspace --all-targets -- -D warnings (default and --features diagnostics): 0 warnings.
  • Native cargo test -p victory-gpu --lib census: 9 passed, 0 failed.
  • Live Wine/D3DMetal (descriptor:: bindless:: census quick): 77 passed, 0 failed both with default env and with VICTORY_QUICK_SYNC=1 VICTORY_CENSUS_FAST=1 (including new CBV/SRV/structured/placed/widened GPU readback equivalence and concurrent-rewrite tests).
  • Matched quiet Apple Silicon M5 Max A/B (identical binary & frozen warm shader/pipeline cache):
    • VICTORY_QUICK_SYNC=0: median world frame 692.5 ms, prep 283.4 ms, heap_slots 51.7 ms, +48..+90 1.229 FPS (34.17 s), +8..+90 1.295 FPS (63.33 s).
    • VICTORY_QUICK_SYNC=1: median world frame 611.8 ms (-11.7%), prep 214.5 ms (-24.3%), heap_slots 42.8 ms (-17.2%), +48..+90 1.493 FPS (28.14 s, +21.5%), +8..+90 1.431 FPS (57.32 s, +10.5%).

@hackgods

Copy link
Copy Markdown
Contributor

With VICTORY_QUICK_SYNC=1 (maps) and VICTORY_CENSUS_FAST unset, shadow::tests::a_heap_written_twice_before_a_barrier_is_expanded_once_at_its_last_place fails in some full Wine runs on an M5 Pro. The test expects [(0x20_0400, 16), (0x20_0000, 0x100), (0x20_0800, 0x100)]; the two heap ranges come back the other way round.

The cause: widened_writes_into now iterates the hash RangesMap, whose order follows the Arc<Shadow> addresses in the keys, so it changes from run to run. With CENSUS_FAST=1 the ordered UnorderedIndex is used and the suite passes.

The writes are refreshed from the same copy's bytes, so the order probably does not matter in game. Could you either sort what widened_writes_into returns when the maps are hashed (as the BTreeMap did), or compare the test's history as a set?

Found by Claude Code (Claude Opus 5.5) running the Wine suites on an M5 Pro.

@gdemontalivet
gdemontalivet force-pushed the perf/macos-quick-sync-census-maps branch from cae43c5 to a49788e Compare November 27, 2026 12:52
@pragyaangaur

Copy link
Copy Markdown
Owner

I merged main into this branch and ran victory-gpu under Wine on a real GPU: all tests pass (575). On GitHub's Windows run (the software adapter) it fails every time, and main passes there. Each time the host device is removed partway through the run (0x887A0005, 'The GPU device instance has been suspended'), and every GPU test that starts after that fails with it. In the latest run the first failures are `quick_sync_gpu_reads_match_standard_for_cbv_typed_structured_placed_and_widened_srvs` and `quick_sync_matches_standard_across_buffer_views_textures_uavs_and_duplicates`. So a test or a code path in this change removes the device on the software adapter, likely a shader or descriptor access that WARP faults on and the real GPU tolerates. Could you find which test does it (running the suite with --test-threads=1 on a Windows machine, or with VICTORY_D3D12_WARP=1 and the diagnostics feature, shows the first removal)? I am leaving this open until it is green, since a merge would turn main red.

@hackgods

Copy link
Copy Markdown
Contributor

The WARP failure looks like the new GPU test's own inputs, not a fault in the quick path. In the CI log the first panic is quick.rs:568:40: host.new_list() in the test's second (quick) run returns 0x887A0005, so the device was already gone after the first (standard) run's dispatch. Every GPU test after that fails with the same code, including quick_sync_matches_standard_… slot 0, because no host copy can be made on a removed device.

quick_sync_gpu_reads_match_standard_for_cbv_typed_structured_placed_and_widened_srvs feeds WARP two views that main already documents as unsafe on native D3D12. Both are made the same way by buffer_srv in the standard run:

A minimal fix keeps both cases where they are valid (under Wine):

// Native D3D12 (WARP): no RGB32 typed views (WARP loses the device on them) and
// raw views 16-byte aligned. Under Wine the 3-channel view is widened and any
// dword start works, which is what this test is for.
let native = !crate::under_wine();
let placed_at: u64 = if native { 32 } else { 28 };
// slot 3: ... ((mem + placed_at) as u32, ((mem + placed_at) >> 32) as u32)
// slot 4: format 3 (R32G32B32A32_UINT) when native, else 7 (R32G32B32_UINT)
// expected: placed_raw.Load(0) = 0xA000_0000 + placed_at / 4;
//           widened[1].xyz = 4, 5, 6 when native (RGBA32 element 1), else 3, 4, 5

quick::resolve makes the same descriptors as buffer_srv for these inputs, and every other raw view in the test is 16-byte aligned (+16 CBV, FirstElement 4 structured fallback, FirstElement 0 UAV). So nothing here points at an out-of-bounds index or size in quick.rs or census_maps.rs. I could not run WARP myself: under our Wine there is no WARP adapter (VICTORY_D3D12_WARP=1 gives no host device). Running the patched test on CI would confirm it.

Found by Claude Code (Claude Opus 5.5) from the CI log and the test's inputs; not reproduced on WARP here.

@pragyaangaur

Copy link
Copy Markdown
Owner

This conflicts with current main in bindless.rs and descriptor/host.rs and its last CI run failed on an older base. #640 and #613 changed the same heap sync code since. Could you rebase onto main and push?

…ion (VICTORY_QUICK_SYNC)

Add opt-in VICTORY_QUICK_SYNC (default off; supports 1, maps, views) layered onto batched heap sync and VICTORY_CENSUS_FAST=1:

- Part A (census_maps.rs + bindless.rs): multiply-and-fold FoldHasher (Hashing) and hash-backed RangesMap replacing BTreeMap in bindless::Viewed when enabled, preserving UnorderedIndex / SnapshotCache (VICTORY_CENSUS_FAST=1), pragyaangaur#613 hull_generation invariance across interior view edits, deterministic BTreeMap-equivalent ordering in widened_writes_into when RangesMap is hash-backed, and sorting range copy keys before dedup in diagnostics.
- Part B (descriptor/host/quick.rs + descriptor/host.rs): resolve each unique buffer SRV, placed buffer SRV, or aligned CBV record once per synced 64 KiB page outside HostHeap::made (including 3-channel widening, raw_structured lowering, held source, stride, view address, and bindless::Seen), then stamp matching slots via StagingHeap templates, keep_batch, and Viewed::set_batch_resolved while preserving pragyaangaur#640 page-span live reads, pragyaangaur#618 deferred show-after, and SyncStat / VICTORY_VIEW_PROBE telemetry. Textures, UAVs, samplers, placed textures, and refused/edge-case views stay on the standard path.
- Add unit and live GPU equivalence tests comparing quick sync against standard sync across all view kinds, VICTORY_CENSUS_FAST=1, deterministic widened_writes_into ordering, and both Wine and native D3D12 (WARP) alignment/format rules.
@gdemontalivet

Copy link
Copy Markdown
Contributor Author

Rebased onto main (cade9e02) and fixed both CI / test issues:

  1. Windows WARP (0x887A0005) in descriptor::host::quick::tests::quick_sync_gpu_reads_match_standard_for_cbv_typed_structured_placed_and_widened_srvs:
    • When !crate::under_wine(), slot 3 now uses a 16-byte aligned raw SRV offset (32 instead of 28, since native D3D12 rejects non-16-byte raw FirstElement) and slot 4 uses R32G32B32A32_UINT instead of R32G32B32_UINT (3-channel widening only applies under Wine).
  2. Deterministic widened_writes_into ordering:
    • When RangesMap uses the hashed backend (VICTORY_QUICK_SYNC=1), bindless::widened_writes_into sorts the newly appended slice by (Arc::as_ptr(s) as usize, address, len) to match BTreeMap order identically (fixing the intermittent ordering flake in shadow::tests::a_heap_written_twice_before_a_barrier_is_expanded_once_at_its_last_place when VICTORY_CENSUS_FAST is unset). Added a dedicated regression test (widened_writes_into_order_matches_tree_when_quick_maps_enabled).
  3. Rebase reconciliation:
    • Preserved #613 (hull_generation invariance across interior edits), #640 page-span live reads, #618 (VICTORY_SHOW_AFTER=1), and #657 (VICTORY_SYNC_EARLY=1).

Verified locally: cargo test -p xtask (10/10), cargo clippy --target x86_64-pc-windows-gnullvm -p victory-gpu -- -D warnings (0 warnings across default, diagnostics, and --tests), and live CrossOver/D3DMetal GPU tests (census_maps::, bindless::, descriptor::host::quick::, show_after, plus 3/3 runs of a_heap_written_twice_before_a_barrier_is_expanded_once_at_its_last_place with VICTORY_QUICK_SYNC=1 VICTORY_CENSUS_FAST=0).

@gdemontalivet
gdemontalivet force-pushed the perf/macos-quick-sync-census-maps branch from 15fc943 to 75efbe4 Compare November 28, 2026 09:20
@pragyaangaur

Copy link
Copy Markdown
Owner

Thank you for the rebase onto main earlier. #675 (VICTORY_DISJOINT_SPANS, also from gdemontalivet) has now merged and changes the same Inner and Viewed constructors in bindless.rs (new(fast, disjoint_spans) against this PR's new(fast, quick_maps), and the hulls and ends fields). The two designs both change how hulls are made, so merging them is a design decision that I would rather leave to you. Could you rebase once more, with both switches working together or with VICTORY_QUICK_SYNC documented as exclusive with VICTORY_DISJOINT_SPANS?

@pragyaangaur

Copy link
Copy Markdown
Owner

Closing as superseded: #689 (merged) integrates this PR's quick descriptor sync with #675's disjoint spans, and both switches stay opt-in. Thank you for the original work and the rebases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants