Timelines of run d216: the chain is the title's threads, our list threads' ExecuteIndirect calls and, in other
frames, DirectStorage's inflating and threads waiting for a core. LEONIDA_VIEW_CACHE, LEONIDA_CONTENT_TABLES and
LEONIDA_ALIAS_QUICK by turns (d217, d218): none moves the frame; they stay off.
AI-assisted (Claude). Tested on an RTX 4060 Ti, driver 616.64 (Windows 11), Series X profile.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Runs d214/d215 (the list ahead recorded by one thread, sampled the same way): the textures' part 4.64 -> 3.40 ms a
frame, the walk for due slots 2.6 -> 0.3 % of the thread's samples, the whole list 8.98 -> 7.80 ms.
AI-assisted (Claude). Tested on an RTX 4060 Ti, driver 616.64 (Windows 11), Series X profile.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A heap keeps some 25,000 slots waiting for their memory in the queue of textures picked by index, of which 100 to
470 fall due a frame, and every pass walked all of them twice under the queue's lock (the due ones, then the census
for the frame line): 2.6 % of the thread that records the list ahead of each frame, more than any other function of
its textures' part (profile ahead-d214, the merged build). The queue now keeps a set by due frame with every change
of a slot's due frame, and the counts of slots waiting for their memory and uploaded in part with every change of
what a slot waits as; the order a pass serves them in is the writes' as before.
AI-assisted (Claude). Tested on an RTX 4060 Ti, driver 616.64 (Windows 11): descriptor::host::tests (49) and the
queue tests pass; the pass's time is read in a standing run next.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
LEONIDA_INDEX_HEAP_SPANS by turns on the merged build (d212, d213): the frame's median -1.64 and -1.61 ms. With
LEONIDA_BATCH_WHY=1 (d211): 785-841 of 926-988 batches a frame end for programs that store through views they pick
from the heap, the culling passes that write the records first.
AI-assisted (Claude). Tested on an RTX 4060 Ti, driver 616.64 (Windows 11), Series X profile.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After the rANS lanes the plain formats' block loop did its arithmetic a
byte at a time: a per-element wrapping add and width mask for each of 8
endpoints and 32 indices, 16 scattered byte stores per quad/pair
residual, a table lookup and shift per index to build the BC1/BC4 code
words, and two 4-step folds per interleave().
- Widths holds the masks of the elements' bits (a word for the
endpoints, four for the indices); endpoints and indices are summed
with their references eight bytes at a time (top bits added apart for
the 8-bit endpoints, masked sums for the at most 7-bit indices).
- quads() takes a 256-entry const table and fills the four 4-pixel rows
as words; pairs() builds eight pixels at a time.
- BC1 and BC4 index codes are computed for eight indices at once
(bc1_codes, bc4_codes) and packed by shifts (pack2, pack3).
- interleave() reads a 16-entry const table.
Output is unchanged: a copy of the previous loop is kept in the tests as
the reference, and plain_blocks_match_the_reference compares the two
byte for byte for BC1, 3, 4 and 5 on the shipped tables and on tables
whose ranks and dictionaries hold any byte (history wrapping).
index_codes_are_the_tables and index_codes_pack_by_position check the
new code words and packers against the code tables for every byte.
AI-assisted: Claude Sonnet 5.5
Co-authored-by: Cursor <cursoragent@cursor.com>
The BC7 block loop of the BCPack decoder took 100-155 ns a block on top
of the rANS lanes: every index residual was rebuilt bit by bit (two
4-iteration folds per byte), each index went through an add_index with
per-element branches, endpoint residuals were done a column at a time and
bc7_pack made about 45 Bits::put calls on a u128.
- nibbles() reads a 256-entry table (also used by BC6H).
- The sixteen indices of a set are added to the reference's in one pass
with the shifts hoisted out of the loop (bc7_indices).
- Endpoint residuals of a row go through one multiply-by-precision per
column instead of a masked shift per column.
- bc7_pack puts each channel's endpoints, the p-bits and each index set
with one put, taking an anchor's top bit out of the fixed-width fields
afterwards.
Output is unchanged: a copy of the previous block loop is kept in the
tests as the reference, and bc7_blocks_match_the_reference compares the
two byte for byte on the shipped tables and on tables with arbitrary
control words, dictionaries and reference widths (history wrapping), and
on the error of an unused control code.
AI-assisted: Claude Sonnet 5.5
Co-authored-by: Cursor <cursoragent@cursor.com>
draw.rs: heap_buffer_pass takes main's snapshot of the spans (SweepState with the indexed backend, d367675c7);
this branch's change to the legacy backend's parts (a slot that shows each copy, 2a1e66b15) stays under it.
heap_range_tests and heap_sweep_tests pass.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Runs d204/d205 (the standard tour at 23:00): no torn frames either way; the card's steps stay. Profile list-d207:
ExecuteIndirect is binding, and over half the list threads' contended waits were the placed textures' one lock;
LEONIDA_PLACED_SHARDS by turns (d209, d210): the frame does not move. Profile ahead-d208 against ahead-d201: the
spans' holds were not the pass's cost; the walk over the parts' slots is.
AI-assisted (Claude). Tested on an RTX 4060 Ti, driver 616.64 (Windows 11), Series X profile.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
texback::write() already kept a thread-local TILED buffer (capped at 32 MiB)
to avoid per-landing heap allocations, but still allocated and freed a fresh
`let mut before = Vec::new()` on every LEONIDA_COPY_BACK read-back (~22/frame
in gameplay) to diff 4 KiB pages.
Pair `before` with `tiled` in a single thread-local SCRATCH `(Vec<u8>, Vec<u8>)`
(both capped at KEPT = 32 MiB) so copy read-backs reuse their snapshot buffer
without repeated multi-MB allocations/page faults.
AI-assisted: Antigravity (Gemini 4 Neon)
Speed up Layout::detile and Layout::tile (hot on texture refreshes and
async texture read-backs) by:
- Pre-dividing uniform chunk offsets into a compact Vec<u32> (4 B vs 24 B
per run, 6x less L1 cache traffic per row).
- Checking `base + max_chunk_off < tiled.len()` once per row so the inner
loop copies without per-chunk bounds checks or per-chunk divisions.
- Specializing 4-byte and 64-byte chunk sizes alongside 8, 16, and 32.
Measured --release A/B on Ryzen 7 7800X3D across 8 64KB_S/64KB_D layouts:
detile +21.6% to +74.3% (1.22x-1.74x), tile +5.8% to +62.7% (1.06x-1.63x),
with zero regressions and 41/41 leonida-formats tests passing.
AI-assisted: Antigravity (Gemini 4 Neon)
DirectStorage texture streaming runs BCPack decompression on the frame's
streaming critical path (d3d12/docs/findings.md). Previously, every
decoded rANS symbol performed four separate 2-D table lookups (freq,
start, 256-entry bucket window, recip), a while-loop bucket scan, an
if b == 0 branch, and a Result<(), Error> check even though Lane::step
is infallible.
Pack per-model decode parameters into a 64-byte-aligned Model struct
with pre-adjusted b == 0 entries (eliminating the if b == 0 branch),
add a runtime-detected AVX2 single-register vpcmpgtd + movemask +
count_ones bucket lookup (dispatched once per lanes() call), and remove
the unused Result wrapper from Lane::step.
Verified exhaustively across all 3 x 128 x 65,536 = 25,165,824
(table, model, slot) pairs against the reference scan and across all 6
BC lane layouts.
Measured --release bcpack_throughput (Ryzen 7 7800X3D, 16,384 blocks x 40):
- BC1: 86.3 -> 94.7 MB out/s (92.7 -> 84.4 ns/block, +9.7%)
- BC4: 83.0 -> 93.8 MB out/s (96.4 -> 85.3 ns/block, +13.0%)
- BC7: 49.7 -> 53.3 MB out/s (322.0 -> 300.3 ns/block, +7.2%)
AI-assisted: Antigravity (Gemini 4 Neon)
changed_records() scans ~400,000 32-byte descriptor slots per frame
(mirroring ~40,000, d3d12/docs/findings.md). Previously, any dirty
512-byte chunk ran memcmp on the 16-record block and then ran 16
individual memcmp + memcpy calls.
Add a runtime-detected AVX2 fast path for step == 32 that loads four
32-byte records (128 bytes) into ymm registers, checks all four with a
single combined vpcmpeqb + vpand + vpmovmskb branch, and on mismatch
stores the already-loaded new ymm vectors directly with
_mm256_storeu_si256 (avoiding the double-scan and memcpy calls).
Measured in --release on Ryzen 7 7800X3D (400k x 32-byte slots, 50
rounds, bit-exact parity verified):
- 0% dirty: 325.9 -> 309.8 us/frame (1.05x)
- 10% dirty: 432.5 -> 325.9 us/frame (1.33x)
- 100% dirty: 611.3 -> 485.1 us/frame (1.26x)
AI-assisted: Antigravity (Gemini 4 Neon)
Addresses the d3d12/docs/findings.md item ("descriptors made again in
every branch of a program (createHandleFromHeap of an index that is a
root constant plus an offset, behind a branch on a load)").
Adds Module::hoist_handles() under REUSE: marks uniform (nonuniform =
false) dx.op.createHandleFromHeap calls, their wrapping annotateHandle
calls, and their pure, non-trapping index/clamp operands
(cbufferLoadLegacy, integer non-div/rem arithmetic, casts, extracts,
dx.op.binary.i32 clamp), and hoists each in RPO order to the deepest
dominating block defining its operands. REUSE then collapses duplicate
handles across sibling branches and loops into a single descriptor
handle.
Nonuniform handles and handles whose index depends on branch-local
memory loads remain in their original blocks.
AI-assisted: Antigravity (Gemini 4 Neon)
Addresses the d3d12/docs/findings.md item ("the entry ticket of every
console wave64 program with lane code (a group barrier and an LDS
atomic, also for groups of one dimension, where the host's waves are
runs of consecutive ids)").
For 1-D host groups (threads[1] == 1 && threads[2] == 1, including all
multi-wave paired groups which are lowered as [32 * waves, 1, 1]), each
host wave already receives a consecutive run of flat thread IDs. Derive
each wave's console thread base directly from
waveReadLaneFirst(flattenedThreadIdInGroup) & !(wave - 1) (shifted left
by 1 for paired groups) instead of allocating an LDS counter dword and
emitting an entry group_sync + atomicrmw add.
2-D and 3-D groups keep the LDS counter path unchanged.
AI-assisted: Antigravity (Gemini 4 Neon)
Addresses the d3d12/docs/findings.md item ("reads of read-only views
repeated in blocks that follow one another").
Previously, merge() cleared the LOADS map at every basic-block boundary,
preventing reuse of Effect::Reads across sequential blocks,
single-predecessor branch targets, and write-free diamonds or loops.
Extend merge() to maintain dominator-scoped dom_reads for Effect::Reads
while keeping Effect::Lanes (wave/quad operations) strictly block-local:
- Single-predecessor blocks inherit dom_reads directly from their
immediate dominator (O(1)).
- Multi-predecessor blocks (merges and loop headers) run a reverse walk
back to the immediate dominator and preserve dom_reads only when no
block on any path (including loop back-edges) executes Effect::Writes.
- Any Effect::Writes instruction clears dom_reads for the rest of its
block and dominator subtree, restoring the pre-block map on Leave so
siblings remain unaffected.
Verified with 3 new Red -> Green unit tests (write-free diamond,
branch-write invalidation + sibling reuse, loop back-edge write vs
write-free loop), the 400-seed randomized differential interpreter suite,
and 370/370 leonida-shader --lib tests on native Windows 11 + AMD Radeon
RX 7900 XTX.
AI-assisted: Antigravity (Gemini 4 Neon)
On Windows, renaming a staging file over a destination already opened or
mapped by a peer runner (without FILE_SHARE_DELETE) fails with
ERROR_ACCESS_DENIED (os error 5). When the destination already holds the
exact bytes being published, remove the staging file and return Ok(()),
while continuing to propagate errors when the destination is missing or
holds different bytes.
Includes deterministic Windows unit tests for both matching and
non-matching locked destinations.
AI-assisted: Antigravity (Gemini 4 Neon)