Repository navigation
GPU: pin textures with pending host_writes against eviction - #2601
fuzeofficlal wants to merge 1 commit into
Conversation
DrDarkDK
left a comment
There was a problem hiding this comment.
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.
|
Verification of Environment: Windows 11, D3D12 on the WARP software adapter only (
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.
|
Thanks @DrDarkDK — both concurrency windows are real and fixed in
Re-verified on RTX 5090 ( |
5e7cbb4 to
883db7a
Compare
|
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. |
|
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 Generated by Claude Code |
Summary
texture::host_writesdefers settingHostTexture::host_written = trueuntilHostList::run_deferredruns at submission time. InHost::execute,resource::evict_unused()runs beforelist.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()seeshost_written == falseand can evict the destinationHostTextureeven though a recorded command list is about to write to it viaCopyResource,CopyTextureRegion, orResolveSubresource. 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:HostTexture::pending_host_writes: AtomicUsize(initialized to0).evictable()to checkpending_host_writes.load(Acquire) == 0before!host_written.load(Acquire).crates/victory-gpu/src/texture.rs:host_writes(), incrementpending_host_writesand move aDropRAII guard (PendingHostWrite) into thelist.defer_host(...)closure that storeshost_written = true(Release). BecauseHostList::run_deferredborrows closures untilHostList::reset()/Drop, the pin stays active through execution and is released without leaking VRAM if an unexecuted list is reset or dropped.Acquireordering inis_host_written().pending_host_write_pins_texture_until_executed_and_unpins_on_reset, verifying: (1) an unexecuted list withCopyResourcepinsdstagainstgive_up_nowand unpins onReset, (2) a recordedCopyResourceblocks eviction before execution, and (3) after executing and resetting the list,dstremains un-evictable and reads back the copied[0xAA; 4]texels rather than stale0x40title memory.Verification
pending_host_write_pins_texture_until_executed_and_unpins_on_resetfails atpending CopyResource must pin dst against eviction.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 warningsand--features diagnostics— pass (0 warnings).cargo xtask policy— pass (184 files ok).