GPU: skip CPU BLAKE3 texture hashing via xmem dirty-generation digests (VICTORY_TEXTURE_DIRTY_GEN=1) #1018

Open
github-bot wants to merge 6 commits from github-bot/gh-1018 into main AGit
Owner

@⁠HailLucifer666 on GitHub · 2026-11-25 16:21 UTC · original
HailLucifer666:perf/texture-dirty-gen-skip-blake3 → main · +1181 −35 · 4 files · 6 commits · open

Stacked on #231 -> #872: merge after #872.

Adds opt-in (VICTORY_TEXTURE_DIRTY_GEN=1, off by default) dirty-generation digest skipping in exture.rs using xmem::va_generation_digest via ictory_gpu::set_page_gen_oracle, plus VICTORY_TEXTURE_HASH_ORACLE=1 to detect any uncounted writes.

Verification

  • cargo test -p victory-gpu texture:: --locked -> 3 passed, 0 failed (dirty_gen_skips_rehash_when_digest_unchanged_and_detiles_when_bumped).
  • cargo test -p victory-hle xmem:: --locked -> 23 passed, 0 failed ( a_generation_digest_tracks_mapped_views_and_rejects_gaps).
  • cargo check -p victory --locked -> pass.
  • /kee mutation probe: ignoring last.gen_digest == Some(digest) fails dirty_gen_skips_rehash_when_digest_unchanged_and_detiles_when_bumped; restored byte-identical.
**@⁠HailLucifer666** on GitHub · 2026-11-25 16:21 UTC · [original](https://github.com/fxceless/victory-fork/pull/1018) `HailLucifer666:perf/texture-dirty-gen-skip-blake3` → `main` · +1181 −35 · 4 files · 6 commits · open **Stacked on #231 -> #872: merge after #872.** Adds opt-in (VICTORY_TEXTURE_DIRTY_GEN=1, off by default) dirty-generation digest skipping in exture.rs using xmem::va_generation_digest via ictory_gpu::set_page_gen_oracle, plus VICTORY_TEXTURE_HASH_ORACLE=1 to detect any uncounted writes. ### Verification - cargo test -p victory-gpu texture:: --locked -> **3 passed, 0 failed** (dirty_gen_skips_rehash_when_digest_unchanged_and_detiles_when_bumped). - cargo test -p victory-hle xmem:: --locked -> **23 passed, 0 failed** ( a_generation_digest_tracks_mapped_views_and_rejects_gaps). - cargo check -p victory --locked -> pass. - /kee mutation probe: ignoring last.gen_digest == Some(digest) fails dirty_gen_skips_rehash_when_digest_unchanged_and_detiles_when_bumped; restored byte-identical.
THE BUG.
write_physical and page_window reached the physical pages through View::map,
which mapped a fresh view of the section, committed it with
VirtualAlloc(MEM_COMMIT), wrote, and unmapped it in Drop for View. Every write
therefore paid a MapViewOfFile, a commit and an UnmapViewOfFile however few
bytes it touched, and paid it once per run of consecutive pages.

WHY NOT HYPOTHETICAL.
Both functions have exactly one production caller: the DirectStorage
destination path in dstorage.rs, which tries page_window first and falls back
to write_physical. That is the streaming path, called once per in-flight
request on every worker, for the whole world load and for every texture the
title streams while playing. An unmap invalidates that mapping's translation
entries on every processor, so the cost is charged to all cores rather than
to the writer alone. Which writes are hot enough to move a frame rate is a
measurement on hardware this tree cannot reach, and it is not claimed here;
the saving is real per write either way.

THE FIX.
Section keeps one read-write view of its whole range, mapped on first use and
unmapped when the section goes (MappedBase, dropped with the Section), plus
one bit per 64 KiB page recording which pages have been committed through it.
View becomes an enum: Temporary is the old map/commit/unmap path, Persistent
points into the section's view and unmaps nothing when it drops.
Section::ensure_committed commits a run in a single call the first time and
makes no calls after, reusing commit() so a host at its commit limit still
produces the same crash report. Every half of the persistent path that the
host can refuse -- the whole-section map, the commit -- falls back to the
temporary view, so the worst case is exactly today's behaviour.

Mapping is the seam that lets the tests force either path: the switch is read
once per process through a OnceLock, so a test cannot set it for itself.

Opt-in with VICTORY_XMEM_PERSISTENT_VIEW=1, matching the other rollout
switches in this tree. It holds a whole-section mapping for as long as the
section lives, which is 6.1 GiB of address space for the title's largest
allocation; that should be proven by a run before it becomes the default.

SCOPE.
xmem.rs only. The default path is byte-identical to before: View::temporary is
the old View::map body, and every test that existed before this change is the
negative control for the persistent one. Deliberately untouched:
XMemMapPhysicalPages, its placeholder mappings and the unmap() bookkeeping,
which the persistent view does not appear in. Deliberately recorded instead of
tested: the committed_all early return in ensure_committed is a saved call and
not a guard, so it is documented as the one surviving mutation rather than
given a test that could not fail.

Verified: cargo test -p victory-hle xmem:: --locked 16 passed (5 new, all
executing); cargo clippy --workspace --all-targets --locked -- -D warnings
clean; the same with --all-features clean; cargo test -p xtask --locked 4
passed. Mutation probes, all run 2026-11-25 and restored: mark_committed made
a no-op fails the two committed_all assertions and nothing else; the commit
call removed makes the persistent write fault with STATUS_ACCESS_VIOLATION,
which is what proves the commit is load-bearing; the committed_all fast path
forced off leaves 16 of 16 green, which is why it is documented as surviving.

Co-Authored-By: DeepSeek V4.1 Flash <[email protected]>
Review of the commit before this one found five things worth fixing. All of
them are about the evidence the change rests on, not about the mapping.

THE BUG.
Three of them are claims the earlier commit message or comments got wrong.

The comment on `View::bytes` said two writers through the section's one
mapping write "exactly as they did through separate temporary views". They do
not. Through separate virtual views two writes to the same bytes were a data
race; through one shared base two live `&mut [u8]` over overlapping bytes is
undefined behaviour, and the compiler may assume they do not alias. The
precondition -- no two writes in flight cover the same page -- is real, is
stated in `dstorage` for the buffer branch only, and is not stated at all for
the page branch that calls these helpers. The comment now names it as a
precondition rather than calling it unchanged behaviour.

The comment on `ensure_committed` promised that a host at its commit limit
gets "the same crash report". It gets two: the persistent commit fails and
reports, then the caller retries on the temporary path and that commit fails
and reports again. The comment says so now instead of claiming otherwise.

The `end <= self.size` comparison was described as unobserved and was
declared alongside the one surviving mutation, which understated it: it is
the only thing keeping `committed_all`'s page arithmetic inside `committed`.
It was also invisible to all 16 tests, because both "past the section" rows
are refused by the host's own `VirtualAlloc` first. A zero-length range past
the end is now asserted refused; that row the host cannot supply, so deleting
the comparison turns the test red.

WHY NOT HYPOTHETICAL.
`persistent_page_windows_share_one_mapping` passed with every write on the
temporary path. Its only evidence was `persistent_ptr()` returning the base
-- but the test itself calls that, and `View::persistent` calls it before the
commit can fail, so the `OnceLock` was populated whether or not the
persistent path ever ran. Every byte assertion still held, because the
temporary view writes the same pages. A test that cannot see the thing it is
named for is worse than no test, and this one would have been quoted as proof
that the path works.

And with the switch on, nothing said whether it was actually in effect: the
map half warns per section, the commit half said nothing, and no run could
report "these sections are persistent". That is the exact evidence the switch
becoming the default is supposed to rest on.

THE FIX.
`page_bytes`, the helper the pre-existing tests read bytes through, now asks
for `Mapping::Temporary` explicitly, so those tests are the persistent path's
negative control whatever the environment says.

`persistent_page_windows_share_one_mapping` asserts the pages were committed
through the section's mapping after the first window, which only the
persistent path does. Confirmed by probe: with `View::persistent` made to
return `None`, it now fails with "the window fell back to a temporary view"
where it previously passed.

`ensure_committed_refuses_ranges_outside_the_section` gains the zero-length
row, and the surviving-mutation note now names one mutation because only one
remains.

A `fallback_noted` on `Section` warns once per section when a commit through
the persistent view fails, and `View::persistent` says once, at info, that
pages are being written through the persistent view. A run with the switch on
can now show which it did.

SCOPE.
xmem.rs only. The whole-section-vs-chunked decision, the fallback behaviour
and the default path are unchanged. Two claims in the previous message are
corrected here rather than by rewriting it: "the default path is
byte-identical to before" is true of writes, not of allocation -- every
section now allocates its page bitset either way, a few KiB per GiB; and "the
same crash report" is two reports, as above.

Verified: cargo test -p victory-hle xmem:: --locked 16 passed; the two clippy
commands clean with and without --all-features; cargo test -p xtask 4 passed;
cargo test --workspace --locked 500 passed, 0 failed, 7 ignored.

Mutation runs 2026-11-25, all restored: `View::persistent` returning `None`
outright fails both persistent tests ("fell back to a temporary view", "not
mapped whole"); the `end <= self.size` comparison deleted fails the new
zero-length row; the earlier probes (bitset not marked, commit removed) still
behave as recorded.

Co-Authored-By: DeepSeek V4.1 Flash <[email protected]>
A review of this pull request found two things. The second is what makes the
first worth anything.

THE BUG.
`View::bytes()` built one exclusive slice over the whole mapped run, and
`write_physical` and `PageWindow::bytes` narrowed it afterwards. Through the
temporary path that was harmless: every view has an address of its own, so two
narrowed slices sat at two addresses. Through the persistent path the section
has one address, so narrowing after the fact is too late -- two requests
writing different bytes of the same page each formed a slice over the whole
page, and two live `&mut` covering the same bytes is undefined behaviour
whatever the writers actually touch.

And the disjointness was assumed, not enforced. The previous commit's comment
said the title writes each destination alone and pointed at `dstorage` for it.
The repository's own fixture (`zlib_into_pages_in_place_and_across_allocations`)
enqueues two requests whose destinations are the same pages from the same
offset, one through the in-place path and one through the buffered path. There
was nothing to point at.

THE FIX.
`View::bytes_at(from, len)` returns exactly the bytes asked for, at an offset
the caller states, on either path. Nothing here forms a slice over a whole run
any more, so two writers whose bytes do not touch hold disjoint slices even
inside one page.

Disjointness is now enforced rather than believed. `Section` records the byte
ranges its live persistent views hold; `hold` refuses a claim that touches one
of them, and `View::persistent` returns `None` when refused, so the caller
takes a temporary view, which has an address of its own. Writers whose bytes
really do overlap therefore go through two addresses and never through two
slices at one. A view gives its range back when it drops, so a section's bytes
are held only while they are in use.

SCOPE.
xmem.rs only. The temporary path is unchanged in behaviour -- its views were
already distinct addresses, and the narrowing it did implicitly with a slice
range is now explicit in the view. What this does not do: it does not make
overlapping concurrent writes correct. It makes them fall back to the path
that was correct before, so a writer overlapping another still sees whichever
write lands last, exactly as it did previously.

Verified: cargo test -p victory-hle xmem:: --locked 18 passed (2 new, both
executing); cargo clippy --workspace --all-targets --locked -- -D warnings
clean; the same with --all-features clean; cargo test -p xtask --locked 4
passed; cargo test --workspace --locked green across every crate.

Mutation runs 2026-11-25, all restored and each verified to have applied:
`bytes_at` returning the whole run fails with "left: 65536, right: 3"; its end
check removed makes the past-the-end row read on instead of refusing; the
claim skipped makes the second overlapping view `Persistent`, which is the
state that would have shared one address; and the claim never released makes
"free again" fail.

Co-Authored-By: DeepSeek V4.1 Flash <[email protected]>
The persistent view was mapped wherever the host chose, which can be inside
the address range the title itself owns (below 40 bits, decision 0001): a view
there either takes an address the title is about to ask for, or looks like a
title address to the title's own pointer arithmetic. It is now reserved with
VirtualAlloc2's address requirement from 1 TiB up to GetSystemInfo's
lpMaximumApplicationAddress (asked, never assumed) and mapped into that
reservation with MapViewOfFile3. The rule has a name, `above_title_memory`, so
a test can hold it; the host is not trusted to honour it either, and a view
that comes back below the ceiling is released and the slow path taken.

Commit state went from one bit per page to three states per page, because the
bit could not promise anything: DirectStorage workers write these sections
concurrently, and a thread that lost the race to `VirtualAlloc(MEM_COMMIT)`
would store into a page whose commit was still in flight. A page is now FREE,
COMMITTING while one thread commits it, and COMMITTED after the call returned;
a loser spins on COMMITTING instead of writing. Giving up after a bounded
number of spins takes the temporary view, which is always correct, so a thread
that died mid-commit cannot hang the title.

Each page also carries a write generation, bumped after its bytes are written:
release on the bump, acquire on the read, so an unchanged digest is a promise
about the bytes and not only about the counter. `physical_generation_digest`
folds a range into one number for the GPU side to compare, which is what lets
it stop hashing texture memory it already knows (phase F; the digest has no
caller here yet beyond its test, and says so).

Tests: the view lands above the ceiling and the rule refuses one page below
it; eight threads committing the same four uncommitted pages all succeed and
every byte they wrote is there; a digest moves on a physical write and on a
page window's bytes, and an untouched page's does not.

Probes, restored byte-identical: dropping the generation bump after a physical
write fails "a write must show"; dropping the window's bump on drop fails "a
page window's bytes must show"; making `above_title_memory` always true fails
on the page below the ceiling. Removing LowestStartingAddress from the
reservation does NOT fail any test on this host, whose allocator is already
top-down: that requirement is a belt, and the predicate is the pin.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:56:05 +00:00
Author
Owner

@⁠MKmajster on GitHub · 2026-11-25 18:47 UTC · original

For anyone who merges this alone: with VICTORY_TEXTURE_DIRTY_GEN=1 on main ca8829f + #1007 and without #872, the run ended while loading, before the world, with an access violation (a write to 0). As the description says, it needs #872 first. Without the switch the same build ran normally.

AI-assisted (Claude Code). No game data attached.

**@⁠MKmajster** on GitHub · 2026-11-25 18:47 UTC · [original](https://github.com/fxceless/victory-fork/pull/1018#issuecomment-7171398941) For anyone who merges this alone: with `VICTORY_TEXTURE_DIRTY_GEN=1` on `main` ca8829f + #1007 and without #872, the run ended while loading, before the world, with an access violation (a write to 0). As the description says, it needs #872 first. Without the switch the same build ran normally. AI-assisted (Claude Code). No game data attached.
Author
Owner

@⁠HailLucifer666 on GitHub · 2026-11-25 20:20 UTC · original

Thanks @MKmajster — that confirms the dependency documented in the PR description: VICTORY_TEXTURE_DIRTY_GEN=1 calls xmem::va_generation_digest and relies on #872's persistent high-VA alias + per-page generation counters, so this PR is stacked on #231 -> #872 and must land after #872 (not cherry-picked onto main alone).

**@⁠HailLucifer666** on GitHub · 2026-11-25 20:20 UTC · [original](https://github.com/fxceless/victory-fork/pull/1018#issuecomment-7173521317) Thanks `@MKmajster` — that confirms the dependency documented in the PR description: `VICTORY_TEXTURE_DIRTY_GEN=1` calls `xmem::va_generation_digest` and relies on `#872`'s persistent high-VA alias + per-page generation counters, so this PR is stacked on `#231` -> `#872` and must land after `#872` (not cherry-picked onto `main` alone).
github-bot force-pushed github-bot/gh-1018 from d973366023 to 606d1e68bf 2026-11-26 14:00:06 +00:00 Compare
Author
Owner

@⁠HailLucifer666 on GitHub · 2026-11-26 13:59 UTC · original

Rebased onto the new #872 head (08aa56ad); this head is now 606d1e68 (--onto, no conflicts). Verified: cargo check for victory-gpu, victory-hle and the victory app; victory-gpu texture:: 3 passed, 0 failed; clippy -D warnings on victory-gpu and victory-hle clean. It still needs #872 (and #231) to land first, as the description says.

**@⁠HailLucifer666** on GitHub · 2026-11-26 13:59 UTC · [original](https://github.com/fxceless/victory-fork/pull/1018#issuecomment-7194121802) Rebased onto the new `#872` head (`08aa56ad`); this head is now `606d1e68` (`--onto`, no conflicts). Verified: `cargo check` for victory-gpu, victory-hle and the victory app; `victory-gpu texture::` 3 passed, 0 failed; `clippy -D warnings` on victory-gpu and victory-hle clean. It still needs `#872` (and `#231`) to land first, as the description says.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1018/head:github-bot/gh-1018'
git switch 'github-bot/gh-1018'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1018'
git switch 'github-bot/gh-1018'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1018'
git switch 'github-bot/gh-1018'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1018'
git switch 'main'
git merge --squash 'github-bot/gh-1018'
git switch 'main'
git merge --ff-only 'github-bot/gh-1018'
git switch 'main'
git merge 'github-bot/gh-1018'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.