From c72bbdb12562b68db083bec3e57f3cf89f74fc76 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 12 Nov 2026 23:09:31 -0800 Subject: [PATCH] A new buffer's zeros are one fill on every device, unified memory too What: `FirstFill::zeros` set a new buffer's word-aligned zeros with one `vkCmdFillBuffer` only on a device without unified memory; a device that shares the host's memory copied the device's zeros instead. It now fills on every device (copies stay for a range whose ends are not multiples of 4). The first-use lane's `a_new_private_buffer_reads_zero_through_its_first_fill` asserts exactly one fill (`WorkCounts::new_buffer_zero_fills`) on every device; it asserted none on unified memory. The docs of `zeros`, `first_move.rs`, `word_fill.rs` and `new_buffer_zero_fills` say so. Why: the copy read the zeros as well as it wrote the range, which costs on unified memory too. Measured on one blit zeroing 17,899,520 bytes, GPU time by timestamps, median of 20 (a diagnostic branch, not committed): - Radeon 780M (RADV, Mesa 26.2.6): fill 0.532 ms, copies 0.748 ms; the command buffer's wall time 1.556 vs 1.900 ms. - Iris Xe (ANV, Mesa 26.2.6): fill 0.517 ms, copies 0.527 ms; wall time 0.948 vs 1.070 ms. The discrete numbers already in the doc (RX 7900 XTX) are unchanged. Verified (at the same change on bfcd63c5e2): the first-use lane on the Iris Xe and the 780M (11 passed each, "its zeros 1 fills" on both); clippy over the workspace, all targets and features, green. Not FAST-PATHS material: no accuracy is traded. --- .../src/texture/first_move.rs | 29 +++++++++---------- crates/metal2vulkan-runtime/src/word_fill.rs | 3 +- .../metal2vulkan-runtime/src/work_counts.rs | 2 +- .../src/runtime_lane/first_use.rs | 14 ++++----- 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/texture/first_move.rs b/crates/metal2vulkan-runtime/src/texture/first_move.rs index 877243bda..0e8b91c4e 100644 --- a/crates/metal2vulkan-runtime/src/texture/first_move.rs +++ b/crates/metal2vulkan-runtime/src/texture/first_move.rs @@ -94,9 +94,9 @@ //! byte over such pages (`in_place.rs`); elsewhere it is a shadow, whose copies address its texels //! alone (`texture.rs`'s `storage_over_buffer`). //! - **The work.** A barrier from every command before (memory other commands wrote), then the -//! buffers' fills with zeros (copies of the device's zeros; on a device without unified memory, -//! which would read those over the bus, one `vkCmdFillBuffer` each, counted: -//! `WorkCounts::new_buffer_zero_fills`, `FirstFill::zeros`), then their copies of staged bytes +//! buffers' fills with zeros (one `vkCmdFillBuffer` each, counted: +//! `WorkCounts::new_buffer_zero_fills`; copies of the device's zeros for a range whose ends are +//! not multiples of 4, `FirstFill::zeros`), then their copies of staged bytes //! after a barrier, then a //! barrier carrying each image's move (after the fills: an image placed in a new heap moves //! after the heap's fill, so the fill never overwrites what the move wrote), then the images' @@ -853,9 +853,8 @@ pub(crate) struct FirstFill { buffer: vk::Buffer, source: vk::Buffer, copies: Vec, - /// The words one `vkCmdFillBuffer` sets to zero in place of the copies (none then): on a - /// device without unified memory, whose zeros (`Zeros`, host-visible) a copy would read over - /// the bus ([`FirstFill::zeros`]). + /// The words one `vkCmdFillBuffer` sets to zero in place of the copies (none then) + /// ([`FirstFill::zeros`]). filled: Option>, /// What keeps the staging buffer `source` is, held until the submission that makes the fill /// completed; `None` where `source` is the device's zeros. @@ -913,13 +912,14 @@ impl FirstFill { } } - /// The zeros of `range` of `buffer` on `device`: one `vkCmdFillBuffer` of its words on a - /// device without unified memory (`word_fill.rs`), where the range's ends are multiples of 4; - /// else copies of the device's zeros, made now if they are not yet. A discrete GPU reads those - /// host-visible zeros over the bus: on the RX 7900 XTX (RADV) 512 KiB of them took 66 us to - /// copy and 11 us more in the barrier after it, where a fill of the same bytes took 0.7 us and - /// 2.5 us. The copy stays where the device shares the host's memory, whose zeros lie in the - /// memory the GPU reads anyway (not measured there). + /// The zeros of `range` of `buffer` on `device`: one `vkCmdFillBuffer` of its words + /// (`word_fill.rs`), where the range's ends are multiples of 4; else copies of the device's + /// zeros, made now if they are not yet. A copy reads the zeros, host-visible, as well as it + /// writes the range: on an RX 7900 XTX (RADV) 512 KiB of them took 66 us to copy and 11 us + /// more in the barrier after it, where a fill of the same bytes took 0.7 us and 2.5 us. On a + /// device that shares the host's memory a fill costs no more: zeroing 17.9 MB in one blit took + /// 0.53 ms of GPU time as a fill and 0.75 ms as copies on a Radeon 780M (RADV), and 0.52 ms and + /// 0.53 ms on an Iris Xe (ANV), medians of 20. fn zeros( device: &Device, moves: &FirstMoves, @@ -927,8 +927,7 @@ impl FirstFill { range: std::ops::Range, ) -> Result { let words = range.start.is_multiple_of(4) && range.end.is_multiple_of(4); - let discrete = !device.properties().has_unified_memory; - if words && discrete { + if words { return Ok(FirstFill { buffer, source: vk::Buffer::null(), diff --git a/crates/metal2vulkan-runtime/src/word_fill.rs b/crates/metal2vulkan-runtime/src/word_fill.rs index fd272e080..4fdaf17db 100644 --- a/crates/metal2vulkan-runtime/src/word_fill.rs +++ b/crates/metal2vulkan-runtime/src/word_fill.rs @@ -12,8 +12,7 @@ //! (median) from its commit to `Scheduled` on an M3 Max, where `vkCmdFillBuffer` takes 0.6 ms, //! and Metal schedules the same fills 0.04 to 0.18 ms after the commit. //! - **Counted.** Each fill counts once (`WorkCounts::fills_recorded`); a new buffer's first zeros -//! filled on a device without unified memory count apart (`WorkCounts::new_buffer_zero_fills`, -//! `texture/first_move.rs`). +//! filled count apart (`WorkCounts::new_buffer_zero_fills`, `texture/first_move.rs`). use crate::device::functions::Vk; use crate::Device; diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 8eee845ca..560650513 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -8581,7 +8581,7 @@ pub struct WorkCounts { pub new_buffer_bytes_cleared: u64, /// New buffers' bytes the host cannot write (`Private` storage, a `Managed` buffer's device /// copy, the device's neutral buffers) that a submission's first work set to zero with one - /// `vkCmdFillBuffer`, on a device without unified memory, in place of copies of the device's host-visible zeros, which such a device reads over the bus + /// `vkCmdFillBuffer`, on every device, in place of copies of the device's host-visible zeros /// (`texture/first_move.rs`); fills apart from `fills_recorded`. pub new_buffer_zero_fills: u64, /// Copies between a shadowed texture's truth and its shadow that encoders planned diff --git a/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs b/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs index 027ccfb18..6f0576983 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs @@ -1056,9 +1056,8 @@ fn a_new_resource_s_zeros_come_before_every_use_on_every_queue() { /// A new `Private` buffer larger than the device's zeros (`ZERO_BYTES`, 4 MiB), made where one /// whose every byte was `0xa7` was just released (most likely its memory), reads all zero after -/// the submission that took its first work: zeroed by one `vkCmdFillBuffer` on a device without -/// unified memory whose fills are not transfers (`WorkCounts::new_buffer_zero_fills` counts -/// exactly one), by copies of the device's zeros elsewhere (it counts none). +/// the submission that took its first work: zeroed by one `vkCmdFillBuffer` on every device, of +/// unified memory or not (`WorkCounts::new_buffer_zero_fills` counts exactly one). #[test] fn a_new_private_buffer_reads_zero_through_its_first_fill() { on_device(|device| { @@ -1084,12 +1083,10 @@ fn a_new_private_buffer_reads_zero_through_its_first_fill() { command_buffer.wait_until_completed_for(WAIT).unwrap(); assert_eq!(command_buffer.error(), None, "on {name}"); let filled = device.work_counts().new_buffer_zero_fills - before; - let fills = !device.properties().has_unified_memory; println!( "FIRST-USE on {name}: a new {len}-byte Private buffer {} the released one's memory, \ - its zeros {}", - if reused { "took" } else { "did not take" }, - if fills { "a fill" } else { "copies" } + its zeros {filled} fills", + if reused { "took" } else { "did not take" } ); let bytes = read_buffer(&out, 0, len).unwrap(); let other = bytes.iter().filter(|&&b| b != 0).count(); @@ -1098,8 +1095,7 @@ fn a_new_private_buffer_reads_zero_through_its_first_fill() { "bytes of a new Private buffer that read other than zero on {name}" ); assert_eq!( - filled, - u64::from(fills), + filled, 1, "fills of the new Private buffer's zeros on {name}" ); });