Skip to content

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

Open
lukaslw-lab wants to merge 1 commit into
R0Y00:mainfrom
lukaslw-lab:runtime/inline-descriptors
Open

lukaslw-lab wants to merge 1 commit 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.

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

2 participants