Skip to content

startup: opt-in release of retired committed 2D textures after list and fence lifetimes (slice 1) - #2443

Merged
MasterDomino merged 2 commits into
R0Y00:mainfrom
JB2WIN:startup/release-retained-textures
Nov 25, 2026
Merged

MasterDomino merged 2 commits into
R0Y00:mainfrom
JB2WIN:startup/release-retained-textures

Conversation

@JB2WIN

@JB2WIN JB2WIN commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Refs #2379. First source-built slice, as asked in the issue: one committed texture class whose uses are accounted for. Opt-in through X2W_D3D12_RELEASE_RETIRED=1 (needs X2W_D3D12_RENDER=1). When unset, nothing changes, and a fixture case checks that.

What changes

  • Class: a committed 2D texture from device slot 27 or 127 with one sample, no render-target, depth or UAV flag, the default heap and no title memory.
  • Release: after its final guest Release, the stand-in drops its desktop reference only when all three conditions below hold:
    • the release-time fence signalled on every registered queue has passed;
    • no recorded list still holds it. CopyResource and CopyTextureRegion hold the source and the destination, ResolveSubresource holds the destination. Holds fall on Reset, on list destruction or on a failed Close: this is the discard path, and it makes up no submission;
    • no submission still holds it. Each submission keeps a reference until a private fence signalled behind it passes.
  • What takes a resource out of the class: a view, MakeResident or Evict, use as a Present surface, and raw (unwrapped) pointers. Descriptor and title-address paths keep the existing lifetime.
  • Without a registered queue, or when a fence cannot be signalled, nothing is freed. The resource stays retained, as before.

Fixture startup_resource_retire (WARP and hardware, plus off and debug-layer variants)

  • The issue's main case: two lists record copies of the same texture. The last guest reference is released while both lists are closed and unsubmitted, and the release-time fences complete. The lists are then submitted separately, and both readbacks match. The second submission is held at its own fence, and the texture stays in the budget retained column until that fence is signalled.
  • Further cases:
    • a copy destination released after recording;
    • CopyTextureRegion and Resolve;
    • Reset (refused on an open list) and destruction of an unsubmitted list and of a list in flight;
    • a copy queue;
    • slot 27 and slot 127 positive cases;
    • negative cases: buffer, view, title memory, render target, MakeResident and Present.
  • The off case runs without the switch and requires the previous behaviour.

Mutants (local patches, each reverted after the run): 11 patches. Each makes the fixture fail with its own FAIL line:

  • early release with closed lists;
  • copy destination not held;
  • submission fence ignored;
  • Resolve destination;
  • CopyTextureRegion;
  • list reset state;
  • view;
  • Present;
  • MakeResident;
  • probe counter;
  • release-time fence.

Tests

Host: RTX 3080, driver 32.0.16.2124, Windows 11 26200, release build on top of 7092683a.

ctest --preset release --no-tests=error -R '^startup_resource_(retire|budget)'   # 14/14 passed, no skips
ctest --preset release --no-tests=error -R '^startup_'                         # 664/664 passed (baseline 658/658; +6 retire cases)
python -B tests/docs_links_check.py                                            # passed

Observation from a local start-up run (not a fixture, same host): 600 s with the switch, X2W_D3D12_BUDGET=1 and a 40 GiB job cap.

Not tested:

  • a Close that fails (it cannot be provoked);
  • the bad_alloc paths;
  • a title exporting __XMemAllocationHooks__ (slot 127 then leaves the class, as documented);
  • sampler feedback (no device slot of the stand-in reaches it).

AI-assisted (Claude Code)

🤖 Generated with Claude Code

…ELEASE_RETIRED)

With X2W_D3D12_RELEASE_RETIRED=1, a committed 2D texture of one narrow class
(slot 27 or 127, one sample, no render-target/depth/UAV flag, default heap, no
title memory) gives up its desktop object after its final guest Release, but
only when the release-time fence of every queue has passed and no recorded
list or submission still names it. Lists hold the resource across copy,
texture copy and resolve until Reset or destruction (an unsubmitted list is
discarded without any submission); each submission holds it until a fence
signalled behind it passes. Views, MakeResident/Evict and Present take a
resource out of the class. Unset, nothing changes.

The fixture startup_resource_retire uses the budget retained column as oracle
and reads results back through its own desktop queue.

AI-assisted (Claude Code)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Refs R0Y00#2379
…follow-up)

- Check the CopyResource destination hold by behaviour first: the fixture no
  longer asserts the hold count before the destination-released stage.
- The retire probe export returns FALSE without the switch.
- Document that the switch needs X2W_D3D12_RENDER=1, that a title exporting
  __XMemAllocationHooks__ leaves slot 127 outside the class, and the debug
  layer skip. Lists drop their holds on Reset, destruction or a Close that
  fails.
- Take the submission reference only after its fence was signalled; shorten
  comments.

AI-assisted (Claude Code)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Refs R0Y00#2379

@triggered02 triggered02 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Windows verification and audit of 1ecd4cfc (no P1/P2 bugs found).

Verified locally on Windows (F:\x2w-wt-2443)

  • Built d3d12_xs_shim and startup_resource_retire with cmake --preset dev (MSVC).
  • ctest --preset dev --no-tests=error -R '^startup_resource_(retire|budget)' -V (focused retire targets built):
    • startup_resource_retire_warp_debug0: PASS (Microsoft Basic Render Driver, debug_layer=off, mode=on, debug_errors_or_warnings=0)
    • startup_resource_retire_off_warp: PASS (debug_layer=off, mode=off)
    • startup_resource_retire_hardware_debug0: PASS (NVIDIA GeForce GTX 1660 SUPER, debug_layer=off, mode=on, debug_errors_or_warnings=0)
    • startup_resource_retire_off_hardware: PASS (NVIDIA GeForce GTX 1660 SUPER, debug_layer=off, mode=off)
    • *_debug1: skipped (77) — Graphics Tools debug layer is not installed on this host.

Audit notes

  • Lock ordering & lifecycle: retire_lock stays strictly a leaf lock (never acquired before draw_lock/allocator_lock/target_lock/pending_writes_lock); retire_note_submission runs under draw_lock + allocator_lock right after ExecuteCommandLists; drain_retired_resources uses TryAcquireSRWLockExclusive and runs all ID3D12Resource::Release() calls after releasing retire_lock; retire_queues is append-only and queues never drop their desktop pointers in release().
  • P3 — Unprotected dropped.insert under retire_lock in drain_retired_resources() (d3d12_xs_device.cpp): dropped.reserve(batch) is guarded before taking retire_lock, but the retire_flights loop can exceed batch and grow dropped via dropped.insert(...) while holding retire_lock without try/catch or an RAII lock guard (if std::bad_alloc occurred there, retire_lock would stay held and already-popped retired_resources entries with real = nullptr would leak their desktop references). Stopping the flight loop before exceeding batch when !dropped.empty() (or reserving inside try/catch with an RAII guard) would close that.
  • P3 — Minor fixture hygiene/coverage: ComPtr<IUnknown> fence; in make_queue (startup_resource_retire.cpp:252) is unused; title_memory in classes() leaks on the early return false path; and failed Close(), double-submitting one closed list, Evict (slot 35), and RTV/DSV/UAV exclusions (view_slot<19..21>) are verified by inspection rather than dedicated sub-cases.

AI-assisted (Gemini).

@MasterDomino MasterDomino left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AMD data for head 1ecd4cfc. This adds a hardware vendor to @triggered02's GTX 1660 SUPER run, plus the debug-layer variants that host had to skip.

Commands (Windows 11 build 26300, --preset dev):

cmake --build --preset dev --target d3d12_xs_shim startup_resource_retire startup_resource_budget
ctest --preset dev --no-tests=error -V -R '^startup_resource_(retire|budget)'

Result: 14/14 passed, with no skips.

  • Adapters: AMD Radeon RX 9070 XT (1002:7550, driver 32.0.32029.2010) and WARP 10.0.26100.9818.
  • Retire fixtures, on both adapters:
    • retire_*_debug0: debug_layer=off mode=on;
    • retire_*_debug1: debug_layer=enabled mode=on, debug_errors_or_warnings=0;
    • retire_off_*: mode=off.
  • Budget fixtures: all 8 startup_resource_budget_* cases, including the inline controls, pass unchanged with this branch.

The retire fixture's fences, list holds and release-time fence therefore behave the same on AMD as on NVIDIA and WARP, including under the debug layer.

Not covered here: I didn't rerun the 11 mutants or a title run. Per @JB2WIN's note in #2379, slice 1 doesn't reach the slot 133 title-memory textures that grow in local start-up runs, and that work continues in slice 2.

This is runtime code, so I'm leaving the verdict to the maintainer.

AI-assisted; commands listed were executed locally.

@MasterDomino MasterDomino left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved at head 1ecd4cfc. Slice 1 of #2379: an opt-in release of retired committed 2D textures (X2W_D3D12_RELEASE_RETIRED=1). This is the slice requested in #2379, and @triggered02's audit found no P1 or P2.

Fixture results (source-built)

Windows 11 build 26300, --preset dev:

cmake --build --preset dev --target d3d12_xs_shim startup_resource_retire startup_resource_budget
ctest --preset dev --no-tests=error -V -R '^startup_resource_(retire|budget)'
  • 14/14 pass on the AMD Radeon RX 9070 XT (1002:7550, driver 32.0.32029.2010) and WARP 10.0.26100.9818, with no skips. This includes retire_*_debug1 with the D3D12 debug layer enabled (debug_errors_or_warnings=0) and retire_off_* (switch off, behaviour unchanged).
  • The 8 startup_resource_budget_* cases pass unchanged.
  • The branch merges cleanly with current main.

Negative control

I applied each mutant to drain_retired_resources() in src/startup_stubs/d3d12_xs_device.cpp, rebuilt, ran it, then reverted:

Mutant retire_{warp,hardware}_debug0
M1: release-time fence ignored (reached = true) fail, FAIL: early release (release-time fence)
M2: submission fence ignored (flight check if (false)) fail on both adapters, FAIL: early release (submission fence)

After reverting and rebuilding, all 14 pass again.

Executable results (separate from the fixtures; local title run, aggregates only)

This slice makes no start-up claim. @JB2WIN's measurement in #2379 shows it releases 0 resources in the title's start-up, where the growing class is slot 133 with title memory. That class is #2468's scope, stacked on this PR. My own executable pair in #2379 used the unreviewed v2 prototype, not this commit.

Merge checks

  • Required CI passed at this head. The /O2 and coverage skips come from explicit workflow conditions.
  • There are no unresolved review threads.
  • There are no dependencies.

@triggered02's P3 notes stay open as follow-ups: the dropped.insert growth under retire_lock in a bad_alloc case, and the fixture hygiene items.

AI-assisted; commands listed were executed locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants