GPU/texture: detile only the pages written since the last refresh (VICTORY_TEXTURE_PAGES) #1127

Open
Lkxex wants to merge 1 commit from upstream-pr/1130 into main
Lkxex commented 2026-11-25 21:04:04 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Lkxex on 2026-11-25T21:04:04Z.


GPU/texture: detile only the pages written since the last refresh (VICTORY_TEXTURE_PAGES)

Problem

crates/victory-gpu/src/texture.rs documented its own shortcut: "a change
detiles the whole texture, not the pages written." In the write-watched
refresh path, refresh_timed read the entire texture (copy_mapped of
size bytes) and detiled all of it (self.detile(read)), even when the
title had written only a handful of 64 KiB pages. On 8 GB cards this is a
large part of the "preparing" time (issue reports ~1.6 s/frame) and of the
bytes touched per refresh. The write-watch already reported which pages
changed (watch::writes returns per-page write times), but that information
was only used as an all-or-nothing "did anything change" gate.

Change

  • victory-formats: add Layout::detile_window, which detiles a window of
    the texture's tiled memory (starting at byte window, shorter than the
    whole texture) into the staging buffer, leaving bytes outside the window
    untouched. The walk is restricted per row and per run, so only elements
    whose tiled address falls in the window are written.
  • victory-gpu: record, per staging buffer, the byte range each subresource
    (mip + slice * mips) occupies in title memory (ranges), including the
    shared tail block. Add refresh_pages, which merges the pages written
    since the last asking into 64 KiB runs, reads each run once, and detiles
    the overlapping subresources via detile_window. Wire it into
    refresh_timed's write-watched branch (only when previous != 0), falling
    back to the whole-texture path on 3D textures, on-demand levels, missing
    range tables, or VICTORY_TEXTURE_PAGES=0.

Correctness

  • The first refresh (previous == 0) and the partly-mapped (hash) path keep
    the whole-texture behaviour, so a texture's first sight and streaming-in
    mips are unaffected.
  • 3D textures, on-demand levels, XOR/MSAA and count mismatches keep
    ranges = None and take the existing whole-texture path.
  • VICTORY_TEXTURE_PAGES=0 restores the previous behaviour.
  • Formats crate: 25 tests pass, including the existing bijection/round-trip
    tests; detile_window of a whole window reproduces detile.
  • victory-gpu: 183 lib tests pass; build clean.

Limitations

  • GPU runtime / real hardware not available; the claim is about bytes read
    and detiled in the synthetic CPU path, not measured frame time.
  • The host-texture upload still copies every subresource; subresource-
    granular GPU copies are a separate follow-up.

Assisted By NetherySiloX

<!-- greyfinch-sync:fxceless/victory-fork:issue:5499145340 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1130). Originally posted by [Lkxex](https://github.com/Lkxex) on 2026-11-25T21:04:04Z. --- GPU/texture: detile only the pages written since the last refresh (VICTORY_TEXTURE_PAGES) Problem ------- `crates/victory-gpu/src/texture.rs` documented its own shortcut: "a change detiles the whole texture, not the pages written." In the write-watched refresh path, `refresh_timed` read the *entire* texture (`copy_mapped` of `size` bytes) and detiled all of it (`self.detile(read)`), even when the title had written only a handful of 64 KiB pages. On 8 GB cards this is a large part of the "preparing" time (issue reports ~1.6 s/frame) and of the bytes touched per refresh. The write-watch already reported *which* pages changed (`watch::writes` returns per-page write times), but that information was only used as an all-or-nothing "did anything change" gate. Change ------ - `victory-formats`: add `Layout::detile_window`, which detiles a window of the texture's tiled memory (starting at byte `window`, shorter than the whole texture) into the staging buffer, leaving bytes outside the window untouched. The walk is restricted per row and per run, so only elements whose tiled address falls in the window are written. - `victory-gpu`: record, per staging buffer, the byte range each subresource (mip + slice * mips) occupies in title memory (`ranges`), including the shared tail block. Add `refresh_pages`, which merges the pages written since the last asking into 64 KiB runs, reads each run once, and detiles the overlapping subresources via `detile_window`. Wire it into `refresh_timed`'s write-watched branch (only when `previous != 0`), falling back to the whole-texture path on 3D textures, on-demand levels, missing range tables, or `VICTORY_TEXTURE_PAGES=0`. Correctness ----------- - The first refresh (`previous == 0`) and the partly-mapped (hash) path keep the whole-texture behaviour, so a texture's first sight and streaming-in mips are unaffected. - 3D textures, on-demand levels, XOR/MSAA and count mismatches keep `ranges = None` and take the existing whole-texture path. - `VICTORY_TEXTURE_PAGES=0` restores the previous behaviour. - Formats crate: 25 tests pass, including the existing bijection/round-trip tests; `detile_window` of a whole window reproduces `detile`. - victory-gpu: 183 lib tests pass; build clean. Limitations ----------- - GPU runtime / real hardware not available; the claim is about bytes read and detiled in the synthetic CPU path, not measured frame time. - The host-texture upload still copies every subresource; subresource- granular GPU copies are a separate follow-up. Assisted By NetherySiloX
LeonardoS11 commented 2026-11-26 00:06:10 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by LeonardoS11 on 2026-11-25T23:44:09Z.


Measured here carried over an integration branch (current main plus a few open PRs), RTX 3080 Ti 12 GB, Windows 11 26200, Series S profile, the fixed room of the prologue, 300 s runs with pipelines cached.

  • Merges cleanly and runs; no image change in our screenshots of that room.
  • Frame time stayed within run-to-run spread (107–120 ms a frame before and after). Texture refresh is about 6 ms of our frame in that scene, so there was little for it to take; a scene that streams textures harder should show more than ours does.

Written with Claude Code.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7177975561 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1130#issuecomment-7177975561). Originally posted by [LeonardoS11](https://github.com/LeonardoS11) on 2026-11-25T23:44:09Z. --- Measured here carried over an integration branch (current main plus a few open PRs), RTX 3080 Ti 12 GB, Windows 11 26200, Series S profile, the fixed room of the prologue, 300 s runs with pipelines cached. - Merges cleanly and runs; no image change in our screenshots of that room. - Frame time stayed within run-to-run spread (107–120 ms a frame before and after). Texture refresh is about 6 ms of our frame in that scene, so there was little for it to take; a scene that streams textures harder should show more than ours does. Written with Claude Code.
LeonardoS11 commented 2026-11-26 06:07:03 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by LeonardoS11 on 2026-11-26T05:52:58Z.


A correction to my comment above, and a defect report. I wrote that the image did not change in our room; that room has almost no texture the title rewrites, so it could not have shown this. Reading the code since (not yet confirmed by a picture; we are checking that now):

In 4720e4c (VICTORY_TEXTURE_PAGES, on by default), refresh_pages() in crates/victory-gpu/src/texture.rs intersects each subresource's range with a run of written pages (let a = rs.max(run_start as u64); let b = re.min((run_start + run_len) as u64);, around line 1344), but the ranges are built as offsets in the texture's memory (base + start, base = slice * slice_size, around line 1741) while run_start is an absolute title address (page * PAGE, clamped to self.memory). The two never meet, so a >= b for every subresource and no page is detiled, yet the function still marks the texture dirty and returns Some(read). A watched texture the title rewrites would keep its old texels from its second refresh on, and the frame stops paying for the detile it skips, which also flatters any timing taken with the switch on.

The fix we carry: intersect in the texture's offsets — let run_at = run_start - self.memory as usize;, then a = rs.max(run_at), b = re.min(run_at + run_len), slicing buf[(a - run_at)..(b - run_at)] — and pass that offset a to detile_window(), which takes the window's offset within the texture. VICTORY_TEXTURE_PAGES=0 reads the whole texture as before.

Written with Claude Code.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7183890633 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1130#issuecomment-7183890633). Originally posted by [LeonardoS11](https://github.com/LeonardoS11) on 2026-11-26T05:52:58Z. --- A correction to my comment above, and a defect report. I wrote that the image did not change in our room; that room has almost no texture the title rewrites, so it could not have shown this. Reading the code since (not yet confirmed by a picture; we are checking that now): In 4720e4c (`VICTORY_TEXTURE_PAGES`, on by default), `refresh_pages()` in `crates/victory-gpu/src/texture.rs` intersects each subresource's range with a run of written pages (`let a = rs.max(run_start as u64); let b = re.min((run_start + run_len) as u64);`, around line 1344), but the ranges are built as offsets in the texture's memory (`base + start`, base = slice * slice_size, around line 1741) while `run_start` is an absolute title address (page * PAGE, clamped to `self.memory`). The two never meet, so `a >= b` for every subresource and no page is detiled, yet the function still marks the texture dirty and returns `Some(read)`. A watched texture the title rewrites would keep its old texels from its second refresh on, and the frame stops paying for the detile it skips, which also flatters any timing taken with the switch on. The fix we carry: intersect in the texture's offsets — `let run_at = run_start - self.memory as usize;`, then `a = rs.max(run_at)`, `b = re.min(run_at + run_len)`, slicing `buf[(a - run_at)..(b - run_at)]` — and pass that offset `a` to `detile_window()`, which takes the window's offset within the texture. `VICTORY_TEXTURE_PAGES=0` reads the whole texture as before. Written with Claude Code.
D4rkGh057 commented 2026-11-26 21:21:16 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by D4rkGh057 on 2026-11-26T21:21:16Z.


Hi, I read the diff of this PR (code only, I have not built or run it). I am looking at it for the plan in avatarmanz/VIctory-7900XTX#358, and the page-granular detile looks like a better fit than the per-mip split proposed there. Two questions I could not answer from the diff:

  1. Row skip in walk. The check row_base + bytes <= start treats a row as bytes contiguous bytes of tiled memory. In a swizzled layout the elements of one row are spread over several blocks, so the runs of a row can sit well past row_base + bytes. Could a row whose runs fall inside the window be skipped and keep stale data? Was it tried with a partial window on a texture wider than one block?
  2. Tests. I could not find a new test in the diff. A whole window cannot reach the row skip, so a test that detiles in two or three partial windows and compares with the full detile would settle question 1. Also, Swizzle::SW_256B_S disappears from the list in tiled_chunks_match_element_offsets_with_odd_row_pitches: is that intended?

Smaller notes: copy_runs (the 8/16/32-byte chunked path) is removed and the full detile used it, which may be an artifact of the branch base; and last.asked is set before detiling, so pages whose detile fails are logged but not retried.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7203643776 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1130#issuecomment-7203643776). Originally posted by [D4rkGh057](https://github.com/D4rkGh057) on 2026-11-26T21:21:16Z. --- Hi, I read the diff of this PR (code only, I have not built or run it). I am looking at it for the plan in avatarmanz/VIctory-7900XTX#358, and the page-granular detile looks like a better fit than the per-mip split proposed there. Two questions I could not answer from the diff: 1. **Row skip in `walk`.** The check `row_base + bytes <= start` treats a row as `bytes` contiguous bytes of tiled memory. In a swizzled layout the elements of one row are spread over several blocks, so the runs of a row can sit well past `row_base + bytes`. Could a row whose runs fall inside the window be skipped and keep stale data? Was it tried with a partial window on a texture wider than one block? 2. **Tests.** I could not find a new test in the diff. A whole window cannot reach the row skip, so a test that detiles in two or three partial windows and compares with the full `detile` would settle question 1. Also, `Swizzle::SW_256B_S` disappears from the list in `tiled_chunks_match_element_offsets_with_odd_row_pitches`: is that intended? Smaller notes: `copy_runs` (the 8/16/32-byte chunked path) is removed and the full detile used it, which may be an artifact of the branch base; and `last.asked` is set before detiling, so pages whose detile fails are logged but not retried.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1130:upstream-pr/1130'
git switch 'upstream-pr/1130'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1130'
git switch 'upstream-pr/1130'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1130'
git switch 'upstream-pr/1130'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1130'
git switch 'main'
git merge --squash 'upstream-pr/1130'
git switch 'main'
git merge --ff-only 'upstream-pr/1130'
git switch 'main'
git merge 'upstream-pr/1130'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
fxceless/victory-fork!1127
No description provided.