retire_orphan_windows frees the retired windows whose submissions have
completed after this submission's refresh_shadows, and freeing writes
the window's copy back to the title's pages once more
(free_retired_shadow). The refresh had already copied those pages to
the live copies, so the slot-133 buffer that replaced the window read
the older bytes until the next submission.
Moving the free ahead of the refresh, as suggested, is not enough on
its own: the write-back opens the pages it writes, and the refresh's own
copy back of the live copies' batches (write_back_titles) then takes
those pages for ones the title wrote and drops the buffer's GPU results
there (measured with this test: with the free moved first, the words
written through the buffer did not all reach the pages). So the free stays after the refresh, and a
retired copy's bytes that go back are written into every other copy over
them that still held the title's older bytes, as the next refresh would;
a copy that changed a byte keeps its own. Under lock_, in place, no
allocation. The epoch/ticket gating (src/command_list_epochs.h,
stamp_retired_windows) is untouched.
d3d12_title_va_test --window-orphans: the submission that frees the
window reads the held words through the buffer placed over the ring.
With LEONIDA_TITLE_WRITEBACK=0 (d3d12_title_window_orphans_free) it read
0 of 64 before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
ShadowPages::write_back compared the copies in 256-byte pieces and kept
one flag per piece: once a live copy had changed any byte of it, a
retired window's copy was left out of the whole piece. A retired window
and the slot-133 buffer that replaced it can have written disjoint
bytes of one piece (the window [384, 512), the buffer [256, 384)), so
the window's bytes were dropped. The batch path (`only` null) had the
same flag (`live = live || pass == 0`).
The piece now keeps a fixed 256-entry mask on the stack (no heap under
lock_) of the bytes a live copy changed, set in the first pass; a
retired copy goes back on every byte it changed that no live copy did,
in runs. Copies are still compared against the title's bytes as they
were before any copy went back, and a live copy now writes only the
bytes it changed, so one live copy no longer writes the title's older
bytes over another's result in the same piece.
d3d12_title_va_test --window-orphans: the held submission writes through
the window from ring + 384, a later one through the buffer placed over
the ring from ring + 128, and all the held words must reach the title's
pages (32 of 64 did before). The ring is write-combined so the buffer is
a copy on every driver. New ctest entry d3d12_title_window_orphans_free
runs it with LEONIDA_TITLE_WRITEBACK=0, where the window's free is the
only way back.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
cover_pool_request planned its cover (title_pool_cover) under short
shared locks of the alias table, then made the mapping and entered it
with replace_twin, which erased every range in the span unconditionally.
A slot-133 buffer is entered (Resource::alias_title_va) without
title_window_mutex, so one placed in either view of the span in between
was erased with the windows. And replace_twin's result was ignored: the
cover was recorded in pool_spans and returned as made either way.
replace_twin now takes the caller's predicate and, under the same
unique lock as the replacement, refuses with nothing changed when a
range it would erase is not one the caller planned to replace. The
cover passes the windows its plan found. On refusal the window it made
is dropped at once (drop_title_window: nothing answered from it, so no
submission names it) and the request falls back to the zero-size view
as before. The same drop now follows add_twin's refusal in
resolve_pool_memory, which left its new window charged to the budget
and refreshed with nothing answering from it.
title_window_plan_test: a non-window range entered in the cover's span
(CPU view, then twin view) between title_pool_cover and replace_twin is
refused and left intact.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
title_pool_cover translated a mapping found in the pool's other view back
with `x - (twin - va)` modulo 2^64. A window that starts below the twin
view (m.title_va < twin - va) wrapped to near UINT64_MAX, passed the
pool bounds check, and replace_twin then erased it whole, leaving the
part of it outside the cover unanswered. A twin view that does not fit
in 64 bits (twin < va - pool_lo, or pool_hi - va > UINT64_MAX - twin)
made the twin-view query wrap into an empty range, so a range in the way
was not seen at all.
The twin view's bounds [twin_lo, twin_hi) are now checked up front,
queried in non-wrapping coordinates, and a hit outside the view it was
found in refuses the cover like one outside the pool.
title_window_plan_test: check_pool_cover_bounds with the review's
example (pool [0x100000, 0x500000), va 0x120000, twin 0x420000, window
[0x200000, 0x440000)) and the two overflow cases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
With LEONIDA_ASYNC_PIPELINES=1, graphics and mesh pipelines (all their stages) now take
LEONIDA_ASYNC_MIN_BYTES_GRAPHICS when it is set, and LEONIDA_ASYNC_MIN_BYTES otherwise, so
nothing changes while it is unset. Compute keeps LEONIDA_ASYNC_MIN_BYTES. 0 makes every graphics
pipeline asynchronous. One startup line states both thresholds; text that is not a number is
ignored with a log line.
Thread dumps from a retail run showed the draw thread building lazy graphics variants under the
1 MiB threshold in place, 2.5-11 s each, back to back, with no present for about 100 s. A skipped
draw costs one draw. The image needs the small lighting and post compute programs, and those
are still built in place.
Tests: async_pipeline_size_test covers the per-kind decision and the unset fallback;
d3d12_pipeline_workers_test --async-graphics-zero / --async-graphics-unset (new ctests) hold
the compile worker with compute over its threshold.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ShadowPages::write_back decided page by page, inside its loop, whether a page's GPU result goes back. A copy
that faults on another guard (a geometry watch cell) makes the handler reopen the whole 64 KiB tile on the
write-back's own thread (reopen_range, under the lock #2418 lets it reuse), which clears kClean and kProtected
there. The runs after it in the tile then read as written by the title: their GPU results were dropped from
a batch already taken off the queue, and the next forward copy put the title's older bytes over them.
The decision is now taken for every page of the range before the first copy, into a host-heap vector the
tracker keeps. A page the title wrote before the write-back (not guarded and not clean after the asking)
is still held, so the title's words win as before.
Test: d3d12_title_window_write_back_fault_tile (d3d12_title_va_test --window-writeback-fault-tile), the
#2418 fault case with a second dispatch on the third page of the tile and a title write on the second.
Found by DadaCap's review of #2418.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolve runtime/README.md conflict with #2220 (LEONIDA_PIPELINE_ORDER_LOG)
and note in docs/d3d12_vulkan.md that LEONIDA_COMPILE_GATE=adaptive and 1
gate graphics creates as well (#2412).
Review of #2462:
- An unclear calibration (a slow submission the create does not explain) is no
longer written to compile_gate_calibration.txt, and an unclear line an
earlier build wrote is not read as a cached verdict: the next launch measures
again instead of staying classic until the driver changes
(compile_gate_policy::cache_verdict, tested in compile_gate_policy_test).
- After a fence wait that fails, the fence and command pool are destroyed only
once the pending submission's fence signals (up to 60 s); on a lost device or
if it still has not, they are left alive. The verdict stays classic.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With LEONIDA_COMPILE_GATE unset or `auto`, the device measures the driver once
before the VulkanContext exists: a worker creates a synthetic compute pipeline
(SPIR-V written in code, new specialization seed each run, no pipeline cache)
while the queue gets empty submissions waited on a fence. Quick submissions with
the create still in the driver give the adaptive gate; a held one, a create too
fast to tell, an unexplained slow submission or any failure give classic. The
verdict is cached per vendor/device/driver/pipelineCacheUUID in
compile_gate_calibration.txt beside the pipeline cache;
LEONIDA_COMPILE_GATE_RECALIBRATE=1 measures again. Only the title process
calibrates; build hosts and tests keep classic. `classic`, `adaptive`, `1`, `0`
still force a mode.
Tests: compile_gate_policy_test (no GPU: settings, NVIDIA version decode,
verdicts, cache file, SPIR-V shape) and vk_compile_gate_calibration_test (GPU:
a verdict within 20 s, adaptive on NVIDIA 621.40).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Second review on #2412:
- enter_submit no longer switches to the held limit for good once it sees a create of a kind that holds
submissions: each pass checks held_blocking(). With one in the driver the held limit applies on its own (also when
it is under the plain limit) and the budget is off; without, the plain limit and budget apply, so a submission
goes ahead at the plain limit once the holding create left while a create of a kind with no verdict stays. The
"reached the limit, but ..." notice comes once per wait, and the wait wakes when a holding create leaves.
- SubmitTurn passes `probe` into enter_submit: a present or wait never goes ahead at the probe budget, only at the
limit.
- VulkanPresenter::submit's turn does not probe (it covers the startup frame's fence wait and the present); the
nested queue_submit turn still probes the vkQueueSubmit alone.
- submit_mutex_test: gate_second_review covers the four cases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- CreateKind, CreateInfo and GateAhead move out of SubmitMutex to namespace scope (CompileGateKind,
CompileGateCreate, CompileGateAhead), with using aliases so SubmitMutex::CreateInfo and the rest still work:
clang and GCC reject a `= {}` default argument of a nested class with default member initializers inside the
enclosing class (review on #2412).
- LEONIDA_COMPILE_GATE unset is the classic gate again, as on main: compute creates only, no limit, no probes;
graphics creates do not pass it. The adaptive gate is LEONIDA_COMPILE_GATE=adaptive: on a driver that holds
submissions (630.20) its first probe of a kind enters the driver after the 2.5 s probe budget and loses the
device if that create stays 10 s more, which the classic gate waits out. `1` and `0` are unchanged.
- submit_mutex_test: a classic-gate case (a graphics create holds nothing back, a 3 s compute create is waited out
with no verdict), the adaptive cases opt into graphics gating; passes unset, with `adaptive` and with `1`.
- README rows and docs/d3d12_vulkan.md say so; the "held verdict 2 s into the probe" wording is corrected (it comes
when the probe returns, and the probe holds the submit lock until then).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The title writes the same words into thousands of slots of its console
descriptor heap, many of them data that reads as a V# into title memory
with no copy, and each fix_vsharp of those costs VirtualQuery calls: a
third of the submitting thread's time after the story load under Wine.
refresh_console_heap now keeps fix_vsharp's answers for the pass over the
copied slots and, separately, for the pass over the slots the changes
since the last submission touch, and drops them after each.
Retail boot, Proton, RTX 3090, 900 s: heap refresh after the story load
107.8 -> 18.8 ms a submission (max 5,332 -> 1,109), submissions after it
1,977 -> 2,527.
AI-assisted; tested under WineHQ 11.22 and GE-Proton11-14 on an RTX 3090
(NVIDIA 590.48.01).
The demanded job leaves the running list only after its function returns,
so has_compile_waiter() could still see it right after the log said it had
finished (1 failure in 10 runs). Found while merging this into #2275.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cs_pk_mul_lo_const runs 32 lanes that each store 16 bytes, 128 words, but
run_gpu_tests.py and check.py ran it without VKC_OUT_WORDS, so
vk_compute_test gave it the default 64 and lanes 16-31 wrote past the
buffer through its device address (GPU-AV:
VUID-RuntimeSpirv-PhysicalStorageBuffer64-11819). Both runners now pass
VKC_OUT_WORDS=128 (check.PK_MUL_LO_ENV), and the fixture's Run line
says so.
run_gpu_tests.py's dump rows (the bounds group, cs_heap_init,
cs_user_sgprs) showed the printed words whenever there were any. A run
that printed the right words and then exited non-zero (validation
errors) was a FAIL that showed only the expected words. dump_result now
reports "exit N: <output>" for a non-zero exit.
Found by kevincommits in the coordination issue (#1608).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With LEONIDA_COMPILE_THREADS=1 (the layout of main: one general worker and
the graphics-only one) next_locked did not look at the owner, so two variants
of one graphics PSO could build at once, one on each worker. A last Release
meanwhile hands the PSO's deletion to the running build it finds first
(after_running), and that worker deleted the PSO while the other was still
in build() on it. The single-worker selection now skips a job whose PSO has
a build running, as the pool already did.
New test in d3d12_compile_pool_test: two graphics variants of one PSO with
one worker plus the graphics worker; the second must not start while the
first runs, another PSO's build still uses the free worker, and a last
Release runs after the first build with the second dropped. It fails
without the fix.
Docs: the heavy count also bounds LEONIDA_SERIAL_BIG_COMPILES=0 on the
workers; only compute creates pass the compile gate.
Found by qhukz13 in review of #2233.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ms_w64x_wave_loops: each wave loops while a wave-wide count is not 0, 5 iterations in wave 0 and 9 in
wave 1, so under --wave64-emulate the waves reach different numbers of workgroup barriers. Written after
stalls were reported with the serial form on an RTX 2070 SUPER (issue 1608). On an RTX 4080 (NVIDIA
621.24) it draws 4096/4096 pixels exact side by side and in series; that does not explain the stalls.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
#1920 stops a new pool extent at the next mapping in either view and, in
d78b0e90a, refuses a mapping shorter than the request: such a request
gets a zero-size view (loads 0, stores dropped). That keeps the views from
reaching past their copies, which the first version of this fix did not,
but a refusal is a regression for requests main served: a V# or a root
view (64 KiB, kRootViewBytes) that crosses from one extent into the next,
or starts inside one and ends past it. Stream-pool extents sit next to
each other, so these are common. Retail runs (local, RTX 5080): our
first version, which gave such requests a short mapping, crashed in the
title in one run (m176); with one mapping over the request instead, runs
m177-m179 ran without one. The crash's cause was not established.
At #1920's two refusals of a request longer than what would serve it
(the extent clipped by clip_title_pool_extent, or the mapping holding its
first byte), resolve_pool_memory now tries, after #1920's reuse,
cover_pool_request: one mapping over the request and every window it
reaches, each whole, in both views (title_pool_cover). It is entered with
the new TitleVaAliases::replace_twin, since add_twin refuses an overlap;
the windows it replaces are retired at the next submission like any
window nothing answers from (previous commit), and their bytes are not
charged to the budget twice meanwhile. With a range in the way that is not
a window (a slot-133 buffer, a geometry copy), or pages that are not
readable, #1920's refusal stands. #1920's own refusal when the window
cannot be made or entered is unchanged.
TitleVaAliases::resolve and resolve_title_memory already ask again for a
range that ends before the view (#1920); vsharp_address did not, so a
V# whose base lies in a short range was refused without asking: it now
asks resolve_title_memory first.
Tests: #1920's pass unchanged (their seam cases cross a range that is
not a window, which still gets a zero-size view). title_window_plan: the
cover takes a window whole in both views, refuses a buffer in the way and
past the cap, and replace_twin enters it where add_twin refuses.
d3d12_title_window_orphans: a 64 KiB CBV across two extents' seam gets
one 448 KiB mapping, a valid V# and the right words on the GPU, and the two
extents are retired at the next submission; without the cover its V# base
is 0.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
On top of #1920, which keeps stream-pool extents inside the free span in
both views and makes TitleVaAliases::add_twin refuse an overlap. Other
ranges still replace a window's ranges whole by design: a slot-133 buffer
the title places over the window's pages, a BVH input's exact copy, an
upload alias. The window was then left in title_windows and pool_spans,
refreshed at every submission and charged to LEONIDA_TITLE_WINDOWS_MB, with
nothing left to reach it. A retail run showed most of the windows' memory
growth to be such orphans; the title released none of the windows' pages,
so retiring a window with its pages never fired.
A window no range answers from any more is retired after the next
submission's refresh: the alias table's listener records removed ranges'
copies, and the touched windows TitleVaAliases::answers_into finds
unreached leave pool_spans and the budget at once.
Its memory is freed only once nothing can still reach it. A command list
resolves its root views when they are recorded (GraphicsCommandList::
address), so a list recorded before the retirement and executed, or
executed again, after it still names the window; so does the copy of the
lists a held batch (a Wait before its Signal) or the async worker keeps.
Each backend CommandList, and each copy, now carries the epoch its
recording began in (src/command_list_epochs.h); a retired window takes its
ticket only once no list older than its retirement is alive and the
replays in progress then have recorded their submissions, and is freed
when that ticket has completed.
Until then the copy stays a target of its pages' tracker, still
refreshed, and what the GPU wrote in it goes back to the title's pages:
with its submissions' write-back, and once more when it is freed. The
second is needed because a list's ranges are noted when it is recorded,
so a held batch's can go back, tagged with a later submission's ticket,
before the batch has run. A retired copy goes back only where no live copy
changed the bytes, and every copy is compared, 256 bytes at a time, with
the title's bytes as they were before any copy went back: a copy that
still held them used to overwrite another's result when it came later in
the tracker.
Tests: d3d12_title_window_orphans. A window a slot-133 buffer replaced is
retired, its bytes returned and its memory freed. A writing batch held on
a fence on another queue, recorded before the next submission's refresh
retires its window, keeps the window alive past a completed submission,
and once it has run and its list is reset, its 64 words reach the title's
pages and the window is freed. Without the retirement the window stays
(3 -> 3 windows); without the list epochs it is freed while the held batch
still names it; without the last copy back 0 of 64 words arrive.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
DadaCap identified the short-mapping case in the review of PR #1920. Reuse and allocation now require full coverage, preserving both live aliases when a request spans a boundary.
With LEONIDA_ASYNC_PIPELINES=1 every pipeline not built yet has its draws
and dispatches skipped, the small lighting and post programs too, so the
world phase stays black while anything compiles; with it off a submission
waits minutes for each large ray-tracing compute program.
LEONIDA_ASYNC_MIN_BYTES=<n> (with async on): a pipeline under n bytes of
SPIR-V (compute: its module; graphics: all its stages) takes the path of
async off: no record-time request, PipelineCompiles builds it at prepare,
and a compile-helper build waits for its file. One of n bytes or more stays
asynchronous. Decided once per pipeline at create (async_build). Unset, 0
or not a number: every pipeline asynchronous, as before.
Tests: async_pipeline_size_test (parsing and decision);
d3d12_m4_compute --async-small-waits (both pipelines under the threshold,
compile worker held: no job reaches it and every word matches).
Docs: runtime/README.md row, docs/d3d12_vulkan.md, docs/running.md.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d3d12_title_window_write_back_fault (d3d12_title_va_test --window-writeback-fault) makes the write-back's
copy fault on a page the tracker did not protect itself, the case review asked for: the ring is
write-watched, a title write makes its first page hot (writable, asked about rather than guarded), and
a geometry watch cell over the first 64 KiB, what a geometry view over the same pages arms, makes that
page read-only behind the tracker's back. The next submission's write-back faults there; the handler
reopens the cell and asks every tracker over the address, the writing one too. Without 95d25843e that
tracker took its own SRW lock again on the thread that held it and the submission never returned: a
watchdog thread ends the process with a FAIL line after 20 s. With it the submission returns and the
GPU's words, and the title's own word, are in the pages.
RTX 4070 Laptop, NVIDIA 621.40, Windows 11: passes (8 of 8 runs) with the fix; with the fix's
d3d12_vulkan_device.cpp change reverted locally it fails at the watchdog. d3d12_title_va,
d3d12_title_window_write_back and d3d12_placed_write_back pass either way.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>