Skip to content

GPU: retain and retire released host textures, staging, shadow buffers, and descriptor resources across submissions - #609

Open
LAKSHYA-cmd-tech wants to merge 1 commit into
Marvisak:mainfrom
LAKSHYA-cmd-tech:fix/gpu-deferred-resource-retirement
Open

LAKSHYA-cmd-tech wants to merge 1 commit into
Marvisak:mainfrom
LAKSHYA-cmd-tech:fix/gpu-deferred-resource-retirement

Conversation

@LAKSHYA-cmd-tech

Copy link
Copy Markdown

Summary

Fixes #557.

When the title streams textures or reuses title buffer ranges, it drops CPU-side objects as soon as recording completes. On PC D3D12, dropping the corresponding host resources immediately deallocates GPU virtual address ranges and underlying physical memory, while GPU work (particularly multi-list submissions, indirect draws, and mesh ExecuteIndirect dispatches reading via descriptor tables) may still be executing or queued on the GPU. This resulted in DRED page faults:

page fault at 0x3a5f76000; inside freed ["texture 1024x1024 f98 flags 0x0 (type 34)"]

Root Cause & Solution

  1. HostTexture drop path: Resource dropped self.host: Option<Arc<HostTexture>>. When no active command list named the texture (e.g. indirect mesh draws or compute shaders reading via bindless descriptor heaps), HostTexture was freed immediately on the CPU. Implemented Drop for HostTexture to forward its ID3D12Resource to crate::host::release_late.
  2. Staging buffer drop path: texture::forget removed entries from STAGING, dropping Staging and releasing upload buffers while CopyTextureRegion might still be in-flight. Implemented Drop for Staging to forward its buffer to release_late.
  3. Shadow buffer drop path: shadow::unregister dropped title buffer shadows. Implemented Drop for Shadow to forward shadow.buffer to release_late.
  4. HostHeap descriptor slot replacement: HostHeap::keep now routes displaced resources to release_late instead of inline CPU destruction.
  5. Deferred Retirement Queue: Implemented RETIREMENT_QUEUE in crates/victory-gpu/src/host.rs with a submission retirement window (RETAIN_SUBMISSIONS = 3). Resources are held across multi-list submissions and drained only after the GPU fence has confirmed execution past their retirement window.

Verification

  • Unit test host::tests::deferred_retirement_retains_resources_across_submissions added and passing.
  • cargo test -p victory-gpu -- --test-threads=1: 44 passed, 0 failed, 1 ignored.
  • cargo clippy -p victory-gpu --lib -- -D warnings: 0 warnings.
  • cargo clippy -p victory-gpu --features diagnostics --lib -- -D warnings: 0 warnings.
  • cargo run -p xtask -- policy: 151 files ok.

@fromthemoonx

Copy link
Copy Markdown

Tested 6d8c72b merged onto current main (8e79c80, merges cleanly) on NVIDIA.

Environment: Windows 11 Pro build 26300, AMD Ryzen 7 5700X, 16 GB, NVIDIA GeForce RTX 3060 12 GB (driver 610.88), Rust 1.98.1 x86_64-pc-windows-msvc.

CI's commands, on the merge

  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • cargo test --workspace --locked: 312 passed, 0 failed, 6 ignored
  • cargo test -p victory-gpu --lib in parallel, 15 runs: all passed, including deferred_retirement_retains_resources_across_submissions

One problem: release_late takes the submission lock. current_submission() reads the counter through host.submitted.lock(), and Host::execute holds that same Mutex from preparation through the GPU wait to copy-back. Now that Drop for HostTexture, Drop for Shadow and Drop for Staging call release_late, a release waits for whatever submission is in flight. A throwaway test (not pushed) shows both cases on the merge:

result
another thread calls release_late while the lock is held for 500 ms (standing in for a submission's GPU wait) returned after 500 ms
a thread already holding the lock calls release_late did not return (3 s timeout): std::sync::Mutex is not reentrant
  • The first case is reachable today. A title thread that releases a texture, or unregisters a buffer shadow, while another thread submits now stalls until that submission's GPU work has finished. In the world that's 0.5–1.8 s per frame by the README.
  • The second case is latent. I didn't find a path in execute that drops the last Arc of one of these objects (the Shadow clones in back are also held by the lists' deferred steps). But anything added to execute later that does would deadlock.
  • A lock-free read would avoid both: a separate AtomicU64 bumped next to *submitted += 1, or the fence's GetCompletedValue() for the drain.

Smaller notes

  • About 170 of the changed lines in host.rs are rustfmt reflow of code this PR doesn't otherwise touch (SUBMISSION_US, COPY_BACK, the timing and DRED helpers, mod batch). It does reduce cargo fmt --check output for the crate (82 → 45 places), but it hides the actual change and will conflict with the other open PRs in host.rs.
  • The test checks that a released buffer is queued with retire_at == sub + 3 and is still queued after one submission. It doesn't cover the drain (that the resource is dropped once the window passes) or that the Drop impls route through release_late.

Not tested: the DRED page fault from #557 (needs the title); no game data used.

AI-assisted (Claude Code) ran the checks and drafted this comment.

@d1ll3x3

d1ll3x3 commented Nov 24, 2026

Copy link
Copy Markdown

checked: #609 on RDNA3 | base: main@8e79c80 + #609 + #568 + #67 (applied locally; one conflict with #568 in descriptor/host.rs describe_slot, resolved with #568's SlotState version) | on: Windows 11, RX 7800 XT (RDNA3, 16 GB), 32 GB RAM, diag build, VICTORY_D3D12_DRED=1, Xbox Series 1.0.2.1

result: the first mission's intro cutscene runs (0–1 fps), then the device is removed after 5 min 16 s with the signature #609 targets:

page fault at 0x4070ab000; inside freed ["texture 2048x1024 f72 flags 0x0 (type 34)", "texture 2048x2048 f72 flags 0x0 (type 34)"]
stopped at command 11 of 25 (op 6): dispatch 128x64x1 [compute 2dc602192abf6288]

At 0–1 fps the cutscene makes about 13 submissions a frame, so RETAIN_SUBMISSIONS = 3 is a fraction of a frame. Raising it locally to 512 (about 40 frames) did not help: removed after 2 min 39 s, inside freed ["texture 2048x2048 f98 …"], DRED stopped at command 418 of 562 (op 15), a ResourceBarrier. Earlier stops on other bases were op 8 (CopyTextureRegion) and op 6 (Dispatch). So some freed textures are named directly by recorded commands (barriers, copies), not only through heap slots, and a longer window does not cover them.

For comparison on the same machine and base, #431 + #568 + #67 (without #609) ran 11 min 39 s; details on #431.

Run-to-run duration varies (2.5–6 min with the same build), so treat the timings as a weak signal.

AI-assisted (Claude Code).

@alexander-src alexander-src 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.

Reviewed and locally tested 6d8c72b merged cleanly onto main 8e79c80 (Windows 11 26200, RTX 5090 driver 621.24, Rust 1.98.1):

  • cargo clippy --workspace --all-targets --locked -- -D warnings: pass
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: pass
  • cargo test -p victory-gpu --locked -- --test-threads=1: 51 passed, 0 failed, 1 ignored

A few code-level issues worth addressing before merge:

  1. release_late() blocks every background thread on Host::submitted for the full GPU wait (and risks re-entrant deadlock).
    release_late() calls current_submission(), which locks h.submitted (crates/victory-gpu/src/host.rs:264-268). Host::execute() holds self.submitted.lock() across the entire submission, including WaitForSingleObject(self.event, INFINITE) (host.rs:496-607), which lasts 0.5–1.8 s per world frame. That stalls any recording or streaming thread hitting HostHeap::keep, HostTexture::drop, Staging::drop, or Shadow::drop until the GPU submission finishes, and makes any Drop firing while self.submitted is held (e.g. an Arc<Shadow>/Arc<Staging> whose last reference drops inside Host::execute) self-deadlock on the non-reentrant Mutex. Tracking the submission counter in an AtomicU64 (read with Ordering::Acquire in current_submission(), updated inside Host::execute) avoids both.

  2. Empirical status against #557.
    Independent RX 9070 XT DRED runs in #557 (comments by @KingKaom and @h0useadv, including testing this PR directly) still hit the freed-texture page fault with RETAIN_SUBMISSIONS = 3, and the latest kept-scan shows the faulted texture already had arc=1 and zero HostHeap::kept slots 3.2 s before the fault (consistent with unbounded root-SRV reads past a shadow buffer into freed VRAM holes, which #493 addresses). So this probably shouldn't claim Fixes #557 unless narrowed to the real cross-thread recording/descriptor-replacement hazard.

  3. Diff noise.
    The branch includes unrelated rustfmt reformatting across descriptor/host.rs, host.rs, resource.rs, and shadow.rs, which makes review and rebasing harder.

AI-assisted code review and local test run; no game data.

@dpalacios69

Copy link
Copy Markdown
Contributor

Thanks for putting this together! Looking through the diff, locking Host::submitted inside current_submission() introduces two deadlocks and heavy lock contention that should be fixed before this can land:

  1. Same-thread self-deadlock in Host::execute (crates/victory-gpu/src/host.rs):
    Host::execute acquires let mut submitted = self.submitted.lock()... at line 550 and holds it until the end of the function (line 689). back: Vec<(Arc<Shadow>, usize, usize)> is declared later (line 655), so Rust drops back before submitted. Whenever back holds the last Arc<Shadow> (for example, if a title thread unregisters the buffer and resets the command list after the bottom-of-pipe completion writes at lines 673–677), dropping back at line 689 triggers Shadow::drop → release_late → current_submission() → h.submitted.lock() on the same thread already holding the non-reentrant Mutex, deadlocking permanently. The same applies to any Arc<Shadow>, HostTexture, or Staging dropped while Host::execute holds submitted.

  2. AB-BA deadlock between shadow::BUFFERS and Host::submitted:

    • shadow::unregister (shadow.rs:401) runs buffers().remove(&(start, resource));, which drops the removed Entry (and thus Shadow::drop → release_late → host.submitted.lock()) before the temporary MutexGuard on BUFFERS is released (BUFFERS → Host::submitted).
    • On device removal, Host::execute holds Host::submitted while calling self.removal_data(runs) → shadow::near(va) (shadow.rs:472), which acquires BUFFERS (Host::submitted → BUFFERS).
      If a title thread unregisters a buffer during a GPU hang, DRED reporting can hang permanently instead of logging the crash.
  3. Contention against the GPU fence wait:
    Because Host::submitted is held across WaitForSingleObject(self.event, INFINITE), calling h.submitted.lock() in current_submission() stalls any worker thread releasing a texture/shadow/staging buffer or overwriting a descriptor slot (HostHeap::mirror holds HostHeap::made across keep/release_late) for the entire duration of the GPU submission.

    Suggested fix for (1)–(3): Mirror the submission counter in a separate AtomicU64 (updated when *submitted += 1 and loaded in current_submission() without locking Host::submitted), and drop the submitted guard before back/to_drop.

  4. A few smaller notes:

Static code review only (not executed on hardware); AI-assisted.

@justr4mzi

Copy link
Copy Markdown

Tested PR #609 (6d8c72bc) integrated cleanly onto main (8e79c80) inside a 13-PR combined stack on Windows 11 with an AMD Radeon RX 6700 XT (RDNA2) and Rust 1.98.1 (x86_64-pc-windows-msvc).

  • Clean merge alongside #688, #652, #346, #472, and #677 (plus 7 shader PRs).
  • cargo test -p victory-gpu -- --test-threads=1: 64 passed, 0 failed, 1 ignored (host::tests::deferred_retirement_retains_resources_across_submissions passes); --features diagnostics: 67 passed, 0 failed, 1 ignored.
  • Full workspace (cargo test --workspace -- --test-threads=1): 329 passed, 0 failed, 6 ignored; cargo xtask policy: 155 files ok.
  • Full combined stack notes: Testers post specs and test results here #49 (comment)
  • Not yet tested against a live title launch (no game data included).

AI-assisted verification and stack testing (reviewed locally).

@rpmaster2

rpmaster2 commented Nov 24, 2026 •

Copy link
Copy Markdown

Tested on RDNA4: Radeon RX 9070 XT 16 GB, Adrenalin 26.11.2 (32.0.32029.2010), Windows 11 26300, Ryzen 5 5600, 32 GB RAM. Diag build, VICTORY_D3D12_DRED=1, this branch merged on main @58fed37, 480 s runs, the same save restored before each run.

A/B, identical start state:

build crash after lists in the failing submission max frame
main 1m39s 30 1091
this PR 3m21s 11 1186

Both end in host GPU device removed, and both are a page fault inside a freed host texture:

  • main: page fault at 0x4721d6000; inside freed ["texture 1024x1024 f95 flags 0x0 (type 34)", "texture 1024x1024 f83 flags 0x0 (type 34)"]
  • this PR: page fault at 0x55c25c000; inside freed ["texture 1024x1024 f98 flags 0x0 (type 34)", "texture 1024x1024 f78 flags 0x0 (type 34)"]

The PR does get further: in the same window it wrote a new checkpoint save, and main wrote none. But the removal is unchanged, so on this host it is not the whole of #557.

Context from six earlier runs (3x main, 2x #688, 1x this PR before the A/B): all died the same way, with 1024x1024 f98/f99/f95/f83, 1024x2048 f98, 2048x2048 f98, 512x512 f29 and 256x256 f99, always type 34. In two runs the stopped list was 7d85c9cab3c80721, whose tables come from shader f0c7efd2aceb2aaa and name placed SRVs. The log also reports a title texture was released while a list not yet reset names its host copy before the removal, which is the path this PR targets.

One correction to my earlier wording in #597, in case it misled anyone: the view heap ... past its end text in the DRED line is only where the fault address lies relative to the heaps (a texture is outside them, so it always prints that); it is not evidence of a wrong heap handle. The meaningful part is inside freed [...].

The crash is deterministic here (every run within 8 minutes, frames 1090-1290), so any probe is easy to check. If you want, I can run with extra logging you point me at, or capture the draws of the stopped list.

No game data: texture sizes, formats, shader hashes and counters only.

AI-assisted: written by DeepSeek (OpenCode harness) from my run logs and observations; the hardware, the dump and the runs are mine.

@rpmaster2

rpmaster2 commented Nov 24, 2026 •

Copy link
Copy Markdown

More data, and it does not support the window being the problem.

I built this branch locally with RETAIN_SUBMISSIONS = 64 instead of 3 (no other change) and ran it from the same restored save:

build window run result
main - 1 removed at 1m39s
this PR 3 1 removed at 3m21s
this PR, local 64 1 alive for 8m (killed), 19 black frames, 0.4-2 fps
this PR, local 64 2 removed at 2m44s

The 64-window removal has the same signature again: page fault at 0x473c99000; inside freed ["texture 1024x2048 f72 flags 0x0 (type 34)"], 30 lists.

So a 64-submission window makes the removal rarer in one run and not in the next; the stale reference either outlives a much longer window than 3, or it comes from a path that does not go through release_late at all. Both would explain why the crash moved from 1m39s to 3m21s with this PR but never disappeared.

If it is useful, I can instrument locally: print every release_late and HostHeap::keep with the resource size and format, plus the slot writes for 1024x1024 f95/f98/f99/f83 and 1024x2048 f72/f98 (the formats that keep faulting here), then correlate with the stopped list 7d85c9cab3c80721. Tell me which log you want and I will produce it - the crash is still reproducible within a few minutes.

No game data: sizes, formats, hashes, counters only.

AI-assisted: written by DeepSeek (OpenCode harness) from my run logs and observations; the hardware, the dump and the runs are mine.

giritsu pushed a commit to giritsu/VIctory that referenced this pull request Nov 24, 2026
…s (PR Marvisak#693)

Allocate Shadow host copies in D3D12_HEAP_TYPE_DEFAULT VRAM and stage
dirty 64 KiB page uploads and pre/post GPU-write readbacks through
pooled 4 MiB StagingArenas, integrated with bindless prepare (Marvisak#413),
page-coalesced refreshes (Marvisak#393), and lock-free retirement (Marvisak#609).
Update raw_view_tests::unknown_stride_reads to stage via DEFAULT buffers.
Callimann8 added a commit to Callimann8/VIctory-fork that referenced this pull request Nov 24, 2026
Callimann8 added a commit to Callimann8/VIctory-fork that referenced this pull request Nov 24, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Daniel333313131

Copy link
Copy Markdown

Tested on AMD (RDNA3), where main faults on freed textures in the world. With this PR the freed-resource faults remain, in all three runs.

Setup: AMD Radeon RX 7900 XTX 24 GB, driver 32.0.32029.2010; Ryzen 7 9800X3D, 32 GB; Windows 11 10.0.26300. Diag build of this PR's head 6d8c72b merged alone onto main ffe310e, compared with main itself. Each run used its own empty VICTORY_CACHE_DIR, VICTORY_D3D12_DRED=1, and VICTORY_KEYS set to Enter every 3 s from 8 s to 47 s. Each run was stopped after 180 s. Method and main numbers as in #400 (comment).

run world ms/frame watches on the GPU device removed DRED
main 1 972 140 ms 178 143 s page fault inside freed texture 2048x2048 f98 (type 34) ×2
main 2 873 139 ms 179 170 s page fault, no resource named
main 3 679 86 ms 157 76 s page fault inside freed texture 2048x1024 f98 (type 34)
#609 1 1430 34 ms 265 75 s page fault inside freed title buffer 0xcbb800000+0x455d80 (type 34)
#609 2 1089 38 ms 186 89 s page fault inside freed texture 2048x2048 f98 (type 34)
#609 3 1005 32 ms 148 114 s page fault inside freed texture 2048x2048 f99 (type 34)

For comparison, #431 on the same machine and method had no freed-resource fault in three runs, and two of them lasted the full 180 s. Details are on that PR.

AI use: Claude Code (Claude Opus 5.5) merged and built the commits, ran the title, summarised the log lines and wrote this comment. The numbers come from the runs above.

@J-DB0

J-DB0 commented Nov 24, 2026

Copy link
Copy Markdown

Tested this PR (head 6d8c72b) on an RX 9070 XT (RDNA 4) with an i5-14600KF, in a diag build with VICTORY_D3D12_DRED=1.

It doesn't stop the world crash on this card. On main and with this PR alike, the title loads into the world, runs for a while, then the device is removed by a page fault inside freed textures while mesh shaders draw:

  • main: page fault at 0x486bfc000; inside freed ["texture 1024x1024 f99 flags 0x0 (type 34)"], a list stopped at command 2183 of 3383 (op 42), around draws of [mesh f0c7efd2aceb2aaa, pixel 7d85c9cab3c80721]
  • this PR: page fault at 0x46356f000; inside freed ["texture 2048x2048 f71 flags 0x0 (type 34)", "texture 2048x2048 f71 flags 0x0 (type 34)", "texture 2048x1024 f99 flags 0x0 (type 34)"], a list stopped at command 788 of 856 (op 42), mostly draws of [mesh 51ff151f75cd5759, pixel 7fe75645c406cac6]

Time in the world before the crash varies from about 15 s to 2.5 min across my runs; with this PR it was 83 s, so no visible change. I can run another build or turn on GBV if that would help.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-gpu/src/descriptor/host.rs, crates/victory-gpu/src/host.rs, crates/victory-gpu/src/resource.rs, crates/victory-gpu/src/texture.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

geiyiliyabumo pushed a commit to geiyiliyabumo/VIctory that referenced this pull request Nov 25, 2026
GPU: count a refresh that copies nothing as skipped in the copy statistics
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.

World entry after #336: device removed reading freed streaming textures (page fault inside freed texture 1024x1024)

10 participants