From 6d3141e9c3ce6c57b88c348ee552387dd4d0b71c Mon Sep 17 00:00:00 2001 From: abdelrahman fahmy <104788132+Apdelrahman1911@users.noreply.github.com> Date: Sun, 6 Dec 2026 12:44:44 +0000 Subject: [PATCH 1/2] P3-4: Coalesce trailing closing barrier into segment end and elide bare opening buffer-blit barrier --- crates/metal2vulkan-runtime/src/blit.rs | 25 ++++++++-- crates/metal2vulkan-runtime/src/command.rs | 46 +++++++++++++++++-- crates/metal2vulkan-runtime/src/tests/blit.rs | 27 +++++------ crates/metal2vulkan-runtime/src/tests/draw.rs | 8 ++-- .../metal2vulkan-runtime/src/tests/mipmaps.rs | 4 +- crates/metal2vulkan-runtime/src/tests/pass.rs | 20 ++++---- .../metal2vulkan-runtime/src/tests/present.rs | 2 +- 7 files changed, 94 insertions(+), 38 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/blit.rs b/crates/metal2vulkan-runtime/src/blit.rs index 3115877d7b..40484e445d 100644 --- a/crates/metal2vulkan-runtime/src/blit.rs +++ b/crates/metal2vulkan-runtime/src/blit.rs @@ -472,6 +472,7 @@ impl BlitCommandEncoder { }; // The copy's segment marks what it does and takes the copies back owed to it, in a new // one past a signal. + let mut opens = building.opens_command_buffer(); building.open_segment()?; building.keep(source); building.keep(destination); @@ -480,10 +481,12 @@ impl BlitCommandEncoder { let mut backs = Vec::new(); building.managed_reach(source, &mut backs); building.managed_reach(destination, &mut backs); + let had_backs = !backs.is_empty(); building.managed_backs(backs)?; building.wrote_managed(destination); // A write of a heap buffer leaves the textures over its bytes stale (`crate::alias`). if building.aliased() { + opens = false; building.places( [ (source.object().placement(), Access::Read), @@ -496,6 +499,9 @@ impl BlitCommandEncoder { (Reached::Buffer(source.span()), Access::Read), (Reached::Buffer(destination.span()), Access::Write), ]; + if had_backs || building.copies_over() { + opens = false; + } building.overs_before(overs, false)?; let command = building.leading_command()?; building.overs_after(overs, false); @@ -507,7 +513,7 @@ impl BlitCommandEncoder { held.wrote(destination.shared_range(held.atom)); } let device = &core.device; - building.lead(command, &[], true, Metric::BlitBarriers); + building.lead(command, &[], !opens, Metric::BlitBarriers); // SAFETY: the command buffer records outside any render pass and keeps both buffers until // it retires; the region lies in each buffer's device range and its source and // destination share no byte (`buffer_to_buffer`). @@ -737,17 +743,23 @@ impl BlitCommandEncoder { let (offset, length) = fill_range(base, buffer.length(), range)?; // The fill's segment marks what it does and takes the copies back owed to it, in a new // one past a signal. + let mut opens = building.opens_command_buffer(); building.open_segment()?; building.keep(buffer); let mut backs = Vec::new(); building.managed_reach(buffer, &mut backs); + let had_backs = !backs.is_empty(); building.managed_backs(backs)?; building.wrote_managed(buffer); // A write of a heap buffer leaves the textures over its bytes stale (`crate::alias`). if building.aliased() { + opens = false; building.places([(buffer.object().placement(), Access::Write)].into_iter())?; } let overs = [(Reached::Buffer(buffer.span()), Access::Write)]; + if had_backs || building.copies_over() { + opens = false; + } building.overs_before(overs, false)?; let command = building.leading_command()?; building.overs_after(overs, false); @@ -759,7 +771,7 @@ impl BlitCommandEncoder { held.wrote(buffer.shared_range(held.atom)); } let device = &core.device; - building.lead(command, &[], true, Metric::BlitBarriers); + building.lead(command, &[], !opens, Metric::BlitBarriers); // SAFETY: the command buffer records outside any render pass and keeps the buffer until it // retires; the range lies in the buffer's device range, in whole words // (`fill_range`). @@ -809,9 +821,10 @@ impl BlitCommandEncoder { reset_regions(&template, buffer.layout(), buffer.records(), range); building.keep_block(buffer.records()); building.keep_block(template.block()); + let opens = building.opens_command_buffer(); let command = building.leading_command()?; let device = &core.device; - building.lead(command, &[], true, Metric::BlitBarriers); + building.lead(command, &[], !opens, Metric::BlitBarriers); // SAFETY: the command buffer records outside any render pass and keeps both blocks until // it retires; each region lies in the template and in the records (`reset_regions`). unsafe { @@ -1114,11 +1127,15 @@ impl BlitCommandEncoder { }; // The copy's segment marks what it reaches and takes the copies back owed to it, in a // new one past a signal. + let mut opens = building.opens_command_buffer(); building.open_segment()?; building.keep(buffer); // The textures over the device copy go back to it first (`crate::over`). let mut backs = Vec::new(); building.managed_reach(buffer, &mut backs); + if !backs.is_empty() { + opens = false; + } building.managed_backs(backs)?; let command = building.leading_command()?; // Marked after `leading_command`, as a copy into a Shared buffer is. The copy writes @@ -1133,7 +1150,7 @@ impl BlitCommandEncoder { )); } let device = &core.device; - building.lead(command, &[], true, Metric::BlitBarriers); + building.lead(command, &[], !opens, Metric::BlitBarriers); // SAFETY: the command buffer records outside any render pass and keeps the buffer, its // block and its device copy until it retires; the copy's place and the buffer's range // of its block are each `length` bytes, and do not overlap. diff --git a/crates/metal2vulkan-runtime/src/command.rs b/crates/metal2vulkan-runtime/src/command.rs index 153fa69d28..99a1a6db9a 100644 --- a/crates/metal2vulkan-runtime/src/command.rs +++ b/crates/metal2vulkan-runtime/src/command.rs @@ -1274,19 +1274,27 @@ impl Building { /// Ends the last segment's commands, if it has any, `moves` in its end barrier and `end`, the /// command buffer's end timestamp where it is the last segment, after that barrier. + /// + /// When no texture-over-buffer copy-backs are owed (`back.is_empty()`), any deferred closing + /// barrier left by a trailing blit or render pass (`self.closing`) coalesces directly into + /// `record::end()`'s single `ALL_COMMANDS` barrier via `joined()` instead of emitting a + /// back-to-back closing barrier followed immediately by a segment-end barrier. fn close( &mut self, - moves: &[vk::ImageMemoryBarrier2<'_>], + moves: &[vk::ImageMemoryBarrier2<'static>], end: Option, ) -> Result<(), Error> { let core = Arc::clone(self.device.core()); match self.last().command { Some(command) => { - // The end barrier the last blit left, before the copies back and the end barrier. - self.settle(); // The images commands wrote go back to their buffers first (`crate::over`). let back = self.last().overs.end(); + let mut use_lead_images = false; + let mut use_closing_back = false; if !back.is_empty() { + // The end barrier the last blit or render pass left must run before the + // copies back so each image is in its home layout. + self.settle(); // SAFETY: the command buffer records outside any render pass and keeps each // texture, which holds its buffer, until it retires; each image is in its // home layout between commands. @@ -1301,6 +1309,29 @@ impl Building { ) }; self.host_written |= back.iter().any(|copy| copy.shared); + } else if let Some((closing, kind)) = self.closing.take() { + let mut joined_closing = false; + if closing == command { + if self.closing_back.is_empty() { + joined_closing = true; + } else if moves.is_empty() { + joined_closing = true; + use_closing_back = true; + } else if joined(&self.closing_back, moves, &mut self.lead_images) { + joined_closing = true; + use_lead_images = true; + } + } + if !joined_closing { + self.closing = Some((closing, kind)); + self.settle(); + } + // A blit of an image whose closing moves coalesced into `record::end()` + // still closed with one global memory barrier. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + if joined_closing && kind == ClosingKind::Blit && !self.closing_back.is_empty() { + self.device.core().counters.closed(1); + } } // Its end barrier pays what is owed and starts the hazard sets again. self.owed = None; @@ -1313,7 +1344,14 @@ impl Building { // completes, and may read after an event the segment signals. let host = self.host_written || !self.last().signals.is_empty(); self.host_written = false; - record::end(&core.device, command, host, moves, end, &core.counters) + let end_images = if use_lead_images { + self.lead_images.as_slice() + } else if use_closing_back { + self.closing_back.as_slice() + } else { + moves + }; + record::end(&core.device, command, host, end_images, end, &core.counters) } None => Ok(()), } diff --git a/crates/metal2vulkan-runtime/src/tests/blit.rs b/crates/metal2vulkan-runtime/src/tests/blit.rs index c21547e46e..f6579fd4a8 100644 --- a/crates/metal2vulkan-runtime/src/tests/blit.rs +++ b/crates/metal2vulkan-runtime/src/tests/blit.rs @@ -3550,8 +3550,8 @@ pub(crate) mod on_device { closed.0 - closings.0, closed.1 - closings.1, ), - (5, 1, 6, 4, 4), - "{name}: four quadrant uploads coalesce 8 -> 5 blit barriers" + (4, 1, 5, 4, 4), + "{name}: four quadrant uploads coalesce 8 -> 4 blit barriers (trailing joins segment end)" ); let mut want = vec![0u8; 16 * 16 * 4]; for (i, origin) in origins.into_iter().enumerate() { @@ -3597,13 +3597,14 @@ pub(crate) mod on_device { closed.0 - closings.0, closed.1 - closings.1, ), - (3, 1, 4, 2, 2), - "{name}: upload + copy_from_texture coalesce 4 -> 3 blit barriers" + (2, 1, 3, 2, 2), + "{name}: upload + copy_from_texture coalesce 4 -> 2 blit barriers (trailing joins segment end)" ); assert_eq!(dst_level.read(), full, "{name}: chained texture copy"); // 3. Chained buffer-only blits (`fill_buffer` -> `copy` -> `copy`): - // 3 blits -> 4 blit barriers (down from 6) and 5 total barriers. + // opening fill skips bare leading barrier; trailing copy coalesces into segment end + // => 2 inter-blit barriers + 1 segment end = 3 total barriers (down from 6+1). settled(&device); let before = counters.metrics(); let closings = counters.closings(); @@ -3630,8 +3631,8 @@ pub(crate) mod on_device { closed.0 - closings.0, closed.1 - closings.1, ), - (4, 1, 5, 0, 0), - "{name}: three buffer blits coalesce 6 -> 4 blit barriers" + (2, 1, 3, 0, 0), + "{name}: three buffer blits coalesce 6 -> 2 blit barriers" ); assert_eq!(read(&b2), vec![0x3c; 256], "{name}: chained buffer blits"); }); @@ -3725,8 +3726,8 @@ pub(crate) mod on_device { closed.0 - closings.0, closed.1 - closings.1, ), - (1, 1, 1, 1, 4, 1, 1), - "{name}: blit into target + load pass coalesce 5 -> 4 barriers" + (1, 1, 0, 1, 3, 1, 1), + "{name}: blit into target + load pass coalesce 5 -> 3 barriers" ); assert_eq!( target_level.read(), @@ -3763,8 +3764,8 @@ pub(crate) mod on_device { after.segment_end_barriers - before.segment_end_barriers, after.barriers - before.barriers, ), - (1, 1, 1, 1, 4), - "{name}: blit into atlas + clear pass coalesce 5 -> 4 barriers" + (1, 1, 0, 1, 3), + "{name}: blit into atlas + clear pass coalesce 5 -> 3 barriers" ); assert_eq!(atlas_level.read(), atlas_bytes, "{name}: atlas uploaded"); let green: Vec = [0u8, 255, 0, 255].repeat(64); @@ -3799,8 +3800,8 @@ pub(crate) mod on_device { after.segment_end_barriers - before.segment_end_barriers, after.barriers - before.barriers, ), - (2, 1, 1, 2, 6), - "{name}: signal between blit and pass settles blit closing in segment 1" + (1, 1, 0, 2, 4), + "{name}: signal between blit and pass coalesces each segment's trailing barrier into its segment end" ); assert_eq!(atlas_level.read(), split_bytes, "{name}: split atlas"); let blue: Vec = [0u8, 0, 255, 255].repeat(64); diff --git a/crates/metal2vulkan-runtime/src/tests/draw.rs b/crates/metal2vulkan-runtime/src/tests/draw.rs index 181f934d38..90571e46ca 100644 --- a/crates/metal2vulkan-runtime/src/tests/draw.rs +++ b/crates/metal2vulkan-runtime/src/tests/draw.rs @@ -2888,11 +2888,11 @@ pub(crate) mod on_device { let draw = |result: Result<(), Error>| result.unwrap_or_else(|e| panic!("{name}: {e}")); let counters = device.counters(); // Both new single-2D attachments are fully cleared by the first pass (`LoadAction::Clear`), - // so `WorkState::close` elides the eager `Upload::home` clears and upload barriers: both - // passes record the same 3 barriers (pass start, pass home, segment end). + // so `WorkState::close` elides the eager `Upload::home` clears and upload barriers, and + // trailing pass home coalesces into `record::end`: 2 barriers (pass start, segment end). for (load, first, color_of, barriers) in [ - (crate::LoadAction::Clear, 3, BLUE, 3), - (crate::LoadAction::Load, 6, WHITE, 3), + (crate::LoadAction::Clear, 3, BLUE, 2), + (crate::LoadAction::Load, 6, WHITE, 2), ] { settled(&device); let before = counters.metrics(); diff --git a/crates/metal2vulkan-runtime/src/tests/mipmaps.rs b/crates/metal2vulkan-runtime/src/tests/mipmaps.rs index f8c1c03e24..f2fcaf7039 100644 --- a/crates/metal2vulkan-runtime/src/tests/mipmaps.rs +++ b/crates/metal2vulkan-runtime/src/tests/mipmaps.rs @@ -132,8 +132,8 @@ mod on_device { after.submissions - before.submissions, after.barriers - before.barriers ), - (1, u64::from(LEVELS) + 1), - "{name}: submissions, barriers (the chain's and the segment's end)" + (1, u64::from(LEVELS)), + "{name}: submissions, barriers (the chain's leading/inter-level and coalesced segment end)" ); assert_eq!(level_bytes(name, &mipmapped, 0), first, "{name}: level 0"); let mut worst = 0.0f32; diff --git a/crates/metal2vulkan-runtime/src/tests/pass.rs b/crates/metal2vulkan-runtime/src/tests/pass.rs index 1eacc7b79f..e6ebe5daec 100644 --- a/crates/metal2vulkan-runtime/src/tests/pass.rs +++ b/crates/metal2vulkan-runtime/src/tests/pass.rs @@ -424,7 +424,7 @@ mod on_device { after.bytes_copied - before.bytes_copied, after.passes - before.passes, ), - (1, 3, 0, 1), + (1, 2, 0, 1), "{name}: submissions, barriers, bytes copied, passes" ); // `colors` (2 mips) and `stencil` (no sampled view) allocate attachment views; @@ -485,7 +485,7 @@ mod on_device { after.barriers - before.barriers, after.passes - before.passes, ), - (1, 4, 2), + (1, 3, 2), "{name}: submissions, barriers, passes" ); assert_eq!( @@ -1032,8 +1032,8 @@ mod on_device { after.barriers - before.barriers, after.passes - before.passes, ), - (4, 1, 1, 6, 4), - "{name}: four passes on same target coalesce 9 -> 6 barriers" + (4, 0, 1, 5, 4), + "{name}: four passes on same target coalesce 9 -> 5 barriers" ); assert_eq!( read(&a, 0, 0, 4), @@ -1123,8 +1123,8 @@ mod on_device { after.segment_end_barriers - before.segment_end_barriers, after.barriers - before.barriers, ), - (2, 2, 2, 6), - "{name}: signal between passes settles pass 1 home in segment 1" + (2, 0, 2, 4), + "{name}: signal between passes coalesces each segment's trailing pass home into its segment end" ); assert_eq!( read(&a, 0, 0, 4), @@ -1223,8 +1223,8 @@ mod on_device { closed.0 - closings.0, closed.1 - closings.1, ), - (1, 0, 2, 1, 4, 1, 1), - "{name}: render pass + copy_from_texture coalesce 5 -> 4 barriers" + (1, 0, 1, 1, 3, 1, 1), + "{name}: render pass + copy_from_texture coalesce 5 -> 3 barriers" ); assert_eq!( read(&b, 0, 0, 4), @@ -1269,8 +1269,8 @@ mod on_device { closed.0 - closings.0, closed.1 - closings.1, ), - (2, 0, 3, 1, 6, 2, 2), - "{name}: pass -> blit -> pass -> blit coalesces 8 -> 6 barriers" + (2, 0, 2, 1, 5, 2, 2), + "{name}: pass -> blit -> pass -> blit coalesces 8 -> 5 barriers" ); let contents = out.contents().expect("a Shared buffer"); // SAFETY: the command buffer completed and `out` is 256 bytes of Shared memory. diff --git a/crates/metal2vulkan-runtime/src/tests/present.rs b/crates/metal2vulkan-runtime/src/tests/present.rs index b49379c465..f3950d3118 100644 --- a/crates/metal2vulkan-runtime/src/tests/present.rs +++ b/crates/metal2vulkan-runtime/src/tests/present.rs @@ -426,7 +426,7 @@ mod on_device { let third_texels = [102u8, 0, 255, 255].repeat(32); for (usage, index, barriers, kind) in [ (TextureUsage::RENDER_TARGET, 4097, 3, "render-target"), - (read_render, 4096, 5, "shader-read"), + (read_render, 4096, 4, "shader-read"), ] { let index = Some(index); let info = swapchain(USAGE); From 00c7784c0eafe155839734059cf95d2c79cf427b Mon Sep 17 00:00:00 2001 From: abdelrahman fahmy <104788132+Apdelrahman1911@users.noreply.github.com> Date: Sun, 6 Dec 2026 12:51:19 +0000 Subject: [PATCH 2/2] Update managed-buffer synchronize and layouts barrier assertions for P3-4 savings --- crates/metal2vulkan-runtime/src/tests/blit.rs | 2 +- crates/metal2vulkan-runtime/src/tests/layouts.rs | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/tests/blit.rs b/crates/metal2vulkan-runtime/src/tests/blit.rs index f6579fd4a8..3d3360364e 100644 --- a/crates/metal2vulkan-runtime/src/tests/blit.rs +++ b/crates/metal2vulkan-runtime/src/tests/blit.rs @@ -3247,7 +3247,7 @@ pub(crate) mod on_device { after.synchronizations - metrics.synchronizations, counters.host_reaches() - reaches, ), - (1, if copied { 4 } else { 3 }, 0, 1, 1), + (1, if copied { 2 } else { 1 }, 0, 1, 1), "{name}: submissions, barriers, bytes copied, synchronizations, host reaches \ (device copy: {copied})" ); diff --git a/crates/metal2vulkan-runtime/src/tests/layouts.rs b/crates/metal2vulkan-runtime/src/tests/layouts.rs index 5c89f0e684..0eb9ac2aec 100644 --- a/crates/metal2vulkan-runtime/src/tests/layouts.rs +++ b/crates/metal2vulkan-runtime/src/tests/layouts.rs @@ -1583,10 +1583,11 @@ mod on_device { let after = (device.counters().metrics(), barriers(handle)); let recorded = after.1 - before.1; // At least: a move home, two for the replace, an upload's end, one for the blit (its - // closing barrier joined the pass's start), two for the pass, the segment's end, two - // for the read and its upload's end. + // closing barrier joined the pass's start), one for the pass start (its home barrier + // coalesced into the segment's end), the segment's end, two for the read and its + // upload's end. assert!( - recorded >= 11, + recorded >= 10, "{name}: {recorded} barrier commands recorded" ); assert_eq!( @@ -1607,7 +1608,7 @@ mod on_device { assert!(uploads >= 6, "{name}: {uploads} upload barriers"); assert_eq!( grown, - [1, 1, 1, 0, 1, uploads, 0], + [1, 0, 1, 0, 1, uploads, 0], "{name}: pass start, pass home, blit, dispatch hazard, segment end, upload, other" ); println!(