Skip to content

startup: opt-in content view tokens so view records can be freed - #3377

Merged
JohnConnorNPC merged 5 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/view-content-tokens
Nov 29, 2026
Merged

JohnConnorNPC merged 5 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/view-content-tokens

Conversation

@lukaslw-lab

Copy link
Copy Markdown
Contributor

Change

Refs #1199 (view records grow without bound: one record per distinct view, never removed).

X2W_D3D12_VIEW_CONTENT_TOKENS=1 (opt-in): note_view writes a CBV (slot 17) or a buffer SRV (slot 18, NONE or RAW) into the title's descriptor memory as a token that carries the view itself (CBV: address and size; SRV: resource, FirstElement, NumElements, stride, format, flags, component mapping) instead of a record id. Unset, nothing changes.

  • Why records could not be freed before: the title copies descriptors with its own memory copies, which the stand-in does not see. A copied id token can name its record at any later time, so a reused id would make it name another view.
  • With a content token, such a copy needs no record. The record is then named only by shadow slots, whose changes the stand-in sees (note_view, note_copy, the out-of-memory drop, the heap release under X2W_D3D12_RELEASE_RETIRED). When the last one lets go, the record is parked: it keeps its contents and its view_ids entry, so the same view created again finds it. The oldest parked record is reused once more than 65536 are parked (about 10 MB).
  • Views that do not fit (textures, counters, FirstElement ≥ 2^32, format values ≥ 1024) keep id tokens and are never freed.
  • Token layout keeps word 0 = X2WV (readers treat anything else as a hardware descriptor), word 1 bit 31 = content (an id is below 2^31, so a reader that compares the id alone sees an unknown id and refuses the binding), word 2 = slot, word 3 = 0 (the formatted-buffer readers write their own word 3 into their copy).
  • All draw and dispatch readers resolve both kinds through token_view(); a content token decodes without note_lock.
  • Off under X2W_D3D12_CAPTURE (its VIEW chunks are written once per id).
  • Heartbeat line x2w-d3d12 view content tokens: written N; records N (ids N, parked N), parked N, reused N, revived N; fixture exports x2w_d3d12_resolve_view, x2w_d3d12_view_content_counts, x2w_d3d12_set_view_parked_keep.
  • Docs: switch row and section "D3D12 view content tokens" in docs/startup-runtime.md; the census section no longer says records are never removed.
  • Why opt-in: it changes the token format every draw reader depends on. Same reasoning as startup: opt-in release of upload ring chunks at a fenced submit #2949, startup: opt-in alias binding for table buffer views over aliased placed buffers #2977, startup: opt-in skip of the graphics root SRV copy that no draw reads #3028.

Verification

Windows 10 22H2, i7-4790K, RTX 3080 Ti (NVIDIA 32.0.16.1047) and WARP; dev and release; commit 503b41ce on main f5a6f6ba.

ctest --preset <dev|release> --no-tests=error -R '^(startup_view_content.*|startup_view_alloc_.*|startup_resource_retire_views.*|startup_formatted_view.*)$' --output-on-failure
  • New startup_view_content_token: round trips, refused views, padding bytes, a decode that ignores word 3.
  • New startup_view_content_{off,on}_{warp,hardware}: a buffer SRV, a CBV and a texture SRV in a heap over title memory; plain-memory copies of their tokens and a CopyDescriptorsSimple copy survive 5000 overwrites, a revival and a reuse of the first view's record. On: records 3 → 5, revived 1; off: records grow by one per view.
  • Negative controls (local mutations), each fails startup_view_content_on_*: id tokens for freeable views (the title-side copy after its record was reused); no shadow reference for copies and parking at the first drop (the copied descriptor's shadow slot); never parking (the records grew with content tokens); no revival and dropping the map entry at park (the re-created first view did not revive its parked record).
  • This head, dev and release: 27/27 per preset with the switch unset and set (startup_view_content*, startup_view_alloc_*, startup_resource_retire_views*, startup_formatted_view*), no skips.
  • On an earlier head with the same logic (local 9af91261), the wider view families (startup_view_alloc_, resource_retire_, descriptor_, address_, stream_draw, table_alias_, sampler_heap, copied_vertex_srv_, root_, memory_srv, formatted_view, logical_view, 1d_srv, single_element, slot133_lazy, view_content, view_token_): 130/130 with the switch unset and set (release); capability skips startup_logical_view_warp, startup_root_heap_{warp,hardware}.

Startup run of the main executable (#870), release, warm shader cache, scripted input, the merged switches (#2949, #2977, #3028, #3313) in every run, two pairs (second in reverse order), 250 s:

unset set
process commit at 150k / 200k loaded blocks (GB) 17.2 / 18.6, 16.9 / 18.3 16.3 / 17.0, 16.4 / 17.8
process commit peak (GB) 19.1, 18.4 17.3, 17.9
view records at the end 6.4 M, 6.2 M 257k, 266k
fps at 100–125k / 125–150k / 150–175k blocks 15.5/13.9/11.4, 14.1/12.9/10.4 14.9/13.4/10.5, 14.2/13.1/10.1
title CPU ms per frame, same windows 300/353/419, 307/376/454 333/378/452, 335/372/454
token null bindings per 1,000 translated draws 19.9, 16.7 19.7, 19.3

One 800 s run with the switch set: no device removal; view records 388k at the end; process private commit flat between loads (17.1 GiB at 260–400 s, 19.3 GiB at 600–790 s). Its 1558 refused formatted-buffer draws all give "view format has no decoded buffer FORMAT", a format check that reads the same fields from both token kinds; the 250 s runs do not reach that content.

This saves memory, not time: fps is unchanged, and CPU per frame is the same or up to ~10 % higher in one window. Not covered: other adapters, hosts with more cores, Wine, an 800 s run with the switch unset.

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

🤖 Generated with Claude Code

…_D3D12_VIEW_CONTENT_TOKENS)

With X2W_D3D12_VIEW_CONTENT_TOKENS=1, note_view writes a CBV or a buffer SRV into title descriptor memory as a token
that carries the view (address and size; resource, range, stride, format, flags) instead of a record id. A copy the
title makes with its own memory copy then needs no record, so the record is named only by shadow slots, which the
stand-in sees change. When the last one lets go, the record is parked: it keeps its contents and its view_ids entry,
so the same view created again finds it, and the oldest parked record is reused once more than 65536 are parked.
Draw and dispatch readers resolve both token kinds through token_view(). Off under X2W_D3D12_CAPTURE. Unset,
nothing changes.

Tests: startup_view_content_token (encoding, no device), startup_view_content_{off,on}_{warp,hardware} (token copies
survive overwrites and record reuse; records bounded; revival). startup_view_alloc_* runs with the switch off: it
reads record ids from tokens.

Refs R0Y00#1199

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

The compute buffer readers copied 16 bytes of a descriptor, so a content token's view (words 4-7) read as zero and a
CBV was refused with "zero records"; the argument probe decoded it from a 4-word array. read_buffer_words reads all 32
bytes when they begin a content token. The two mesh57 SRV checks resolve tokens with token_view instead of as record ids.

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

Copy link
Copy Markdown
Contributor Author

Pushed 7aa09ec0: content tokens reached the compute and mesh buffer readers incomplete.

  • d3d12_compute_dispatch.inl copied 16 bytes of a buffer descriptor (input table slots, heap records, the argument
    probe). An id token needs only words 0–1, but a content token keeps its view in words 4–7, so a CBV arrived with
    address and size 0 and the dispatch was refused with an input buffer descriptor has zero records. The argument
    probe decoded it from a 4-word array.
  • native_mesh57.inl read tokens as record ids only, so a content buffer SRV was refused as not an SRV.

Fix: read_buffer_words (d3d12_capture.inl) reads all 32 bytes when they begin a content token; the three compute
sites use it, and the two mesh57 checks use token_view. Without the switch nothing changes.

Verification (Windows 10 22H2, RTX 3080 Ti, NVIDIA 32.0.16.1047 and WARP; release):

  • startup_view_content_{off,on}_{warp,hardware} now also read a buffer SRV and a CBV through the readers' path
    (fixture export x2w_d3d12_buffer_words): 5/5 startup_view_content*. Mutant (helper without the 32-byte read):
    both on tests fail on those two checks, off passes.
  • startup_view_|startup_compute|startup_indirect|startup_mesh|startup_native_mesh|startup_argument, release and
    dev: 291/302 each, skips the same in both. The 11 failures (startup_compute_dispatch_*,
    startup_compute_descriptor_scc_hardware, startup_compute_half_buffer_uav_controls,
    startup_compute_waterfall_heap_dispatch_warp) fail the same on 503b41ce without this commit. On a dev build with
    Agility SDK 1.620.2 and WARP 1.0.22 (OFFSCREEN_AGILITY_620_DIR, OFFSCREEN_WARP_DIR), 8 of them pass;
    startup_compute_dispatch_output_{warp,hardware} (an output token does not identify a slot-19 UAV where
    an output buffer descriptor is a view token is expected) and startup_compute_half_buffer_uav_controls
    (UAV kind unknown) fail the same with and without this commit.
  • Startup run (P0: main executable startup progress #870), switch on, 250 s: the CBV content token refusal (265 lines before) is gone; that shader now stops
    at the same later check as without the switch (an input is in a placed buffer that does not alias title memory).
    No device removal.

This comment was AI-assisted.

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

One change requested at 7aa09ec. Read the diff against f5a6f6b and the surrounding code at that head; nothing built or run.

Out-of-bounds read with the switch on. The compute input_word3 path keeps only 4 words of an input descriptor. View::words is std::uint32_t words[4] (d3d12_compute_dispatch.inl:566, filled at :805), and the local word[4] (:941, copied at :946) is passed to formatted_buffer_word3 (:947). That calls token_view (d3d12_draw.inl:2662), and for a content token decode_view_content reads words[4] and words[5] (view_content_token.h:88), then words[6] and words[7] (:103-104). A typed buffer SRV fits a content token, so with X2W_D3D12_VIEW_CONTENT_TOKENS=1 a dispatch with such an input and an input_word3 value reads 16 bytes past word on the stack. The word 3 it returns is still right, because view_word3 only uses fields from words 1-2.

Fix: std::uint32_t word[8] = {} at :941, with the copy at :946 changed to sizeof(view.words) (left at sizeof(word) it would read 32 bytes from the 4-word View::words). Or widen View::words to 8 as well: every copy into it uses sizeof and comes from the 8-word words (:576). No test reaches this path, because startup_view_content goes through the fixture exports, not record_translated_dispatch.

token_view() vs the old decodes. For an id token (words[1] below 2^31) token_view (d3d12_capture.inl:269) runs the old words[1] < view_records().size() check under note_lock, so the accepted set and the record returned are unchanged at its 11 call sites: capture.inl:331; compute_dispatch.inl:667; draw.inl:2244, :2524, :2662, :2736, :2790, :3088, :3221; native_mesh57.inl:45, :69. The kind checks after each call are kept. The three compute reads that now use read_buffer_words (:658, :918, :1111) still copy 16 bytes for an id token or a hardware descriptor. Three decodes stay on the old form: compute_token_output (:473, slot 19), resolve_draw_sampler (draw.inl:1665, slot 22) and the mesh57 PS texture heap (native_mesh57.inl:98). A content token is always a slot 17 or 18 buffer view with words[1] at or above 2^31, so all three still refuse it. main 212d4b5 has added no decode since the base.

Reused records. There is no generation counter. An id token is only written for a record that is not freeable, and only freeable records are parked and reused, so an id token in descriptor memory never names a reused record. A missed shadow_ref_drop keeps a record unparked: it leaks, it is not reused.

Tests.

  • Missing negative controls:
    • an out-of-range id token through token_view;
    • a malformed content token (bit 31 with slot 19 or 22, or slot 17 with stride bits in word 2);
    • a content token whose second half is unreadable in read_buffer_words.
  • By reading, these mutations survive:
    • removing words[2] != 17 (view_content_token.h:90);
    • token_view returning true when the decode fails;
    • removing readable(..., 32) in read_buffer_words;
    • reverting any single draw, compute or mesh57 site to the old decode.
  • The pr preset neither builds nor runs startup_view_content*, so CI on 7aa09ec (Windows build and test, fixture and Python jobs passed; /O2 and coverage skipped) does not cover the new tests.

Merge. The merge with main is clean. With #3429 (2130dce) it conflicts in one hunk of d3d12_xs_device.cpp, where both PRs add fixture exports after x2w_d3d12_view_probe. Keep both, and convert x2w_d3d12_token_texture's words[1] < view_records().size() to token_view.

lukaslw-lab and others added 2 commits November 29, 2026 07:23
View::words and the input_word3 copy held 4 descriptor words, but
token_view decodes 8 for a content token, so with
X2W_D3D12_VIEW_CONTENT_TOKENS=1 formatted_buffer_word3 read 16 bytes past
the copy. Both are 8 words now, and a static_assert ties View::words to the
8-word descriptor buffer it is filled from.

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

Copy link
Copy Markdown
Contributor Author

Thanks, confirmed. Fixed in 740659ec (merged with main 212d4b5 in 603228b9):

  • View::words and the input_word3 copy are now 8 words, so a content token is read whole.
  • static_assert(sizeof(View::words) == sizeof(words)) ties the copy to the 8-word buffer. With View::words back
    at 4, the build fails at the assert.

No fixture binds a content token on this path (startup_compute_dispatch_entry writes raw words); I left that out.
The #3429 conflict: whichever merges second gets the token_view() conversion.

Release and dev: startup_view_content* 5/5; startup_view_|compute|indirect|mesh|native_mesh|argument
291/304. The 11 local failures from 7aa09ec0 plus two from the main merge
(startup_compute_dispatch_native_warp, new startup_compute_token_interval_warp). Both also fail here with
main's src/ (Agility build too), so they are local, not this PR.

@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 603228b; this replaces my change request at 7aa09ec. Read the diff from 7aa09ec and the code at the head; nothing built or run.

  • 740659ec is the only non-merge commit since 7aa09ec and touches only d3d12_compute_dispatch.inl (+4 -2): View::words (:583) and the input_word3 local word (:960) go from 4 to 8 words, and static_assert(sizeof(View::words) == sizeof(words)) (:595) fails if View::words goes back to 4.
  • The copies into View::words (:640, :824, :928, :950, sizeof(views[i].words) from the 8-word words) and out of it (:965 into word, :968 into words) are 32 bytes on both sides. refusal_words (:987) still takes the first 16 bytes of words.
  • Every array read from descriptor memory that reaches token_view holds 8 words: capture.inl:327; compute :593 and :1126; draw.inl:2238, :2652 (FormattedBufferDescriptor::words), :2731, :2785, :3078; native_mesh57.inl:41, :64; x2w_d3d12_resolve_view (d3d12_xs_device.cpp:6602).
  • Two 4-word arrays built from an address still reach indirect_table_buffer, and neither can be a content token (is_view_content tests bit 31 of word 1, so words 4-7 are not read):
    • the vertex-buffer stream (draw.inl:3186): vb.StrideInBytes <= 0x3fff && ... vb.BufferLocation <= 0xffffffffffffull (:3184-3185), and word 1 is static_cast<UINT>(vb.BufferLocation >> 32) | vb.StrideInBytes << 16, at most bit 29;
    • the mesh root SRV words (native_mesh57.inl:33): address >> 48 is refused at :31, so word 1 is at most 0xffff.
  • The merge 603228b9 has the tree of git merge-tree --write-tree 740659ec 212d4b5f (5d541dda).
  • CI on 603228b: fixture, Windows build and test, and Linux Python jobs pass; /O2 and coverage are skipped.
  • Not covered: no test binds a content token on the input_word3 path.

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

Copy link
Copy Markdown
Contributor Author

Merged main (#3429) in 31c0e42b. The one conflict was the fixture exports both PRs added; both kept. As promised
on #3429, x2w_d3d12_token_texture now decodes through token_view(), like the draw texture path.

Release and dev: startup_view_content* 5/5; #3429's startup_inline_retire_* and startup_resource_retire_title*
14/14; wider families 297/310, the same 13 local failures as on 603228b9.

@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 31c0e42; this replaces my approval of 603228b. Read the merge's conflict resolution (git show --remerge-diff 31c0e42b) and the code at the head; nothing built or run.

  • One conflict, in d3d12_xs_device.cpp: both PRs added fixture exports at the same place. The resolution keeps this PR's four (x2w_d3d12_resolve_view, x2w_d3d12_buffer_words, x2w_d3d12_set_view_parked_keep, x2w_d3d12_view_content_counts, :6636-6691) and #3429's x2w_d3d12_token_texture (:6695). In the latter it changes the comment ("record id" becomes "token_view") and the lookup; nothing else.
  • #3429's lookup:
    if (words[0] != view_token_magic) return FALSE;
    AcquireSRWLockShared(&note_lock);
    const bool known = words[1] < view_records().size();
    if (known) record = view_records()[words[1]];
    ReleaseSRWLockShared(&note_lock);
    if (!known) return FALSE;
    At the head (:6701):
    if (words[0] != view_token_magic || !token_view(words, record)) return FALSE;
    For an id token (bit 31 of word 1 clear), token_view (d3d12_capture.inl:283-290) does the same under the same lock:
    AcquireSRWLockShared(&note_lock);
    const bool known = words[1] < view_records().size();
    if (known) {
        record = view_records()[words[1]];
        if (materialized) *materialized = view_materialized()[words[1]] != 0;
    }
    ReleaseSRWLockShared(&note_lock);
    return known;
    materialized is null here, so id tokens are accepted and refused as before, with the same record. A content token now decodes through decode_view_content instead of being taken as an id.
  • words in x2w_d3d12_token_texture is std::uint32_t words[8], filled with memcpy(words, token, sizeof(words)), so token_view has the 8 words a content token needs. main had no token_view or decode_view_content caller before this merge; #3429's edits to d3d12_compute_dispatch.inl, d3d12_draw.inl and native_mesh57.inl only take retire_hold before the first use of the texture.
  • The change from main (eab7180) to 31c0e42 has the same added and removed lines as the change from 212d4b5 to 603228b (git diff -U0, compared line by line), apart from that resolution and one blank line dropped before the x2w_d3d12_token_texture comment.
  • git merge-tree --write-tree eab7180c 31c0e42b merges cleanly; its tree (2397de7e) is the tree of 31c0e42.
  • CI on 31c0e42: fixture, Windows build and test, and Linux Python jobs pass; /O2 and coverage are skipped.

@JohnConnorNPC
JohnConnorNPC merged commit 7dc168a 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 PRs' additions in d3d12_capture.inl and docs/startup-runtime.md
(content tokens first, then the table lock).

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
note_view: the token is built first and, for a freeable view, replaced by
its content token (R0Y00#3377); the guarded write (X2W_D3D12_VIEW_TOKEN_GUARDED)
and the writable() fallback then write that token. Both PRs' helpers kept.

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/view-content-tokens 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.

3 participants