feat(perf): page-granular texture detile for watched memory #489

Open
github-bot wants to merge 2 commits from github-bot/gh-489 into main AGit
Owner

@⁠cerealfordinner on GitHub · 2026-12-04 18:19 UTC · original
cerealfordinner:pr-texture-detile-fix → main · +22 −12 · 2 files · 2 commits · open

Only detile texture levels whose hashes actually changed, instead of detiling the whole texture whenever any page is written. Uses the existing refresh_levels() infrastructure that already tracks per-level hashes and versions. Falls back to full detile when constraints aren't met (non-Tex2D, >32 footprints, etc.).

Note: on real write-watched memory this path still takes the whole-detile fallback. refresh_levels needs inplace::texture_stamp for every level, and that only answers when runtime section views cover the whole range; GetWriteWatch refuses view ranges. Until texture_stamp covers watched allocations this is correctness-preserving groundwork with no perf change in the watched case.

Signed-off-by: cerealfordinner [email protected]

**@⁠cerealfordinner** on GitHub · 2026-12-04 18:19 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/489) `cerealfordinner:pr-texture-detile-fix` → `main` · +22 −12 · 2 files · 2 commits · open Only detile texture levels whose hashes actually changed, instead of detiling the whole texture whenever any page is written. Uses the existing refresh_levels() infrastructure that already tracks per-level hashes and versions. Falls back to full detile when constraints aren't met (non-Tex2D, >32 footprints, etc.). Note: on real write-watched memory this path still takes the whole-detile fallback. `refresh_levels` needs `inplace::texture_stamp` for every level, and that only answers when runtime section views cover the whole range; `GetWriteWatch` refuses view ranges. Until `texture_stamp` covers watched allocations this is correctness-preserving groundwork with no perf change in the watched case. Signed-off-by: cerealfordinner <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 18:20:10 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-04 19:03 UTC · original

Checked 3fc3ab4 on native Windows (one NVIDIA desktop card plus WARP, real GetWriteWatch, didn't run the game).

cargo test -p victory-gpu --lib is the same here as on main. 348 pass on both, and the same 5 fail on both, all of them ones that already fail natively for me (the two indirect::host_list_*, the two pipeline::host library loads, one watch test).

As far as I can tell the new branch never runs on real write-watched memory. refresh_levels needs inplace::texture_stamp for every level (texture.rs:1708), and that only answers when runtime section views cover the whole range (inplace.rs:586). GetWriteWatch only works on MEM_WRITE_WATCH allocations, and those aren't views. A view range can't get into the ahead journal either, because GetWriteWatch refuses it (watch.rs:681, 1003). So the watched path always gets Levels::Whole and does the old whole detile.

I tested it with a throwaway test. It sets up a tiled 1024x1024 BC7 Tex2D with 11 mips (6 of them in the tail) in write-watched memory and writes into mip 0, mip 1, mip 3 and the tail. I ran it with and without ask_once, and also with the texture asked together with other ranges. Every write still copies and detiles all 1,400,832 bytes, the same as main, and the staging buffer matches a full detile. NVIDIA and WARP with the debug layer give the same result.

If the branch did run, I think it would be slower than main. texture.rs:1864 resets last whenever last.levels isn't empty, and refresh_levels always fills last.levels (1714). So after one Done, every later check starts with asked at 0 and no level hashes, skips the "nothing written" return at 1867, and copies, hashes and detiles every level again. I forced the branch by also registering the write-watched range as a runtime view in the test. On this branch it read 1,400,832 bytes and marked every level dirty on all 9 checks, including the 4 with nothing written. Main read 0 bytes on those 4. Because of that reset I couldn't check whether a write to only one level, or only to the tail, gets detected on this path.

One more thing, from reading only. previous is last.asked after it has already been set to now (1870-1871). So the clean test in refresh_levels (1725) treats every written page as clean. The reset hides this right now. But if someone fixes only the reset, writes could get skipped. The view branch passes the old asked and sets asked = now only after Done (1904-1906).

The [profile.perf] fat LTO change in Cargo.toml isn't related to the rest of the PR. I'd split it out.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-04 19:03 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/489#issuecomment-7421327730) Checked 3fc3ab4 on native Windows (one NVIDIA desktop card plus WARP, real GetWriteWatch, didn't run the game). `cargo test -p victory-gpu --lib` is the same here as on main. 348 pass on both, and the same 5 fail on both, all of them ones that already fail natively for me (the two `indirect::host_list_*`, the two `pipeline::host` library loads, one watch test). As far as I can tell the new branch never runs on real write-watched memory. `refresh_levels` needs `inplace::texture_stamp` for every level (texture.rs:1708), and that only answers when runtime section views cover the whole range (inplace.rs:586). GetWriteWatch only works on `MEM_WRITE_WATCH` allocations, and those aren't views. A view range can't get into the ahead journal either, because GetWriteWatch refuses it (watch.rs:681, 1003). So the watched path always gets `Levels::Whole` and does the old whole detile. I tested it with a throwaway test. It sets up a tiled 1024x1024 BC7 Tex2D with 11 mips (6 of them in the tail) in write-watched memory and writes into mip 0, mip 1, mip 3 and the tail. I ran it with and without `ask_once`, and also with the texture asked together with other ranges. Every write still copies and detiles all 1,400,832 bytes, the same as main, and the staging buffer matches a full detile. NVIDIA and WARP with the debug layer give the same result. If the branch did run, I think it would be slower than main. texture.rs:1864 resets `last` whenever `last.levels` isn't empty, and `refresh_levels` always fills `last.levels` (1714). So after one `Done`, every later check starts with `asked` at 0 and no level hashes, skips the "nothing written" return at 1867, and copies, hashes and detiles every level again. I forced the branch by also registering the write-watched range as a runtime view in the test. On this branch it read 1,400,832 bytes and marked every level dirty on all 9 checks, including the 4 with nothing written. Main read 0 bytes on those 4. Because of that reset I couldn't check whether a write to only one level, or only to the tail, gets detected on this path. One more thing, from reading only. `previous` is `last.asked` after it has already been set to `now` (1870-1871). So the `clean` test in `refresh_levels` (1725) treats every written page as clean. The reset hides this right now. But if someone fixes only the reset, writes could get skipped. The view branch passes the old `asked` and sets `asked = now` only after `Done` (1904-1906). The `[profile.perf]` fat LTO change in Cargo.toml isn't related to the rest of the PR. I'd split it out. AI-assisted: yes
Author
Owner

@⁠cerealfordinner on GitHub · 2026-12-04 22:15 UTC · original


Thanks for the detailed review — all three points are fixed in this branch now:

  1. Watched path never triggers — confirmed the cause: inplace::texture_stamp needs every page covered by runtime section views, and GetWriteWatch refuses view ranges (watch.rs:749). On real write-watched memory this path still can't run until inplace::texture_stamp itself covers watched allocations; that's outside this PR's scope, but nothing regresses here.

  2. Performance regression — removed if !last.levels.is_empty() { *last = Last::default(); } from the write-watch path (texture.rs:1864). Level hashes now persist across checks, so levels with nothing written since the last refresh skip via the "nothing written" early return at 1867 instead of re-detiling everything.

  3. clean-test bug — capture let previous = last.asked; before last.asked = now; and pass it as previous to refresh_levels(...), so the clean test compares writes against the previous asked watermark rather than the current one.

  4. Unrelated Cargo.toml change — dropped the [profile.perf] fat LTO block from Cargo.toml entirely.

Build passes (cargo build --release -p victory, exit 0, warnings only).

Pushed to cerealfordinner:victory-xmem-hw-rt@pr-texture-detile-fix.

Signed-off-by: cerealfordinner [email protected]

**@⁠cerealfordinner** on GitHub · 2026-12-04 22:15 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/489#issuecomment-7425730282) --- Thanks for the detailed review — all three points are fixed in this branch now: 1. **Watched path never triggers** — confirmed the cause: `inplace::texture_stamp` needs every page covered by runtime section views, and `GetWriteWatch` refuses view ranges (`watch.rs:749`). On real write-watched memory this path still can't run until `inplace::texture_stamp` itself covers watched allocations; that's outside this PR's scope, but nothing regresses here. 2. **Performance regression** — removed `if !last.levels.is_empty() { *last = Last::default(); }` from the write-watch path (texture.rs:1864). Level hashes now persist across checks, so levels with nothing written since the last refresh skip via the "nothing written" early return at 1867 instead of re-detiling everything. 3. **clean-test bug** — capture `let previous = last.asked;` before `last.asked = now;` and pass it as `previous` to `refresh_levels(...)`, so the clean test compares writes against the previous asked watermark rather than the current one. 4. **Unrelated Cargo.toml change** — dropped the `[profile.perf]` fat LTO block from Cargo.toml entirely. Build passes (`cargo build --release -p victory`, exit 0, warnings only). Pushed to `cerealfordinner:victory-xmem-hw-rt@pr-texture-detile-fix`. Signed-off-by: cerealfordinner <[email protected]>
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-04 22:20 UTC · original

Read cad3ed4, thanks. The two fixes look right to me. previous is now taken before asked moves, the level state survives between checks, and the Whole arm clears levels after a full detile so a later partial refresh doesn't trust stale hashes. The perf profile is gone too.

I didn't rerun it, because as you say the new arm still can't be reached on write-watched memory, so on the path the PR targets it does the same work as main. Maybe say that in the description until texture_stamp covers watched allocations, so nobody expects a saving from it yet.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-04 22:20 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/489#issuecomment-7425823211) Read cad3ed4, thanks. The two fixes look right to me. `previous` is now taken before `asked` moves, the level state survives between checks, and the Whole arm clears `levels` after a full detile so a later partial refresh doesn't trust stale hashes. The perf profile is gone too. I didn't rerun it, because as you say the new arm still can't be reached on write-watched memory, so on the path the PR targets it does the same work as main. Maybe say that in the description until `texture_stamp` covers watched allocations, so nobody expects a saving from it yet. AI-assisted: yes
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/489/head:github-bot/gh-489'
git switch 'github-bot/gh-489'

Merge

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