Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions crates/metal2vulkan-runtime/src/blit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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),
Expand All @@ -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);
Expand All @@ -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`).
Expand Down Expand Up @@ -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);
Expand All @@ -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`).
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
46 changes: 42 additions & 4 deletions crates/metal2vulkan-runtime/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pair>,
) -> 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.
Expand All @@ -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;
Expand All @@ -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(()),
}
Expand Down
29 changes: 15 additions & 14 deletions crates/metal2vulkan-runtime/src/tests/blit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
);
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand All @@ -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");
});
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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<u8> = [0u8, 255, 0, 255].repeat(64);
Expand Down Expand Up @@ -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<u8> = [0u8, 0, 255, 255].repeat(64);
Expand Down
8 changes: 4 additions & 4 deletions crates/metal2vulkan-runtime/src/tests/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 5 additions & 4 deletions crates/metal2vulkan-runtime/src/tests/layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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!(
Expand Down
4 changes: 2 additions & 2 deletions crates/metal2vulkan-runtime/src/tests/mipmaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions crates/metal2vulkan-runtime/src/tests/pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion crates/metal2vulkan-runtime/src/tests/present.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading