GPU: invalidate HOST_WRITTEN textures when title memory is written #1547
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!1547
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1547"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@n-romanovich on GitHub · 2026-11-24 19:02 UTC · original
n-romanovich:fix/host-written-texture-invalidation→main· +114 −12 · 3 files · 4 commits · openFixes #758.
Problem
Once a host-side GPU copy writes to a texture (
copy.host_written = true), subsequent title writes to the texture's backing memory were ignored, causingupload()andupload_changed()to skip re-uploading the updated title data.Changes (
crates/victory-gpu/src/texture.rs)host_written():staging.tracked == Some(...)), consumes prior writes viatracked.take(...)and setsstaging.pending_refresh = trueif any writes were already pending, so an earlier command list in the same submission with a deferred refresh still sees them.staging.tracked == None), snapshots the currentblake3::hashof committed memory intolast.hash.copy.host_written = true.copy_host_written():tracked.take(...); if non-empty, setsstaging.pending_refresh = true, clearscopy.host_written, and returnsfalse.blake3::hashof committed memory againstlast.hash; if different, clearscopy.host_writtenand returnsfalsesorefresh_now()re-uploads it.upload():!list.first_use(...)beforeis_host_written(resource)so the staging lock andtake()only run once per command list.host_written_invalidates_after_write_watch_hitcovering write-watch invalidation.@wbkepler on GitHub · 2026-11-24 20:48 UTC · original
I compared this with #1210, which fixes the same issue. The watched-memory logic is right:
sinceis the asking that also stamped the pages written before the copy, so only later writes (w > since) re-arm the upload. Three points:texture.rs:86-89,:104). For view memory,watch::writes(.., false)returnsNone, so the entry becomesUnwatchedand is never re-armed. Perwatch.rs's header, writes into physical pages (DirectStorage destinations,write_physical) go to the texture pool, which is hashed rather than watched. That makes streamed textures, the main case in #758, the one this PR leaves unfixed. #1210 covers them with a content hash (HostWrite::Hashed).texture.rs:147).host_written_still_currentruns beforelist.first_use, andupload()is called fromdraw.rsreadable()for every SRV of every draw. For a host-written watched texture, every bound SRV on every draw costs aHOST_WRITTENlock, aGetWriteWatchcall, a journal lock and a Vec, where once per list would do. Puttingfirst_usefirst, as #1210 does, gives the same result.GetWriteWatchper copy or resolve into them.upload()never runs for those textures; #1210 skips them throughtitle_written.The record-time snapshot, 64 KiB false sharing on the watched path, and the overlap with our #230 apply to both PRs. They are written up here: #1210
AI-assisted review (Claude Code); checked on Windows 11 / RTX 5090.
@Marvisak on GitHub · 2026-11-25 01:30 UTC · original · edited 2026-11-26 00:25 UTC
Automated triage for the maintainers.
Needs work. Thank you for the update. Two of the earlier points are fixed: every reader (
bindless.rsuploads,resource.rsevictable,texture.rsis_host_written) now goes throughcopy_host_written, and that function checks!tracked.clean()instead of callingtake(), soStaging::refresh_nowkeeps its dirty ranges. The doc comment onhost_writesis restored. Several things still stand:host_writtenon any title write and re-uploading from title memory overwrites them, including when the title wrote only part of the texture. Please show that the title relies on this: a capture, or a case where a host-written texture shows stale content onmain.ce04f94, with GPU readback, that the deferred closure inhost_writesrecords nothing when the texture has no staging yet, socopy_host_writtenreturns true forever. He also showed that writing the copy-time hash intostaging.last.hashoverwrites the hashrefresh_nowuses to describe the bytes already in staging. The code at this head is the same. His fixea1bc83addresses both.copy_host_writtenhashes the texture's whole committed memory with blake3 on every call. It is now called per texture frombindless::uploadsand fromevictableevery frame.main's comment inrefresh_nownotes that hashing a movie frame cost 8 ms per frame. Please cache the answer, for example once per submission or frame, or limit the hashing to the path that needs it.host_writesstill callstracked.take(...)when the submission is made. This discards title writes made before the copy that were not yet uploaded, which is wrong when the copy covers only part of the texture. Please explain why that is safe, or keep those ranges.CI: passing (
check).Generated by Claude Code
@n-romanovich on GitHub · 2026-11-25 05:51 UTC · original
@Marvisak, updated
fix/host-written-texture-invalidation(ce04f94) to address the triage points:staging.tracked == None):host_written()now snapshots the currentblake3::hashof committed memory intolast.hash, andcopy_host_written()invalidateshost_writtenwhenever the current hash differs fromlast.hash.host_written()consumes pending writes viatracked.take(...), it setsstaging.pending_refresh = trueinstead of clearing it, so an earlier command list in the same submission with a deferred refresh still sees the title writes.first_usecheck:upload()now checks!list.first_use(...)beforeis_host_written(resource)so the staging lock andtake()only run once per list.I've also updated the PR description to match
Tracked::takeandpending_refresh(nosince/HostWritten::Unwatched/watch.rs clear_range). Ready for another look!@andrewpozdnakov7-jpg on GitHub · 2026-11-25 12:58 UTC · original
I reproduced both snapshot problems in the review using GPU readback on
ce04f94. After the first copy into a texture with no staging buffer, all 16 texels still showed the GPU copy color after a title-memory write. After staging had been initialized, a later copy overwrote its hash, so an earlier queued read got the old staging color in all 16 texels.ea1bc83 records the copy-time snapshot even before the first upload and leaves the staging hash to describe the bytes already detiled. Both readbacks now match the new title color in all 16 texels. The fixture also checks that unchanged title memory preserves the GPU copy and that the heap upload path restores the correct color when title memory returns to the bytes already in staging.
Windows app-local WARP passed 148 project tests (0 failed, 2 ignored), including the existing write-watch test. Strict GPU Clippy passed with and without diagnostics, and the asset policy passed. This follows #1547's current recording-time policy. Submission ordering is tracked in #1891.
The follow-up is a single commit on your exact head. If you take it, please preserve its Git author. Claim. AI-assisted with Codex. Synthetic textures only.
@n-romanovich on GitHub · 2026-11-25 22:57 UTC · original
@Marvisak Updated
fix/host-written-texture-invalidation(52f7eb9):texture::copy_host_writtenpub(crate)and routed allhost_writtenreaders (bindless.rs,resource::evictable, andtexture::is_host_written) through it.tracked.take()incopy_host_writtenwith non-destructive!tracked.clean()(and removedpending_refresh), so dirty ranges stay intact for per-subresource uploads inStaging::refresh_now.host_writes.texture::tests(including!tracked.clean()after invalidation) anda_list_run_after_a_host_write_keeps_the_host_texels; CIcheckpasses.@AndyLatimer on GitHub · 2026-11-25 10:18 UTC · original · reviewed
Tested
ce04f94on Windows 11, NVIDIA, MSVC (rustc 1.98.1), and wrote the two tests the triage asks for, each with a small fix. I don't have push access to your branch, so the patch is below.watch::tests::poolpage: the refresh still detiles.last.hashreused (triage point 2):host_copy_keeps_a_pending_refresh_of_unwatched_memoryfails once04f94("the pending refresh detiles the title's write") and passes onmain(940d32f). Fix: the copy-time hash goes in its ownLast::host, set inhost_writtenand compared incopy_host_written, solast.hashkeeps meaning what the staging buffer holds. The test also covers what the current one doesn't reach on this path: unchanged memory keeps the host's contents, and a later title write hands the texture back to its memory.host_copy_before_any_upload_sees_a_title_writefails once04f94("a title write after the copy"). Fix:host_writtenmakes the staging when there is none. None of its callers holds a lock at the call:copy_resourceandcopy_texture_regioncalluploadfor their source just before,alias_copyruns fromdraw::readable, which uploads sampled textures, andresolveholds nothing there. The cost is the upload buffer a first sample would make anyway, now also made for textures only ever copied into.Patch on
ce04f94(texture.rs, +60/−2)With it, on this machine:
cargo test -p victory-gpu --locked --no-fail-fast144 passed, 2 failed. The same 2 fail without the patch (142 passed):dynamic_slots_are_held_until_releasedandlists_share_a_table_and_copies_keep_their_views, the descriptor-heap cap assert from #1744/#1835.cargo clippy -p victory-gpu --all-targets -- -D warningsis clean, default and--all-features.@andrewpozdnakov7-jpg on GitHub · 2026-11-26 08:45 UTC · original
@n-romanovich I reran the synthetic texture readback from the earlier review on the current
52f7eb9. I switched the test to the current deferred upload path and used the same fixture on that head and on a local adaptation of the snapshot fix.52f7eb9In the warm case, staging last detiled
A. The title wroteB, then the GPU copy savedBas its copy-time snapshot. A write back toAinvalidated host ownership while I recorded a read. I restoredBbefore submitting that read. The current head kept the GPU-copy color in all 16 texels because its staging hash had already been set toBby the copy.The local adaptation keeps the copy-time snapshot separate from the upload hash, including before staging exists. The unchanged-title and pooled reupload controls passed, as did the existing write-watch and submission-order tests. Strict all-feature GPU Clippy passed. Earlier fix and fixture.
The title capture requested for the overall PR is still outstanding. If you reuse the fix or fixture, please retain the Git author or add
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>when folding it into another commit.AI-assisted. Synthetic textures only.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.