Files
metal2vulkan/docs
Anees Iqbal 0d65ab5579 Staged scatters: a first scatter into guest RAM makes no import or memory on the calling thread
A copy_to_host_ram over a chunk not yet imported used to import on the
encoding thread. With 16 ranges or fewer it imported those ranges alone
(each import about 2.3 ms on NVIDIA, its pages pinned). With more, it
imported the whole chunk or waited for its import. A clip switch's
block release (a device cache scattered to fresh guest pages) paid this
on the guest's command thread.

Such a scatter is now staged through the CPU, on devices whose thread
imports ahead (plans other than Plan::listed), as gathers are since
31e642ecc2. It leaves its work to two host steps, as two new Deferred
kinds:
- ScatterStage: the host step of the copy's batch waits for no GPU
  work (Deferred::records_at_its_step, which Segment::needs_step and
  known_summary take). It takes a region of the command buffer's arena
  and records one vkCmdCopyBuffer of the bytes the runs cover into it,
  at the copy's place in the encoder. The region's memory is made on
  neither the encoding nor the committing thread (commit_and_submit).
- ScatterDrain: gpu_written, so the rest goes in a part and batch of
  its own. Its host step waits until every VkQueue completed what was
  submitted before it, then copies each run's bytes from the region
  into the pages in run order (a host byte named twice keeps the
  later's), before that batch is submitted. So the pages hold the bytes
  once the command buffer completed, and every later command sees them.
The buffer's read is noted where the GPU copy lies. The chunk keys'
writes are noted at the start of the drain's part, so hazard tracking
orders later readers of those host bytes after the CPU's write, and
the RAM's log hears of them as of a copy through the imports. Each
chunk is taken and asked of the import thread, as a staged gather's is.

An encoder that holds a staged scatter keeps no replay log
(Unlogged::Segments). A rebind before first submission of a buffer
over host pages that the same encoder named fails that submission
with EncodedUseRebound. The HOWTO says so.

Counted:
- WorkCounts::host_ram_scatters_staged
- WorkCounts::host_ram_staged_drain_nanos
Upload::copy_out reads a region back.
host_ram_block_release_bench prints both.

Tests (host_ram_copy_device, device tests):
- a_first_scatter_over_chunks_the_thread_imports_is_in_its_pages_once_it_completes:
  40 single pages over two chunks not imported, one page named twice,
  from a Private buffer the GPU wrote just before in the same encoder.
  At encode: staged 1, no run import, no chunk imported by a copy, no
  import wait. At completion: the pages hold the bytes (the later's for
  the page named twice), the page beside them is untouched, and a
  buffer over one page reads them in a later encoder and in another
  queue's command buffer committed after.
- a_first_scatter_of_few_ranges_imports_nothing_on_the_encoding_thread:
  four pages over two chunks, which the old path imported as ranges
  alone. At encode: no run import made. The thread then imports exactly
  the two chunks, and the next scatter goes through their imports.
- The window test's scatter past the window is now staged: no copy
  import, the thread imports its chunk.

Not yet verified; the gem10 checks follow this commit.

(cherry picked from commit 93965ee58ed9b238084696fa80b910745d6dc281)
2026-11-08 11:12:40 -08:00
..