Files
metal2vulkan/docs
Anees Iqbal f514cb9a7d Cut shadowed textures' copies to what their commands touch, merge back every buffer another writer may write, and count copies by cause
A texture over a buffer's bytes or another image whose own image is a shadow now copies in, keeps,
merges and copies back only the texels its commands reach, with fewer barriers, descriptor pools
and scratch fills, and merges its writes back wherever another writer may write the bytes during
the run.

What changes:
- Which shadows merge. A shadow merges back (kept before the first command that writes through
  it, only the words its commands changed reaching the bytes) over every buffer's bytes but a
  tracked Private or Memoryless buffer the runtime made: Shared buffers and host pages as before,
  and now Managed buffers (on unified memory the CPU writes the GPU's memory, and without it a
  later upload may run on another queue) and untracked buffers another queue may write. A tracked
  Private buffer's shadow, a command replacing every texel, an image's shadow, and turned and
  two-plane textures still copy back. A merge goes in place wherever the texture's first and last
  words lie in the buffer and the bytes lie in pages; only a first byte inside a word, a staging
  buffer or a last word past the buffer still merges through a copy, a new FAST-PATHS known gap
  since that round trip can undo another writer's write between its copies.
- Correct merges. A command that replaces every texel followed by one that writes part of it is
  copied back before the keep, so the merge carries the later writes. A merge of a part leaves a
  twin stale there alone. A detached buffer's shadow binds no stand-in (the null handle while
  detached), so no merge binds a range past a 256-byte stand-in.
- Cut copies. A read inside a rectangle keeps the rest of the changed rectangle for the next read;
  an image's one-level, one-slice shadow cuts its copies in as a buffer texture's do; a keep and
  merge are cut to the rectangle the encoder's commands write in, in bands within the device's
  limits; a loaded shadowed colour's copies are cut to its draws' scissors also in passes that
  clear another attachment or take a reach clear (Vulkan's scissor test leaves coverage outside
  at 0).
- Cheaper copies. On ANV (DriverFact::ShadowCopyBackDispatchFaster, now every ANV device measured)
  a merge's keep and changed copy are the copy back's dispatch, and a cut merge reads the changed
  texels straight from the shadow's integer view (bind::shadow_merge_image_module), with no copy
  after the commands. On the UHD 770 (new DriverFact::ShadowCopyInDispatchFaster) a merged
  shadow's copy in and its keep are one dispatch (bind::shadow_copy_in_module), its 4-byte view
  R8G8B8A8_UINT because ANV drops a storage image's compression for an R32 view. The shadow
  kernels take a bind::ShadowTexelView. A cut merge's scratch is kept for the next merges and not
  zeroed. The shadow kernels and a linear attachment's merge push their descriptors where the
  device pushes set 0, so a shadowed pass makes no descriptor pool. A run of shadow copies in an
  unordered encoder shares one barrier before and one after, keeping a barrier only between copies
  that touch the same memory and one writes.
- Counters (WorkCounts): every copy in by why it is made and by what holds the texture's truth,
  first copies apart (shadow_texels_first_copied_in_*, shadow_texels_recopied_*,
  shadow_texels_copied_in_images, shadow_texels_bounded_copied_in), shadow_texels_kept,
  shadow_merge_copies_dispatched, shadow_merges_fetched, shadow_copies_in_dispatched,
  shadow_merge_scratches_reused and _zeroed, owned_descriptor_pools, how passes take shadowed
  colours (shadow_attachment_loads_*, _clears, _clears_in_part), shadows_sharing_truth, and the
  tiles a loaded colour's scissors cover against their bounds.
- Docs: shadow.rs and shadow_merge.rs module rules, API.md's shadow entries, FAST-PATHS' costs of
  matching Metal and known gaps; the snapshot rule's figure is now an upper bound.

Why: Metal writes only the texels a command writes, and another writer may write bytes no command
touches during a run. On an M3 Max under macOS 27.0.1, a fill of an untracked Private buffer from
a second queue ran inside a first queue's kernel or render pass 5 times of 6 and both texels kept
their writes, as did a CPU write to a Managed buffer; a tracked Private buffer's fill always ran
after. And in the consumer's Finder drag on the Iris Xe, whole 1080p shadow copies were most of
the composite command buffers' GPU time.

Verified:
- Iris Xe, four shadowed textures a frame over anonymous pages, commit to completion: merged passes
  that draw 11.7-11.8 ms -> 5.0-5.4 ms and scissored ones 11.3-11.4 -> 3.6-3.9 ms across the
  dispatched copies, cut keeps and fetched merges; a 1080p Load pass's GPU time 4572 -> 3484 us
  with the fetched merge.
- UHD 770: a 1080p Load/Store pass 2390 -> 1683 us GPU with the fetched merge; a 1080p Load pass
  1.64-1.67 -> 1.43 ms with the fused copy in; the kept scratch took a 1080p Load pass 4205-4755 ->
  2626-3595 us commit to completion (RX 7900 XTX 4112-4392 -> 1628-1697 us). Shared barriers saved
  about 0.65 us of GPU time per barrier removed on the Iris Xe (680 -> 120 barriers a frame).
- runtime_lane::pitched_surfaces 28 of 28 on the UHD 770, Iris Xe, Radeon 780M, RX 7900 XTX and
  lavapipe, with merges under changing scissors, split instances and two textures over one buffer
  matching a private texture's bytes; under synchronization validation with no message. The shadow
  lanes 123 to 132 of 123 to 132 on the M3 Max, lavapipe, Iris Xe and RADV; extended_range_blends,
  linear_attachments and padded_loads on the RTX 5080 with no validation message.
- Unit tests: tests::shadow (24), clear_reach, owned_pushes, shadow kernels through all four views
  on the CPU interpreter, driver_facts. clippy -D warnings (plain, m2v_gpu, m2v_cpu_icd), rustdoc.
2026-10-19 00:16:00 +03:00
..