Repository navigation
startup: opt-in content view tokens so view records can be freed - #3377
Conversation
…_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>
|
Pushed
Fix: Verification (Windows 10 22H2, RTX 3080 Ti, NVIDIA 32.0.16.1047 and WARP; release):
This comment was AI-assisted. |
Leyukaka
left a comment
There was a problem hiding this comment.
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.
- an out-of-range id token through
- By reading, these mutations survive:
- removing
words[2] != 17(view_content_token.h:90); token_viewreturning true when the decode fails;- removing
readable(..., 32)inread_buffer_words; - reverting any single draw, compute or mesh57 site to the old decode.
- removing
- The
prpreset neither builds nor runsstartup_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.
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>
|
Thanks, confirmed. Fixed in
No fixture binds a content token on this path ( Release and dev: |
Leyukaka
left a comment
There was a problem hiding this comment.
Approving 603228b; this replaces my change request at 7aa09ec. Read the diff from 7aa09ec and the code at the head; nothing built or run.
740659ecis the only non-merge commit since 7aa09ec and 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.- The copies into
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. - Every array read from descriptor memory that reaches
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). - Two 4-word arrays built from an address still reach
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):- the vertex-buffer
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; - the mesh root SRV
words(native_mesh57.inl:33):address >> 48is refused at :31, so word 1 is at most 0xffff.
- the vertex-buffer
- The merge
603228b9has the tree ofgit 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_word3path.
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>
|
Merged Release and dev: |
Leyukaka
left a comment
There was a problem hiding this comment.
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'sx2w_d3d12_token_texture(:6695). In the latter it changes the comment ("record id" becomes "token_view") and the lookup; nothing else. - #3429's lookup:
At the head (:6701):
if (words[0] != view_token_magic) return FALSE; AcquireSRWLockShared(¬e_lock); const bool known = words[1] < view_records().size(); if (known) record = view_records()[words[1]]; ReleaseSRWLockShared(¬e_lock); if (!known) return FALSE;
For an id token (bit 31 of word 1 clear),if (words[0] != view_token_magic || !token_view(words, record)) return FALSE;
token_view(d3d12_capture.inl:283-290) does the same under the same lock:AcquireSRWLockShared(¬e_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(¬e_lock); return known;
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.- 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 thex2w_d3d12_token_texturecomment. git merge-tree --write-tree eab7180c 31c0e42bmerges 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.
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>
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>
…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>
…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>
…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>
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