Skip to content

startup: opt-in inline descriptors (X2W_D3D12_INLINE_DESCRIPTORS) - #3429

Merged
JohnConnorNPC merged 2 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/inline-descriptors
Nov 29, 2026
Merged

JohnConnorNPC merged 2 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/inline-descriptors

Conversation

@lukaslw-lab

Copy link
Copy Markdown
Contributor

Change

Fixes #3428 (every constant buffer view and descriptor copy goes through the stand-in).

X2W_D3D12_INLINE_DESCRIPTORS=1 (opt-in): wrap() leaves byte +0x18 of the guest device at 0, so the title writes
constant buffer views and copies CBV/SRV/UAV descriptors itself instead of calling slots 17 and 24; sampler copies
still arrive through slot 24.

Verification

Windows 10 22H2, i7-4790K, RTX 3080 Ti (NVIDIA 32.0.16.1047) and WARP; release; commit 9d49b8c2 on main 212d4b5f.

ctest --preset release --no-tests=error -R '^(startup_inline_retire_|startup_indirect_|startup_resource_retire|startup_view_)' --output-on-failure
  • New startup_inline_retire_{on,control}_{warp,hardware}:
    1. a texture with a view and a 32-byte copy of its token made with memcpy: with the switch it is freed at its
      fences, and both descriptors then bind null; in the control the live slot holds it until overwritten, and the
      copy then binds null;
    2. 64 new textures never get the freed wrapper's address, and the copy still binds null;
    3. behind a closed gate fence nothing is freed until the gate opens;
    4. a live texture with a copy stays.
  • New startup_indirect_{dispatch,constants,count}_inline_{warp,hardware}: byte +0x18 stays 0 and the indirect
    dispatch cases pass.
  • Mutant (local): the drain keeps the retire_view_refs check under the switch →
    startup_inline_retire_on_{warp,hardware} fail with T1 not freed at its fences with inline descriptors; the
    controls pass.
  • This head, release: 136/136 in the families above, 17 capability skips as on main (WARP mesh cases,
    startup_view_heap_warp).
  • tests/docs_links_check.py: passed.

Startup run of the main executable (#870), this head, release, warm shader cache, scripted input, merged switches
(#2949, #2977, #3028, #3313) and X2W_D3D12_BUDGET=1 X2W_D3D12_RELEASE_RETIRED=1 in every run, 250 s, order
off/on/on/off:

loaded blocks 75–100k 100–125k 125–150k 150–175k 175–200k
fps, unset 11.0 / 12.5 13.1 / 13.9 12.6 / 12.2 9.4 / 10.0 7.4 / 9.1
fps, set 16.1 / 16.4 16.3 / 16.1 14.6 / 13.6 12.5 / 11.2 10.3 / –
title CPU ms per frame, unset 359 / 297 320 / 328 410 / 391 476 / 465 542 / 496
title CPU ms per frame, set 276 / 218 290 / 258 315 / 339 380 / 392 437 / –
  • Every set run is faster than every unset run in every window (one set run did not reach 175k).
  • Null bindings per 1,000 translated draws: token 19.5 / 22.6 unset, 21.4 / 22.0 set; wrapper 0.26 / 0.31 unset,
    0.36 / 0.24 set. Retained textures peak 0.25 / 0.21 GB unset, 0.31 / 0.16 GB set. Refusal lines 83 / 84 vs 85 / 85.
    No device removal.
  • With a local lock-counter build (not part of this change), slot 24 copies in one 250 s run fell from ~10.6 million
    to 54.
  • Without retirement at the fences (the byte alone), retained textures grew to 2.2 GB in one local run, which is why
    the switch also changes the drain.

Not covered: other adapters, hosts with more cores, Wine.

AI-assisted: the change, tests and this description were written with Claude; I ran the startup comparison.

🤖 Generated with Claude Code

With X2W_D3D12_INLINE_DESCRIPTORS=1 the stand-in leaves byte +0x18 of the guest device at 0, so the title writes
constant buffer views and copies CBV/SRV/UAV descriptors itself instead of calling slots 17 and 24 (R0Y00#3211 set the
byte). Copies made in title memory are not counted in retire_view_refs, so with X2W_D3D12_RELEASE_RETIRED=1 a retired
resource is freed once its release fences pass and no list or submission holds it. Wrappers are never freed, so a
stale token binds null and never another resource.

Fixtures: startup_inline_retire_{on,control}_{warp,hardware} (fixture export x2w_d3d12_token_texture) and
startup_indirect_{dispatch,constants,count}_inline_{warp,hardware}; startup_indirect_dispatch expects byte +0x18 at 0
under the switch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Leyukaka Leyukaka 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.

Changes requested at 9d49b8c. Read the diff and the surrounding code on main 212d4b5; nothing built or run, so the race below is found by reading, not reproduced.

Release vs. draw race. With the switch, the drain frees a retired texture while descriptors still name it, and a draw that reaches such a descriptor is the expected case (it should bind null). That draw can instead record the texture while the drain frees it:

  • drain_retired_resources claims the wrapper and sets own().real = nullptr under retire_lock only (d3d12_xs_device.cpp:6300-6302). It takes draw_lock later, only around texture_uploads().erase (:6337-6339), and calls Release() after dropping it (:6340).
  • draw_translated holds draw_lock from d3d12_draw.inl:2883 to the end of the call. It reads texture = sampled_address_texture(resource) at :3114 (a plain read of own().real, d3d12_memory_srv.inl:583, no retire_lock), then runs upload_texture (:3151), require_state (:3153) and only then retire_hold (:3154).
  • If the drain clears real between :3114 and :3154, retire_hold reads null under retire_lock (:6131), holds nothing and sets retire_hold_missed (:6152) on a wrapper that already left retired_resources. The draw keeps its local texture: the transition is already recorded and CreateShaderResourceView(texture, ...) follows at :3195.
  • draw_lock does not close the window. It only delays the drain's Release() until the draw call returns; the list is still open and unsubmitted, and nothing holds the desktop object unless an earlier draw of the same list did.
  • The drain runs at the top of queue_execute (:4776) and queue_present (:7568), before either takes draw_lock, so it needs one thread recording while another submits or presents.

On main the drain skips a texture while any tracked slot names it (retire_view_refs > 0), so a draw can only get there through an uncounted descriptor. With the switch no copy is counted and the check is skipped (:6291).

Possible fix: take the hold before the first use (right after :3114, ahead of upload_texture and require_state), have retire_hold report that it found real cleared, and bind null in that case. native_mesh57.inl:122/188-189 has the same read, use, hold order. The compute path reads real at d3d12_compute_dispatch.inl:690 and takes no hold at all. startup_inline_retire does not reach any of this: it resolves tokens through x2w_d3d12_token_texture on one thread.

Smaller points:

  • X2W_D3D12_RETIRE_REASONS (:6479) still reports a queued resource with retire_view_refs > 0 as view_refs. With the switch the drain no longer waits on view refs.
  • #3377 (open) replaces the words[1] < view_records().size() decodes with token_view(). x2w_d3d12_token_texture (:6610-6613) adds one more of the old decodes, so whichever PR merges second has to convert it.
  • PR CI runs none of the new tests. startup_inline_retire is not a target of the pr build preset, and the pr test filter startup_indirect_(dispatch|...)_warp matches neither startup_indirect_*_inline_warp nor startup_inline_retire_*. It runs startup_indirect_dispatch_warp with the switch unset. They would first run in the full build on main.

Also checked by reading:

  • Wrappers stay registered after the drain (registry().erase is only on the publish-failure path, :5784), so a stale token resolves to the dead wrapper and sampled_address_texture returns null for a non-tiled resource.
  • I found no one-line mutation of the changed lines that the tests would miss:
    • :6291 with the switch term removed fails the on cases (the mutant in the description); inverted, it also fails the control (T1 freed while its slot was live);
    • :536 without the switch term fails the startup_indirect_*_inline byte check;
    • startup_indirect_dispatch.cpp:386 with == fails every startup_indirect_dispatch case.

…review)

With X2W_D3D12_INLINE_DESCRIPTORS=1 a descriptor no longer keeps its texture, so the drain can free a retired
texture while a draw, mesh or compute call that reached it through a descriptor is between reading own().real and
taking the list's hold. Each of them now takes the hold right after the read, before any use: retire_hold returns
false when the drain already cleared real, and the read then binds null (draw) or refuses (mesh, compute). Compute
took no hold before.

- Late reads are counted and logged and no longer mark retire_hold_missed on a drained wrapper.
- A repeated hold by the same recording skips retire_lock and the search (per-recording stamp on the resource).
- X2W_D3D12_RETIRE_REASONS no longer reports view_refs with the switch.
- startup_resource_retire_title inline mode, case 11: a translated draw through a stale descriptor holds a released
  texture (also from a second list and after its own list's reset) and binds null once it is freed.
- pr preset: builds startup_inline_retire and startup_resource_retire_title and runs their warp cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

Thanks — confirmed by reading. Fixed in 2130dcec: draw, mesh and compute take the hold right after reading the
texture, before any use; retire_hold returns false if the drain already cleared real, and the read then binds
null (draw) or refuses (mesh, compute). Compute had no hold at all before.

New fixture startup_resource_retire_title … inline (case 11): a draw through a stale descriptor holds a released
texture (also from a second list and after a reset) and binds null once it is freed. Removing the hold fails it; the
read-to-hold order itself needs a second thread and is not covered.

Check Result
PR set + copies 151 run, 134 pass, 17 skip (WARP mesh), 0 fail
Stack A/B, same save fps 23.23 → 23.00 (3 baseline runs vs 1), memory and draws unchanged, 0 late reads

@Leyukaka Leyukaka 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.

Approving at 2130dce. Read the diff 9d49b8c..2130dce and the surrounding code at that head; nothing built or run.

Race. Closed, as far as reading shows:

  • Draw: the hold now sits right after the read and ahead of every use. d3d12_draw.inl:3114 read, :3116 retire_hold, then GetDesc (:3124), upload_texture (:3153), require_state (:3154) and CreateShaderResourceView (:3194).
  • retire_hold reads own().real under retire_lock, the lock the drain clears it under (d3d12_xs_device.cpp:6326). If real is null it returns false (:6144-6149) without setting retire_hold_missed. The draw then binds null (:3194 with texture == nullptr), and an indirect draw refuses (:3174-3178). If the hold wins, the AddRef happens before the drain's clear, and the drain's own Release() (:6364) leaves the list's reference.
  • real of a resource wrapper is set once at creation (:534) and afterwards only cleared, and a wrapper handed to the caller is never freed (the only free, :5792, is on the publish-failure path). So a non-null real under the lock is the object the caller read.
  • Mesh: hold at native_mesh57.inl:124, right after the read at :122. On false the call refuses (:125-126). The old hold after require_state (:189) is removed.
  • Compute: hold at d3d12_compute_dispatch.inl:693, right after the read at :689-690 and before GetDesc (:694).
  • The fast path (:6136-6138) skips the lock only when the resource carries the stamp of this list's current hold set. That set is dropped only by retire_discard_holds (:6182 clears the stamp), reached from Reset or destruction of that list (:3308), which D3D12 does not allow while another thread records on it. So the object the caller read is still referenced.
  • The one remaining unheld read, d16_srvs_ready (d3d12_draw.inl:2251), uses the pointer for GetDesc and a stand-in lookup and has no list to record on. It runs under draw_lock (called at :2896, lock at :2883), and the drain takes draw_lock (:6361) after clearing real and before its Release() (:6364).

Remaining limit: fixture case 11. It runs on one thread, as its comment says, so it never reaches the read-to-hold window or the !real branch: in 11b the draw reads real as already null and does not call retire_hold. It would also pass with the old read, use, hold order, so it does not tell the fix apart. What it covers: a draw takes a hold, the stamp is per recording (second list, and the same list after a reset), and a freed texture binds null. Mesh and compute holds are not exercised.

Smaller points.

  • X2W_D3D12_RETIRE_REASONS: view_refs is now reported only without the switch (:6503), which matches the drain (:6315).
  • x2w_d3d12_token_texture still uses the words[1] < view_records().size() decode (:6638). #3377 is still open, and the author has said they will convert it in whichever of the two PRs merges second.
  • The PR now also adds its tests to the pr preset: CMakePresets.json gains two build targets and three alternatives in the test filter (startup_indirect_(dispatch|constants|count)_inline_warp, startup_inline_retire_(on|control)_warp, and the warp debug0 run of the new retire test); it removes nothing. The 11 lines added to tests/cmake/startup_inline_retire.cmake are foreach/set/add_test/set_tests_properties only.
  • CI on 2130dce: "Build and test on Windows" passed; the gfx1013 and Python jobs pass, /O2 and coverage are skipped.

@JohnConnorNPC
JohnConnorNPC merged commit eab7180 into R0Y00:main Nov 29, 2026
5 checks passed
lukaslw-lab added a commit to lukaslw-lab/Xbox2Windows that referenced this pull request Nov 29, 2026
Keeps both sets of fixture exports; x2w_d3d12_token_texture now decodes
through token_view, as the draw texture path does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lukaslw-lab added a commit to lukaslw-lab/Xbox2Windows that referenced this pull request Nov 29, 2026
Keeps both PRs' pr preset entries (build targets and test filter).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lukaslw-lab added a commit to lukaslw-lab/Xbox2Windows that referenced this pull request Nov 30, 2026
…ches to the shared run presets

X2W_D3D12_INLINE_DESCRIPTORS (R0Y00#3429), X2W_D3D12_VIEW_CONTENT_TOKENS (R0Y00#3377) and X2W_D3D12_TABLE_LOCK (R0Y00#3391)
join base.env. The base why gives the 360 s A/B runs with native shaders only and with -Translate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lukaslw-lab added a commit to lukaslw-lab/Xbox2Windows that referenced this pull request Nov 30, 2026
…ches to the shared run presets

X2W_D3D12_INLINE_DESCRIPTORS (R0Y00#3429), X2W_D3D12_VIEW_CONTENT_TOKENS (R0Y00#3377) and X2W_D3D12_TABLE_LOCK (R0Y00#3391)
join base.env. The base why gives the 360 s A/B runs with native shaders only and with -Translate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tedin7 pushed a commit that referenced this pull request Nov 30, 2026
…ches to the shared run presets (#3550)

X2W_D3D12_INLINE_DESCRIPTORS (#3429), X2W_D3D12_VIEW_CONTENT_TOKENS (#3377) and X2W_D3D12_TABLE_LOCK (#3391)
join base.env. The base why gives the 360 s A/B runs with native shaders only and with -Translate.

Co-authored-by: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@lukaslw-lab
lukaslw-lab deleted the runtime/inline-descriptors branch December 1, 2026 01:11
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.

startup: every constant buffer view and descriptor copy goes through the stand-in, and it costs frame rate

3 participants