Files
metal2vulkan/docs
Anees Iqbal 8ba1fd7d67 RUNTIME 2358: Host RAM copies: a repeated copy takes the plan kept of it, where its buffer, the device's figures and its imports are the plan's
A copy by dispatches spent most of its encode on work that depends only on its runs, its
buffer and the imports it reads through: cutting the runs into pieces, the chunks' ranges,
the pieces' places in the imports, the table and the dispatches. A consumer repeats the same
copy (a surface's same runs) every frame.

Such a copy's plan is now kept per RAM per device (RamCopies, which ChunkKeys becomes):
direction, runs' form, buffer key (object id, device generation, length), the runs, the
blit_at VkBuffer and byte, the device's figures (window, alignment, workgroups, array), the
span and total, the chunk ranges and joined ranges, the import serials in order with the
chunk-to-imports map, the table and the dispatches. The next copy with the same key (runs
compared element by element, no hash trusted) takes the table and dispatches where:
- blit_at gives the same VkBuffer and byte (a rebind or a new VkBuffer plans anew);
- the device's figures are the same;
- every chunk takes at once (copy_take Now for all; else the full path stages it);
- copy_imports gives the same imports, by serial number, in the same order, with the same
  map. RamChunk now carries a serial from a process-wide counter, never reused, so a chunk
  released, given back or imported again has a new one. Plans keep numbers, not Arcs.
Every step a copy takes each time still runs: blit_at, copy_wrote, the takes, copy_imports,
the upload, the binds, the uses and the counts. Where the plan does not hold, what it took
(blit, takes, imports) goes to the full path, which takes none twice: a chunk released
between the takes and the imports plans over the imports it got.

Bounds: 8 plans and 4 MiB per RAM per device, the one taken longest ago dropped first; a
plan over 4 MiB is not kept. Counter: WorkCounts::host_ram_copy_plans_reused.

stages() is split into takes() and Takes::staging() so both paths share the takes;
record_dispatches() is the dispatch tail both paths record through.

Tests: unit, the bounds; device (lavapipe): a repeated copy takes its plan and a run
changed, the other direction, another buffer offset, another buffer, a rebound no-copy
buffer each plan anew; a chunk let go of (idle, given back) makes the next copy plan anew
and the one after take the new plan; a new use of the RAM plans anew; a kept plan's copy
is ordered after a fill of its buffer and after a scatter into its pages in the same
command buffer.

The plan also holds the buffer's host pages weakly and compares them by
identity: lavapipe gave a rebound buffer's new VkBuffer the destroyed
one's handle value (m3-max-32579), so a handle match alone cannot see a
rebind. The plan names no VkBuffer; the binds take the live one on every
call. The pages copy's kernel is looked up only where a copy goes by
dispatches, as before. The layer bench says how many copies a kept plan
recorded.

Measured (release, msi lavapipe, m3-max-32581, before d0ef5e0145 and
after, one job, median us a call over 8100 single-page runs): layer in 1
chunk 373.9 -> 139.1, over 128 chunks 859.8 -> 323.4; at 0bae61504a
(m3-max-32615) 200 of 200 copies by a kept plan.

Verified at 0bae61504a (m3-max-32610): host_ram tests on lavapipe
103/103, clippy plain, m2v_gpu and m2v_cpu_icd 0, rustdoc 0. Negative
control (m3-max-32579): with the serial compare removed, the let-go test
fails (a reuse happened).
2026-11-15 08:30:44 -08:00
..