GPU: hash a view-memory texture once more at its next check, and every check while it keeps changing #460

Closed
github-bot wants to merge 1 commit from gh-pr/460/head into gh-pr/460/base
Owner

@⁠xunbreakablex on GitHub · 2026-11-25 01:12 UTC · original
xunbreakablex:texture-hot-pr → main · +100 −1 · 1 files · 1 commits · closed without merging

Needs human attention: this overlaps #115, #159, #343, #438 and #456 (and the closed #58 and #63), which all try to fix the same intro flicker (textures written through another view of their pages are seen only by the hash every 8th frame). I could not tell from the threads which one you prefer to keep, so this is not a request to pick mine; it is the variant for which I have measured the cost, so that the choice can use numbers. If one of the others is merged, this can be closed.

The problem

On main a texture in view memory is checked by its write guard, which does not see writes made through another view of the same physical pages (the Bink video planes are written that way), so the only thing that notices a change is a hash taken every 8th frame (HASH_EVERY). The planes are sampled in a ring of three, each checked every third frame, so the three host copies refresh on different frames and the picture jumps between three older frames.

What this changes

After the hash finds a texture changed, it is hashed once more at its next check (not the next frame: a ring texture is checked every third frame, so a "next frame" check never sees the second change). Only if that second hash also finds a change is the texture "hot" and hashed at every check while it keeps changing, until 24 frames after the last change. A texture that streams in changes once and is not hashed any more. State per texture: Last { hot_until, changed_at, verify }, with changed, unchanged and hash_due. Only crates/victory-gpu/src/texture.rs changes (+100/-1).

Measured

All on a diag build of 7b0f736 plus this change (the commit here is the same change on a803c87; I have not run the game on a803c87), RTX 4070 12 GB, Ryzen 5 5600, driver 621.01, Windows 11 26300.

  • Intro, from VICTORY_CAPTURE frames, mean difference of consecutive frames divided by the mean difference of frames three apart: smooth scene 0.36; with the bug 7.5 (15 runs, 6.5-7.9); with this change 0.57 (2 runs). Counting the moving frame triples where frame n+1 looks more like n-1 than like n: 47-62% in 15 runs with the bug, 0.0% (95% interval 0.0-3.4%, 108 triples) in 2 runs with this change.
  • The first variant I tried, hashing every changed texture at every check for 120 frames (the same idea as #343 as I read it), raised the texture preparation in the world from 13 ms to 160 ms per frame (median frame 394 → 607 ms, one run each), because streaming textures change once and were then hashed every frame for 120 frames. With this change: texture preparation 13-21 ms (it was 13 ms before any fix), median frame 336-343 ms (3 runs, warm shader cache, no capture), same number of dropped draws.
  • 3 unit tests for the state machine (cargo test -p victory-gpu --lib -- hot_texture_tests, no GPU).
  • cargo check -p victory-gpu (also --features diagnostics) and cargo clippy -p victory-gpu --all-targets -- -D warnings (also --all-features): clean.

Not measured

#58, #159, #343, #438, #456 and #115 themselves: I do not know their cost in the world, only the cost of my first variant. Other GPUs. The Linux/Proton reports in #39 mention a remaining jump of about one frame in 16 to 20 that this does not address.

AI-assisted (Claude); the runs were done on my machine.

**@⁠xunbreakablex** on GitHub · 2026-11-25 01:12 UTC · [original](https://github.com/fxceless/victory-fork/pull/460) `xunbreakablex:texture-hot-pr` → `main` · +100 −1 · 1 files · 1 commits · closed without merging **Needs human attention:** this overlaps #115, #159, #343, #438 and #456 (and the closed #58 and #63), which all try to fix the same intro flicker (textures written through another view of their pages are seen only by the hash every 8th frame). I could not tell from the threads which one you prefer to keep, so this is not a request to pick mine; it is the variant for which I have measured the cost, so that the choice can use numbers. If one of the others is merged, this can be closed. ### The problem On `main` a texture in view memory is checked by its write guard, which does not see writes made through another view of the same physical pages (the Bink video planes are written that way), so the only thing that notices a change is a hash taken every 8th frame (`HASH_EVERY`). The planes are sampled in a ring of three, each checked every third frame, so the three host copies refresh on different frames and the picture jumps between three older frames. ### What this changes After the hash finds a texture changed, it is hashed **once more at its next check** (not the next frame: a ring texture is checked every third frame, so a "next frame" check never sees the second change). Only if that second hash also finds a change is the texture "hot" and hashed at every check while it keeps changing, until 24 frames after the last change. A texture that streams in changes once and is not hashed any more. State per texture: `Last { hot_until, changed_at, verify }`, with `changed`, `unchanged` and `hash_due`. Only `crates/victory-gpu/src/texture.rs` changes (+100/-1). ### Measured All on a diag build of `7b0f736` plus this change (the commit here is the same change on `a803c87`; I have not run the game on `a803c87`), RTX 4070 12 GB, Ryzen 5 5600, driver 621.01, Windows 11 26300. - Intro, from `VICTORY_CAPTURE` frames, mean difference of consecutive frames divided by the mean difference of frames three apart: smooth scene 0.36; with the bug 7.5 (15 runs, 6.5-7.9); with this change 0.57 (2 runs). Counting the moving frame triples where frame n+1 looks more like n-1 than like n: 47-62% in 15 runs with the bug, 0.0% (95% interval 0.0-3.4%, 108 triples) in 2 runs with this change. - The first variant I tried, hashing every changed texture at every check for 120 frames (the same idea as #343 as I read it), raised the texture preparation in the world from 13 ms to 160 ms per frame (median frame 394 → 607 ms, one run each), because streaming textures change once and were then hashed every frame for 120 frames. With this change: texture preparation 13-21 ms (it was 13 ms before any fix), median frame 336-343 ms (3 runs, warm shader cache, no capture), same number of dropped draws. - 3 unit tests for the state machine (`cargo test -p victory-gpu --lib -- hot_texture_tests`, no GPU). - `cargo check -p victory-gpu` (also `--features diagnostics`) and `cargo clippy -p victory-gpu --all-targets -- -D warnings` (also `--all-features`): clean. ### Not measured #58, #159, #343, #438, #456 and #115 themselves: I do not know their cost in the world, only the cost of my first variant. Other GPUs. The Linux/Proton reports in #39 mention a remaining jump of about one frame in 16 to 20 that this does not address. AI-assisted (Claude); the runs were done on my machine.
Why: the title's Bink 2 video planes live in view memory. They are written
through the CPU view of their pages and sampled through the GPU view, and
they cycle through a ring of three textures, so each texture is sampled
(and checked) only every third frame. The guard on view memory sees writes
made through the texture's own view but not a write to the same pages
through another view, and what it cannot see was found only by a hash taken
every eighth frame (`HASH_EVERY`), by the texture's address. Each plane
therefore showed up to seven frames late, and the intro's picture jumped
between three older frames.

What: a texture whose hash found it changed is hashed once more at its next
check. If it changed again it is hot: it is hashed at every check while it
keeps changing, and for 24 frames after its last change (`HOT_FRAMES`). A
texture that streams in changes once, is found as it was at the extra look,
and is not hashed on. The state is three fields of `Last` (`hot_until`,
`changed_at`, `verify`) and three small methods (`changed`, `unchanged`,
`hash_due`); the turn test becomes `turn || last.hash_due(frame)`.

Why the next check and not the next frame: a ring texture is checked every
third frame, so "changed again" cannot mean "changed in the next frame";
the extra look counts for `VERIFY_FRAMES` (24) frames after the change.

Measured on frames of the intro: the ratio of the mean difference between
consecutive frames to that between frames three apart was 7.5 with the bug
and is 0.57 now (a smooth scene: 0.36). In the world the cost is as before
(textures 12.9 ms against 13.2 ms, median frame 336 ms against 394 ms).
Hashing every check (VICTORY_TEXTURE_GUARD=0) fixes the intro too but cost
160 ms a frame in the world in an earlier attempt, and a 120-frame hot
window made the world's texture work 160 ms. Three unit tests (no GPU)
cover the state machine: a texture changing at every check goes hot, one
that changed once does not, a hot one cools down.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:43:50 +00:00
github-bot closed this pull request 2026-11-26 03:43:50 +00:00
Author
Owner

@⁠xunbreakablex on GitHub · 2026-11-25 07:27 UTC · original

Closing this one. main now has 8dd30ab ("A write through one view of a page is seen at its other views"), which removes the cause this change worked around: the guard did not see writes made through another view of a texture's pages, so only the hash every 8th frame noticed them. I have not re-run the intro on the new main, so I cannot say whether the flicker is gone there; if it comes back, the cost numbers in the description (hashing every changed texture for 120 frames cost 13 → 160 ms a frame in the world, hash-once-more-then-hot cost 13-21 ms) may still be useful to whoever looks at it.

I cannot follow this PR further, so closing it is cleaner than leaving it open.

**@⁠xunbreakablex** on GitHub · 2026-11-25 07:27 UTC · [original](https://github.com/fxceless/victory-fork/pull/460#issuecomment-7155888873) Closing this one. `main` now has `8dd30ab` ("A write through one view of a page is seen at its other views"), which removes the cause this change worked around: the guard did not see writes made through another view of a texture's pages, so only the hash every 8th frame noticed them. I have not re-run the intro on the new `main`, so I cannot say whether the flicker is gone there; if it comes back, the cost numbers in the description (hashing every changed texture for 120 frames cost 13 → 160 ms a frame in the world, hash-once-more-then-hot cost 13-21 ms) may still be useful to whoever looks at it. I cannot follow this PR further, so closing it is cleaner than leaving it open.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.