startup: opt-in content view tokens so view records can be freed #3377
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3377"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-28T16:18:12Z.
Change
Refs #1199 (view records grow without bound: one record per distinct view, never removed).
X2W_D3D12_VIEW_CONTENT_TOKENS=1(opt-in):note_viewwrites a CBV (slot 17) or a buffer SRV (slot 18,NONEorRAW) 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.note_view,note_copy, the out-of-memory drop, the heap release underX2W_D3D12_RELEASE_RETIRED). When the last one lets go, the record is parked: it keeps its contents and itsview_idsentry, so the same view created again finds it. The oldest parked record is reused once more than 65536 are parked (about 10 MB).FirstElement≥ 2^32, format values ≥ 1024) keep id tokens and are never freed.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).token_view(); a content token decodes withoutnote_lock.X2W_D3D12_CAPTURE(itsVIEWchunks are written once per id).x2w-d3d12 view content tokens: written N; records N (ids N, parked N), parked N, reused N, revived N; fixture exportsx2w_d3d12_resolve_view,x2w_d3d12_view_content_counts,x2w_d3d12_set_view_parked_keep.docs/startup-runtime.md; the census section no longer says records are never removed.Verification
Windows 10 22H2, i7-4790K, RTX 3080 Ti (NVIDIA 32.0.16.1047) and WARP; dev and release; commit
503b41ceonmainf5a6f6ba.startup_view_content_token: round trips, refused views, padding bytes, a decode that ignores word 3.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 aCopyDescriptorsSimplecopy 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.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).startup_view_content*,startup_view_alloc_*,startup_resource_retire_views*,startup_formatted_view*), no skips.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 skipsstartup_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:
tokennull bindings per 1,000 translated drawsOne 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
Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-29T00:52:15Z.
Pushed
7aa09ec0: content tokens reached the compute and mesh buffer readers incomplete.d3d12_compute_dispatch.inlcopied 16 bytes of a buffer descriptor (input table slots, heap records, the argumentprobe). 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 argumentprobe decoded it from a 4-word array.
native_mesh57.inlread tokens as record ids only, so a content buffer SRV was refused asnot an SRV.Fix:
read_buffer_words(d3d12_capture.inl) reads all 32 bytes when they begin a content token; the three computesites 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/5startup_view_content*. Mutant (helper without the 32-byte read):both
ontests fail on those two checks,offpasses.startup_view_|startup_compute|startup_indirect|startup_mesh|startup_native_mesh|startup_argument, release anddev: 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 on503b41cewithout this commit. On a dev build withAgility 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 UAVwherean output buffer descriptor is a view tokenis expected) andstartup_compute_half_buffer_uav_controls(
UAV kind unknown) fail the same with and without this commit.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.
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T05:40:36Z.
Review: CHANGES_REQUESTED
One change requested at
7aa09ec0. Read the diff againstf5a6f6baand the surrounding code at that head; nothing built or run.Out-of-bounds read with the switch on. The compute
input_word3path keeps only 4 words of an input descriptor.View::wordsisstd::uint32_t words[4](d3d12_compute_dispatch.inl:566, filled at :805), and the localword[4](:941, copied at :946) is passed toformatted_buffer_word3(:947). That callstoken_view(d3d12_draw.inl:2662), and for a content tokendecode_view_contentreadswords[4]andwords[5](view_content_token.h:88), thenwords[6]andwords[7](:103-104). A typed buffer SRV fits a content token, so withX2W_D3D12_VIEW_CONTENT_TOKENS=1a dispatch with such an input and aninput_word3value reads 16 bytes pastwordon the stack. The word 3 it returns is still right, becauseview_word3only uses fields from words 1-2.Fix:
std::uint32_t word[8] = {}at :941, with the copy at :946 changed tosizeof(view.words)(left atsizeof(word)it would read 32 bytes from the 4-wordView::words). Or widenView::wordsto 8 as well: every copy into it usessizeofand comes from the 8-wordwords(:576). No test reaches this path, becausestartup_view_contentgoes through the fixture exports, notrecord_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 oldwords[1] < view_records().size()check undernote_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 useread_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 withwords[1]at or above 2^31, so all three still refuse it.main212d4b5fhas 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_dropkeeps a record unparked: it leaks, it is not reused.Tests.
token_view;read_buffer_words.words[2] != 17(view_content_token.h:90);token_viewreturning true when the decode fails;readable(..., 32)inread_buffer_words;prpreset neither builds nor runsstartup_view_content*, so CI on7aa09ec0(Windows build and test, fixture and Python jobs passed; /O2 and coverage skipped) does not cover the new tests.Merge. The merge with
mainis clean. With #3429 (2130dcec) it conflicts in one hunk of d3d12_xs_device.cpp, where both PRs add fixture exports afterx2w_d3d12_view_probe. Keep both, and convertx2w_d3d12_token_texture'swords[1] < view_records().size()totoken_view.Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-29T06:40:10Z.
Thanks, confirmed. Fixed in
740659ec(merged withmain212d4b5fin603228b9):View::wordsand theinput_word3copy 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. WithView::wordsbackat 4, the build fails at the assert.
No fixture binds a content token on this path (
startup_compute_dispatch_entrywrites 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|argument291/304. The 11 local failures from
7aa09ec0plus two from themainmerge(
startup_compute_dispatch_native_warp, newstartup_compute_token_interval_warp). Both also fail here withmain'ssrc/(Agility build too), so they are local, not this PR.Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T06:45:56Z.
Review: APPROVED
Approving
603228b9; this replaces my change request at7aa09ec0. Read the diff from7aa09ec0and the code at the head; nothing built or run.740659ecis the only non-merge commit since7aa09ec0and touches only d3d12_compute_dispatch.inl (+4 -2):View::words(:583) and theinput_word3localword(:960) go from 4 to 8 words, andstatic_assert(sizeof(View::words) == sizeof(words))(:595) fails ifView::wordsgoes back to 4.View::words(:640, :824, :928, :950,sizeof(views[i].words)from the 8-wordwords) and out of it (:965 intoword, :968 intowords) are 32 bytes on both sides.refusal_words(:987) still takes the first 16 bytes ofwords.token_viewholds 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).indirect_table_buffer, and neither can be a content token (is_view_contenttests bit 31 of word 1, so words 4-7 are not read):stream(draw.inl:3186):vb.StrideInBytes <= 0x3fff && ... vb.BufferLocation <= 0xffffffffffffull(:3184-3185), and word 1 isstatic_cast<UINT>(vb.BufferLocation >> 32) | vb.StrideInBytes << 16, at most bit 29;words(native_mesh57.inl:33):address >> 48is refused at :31, so word 1 is at most 0xffff.603228b9has the tree ofgit merge-tree --write-tree 740659ec 212d4b5f(5d541dda).603228b9: fixture, Windows build and test, and Linux Python jobs pass; /O2 and coverage are skipped.input_word3path.Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-29T07:52:26Z.
Merged
main(#3429) in31c0e42b. The one conflict was the fixture exports both PRs added; both kept. As promisedon #3429,
x2w_d3d12_token_texturenow decodes throughtoken_view(), like the draw texture path.Release and dev:
startup_view_content*5/5; #3429'sstartup_inline_retire_*andstartup_resource_retire_title*14/14; wider families 297/310, the same 13 local failures as on
603228b9.Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T08:01:21Z.
Review: APPROVED
Approving
31c0e42b; this replaces my approval of603228b9. Read the merge's conflict resolution (git show --remerge-diff 31c0e42b) and the code at the head; nothing built or run.x2w_d3d12_resolve_view,x2w_d3d12_buffer_words,x2w_d3d12_set_view_parked_keep,x2w_d3d12_view_content_counts, :6636-6691) and #3429'sx2w_d3d12_token_texture(:6695). In the latter it changes the comment ("record id" becomes "token_view") and the lookup; nothing else.token_view(d3d12_capture.inl:283-290) does the same under the same lock:materializedis null here, so id tokens are accepted and refused as before, with the same record. A content token now decodes throughdecode_view_contentinstead of being taken as an id.wordsinx2w_d3d12_token_textureisstd::uint32_t words[8], filled withmemcpy(words, token, sizeof(words)), sotoken_viewhas the 8 words a content token needs.mainhad notoken_viewordecode_view_contentcaller before this merge; #3429's edits to d3d12_compute_dispatch.inl, d3d12_draw.inl and native_mesh57.inl only takeretire_holdbefore the first use of the texture.main(eab7180c) to31c0e42bhas the same added and removed lines as the change from212d4b5fto603228b9(git diff -U0, compared line by line), apart from that resolution and one blank line dropped before thex2w_d3d12_token_texturecomment.git merge-tree --write-tree eab7180c 31c0e42bmerges cleanly; its tree (2397de7e) is the tree of31c0e42b.31c0e42b: fixture, Windows build and test, and Linux Python jobs pass; /O2 and coverage are skipped.