Skip to content

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

Open
lukaslw-lab wants to merge 1 commit into
R0Y00:mainfrom
lukaslw-lab:runtime/view-content-tokens
Open

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

1 participant