Skip to content

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

Open
powplusplus wants to merge 1 commit into
Marvisak:mainfrom
powplusplus:gpu/host-written-reupload
Open

powplusplus wants to merge 1 commit into
Marvisak:mainfrom
powplusplus:gpu/host-written-reupload

Conversation

@powplusplus

Copy link
Copy Markdown

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

@Zipettte

Copy link
Copy Markdown

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).

@wwwwwwww3333333

Copy link
Copy Markdown
Contributor

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).

@wbkepler

Copy link
Copy Markdown
Contributor

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

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):

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

Copy link
Copy Markdown
Owner

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

gmankab pushed a commit to gmankab/victory that referenced this pull request Nov 26, 2026
… level as it is (fork PR Marvisak#1210)

ResourceMinLODClamp counts from the resource's level 0, so moving the view's first level must not lower it; lowered, a view could sample a level the title held back. Its author's test on an RTX 4080 and WARP; read whole, applied as it is; its card test not run here.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

At the host-writing submission (`host_writes`), snapshot where title memory
stands on `HostTexture`: a `watch::Tracked` handle plus a content hash for
runtime-tracked memory (pools and physical-page views, so 64 KiB page false
sharing does not discard host texels), or a content hash otherwise.
`bindless::uploads` checks `still_host_written` once per submission; once
title bytes actually change, the host-written marker is cleared and
staging's last-detiled hashes are reset so the next refresh uploads the new
title bytes.

Fixes Marvisak#758
@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

@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 <andrewpozdnakov7-jpg@users.noreply.github.com> if folding it into another commit. AI-assisted with Codex.

XFlawless1 pushed a commit to XFlawless1/VIctory that referenced this pull request Nov 26, 2026
… level as it is (fork PR Marvisak#1210)

ResourceMinLODClamp counts from the resource's level 0, so moving the view's first level must not lower it; lowered, a view could sample a level the title held back. Its author's test on an RTX 4080 and WARP; read whole, applied as it is; its card test not run here.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit 41f9857)
@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

@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 <andrewpozdnakov7-jpg@users.noreply.github.com> if folding them into another commit. AI-assisted with Codex.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

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.

Biskwit pushed a commit to Biskwit/VIctory-reloaded that referenced this pull request Nov 26, 2026
… level as it is (fork PR Marvisak#1210)

ResourceMinLODClamp counts from the resource's level 0, so moving the view's first level must not lower it; lowered, a view could sample a level the title held back. Its author's test on an RTX 4080 and WARP; read whole, applied as it is; its card test not run here.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit 41f9857)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A texture that once received a GPU copy ignores later writes to its memory

6 participants