`BlitCommandEncoder::copy_from_host_ram(ram, runs, destination, destination_offset)` gathers runs
of a `HostRam` the device uses into consecutive bytes of a buffer, and
`copy_to_host_ram(source, source_offset, ram, runs)` scatters them back (a runtime extension:
Metal has none). A surface that is a scatter of guest pages is copied straight from the RAM's
64 MiB chunk imports, with no view mapped per surface (one mmap per run) and no import made per
surface.
How: adjacent ascending runs merge, descending neighbours stay apart; runs are cut at chunk ends;
the 4 KiB pages become a table in the command buffer's upload arena, and each chunk the runs touch
takes one dispatch of the new `bind::pages_copy_module` (more only past the storage-buffer range
or the workgroup count). A chunk not imported yet is imported, or its pending import waited for,
as an object over it does.
Ordering: each chunk a copy uses gets a resource key placed over the chunk's host bytes in the
host-alias placements, the way no-copy buffers and heaps are placed, so the copy's chunk uses
(joined to at most 16 ranges per chunk) order against other copies and against every no-copy
object over the RAM's own addresses, in one encoder and across encoders; the buffer side is
tracked like any blit. A scatter's writes reach the RAM's dirty log as the GPU's in place, so a
copied backing over those pages takes them in at its next commit. Objects made over another
mapping of the pages (a view resolved through `with_runs`) are not ordered against the copies.
Refused with `Problem::HostRam`: an empty run, one off 4 KiB, one outside the RAM, a RAM the
device does not use, buffer bytes off a 4-byte word; no host import is `Unsupported`; bytes past
the buffer are `RangeOutOfBounds`. Replayed at rebinds (`BlitCall::HostRamCopy`). New counters
`host_ram_copies`, `host_ram_copy_bytes`, `host_ram_copy_dispatches`. README, HOWTO, the
API signature rows and docs/REFUSALS.md updated.
Verified:
- On this M3 Max (MoltenVK), --cfg m2v_gpu under nextest: the five new device tests
(`tests::host_ram_copy_device`: round trips byte for byte over ascending, descending, adjacent,
repeated and chunk-crossing runs into Shared and Private buffers; CPU writes before the commit
gathered and a scatter's bytes in the pages after completion; the two hazard cases in one
encoder and across encoders; a copied backing over the pages taking a scatter at the next
commit; the refusals) and the ten `tests::host_ram_device` tests: 15 passed.
- Negative controls, each a saved build: merging descending neighbours fails
`adjacent_ascending_runs_merge_and_descending_ones_stay_apart` and
`a_gather_and_a_scatter_move_every_runs_bytes_in_order` ("the gathered bytes"); not placing the
chunk keys over the host bytes fails
`copies_are_ordered_against_a_no_copy_buffer_over_the_same_pages` ("a later reader sees the
scatter"). The first control found the test pattern repeating every 1 KiB, so a page swap
went unseen; the pattern now differs per page.
- The `host_ram::copy` unit tests (merge, refusals, chunk cuts, the plan, the joined uses) and
`consumer_auto_traits`; clippy --workspace --all-targets --all-features -D warnings, plain and
--cfg m2v_gpu; rustdoc -D warnings; `signatures.py --check` after the bless; the refusal
surface `--check`.
- The ignored release bench `tests::host_ram_copy_bench` on the M3, a 3840x2054 BGRA8 surface
(31,549,440 bytes, 31,551,488 in whole pages) scattered over 1 GiB of shared RAM: 7703 4 KiB
runs (5490 descending neighbours, 7279 runs after the merge) take 16 dispatches, 0.35 ms of CPU in the call, 0.59 ms of GPU and 0.64 ms from commit to completion
for a gather (scatter 0.37 / 0.58 / 0.64). The view path at the host's 16 KiB page (1926 runs:
a view mapped run by run, a buffer `with_runs`, a buffer-to-buffer copy, the release) takes
1.58 ms to map, 2.36 ms to make the buffer, 0.99 ms commit to completion and 0.78 ms to
release; the gather of the same 1926 runs takes 0.14 ms CPU and 0.60 ms commit to completion.
Ported onto gate/stack27, where the device's thread imports a window of chunks ahead of the
objects (paced, with the memory lock released, short of maxMemoryAllocationCount and giving back
what no object took). The conflict was `RamOnDevice`'s fields alone: the copies' `ChunkKeys` sit
beside the window and its wake, and the old cursor is gone. The copy still takes each chunk
through `RamOnDevice::chunk(.., By::Object)`, the objects' own path, which now moves the window
past it, wakes the thread and clears the chunk's untaken mark; there is no second import path. The
copy module docs and HOWTO say so. The ignored release bench waited for every chunk imported
ahead, which the window never does; its warm-up copy now imports the chunks the runs touch.
The verification above was on the old base; this tree's is in the commit that follows.
(cherry picked from commit 923718777c198bef526dd07f701fc71a71e07b00)
(cherry picked from commit fa00224f7b29ab0359c8305a47da9c9273fc5d37)