GPU: an option to refresh a texture once every N frames #2478

Open
github-bot wants to merge 1 commit from github-bot/gh-2478 into main AGit
Owner

@⁠HoLa-Lee on GitHub · 2026-11-25 14:25 UTC · original
HoLa-Lee:gpu/texture-refresh-stride → main · +29 −3 · 1 files · 1 commits · open

What this is

A world that renders at a frame a second spends most of that frame reading title memory for textures. Measured on one title and machine, on the card, with docs-level counters: 24,821 texture refreshes pulling 24.8 GB of title memory in a single frame, with none of them skipped, and 6,814 ms of the frame's 7,000 ms of texture work spent waiting for that memory. The GPU did 71-72 ms in the same frame.

VICTORY_TEXTURE_REFRESH_EVERY=<n> reads a texture once every n frames at most, round-robin by the texture's own address so the frames share the work instead of one frame taking all of it:

fn due(&self) -> bool {
    // diagnostics only; 0, 1 or unset: every frame
    every <= 1 || (crate::frame::current() + ((self.memory as usize >> 16) as u64)).is_multiple_of(every)
}

// refresh_all, before in_parallel: a texture that is not due is not handed to a thread
let changed: Vec<usize> = (0..work.len()).filter(|&i| work[i].0.due() && work[i].0.maybe_changed()).collect();

and refresh() returns None (nothing changed, keep the last upload) on the frames that are not its turn.

What it buys

At VICTORY_TEXTURE_REFRESH_EVERY=8, same scene, same captures: 145-522 ms a frame instead of 2,333-6,906 ms (0.1-0.4 fps to 1.9-6.9 fps), with the picture intact - the same frame numbers captured before and after compare.

What it costs

A texture's contents can lag by up to n frames. Unset, 0 or 1: nothing changes, every frame refreshes exactly as now, so this is inert for anyone who does not ask for it. It is a switch for slow machines and for measuring what the refresh costs, not a claim that every frame should skip reads - the proper fix (refresh only what the frame reads) is a larger change in draw.rs/host.rs.

Changes since the last review (a6d6afa)

All four review points are in, in this push (c8d6571):

  1. Behind the diagnostics feature, like the other VICTORY_* switches in capture.rs and pipeline.rs. A build that ships has due() returning true unconditionally: the OnceLock and the env read do not exist there at all.
  2. The check moved out of refresh() into refresh_all, before in_parallel - a texture that is not due this frame is no longer handed to a worker thread. refresh() keeps only its own work.
  3. The trailing newline is back, and the commit is LF-only: git diff --check is clean and the blob starts 2f 2f 21, ends 0a 7d 0a.
  4. Clippy 1.98: % every == 0 became .is_multiple_of(every) (manual_is_multiple_of), and the parse now .filter(|&n| n > 0)s so VICTORY_TEXTURE_REFRESH_EVERY=0 is "every frame" rather than something to reason about. Thanks @⁠kotekompotek and @⁠kais7654 - both of your points are the same two lines.

Checked

  • cargo check -p victory-gpu (default features): ok
  • cargo check -p victory-gpu --profile diag --features diagnostics: ok
  • cargo clippy -p victory-gpu --all-targets --all-features -- -D warnings: 0 warnings
  • cargo test -p victory-gpu --lib --profile diag --features diagnostics: 196 passed, 2 failed, 1 ignored - the 2 are the known #1744 view-heap failures (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views), which fail on main too on this machine.

The frame-time numbers above come from runs on the title (RTX 4080 SUPER, VICTORY_FAST=1, VICTORY_TEXTURE_STAGE_MB=64), not from a test.

**@⁠HoLa-Lee** on GitHub · 2026-11-25 14:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/2478) `HoLa-Lee:gpu/texture-refresh-stride` → `main` · +29 −3 · 1 files · 1 commits · open ## What this is A world that renders at a frame a second spends most of that frame reading title memory for textures. Measured on one title and machine, on the card, with docs-level counters: **24,821 texture refreshes pulling 24.8 GB of title memory in a single frame, with none of them skipped**, and **6,814 ms of the frame's 7,000 ms of texture work spent waiting for that memory**. The GPU did 71-72 ms in the same frame. `VICTORY_TEXTURE_REFRESH_EVERY=<n>` reads a texture once every n frames at most, round-robin by the texture's own address so the frames share the work instead of one frame taking all of it: ``` fn due(&self) -> bool { // diagnostics only; 0, 1 or unset: every frame every <= 1 || (crate::frame::current() + ((self.memory as usize >> 16) as u64)).is_multiple_of(every) } // refresh_all, before in_parallel: a texture that is not due is not handed to a thread let changed: Vec<usize> = (0..work.len()).filter(|&i| work[i].0.due() && work[i].0.maybe_changed()).collect(); ``` and `refresh()` returns `None` (nothing changed, keep the last upload) on the frames that are not its turn. ## What it buys At `VICTORY_TEXTURE_REFRESH_EVERY=8`, same scene, same captures: **145-522 ms a frame instead of 2,333-6,906 ms** (0.1-0.4 fps to 1.9-6.9 fps), with the picture intact - the same frame numbers captured before and after compare. ## What it costs A texture's contents can lag by up to n frames. Unset, 0 or 1: nothing changes, every frame refreshes exactly as now, so this is inert for anyone who does not ask for it. It is a switch for slow machines and for measuring what the refresh costs, not a claim that every frame should skip reads - the proper fix (refresh only what the frame reads) is a larger change in draw.rs/host.rs. ## Changes since the last review (`a6d6afa`) All four review points are in, in this push (`c8d6571`): 1. **Behind the `diagnostics` feature**, like the other `VICTORY_*` switches in `capture.rs` and `pipeline.rs`. A build that ships has `due()` returning `true` unconditionally: the `OnceLock` and the env read do not exist there at all. 2. **The check moved out of `refresh()` into `refresh_all`, before `in_parallel`** - a texture that is not due this frame is no longer handed to a worker thread. `refresh()` keeps only its own work. 3. **The trailing newline is back**, and the commit is LF-only: `git diff --check` is clean and the blob starts `2f 2f 21`, ends `0a 7d 0a`. 4. **Clippy 1.98**: `% every == 0` became `.is_multiple_of(every)` (`manual_is_multiple_of`), and the parse now `.filter(|&n| n > 0)`s so `VICTORY_TEXTURE_REFRESH_EVERY=0` is "every frame" rather than something to reason about. Thanks @⁠kotekompotek and @⁠kais7654 - both of your points are the same two lines. ## Checked - `cargo check -p victory-gpu` (default features): ok - `cargo check -p victory-gpu --profile diag --features diagnostics`: ok - `cargo clippy -p victory-gpu --all-targets --all-features -- -D warnings`: **0 warnings** - `cargo test -p victory-gpu --lib --profile diag --features diagnostics`: **196 passed, 2 failed, 1 ignored** - the 2 are the known `#1744` view-heap failures (`dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`), which fail on `main` too on this machine. The frame-time numbers above come from runs on the title (RTX 4080 SUPER, `VICTORY_FAST=1`, `VICTORY_TEXTURE_STAGE_MB=64`), not from a test.
A world that renders at a frame a second spends most of that frame reading
title memory for textures. Measured on one title and machine, on the card:
24,821 texture refreshes pulling 24.8 GB of title memory in a single frame,
with none of them skipped, and 6,814 ms of the frame's 7,000 ms of texture
work spent waiting for that memory. Spreading the reads over frames is what
makes such a scene usable at all: at `VICTORY_TEXTURE_REFRESH_EVERY=8` the
same frame was 145-522 ms instead of 2,333-6,906 ms, with the picture intact
(captures at the same frame numbers compared).

`VICTORY_TEXTURE_REFRESH_EVERY=<n>` reads a texture once every n frames at
most, round-robin by the texture's own address so the frames share the work
instead of one frame taking all of it. Unset, 0 or 1: every frame, as before,
so nothing changes for anyone who does not ask for it. The cost is staleness:
a texture's contents can lag by up to n frames. It is a switch for slow
machines and for measuring what the refresh actually costs, not a claim that
every frame should skip reads.

Only a build with the diagnostics feature has the switch: it trades the
picture for frame time, and `capture.rs` and `pipeline.rs` keep their switches
there for the same reason. The check runs in `refresh_all` before `in_parallel`,
so a texture that is not due this frame is never handed to a worker thread.

Checked: `cargo check -p victory-gpu` and `cargo check -p victory-gpu --profile
diag --features diagnostics`; `cargo clippy -p victory-gpu --all-targets
--all-features -- -D warnings` (0 warnings); `cargo test -p victory-gpu --lib
--profile diag --features diagnostics` 196 passed, 2 failed - the two #1744
view-heap failures that fail on main too.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:07:00 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 16:16 UTC · original · edited 2026-11-25 20:21 UTC

Automated triage for the maintainers.

Merge candidate. Staging::due in crates/victory-gpu/src/texture.rs adds VICTORY_TEXTURE_REFRESH_EVERY=N, a diagnostics-only switch. With it set, a texture is re-read from title memory at most once every N frames, with the frames assigned round-robin by the texture's address. Without the feature, or when the variable is unset, 0 or 1, due() returns true and nothing changes.

All three points from the last triage are in c8d6571:

  • the switch is behind #[cfg(feature = "diagnostics")];
  • refresh_all checks due() before in_parallel, so textures that aren't due never reach a worker thread;
  • the file ends with a newline again.

The 0 divide that kotekompotek found is handled by .filter(|&n| n > 0), and the modulo now uses is_multiple_of, as clippy 1.98 wants. On main, refresh_all's only caller is the bindless upload pass in bindless.rs, which only covers copies that already exist. A skipped texture keeps its last upload, and because its tracker isn't cleared, the change is picked up on its next due frame.

Reply to the author: thanks, every point checks out against the code.

CI: not run. The workflow run for c8d6571 is waiting for a maintainer's approval (action_required).


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 16:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/2478#issuecomment-7167742669) · edited 2026-11-25 20:21 UTC <!-- victory-triage v1 head=c8d6571db41b66b6e553dc234a5d578d3dee215e verdict=merge-candidate at=2026-11-25T20:18:54Z --> Automated triage for the maintainers. **Merge candidate.** `Staging::due` in `crates/victory-gpu/src/texture.rs` adds `VICTORY_TEXTURE_REFRESH_EVERY=N`, a `diagnostics`-only switch. With it set, a texture is re-read from title memory at most once every N frames, with the frames assigned round-robin by the texture's address. Without the feature, or when the variable is unset, 0 or 1, `due()` returns `true` and nothing changes. All three points from the last triage are in `c8d6571`: - the switch is behind `#[cfg(feature = "diagnostics")]`; - `refresh_all` checks `due()` before `in_parallel`, so textures that aren't due never reach a worker thread; - the file ends with a newline again. The `0` divide that kotekompotek found is handled by `.filter(|&n| n > 0)`, and the modulo now uses `is_multiple_of`, as clippy 1.98 wants. On `main`, `refresh_all`'s only caller is the bindless upload pass in `bindless.rs`, which only covers copies that already exist. A skipped texture keeps its last upload, and because its tracker isn't cleared, the change is picked up on its next due frame. Reply to the author: thanks, every point checks out against the code. CI: not run. The workflow run for `c8d6571` is waiting for a maintainer's approval (`action_required`). --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 16:53 UTC · original

The knob is sound as a diagnostic: due() buckets textures by (address >> 16) against frame::current(), so with EVERY=N each texture reads at most once per N frames and the reads spread evenly — exactly what a "world at 1 fps" frame cost needs. Semantics are safe: skipping refresh() leaves the last uploaded copy live, so the worst case is N frames of staleness, never a missing texture.

Two nits: the file lost its trailing newline, and EVERY=0 parses to 0 → % 0 panics — unwrap_or(1) doesn't catch Ok(0). every <= 1 handles 0/1 for the <= 1 check but parse accepts 0 → division by zero in the else branch. Clamp with .filter(|&n| n > 0) or max(1).

**@⁠kotekompotek** on GitHub · 2026-11-25 16:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/2478#issuecomment-7168679037) The knob is sound as a diagnostic: `due()` buckets textures by `(address >> 16)` against `frame::current()`, so with `EVERY=N` each texture reads at most once per N frames and the reads spread evenly — exactly what a "world at 1 fps" frame cost needs. Semantics are safe: skipping `refresh()` leaves the last uploaded copy live, so the worst case is N frames of staleness, never a missing texture. Two nits: the file lost its trailing newline, and `EVERY=0` parses to `0` → `% 0` panics — `unwrap_or(1)` doesn't catch `Ok(0)`. `every <= 1` handles 0/1 for the `<= 1` check but `parse` accepts 0 → division by zero in the else branch. Clamp with `.filter(|&n| n > 0)` or `max(1)`.
Author
Owner

@⁠kais7654 on GitHub · 2026-11-25 17:14 UTC · original

Tested on Windows 11, RTX 5070 Laptop GPU (driver 616.87) + WARP, rustc
1.98.1, --locked. The CI will fail as-is: clippy -D warnings rejects the
modulo line in crates/victory-gpu/src/texture.rs:555 with
clippy::manual_is_multiple_of (pinned clippy 1.98):

  • every <= 1 || (crate::frame::current() + ((self.memory as usize >> 16) as u64)) % every == 0
  • every <= 1 || (crate::frame::current() + ((self.memory as usize >> 16) as u64)).is_multiple_of(every)

With that one line applied (not committed on my side), the branch is
green: clippy -D warnings clean with and without --all-features,
victory-gpu lib 193 passed / 2 failed (the heap-cap pair #1943 owns),
victory-shader lib 266 passed / 3 failed (the NVIDIA machine trio of
#2317). Merge into current main (67e8ae1) is clean.

**@⁠kais7654** on GitHub · 2026-11-25 17:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/2478#issuecomment-7169224682) Tested on Windows 11, RTX 5070 Laptop GPU (driver 616.87) + WARP, rustc 1.98.1, --locked. The CI will fail as-is: clippy -D warnings rejects the modulo line in crates/victory-gpu/src/texture.rs:555 with clippy::manual_is_multiple_of (pinned clippy 1.98): - every <= 1 || (crate::frame::current() + ((self.memory as usize >> 16) as u64)) % every == 0 + every <= 1 || (crate::frame::current() + ((self.memory as usize >> 16) as u64)).is_multiple_of(every) With that one line applied (not committed on my side), the branch is green: clippy -D warnings clean with and without --all-features, victory-gpu lib 193 passed / 2 failed (the heap-cap pair #1943 owns), victory-shader lib 266 passed / 3 failed (the NVIDIA machine trio of #2317). Merge into current main (67e8ae1) is clean.
Author
Owner

@⁠HoLa-Lee on GitHub · 2026-11-25 19:16 UTC · original

Both review comments are in, in c8d6571:

  • @⁠kotekompotek - VICTORY_TEXTURE_REFRESH_EVERY=0 no longer reaches the divide: the parse is .filter(|&n| n > 0).unwrap_or(1), so 0 and 1 both mean "every frame"; the trailing newline is back.
  • @⁠kais7654 - your clippy line is exactly what is committed: .is_multiple_of(every). cargo clippy -p victory-gpu --all-targets --all-features -- -D warnings is 0 warnings on 1.98.1 here, and cargo test -p victory-gpu --lib --profile diag --features diagnostics gives 196 passed / 2 failed (the #1744 view-heap pair, which fails on main on this machine too).

The switch is also behind diagnostics now, and the due check runs in refresh_all before in_parallel, so a texture that is not due is never handed to a thread.

**@⁠HoLa-Lee** on GitHub · 2026-11-25 19:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/2478#issuecomment-7172061568) Both review comments are in, in `c8d6571`: - **@⁠kotekompotek** - `VICTORY_TEXTURE_REFRESH_EVERY=0` no longer reaches the divide: the parse is `.filter(|&n| n > 0).unwrap_or(1)`, so 0 and 1 both mean "every frame"; the trailing newline is back. - **@⁠kais7654** - your clippy line is exactly what is committed: `.is_multiple_of(every)`. `cargo clippy -p victory-gpu --all-targets --all-features -- -D warnings` is 0 warnings on 1.98.1 here, and `cargo test -p victory-gpu --lib --profile diag --features diagnostics` gives 196 passed / 2 failed (the `#1744` view-heap pair, which fails on `main` on this machine too). The switch is also behind `diagnostics` now, and the due check runs in `refresh_all` before `in_parallel`, so a texture that is not due is never handed to a thread.
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/2478/head:github-bot/gh-2478'
git switch 'github-bot/gh-2478'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2478'
git switch 'github-bot/gh-2478'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2478'
git switch 'github-bot/gh-2478'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2478'
git switch 'main'
git merge --squash 'github-bot/gh-2478'
git switch 'main'
git merge --ff-only 'github-bot/gh-2478'
git switch 'main'
git merge 'github-bot/gh-2478'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2478
No description provided.