D3D12.X: remove tracked textures before final COM release #1517

Open
OtavioTadeu wants to merge 1 commit from upstream-pr/1527 into main
OtavioTadeu commented 2026-11-24 17:17:17 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by OtavioTadeu on 2026-11-24T17:17:17Z.


Synchronizes real::res_release with title_textures::TEXTURES so tracked resources are removed before their COM refcount can drop to 0 (under XCL_LATE_RELEASE=0 or a concurrent refcount-2 race), preventing refresh_once and clone_resources from calling AddRef on freed memory. Fixes #1359.

What is in it

  • crates/xcl/src/hle/d3d12x/title_textures.rs: add TRACKED_COUNT, ReleaseOutcome, release_tracked, and unit + concurrent refcount-2 race tests
  • crates/xcl/src/hle/d3d12x/real.rs: route res_release through title_textures::release_tracked before final release and cleanup

Tested

  • NVIDIA GeForce GTX 1660 SUPER (driver 32.0.16.1088), AMD Ryzen 5 5600G, Windows 11 build 26200, DXC 1.9.2602.17 (Windows SDK 10.0.26100.0), rustc 1.100.0 (stable-x86_64-pc-windows-msvc), debug build
  • cargo test --workspace -- --test-threads=4: 777 passed, 0 failed, 41 ignored (SKIPPED GPU tests: gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask [no wave64], gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware [no wave64], heap_window_out_of_memory_falls_back_to_the_switch_form [no heap windows])
  • cargo test -p xcl --lib title_textures: 14 passed, 0 failed
  • cargo test -p xcl --lib d3d12x: 144 passed, 0 failed, 1 ignored

Regression run

  • scripts/smoke.ps1 -Game retail_title -Seconds 60 (main baseline vs PR head on NVIDIA GeForce GTX 1660 SUPER):

xcl-smoke v1

commit: 9e3831e1f570d3646e53ab753b19ec07eb8f1485

gpu: NVIDIA GeForce GTX 1660 SUPER

title: retail_title

seconds: 60

exit: 0x0

frames shown: 631 (PresentX 631)

presents per 2 s (last 3): 47 47 49

device removals: 0

crashes: 0

peak commit: 15199 MB


xcl-smoke v1

commit: 25fe06723d867defb80671d309a002f30a17475f

gpu: NVIDIA GeForce GTX 1660 SUPER

title: retail_title

seconds: 60

exit: 0x0

frames shown: 644 (PresentX 644)

presents per 2 s (last 3): 44 46 44

device removals: 0

crashes: 0

peak commit: 15211 MB

Not tested

  • AMD / native wave64 GPUs, Linux/Wine, and extended gameplay past the 60 s smoke window

AI use

  • Antigravity with Gemini 4 Neon: analyzed the issue, wrote the fix, tests, and PR description; reviewed by author.
<!-- greyfinch-sync:ivall/xcl:issue:5476939453 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1527). Originally posted by [OtavioTadeu](https://github.com/OtavioTadeu) on 2026-11-24T17:17:17Z. --- Synchronizes `real::res_release` with `title_textures::TEXTURES` so tracked resources are removed before their COM refcount can drop to 0 (under `XCL_LATE_RELEASE=0` or a concurrent refcount-2 race), preventing `refresh_once` and `clone_resources` from calling `AddRef` on freed memory. Fixes #1359. **What is in it** - `crates/xcl/src/hle/d3d12x/title_textures.rs`: add `TRACKED_COUNT`, `ReleaseOutcome`, `release_tracked`, and unit + concurrent refcount-2 race tests - `crates/xcl/src/hle/d3d12x/real.rs`: route `res_release` through `title_textures::release_tracked` before final release and cleanup **Tested** - NVIDIA GeForce GTX 1660 SUPER (driver 32.0.16.1088), AMD Ryzen 5 5600G, Windows 11 build 26200, DXC 1.9.2602.17 (Windows SDK 10.0.26100.0), rustc 1.100.0 (`stable-x86_64-pc-windows-msvc`), debug build - `cargo test --workspace -- --test-threads=4`: 777 passed, 0 failed, 41 ignored (SKIPPED GPU tests: `gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask` [no wave64], `gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware` [no wave64], `heap_window_out_of_memory_falls_back_to_the_switch_form` [no heap windows]) - `cargo test -p xcl --lib title_textures`: 14 passed, 0 failed - `cargo test -p xcl --lib d3d12x`: 144 passed, 0 failed, 1 ignored **Regression run** - `scripts/smoke.ps1 -Game retail_title -Seconds 60` (`main` baseline vs PR head on NVIDIA GeForce GTX 1660 SUPER): ``` xcl-smoke v1 commit: 9e3831e1f570d3646e53ab753b19ec07eb8f1485 gpu: NVIDIA GeForce GTX 1660 SUPER title: retail_title seconds: 60 exit: 0x0 frames shown: 631 (PresentX 631) presents per 2 s (last 3): 47 47 49 device removals: 0 crashes: 0 peak commit: 15199 MB ``` ``` xcl-smoke v1 commit: 25fe06723d867defb80671d309a002f30a17475f gpu: NVIDIA GeForce GTX 1660 SUPER title: retail_title seconds: 60 exit: 0x0 frames shown: 644 (PresentX 644) presents per 2 s (last 3): 44 46 44 device removals: 0 crashes: 0 peak commit: 15211 MB ``` **Not tested** - AMD / native wave64 GPUs, Linux/Wine, and extended gameplay past the 60 s smoke window **AI use** - Antigravity with Gemini 4 Neon: analyzed the issue, wrote the fix, tests, and PR description; reviewed by author.
upipibi commented 2026-11-24 18:09:11 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by upipibi on 2026-11-24T17:47:55Z.


Duplicate note: #1404 (opened earlier, not a draft, 3 comments) also fixes #1359 in real::res_release. The approaches differ:

  • #1404: +15/−2 in real.rs. It calls title_textures::forget from the reference count Release returns, and around an extra AddRef.
  • This PR: routes the release through a new title_textures::release_tracked (+202 in title_textures.rs), with unit tests and a concurrent refcount-2 race test.

Both edit res_release, so only one can land. Comparison only: I haven't run either.

AI use: Claude Code (Claude Opus 5.5) compared the diffs and wrote this.

<!-- greyfinch-sync:ivall/xcl:comment:7139719631 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1527#issuecomment-7139719631). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-24T17:47:55Z. --- Duplicate note: #1404 (opened earlier, not a draft, 3 comments) also fixes #1359 in `real::res_release`. The approaches differ: - #1404: +15/−2 in `real.rs`. It calls `title_textures::forget` from the reference count `Release` returns, and around an extra `AddRef`. - This PR: routes the release through a new `title_textures::release_tracked` (+202 in `title_textures.rs`), with unit tests and a concurrent refcount-2 race test. Both edit `res_release`, so only one can land. Comparison only: I haven't run either. AI use: Claude Code (Claude Opus 5.5) compared the diffs and wrote this.
Belox17 commented 2026-11-24 19:07:37 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Belox17 on 2026-11-24T18:48:49Z.


Test report for 0789e88b / 7c748c8b on NVIDIA (RTX 3060 Ti): the whole suite passes, but the new test is flaky and takes two other tests down with it

Tested

  • NVIDIA GeForce RTX 3060 Ti 8 GB, driver 32.0.16.2124; Ryzen 5 5600X; Windows 11 build 26300; rustc 1.98.1 MSVC; debug build. Head 0789e88b as is, clean tree. The current head 7c748c8b is the same patch rebased onto 9e3831e (git range-diff); its tree is identical to my merge of 0789e88b into 9e3831e, and the test is unchanged.
  • cargo test --workspace -- --test-threads=4: 771 passed, 0 failed, 41 ignored (base b7aa9f3: 770). Merged into 9e3831e: 777 passed, 0 failed, 41 ignored.
  • cargo test -p xcl --lib tracked_release_removes_entry, alone, 20 runs: 13 passed, 7 failed, always object not freed on iteration N (title_textures.rs:2181; N = 2, 14, 70, 85, 115, 208, 460).
  • cargo test -p xcl --lib title_textures -- --test-threads=4, 10 runs: 5 passed, 5 failed. Each failed run has 3 failures: this test, plus placed_title_textures_share_vram_and_preserve_pixels and transitions_apply_when_their_list_is_submitted with PoisonError, because the panic happens while TEST_LOCK is held.

Likely cause, from reading the test (I did not instrument it): the reader thread AddRefs every tracked key under the TEXTURES lock and releases it later through release_tracked. When the reader holds the last reference in an iteration, the final mock_release runs on the reader thread, possibly after t2.join(), so obj.freed can still be false at the assertion. Waiting until the reader has dropped its references to the current object (or keeping the reader away from the object under test) before asserting would make the check deterministic.

Not tested

  • res_release with a title (late release on and XCL_LATE_RELEASE=0); AMD; any retail title (no smoke run).

AI use: Claude Code (Claude Opus 5.5) ran the commands and wrote this report; I checked it before posting.

<!-- greyfinch-sync:ivall/xcl:comment:7141187587 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1527#issuecomment-7141187587). Originally posted by [Belox17](https://github.com/Belox17) on 2026-11-24T18:48:49Z. --- **Test report for `0789e88b` / `7c748c8b` on NVIDIA (RTX 3060 Ti): the whole suite passes, but the new test is flaky and takes two other tests down with it** **Tested** - NVIDIA GeForce RTX 3060 Ti 8 GB, driver 32.0.16.2124; Ryzen 5 5600X; Windows 11 build 26300; rustc 1.98.1 MSVC; debug build. Head `0789e88b` as is, clean tree. The current head `7c748c8b` is the same patch rebased onto `9e3831e` (`git range-diff`); its tree is identical to my merge of `0789e88b` into `9e3831e`, and the test is unchanged. - `cargo test --workspace -- --test-threads=4`: 771 passed, 0 failed, 41 ignored (base `b7aa9f3`: 770). Merged into `9e3831e`: 777 passed, 0 failed, 41 ignored. - `cargo test -p xcl --lib tracked_release_removes_entry`, alone, 20 runs: **13 passed, 7 failed**, always `object not freed on iteration N` (`title_textures.rs:2181`; N = 2, 14, 70, 85, 115, 208, 460). - `cargo test -p xcl --lib title_textures -- --test-threads=4`, 10 runs: **5 passed, 5 failed**. Each failed run has 3 failures: this test, plus `placed_title_textures_share_vram_and_preserve_pixels` and `transitions_apply_when_their_list_is_submitted` with `PoisonError`, because the panic happens while `TEST_LOCK` is held. Likely cause, from reading the test (I did not instrument it): the `reader` thread AddRefs every tracked key under the `TEXTURES` lock and releases it later through `release_tracked`. When the reader holds the last reference in an iteration, the final `mock_release` runs on the reader thread, possibly after `t2.join()`, so `obj.freed` can still be false at the assertion. Waiting until the reader has dropped its references to the current object (or keeping the reader away from the object under test) before asserting would make the check deterministic. **Not tested** - `res_release` with a title (late release on and `XCL_LATE_RELEASE=0`); AMD; any retail title (no smoke run). **AI use:** Claude Code (Claude Opus 5.5) ran the commands and wrote this report; I checked it before posting.
DavidM1911 commented 2026-11-24 19:07:52 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T18:58:39Z.


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:7141417033 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1527#issuecomment-7141417033). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T18:58:39Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. Thanks!
throny commented 2026-11-24 21:09:31 +00:00 (Migrated from github.com)

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


Two contributors checked independently and found that #801 #1404 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:7143638659 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1527#issuecomment-7143638659). Originally posted by [throny](https://github.com/throny) on 2026-11-24T20:29:58Z. --- <!-- xcl-1607-hint:D38 --> Two contributors checked independently and found that #801 #1404 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.
JJAutoCompare commented 2026-11-25 06:05:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by JJAutoCompare on 2026-11-25T05:20:21Z.


Empirical test report (25fe0672)

PR #1527, tested at 25fe06723d867defb80671d309a002f30a17475f.

Hardware / environment

  • Windows x64, build 26200, on an AMD64 host (family 23, model 113, stepping 0). The focused fixture ran on the CPU and did not initialize a D3D12 device.
  • rustc 1.100.0 (0ef1bc386 2026-11-09)
  • dxcompiler.dll: 1.9(5512-01b62ad4)(1.9.2609.5) - 1.9.2609.5 (01b62ad47)
  • Cargo test profile, unoptimized, with debug info and incremental compilation disabled.

Commands and results

  • cargo test --workspace --no-run exited 0. This compiled the workspace test binaries.
  • cargo test -p xcl --lib tracked_release_removes_entry_before_final_drop_and_survives_refcount_2_race ran outside Invoke-ArcTest.ps1 with XCL_ADAPTER=NONE_CPU_ONLY and RUST_TEST_NOCAPTURE=1. It exited 0 with 1 passed, 0 failed, 0 ignored, 0 skipped, and 748 filtered out.

Observed behavior

The mock COM fixture confirmed that the single-reference resource left the texture table while the final reference was still held. Its concurrent case ran 500 iterations with two releasing threads and a reader that attempted to retain the resource under the table lock. Every iteration reached mock destruction, removed the table entry, and completed without the mock recording an AddRef after destruction.

Not tested

The full GPU workspace suite and game smoke runs were omitted. This fixture exercises the release helper with mock reference counts. It provides no coverage of the real D3D12 release hook, GPU queue behavior, or a title running with XCL_LATE_RELEASE=0.

AI use

Verified with T3 Code / Codex (gpt-6.2-sol, reasoningEffort: high). Coordinator #2 (T3 Code / Antigravity, gemini-4-neon) verified counts against pr1527-focused.log and published.

<!-- greyfinch-sync:ivall/xcl:comment:7153675376 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1527#issuecomment-7153675376). Originally posted by [JJAutoCompare](https://github.com/JJAutoCompare) on 2026-11-25T05:20:21Z. --- ### Empirical test report (25fe0672) PR #1527, tested at `25fe06723d867defb80671d309a002f30a17475f`. **Hardware / environment** - Windows x64, build 26200, on an AMD64 host (family 23, model 113, stepping 0). The focused fixture ran on the CPU and did not initialize a D3D12 device. - `rustc 1.100.0 (0ef1bc386 2026-11-09)` - `dxcompiler.dll: 1.9(5512-01b62ad4)(1.9.2609.5) - 1.9.2609.5 (01b62ad47)` - Cargo test profile, unoptimized, with debug info and incremental compilation disabled. **Commands and results** - `cargo test --workspace --no-run` exited 0. This compiled the workspace test binaries. - `cargo test -p xcl --lib tracked_release_removes_entry_before_final_drop_and_survives_refcount_2_race` ran outside `Invoke-ArcTest.ps1` with `XCL_ADAPTER=NONE_CPU_ONLY` and `RUST_TEST_NOCAPTURE=1`. It exited 0 with **1 passed, 0 failed, 0 ignored, 0 skipped**, and 748 filtered out. **Observed behavior** The mock COM fixture confirmed that the single-reference resource left the texture table while the final reference was still held. Its concurrent case ran 500 iterations with two releasing threads and a reader that attempted to retain the resource under the table lock. Every iteration reached mock destruction, removed the table entry, and completed without the mock recording an AddRef after destruction. **Not tested** The full GPU workspace suite and game smoke runs were omitted. This fixture exercises the release helper with mock reference counts. It provides no coverage of the real D3D12 release hook, GPU queue behavior, or a title running with `XCL_LATE_RELEASE=0`. **AI use** Verified with T3 Code / Codex (gpt-6.2-sol, reasoningEffort: high). Coordinator #2 (T3 Code / Antigravity, gemini-4-neon) verified counts against `pr1527-focused.log` and published.
ettehn commented 2026-11-25 07:05:45 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ettehn on 2026-11-25T07:01:19Z.


I repeated the mock COM test after the earlier flaky result. I applied the 25fe067 diff by itself to main at 568e7db and verified that the added and removed lines match the upstream diff.

On Windows 11 25H2 (26200.9974), Ryzen 9 8945HX, Rust 1.97.1 MSVC (debug), I ran cargo test --locked --offline -j 2 -p xcl --lib hle::d3d12x::title_textures::tests::tracked_release_removes_entry_before_final_drop_and_survives_refcount_2_race -- --exact --test-threads=1 with XCL_ADAPTER=NONE_CPU_ONLY. 20 of 20 invocations passed, with 500 concurrent iterations per invocation and zero failures or ignored tests. I tested the updated fixture on this machine. This run did not exercise the actual D3D12 release hook, the GPU, or a retail title.

Verification assisted by OpenAI Codex (GPT-6).

<!-- greyfinch-sync:ivall/xcl:comment:7155402226 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1527#issuecomment-7155402226). Originally posted by [ettehn](https://github.com/ettehn) on 2026-11-25T07:01:19Z. --- I repeated the mock COM test after the earlier flaky result. I applied the `25fe067` diff by itself to `main` at `568e7db` and verified that the added and removed lines match the upstream diff. On Windows 11 25H2 (`26200.9974`), Ryzen 9 8945HX, Rust 1.97.1 MSVC (debug), I ran `cargo test --locked --offline -j 2 -p xcl --lib hle::d3d12x::title_textures::tests::tracked_release_removes_entry_before_final_drop_and_survives_refcount_2_race -- --exact --test-threads=1` with `XCL_ADAPTER=NONE_CPU_ONLY`. **20 of 20 invocations passed**, with 500 concurrent iterations per invocation and zero failures or ignored tests. I tested the updated fixture on this machine. This run did not exercise the actual D3D12 release hook, the GPU, or a retail title. Verification assisted by OpenAI Codex (GPT-6).
upstream-sync closed this pull request 2026-11-25 23:07:16 +00:00
upstream-sync reopened this pull request 2026-11-26 00:04:16 +00:00
DavidM1911 commented 2026-11-26 06:06:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T05:40:35Z.


Re-verification on RTX 3060 Ti (Windows 11): still a merge conflict with current main on the new head — needs another rebase before it can be tested.

<!-- greyfinch-sync:ivall/xcl:comment:7183703104 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1527#issuecomment-7183703104). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-26T05:40:35Z. --- Re-verification on RTX 3060 Ti (Windows 11): still a **merge conflict with current `main`** on the new head — needs another rebase before it can be tested.
Foniass commented 2026-12-01 06:00:14 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T06:00:14Z.


Triage (#1607) – head 25fe067

Same problem (#1359) as #1404 and #801 (#1607 D4 / D38), solved differently: this PR serializes a tracked resource's last Release under the TEXTURES lock and keeps late_release; #1404 forgets on unlocked AddRef probes; #801 runs Release under the lock on an older base without late_release.
Main commit 6a7e44e has since rewritten res_release (real.rs): it now tells the last reference by an AddRef/Release probe on every path and calls title_textures::forget while the object is still alive, which closes most of the window this PR targets. What is left is two releases of the last two references at once, which release_tracked still covers for tracked textures. The PR conflicts with that rewrite and needs rebasing onto it; in its Last { hold_ref: false } path the object is already freed before the address-keyed cleanup in res_release, which is what 6a7e44e moved away from.
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:7302368533 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1527#issuecomment-7302368533). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T06:00:14Z. --- **Triage (#1607)** – head 25fe067 Same problem (#1359) as #1404 and #801 (#1607 D4 / D38), solved differently: this PR serializes a tracked resource's last Release under the `TEXTURES` lock and keeps late_release; #1404 forgets on unlocked AddRef probes; #801 runs Release under the lock on an older base without late_release. Main commit 6a7e44e has since rewritten `res_release` (real.rs): it now tells the last reference by an AddRef/Release probe on every path and calls `title_textures::forget` while the object is still alive, which closes most of the window this PR targets. What is left is two releases of the last two references at once, which `release_tracked` still covers for tracked textures. The PR conflicts with that rewrite and needs rebasing onto it; in its `Last { hold_ref: false }` path the object is already freed before the address-keyed cleanup in `res_release`, which is what 6a7e44e moved away from. 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/1527:upstream-pr/1527'
git switch 'upstream-pr/1527'

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/1527'
git switch 'upstream-pr/1527'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1527'
git switch 'upstream-pr/1527'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1527'
git switch 'main'
git merge --squash 'upstream-pr/1527'
git switch 'main'
git merge --ff-only 'upstream-pr/1527'
git switch 'main'
git merge 'upstream-pr/1527'
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!1517
No description provided.