GPU: a remap under an uploaded texture uploads it again (#574) #606

Open
github-bot wants to merge 3 commits from github-bot/gh-606 into main AGit
Owner

@⁠laptopnotebook on GitHub · 2026-11-25 03:06 UTC · original
laptopnotebook:fix/574-remapped-staged-texture → main · +353 −2 · 3 files · 3 commits · open

Fixes #574.

A title texture that was uploaded and found unchanged since is only checked when a list samples it (texture::upload): the copy into its host texture is not recorded, and a change the check finds is uploaded by the next list. A remap of the texture's memory did not reset that state, so the first list that sampled the texture after the remap read its old texels. Since placed textures are keyed without their filler words (c96b4f2), a view with new filler after a remap reuses the old texture, which made this common.

Now watch::title_memory_remapped calls a new texture::remapped, which marks every staged texture overlapping the range dirty. Its next upload records the copy, and the check at that list's submission reads the new memory into the staging buffer first, so the first list after the remap gets the new texels. Staged textures are indexed by address (weak entries, pruned when let go) and the search is bounded by the largest staged texture, so a remap looks only at textures that can overlap it instead of scanning all of them. Textures still share one host copy while their memory stays mapped, as #574 asks.

Validation:

  • New test texture::remap_tests::a_remap_under_a_staged_texture_makes_it_upload_again stages a texture on the GPU and remaps memory next to it (left alone), an empty range (left alone) and memory under it (marked). It fails with the fix disabled.
  • cargo test --workspace --locked: all pass (Windows 11, NVIDIA).
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean. The run without --all-features fails only on placed_filler_twins being unused, which main fails too; #530 addresses that.
  • A run of the title on this branch with a temporary counter (not in this PR): 8-15 remaps hit uploaded, unchanged textures (1 KiB to 2.7 MB) through the menus and into the prologue, each of which would have been stale for one list before. Texture preparation stayed at 10-28 ms a frame; no crash or device loss. Not shown: a visible difference in a scene, since the stale window is one list.
**@⁠laptopnotebook** on GitHub · 2026-11-25 03:06 UTC · [original](https://github.com/fxceless/victory-fork/pull/606) `laptopnotebook:fix/574-remapped-staged-texture` → `main` · +353 −2 · 3 files · 3 commits · open Fixes #574. A title texture that was uploaded and found unchanged since is only checked when a list samples it (`texture::upload`): the copy into its host texture is not recorded, and a change the check finds is uploaded by the next list. A remap of the texture's memory did not reset that state, so the first list that sampled the texture after the remap read its old texels. Since placed textures are keyed without their filler words (c96b4f2), a view with new filler after a remap reuses the old texture, which made this common. Now `watch::title_memory_remapped` calls a new `texture::remapped`, which marks every staged texture overlapping the range dirty. Its next upload records the copy, and the check at that list's submission reads the new memory into the staging buffer first, so the first list after the remap gets the new texels. Staged textures are indexed by address (weak entries, pruned when let go) and the search is bounded by the largest staged texture, so a remap looks only at textures that can overlap it instead of scanning all of them. Textures still share one host copy while their memory stays mapped, as #574 asks. Validation: - New test `texture::remap_tests::a_remap_under_a_staged_texture_makes_it_upload_again` stages a texture on the GPU and remaps memory next to it (left alone), an empty range (left alone) and memory under it (marked). It fails with the fix disabled. - `cargo test --workspace --locked`: all pass (Windows 11, NVIDIA). - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean. The run without `--all-features` fails only on `placed_filler_twins` being unused, which `main` fails too; #530 addresses that. - A run of the title on this branch with a temporary counter (not in this PR): 8-15 remaps hit uploaded, unchanged textures (1 KiB to 2.7 MB) through the menus and into the prologue, each of which would have been stale for one list before. Texture preparation stayed at 10-28 ms a frame; no crash or device loss. Not shown: a visible difference in a scene, since the stale window is one list.
A title texture that was uploaded and found unchanged since is only
checked when a list samples it: the copy into its host texture is not
recorded, and a change the check finds is uploaded by the next list.
A remap of the texture's memory did not reset that state, so the first
list that sampled the texture after the remap read its old texels.
Since placed textures are keyed without their filler words, a view with
new filler after a remap reuses the old texture, which made this common.

Now title_memory_remapped marks every staged texture overlapping the
range dirty, so its next upload records the copy, and the check at that
list's submission reads the new memory first. Staged textures are
indexed by address (weak entries, pruned when let go), so a remap looks
only at textures that can overlap it instead of scanning all of them.
Textures still share one host copy while their memory stays mapped.

Validation: a new test stages a texture on the GPU, remaps memory next
to it (left alone) and under it (marked); it fails without the fix.
In a run of the title (RTX, Windows 11) a temporary counter, not part
of this commit, saw 8-15 remaps hit uploaded, unchanged textures (1 KiB
to 2.7 MB) through the menus and the prologue; texture preparation
stayed at 10-28 ms a frame and nothing crashed.
A partly mapped texture is read at most every 30 frames. A remap inside
that window marked the texture dirty, but the read at submission still
waited its turn, so the list copied the old texels again.

The texture now remembers the remap, and the next partial read skips the
wait once.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:47:17 +00:00
Author
Owner

@⁠yualk on GitHub · 2026-11-25 04:15 UTC · original

Independent verification of a45374449912db2dabbe0dc999501dfa522ed47e against the original #574 fixture, no title assets or title process. Locked MSVC diag, Windows, four fresh processes on RTX 5070 Ti (10de:2c05) and D3D12 WARP (1414:008c), each with default normalization and VICTORY_PLACED_FILLER=1.

  • Default retains one backing for two padding encodings (raw mode keeps two). A different-format view remains distinct.
  • After decommit/recommit at the same VA under the mapping write guard, the first list recorded after remap, through draw::readable, returns all 64 expected a5 bytes on both adapters; the next list also returns a5. The original default base returned 5a on the first list.
  • An unrelated recommit records zero uploads. Cumulative first/second/third recorded uploads are 1/2/2, same as independent cold creation. Replaying the held pre-remap upload list does not restore the old 5a data; this is not a claim about a previously recorded refresh-only list.

This verifies completed texture bytes, not only the dirty flag. It does not cover indexed-slot reads, title visuals, or FPS. AI-assisted testing and write-up.

**@⁠yualk** on GitHub · 2026-11-25 04:15 UTC · [original](https://github.com/fxceless/victory-fork/pull/606#issuecomment-7152679298) Independent verification of `a45374449912db2dabbe0dc999501dfa522ed47e` against the original #574 fixture, no title assets or title process. Locked MSVC `diag`, Windows, four fresh processes on RTX 5070 Ti (`10de:2c05`) and D3D12 WARP (`1414:008c`), each with default normalization and `VICTORY_PLACED_FILLER=1`. - Default retains one backing for two padding encodings (raw mode keeps two). A different-format view remains distinct. - After decommit/recommit at the same VA under the mapping write guard, the first list **recorded after remap**, through `draw::readable`, returns all 64 expected `a5` bytes on both adapters; the next list also returns `a5`. The original default base returned `5a` on the first list. - An unrelated recommit records zero uploads. Cumulative first/second/third recorded uploads are `1/2/2`, same as independent cold creation. Replaying the held pre-remap **upload** list does not restore the old `5a` data; this is not a claim about a previously recorded refresh-only list. This verifies completed texture bytes, not only the dirty flag. It does not cover indexed-slot reads, title visuals, or FPS. AI-assisted testing and write-up.
Author
Owner

@⁠SantannaCarlos on GitHub · 2026-11-25 04:38 UTC · original

Tested a453744 with two remap tests, on an RTX 4070 and on WARP: both pass, including with the host copy held across the remap.

The tests follow yualk's reproducer from #574 step by step, through the host device:

  1. 64 KiB reserved, committed and filled with 0x5a. A placed SRV through descriptor::host::placed_texture (linear 4x4 RGBA8, one mip), first with filler 17, then 31. Each variant gets texture::upload, a copy-source transition and a fenced readback.
  2. Decommit, title_memory_remapped, commit, title_memory_remapped again, all under title_memory_remapping, then fill with 0xa5.
  3. A view with filler 47, read in two lists.

The second test is the same, except that a host_copy of the texture is held across the remap, the way a descriptor slot that still shows it or a list not yet reset would hold it.

First list after the remap (5a = old bytes):

Tree Nothing holds the copy Copy held
c96b4f2 fails (5a) fails (5a)
c96b4f2 + #432 passes fails (5a)
#606 passes passes
#606 + #432 passes passes
#606, VICTORY_PLACED_FILLER=1 (WARP) passes passes

The RTX 4070 runs use a diagnostics build. On the card, the PR's own remap_tests::a_remap_under_a_staged_texture_makes_it_upload_again passes as well. #606 and #432 apply together cleanly; each adds one line to title_memory_remapped.

So #606 also covers the case #432 leaves open: a host copy that stays alive across the remap.

One case these tests don't reach, from reading refresh_timed: the guarded path is fine, since the remap takes the blocks out of protected and clean, so the next guard reports them written. But a texture that is still only partly mapped after the remap takes the partial-read branch. That branch returns early for 30 frames after the last partial read (last.partial), so even with dirty set the upload copies the staging buffer as it was, and the pages the remap brought in show up as late as the shortcut allows. Clearing last.partial along with the dirty mark would let a remap end that pause. This is the streaming case the shortcut already accepts, not the stale first list from #574. I have not measured it.

The two tests can go in as a commit on top of this if that is useful.

**@⁠SantannaCarlos** on GitHub · 2026-11-25 04:38 UTC · [original](https://github.com/fxceless/victory-fork/pull/606#issuecomment-7153026113) **Tested `a453744` with two remap tests, on an RTX 4070 and on WARP: both pass, including with the host copy held across the remap.** The tests follow yualk's reproducer from #574 step by step, through the host device: 1. 64 KiB reserved, committed and filled with `0x5a`. A placed SRV through `descriptor::host::placed_texture` (linear 4x4 RGBA8, one mip), first with filler 17, then 31. Each variant gets `texture::upload`, a copy-source transition and a fenced readback. 2. Decommit, `title_memory_remapped`, commit, `title_memory_remapped` again, all under `title_memory_remapping`, then fill with `0xa5`. 3. A view with filler 47, read in two lists. The second test is the same, except that a `host_copy` of the texture is held across the remap, the way a descriptor slot that still shows it or a list not yet reset would hold it. First list after the remap (`5a` = old bytes): | Tree | Nothing holds the copy | Copy held | |---|---|---| | `c96b4f2` | fails (`5a`) | fails (`5a`) | | `c96b4f2` + #432 | passes | fails (`5a`) | | #606 | passes | passes | | #606 + #432 | passes | passes | | #606, `VICTORY_PLACED_FILLER=1` (WARP) | passes | passes | The RTX 4070 runs use a diagnostics build. On the card, the PR's own `remap_tests::a_remap_under_a_staged_texture_makes_it_upload_again` passes as well. #606 and #432 apply together cleanly; each adds one line to `title_memory_remapped`. So #606 also covers the case #432 leaves open: a host copy that stays alive across the remap. One case these tests don't reach, from reading `refresh_timed`: the guarded path is fine, since the remap takes the blocks out of `protected` and `clean`, so the next `guard` reports them written. But a texture that is still only partly mapped after the remap takes the partial-read branch. That branch returns early for 30 frames after the last partial read (`last.partial`), so even with `dirty` set the upload copies the staging buffer as it was, and the pages the remap brought in show up as late as the shortcut allows. Clearing `last.partial` along with the `dirty` mark would let a remap end that pause. This is the streaming case the shortcut already accepts, not the stale first list from #574. I have not measured it. The two tests can go in as a commit on top of this if that is useful.
Author
Owner

@⁠laptopnotebook on GitHub · 2026-11-25 04:41 UTC · original

Thanks, this is really thorough. The held-copy case is a good one to have covered, and good to know it stacks cleanly with #432.

Yes please on the tests. Since you can't push to my branch, the easiest is probably a PR against laptopnotebook/victory-fork:fix/574-remapped-staged-texture (or just paste them here and I'll add them with you as the author).

On last.partial: agreed it's a real gap, but I'd rather keep this PR to the stale first list and do that separately. One thing to watch when someone does it: remapped() runs while the runtime holds the mapping write guard, and refresh_timed takes last and then waits on title_memory_reading, so locking last from remapped() can deadlock. It'll need an atomic on Staging (something like a "remapped" flag that refresh_timed swaps and uses to clear partial) rather than touching last directly. I'll look at it after this one, with a measurement of how often a remap actually lands on a partly mapped texture.

**@⁠laptopnotebook** on GitHub · 2026-11-25 04:41 UTC · [original](https://github.com/fxceless/victory-fork/pull/606#issuecomment-7153069799) Thanks, this is really thorough. The held-copy case is a good one to have covered, and good to know it stacks cleanly with #432. Yes please on the tests. Since you can't push to my branch, the easiest is probably a PR against `laptopnotebook/victory-fork:fix/574-remapped-staged-texture` (or just paste them here and I'll add them with you as the author). On `last.partial`: agreed it's a real gap, but I'd rather keep this PR to the stale first list and do that separately. One thing to watch when someone does it: `remapped()` runs while the runtime holds the mapping write guard, and `refresh_timed` takes `last` and then waits on `title_memory_reading`, so locking `last` from `remapped()` can deadlock. It'll need an atomic on `Staging` (something like a "remapped" flag that `refresh_timed` swaps and uses to clear `partial`) rather than touching `last` directly. I'll look at it after this one, with a measurement of how often a remap actually lands on a partly mapped texture.
Author
Owner

@⁠laptopnotebook on GitHub · 2026-11-25 04:42 UTC · original

@⁠yualk thanks for running your original fixture against it, and on WARP too. Good to see the first list after the remap come back with the new bytes, and that the unrelated recommit stays at zero uploads. That was the part I couldn't check from a unit test.

**@⁠laptopnotebook** on GitHub · 2026-11-25 04:42 UTC · [original](https://github.com/fxceless/victory-fork/pull/606#issuecomment-7153097977) @⁠yualk thanks for running your original fixture against it, and on WARP too. Good to see the first list after the remap come back with the new bytes, and that the unrelated recommit stays at zero uploads. That was the part I couldn't check from a unit test.
Author
Owner

@⁠SantannaCarlos on GitHub · 2026-11-25 05:26 UTC · original

The two tests are laptopnotebook/victory-fork#1, against your branch.

**@⁠SantannaCarlos** on GitHub · 2026-11-25 05:26 UTC · [original](https://github.com/fxceless/victory-fork/pull/606#issuecomment-7153767992) The two tests are laptopnotebook/victory-fork#1, against your branch.
Author
Owner

@⁠laptopnotebook on GitHub · 2026-11-25 05:27 UTC · original

Merged @⁠SantannaCarlos's two tests into this branch (638afd2). I ran them here too: both pass with the fix and both fail at the first list after the mapping with it reverted, so they pin down #574, including the case where the host copy is held across the remap.

**@⁠laptopnotebook** on GitHub · 2026-11-25 05:27 UTC · [original](https://github.com/fxceless/victory-fork/pull/606#issuecomment-7153794933) Merged @⁠SantannaCarlos's two tests into this branch (638afd2). I ran them here too: both pass with the fix and both fail at the first list after the mapping with it reverted, so they pin down #574, including the case where the host copy is held across the remap.
Author
Owner

@⁠laptopnotebook on GitHub · 2026-11-25 18:44 UTC · original

Pushed one follow-up (7476e80) for a case the first fix missed.

A partly mapped texture (some pages there, as while it streams in) is read at most every 30 frames. A remap inside that window marked the texture dirty, so the list recorded the copy, but the read at submission still waited its turn and the copy took the old texels.

The texture now remembers that it was remapped, and the next partial read skips the wait once. Otherwise the 30-frame limit works as before.

The new test (a_remap_under_a_partly_mapped_texture_is_read_at_once) fails without the change (the staging buffer still holds the old byte) and passes with it. cargo test --workspace passes (509). I found this by reading the code and haven't seen a visible difference from it in game.

**@⁠laptopnotebook** on GitHub · 2026-11-25 18:44 UTC · [original](https://github.com/fxceless/victory-fork/pull/606#issuecomment-7171328180) Pushed one follow-up (7476e80) for a case the first fix missed. A partly mapped texture (some pages there, as while it streams in) is read at most every 30 frames. A remap inside that window marked the texture dirty, so the list recorded the copy, but the read at submission still waited its turn and the copy took the old texels. The texture now remembers that it was remapped, and the next partial read skips the wait once. Otherwise the 30-frame limit works as before. The new test (`a_remap_under_a_partly_mapped_texture_is_read_at_once`) fails without the change (the staging buffer still holds the old byte) and passes with it. `cargo test --workspace` passes (509). I found this by reading the code and haven't seen a visible difference from it in game.
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/606/head:github-bot/gh-606'
git switch 'github-bot/gh-606'

Merge

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