Commit Graph
2 Commits
Author SHA1 Message Date
Anees Iqbal 574d199d67 RUNTIME 2236: a managed synchronize decides its copy as it is encoded and records it in place
RUNTIME 2231 split the guest's encoder at each synchronize of a Managed buffer into its own
deferred segment (Deferred::SyncOut): every command buffer holding one paid an extra continuation
VkCommandBuffer and a segment VkCommandBuffer, its batch could no longer fold its end (an extra
VkSubmitInfo), and the copy was decided at submission. On the consumer's 2080 Geekbench walk that
showed as ~1 extra no-dispatch command buffer per synchronize.

Now the copy is decided at encode, from the kept writes of other command buffers plus this command
buffer's own writes before the synchronize, and recorded in place with only the written regions;
an unwritten synchronize records nothing. The copy is whole where an earlier segment is unknown,
the command buffer writes memory no use names, or the buffer was handed out. At submission,
settle_syncs_out adds a top-up command buffer just before the part holding the copy, covering only
writes from other command buffers noted after the encode, between two all-stage barriers
(WorkCounts::managed_synchronizes_topped_up, new). The SyncOut segment is gone (deferred.rs is
back to its 041038a914 form); may_fold folds again.

Verified (mansync, 4e3e22f4d6 on 8a81350e13; 2080 release, one run each, 26257), per command
buffer, 041038a914 / 8a81350e13 / fix:
- all (40 written): VkSubmitInfos 1/2/1, VkCBs 2/4.1/2, GPU 95.61/95.66/95.62 ms
- mixed (30 written, 10 CPU did_modify_range): VkSubmitInfos 2/3/2, VkCBs 3/5/3, copied
  176/132/132 MB, GPU 95.4/71.6/71.6 ms
- none (40 unwritten): VkCBs 1/3/1, GPU 95.05/0.014/0.007 ms
- small (import shape: 1 MiB fill + 2 unwritten 335,872 B no-copy synchronizes): VkCBs 2/4/2,
  commit-to-completion 0.519/0.111/0.116 ms (keeps ~4.5x)
vkQueueSubmit calls equal in every row; topped_up 0 in every fix row. Tests on the 2080 (26258):
managed_copy_device, detached_contents, kept_padding, device_memory_overflow 41/41, with two new
steps (a write committed before but encoded after the synchronize: topped up 1 and every byte read
back; a write committed after: nothing copied, the next synchronize copies exactly that range).
clippy plain 26228, --cfg m2v_gpu 26227, --cfg m2v_cpu_icd 26229 all 0; rustdoc 26230 = 0.

RUNTIME_VERSION 2235 -> 2236.
2026-11-13 12:24:45 -08:00
Anees Iqbal d38b5c4ef6 RUNTIME 2231: a managed synchronize copies out only the bytes the GPU wrote
On a device without unified memory, synchronizeResource: copied the whole Managed buffer (or a
texture's whole span over one) to its host copy every time. The runtime now keeps the GPU's
writes for each buffer that has been synchronized once (managed_sync.rs: GpuWrites, SyncCopy,
SyncOut), stamped in the scheduler's submission order across all queues. The copy-out is its own
segment (Deferred::SyncOut), decided when its batch is submitted, so it covers the writes of
earlier batches, of earlier encoders in its batch and of its own encoder, including a write
encoded after the synchronize but committed before it. Completion of the command buffer holding
the copy drops only the writes noted before the copy. It copies nothing when the GPU wrote
nothing since the last copy, and still copies the whole buffer where the writes cannot be known:
an untracked buffer; one whose VkBuffer was handed out or whose memory is exported; a buffer's
first synchronize; any batch since the last copy, not yet complete, that may write memory no use
names (State::writes_unnamed); and a synchronize recorded in place (a predicated loop's laying, or
no VkCommandBuffer could be made). Every barrier is still recorded. Pages no import maps copy back
only the regions copied. Textures over buffers take the same path (an image range counts as the
whole buffer). Touches command_buffer/submit.rs (record_syncs_out after record_scrubs) and
command_buffer/mod.rs (the footprint loop skips SyncOut segments), encoder.rs, tracker.rs,
deferred.rs, device/mod.rs. New WorkCounts: managed_synchronizes_unwritten,
managed_synchronizes_whole, managed_bytes_unwritten_skipped. FAST-PATHS managed bullet, API.md
managed section and the runtime README updated. No speed-for-accuracy choice.

Why: on the RTX 2080 the guest's Background Blur spent 0.90 s of GPU time copying out 3.5 GB
over 800 synchronizes (~176 MB a command buffer, the 2080's x4 link), whole resources each time.

Verified (mansync; step codes read inside the jobs):
- clippy --all-targets --all-features -D warnings, workspace: plain 25924, m2v_cpu_icd 25927,
  m2v_gpu 25928, all 0; rustdoc -D warnings 25935 = 0 (at b72228629c).
- RTX 2080 (um790) at ac0887e7f5, 25942: 41/41 (managed_copy_device, detached_contents,
  kept_padding, device_memory_overflow; hazard tests excluded). Counts (copied, skipped,
  unwritten, whole), both Import and Pages: first synchronize (1048576, 0, 0, 1); after no write
  (0, 1048576, 1, 0); after a fill in an earlier command buffer (16382, 1032194, 0, 0). Every
  byte checked after each step, including a write committed before the synchronize's command
  buffer but encoded after it, and a CPU write named by didModifyRange.
- Negative control (throwaway e1542d94c4, 26010): imports copy whole and pages drop kept writes;
  both legs fail.
- Release, 2080, examples/managed_sync_out (40 no-copy Managed buffers of 4,399,104 B, 176 MB
  synchronized a command buffer), median GPU ms over 20, base 010888b011 vs tip (25985):
  40/40 written 95.62 -> 95.58; 4/40 written 95.10 -> 9.57; 0/40 written 95.05 -> 0.014.
Not measured: Background Blur itself (the counters say how much of its 0.90 s goes); lavapipe
(unified memory, the copy-out legs return early).

RUNTIME_VERSION 2230 -> 2231.
2026-11-13 10:34:23 -08:00