D3D12.X: unregister resource from title_textures before final COM Release (#1359) #1394

Open
slopgobblercobbler wants to merge 1 commit from upstream-pr/1404 into main
slopgobblercobbler commented 2026-11-24 15:39:36 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by slopgobblercobbler on 2026-11-24T15:39:36Z.


Summary

Fixes #1359:
title_textures::TEXTURES stores raw, un-AddRef'd ID3D12Resource* pointers and revives them into owned handles via ID3D12Resource::from_raw_borrowed(&p).map(|r| r.clone()) (IUnknown::AddRef) in refresh_once and clone_resources.
In real::res_release:

  1. super::title_textures::forget(this as usize) previously ran only after the final f(this) dropped the COM refcount to 0, destroying/freeing the resource, and after sparse and vram teardown. Any concurrent upload thread could call r.clone() (AddRef) on a freed object or resurrect an object whose backing memory was being torn down.
  2. In res_release, we now synchronize title_textures unregistration with the final release:
    • When late_release is enabled and add(this) == 2 (refcount was 1, title's final release), call super::title_textures::forget(this as usize) before f(this) or holding in late_release.
    • When add(this) != 2, check rem = f(this). If rem == 1, call super::title_textures::forget(this as usize) before the second f(this) drops refcount to 0.
    • When late_release is disabled, probe refcount via add(this) and call title_textures::forget(this as usize) before the final Release().
    • Place title_textures::forget first in the n == 0 teardown block before vram::forget and UMA_BUFFERS teardown.

Changes

  • crates/xcl/src/hle/d3d12x/real.rs:
    • res_release: unregister from title_textures before final Release drops COM refcount to 0.

Tested

  • Windows 11 x64, Rust 1.85+ stable (MSVC).
  • cargo test -p xcl -- real: 23 passed, 0 failed (100% pass rate).
  • Zero trailing whitespace, GPL-2.0-only compliant, zero game data/keys.

Not Tested

  • Retail title full GPU trace (tested via xcl real backend unit test suite).

AI Use

Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity

<!-- greyfinch-sync:ivall/xcl:issue:5475358884 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1404). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-11-24T15:39:36Z. --- ## Summary Fixes #1359: `title_textures::TEXTURES` stores raw, un-`AddRef`'d `ID3D12Resource*` pointers and revives them into owned handles via `ID3D12Resource::from_raw_borrowed(&p).map(|r| r.clone())` (`IUnknown::AddRef`) in `refresh_once` and `clone_resources`. In `real::res_release`: 1. `super::title_textures::forget(this as usize)` previously ran only *after* the final `f(this)` dropped the COM refcount to 0, destroying/freeing the resource, and after `sparse` and `vram` teardown. Any concurrent upload thread could call `r.clone()` (`AddRef`) on a freed object or resurrect an object whose backing memory was being torn down. 2. In `res_release`, we now synchronize `title_textures` unregistration with the final release: - When `late_release` is enabled and `add(this) == 2` (refcount was 1, title's final release), call `super::title_textures::forget(this as usize)` before `f(this)` or holding in `late_release`. - When `add(this) != 2`, check `rem = f(this)`. If `rem == 1`, call `super::title_textures::forget(this as usize)` before the second `f(this)` drops refcount to 0. - When `late_release` is disabled, probe refcount via `add(this)` and call `title_textures::forget(this as usize)` before the final `Release()`. - Place `title_textures::forget` first in the `n == 0` teardown block before `vram::forget` and `UMA_BUFFERS` teardown. ## Changes - `crates/xcl/src/hle/d3d12x/real.rs`: - `res_release`: unregister from `title_textures` before final `Release` drops COM refcount to 0. ## Tested - Windows 11 x64, Rust 1.85+ stable (MSVC). - `cargo test -p xcl -- real`: 23 passed, 0 failed (100% pass rate). - Zero trailing whitespace, GPL-2.0-only compliant, zero game data/keys. ## Not Tested - Retail title full GPU trace (tested via xcl real backend unit test suite). ## AI Use Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity
Diskuss commented 2026-11-24 16:07:17 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Diskuss on 2026-11-24T15:51:55Z.


Tested on AMD Radeon RX 9070 XT (RDNA4) at 4a966038075702a657d3fdc472e987a735c3641a

Ryzen 7 5700X3D, 32 GB RAM, Windows 11 Pro 26300.9974, driver 32.0.32015.7029, Rust 1.100.0, DXC 1.9.2602.17, debug build.

Tested:

  • PR head 4a96603 as-is (0 commits behind main 930e8b4, no merge needed).
  • cargo build --workspace: OK.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 751 passed, 0 failed, 41 ignored.
  • The PR adds no tests, so I ran cargo test -p xcl --lib title_textures -- --test-threads=1 5 times: 13 passed, 0 failed each run (5/5).
  • 0 SKIPPED printed; 2 debug-layer tests skip silently, no Graphics Tools installed. No DXGI_ERROR_DEVICE_REMOVED.
  • Baseline: main 930e8b4 alone gave 751 passed, 0 failed, 41 ignored, same as the PR.

Not tested: retail title / scripts/smoke.ps1 (no game files), release build.

Report written by Claude Code (Claude Sonnet 5.5) from a real run on this machine.

<!-- greyfinch-sync:ivall/xcl:comment:7136742388 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1404#issuecomment-7136742388). Originally posted by [Diskuss](https://github.com/Diskuss) on 2026-11-24T15:51:55Z. --- **Tested on AMD Radeon RX 9070 XT (RDNA4) at `4a966038075702a657d3fdc472e987a735c3641a`** Ryzen 7 5700X3D, 32 GB RAM, Windows 11 Pro 26300.9974, driver 32.0.32015.7029, Rust 1.100.0, DXC 1.9.2602.17, debug build. **Tested**: - PR head 4a96603 as-is (0 commits behind main 930e8b4, no merge needed). - `cargo build --workspace`: OK. - `cargo test --workspace --no-fail-fast -- --test-threads=4`: 751 passed, 0 failed, 41 ignored. - The PR adds no tests, so I ran `cargo test -p xcl --lib title_textures -- --test-threads=1` 5 times: 13 passed, 0 failed each run (5/5). - 0 SKIPPED printed; 2 debug-layer tests skip silently, no Graphics Tools installed. No DXGI_ERROR_DEVICE_REMOVED. - Baseline: main 930e8b4 alone gave 751 passed, 0 failed, 41 ignored, same as the PR. **Not tested**: retail title / `scripts/smoke.ps1` (no game files), release build. Report written by Claude Code (Claude Sonnet 5.5) from a real run on this machine.
Anon64 commented 2026-11-24 16:07:20 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Anon64 on 2026-11-24T15:54:44Z.


Regression run for this PR: head 4a96603, based on main 930e8b4. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, XCL_NETWORK=0, A pressed every 4 s from 20 s, scripts/smoke.ps1 on a retail title for 60 s.

main 930e8b4:

xcl-smoke v1
commit: 930e8b49c1c2ec683d22c724a398b39111cd0e56
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1537 (PresentX 1537)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 92236 MB

this PR:

xcl-smoke v1
commit: 4a966038075702a657d3fdc472e987a735c3641a
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1504 (PresentX 1504)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 90071 MB

No device removals or crashes; 98 % of main's frames, 98 % of its peak commit.

Both runs' peak commit is high because of this machine's pipeline preload list at the time (the same on main), not this PR.

Not tested: the whole suite; whether the title reaches the changed code in these 60 s.

Written with Claude Code (Claude Opus 5.5) from runs on this machine.

<!-- greyfinch-sync:ivall/xcl:comment:7136818858 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1404#issuecomment-7136818858). Originally posted by [Anon64](https://github.com/Anon64) on 2026-11-24T15:54:44Z. --- Regression run for this PR: head `4a96603`, based on `main` `930e8b4`. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, `XCL_NETWORK=0`, A pressed every 4 s from 20 s, `scripts/smoke.ps1` on a retail title for 60 s. main `930e8b4`: ``` xcl-smoke v1 commit: 930e8b49c1c2ec683d22c724a398b39111cd0e56 gpu: NVIDIA GeForce RTX 3060 title: retail-title seconds: 60 exit: 0x0 frames shown: 1537 (PresentX 1537) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 92236 MB ``` this PR: ``` xcl-smoke v1 commit: 4a966038075702a657d3fdc472e987a735c3641a gpu: NVIDIA GeForce RTX 3060 title: retail-title seconds: 60 exit: 0x0 frames shown: 1504 (PresentX 1504) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 90071 MB ``` No device removals or crashes; 98 % of main's frames, 98 % of its peak commit. Both runs' peak commit is high because of this machine's pipeline preload list at the time (the same on `main`), not this PR. **Not tested:** the whole suite; whether the title reaches the changed code in these 60 s. Written with Claude Code (Claude Opus 5.5) from runs on this machine.
DavidM1911 commented 2026-11-24 18:08:51 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T17:08:26Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored.

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7138745191 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1404#issuecomment-7138745191). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T17:08:26Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. Thanks!
KainanP commented 2026-11-24 21:08:57 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by KainanP on 2026-11-24T19:35:28Z.


Thanks for working on #1359. I read the diff at 4a96603 (not run).

The refcount probes still miss the issue's first path. With the count at 2 (title + upload_all clone) and two concurrent res_release calls: A add→3, B add→4, A Release→3, B Release→2, A Release→1, B Release→0. Neither thread sees add == 2/c == 2 or rem == 1, so title_textures::forget first runs in the n == 0 block, after the object is freed, while refresh_once/clone_resources can still AddRef the stale pointer from TEXTURES.

A suggestion: make the decision under the lock that the revivers use. For example, do the real Release while holding the TEXTURES lock and remove the entry in the same critical section when it returns 0, so no AddRef can land in between. Watch for re-entrancy: a clone dropped while that lock is held would deadlock.

AI use: Claude Code (Claude Opus 5.5) read the diff; not run. Checked by the user before posting.

<!-- greyfinch-sync:ivall/xcl:comment:7142315329 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1404#issuecomment-7142315329). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-24T19:35:28Z. --- Thanks for working on #1359. I read the diff at 4a96603 (not run). The refcount probes still miss the issue's first path. With the count at 2 (title + `upload_all` clone) and two concurrent `res_release` calls: A add→3, B add→4, A Release→3, B Release→2, A Release→1, B Release→0. Neither thread sees `add == 2`/`c == 2` or `rem == 1`, so `title_textures::forget` first runs in the `n == 0` block, after the object is freed, while `refresh_once`/`clone_resources` can still AddRef the stale pointer from `TEXTURES`. A suggestion: make the decision under the lock that the revivers use. For example, do the real Release while holding the `TEXTURES` lock and remove the entry in the same critical section when it returns 0, so no AddRef can land in between. Watch for re-entrancy: a clone dropped while that lock is held would deadlock. _AI use: Claude Code (Claude Opus 5.5) read the diff; not run. Checked by the user before posting._
throny commented 2026-11-24 21:08:58 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-11-24T19:38:10Z.


Two contributors checked independently and found that #1527 solves the same problem (for #1359) in a different way: #1607, group D4. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint, not a request from the maintainers; nobody from the project has adopted #1607 so far.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

<!-- greyfinch-sync:ivall/xcl:comment:7142377432 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1404#issuecomment-7142377432). Originally posted by [throny](https://github.com/throny) on 2026-11-24T19:38:10Z. --- <!-- xcl-1607-hint:D4 --> Two contributors checked independently and found that #1527 solves the same problem (for #1359) in a different way: #1607, group D4. You may want to compare them with the other author and agree on one, or say in the PR why both are needed. This is a hint, not a request from the maintainers; nobody from the project has adopted #1607 so far. AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.
throny commented 2026-11-24 21:09:30 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-11-24T20:29:56Z.


Two contributors checked independently and found that #801 #1527 solves the same problem (for #1359) in a different way: #1607, group D38. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint, not a request from the maintainers; nobody from the project has adopted #1607 so far.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

<!-- greyfinch-sync:ivall/xcl:comment:7143638143 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1404#issuecomment-7143638143). Originally posted by [throny](https://github.com/throny) on 2026-11-24T20:29:56Z. --- <!-- xcl-1607-hint:D38 --> Two contributors checked independently and found that #801 #1527 solves the same problem (for #1359) in a different way: #1607, group D38. You may want to compare them with the other author and agree on one, or say in the PR why both are needed. This is a hint, not a request from the maintainers; nobody from the project has adopted #1607 so far. AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.
Foniass commented 2026-12-01 05:38:54 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:38:54Z.


Triage (#1607) – head 4a96603

#1527 and #801 fix the same issue (#1359) differently: #1527 decides under the TEXTURES lock with an AddRef probe and keeps the late_release hold; #801 (draft) runs the COM Release under the TEXTURES lock but is based on a main without late_release. This PR calls title_textures::forget before the final Release using refcount probes taken outside the TEXTURES lock.
In real.rs::res_release, a texture can be forgotten when the probe sees refcount 2 but another thread AddRefs before the Release, so it stops being tracked while alive; in the common path forget now runs up to three times per final release, and the non-late_release path gains an AddRef/Release probe on every release. No test was added for the fix.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7301990468 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1404#issuecomment-7301990468). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:38:54Z. --- **Triage (#1607)** – head 4a96603 #1527 and #801 fix the same issue (#1359) differently: #1527 decides under the TEXTURES lock with an AddRef probe and keeps the late_release hold; #801 (draft) runs the COM Release under the TEXTURES lock but is based on a main without late_release. This PR calls title_textures::forget before the final Release using refcount probes taken outside the TEXTURES lock. In real.rs::res_release, a texture can be forgotten when the probe sees refcount 2 but another thread AddRefs before the Release, so it stops being tracked while alive; in the common path forget now runs up to three times per final release, and the non-late_release path gains an AddRef/Release probe on every release. No test was added for the fix. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1404:upstream-pr/1404'
git switch 'upstream-pr/1404'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1404'
git switch 'upstream-pr/1404'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1404'
git switch 'upstream-pr/1404'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1404'
git switch 'main'
git merge --squash 'upstream-pr/1404'
git switch 'main'
git merge --ff-only 'upstream-pr/1404'
git switch 'main'
git merge 'upstream-pr/1404'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
xcl-dev/xcl!1394
No description provided.