Skip to content

GPU: userspace placeholder fast path, 64-block BlockSet guards & immediate decoration-bake texture flush - #1097

Open
RealAzemed wants to merge 1 commit into
fxceless:mainfrom
RealAzemed:perf/placeholder-fast-blockset-bake-flush
Open

RealAzemed wants to merge 1 commit into
fxceless:mainfrom
RealAzemed:perf/placeholder-fast-blockset-bake-flush

Conversation

@RealAzemed

Copy link
Copy Markdown

Summary

Three self-contained GPU-layer optimizations and a character decoration-bake texture-freshness fix, all gated behind env switches (default on, easily disabled for A/B) and verified with unit tests and cargo xtask policy:

1. Userspace xmem Placeholder & View-Region Fast Path (VICTORY_PLACEHOLDER_FAST=1)

  • Registers MEM_RESERVE_PLACEHOLDER reservations once in XMemVirtualAlloc via victory_gpu::title_memory_placeholder(base, size) (RwLock<BTreeMap<usize, usize>>).
  • watch::writes(..., false) returns None immediately in userspace for placeholders, avoiding doomed GetWriteWatch calls.
  • watch::guard() recognizes placeholder ranges without GetWriteWatch / VirtualQuery, skips uncommitted_end() queries on absent blocks (which only become mapped via xmem::map -> title_memory_remapped), and protect_here() marks unmapped holes directly from VIEWS while only calling VirtualProtect on contiguous mapped runs.
  • watch::protect() looks up aliased pages across [from, to) in a single VIEWS lock (aliased_pages_in) instead of per-block locking.
  • texture.rs::{committed, mapped_bytes}() answer directly from VIEWS inside placeholders (0 VirtualQuery syscalls).
  • Measured (Proton / RTX 3070 8 GB, 16 GB RAM): skips ~12,600 syscalls/frame; copies 243.6 -> 115–127 ms (-53%), heap_tex_pass 107.8 -> 22.8–38.6 ms (-79%), median frame time 733 -> 629 ms.

2. 64-Block Bitset BlockSet, Coalesced open_here, & Same-Frame Hot-Block Guarding (VICTORY_HOT_GUARD=1)

  • Replaces per-block BTreeSet<usize> in watch::Guards (protected, clean, absent, viewed) with BlockSet (BTreeMap<usize, u64>, 64 blocks = 4 MiB per u64), turning guard() into an O(words) bitwise !(clean | absent) + trailing_zeros() scan with zero allocations.
  • Coalesces contiguous VirtualProtect(..., PAGE_READWRITE) runs in open_here() with per-block fallback.
  • Tracks same-frame write faults in on_write: blocks faulting >= 4 times within the same frame stay PAGE_READWRITE for a short bounded cooldown (4 -> 8 frames, then re-probed), strictly conservative (guard() always reports them written).
  • Measured: watch 243 -> 41.1 ms (guard_scan 8.8 ms, guard_prot 36.5 ms / 425 vp, down from 64–104 ms / 675–960 vp); median frame time 629 -> 566 ms (0.90 -> 1.75 fps, +94% vs baseline).

3. Immediate Decoration-Bake Texture Flush & 8 GB VRAM bake_room()

  • Retains recent DirectStorage PAGE_WRITTEN ranges (BAKE_PAGE_WRITTEN, capped) when continuous VICTORY_PAGE_WRITES bit 2 is off, and flushes REMAPPED, PAGE_WRITTEN, and recently-uploaded/partially-mapped slots (texture_slots_bake_prepare) with 0-frame delay (due = frame) right before PS_BakeDecorations_* draws.
  • Adds resource::bake_room() (97% DXGI budget, ignoring long-term reserves during one-time bakes) so 8 GB GPUs do not permanently bake black character faces when VRAM is near the normal texture limit.

Verification

  • cargo xtask policy passes (181 files ok)
  • cargo check --target x86_64-pc-windows-gnu --tests -p victory-gpu -p victory-hle passes
  • New unit tests for placeholder_fast_path_tracks_mapped_bytes_and_blocks, blockset_range_and_run_operations_match_exact_membership, and immediate texture_slots_remapped / slots_written_under bake flush pass under Proton.

@Proudnoob4

Copy link
Copy Markdown

Tested on Windows 10 (i5-13600K + RTX 4080 16 GB, driver 621.01, Series X) stacked on Xeltax/VIcky vicky/rt b18fce0d (RT40b live BVH RT) alongside #1014, #1015, #1029, #1044, #1058, #1059 (NtReadVirtualMemory + Deferred::clear), and #1094 (420s warm-cache run, exit 0, reached frame 3072):

  • Placeholder fast-path skips (VICTORY_PLACEHOLDER_FAST=1): watch 8,396–8,902, absent 52,282–53,173, tex_query 2,126–2,664 per frame (~63k+ syscalls skipped/frame)
  • watch breakdown (BlockSet + VICTORY_HOT_GUARD=1): 9.2–10.9 ms/frame (gww 0.0 ms/29–31, guard_scan 1.5–2.1 ms, guard_prot 2.6–3.5 ms / 297–322 vp, hot_skip 723–766)
  • copies & kernel time (vs prior 420s vickyrt_fast without GPU: userspace placeholder fast path, 64-block BlockSet guards & immediate decoration-bake texture flush #1097/RT40b): copies dropped from 104–110.8 ms down to 55.9 ms (197.9 MB); submit thread kernel time dropped 30.6–37.6 ms -> 9.1 ms; page faults dropped 1,186–5,351 -> 235; framelog median 168 ms (shortest 137 ms, world peak 6.1 fps).
  • All 82 targeted victory-gpu unit tests (including watch::tests) and 12 xmem tests passed on Windows 10 before the run.

Note this run measured #1097 bundled with RT40b + the PRs above rather than standalone.

AI-assisted benchmark and analysis (Gemini 4 Neon).

@Ahmet-Dedeler

Copy link
Copy Markdown

Data point from macOS (CrossOver 26.3 + D3DMetal 4.0b3, M5 Max, fxceless 0b0074e base), from our play logs.

The flush here only fires for the four hashes in BAKE_PIXEL_SHADERS. Two decoration-blend programs show up in every one of our sessions and are not in that list: d126bcae5d5d8d26 (PS_DecorationBlend_0000) and fcee612943c008e0 (PS_DecorationBlend_0011), first bound in 5 of 5 logs. Reading the PSV0 entry names in shaders.rpf gives 322 PS_BakeDecorations* and 17 PS_DecorationBlend* pixel programs, and the list has 4 of them, so matching by entry name might cover a lot more than a hash list.

Also, early_textures runs one heap_textures pass of at most 4,096 slots, but due_at_the_last_pass on the first four bake draws was 1,033-14,183 across 7 sessions (5,398 / 5,342 / 10,177 in one run, 12,102 / 12,441 / 14,183 in another). So even with due = frame, one pass leaves thousands of slots for after the bake.

We have a branch that matches by entry name and loops passes until nothing is due, but it's not tested in game yet, so take this as numbers only. If someone tries a loop, #594's point about bake pre-uploads getting expensive when bakes repeat probably applies too.

Copy link
Copy Markdown

Windows memory observation on an RTX 3060 12 GB with 32 GB system RAM. I tested a8bcf21 in a local integration of 9c184b3 with #253 and the 16 KiB readback option from #1164.

With the usual preload settings, live texture staging went from 70 MiB to 1,420 MiB between two diagnostic samples during loading. Our external guard stopped the run at 253 seconds, with about 8,769 MiB of physical RAM free but only 1,727 MiB of commit headroom. This is a combined build and the observation does not isolate #1097 as the cause.

A warmed retry with VICTORY_BAKE_TEXTURES_MS=100, VICTORY_TEXTURE_STAGE_MB=128, and VICTORY_STAGING_POOL_MB=64 reached the checkpoint and finished a 440-second run. I also tried an opt-in local patch that pauses optional heap texture preloads below 3,200 MiB of commit headroom. With the usual preload settings it logged the pause, recovered from 2,907 to 4,629 MiB between periodic samples, and reached the checkpoint. The exterior was more washed out in that launch. Both launches were around 5 FPS after warming up.

AI-assisted with OpenAI Codex.

@rafaconvaitis

Copy link
Copy Markdown

I measured a narrower view-protection experiment on native Windows/D3D12, RTX 4070 driver 616.64, Series S, Flamingo 63d976b with VIEW_SET. It keeps the first broad VirtualProtect call. After a failure, it retries only multi-block runs inside each distinct registered view, then uses the original per-block fallback. The Windows fixture passed in both modes and cut the calls from 5 to 3 for two adjacent 128 KiB mapped views, with the same write stamps.

In the controllable prison window, the same process and fixed camera alternated OFF/ON every 20 s. Three complete segments and 300 presents per mode, excluding cross-boundary samples:

OFF ON
Effective FPS 7.357 7.432
P95 frame time 183.2 ms 186.05 ms
Median write-watch guard time per frame report 10.315 ms 10.185 ms
Median failed wide protections per fallback report 1,837 1,831
Median per-block retries per fallback report 15,583 15,452
Median grouped retries per fallback report 0 0

I found no useful gain from batching only the successfully identified multi-block views in this native scene. This did not test the PR's placeholder-hole skip, bitsets or texture changes. The block retry counts include successful protections as well as failures, so they do not estimate how many retries a trusted hole skip could remove.

@RealAzemed

Copy link
Copy Markdown
Author

Thanks @Ahmet-Dedeler, @alpz1000, and @rafaconvaitis for the cross-platform data points — super useful context:

  • @Ahmet-Dedeler (macOS / CrossOver / M5 Max): Great catch on the two PS_DecorationBlend_* hashes (d126bcae5d5d8d26, fcee612943c008e0) and due_at_the_last_pass exceeding 4,096 slots on early bakes. Matching PS_BakeDecorations* / PS_DecorationBlend* by entry name instead of a small hash list is definitely much cleaner and more general across scenes/builds — and pairing a multi-pass drain with a bounded time cap (like VICTORY_BAKE_TEXTURES_MS) avoids the VICTORY_BAKE_TEXTURES_MS default: the decoration-bake draws repeat (128+), world fps halves and load +110 s (Linux/Proton, RTX 4070 Ti) #594 repeat-bake stall. Happy to see that approach land or follow up with entry-name matching!
  • @alpz1000 (Win10 RTX 3060 commit headroom): Helpful note on VICTORY_TEXTURE_STAGE_MB=128, VICTORY_STAGING_POOL_MB=64, and VICTORY_BAKE_TEXTURES_MS=100. Just to confirm: resource::bake_room() here only relaxes the VRAM budget check during bake draws (it does not uncap staging buffers), so capping staging/pool size is a great companion lever on Windows when commit headroom is tight.
  • @rafaconvaitis (Win10 RTX 4070): Really clean A/B — that confirms batching adjacent multi-block VirtualProtect calls alone isn't where the win comes from; the measurable speedup in GPU: userspace placeholder fast path, 64-block BlockSet guards & immediate decoration-bake texture flush #1097 (e.g. @Proudnoob4's ~63k syscalls/frame skipped and copies 104–110 ms -> 55.9 ms on Win10) comes from skipping GetWriteWatch / VirtualQuery on unmapped placeholder holes in userspace plus the 64-block BlockSet scan and same-frame VICTORY_HOT_GUARD.

myokex added a commit to myokex/victory-amd that referenced this pull request Nov 26, 2026
…ts as bit blocks, counters; s2 switches (g22-g24)

Measured (g24, VICTORY_JOURNAL_FREE=1 with the bit blocks): 158.9 ms a
frame at 7-10k draws against 179.1 (g23) and 167.5 (g21); copies 45.7 ms
against 54.4; journal waits 124 -> 5 ms a frame.

- watch.rs: COSTS counters (lock waits, kernel askings, guard passes,
  questions to Windows, faults), logged on the frame line.
- watch.rs: protected/clean/absent/viewed guard sets kept as 64-block
  words (`Blocks`); a guard pass walks only the blocks not known clean.
  Idea from RealAzemed (fxceless#1097).
- VICTORY_JOURNAL_FREE=1: the journal's lock is let go while the kernel
  is asked about written pages; askings in flight are waited for by
  range. Idea from Joshua Odom (JoshuuDev/victory-amd 3b270b1).
- texture.rs: why textures are read again, counted per reason.
- Off by default, not yet measured in a game:
  - VICTORY_HOLES_KEEP (+ _FRAMES): placeholder holes kept until a remap
    or write hook (ideas from Peppacodes e277b98, cc642f9);
  - VICTORY_OPEN_RUNS: one VirtualProtect per run (idea from
    Marvisak/VIctory PR 2849);
  - VICTORY_VIEW_RECHECK=N: aliases unguarded, clean blocks made due
    every N frames (idea from d55223b);
  - VICTORY_SYNC_SKIP: sync_heaps skips unchanged 64 KiB pages (ideas
    from naixue2233 d695015/c6060592, UwUCammie PR 1043, Nazar Ukolov
    7f475db6);
  - VICTORY_TEXTURE_MISSING_WAIT, VICTORY_TEXTURE_TURN,
    VICTORY_TEXTURE_SKIP_FAILED_ASK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ahmet-Dedeler

Copy link
Copy Markdown

I checked the copy at the end of the decoration bake separately on an M5 Max (CrossOver 26.3, D3DMetal 4.0b3). A direct copy from a 64×64 R32G32B32A32_UINT texture to a 256×256 BC3_UNORM_SRGB texture preserved all 65,536 bytes. The buffer path did too. Both passed with a CPU upload and with the GPU writing the source through a UAV clear.

This covers mip 0 on committed textures. For the black clothing I’d check what the compressor actually wrote and which texture the draw sampled before assuming the driver dropped the format copy.

Standalone probe and results

@RealAzemed

Copy link
Copy Markdown
Author

Thanks @Ahmet-Dedeler — super useful standalone probe! Confirming that direct R32G32B32A32_UINT -> BC3_UNORM_SRGB preserves all 65,536 bytes on D3DMetal rules out a dropped format copy on the driver side, which lines up nicely with #1241 (copying GPU texture aliases even when a list already uploaded the view from title memory) as a real culprit for black clothing/baked textures.

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.

5 participants