From da2f8704679f513cd593a3c2ed347bde7fb3d2d6 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 18 Nov 2026 04:11:19 -0800 Subject: [PATCH] rt: a copy-in records no barrier before its copies A copy-in with no copies back began with a full barrier (every earlier command, every access) before its copies into the device copies. That barrier ordered nothing new: every segment, upload, fill and copy-in on the queue ends with a barrier whose second scope takes every later command, the rule a segment already keeps (record::end: a segment begins with no barrier). So it is gone, and a commit that copies in a Managed buffer's named ranges records one barrier more than one that does not, the one after its copies, where it recorded two. The trailing barrier stays: it orders the command buffer's reads after the copies. A narrower form of it (source stage COPY and TRANSFER_WRITE only, arm E below) changed nothing measurable. Test: tests::managed's every_device_copies_a_managed_buffer_s_changed_ranges counts the barriers of a commit with a copy-in against the same commit with nothing named: one more where the device keeps a device copy, none where it does not. Without the change it reads two. Probe (harness constant_managed and constant_private, 300 runs, order A B E D D E B A, exclusive, one GPU each; wall_us is the host wall a run adds, mean of the two runs per arm). Arms: A base 6ab8b5da26; B this change's form 69228c0b92; E B plus the narrowed trailing barrier 6a5afdbafb; D no copy-in barrier at all b487b7d9f1 (not correct: the bound only). GPU row A B E D RTX 3090 managed 109.4 104.2 104.0 95.2 (job 45800) RTX 3090 private 90.7 90.2 90.0 90.8 RTX 2080 managed 318.5 314.3 318.0 308.3 (job 45801) RTX 2080 private 303.4 302.6 301.4 300.6 RX 7900 XTX managed 74.3 75.2 75.9 75.0 (job 45802) RX 7900 XTX private 74.2 73.9 73.9 73.4 Iris Xe managed 2673.6 2675.9 2672.8 2680.7 (job 45803) Iris Xe private 2685.5 2692.9 2685.3 2685.9 B takes 5.2 us off the 3090's managed row and 4.2 us off the 2080's; the XTX and Iris Xe move within their run-to-run spread (XTX runs 75.6/74.8 against 73.9/74.7). The first round (job 45779, 3090) read A 109.3, B 103.9, D 95.9 the same way. --- crates/metal2vulkan-rt/README.md | 5 +++-- crates/metal2vulkan-rt/src/capture.rs | 19 ++++++++----------- crates/metal2vulkan-rt/src/tests/managed.rs | 17 +++++++++++++++-- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index fd566726e..f3b8221b5 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -185,8 +185,9 @@ in its memory. The next commit of a command buffer with commands, on any queue, takes the bytes of every range named since: on the committing thread, before `commit` returns, it copies each once into a 64 MiB staging ring of the Shared type, and the copies from the ring into the device copies go - ahead of that command buffer's commands, after every earlier command on the device. So the - bytes are taken at the commit; Metal takes them shortly after, before the command buffer is + ahead of that command buffer's commands, after every earlier command on the device, which the + barrier ending each earlier command buffer orders; the copies record one barrier, after them. So + the bytes are taken at the commit; Metal takes them shortly after, before the command buffer is scheduled. A write or a `did_modify_range` made while a command buffer runs does not reach it. Commits take turns while they copy, so command buffers keep the order of their captures. A commit that finds the ring full waits for its oldest copy's submission to complete. Where that diff --git a/crates/metal2vulkan-rt/src/capture.rs b/crates/metal2vulkan-rt/src/capture.rs index 2020e8156..c598d9ae5 100644 --- a/crates/metal2vulkan-rt/src/capture.rs +++ b/crates/metal2vulkan-rt/src/capture.rs @@ -7,8 +7,10 @@ //! the command buffers keep the order of their captures. The copies into the device copies, and //! then into the images of the textures over the ranges in images of their own //! (`crate::over::ManagedOvers`), are recorded into command buffers of their own, [`CopyIn`]s, -//! which go ahead of the command buffer's own commands in queue order, each after a barrier that -//! puts every earlier command on the queue first. So a command buffer reads the bytes as they were +//! which go ahead of the command buffer's own commands in queue order. A copy-in records no barrier +//! before its copies: the one that ends every earlier segment, upload, fill and copy-in on the queue +//! puts every earlier command first, as it does for a segment (`record::end`). So a command buffer +//! reads the bytes as they were //! at its commit, and a write after the commit reaches none committed before it. Metal takes them //! shortly after the commit, before the command buffer is scheduled, and a write or a //! `didModifyRange:` made while the command buffer runs does not reach it. @@ -207,7 +209,8 @@ pub(crate) struct Chunk { impl Chunk { /// A copy-in's command buffer, recording, after the copies back `backs` (the first of a - /// capture's), or a barrier that puts every earlier command first. + /// capture's). It begins with no barrier of its own: the one ending each earlier command + /// buffer on the queue puts every earlier command first (`record::end`). /// /// Err: [`Error::Vulkan`] where a command pool or buffer cannot be made. pub(crate) fn begin(core: &Core, backs: Vec) -> Result { @@ -223,14 +226,8 @@ impl Chunk { let copies: Vec = backs.iter().map(|(_, copy, _)| *copy).collect(); // SAFETY: the command buffer is recording, and only this thread has it; each image is in // its home layout between commands, and the copy-in holds it. - unsafe { - match copies.is_empty() { - true => { - record::barrier(device, command, Reach::Commands); - core.counters.count(Metric::Barriers); - } - false => over::record_side(device, command, &copies, false, &core.counters), - } + if !copies.is_empty() { + unsafe { over::record_side(device, command, &copies, false, &core.counters) }; } Ok(Chunk { recorder, diff --git a/crates/metal2vulkan-rt/src/tests/managed.rs b/crates/metal2vulkan-rt/src/tests/managed.rs index cab1e9524..6c2af5968 100644 --- a/crates/metal2vulkan-rt/src/tests/managed.rs +++ b/crates/metal2vulkan-rt/src/tests/managed.rs @@ -2,8 +2,8 @@ //! every device of the cfg's kind, in the form Vulkan's device type picks: with a device copy //! where the device is neither integrated nor a CPU, the copy holds the consumer's first bytes and //! zeros after them, a changed range reaches it at the next commit and a range not marked changed -//! does not, and a change submits nothing; without one, shaders read the consumer's bytes as they -//! are. A command buffer reads the bytes of its commit, not those written and named after it +//! does not, a change submits nothing, and the copy-in at a commit adds one barrier; without one, +//! shaders read the consumer's bytes as they are. A command buffer reads the bytes of its commit, not those written and named after it //! while it waits, where there is a device copy; a write after the range is named and before the //! commit is seen. Three rings' worth named with nothing held streams through the ring alone; //! more than a ring behind a command buffer held on the host takes staging of its own, which it @@ -146,10 +146,23 @@ pub(crate) mod on_device { if !copied { expected[20_000..20_100].fill(0x5A); } + let before = counters.metrics().barriers; assert!( device_bytes(&device, &managed, &into, LEN) == expected, "{name}: the bytes after the changes (device copy: {copied})" ); + let named = counters.metrics().barriers - before; + assert!( + device_bytes(&device, &managed, &into, LEN) == expected, + "{name}: the bytes with nothing named (device copy: {copied})" + ); + // The copy-in adds one barrier, after its copies: the one ending each earlier command + // buffer on the queue orders it after them. + assert_eq!( + named - (counters.metrics().barriers - before - named), + u64::from(copied), + "{name}: the barriers a copy-in adds (device copy: {copied})" + ); // A change only names its range: nothing is submitted until a commit. let before = counters.metrics().submissions;