GPU: pin textures with pending host_writes against eviction #2601

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

@⁠fuzeofficlal on GitHub · 2026-11-25 16:35 UTC · original
fuzeofficlal:fix/gpu-evict-pending-host-writes → main · +380 −22 · 3 files · 1 commits · open

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).
**@⁠fuzeofficlal** on GitHub · 2026-11-25 16:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/2601) `fuzeofficlal:fix/gpu-evict-pending-host-writes` → `main` · +380 −22 · 3 files · 1 commits · open ## 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`).
texture::host_writes defers setting HostTexture::host_written until HostList::run_deferred, which runs after Host::execute calls resource::evict_unused() (or while another queue waits). Between recording CopyResource, CopyTextureRegion, ResolveSubresource, or texture::alias_copy and submission-time run_deferred, eviction could remove the destination HostTexture so GPU writes landed in an orphaned copy.

Use a lock-free EVICTED/HOST_WRITTEN bit protocol on HostTexture::pending_host_writes paired with an RAII PendingHostWrite guard:
- texture::prepare_host_write atomically acquires the current HostTexture and increments pending_host_writes via fetch_update (rejecting copies with EVICTED set, detaching them via resource::detach_if_same, and retrying after shared_texture filters out EVICTED copies). Recording callers (copy_resource, copy_texture_region, resolve, alias_copy) obtain their target ID3D12Resource from the pinned PendingHostWrite and move the guard into list.defer_host via record(), or drop it on early return without marking host_written.
- PendingHostWrite::record sets HOST_WRITTEN alongside host_written before the guard's Drop releases the count, preventing pending_host_writes from ever returning to zero once committed.
- resource::give_up atomically claims candidates via compare_exchange(0, EVICTED) before descriptor::evict or clearing host pointers, skipping any candidate pinned or marked after candidates() ran.

Verified on hardware (RTX 5090) and WARP (--features diagnostics), including deterministic Window 1 (EVICTED claim race) and Window 2 (stale candidates snapshot race) regression tests.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:10:07 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 16:49 UTC · original

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.

**@⁠oni-swr** on GitHub · 2026-11-25 16:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/2601#issuecomment-7168595800) **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._
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 16:54 UTC · original

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-25 16:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/2601#issuecomment-7168724255) 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`.
Author
Owner

@⁠Machad3x on GitHub · 2026-11-25 18:26 UTC · original

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.

**@⁠Machad3x** on GitHub · 2026-11-25 18:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/2601#issuecomment-7170912470) 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.
Author
Owner

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

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

**@⁠Marvisak** on GitHub · 2026-11-25 23:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2601#issuecomment-7177396415) <!-- 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 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/2601/head:github-bot/gh-2601'
git switch 'github-bot/gh-2601'

Merge

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