GPU: pin textures with pending host_writes against eviction #2599

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/2601 into main
fuzeofficlal commented 2026-11-25 16:35:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T16:35:40Z.


Summary

texture::host_writes defers setting HostTexture::host_written = true until HostList::run_deferred runs at submission time. In Host::execute, resource::evict_unused() runs before list.run_deferred(&mut batch) (and eviction can also run between recording a copy/resolve list and submitting it on another queue).

During that window, resource::evictable() sees host_written == false and can evict the destination HostTexture even though a recorded command list is about to write to it via CopyResource, CopyTextureRegion, or ResolveSubresource. When the list subsequently executes, the copy lands in the orphaned host texture, and later readers re-create the texture from stale title memory.

Changes

  • crates/victory-gpu/src/resource.rs:
    • Add HostTexture::pending_host_writes: AtomicUsize (initialized to 0).
    • Update evictable() to check pending_host_writes.load(Acquire) == 0 before !host_written.load(Acquire).
  • crates/victory-gpu/src/texture.rs:
    • In host_writes(), increment pending_host_writes and move a Drop RAII guard (PendingHostWrite) into the list.defer_host(...) closure that stores host_written = true (Release). Because HostList::run_deferred borrows closures until HostList::reset() / Drop, the pin stays active through execution and is released without leaking VRAM if an unexecuted list is reset or dropped.
    • Use Acquire ordering in is_host_written().
    • Add pending_host_write_pins_texture_until_executed_and_unpins_on_reset, verifying: (1) an unexecuted list with CopyResource pins dst against give_up_now and unpins on Reset, (2) a recorded CopyResource blocks eviction before execution, and (3) after executing and resetting the list, dst remains un-evictable and reads back the copied [0xAA; 4] texels rather than stale 0x40 title memory.

Verification

  • Negative control (before fix, WARP): pending_host_write_pins_texture_until_executed_and_unpins_on_reset fails at pending CopyResource must pin dst against eviction.
  • Hardware (VICTORY_D3D12_WARP=0) and WARP (VICTORY_D3D12_WARP=1): cargo test -p victory-gpu --lib texture::tests -- --test-threads=1 — 3/3 pass on both backends.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings and --features diagnostics — pass (0 warnings).
  • cargo xtask policy — pass (184 files ok).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6609056368 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2601). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T16:35:40Z. --- ## Summary `texture::host_writes` defers setting `HostTexture::host_written = true` until `HostList::run_deferred` runs at submission time. In `Host::execute`, `resource::evict_unused()` runs before `list.run_deferred(&mut batch)` (and eviction can also run between recording a copy/resolve list and submitting it on another queue). During that window, `resource::evictable()` sees `host_written == false` and can evict the destination `HostTexture` even though a recorded command list is about to write to it via `CopyResource`, `CopyTextureRegion`, or `ResolveSubresource`. When the list subsequently executes, the copy lands in the orphaned host texture, and later readers re-create the texture from stale title memory. ## Changes - `crates/victory-gpu/src/resource.rs`: - Add `HostTexture::pending_host_writes: AtomicUsize` (initialized to `0`). - Update `evictable()` to check `pending_host_writes.load(Acquire) == 0` before `!host_written.load(Acquire)`. - `crates/victory-gpu/src/texture.rs`: - In `host_writes()`, increment `pending_host_writes` and move a `Drop` RAII guard (`PendingHostWrite`) into the `list.defer_host(...)` closure that stores `host_written = true` (`Release`). Because `HostList::run_deferred` borrows closures until `HostList::reset()` / `Drop`, the pin stays active through execution and is released without leaking VRAM if an unexecuted list is reset or dropped. - Use `Acquire` ordering in `is_host_written()`. - Add `pending_host_write_pins_texture_until_executed_and_unpins_on_reset`, verifying: (1) an unexecuted list with `CopyResource` pins `dst` against `give_up_now` and unpins on `Reset`, (2) a recorded `CopyResource` blocks eviction before execution, and (3) after executing and resetting the list, `dst` remains un-evictable and reads back the copied `[0xAA; 4]` texels rather than stale `0x40` title memory. ## Verification - Negative control (before fix, WARP): `pending_host_write_pins_texture_until_executed_and_unpins_on_reset` fails at `pending CopyResource must pin dst against eviction`. - Hardware (`VICTORY_D3D12_WARP=0`) and WARP (`VICTORY_D3D12_WARP=1`): `cargo test -p victory-gpu --lib texture::tests -- --test-threads=1` — 3/3 pass on both backends. - `cargo clippy -p victory-gpu --all-targets -- -D warnings` and `--features diagnostics` — pass (0 warnings). - `cargo xtask policy` — pass (`184 files ok`).
DrDarkDK commented 2026-11-25 17:04:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DrDarkDK on 2026-11-25T16:43:02Z.

Review: CHANGES_REQUESTED


copy_resource gets to from host_resource(dst), then host_writes looks up dst again to pin it. If eviction removes the original host texture between those calls, host_writes can pin a replacement while CopyResource still targets the original. Later readers can then miss the copied texels.

There’s a second window after candidates() checks the pin count. If recording pins a texture after that check, give_up() can still evict it from the candidate snapshot. Could recording acquire the destination and its pin together, and coordinate pin acquisition with the final eviction decision before clearing descriptors?

The current regression test records the copy before attempting eviction, so it misses both interleavings. I traced these paths in the code and haven’t reproduced them on hardware.

AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:review:6061394152 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2601#pullrequestreview-6061394152). Originally posted by [DrDarkDK](https://github.com/DrDarkDK) on 2026-11-25T16:43:02Z. **Review:** CHANGES_REQUESTED --- `copy_resource` gets `to` from `host_resource(dst)`, then `host_writes` looks up `dst` again to pin it. If eviction removes the original host texture between those calls, `host_writes` can pin a replacement while `CopyResource` still targets the original. Later readers can then miss the copied texels. There’s a second window after `candidates()` checks the pin count. If recording pins a texture after that check, `give_up()` can still evict it from the candidate snapshot. Could recording acquire the destination and its pin together, and coordinate pin acquisition with the final eviction decision before clearing descriptors? The current regression test records the copy before attempting eviction, so it misses both interleavings. I traced these paths in the code and haven’t reproduced them on hardware. *AI-assisted with Codex.*
DrDarkDK commented 2026-11-25 17:04:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DrDarkDK on 2026-11-25T16:56:48Z.

Review: APPROVED


No description provided.

<!-- greyfinch-sync:Marvisak/VIctory:review:6061609315 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2601#pullrequestreview-6061609315). Originally posted by [DrDarkDK](https://github.com/DrDarkDK) on 2026-11-25T16:56:48Z. **Review:** APPROVED --- _No description provided._
oni-swr commented 2026-11-25 17:05:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T16:49:50Z.


Verification of 5e7cbb4

Environment: Windows 11, D3D12 on the WARP software adapter only (VICTORY_D3D12_WARP=1, cargo ... --features diagnostics, which is what compiles that switch in; VICTORY_DXIL set to the Windows SDK dxil.dll). No physical GPU device was created. Not run on real GPU hardware.

  • Mergeable: git merge-tree against origin/main (67e8ae1) is clean.
  • cargo test -p victory-gpu --features diagnostics --lib pending_host_write_pins_texture_until_executed_and_unpins_on_reset: 1 passed. I confirmed it runs on a real WARP device and is not skipped.
  • Baseline-red: with the pending_host_writes == 0 condition removed from evictable() and the test kept, it fails.
  • Full cargo test -p victory-gpu --features diagnostics --lib: 199 passed, 0 failed, 1 ignored in two of three runs. One run had 1 failure, frame::tests::waits_end_on_ticks_and_count_frames. That test is unrelated to this diff. It also failed on origin/main in 1 of 3 runs there (along with descriptor::host::tests::views_bound_while_another_slot_gets_its_texture_do_not_wait once), so it looks flaky under parallel tests.
  • cargo clippy -p victory-gpu --features diagnostics --all-targets -- -D warnings: clean.

Independent verification by Claude Code (Claude Sonnet 5.5), an AI assistant, run for oni-swr. No game files, dumps, or shader names were used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7168595800 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2601#issuecomment-7168595800). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-25T16:49:50Z. --- **Verification of `5e7cbb4`** **Environment:** Windows 11, D3D12 on the **WARP software adapter only** (`VICTORY_D3D12_WARP=1`, `cargo ... --features diagnostics`, which is what compiles that switch in; `VICTORY_DXIL` set to the Windows SDK `dxil.dll`). No physical GPU device was created. Not run on real GPU hardware. - Mergeable: `git merge-tree` against `origin/main` (`67e8ae1`) is clean. - `cargo test -p victory-gpu --features diagnostics --lib pending_host_write_pins_texture_until_executed_and_unpins_on_reset`: **1 passed**. I confirmed it runs on a real WARP device and is not skipped. - **Baseline-red:** with the `pending_host_writes == 0` condition removed from `evictable()` and the test kept, it fails. - Full `cargo test -p victory-gpu --features diagnostics --lib`: **199 passed, 0 failed, 1 ignored** in two of three runs. One run had **1 failure, `frame::tests::waits_end_on_ticks_and_count_frames`**. That test is unrelated to this diff. It also failed on `origin/main` in 1 of 3 runs there (along with `descriptor::host::tests::views_bound_while_another_slot_gets_its_texture_do_not_wait` once), so it looks flaky under parallel tests. - `cargo clippy -p victory-gpu --features diagnostics --all-targets -- -D warnings`: clean. _Independent verification by Claude Code (Claude Sonnet 5.5), an AI assistant, run for oni-swr. No game files, dumps, or shader names were used._
fuzeofficlal commented 2026-11-25 17:05:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T16:54:59Z.


Thanks @DrDarkDK — both concurrency windows are real and fixed in 883db7ad:

  1. Atomic destination acquisition + pin (Window 1): Introduced texture::PendingHostWrite and texture::prepare_host_write(resource), which acquires HostTexture and increments pending_host_writes via fetch_update rejecting EVICTED copies (detaching any claimed copy via resource::detach_if_same and filtering EVICTED entries out of shared_texture's MEMORY weak-reference cache before retrying). Updated copy_resource, copy_texture_region, resolve, and texture::alias_copy to obtain their target ID3D12Resource directly from the pinned PendingHostWrite and commit it via record(list) (dropping early on validation failure releases the pin without setting host_written).
  2. Claim before eviction (Window 2): In resource::give_up, each candidate from candidates() is now claimed atomically via pending_host_writes.compare_exchange(0, EVICTED, AcqRel, Acquire) before descriptor::evict or clearing resource.host. PendingHostWrite::record also sets a sticky HOST_WRITTEN bit on submission so pending_host_writes never returns to zero once committed. Any texture pinned after candidates() ran fails the CAS and is skipped untouched.
  3. Deterministic tests for both interleavings: Added stale_eviction_snapshot_skips_texture_pinned_after_candidates (Window 2) and prepare_host_write_detaches_claimed_evicted_copy_and_pins_replacement (Window 1) alongside the original copy/reset/readback test.

Re-verified on RTX 5090 (texture::tests 5/5, resource::tests 17/17, draw::tests host-write & eviction tests) and WARP --features diagnostics (full victory-gpu --lib: 201 passed, 0 failed), plus clippy -D warnings (default & diagnostics) and cargo xtask policy.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7168724255 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2601#issuecomment-7168724255). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T16:54:59Z. --- Thanks @DrDarkDK — both concurrency windows are real and fixed in `883db7ad`: 1. **Atomic destination acquisition + pin (Window 1):** Introduced `texture::PendingHostWrite` and `texture::prepare_host_write(resource)`, which acquires `HostTexture` and increments `pending_host_writes` via `fetch_update` rejecting `EVICTED` copies (detaching any claimed copy via `resource::detach_if_same` and filtering `EVICTED` entries out of `shared_texture`'s `MEMORY` weak-reference cache before retrying). Updated `copy_resource`, `copy_texture_region`, `resolve`, and `texture::alias_copy` to obtain their target `ID3D12Resource` directly from the pinned `PendingHostWrite` and commit it via `record(list)` (dropping early on validation failure releases the pin without setting `host_written`). 2. **Claim before eviction (Window 2):** In `resource::give_up`, each candidate from `candidates()` is now claimed atomically via `pending_host_writes.compare_exchange(0, EVICTED, AcqRel, Acquire)` **before** `descriptor::evict` or clearing `resource.host`. `PendingHostWrite::record` also sets a sticky `HOST_WRITTEN` bit on submission so `pending_host_writes` never returns to zero once committed. Any texture pinned after `candidates()` ran fails the CAS and is skipped untouched. 3. **Deterministic tests for both interleavings:** Added `stale_eviction_snapshot_skips_texture_pinned_after_candidates` (Window 2) and `prepare_host_write_detaches_claimed_evicted_copy_and_pins_replacement` (Window 1) alongside the original copy/reset/readback test. Re-verified on RTX 5090 (`texture::tests` 5/5, `resource::tests` 17/17, `draw::tests` host-write & eviction tests) and WARP `--features diagnostics` (full `victory-gpu --lib`: 201 passed, 0 failed), plus `clippy -D warnings` (default & diagnostics) and `cargo xtask policy`.
Machad3x commented 2026-11-25 19:06:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Machad3x on 2026-11-25T18:26:33Z.


Setup: RTX 3080 Ti (driver 621.24), Windows 11, a local stack of open PRs merged with main 67e8ae1.

Applied cleanly on that stack, together with #2598, #2576, #2611, #2626. One 10-minute game run reached gameplay with no device removal. These five PRs were not tested one by one, so nothing in that run is attributed to this PR alone.

Written with Claude Code; tested locally.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7170912470 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2601#issuecomment-7170912470). Originally posted by [Machad3x](https://github.com/Machad3x) on 2026-11-25T18:26:33Z. --- Setup: RTX 3080 Ti (driver 621.24), Windows 11, a local stack of open PRs merged with main 67e8ae1. Applied cleanly on that stack, together with #2598, #2576, #2611, #2626. One 10-minute game run reached gameplay with no device removal. These five PRs were not tested one by one, so nothing in that run is attributed to this PR alone. Written with Claude Code; tested locally.
Marvisak commented 2026-11-26 00:03:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T23:18:54Z.


Automated triage for the maintainers.

This pins a destination host texture against eviction from the moment a copy, copy-region, resolve or alias copy is recorded until the list's deferred host step runs or the list is reset. It adds texture::prepare_host_write/PendingHostWrite and a pending_host_writes counter on HostTexture. resource::give_up now claims each candidate with a CAS to an EVICTED bit, and shared_texture ignores claimed copies, which closes the race where evict_unused runs before run_deferred sets host_written. I checked that every non-test caller of texture::host_writes in list.rs and texture::alias_copy now uses the pin. host_copy and host_resource resolve the same resource.host(), so behaviour stays the same for buffers and missing copies, and early returns drop the guard and unpin. host_writes itself is now only used by the draw.rs test. This adds a new lock-free protocol (pin count plus EVICTED/HOST_WRITTEN bits), so it deserves a careful maintainer read. The three new tests cover pin/unpin on reset, a candidate snapshot taken before a pin, and a copy already claimed. It merges cleanly with current main. CI: passing (check). This is a recommendation for a maintainer to look, not an approval.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7177396415 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2601#issuecomment-7177396415). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T23:18:54Z. --- <!-- victory-triage v1 head=883db7ad1102f4ebf28cb01a52a0870ece1d63aa verdict=merge-candidate at=2026-11-25T23:18:45Z --> Automated triage for the maintainers. This pins a destination host texture against eviction from the moment a copy, copy-region, resolve or alias copy is recorded until the list's deferred host step runs or the list is reset. It adds `texture::prepare_host_write`/`PendingHostWrite` and a `pending_host_writes` counter on `HostTexture`. `resource::give_up` now claims each candidate with a CAS to an `EVICTED` bit, and `shared_texture` ignores claimed copies, which closes the race where `evict_unused` runs before `run_deferred` sets `host_written`. I checked that every non-test caller of `texture::host_writes` in `list.rs` and `texture::alias_copy` now uses the pin. `host_copy` and `host_resource` resolve the same `resource.host()`, so behaviour stays the same for buffers and missing copies, and early returns drop the guard and unpin. `host_writes` itself is now only used by the `draw.rs` test. This adds a new lock-free protocol (pin count plus `EVICTED`/`HOST_WRITTEN` bits), so it deserves a careful maintainer read. The three new tests cover pin/unpin on reset, a candidate snapshot taken before a pin, and a copy already claimed. It merges cleanly with current `main`. CI: passing (check). This is a recommendation for a maintainer to look, not an approval. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2601:upstream-pr/2601'
git switch 'upstream-pr/2601'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2601'
git switch 'upstream-pr/2601'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2601'
git switch 'upstream-pr/2601'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2601'
git switch 'main'
git merge --squash 'upstream-pr/2601'
git switch 'main'
git merge --ff-only 'upstream-pr/2601'
git switch 'main'
git merge 'upstream-pr/2601'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!2599
No description provided.