Skip to content

startup: opt-in release of upload ring chunks at a fenced submit - #2949

Merged
R0Y00 merged 4 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/upload-ring-release-at-submit
Nov 27, 2026
Merged

R0Y00 merged 4 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/upload-ring-release-at-submit

Conversation

@lukaslw-lab

@lukaslw-lab lukaslw-lab commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Title: startup: opt-in release of upload ring chunks at a fenced submit

Change

Fixes #ISSUE (after #2849 the upload ring still runs out in the post-load animation). Refs #2837, #2849.

X2W_D3D12_RING_RELEASE_AT_SUBMIT=1 (opt-in): a closed command list stops holding its upload ring chunks once its submit is fenced, instead of holding them until Reset. The queue fence still keeps the chunks from reuse until the GPU has passed it. Unset, the ring behaves exactly as on main.

  • upload_reservation.h: ReleasedUploadChunks, release_upload_chunks_at_submit, resubmitted_upload_chunks; chunk arrays sized for 512 MiB.
  • d3d12_xs_device.cpp:
    • note_submits releases each submitted list's holds after a signalled fence (no signal: nothing changes).
    • A later submit of the same list before its Reset fences its released chunks again when their generation is unchanged. If a chunk was recycled in between, queue_execute leaves that list out of ExecuteCommandLists before the execute (released_chunks_recycled, under draw_lock + allocator_lock), counts and logs it (... were reused; refused, it is not executed); it stays refused until its Reset, and the other lists of the call still run.
    • The release record is dropped at list Reset and Release; a failed allocation keeps the holds.
    • With the switch the ring grows in 16 MiB chunks up to 512 MiB, so a list still recording or waiting to be submitted pins 16 MiB, not up to 128 MiB.
  • Why opt-in: D3D12 allows a closed list to be submitted again without a Reset (startup_list_resubmit step 2). With the switch, such a resubmit after a chunk reuse is refused, so that list does not run. The mode suits titles that Reset a list before submitting it again; the main executable did so in every measured submit (0 of more than 500,000 over five runs).
  • Docs: switch row and ring paragraph in docs/startup-runtime.md; docs/experiments/startup-upload-ring-release-at-submit.md.
  • Cost: the 512 MiB ring adds about 1 GiB of process commit at peak in the run below (startup-runtime: physical-page sections exhaust system commit before loading completes #1199).
  • Overlap: startup: read translated compute inputs that live only in title memory from a snapshot #2862 changes discard_upload_chunks at the same call sites and startup: count a failed buffer copy as the null cause ring_full and refuse the draw; report upload ring statistics #2864 a reserve_upload_ring caller; text conflicts only, whichever lands second.

Verification

Windows 10 22H2, i7-4790K, RTX 3080 Ti (NVIDIA 32.0.16.1047) and WARP; dev and release; commit 33d31412 on 6ee878d8.

ctest --preset dev     --no-tests=error -j 1 -R '^(upload_reservation|startup_list_resubmit.*|startup_address_console.*|startup_slot133_heap_cap.*|startup_null.*|startup_stream_draw.*)$'
ctest --preset release --no-tests=error -j 1 -R '^(upload_reservation|startup_list_resubmit.*|startup_address_console.*|startup_slot133_heap_cap.*|startup_null.*|startup_stream_draw.*)$'

35 tests per preset, run with the switch unset and set: 33/35 pass in each run. The two failures are startup_list_resubmit_nodebug_hardware and startup_list_resubmit_nodebug_untracked_hardware (SegFault). Both fail the same way on unmodified main 6ee878d8 on this host, with and without the switch.

  • upload_reservation, new check_release_at_submit: release, fence guard, reuse, resubmit with one intact and one reused chunk, and the re-fence. Negative controls (local mutations): keeping the holds fails release: holds dropped; ignoring generations fails a resubmit finds chunk 1 intact and chunk 0 reused.
  • New startup_address_console_gate_ring_keep{,_release}_{warp,hardware}: list A uploads 32 × 16 MiB and is never Reset; a second list then draws payload 32 over the right half; A is submitted again, and the target is read back after that submit.
    • Default: the ring refuses past 256 MiB and the right half reads the null view.
    • Switch set: the right half holds payload 32 from a chunk A released; A's second submit is refused (logged) and does not run, so the right half still holds payload 32, not A's payload 31.
    • Negative controls: with the release disabled the right half stays on the null view; with the check before the execute disabled, A runs and the test fails on pixels (its payload 0 draw shows payload 32's colour on the left, its payload 31 covers the right).
  • Fixtures that describe the default ring now clear the switch for their own process (startup_address_console_gate_ring{,_reuse}, startup_slot133_heap_cap_capped), so they keep testing the default when it is set in the environment.
  • Startup run of the main executable, one build, only the switch differs (250 s, warm cache, scripted input):
Switch unset Switch set
Null bindings, cause buffer 33,632 0
upload ring reservation refused logged none
Resubmit reports n/a 0
Process commit peak 16.7 GiB 18.1 GiB
Visible single black frames in the animation none seen

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed. (docs/startup-runtime.md, experiment note)
  • I did not include game files, dumps, distribution links or secrets.

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

🤖 Generated with Claude Code

X2W_D3D12_RING_RELEASE_AT_SUBMIT=1 lets a closed command list stop holding its
upload ring chunks once its submit is fenced, instead of holding them until
Reset. The queue fence alone then keeps the chunks from reuse until the GPU has
passed it. In this mode the ring grows in 16 MiB chunks up to 512 MiB.

A later submit of the same list before its Reset fences its chunks again when
their generation is unchanged; a chunk recycled in between is counted and
logged. Unset, the ring behaves as before (doubling chunks up to 256 MiB,
holds until Reset).

- upload_reservation.h: ReleasedUploadChunks, release_upload_chunks_at_submit,
  resubmitted_upload_chunks; chunk arrays sized for 512 MiB.
- d3d12_xs_device.cpp: switch, release in note_submits, resubmit check,
  cleanup at Reset and Release, ring sizing.
- Tests: upload_reservation check_release_at_submit; new
  startup_address_console_gate_ring_keep{,_release}_{warp,hardware}; fixtures
  that describe the default ring clear the switch for their process.
- Docs: switch row and ring paragraph in docs/startup-runtime.md, experiment
  note docs/experiments/startup-upload-ring-release-at-submit.md.

Refs R0Y00#2837, R0Y00#2849.

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

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The release-after-submit experiment has a correctness gap in the resubmit case documented by the fixture. note_submits discovers a recycled generation only after ExecuteCommandLists, and the resubmit fences only the intact chunks. The re-executed list can therefore fetch overwritten upload data, and the recycled chunk is also unprotected from later ring reuse while that draw runs. Please detect this before queue submission and either refuse the stale resubmit or retain/reconstruct the recorded bytes. The fixture should assert that the stale list did not execute with replacement bytes; logging the post-submit detection alone cannot establish that.

…-release-at-submit

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

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #2949 by @lukaslw-lab — startup: opt-in release of upload ring chunks at a fenced submit

What works

  • Clean opt-in via environment variable; completely zero-cost when unset (static const bool on)
  • upload_reservation.h additions are minimal and focused: ReleasedUploadChunks struct, release_upload_chunks_at_submit, resubmitted_upload_chunks
  • Correct fence guarding: released chunks stay fenced until the GPU passes, preventing premature reuse
  • generation tracking properly detects reused-vs-intact chunks on resubmit
  • upload ring grows in 16 MiB chunks up to 512 MiB (consistent with initial_upload_chunk_bytes, 32 chunks fits in UploadChunkMask uint32_t)
  • note_submits updates chunks mask to include both original holds AND released chunks that need refencing
  • Test coverage: gate-ring-keep (without switch, ring stalls at 256 MiB) and gate-ring-keep-release (with switch, ring releases)
  • upload_slot133_heap_cap_check.cpp cleared the env var to avoid interfering with default ring tests
  • Good doc quality in startup-upload-ring-release-at-submit.md

Issues

  • release_upload_chunks_at_submit does not validate that the list's fence is actually signalled before releasing; the caller must ensure that. If someone calls this outside note_submits with a non-signaled fence, chunks become reusable while the GPU still writes to them. Consider adding a DEBUG assertion that chunks[i].fence_value <= queue fence value.
  • note_submits only releases held chunks when submit_fence signals successfully (the code path after if (submit_fence_fails()) return). If the fence fails, the list remains in the released_upload_chunks map and its chunks never get cleaned up. Consider clearing the map entry when the fence fails to avoid memory growth.
  • The UploadChunkMask type is uint32_t with max_upload_chunks = 32 (512 MiB / 16 MiB). The loop in release_upload_chunks_at_submit does ++i, recording >>= 1 which correctly iterates at most 32 times, but a future increase beyond 32 MiB chunks would silently truncate. The static_assert(max_upload_chunks <= 32) catches overflows but not undersized masks.
  • resubmitted_upload_chunks iterates all 32 indices even when released.chunks has few bits set. For correctness this is fine, but worth noting that a bit-iteration pattern (while remaining) would scale better if max_upload_chunks ever changes.
  • X2W_D3D12_RING_RELEASE_AT_SUBMIT=1 and the ring-size increase together add ~1 GiB process commit. Consider documenting this in startup-runtime.md alongside other memory-affecting switches.

Recommendations

  • Add a runtime assert in note_submits after release_upload_chunks_at_submit: assert(list->own().upload_chunks == 0) to verify the list's original holds were fully released.
  • Clear released_upload_chunks[list] in the submit_fence_fails path to avoid leaking map entries.
  • In the gate-ring-keep-release test mode, add a negative check: verify that without the env var, the ring does refuse beyond 256 MiB (already covered by gate-ring-keep, but could note it in the test doc comment).

Overall

Well-architected memory optimization that cleanly separates the opt-in path. The generation-based tracking is elegant and the fence-guarding is correct. After the fence-failure cleanup fix, this should be solid.

lukaslw-lab and others added 2 commits November 27, 2026 06:20
With X2W_D3D12_RING_RELEASE_AT_SUBMIT=1, a closed list submitted again after
one of its released upload chunks was reused would read replaced bytes. The
check ran in note_submits, after ExecuteCommandLists, so it could only log.

queue_execute now checks each closed list before the execute, with draw_lock
and allocator_lock held (released_chunks_recycled). Such a list is left out of
that execute, counted and logged, and stays refused until its Reset; the other
lists of the call still run. The locks stay held through note_submits, which
therefore only re-fences intact chunks.

gate-ring-keep-release now reads the target back after A's second submit and
asserts the right half still holds payload 32 (A's last draw is payload 31).
With the check disabled, A runs and its payload 0 draw shows the replacement
bytes (payload 32's colour) on the left half.

Docs: the experiment note and docs/startup-runtime.md describe the refusal;
the switch row states the ~1 GiB commit cost.

Refs R0Y00#2948.

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

Same settings on a later main: buffer null bindings 50,665 -> 0, no reservation refusals, 0 stale resubmits refused, commit peak 17.9 -> 18.0 GiB, no black frames seen. The switch row now says the ring can add up to about 1 GiB of commit.

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

Copy link
Copy Markdown
Contributor Author

Thanks, agreed: the post-submit report could not stop the stale execute. A stale resubmit is now refused before the queue submits it.

Change (3fcd36fc + docs 8c183700, on top of a merge of main 57f9e07f; no force-push)

  • queue_execute checks each closed list in the list_ready loop, with draw_lock and allocator_lock held, before ExecuteCommandLists (released_chunks_recycled). A list whose released chunk was reused since its fenced submit is left out of that execute, the same way a list that is not closed is left out today. It is counted and logged (... were reused; refused, it is not executed) and stays refused until its Reset. The other lists of the call still run.
  • Both locks stay held from that check through note_submits, so no reservation can recycle a chunk in between. note_submits therefore only sees intact chunks and fences them again. The recycled chunk is never read, so nothing is left unprotected.
  • Fixture gate-ring-keep-release: the target is now read back after A's second submit. The check asserts the right half still holds payload 32 (A's last draw would put payload 31 there) and that the refusal was logged.
result
gate_ring_keep{,_release}_{warp,hardware} + upload_reservation, release 5/5
mutant: check before the execute disabled (if (false && …)) _release_warp and _release_hardware fail on pixels: {'left': 32768, 'right': 32768} wrong pixels, found ['12e845ff', '23df7aff']; expected 03ff5aff left, 23df7aff right. The stale A ran: its payload 0 draw read the reused chunk and painted payload 32's colour (23df7aff) on the left, and its payload 31 (12e845ff) covers the right
affected matrix (upload_reservation, startup_list_resubmit.*, startup_address_console.*, startup_slot133_heap_cap.*, startup_null.*, startup_stream_draw.*), release + dev, switch unset and set 33/35 in each of the four arms (only the 2 below)
#2977 filter (startup_table_alias_.*, startup_frame_copy_.*, …), release + dev, switch unset and set 16/16 in each of the four arms
  • Command: ctest --preset <release|dev> --no-tests=error -j 1 -R '<filter>' with X2W_D3D12_RING_RELEASE_AT_SUBMIT unset and =1.
  • Host: Windows 10 22H2 (19045), RTX 3080 Ti, NVIDIA 32.0.16.1047, plus WARP; debug layer off.
  • The 2 failures in every arm are startup_list_resubmit_nodebug_hardware / _nodebug_untracked_hardware. They segfault identically on unmodified main on this host (unrelated, not touched here).
  • Docs: the experiment note and both docs/startup-runtime.md passages now describe the refusal instead of the report. The switch row also states the commit cost (up to ~1 GiB at peak).

Startup run repeated on 3fcd36fc (settings of the experiment note, 250 s, one build, only the switch differs):

switch unset switch set
null bindings with cause buffer 50,665 0
upload ring reservation refused logged yes no
stale resubmits refused (log lines) 0 0
process commit peak 17.9 GiB 18.0 GiB
visible single black frames in the post-load animation none seen

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent integration review of 8c183700 with main 6aa59440 (e8e855c8 locally). The recycled-generation check now runs before ExecuteCommandLists, under both locks through submission and refencing. Only accepted lists reach queue execution and GPU-write bookkeeping. The pixel readback confirms that the rejected resubmission left both halves unchanged.

ctest --preset <dev|release> --no-tests=error -V -R '^(upload_reservation|startup_address_console_gate_ring_keep(_release)?_(warp|hardware)|startup_stream_draw_warp|startup_table_alias_warp)$' passed 7/7 in Debug and 7/7 in Release /O2, with no skips. Windows 11 build 26200.9974, RTX 4060 Laptop driver 32.0.16.1656, and WARP 10.0.26100.9818. The table-alias fixture used the debug layer and GPU-based validation and reported zero debug errors. The ring fixture ran with the debug layer off. The contributor’s broader matrix recorded two matching failures on its unchanged baseline.

Hosted Windows, Linux Python and gfx1013 fixture checks are green. The hosted /O2 job was skipped and the local /O2 run above passed. The matching issue is #2948.

@R0Y00
R0Y00 merged commit 98e4a2e into R0Y00:main Nov 27, 2026
5 checks passed
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