Skip to content

GPU: pin textures with pending host_writes against eviction - #2601

Open
fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/gpu-evict-pending-host-writes
Open

fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/gpu-evict-pending-host-writes

Conversation

@fuzeofficlal

Copy link
Copy Markdown
Contributor

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 DrDarkDK left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

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.

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

Copy link
Copy Markdown
Contributor Author

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
fuzeofficlal force-pushed the fix/gpu-evict-pending-host-writes branch from 5e7cbb4 to 883db7a Compare November 25, 2026 16:55
@Machad3x

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Owner

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

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.

5 participants