GPU: upload a host-copied texture again once the title writes its memory #1210

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

@⁠powplusplus on GitHub · 2026-11-24 16:26 UTC · original
powplusplus:gpu/host-written-reupload → main · +194 −28 · 3 files · 1 commits · open

Fixes #758.

Once a texture received a GPU copy or resolve, upload() skipped it until the resource was released, so later title writes to its memory (streamed mips, for example) never reached the host copy.

host_written now records where the memory stood at the copy: the write-watch asking for watched memory, a content hash for physical-page views. upload() keeps the host contents only while the memory is unchanged since then. Render targets, depth and UAV textures are not tracked, since upload() never runs for them.

Tested:

  • clippy, default and all features
  • cargo test -p victory-gpu: two new tests, one for write-watched memory and one for hashed memory
  • not run in the title yet
**@⁠powplusplus** on GitHub · 2026-11-24 16:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/1210) `powplusplus:gpu/host-written-reupload` → `main` · +194 −28 · 3 files · 1 commits · open Fixes #758. Once a texture received a GPU copy or resolve, `upload()` skipped it until the resource was released, so later title writes to its memory (streamed mips, for example) never reached the host copy. `host_written` now records where the memory stood at the copy: the write-watch asking for watched memory, a content hash for physical-page views. `upload()` keeps the host contents only while the memory is unchanged since then. Render targets, depth and UAV textures are not tracked, since `upload()` never runs for them. Tested: - clippy, default and all features - `cargo test -p victory-gpu`: two new tests, one for write-watched memory and one for hashed memory - not run in the title yet
A texture that received a GPU copy or resolve kept the host contents for
good, so later title writes (streamed mips, for example) never reached the
host copy.

Record where the memory stood at the host copy: the write-watch asking for
watched memory, a content hash otherwise. Upload skips the texture only
while the memory is unchanged since then.

Fixes #758
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:23:53 +00:00
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 18:00 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at fd59489: cargo test -p victory-gpu passes, 52 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 18:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/1210#issuecomment-7140010928) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at fd59489: `cargo test -p victory-gpu` passes, 52 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠wwwwwwww3333333 on GitHub · 2026-11-24 20:18 UTC · original

With --test-threads=1, a main test fails with this PR: watch::tests::written_pages_are_reported_to_every_asker, at assert!(before.is_empty()) (watch.rs:307). That gives 51 passed, 1 failed. main alone passes all 50 the same way.

Setup: head fd59489 on main ffe310e (clean merge), Windows 11, RX 6750 XT, Rust 1.98.1 MSVC, cargo test -p victory-gpu --lib -- --test-threads=1. @⁠Zipettte's 52/52 ran with the default parallel threads, so the order and the addresses differ there.

What I think happens, from the source:

  • host_written_clears_after_write_watched_memory_is_written (texture::tests, which runs before watch::tests in a sequential run) allocates one write-watched 64 KiB page and writes to it.
  • still_host_written then calls watch::writes, which records that page in the global JOURNAL.written.
  • The test frees the page. Nothing removes its journal entry: written only ever grows.
  • The watch test's fresh VirtualAlloc gets the same address back. So the first writes on it already lists that page, and before.is_empty() fails.

Ways out, smallest first:

  • In the watch test, compare against before instead of requiring it to be empty.
  • Give the new texture test its own address range, so the two tests don't share pages.
  • Have watch drop journal entries for a range when the title frees it. In the emulator, the same stale entry makes a reallocated buffer look written once, which costs one extra copy rather than a wrong result.

Unit tests only, no game files. AI-assisted (Claude found it in a batch run and traced it in the source; I ran the tests on my machine).

**@⁠wwwwwwww3333333** on GitHub · 2026-11-24 20:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/1210#issuecomment-7143331644) With `--test-threads=1`, a main test fails with this PR: `watch::tests::written_pages_are_reported_to_every_asker`, at `assert!(before.is_empty())` (watch.rs:307). That gives 51 passed, 1 failed. main alone passes all 50 the same way. Setup: head fd59489 on main ffe310e (clean merge), Windows 11, RX 6750 XT, Rust 1.98.1 MSVC, `cargo test -p victory-gpu --lib -- --test-threads=1`. @⁠Zipettte's 52/52 ran with the default parallel threads, so the order and the addresses differ there. What I think happens, from the source: - `host_written_clears_after_write_watched_memory_is_written` (texture::tests, which runs before watch::tests in a sequential run) allocates one write-watched 64 KiB page and writes to it. - `still_host_written` then calls `watch::writes`, which records that page in the global `JOURNAL.written`. - The test frees the page. Nothing removes its journal entry: `written` only ever grows. - The watch test's fresh `VirtualAlloc` gets the same address back. So the first `writes` on it already lists that page, and `before.is_empty()` fails. Ways out, smallest first: - In the watch test, compare against `before` instead of requiring it to be empty. - Give the new texture test its own address range, so the two tests don't share pages. - Have `watch` drop journal entries for a range when the title frees it. In the emulator, the same stale entry makes a reallocated buffer look written once, which costs one extra copy rather than a wrong result. Unit tests only, no game files. AI-assisted (Claude found it in a batch run and traced it in the source; I ran the tests on my machine).
Author
Owner

@⁠wbkepler on GitHub · 2026-11-24 20:48 UTC · original

I compared this with #1547, which fixes the same issue. This PR is the more complete fix:

  • Watched memory re-arms on w > asked, as in #1547.
  • Physical-page view memory is covered by HostWrite::Hashed. Per watch.rs's header, that is where the texture pool lives, so it is the streamed case from #758. #1547 leaves views host-written for good.

These points apply to both PRs:

  1. The snapshot is taken at record time. host_written runs when the copy or resolve is recorded (list.rs copy_resource / copy_texture_region / resolve). A CPU write to the texture's memory between recording and submission therefore counts as "after the copy". A sampler recorded later in the same frame then re-uploads title bytes over the copy, although on the console the GPU copy runs after submission and wins. Taking the snapshot at submission (in run_deferred) matches hardware ordering.
  2. 64 KiB false sharing on the watched path (texture.rs:119). The journal works in 64 KiB watch::PAGE units. A texture that shares a 64 KiB page with data the title writes on the CPU is reported as rewritten even when its own bytes didn't change; small 256 B or 4 KiB swizzled textures sub-allocated next to per-frame data are the likely case. upload() then detiles the pre-copy title bytes over the GPU copy result. For a texture that gets a GPU copy every frame, that means stale content every frame plus a re-upload. The staging refresh upstream has the same granularity, but there a false positive only re-detiles identical bytes. The hash helper is already here: treat the watch result as "maybe changed" and confirm it against a hash of the texture's bytes taken in host_written.
  3. Cost of the hashed path (texture.rs:121-127). still_host_written hashes the whole texture once for every list that samples it, on the recording thread. That comes on top of the hash in host_written for every copy and the staging refresh's hash once per submission. A large GPU-copied texture in view memory that many lists sample gets hashed in full many times per frame. Doing the check once per submission, next to batch.texture_due, would bound it.

Render targets, depth and UAV textures are not affected in either PR, because upload() never runs for them.

Interaction with #230 (gpu/skip-clean-texture-uploads, ours):

  • #230 replaces HOST_WRITTEN with HostTexture::host_written: AtomicBool. A Step::TextureHostWrite in run_deferred sets it at submission, and Step::Texture checks it again at submission; that is also where #230 records the staging-to-texture copy, and only when staging.refresh() reports dirty.
  • So #230 conflicts textually with this PR and with #1547. Whichever lands second, the natural merge takes this PR's watch/hash snapshot where #230 sets the flag (fixing point 1) and evaluates it in #230's submission-time Step::Texture check (fixing point 3).
  • #230's dirty-only copy adds one requirement. When the marker is cleared, the staging Last (asked/hash) must be reset as well, because after a host copy the host texture no longer matches what staging last detiled. Otherwise, on the hashed path, title memory that returns to the bytes staging last held reads as clean and no upload is recorded. We'll handle that side in #230 if this lands first.

AI-assisted review (Claude Code); checked on Windows 11 / RTX 5090.

**@⁠wbkepler** on GitHub · 2026-11-24 20:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/1210#issuecomment-7144086921) I compared this with #1547, which fixes the same issue. This PR is the more complete fix: - Watched memory re-arms on `w > asked`, as in #1547. - Physical-page view memory is covered by `HostWrite::Hashed`. Per `watch.rs`'s header, that is where the texture pool lives, so it is the streamed case from #758. #1547 leaves views host-written for good. These points apply to both PRs: 1. **The snapshot is taken at record time.** `host_written` runs when the copy or resolve is recorded (`list.rs` `copy_resource` / `copy_texture_region` / `resolve`). A CPU write to the texture's memory between recording and submission therefore counts as "after the copy". A sampler recorded later in the same frame then re-uploads title bytes over the copy, although on the console the GPU copy runs after submission and wins. Taking the snapshot at submission (in `run_deferred`) matches hardware ordering. 2. **64 KiB false sharing on the watched path** (`texture.rs:119`). The journal works in 64 KiB `watch::PAGE` units. A texture that shares a 64 KiB page with data the title writes on the CPU is reported as rewritten even when its own bytes didn't change; small 256 B or 4 KiB swizzled textures sub-allocated next to per-frame data are the likely case. `upload()` then detiles the pre-copy title bytes over the GPU copy result. For a texture that gets a GPU copy every frame, that means stale content every frame plus a re-upload. The staging refresh upstream has the same granularity, but there a false positive only re-detiles identical bytes. The hash helper is already here: treat the watch result as "maybe changed" and confirm it against a hash of the texture's bytes taken in `host_written`. 3. **Cost of the hashed path** (`texture.rs:121-127`). `still_host_written` hashes the whole texture once for every list that samples it, on the recording thread. That comes on top of the hash in `host_written` for every copy and the staging refresh's hash once per submission. A large GPU-copied texture in view memory that many lists sample gets hashed in full many times per frame. Doing the check once per submission, next to `batch.texture_due`, would bound it. Render targets, depth and UAV textures are not affected in either PR, because `upload()` never runs for them. **Interaction with #230** (`gpu/skip-clean-texture-uploads`, ours): - #230 replaces `HOST_WRITTEN` with `HostTexture::host_written: AtomicBool`. A `Step::TextureHostWrite` in `run_deferred` sets it at submission, and `Step::Texture` checks it again at submission; that is also where #230 records the staging-to-texture copy, and only when `staging.refresh()` reports dirty. - So #230 conflicts textually with this PR and with #1547. Whichever lands second, the natural merge takes this PR's watch/hash snapshot where #230 sets the flag (fixing point 1) and evaluates it in #230's submission-time `Step::Texture` check (fixing point 3). - #230's dirty-only copy adds one requirement. When the marker is cleared, the staging `Last` (`asked`/`hash`) must be reset as well, because after a host copy the host texture no longer matches what staging last detiled. Otherwise, on the hashed path, title memory that returns to the bytes staging last held reads as clean and no upload is recorded. We'll handle that side in #230 if this lands first. _AI-assisted review (Claude Code); checked on Windows 11 / RTX 5090._
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:18 UTC · original

Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-gpu/src/texture.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/1210#issuecomment-7149418034) <!-- victory-triage v1 head=fd5948937bc66149253e660b0cca3c23ac73e74a verdict=needs-rebase at=2026-11-25T01:18:42Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-gpu/src/texture.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
github-bot force-pushed github-bot/gh-1210 from fd5948937b to 60f6d1a575 2026-11-26 11:39:52 +00:00 Compare
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 13:07 UTC · original

@⁠powplusplus I ran the same two-mip RGBA8 regression on 63bcca43 and your exact 60f6d1a. A submitted CopyResource made both mips green. Then a CPU write replaced all four texels in mip 1 with blue and left the backing bytes for mip 0 red. A later compute dispatch read both mips.

Revision Mip 0, untouched by the CPU write Mip 1, rewritten by the CPU
63bcca43 16/16 green from the GPU copy 4/4 green from the GPU copy
60f6d1a 16/16 red from the original CPU data 4/4 blue from the later CPU write

Three runs per revision on app-local Agility 1.620.2/WARP 1.0.22 gave the same values for both write-watched and untracked allocations. The ordinary diagnostics suite on the PR tree passed with 249 tests and 2 ignored, and strict GPU Clippy passed with all features. This is synthetic D3D12 evidence. A title capture would establish whether this combination occurs in the game.

The opt-in regression is one test-only commit on your head. It checks that mip 1 takes the CPU update while mip 0 keeps the GPU copy. Could you check whether the fix can retain the host contents for untouched subresources?

cargo test --locked -p victory-gpu --lib --features diagnostics draw::tests::issue758_cpu_mip_write_keeps_the_other_gpu_mip -- --exact --ignored --nocapture --test-threads=1

If you reuse the test, please retain its Git author on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg <[email protected]> if folding it into another commit. AI-assisted with Codex.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 13:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/1210#issuecomment-7192916281) @⁠powplusplus I ran the same two-mip RGBA8 regression on `63bcca43` and your exact `60f6d1a`. A submitted `CopyResource` made both mips green. Then a CPU write replaced all four texels in mip 1 with blue and left the backing bytes for mip 0 red. A later compute dispatch read both mips. | Revision | Mip 0, untouched by the CPU write | Mip 1, rewritten by the CPU | | --- | --- | --- | | `63bcca43` | 16/16 green from the GPU copy | 4/4 green from the GPU copy | | `60f6d1a` | 16/16 red from the original CPU data | 4/4 blue from the later CPU write | Three runs per revision on app-local Agility 1.620.2/WARP 1.0.22 gave the same values for both write-watched and untracked allocations. The ordinary diagnostics suite on the PR tree passed with 249 tests and 2 ignored, and strict GPU Clippy passed with all features. This is synthetic D3D12 evidence. A title capture would establish whether this combination occurs in the game. The [opt-in regression](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/d0a4d3a8b07eef4c5b2e22bee65fbb2881821998) is one test-only commit on your head. It checks that mip 1 takes the CPU update while mip 0 keeps the GPU copy. Could you check whether the fix can retain the host contents for untouched subresources? ```text cargo test --locked -p victory-gpu --lib --features diagnostics draw::tests::issue758_cpu_mip_write_keeps_the_other_gpu_mip -- --exact --ignored --nocapture --test-threads=1 ``` If you reuse the test, please retain its Git author on cherry-pick or rebase, or add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>` if folding it into another commit. AI-assisted with Codex.
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 16:40 UTC · original

@⁠powplusplus I tested a subresource follow-up 8595658 directly above 60f6d1a and the earlier regression commit. It takes the CPU baseline at the original submission point, compares logical texels per subresource when the CPU bytes change, and uploads only the changed subresources. A mip tail can share a tile, so a hash of the whole tile alone cannot identify which mip to replace. The host copy stays resident until every mip and slice has received CPU data.

The updated issue758_cpu_mip_write_keeps_the_other_gpu_mip fixture failed three times on the PR control, at the first mip-1 update. The candidate passed all three fresh runs. Each run checked linear and 4 KiB swizzled layouts (both mips in the same tile), on watched and untracked allocations. It also checks a same-page write outside the texture, two CPU updates to one mip, the copy count, and eviction followed by recreation once both mips are CPU-backed. App-local Agility 1.620.2/WARP 1.0.22 diagnostics suite: 248 passed with the runtime identity test, 0 failed, 1 ignored. Strict GPU Clippy passed with default and all features. Policy passed.

Could you review this approach and check it against a title capture? The logical baseline adds CPU detiling when a host copy is recorded for submission and when its title bytes actually change. I have synthetic correctness results and no title timings for that path.

If you reuse the commits, please retain their Git author on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg <[email protected]> if folding them into another commit. AI-assisted with Codex.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 16:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/1210#issuecomment-7197945490) @⁠powplusplus I tested a [subresource follow-up `8595658`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/859565865f9be63a420ad74f1f429944668efb66) directly above `60f6d1a` and the earlier regression commit. It takes the CPU baseline at the original submission point, compares logical texels per subresource when the CPU bytes change, and uploads only the changed subresources. A mip tail can share a tile, so a hash of the whole tile alone cannot identify which mip to replace. The host copy stays resident until every mip and slice has received CPU data. The updated `issue758_cpu_mip_write_keeps_the_other_gpu_mip` fixture failed three times on the PR control, at the first mip-1 update. The candidate passed all three fresh runs. Each run checked linear and 4 KiB swizzled layouts (both mips in the same tile), on watched and untracked allocations. It also checks a same-page write outside the texture, two CPU updates to one mip, the copy count, and eviction followed by recreation once both mips are CPU-backed. App-local Agility 1.620.2/WARP 1.0.22 diagnostics suite: 248 passed with the runtime identity test, 0 failed, 1 ignored. Strict GPU Clippy passed with default and all features. Policy passed. Could you review this approach and check it against a title capture? The logical baseline adds CPU detiling when a host copy is recorded for submission and when its title bytes actually change. I have synthetic correctness results and no title timings for that path. If you reuse the commits, please retain their Git author on cherry-pick or rebase, or add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>` if folding them into another commit. AI-assisted with Codex.
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 17:35 UTC · original

I also checked the selective mip candidate with #3288's shared texture view refresh. The bindless conflict has a tested resolution. The original four replacement-view tests passed, and partial CPU mip updates still preserve GPU-written mip contents across the linear and packed cases. Combined WARP results and branch. AI-assisted with Codex.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 17:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1210#issuecomment-7199286519) I also checked the selective mip candidate with #3288's shared texture view refresh. The bindless conflict has a tested resolution. The original four replacement-view tests passed, and partial CPU mip updates still preserve GPU-written mip contents across the linear and packed cases. [Combined WARP results and branch](https://git.gtahub.st/github/Marvisak--VIctory/pulls/3288). AI-assisted with Codex.
This pull request can be merged automatically.
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/1210/head:github-bot/gh-1210'
git switch 'github-bot/gh-1210'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1210'
git switch 'github-bot/gh-1210'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1210'
git switch 'github-bot/gh-1210'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1210'
git switch 'main'
git merge --squash 'github-bot/gh-1210'
git switch 'main'
git merge --ff-only 'github-bot/gh-1210'
git switch 'main'
git merge 'github-bot/gh-1210'
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!1210
No description provided.