D3D12.X: scope resource_at by device and clean up on release (#248) #292
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!292
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-292"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@heretic1321 on GitHub · 2026-11-23 22:29 UTC · original
heretic1321:fix/title-texture-lifetime-248→main· +210 −13 · 4 files · 1 commits · draftRe-scoped onto
upstream/main(47efa2e, head8eb79c2), kept as draft. Part of #248.Re-scope after #288, #866, and review feedback
#269base andtitle_texturesoverlap-retirement:#288landed onmainusingvram::Pagesand already includesshadowed/retire_overlapping/unmap_superseded. Addressing live non-overlapping texture growth (@emirduran707's 9.8 GB RX 9060 XT table /@albertoglvn's 8 GB census) belongs to opt-in page budgets such as#640, not overlap retirement.MADE_MAX/ 2 sMADE_COOLeviction: per@qinrxx(points 1–2) and@Cattn, D3D12 descriptors hold no COM reference, so dropping aMADEresource while descriptors may still exist in reused heaps is unsafe.What is in it
crates/xcl/src/hle/d3d12x/real.rs:ResourceAtCachekeysMADEby(device, va, desc)(previously(va, desc)).resource_atreturns an ownedOption<ID3D12Resource>cloned while holding theMADElock sodev_create_srv_at_x/dev_create_uav_at_xhold a live COM reference through descriptor creation (@Cattn's lifetime fix).ID3D12Device::Release(dev_release). A cached resource is a COM child and holds the device, so theReleaseof the last outside reference returns 1 and a teardown placed after that call never runs (@WebIdentity1reproduced this on two machines). The hook therefore tears the device'sMADEentries down first when at most this caller's andreal::init's process-lifetime reference remain (routing finalID3D12Resource::Releasethrough#866'slate_releasewhile upload queues remain registered), thentitle_texturesstate for that device, and the device really reaches zero.vram::on_queue_releasefromq_release.resource_at_tests::resource_at_cache_scopes_by_device_and_tears_down(holdsgating, per-device isolation, idempotent teardown).crates/xcl/src/hle/d3d12x/resource_lifetime_probe.rs: two on-device regression tests adapted from@WebIdentity1's reproducer — a raw COM-child teardown control and the productionresource_atmiss/hit/teardown path — both assert the cache is empty and the device reached 0. They skip without a GPU.crates/xcl/src/hle/d3d12x/vram.rs:on_queue_release(queue)clearing released queues from every device'spages.waitedmap so reused queue pointers cannot skip mapping-fence waits.crates/xcl/src/hle/d3d12x/title_textures.rs:Table::teardown/teardown(device)removing entries for a released device fromby_res/by_va/shadowed/shadowed_by_va/LAST_CONTENT/QUEUES/WATCH_CACHE.Statsandstats()(active,shadowed,covered_bytes), plus unit testtable_teardown_and_stats_are_scoped_by_device.crates/xcl/src/host.rs:; title textures {} ({} MB covered, {} shadowed)on theXCL_ACTIVITYmemory:line.Tested
1.98.1, head8eb79c2onupstream/main47efa2e:AR_x86_64_pc_windows_msvc=llvm-ar cargo check --release --workspace --target x86_64-pc-windows-msvc— exit 0 (15.12s, 0 errors, 0 warnings).AR_x86_64_pc_windows_msvc=llvm-ar cargo check --tests --workspace --target x86_64-pc-windows-msvc— exit 0 (8.95s, 0 errors, 0 warnings).python3 scripts/pr_check.py --localverified all required sections and asset/model checks (failing only on the missing Windowsxcl-smokeblocks for8eb79c2).@WebIdentity1one0e3910— RTX 3090 driver 591.74, Windows 10 19045, Ryzen 7 7800X3D, Rust 1.98.1 MSVC, DXC 1.8.2502.11:cargo build --locked --workspacesucceeds onmainand the head; full suite720 passed / 1 failed / 41 ignoredon the head against718 passed / 1 failed / 41 ignoredonmain(same pre-existing integer-CLAMP failure on both, four explicit skips each, the two added PR tests pass). Their cache-teardown finding reproduced on the RTX 3090 and on AMD integrated graphics; it is addressed by the release-hook change below.bf8e16c(@grahamster2RTX 3080,@qinrxxd3d12x::tests) were on the#269-stacked version and do not cover this re-scoped commit8eb79c2.Regression run
8eb79c2(draft; no Windows GPU host here).Not tested
cargo test --workspaceexecution, GPU tests,scripts/smoke.ps1/ retail title runs on8eb79c2, and multi-device teardown in a real title. Both new probe tests and@WebIdentity1's numbers are one0e3910; the release-hook fix and the probes have not been run on a GPU yet.AI use
T3 Code / Antigravity, Orchestrator (Neon) / gemini-pro-orchestrator-neon: re-scoped ontoupstream/main(47efa2e), fixedresource_atowned COM reference return and removed unsafe mid-device eviction per reviews, updatedvram::on_queue_releasefor#288's per-devicePAGESmap, added unit tests, and ran Linuxcargo checkandscripts/pr_check.py --local.Rebase note (
47efa2e)main(wait_budget,resolve_within(&Pending, …),Pending::late) from theXCL_PSO_WAIT_MSwork. Instead of keeping a second copy,resolve_for_drawnow builds onmain'sresolve_within/wait_budget: this branch keeps the draw-specific 4 s default, the compute bypass, thestandin_drawscounter and the long-wait log, and drops the duplicated wait loop.main'sreal.rs/title_textures.rs/vram.rs/host.rsand dropped this branch's half of the PR; it is restored in8eb79c2, which is why the device-scoping and teardown work is back in the diff.cargo check --release --workspaceand--testsforx86_64-pc-windows-msvcon head8eb79c2: 0 errors, 0 warnings.mainreaches on Windows; the obstacle is replaced vtables — proposal: proxies #175@grahamster2 on GitHub · 2026-11-23 23:03 UTC · original · edited 2026-11-24 00:18 UTC
Retail run of
bf8e16con a 10 GB NVIDIA card: the retail title (Series 1.0.2.1), RTX 3080, driver 610.88, warm caches,XCL_PRESS_A_AT=30,40,50,60,70,80,95,110, 180 s, two runs. All four heads are compared in https://github.com/ivall/xcl/issues/248#issuecomment-7116498970.0x8007000e+0x2926872, 145 s+0x13936, 95 smain(two runs)+0x13936128 s /0x80000003+0x193e00f138 sThe access violation at
+0x13936is also one ofmain's endings, and this run ended at 9.3 GiB, well under the card. The title textures counter at the end: 19 330 active, 4 829 MB covered, 31 003 retired (run 1); 16 349 / 1 963 MB / 14 997 (run 2).No device removal, and no new warning kinds compared with
main. Correctness wasn't checked: the loading frames are a dark fade, and I can't look at them.xcl-smoke, 60 s
Claude Code (Claude Opus 5.5) ran the tests and wrote this report. No game files attached.
@emirduran707 on GitHub · 2026-11-24 00:33 UTC · original
A data point for the retirement part of this PR, on current
main(where #288 is in and #269 is closed, so this branch conflicts now).RX 9060 XT 16 GB, 32 GB RAM, release,
maina6f5f7a+ #440 (video at speed) + #300 + #435, the same title, A pressed by hand,XCL_ACTIVITY=1. While the world streams in (from ~60 s), the tiled-texture pages (thetarget pagesfigure on the activity line, which since #288 counts targets and sampled textures on one shared book) keep growing, with noE_OUTOFMEMORY:Pages are shared per address on
main, so this is distinct addresses covered by resources the title has not released, which is what the retirement here addresses. Are you planning to rebase it ontomain? I can run the same measurement on this card when it is.AI use: Claude Code with Claude Opus 5.5 read the run's log and wrote this comment from my run.
@Cattn on GitHub · 2026-11-24 00:49 UTC · original
From a static review (no builds or runs) of
bf8e16c:resource_atreturns a borrowed COM pointer, which the caller uses to create a descriptor after the cache lock is released. Another caller can retire the entry and drain the cache's reference after the two-second cooldown, leaving descriptor creation or submitted GPU work without a live resource if that was the final reference. This is a resource-lifetime bug, and I found no indication of malicious code.Hold a COM reference through descriptor creation and defer GPU retirement until the relevant queue fences complete.
github.com/ivall/xcl@bf8e16cdc2/crates/xcl/src/hle/d3d12x/real.rs (L2136-L2149)github.com/ivall/xcl@bf8e16cdc2/crates/xcl/src/hle/d3d12x/real.rs (L2077-L2095)Review by Claude Code (Claude Opus 5.5) with a gpt-6.2-sol audit subagent; I checked each point against the code.
@albertoglvn on GitHub · 2026-11-24 05:34 UTC · original
An 8 GB data point on how much the retirement here could free on the newer retail title (TitleID 7952987A). It's from a page census, not from a run of this branch, since it conflicts with main.
Machine: RTX 4060 Laptop 8 GB (7956 MiB), 15.2 GB RAM, Windows 11 26300, driver 621.24. Build: main
f4ce27c+ #495 + #496 + #640 with its budget off, plus a local patch that counts each 64 KiB page once, under the strongest of its holders (details in https://github.com/ivall/xcl/pull/640#issuecomment-7122239021). At 62 s:Pages are shared per address on main, so retiring an overwritten placement frees only the pages no other placement still holds. If the overwritten textures are among those whose T# is in no view heap, which I'd expect but didn't check, the last row is the most this can free here: about 1.1 GB, and less in practice, since most of it was under 2 s old.
On this machine that wouldn't move the end much. The out-of-memory stop comes when local plus non-local video memory fills the card plus Windows' shared GPU memory (https://github.com/ivall/xcl/issues/333#issuecomment-7122935494): 14.8 of about 15.7 GB at the refusal. On cards where the pages keep growing well past that, like emirduran707's table above, it may matter much more.
When this is rebased onto main, I can measure it here with the budgets logged at the refusal.
AI use: Claude Code (Claude Opus 5.5) wrote the census patch and this comment and read the logs; I ran the title.
@heretic1321 on GitHub · 2026-11-24 09:46 UTC · original
Re-scoped cleanly onto
upstream/main(6bac4b4) ate0e3910947bfeaf74fb38b52c0bafa0bae10aaa5(0 errors, 0 warnings onAR_x86_64_pc_windows_msvc=llvm-ar cargo check --release --workspaceand--tests; kept as draft until Windows/GPU tests run).Thank you everyone for the thorough reviews and measurements:
#269and thetitle_texturesoverlap-retirement changes since#288(vram::Pages,shadowed,retire_overlapping,unmap_superseded) is now onmain. As @emirduran707's 9.8 GB RX 9060 XT table and @albertoglvn's 8 GB census show, the remainingtarget pagesgrowth onmaincomes from live distinct addresses rather than overlaps (addressed by opt-in page budgets like#640), so this PR is re-scoped strictly toresource_atsafety/scoping and release cleanup.MADEeviction (@Cattn, @qinrxx points 1–3):MADE_MAXcreation-order eviction and the 2 s wall-clockMADE_COOLtimer, since D3D12 descriptors hold no COM reference (@qinrxx).resource_atCOM lifetime (@Cattn):resource_atnow clones and returns an ownedOption<ID3D12Resource>while holding theMADElock, anddev_create_srv_at_x/dev_create_uav_at_xhold that reference acrossdev_create_srv_x/dev_create_uav_x.MADEis keyed by(device, va, desc);dev_releasedrains device-ownedMADEentries (whose finalID3D12Resource::Releaseroutes through#866'slate_releasewhile upload queues are still alive) beforetitle_textures::teardown(Some(device));q_releaseclearsvram::Pages::waitedviavram::on_queue_release.XCL_ACTIVITYstats: logstitle textures <active> (<covered> MB covered, <shadowed> shadowed)on thememory:line.What is still needed before marking ready for review: Windows
cargo test --workspace -- --test-threads=4and ascripts/smoke.ps1pair (mainvse0e3910).@heretic1321 on GitHub · 2026-11-24 16:28 UTC · original
@WebIdentity1 thank you — the full-suite counts, the four skips and the same pre-existing integer-CLAMP failure on both revisions make this the most useful run on the PR, and the teardown finding is correct: I reproduced the mechanism from the code (
ID3D12Resourceis a COM child and holds its device, so the app's lastReleasereturns 1, not 0, and myif n == 0teardown sat on the branch that could never run).Fixed on
8eb79c2rather than left as a diagnostic:dev_releasenow tears the device'sMADEentries down before forwarding when at most the caller's andreal::init's process-lifetime reference are left (refs()= AddRef+Release, unchanged count), so the device really reaches 0 and the teardown is no longer unreachable.title_textures::teardownmoves with it. Cached resources are rebuilt on their next lookup, and owner-cloned references keep their own COM ref, so nothing is freed under a live descriptor.ResourceAtCache::holdsgates the teardown, so the hook stops tearing down a device with no entries.crates/xcl/src/hle/d3d12x/resource_lifetime_probe.rs, adapted to pass instead of intentionally failing: both now assertReleasereturns 0 and the cache is empty. They skip without a GPU.Since you have the hardware and I don't: could you run the same two filters on
8eb79c2(native_cached_resource_device_teardown,resource_at_device_teardown_path), plus the full suite, againstmain? That would also cover thexcl-smokepair this needs. The rebase onto47efa2ealso re-restored thereal.rs/title_textures.rs/vram.rs/host.rshalf of the PR that an earlier rebase had silently dropped, so the diff is now the intended one.@WebIdentity1 on GitHub · 2026-11-24 16:21 UTC · original · edited 2026-11-24 16:35 UTC
Native Windows validation of
e0e3910947bfeaf74fb38b52c0bafa0bae10aaa5, plus a reproduced cleanup gap.Tested
RTX 3090 driver 591.74, Windows 10 19045, Ryzen 7 7800X3D, Rust 1.98.1 MSVC, DXC 1.8.2502.11, Agility 1.620.2. Unmodified source, debug builds, interactive desktop session:
cargo build --locked --workspacesucceeds on both revisions.cargo test --locked --workspace --no-fail-fast -- --test-threads=4 --show-output:6bac4b4419886acc940d479dcf8815d0b1804435:718 passed /1 failed /41 ignored.Cleanup finding
dev_releasewaits for native Release to return zero before dropping MADE. A cache-owned native resource keeps the device at one, so this cleanup does not run. A local GPU diagnostic calls productionresource_atfor a synthetic64KiB buffer, verifies cache miss/hit identity, and drops both returned references:Reproduced independently on the RTX 3090 and AMD integrated Graphics (driver32.0.21030.2001): each targeted test0 passed /1 failed /0 ignored, no skips/timeouts. The failed assertion checks the retained cache entry; fixture cleanup occurs before asserting. A second real-COM-child probe reproduces the same dependency. Patch below adds only diagnostics and intentionally fails.
The old cache already retained resources; this finding limits the new cleanup claim. Device scoping and owned lookup results remain useful. An explicit owner/session teardown boundary would need to release cache-owned resources before the final device reference, while preserving descriptor lifetimes.
real::initalso intentionally holds a process-lifetime device reference.Not tested
Retail title/smoke pair, actual texture descriptor execution, in-flight destruction, game-memory growth or gameplay. The production-entry probe uses the committed-buffer fallback outside UMA. Deliberately failing diagnostics are separate from the unmodified full-suite counts above.
AI use: Codex (GPT-6) wrote the diagnostics, ran the native tests and prepared this report. No competing runtime fix.
Reproducer patch against
e0e3910Run each probe in its own process with
cargo test --locked -p xcl --lib native_cached_resource_device_teardown -- --test-threads=1 --show-output --nocaptureand the same command with filterresource_at_device_teardown_path. SetXCL_ADAPTER=3090orAMD.Follow-up on the requested
8eb79c2run (November24)@heretic1321 I fetched the named commit directly from your fork:
8eb79c2ace0a5c5c9946ce91b9d38aac487a3be0. Both the PR head andfix/title-texture-lifetime-248currently still point toe0e3910.The fetched8eb tree differs from the described fix:
dev_releasestill forwards Release first and tears down only whenn == 0.ResourceAtCache::holdsand the new count-based branch are absent.resource_lifetime_probe.rsis present but not declared as a module. On native Windows, each requested filter with-- --listreports 0 tests,0 benchmarks (exit0); that is test discovery, not passing GPU validation.cargo test --locked -p xcl --lib native_cached_resource_device_teardown -- --listfail compilation (exit101):E0425 cannot find function refs in this scope, atresource_lifetime_probe.rs:70. No GPU test executes. I preserved this four-line diagnostic patch and restored the original source afterward.Please push/link the revision containing the intended release-hook change, its helpers and module declaration. I can then run the focused native checks and, if appropriate, the suite. Before accepting a count threshold, it also needs cases with more than one cached child and retained descriptor/device owners; the one-child probe alone does not establish descriptor lifetime safety.
One scope correction: these are two adapters in one Windows machine. We have no retail files here, so neither these filters nor a full workspace run can supply the required
xcl-smokeretail pair.AI use: Codex (GPT-6) inspected the exact Git objects and ran the Windows discovery/compile diagnostics. No runtime fix was fabricated from the prose description.
@Toons29 on GitHub · 2026-11-25 02:53 UTC · original
Regression run of
e0e39109on the retail title, for this PR's Regression run section. Both blocks are fromscripts/smoke.ps1as printed, not edited:mainon4178e888, this PR on its head commit with no uncommitted changes, same title and time.Against the limits in CONTRIBUTING.md: 100 % of
main's frames, 100 % of its peak commit, no crash, no device removal.AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0,
cargo build --release -p xcl, 60 s,XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.AI use: Claude Code (Claude Opus 5.5) built both trees and ran
scripts/smoke.ps1; I checked the blocks before posting.@DavidM1911 on GitHub · 2026-11-25 14:43 UTC · original
Verification on RTX 3060 Ti (Windows 11): merge conflict with current
main— needs a rebase before it can be tested.@qinrxx on GitHub · 2026-11-24 02:05 UTC · original · reviewed
Review of
bf8e16c(with8111cf9from #269 under it), on correctness and tests only. The diff was read againstmain2997c9a.What I ran:
cargo test -p xcl --lib d3d12x:: -- --test-threads=4on the PR head (base63b9930): 84 passed, 0 failed, 1 ignored.overlapping_placements_retire_old_release_and_teardown,resource_at_cache_retires_cools_caps_and_tears_down).title_textures_at_one_address_share_video_memory(RTX 5080, not skipped).main, because the PR doesn't merge (below); the workspace suite; the retail title.0. State against
main: this needs more than a rebase.vram::Pages, one reserved resource per target, with tiles mapped onto shared 64 KiB pages.mainhas novram::Book,Spaceorplace_title_texture. A merge of this branch intomainconflicts indevice_x.rs,title_textures.rsandvram.rs.main'stitle_texturesalready retires overlapped entries when a newer placement covers their range (retire_range,title_textures.rs:141onmain). TheRegistry/by_endretirement andvram::detach_title_texturehere cover the same ground, built on #269's types.real.rs(theresource_atcache,dev_release,q_release) andhost.rs, which merge intomainwithout conflicts.main, with its unit test. Points 1–3 are about it.1. Releasing a
resource_atresource can leave descriptors pointing at freed memory.resource_atmakes a resource and writes an SRV/UAV for it into the title's descriptor (dev_create_srv_x(this, resource_at(..), &v, cpu)). D3D12 descriptors hold no reference, soMADEis the only thing keeping that resource alive, which is presumably whymainnever lets go of it. Here it is released 2 s after one of two things:title_texturescallsretire_resource_at(old_res);MADE_MAX(65,536) entries are reached.The title may still have descriptors over that resource in a heap it reuses, and does not need to make the view again: on the console the view reads the memory, which still exists. The GPU then reads through a descriptor of a destroyed resource. Is there something that shows the title's descriptors over a retired resource are rewritten within the 2 s?
2. The cap evicts the oldest entry made, not the least used.
insertevicts in creation order (order.pop_front()). A view made early and used every frame (a UI atlas, a lookup texture) is the first to go once 65,536 later ones exist. Its nextresource_atmakes a new resource, but the descriptors written before still point at the old one, the case in point 1. Least-recently-used order would make this rarer, but not safe.3. Tests.
resource_at_cache_retires_cools_caps_and_tears_downchecks the bookkeeping with numbers in place of resources, which is fine for the data structure. Nothing covers what points 1 and 2 are about: a view by address still in use after its resource was retired or evicted. A GPU test that makes a view through device #70 / #71, makes 65,537 more (or overlaps the texture), waits pastMADE_COOLand samples through the first descriptor would show it. On the debug layer it should report the destroyed resource.Overlap with our work:
pso_async.rsonly: no overlap.D3d12Statsandstats()inreal.rs. This PR'sreal.rschanges are elsewhere in the file. I didn't merge the two together.AI use: Claude Code (Claude Opus 5.5) built the PR head, ran the tests above, read the diff and wrote this review. I checked each point myself before posting.
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.