GPU: invalidate HOST_WRITTEN textures when title memory is written #1547

Open
github-bot wants to merge 4 commits from github-bot/gh-1547 into main AGit
Owner

@⁠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 · open

Fixes #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, causing upload() and upload_changed() to skip re-uploading the updated title data.

Changes (crates/victory-gpu/src/texture.rs)

  • host_written():
    • For watched textures (staging.tracked == Some(...)), consumes prior writes via tracked.take(...) and sets staging.pending_refresh = true if any writes were already pending, so an earlier command list in the same submission with a deferred refresh still sees them.
    • For untracked / streamed textures (staging.tracked == None), snapshots the current blake3::hash of committed memory into last.hash.
    • Marks copy.host_written = true.
  • copy_host_written():
    • For watched textures, checks tracked.take(...); if non-empty, sets staging.pending_refresh = true, clears copy.host_written, and returns false.
    • For untracked / streamed textures, compares the current blake3::hash of committed memory against last.hash; if different, clears copy.host_written and returns false so refresh_now() re-uploads it.
  • upload():
    • Checks !list.first_use(...) before is_host_written(resource) so the staging lock and take() only run once per command list.
  • Tests:
    • Added host_written_invalidates_after_write_watch_hit covering write-watch invalidation.
**@⁠n-romanovich** on GitHub · 2026-11-24 19:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/1547) `n-romanovich:fix/host-written-texture-invalidation` → `main` · +114 −12 · 3 files · 4 commits · open Fixes #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, causing `upload()` and `upload_changed()` to skip re-uploading the updated title data. ### Changes (`crates/victory-gpu/src/texture.rs`) - **`host_written()`**: - For watched textures (`staging.tracked == Some(...)`), consumes prior writes via `tracked.take(...)` and sets `staging.pending_refresh = true` if any writes were already pending, so an earlier command list in the same submission with a deferred refresh still sees them. - For untracked / streamed textures (`staging.tracked == None`), snapshots the current `blake3::hash` of committed memory into `last.hash`. - Marks `copy.host_written = true`. - **`copy_host_written()`**: - For watched textures, checks `tracked.take(...)`; if non-empty, sets `staging.pending_refresh = true`, clears `copy.host_written`, and returns `false`. - For untracked / streamed textures, compares the current `blake3::hash` of committed memory against `last.hash`; if different, clears `copy.host_written` and returns `false` so `refresh_now()` re-uploads it. - **`upload()`**: - Checks `!list.first_use(...)` before `is_host_written(resource)` so the staging lock and `take()` only run once per command list. - **Tests**: - Added `host_written_invalidates_after_write_watch_hit` covering write-watch invalidation.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:35:53 +00:00
Author
Owner

@⁠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: since is the asking that also stamped the pages written before the copy, so only later writes (w > since) re-arm the upload. Three points:

  1. Physical-page views stay host-written for good (texture.rs:86-89, :104). For view memory, watch::writes(.., false) returns None, so the entry becomes Unwatched and is never re-armed. Per watch.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).
  2. A syscall on every draw (texture.rs:147). host_written_still_current runs before list.first_use, and upload() is called from draw.rs readable() for every SRV of every draw. For a host-written watched texture, every bound SRV on every draw costs a HOST_WRITTEN lock, a GetWriteWatch call, a journal lock and a Vec, where once per list would do. Putting first_use first, as #1210 does, gives the same result.
  3. Entries are also recorded for render-target, depth and UAV destinations, which costs one GetWriteWatch per copy or resolve into them. upload() never runs for those textures; #1210 skips them through title_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.

**@⁠wbkepler** on GitHub · 2026-11-24 20:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/1547#issuecomment-7144093932) I compared this with #1210, which fixes the same issue. The watched-memory logic is right: `since` is the asking that also stamped the pages written before the copy, so only later writes (`w > since`) re-arm the upload. Three points: 1. **Physical-page views stay host-written for good** (`texture.rs:86-89`, `:104`). For view memory, `watch::writes(.., false)` returns `None`, so the entry becomes `Unwatched` and is never re-armed. Per `watch.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`). 2. **A syscall on every draw** (`texture.rs:147`). `host_written_still_current` runs before `list.first_use`, and `upload()` is called from `draw.rs` `readable()` for every SRV of every draw. For a host-written watched texture, every bound SRV on every draw costs a `HOST_WRITTEN` lock, a `GetWriteWatch` call, a journal lock and a Vec, where once per list would do. Putting `first_use` first, as #1210 does, gives the same result. 3. Entries are also recorded for render-target, depth and UAV destinations, which costs one `GetWriteWatch` per copy or resolve into them. `upload()` never runs for those textures; #1210 skips them through `title_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: https://git.gtahub.st/github/Marvisak--VIctory/pulls/1210 _AI-assisted review (Claude Code); checked on Windows 11 / RTX 5090._
Author
Owner

@⁠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.rs uploads, resource.rs evictable, texture.rs is_host_written) now goes through copy_host_written, and that function checks !tracked.clean() instead of calling take(), so Staging::refresh_now keeps its dirty ranges. The doc comment on host_writes is restored. Several things still stand:

  • Evidence is still missing. After a host copy, the host texture holds the GPU's texels. Clearing host_written on 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 on main.
  • The reproduced snapshot bugs are still present. andrewpozdnakov7-jpg showed on ce04f94, with GPU readback, that the deferred closure in host_writes records nothing when the texture has no staging yet, so copy_host_written returns true forever. He also showed that writing the copy-time hash into staging.last.hash overwrites the hash refresh_now uses to describe the bytes already in staging. The code at this head is the same. His fix ea1bc83 addresses both.
  • Cost. For an untracked texture, copy_host_written hashes the texture's whole committed memory with blake3 on every call. It is now called per texture from bindless::uploads and from evictable every frame. main's comment in refresh_now notes 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_writes still calls tracked.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

**@⁠Marvisak** on GitHub · 2026-11-25 01:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1547#issuecomment-7149627098) · edited 2026-11-26 00:25 UTC <!-- victory-triage v1 head=52f7eb9cdff6477cc698f8aaa48ae74c1c1a73eb verdict=needs-work at=2026-11-26T00:25:44Z --> Automated triage for the maintainers. **Needs work.** Thank you for the update. Two of the earlier points are fixed: every reader (`bindless.rs` `uploads`, `resource.rs` `evictable`, `texture.rs` `is_host_written`) now goes through `copy_host_written`, and that function checks `!tracked.clean()` instead of calling `take()`, so `Staging::refresh_now` keeps its dirty ranges. The doc comment on `host_writes` is restored. Several things still stand: - **Evidence is still missing.** After a host copy, the host texture holds the GPU's texels. Clearing `host_written` on 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 on `main`. - **The reproduced snapshot bugs are still present.** andrewpozdnakov7-jpg showed on `ce04f94`, with GPU readback, that the deferred closure in `host_writes` records nothing when the texture has no staging yet, so `copy_host_written` returns true forever. He also showed that writing the copy-time hash into `staging.last.hash` overwrites the hash `refresh_now` uses to describe the bytes already in staging. The code at this head is the same. His fix `ea1bc83` addresses both. - **Cost.** For an untracked texture, `copy_host_written` hashes the texture's whole committed memory with blake3 on every call. It is now called per texture from `bindless::uploads` and from `evictable` every frame. `main`'s comment in `refresh_now` notes 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_writes` still calls `tracked.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](https://claude.ai/code)_
Author
Owner

@⁠n-romanovich on GitHub · 2026-11-25 05:51 UTC · original

@⁠Marvisak, updated fix/host-written-texture-invalidation (ce04f94) to address the triage points:

  1. Untracked / streamed textures (staging.tracked == None): host_written() now snapshots the current blake3::hash of committed memory into last.hash, and copy_host_written() invalidates host_written whenever the current hash differs from last.hash.
  2. Deferred refresh ordering: when host_written() consumes pending writes via tracked.take(...), it sets staging.pending_refresh = true instead of clearing it, so an earlier command list in the same submission with a deferred refresh still sees the title writes.
  3. first_use check: upload() now checks !list.first_use(...) before is_host_written(resource) so the staging lock and take() only run once per list.

I've also updated the PR description to match Tracked::take and pending_refresh (no since / HostWritten::Unwatched / watch.rs clear_range). Ready for another look!

**@⁠n-romanovich** on GitHub · 2026-11-25 05:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/1547#issuecomment-7154209029) @⁠Marvisak, updated `fix/host-written-texture-invalidation` (`ce04f94`) to address the triage points: 1. **Untracked / streamed textures (`staging.tracked == None`)**: `host_written()` now snapshots the current `blake3::hash` of committed memory into `last.hash`, and `copy_host_written()` invalidates `host_written` whenever the current hash differs from `last.hash`. 2. **Deferred refresh ordering**: when `host_written()` consumes pending writes via `tracked.take(...)`, it sets `staging.pending_refresh = true` instead of clearing it, so an earlier command list in the same submission with a deferred refresh still sees the title writes. 3. **`first_use` check**: `upload()` now checks `!list.first_use(...)` before `is_host_written(resource)` so the staging lock and `take()` only run once per list. I've also updated the PR description to match `Tracked::take` and `pending_refresh` (no `since` / `HostWritten::Unwatched` / `watch.rs clear_range`). Ready for another look!
Author
Owner

@⁠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.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-25 12:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/1547#issuecomment-7163118583) 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](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/ea1bc83535c4434749b984236e0e25e56a871e8e) 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](https://git.gtahub.st/github/Marvisak--VIctory/issues/1633). AI-assisted with Codex. Synthetic textures only.
Author
Owner

@⁠n-romanovich on GitHub · 2026-11-25 22:57 UTC · original

@⁠Marvisak Updated fix/host-written-texture-invalidation (52f7eb9):

  • Made texture::copy_host_written pub(crate) and routed all host_written readers (bindless.rs, resource::evictable, and texture::is_host_written) through it.
  • Replaced tracked.take() in copy_host_written with non-destructive !tracked.clean() (and removed pending_refresh), so dirty ranges stay intact for per-subresource uploads in Staging::refresh_now.
  • Restored the submission-order vs recording-order doc comment on host_writes.
  • Verified texture::tests (including !tracked.clean() after invalidation) and a_list_run_after_a_host_write_keeps_the_host_texels; CI check passes.
**@⁠n-romanovich** on GitHub · 2026-11-25 22:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/1547#issuecomment-7176983368) @⁠Marvisak Updated `fix/host-written-texture-invalidation` (`52f7eb9`): - Made `texture::copy_host_written` `pub(crate)` and routed all `host_written` readers (`bindless.rs`, `resource::evictable`, and `texture::is_host_written`) through it. - Replaced `tracked.take()` in `copy_host_written` with non-destructive `!tracked.clean()` (and removed `pending_refresh`), so dirty ranges stay intact for per-subresource uploads in `Staging::refresh_now`. - Restored the submission-order vs recording-order doc comment on `host_writes`. - Verified `texture::tests` (including `!tracked.clean()` after invalidation) and `a_list_run_after_a_host_write_keeps_the_host_texels`; CI `check` passes.
Author
Owner

@⁠AndyLatimer on GitHub · 2026-11-25 10:18 UTC · original · reviewed

Tested ce04f94 on 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.

  • Watched path: as described. I also checked it with the deferred-refresh ordering (a list samples, the title writes, a host copy is recorded, then the submission's refresh) on a watch::tests::pool page: the refresh still detiles.
  • Hashed path, last.hash reused (triage point 2): host_copy_keeps_a_pending_refresh_of_unwatched_memory fails on ce04f94 ("the pending refresh detiles the title's write") and passes on main (940d32f). Fix: the copy-time hash goes in its own Last::host, set in host_written and compared in copy_host_written, so last.hash keeps 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.
  • No staging yet (triage point 1): host_copy_before_any_upload_sees_a_title_write fails on ce04f94 ("a title write after the copy"). Fix: host_written makes the staging when there is none. None of its callers holds a lock at the call: copy_resource and copy_texture_region call upload for their source just before, alias_copy runs from draw::readable, which uploads sampled textures, and resolve holds 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)
diff --git a/crates/victory-gpu/src/texture.rs b/crates/victory-gpu/src/texture.rs
index 59ed198..98c9de7 100644
--- a/crates/victory-gpu/src/texture.rs
+++ b/crates/victory-gpu/src/texture.rs
@@ -59,6 +59,10 @@ pub(crate) struct Staging {
 #[derive(Default)]
 struct Last {
     hash: Option<blake3::Hash>,
+    /// Unwatched memory's hash at the last host copy: a different one
+    /// later is a title write. Apart from `hash`, which a pending refresh
+    /// still compares with.
+    host: Option<blake3::Hash>,
     read: Vec<u8>,
 }
 
@@ -74,6 +78,12 @@ pub(crate) fn host_written(resource: This) {
     let Some(copy) = crate::resource::host_copy(resource) else {
         return;
     };
+    // First contents from a host copy: the staging is made now (an upload
+    // makes it at the first sample anyway), so a title write after this
+    // copy is seen.
+    if let Some(texture) = crate::resource::host_resource(resource) {
+        let _ = staging(resource, texture);
+    }
     if let Some(staging) = copy.staging.lock().unwrap_or_else(|e| e.into_inner()).as_ref() {
         let size = staging.layout.size as usize;
         if let Some(tracked) = &staging.tracked {
@@ -89,7 +99,7 @@ pub(crate) fn host_written(resource: This) {
             // SAFETY: committed memory of the texture's layout size.
             let tiled = unsafe { std::slice::from_raw_parts(staging.memory, size) };
             let mut last = staging.last.lock().unwrap_or_else(|e| e.into_inner());
-            last.hash = Some(blake3::hash(tiled));
+            last.host = Some(blake3::hash(tiled));
         }
     }
     copy.host_written.store(true, std::sync::atomic::Ordering::Relaxed);
@@ -116,7 +126,7 @@ fn copy_host_written(copy: &crate::resource::HostTexture) -> bool {
             let tiled = unsafe { std::slice::from_raw_parts(staging.memory, size) };
             let hash = blake3::hash(tiled);
             let last = staging.last.lock().unwrap_or_else(|e| e.into_inner());
-            last.hash != Some(hash)
+            last.host != Some(hash)
         } else {
             false
         };
@@ -633,4 +643,52 @@ mod tests {
         assert!(!copy_host_written(&copy));
         assert!(!copy.host_written.load(std::sync::atomic::Ordering::Relaxed));
     }
+
+    /// A list sampling a texture refreshes it at submission. A host copy
+    /// into it recorded before then must not hide a title write that
+    /// refresh has not seen yet (unwatched memory: hashed).
+    #[test]
+    fn host_copy_keeps_a_pending_refresh_of_unwatched_memory() {
+        if host().is_none() {
+            return; // no host GPU
+        }
+        // Leaked: the placed-texture map keeps the address.
+        let backing = vec![0x11u8; 64 << 10].leak();
+        // A 4x4 RGBA8 2D texture (`D3D12_RESOURCE_DESC` as words).
+        let desc = [3, 0, 0, 0, 4, 0, 4, 1 | 1 << 16, 28, 1, 0, 0, 0, 0];
+        let image = crate::resource::texture_at(backing.as_ptr() as u64, &desc).expect("texture");
+        let texture = crate::resource::host_resource(image).expect("host texture");
+        let staging = staging(image, texture).expect("staging");
+        assert!(staging.tracked.is_none(), "plain memory is hashed, not watched");
+        assert!(staging.layout.size as usize <= backing.len());
+        assert!(staging.refresh(), "first upload");
+        // The title writes the memory and a list samples the texture: its
+        // refresh waits for the submission...
+        backing.fill(0x22);
+        // ...and a host copy into the texture is recorded before it.
+        host_written(image);
+        assert!(staging.refresh(), "the pending refresh detiles the title's write");
+        // Later lists keep the host's contents until the title writes again.
+        assert!(is_host_written(image));
+        backing.fill(0x33);
+        assert!(!is_host_written(image), "a title write after the copy");
+    }
+
+    /// A texture whose first contents come from a host copy, before
+    /// anything sampled it, still sees a later title write.
+    #[test]
+    fn host_copy_before_any_upload_sees_a_title_write() {
+        if host().is_none() {
+            return; // no host GPU
+        }
+        // Leaked: the placed-texture map keeps the address.
+        let backing = vec![0x11u8; 64 << 10].leak();
+        // A 4x4 RGBA8 2D texture (`D3D12_RESOURCE_DESC` as words).
+        let desc = [3, 0, 0, 0, 4, 0, 4, 1 | 1 << 16, 28, 1, 0, 0, 0, 0];
+        let image = crate::resource::texture_at(backing.as_ptr() as u64, &desc).expect("texture");
+        host_written(image);
+        assert!(is_host_written(image), "the host's contents until a title write");
+        backing.fill(0x22);
+        assert!(!is_host_written(image), "a title write after the copy");
+    }
 }

With it, on this machine: cargo test -p victory-gpu --locked --no-fail-fast 144 passed, 2 failed. The same 2 fail without the patch (142 passed): dynamic_slots_are_held_until_released and lists_share_a_table_and_copies_keep_their_views, the descriptor-heap cap assert from #1744/#1835. cargo clippy -p victory-gpu --all-targets -- -D warnings is clean, default and --all-features.

**@⁠AndyLatimer** on GitHub · 2026-11-25 10:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/1547#pullrequestreview-6055707507) · **reviewed** Tested `ce04f94` on 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. - **Watched path:** as described. I also checked it with the deferred-refresh ordering (a list samples, the title writes, a host copy is recorded, then the submission's refresh) on a `watch::tests::pool` page: the refresh still detiles. - **Hashed path, `last.hash` reused** (triage point 2): `host_copy_keeps_a_pending_refresh_of_unwatched_memory` fails on `ce04f94` ("the pending refresh detiles the title's write") and passes on `main` (`940d32f`). Fix: the copy-time hash goes in its own `Last::host`, set in `host_written` and compared in `copy_host_written`, so `last.hash` keeps 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. - **No staging yet** (triage point 1): `host_copy_before_any_upload_sees_a_title_write` fails on `ce04f94` ("a title write after the copy"). Fix: `host_written` makes the staging when there is none. None of its callers holds a lock at the call: `copy_resource` and `copy_texture_region` call `upload` for their source just before, `alias_copy` runs from `draw::readable`, which uploads sampled textures, and `resolve` holds nothing there. The cost is the upload buffer a first sample would make anyway, now also made for textures only ever copied into. <details><summary>Patch on <code>ce04f94</code> (texture.rs, +60/−2)</summary> ```diff diff --git a/crates/victory-gpu/src/texture.rs b/crates/victory-gpu/src/texture.rs index 59ed198..98c9de7 100644 --- a/crates/victory-gpu/src/texture.rs +++ b/crates/victory-gpu/src/texture.rs @@ -59,6 +59,10 @@ pub(crate) struct Staging { #[derive(Default)] struct Last { hash: Option<blake3::Hash>, + /// Unwatched memory's hash at the last host copy: a different one + /// later is a title write. Apart from `hash`, which a pending refresh + /// still compares with. + host: Option<blake3::Hash>, read: Vec<u8>, } @@ -74,6 +78,12 @@ pub(crate) fn host_written(resource: This) { let Some(copy) = crate::resource::host_copy(resource) else { return; }; + // First contents from a host copy: the staging is made now (an upload + // makes it at the first sample anyway), so a title write after this + // copy is seen. + if let Some(texture) = crate::resource::host_resource(resource) { + let _ = staging(resource, texture); + } if let Some(staging) = copy.staging.lock().unwrap_or_else(|e| e.into_inner()).as_ref() { let size = staging.layout.size as usize; if let Some(tracked) = &staging.tracked { @@ -89,7 +99,7 @@ pub(crate) fn host_written(resource: This) { // SAFETY: committed memory of the texture's layout size. let tiled = unsafe { std::slice::from_raw_parts(staging.memory, size) }; let mut last = staging.last.lock().unwrap_or_else(|e| e.into_inner()); - last.hash = Some(blake3::hash(tiled)); + last.host = Some(blake3::hash(tiled)); } } copy.host_written.store(true, std::sync::atomic::Ordering::Relaxed); @@ -116,7 +126,7 @@ fn copy_host_written(copy: &crate::resource::HostTexture) -> bool { let tiled = unsafe { std::slice::from_raw_parts(staging.memory, size) }; let hash = blake3::hash(tiled); let last = staging.last.lock().unwrap_or_else(|e| e.into_inner()); - last.hash != Some(hash) + last.host != Some(hash) } else { false }; @@ -633,4 +643,52 @@ mod tests { assert!(!copy_host_written(&copy)); assert!(!copy.host_written.load(std::sync::atomic::Ordering::Relaxed)); } + + /// A list sampling a texture refreshes it at submission. A host copy + /// into it recorded before then must not hide a title write that + /// refresh has not seen yet (unwatched memory: hashed). + #[test] + fn host_copy_keeps_a_pending_refresh_of_unwatched_memory() { + if host().is_none() { + return; // no host GPU + } + // Leaked: the placed-texture map keeps the address. + let backing = vec![0x11u8; 64 << 10].leak(); + // A 4x4 RGBA8 2D texture (`D3D12_RESOURCE_DESC` as words). + let desc = [3, 0, 0, 0, 4, 0, 4, 1 | 1 << 16, 28, 1, 0, 0, 0, 0]; + let image = crate::resource::texture_at(backing.as_ptr() as u64, &desc).expect("texture"); + let texture = crate::resource::host_resource(image).expect("host texture"); + let staging = staging(image, texture).expect("staging"); + assert!(staging.tracked.is_none(), "plain memory is hashed, not watched"); + assert!(staging.layout.size as usize <= backing.len()); + assert!(staging.refresh(), "first upload"); + // The title writes the memory and a list samples the texture: its + // refresh waits for the submission... + backing.fill(0x22); + // ...and a host copy into the texture is recorded before it. + host_written(image); + assert!(staging.refresh(), "the pending refresh detiles the title's write"); + // Later lists keep the host's contents until the title writes again. + assert!(is_host_written(image)); + backing.fill(0x33); + assert!(!is_host_written(image), "a title write after the copy"); + } + + /// A texture whose first contents come from a host copy, before + /// anything sampled it, still sees a later title write. + #[test] + fn host_copy_before_any_upload_sees_a_title_write() { + if host().is_none() { + return; // no host GPU + } + // Leaked: the placed-texture map keeps the address. + let backing = vec![0x11u8; 64 << 10].leak(); + // A 4x4 RGBA8 2D texture (`D3D12_RESOURCE_DESC` as words). + let desc = [3, 0, 0, 0, 4, 0, 4, 1 | 1 << 16, 28, 1, 0, 0, 0, 0]; + let image = crate::resource::texture_at(backing.as_ptr() as u64, &desc).expect("texture"); + host_written(image); + assert!(is_host_written(image), "the host's contents until a title write"); + backing.fill(0x22); + assert!(!is_host_written(image), "a title write after the copy"); + } } ``` </details> With it, on this machine: `cargo test -p victory-gpu --locked --no-fail-fast` 144 passed, 2 failed. The same 2 fail without the patch (142 passed): `dynamic_slots_are_held_until_released` and `lists_share_a_table_and_copies_keep_their_views`, the descriptor-heap cap assert from #1744/#1835. `cargo clippy -p victory-gpu --all-targets -- -D warnings` is clean, default and `--all-features`.
Author
Owner

@⁠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.

WARP readback 52f7eb9 Local adaptation
Title writes after a GPU copy to a texture with no staging 0/16 new title texels 16/16
Warm staging with a delayed read after host ownership is invalidated 0/16 new title texels 16/16
Read submitted before the GPU copy 16/16 new title texels 16/16

In the warm case, staging last detiled A. The title wrote B, then the GPU copy saved B as its copy-time snapshot. A write back to A invalidated host ownership while I recorded a read. I restored B before submitting that read. The current head kept the GPU-copy color in all 16 texels because its staging hash had already been set to B by 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.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 08:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/1547#issuecomment-7186976058) @⁠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. | WARP readback | `52f7eb9` | Local adaptation | | --- | --- | --- | | Title writes after a GPU copy to a texture with no staging | 0/16 new title texels | 16/16 | | Warm staging with a delayed read after host ownership is invalidated | 0/16 new title texels | 16/16 | | Read submitted before the GPU copy | 16/16 new title texels | 16/16 | In the warm case, staging last detiled `A`. The title wrote `B`, then the GPU copy saved `B` as its copy-time snapshot. A write back to `A` invalidated host ownership while I recorded a read. I restored `B` before submitting that read. The current head kept the GPU-copy color in all 16 texels because its staging hash had already been set to `B` by 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](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/ea1bc83535c4434749b984236e0e25e56a871e8e). 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.
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/1547/head:github-bot/gh-1547'
git switch 'github-bot/gh-1547'

Merge

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