From 212d7b2d8e63aa6935dff04e7a0f935d8010cdf7 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 2 Dec 2026 08:42:26 -0800 Subject: [PATCH] rt: blit copies whose rows meet go a row at a time where no one axis orders them, and a surface's plane lies in its buffer Row L212. Metal leaves overlapping copies undefined; an Apple M3 Max reads the source whole before it writes, which the runtime gives as slabs, and no Vulkan copy command may read bytes it writes (VUID-vkCmdCopyImage-pRegions-00124, VUID-vkCmdCopyBufferToImage-pRegions-00173, VUID-vkCmdCopyImageToBuffer-pRegions-00184). Form (blit.rs): - Slabs becomes an enum: Across (the existing single-axis cut) and Rows (RowSlabs). Where cut() finds no axis, row_by_row() takes the rows one at a time. Each row goes before every row whose source its destination lies on: descending where every destination row lies on a source row after its own (meeting()'s y shifts all positive), ascending where all are negative. - Within a row, the shift from the destination row to its own source row (the first row's, plus the pitch difference per row) decides the cut. Zero: the row is its own bytes and copies nothing. Less than the row: slabs of |shift| in whole texels, the far end first. Otherwise: one command. - Slabs::of yields each slab's 3D start, which ImageCopy::regions and BufferCopy::slabs add to the other side's origin and to the buffer offset. - An execution barrier still separates two commands. Every hazard between them is a write after a read, as no row reads bytes an earlier row wrote. - Still refused (Gap::OverlappingCopy): rows of two pitches that cross, where one destination row lies on a source row before its own and another on one after its own. Surface (texture.rs, buffer.rs, surface.rs, d3d11.rs): - An exported surface's plane image keeps its PlaneLayout (Under::Surface laid, from SurfaceImage). - TextureObject::rows_in(buffer) answers the plane's rows in a buffer on the surface's memory: BufferObject::surface_range matches the buffer's block aliases to the plane's (Arc::ptr_eq) and maps the plane's offset through the block's memory range. - A copy between a buffer and a texture takes those rows where the texture has none of its own. A texture over the surface's buffer is always the copied form (OverCause::Surface), so a copy between it and a plane shares no bytes in Vulkan, and is left as it was. - The surface's buffer and planes are aliases that read alike in the general layout. Surface::buffer's doc quotes the section. Docs: - blit.rs and the copy methods' docs; error.rs's OverlappingCopy (REFUSALS.md regenerated). - The runtime README: Blit encoders, their known gaps (only crossing pitches remain refused), and the surface's buffer. Verified: - Green, on lavapipe, m3-max-78902 (at 70c887a155, before the rebase): the blit module, 26 of 26 passed (865 skipped). The plane is in place (OpaqueFd); into it and out of it go a row at a time. - Cards, the blit module at 02421a08a9, 26 of 26 passed (867 skipped) on each. That tree differs from this one only by a branch for a texture in place over a surface's buffer, now deleted: no device laid one in place, and each printed "copied". - Radeon 780M (RADV, Mesa 26.2.6), m3-max-78888. - Iris Xe (ANV, Mesa 26.2.6), m3-max-78889. - RTX 3090 (NVIDIA 620.70.06), m3-max-78896. On each, the textures over one buffer are in place: whole rows go a row at a time (16 slabs; 256 on the Iris, whose memory alignment is 4), and two pitches go a row at a time. The surface's plane is in place (OpaqueFd), into it and out of it a row at a time. - m3-max-78906 at 70c887a155: workspace clippy (all targets, all features, -D warnings) EXIT=0 in all three configurations (plain, --cfg m2v_cpu_icd, --cfg m2v_gpu). Rustdoc and the runtime's private Rustdoc EXIT=0. The runtime suite on lavapipe: 888 tests run, 888 passed, 3 skipped. - refusal_surface.py --check: 0 at this commit. --- crates/metal2vulkan-runtime/README.md | 33 ++- crates/metal2vulkan-runtime/src/blit.rs | 226 +++++++++++++----- crates/metal2vulkan-runtime/src/buffer.rs | 13 + crates/metal2vulkan-runtime/src/d3d11.rs | 1 + crates/metal2vulkan-runtime/src/error.rs | 17 +- crates/metal2vulkan-runtime/src/surface.rs | 37 +-- crates/metal2vulkan-runtime/src/tests/blit.rs | 10 +- crates/metal2vulkan-runtime/src/texture.rs | 30 +++ docs/REFUSALS.md | 2 +- 9 files changed, 270 insertions(+), 99 deletions(-) diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index f4cacae24..1ae378f15 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -1075,8 +1075,9 @@ import across instances and APIs, and rt takes imports in one instance as those planes already do so, each on an import of its own. The surface, its planes and their views, and its buffer are one resource in rt's ordering of reads and writes, as a heap's resources are. A kernel's write through the buffer is ordered before the next kernel's read through a plane, by -the global memory barrier rt records between them. `Surface::buffer`'s doc comment quotes the -sections. +the global memory barrier rt records between them. A blit copy between the buffer and a plane +takes the plane's rows at their place in the buffer, and copies as from the source before the +copy where they meet the region's (Blit encoders). `Surface::buffer`'s doc comment quotes the sections. On Windows, `Device::import_d3d11_surface(&[D3d11Plane])` takes a picture's planes the other API made as Direct3D 11 textures, each by its NT handle (`IDXGIResource1::CreateSharedHandle`), its @@ -3074,8 +3075,15 @@ while the driver's exit handler waited on a lock, with every runtime thread alre that from a destination row to a source row on its bytes, by the row pitch the device reports for the linear images: in rows along y, in whole texels along x. Regions on the same bytes copy nothing. Where the rows meet shifted both ways, or not shifted, along each axis (a destination - row across the end of its own source row and the start of the next), no cut along one axis - keeps the order, and the copy is `Gap::OverlappingCopy` (below). + row across the end of its own source row and the start of the next, or rows of two pitches), + no cut along one axis keeps the order, and the rows go one at a time: each before every row + whose source its destination lies on, the last row first where each destination row lies on a + source row after its own, else the first. A row whose destination meets its own source is cut + across in slabs no wider than the shift between the two, the far end first; a row on its own + bytes copies nothing. Each row's slab is one `vkCmdCopyImage`, with an execution barrier between + two. Where one destination row lies on a source row before its own and another on one after + its own (rows of two pitches that cross), no such order is taken, and the copy is + `Gap::OverlappingCopy` (below). - Two textures whose memory meets where one is placed in a heap (`Device::new_heap_texture`), which Metal leaves undefined as it does any aliased heap resources' contents: the device lays that texture's texels out as it chooses, so no copy command is known to read no byte it writes, @@ -3112,8 +3120,11 @@ while the driver's exit handler waited on a lock, with every runtime thread alre destination row to a source row on its bytes, the region's rows its `bytes_per_row` apart and the texture's at the row pitch the device reports, as from the source before the copy, an execution barrier between two slabs. The region on the texture's own texels copies nothing. - Where the rows meet in no order a cut along one axis keeps, and where the region's bytes meet - the memory of a texture placed in a heap, the copy is `Gap::OverlappingCopy` (below). Metal + Where the rows meet in no order a cut along one axis keeps, they go one at a time, as between + two textures over one buffer. An exported surface's buffer (`Surface::buffer`) and its own + plane in place are a buffer and a texture in place over it so. Where rows of two pitches cross, + and where the region's bytes meet the memory of a texture placed in a heap, the copy is + `Gap::OverlappingCopy` (below). Metal leaves such a copy undefined; the answer between a buffer and its texture was not recorded on a Mac, and is the one an Apple M3 Max gives between two textures over one buffer. - Refused (`Refusal::BufferCopyLayout`), as Metal's validation stops them: an offset or row stride @@ -3177,11 +3188,11 @@ while the driver's exit handler waited on a lock, with every runtime thread alre built (`Gap::OverlappingCopy`), and each refusal counts in `Metrics::overlapping_copies_refused`: no Vulkan copy command reads bytes it writes (`VUID-vkCmdCopyImage-pRegions-00124`, `VUID-vkCmdCopyBufferToImage-pRegions-00173`, - `VUID-vkCmdCopyImageToBuffer-pRegions-00184`). Two shapes of rows in place over one buffer, - between two textures or a buffer and a texture, are cut along no one axis: a destination row - across the end of its own source row and the start of the next, and rows of two pitches that - meet at their starts. Each is built, as a cut on two levels (rows, then the texels within - them), when a replay stream or a walk counts one. + `VUID-vkCmdCopyImageToBuffer-pRegions-00184`). Rows in place over one buffer, between two + textures or a buffer and a texture, that neither a cut along one axis nor the rows one at a + time from one end order are refused: rows of two pitches that cross, one destination row on a + source row before its own and another on one after its own. An order of rows from where they + cross is built when a replay stream or a walk counts one. - A copy between a texture placed in a heap and a texture or a buffer whose memory meets its range is refused alike (`Gap::OverlappingCopy`): the device lays the heap texture's texels out as it chooses, and Metal leaves the contents of aliased heap resources undefined, so nothing is diff --git a/crates/metal2vulkan-runtime/src/blit.rs b/crates/metal2vulkan-runtime/src/blit.rs index 8dd00dd4e..7763c3447 100644 --- a/crates/metal2vulkan-runtime/src/blit.rs +++ b/crates/metal2vulkan-runtime/src/blit.rs @@ -8,9 +8,10 @@ //! copy's writes are visible to every later command through any resource over the same bytes (a //! heap's aliases), not through the image alone. Nothing is copied but the region Metal names. //! Regions whose bytes meet, of one level and slice or of two textures in place over one buffer, -//! are the one exception to one command: slabs, one `vkCmdCopyImage` each, an execution barrier -//! between two ([`Slabs`]). Memory a texture placed in a heap shares with the other side is in no -//! order a copy command keeps (`Gap::OverlappingCopy`). +//! are the one exception to one command: slabs along one axis, or rows one at a time each cut +//! across, one `vkCmdCopyImage` each, an execution barrier between two ([`Slabs`]). Memory a +//! texture placed in a heap shares with the other side is in no order a copy command keeps +//! (`Gap::OverlappingCopy`). //! //! A copy between a buffer and a texture is the same: `vkCmdCopyBufferToImage` or //! `vkCmdCopyImageToBuffer` against the bytes shaders reach (the buffer's range of its block, or a @@ -179,14 +180,20 @@ impl BlitCommandEncoder { /// row to each source row on its bytes: in rows along y, in whole texels along x), so that its /// two regions share no byte, one `vkCmdCopyImage` a slab and the far end of the shift first, /// an execution barrier between two slabs. The axis is the one of fewest slabs; a shift of one - /// texel takes one slab a texel. Two textures over one buffer whose regions are the same bytes - /// copy nothing. The command buffer keeps both textures until it retires. + /// texel takes one slab a texel. Rows of two textures over one buffer that no cut along one + /// axis orders (a destination row across the end of its own source row and the start of the + /// next, or rows of two pitches) go one at a time, each before every row whose source its + /// destination lies on, and a row that meets its own source is cut across as above. Two + /// textures over one buffer whose regions are the same bytes copy nothing, and so does a row + /// on its own bytes. The command buffer keeps both textures until it retires. /// /// Err: [`Refusal::OtherDevice`] for another device's texture; [`Refusal::TextureRegion`] for /// a level, slice or region outside either texture or off its format's block grid; /// [`Refusal::TextureCopyMismatch`]; [`Gap::TextureCopy`]; [`Gap::OverlappingCopy`], counted, - /// for regions of two textures over one buffer whose bytes meet where no axis cuts them so, - /// and for two textures whose memory meets where one is placed in a heap + /// for regions of two textures over one buffer whose rows meet where neither a cut along one + /// axis nor rows one at a time order them (one destination row on a source row before its own + /// and another on one after its own), and for two textures whose memory meets where one is + /// placed in a heap /// ([`Device::new_heap_texture`](crate::Device::new_heap_texture)); [`Error::Vulkan`] where the /// command buffer cannot record. pub fn copy_from_texture( @@ -328,16 +335,20 @@ impl BlitCommandEncoder { /// those shaders reach: its device copy, where the device keeps one. A texture in place over /// those bytes ([`Buffer::new_texture`]) whose rows meet the region's, which Metal leaves /// undefined, copies as [`copy_from_texture`](Self::copy_from_texture) copies two textures over - /// one buffer whose bytes meet: in slabs, as from the source before the copy; the region on - /// its own texels copies nothing. Into a 32-bit float depth it carries + /// one buffer whose bytes meet: in slabs, or rows one at a time, as from the source before the + /// copy; the region on its own texels copies nothing. An exported surface's plane in place + /// ([`Device::new_exported_surface`](crate::Device::new_exported_surface)) lies in its + /// surface's buffer's bytes ([`Surface::buffer`](crate::Surface::buffer)) so. Into a 32-bit + /// float depth it carries /// [`Texture::FLOAT_DEPTH_UPLOAD_NOTE`]. /// /// Err: [`Refusal::OtherDevice`] for another device's buffer or texture; /// [`Refusal::TextureRegion`]; [`Refusal::BufferCopyLayout`] for a layout Metal's validation /// stops; [`Gap::BufferCopyAlignment`] for one no copy command in the floor addresses; - /// [`Gap::TextureBytes`]; [`Gap::OverlappingCopy`], counted, for - /// rows of a texture in place that meet the region's where no axis cuts them so, and for a - /// texture placed in a heap whose memory meets the region's bytes; [`Error::Vulkan`] where the + /// [`Gap::TextureBytes`]; [`Gap::OverlappingCopy`], counted, for rows of a texture in place + /// that meet the region's where neither a cut along one axis nor rows one at a time order + /// them, and for a texture placed in a heap whose memory meets the region's bytes; + /// [`Error::Vulkan`] where the /// command buffer cannot record. pub fn copy_from_buffer( &mut self, @@ -549,7 +560,10 @@ impl BlitCommandEncoder { len: buffer.length(), layout, }; - let side = Side::of(texture).with_option(option)?; + let mut side = Side::of(texture).with_option(option)?; + // An exported surface's plane in place lies in the surface's buffer's bytes + // (`Surface::buffer`). + side.rows = side.rows.or_else(|| texture.rows_in(buffer)); let Some(copy) = buffer_copy(&side, at, size, lies, into_image)? else { return Ok(()); }; @@ -1325,7 +1339,9 @@ impl ImageCopy { self.slabs.into_iter().flat_map(move |slabs| { slabs.of(self.from).map(move |(start, from)| { let mut to = self.to; - to.origin[slabs.axis] += start; + for (origin, start) in to.origin.iter_mut().zip(start) { + *origin += start; + } vk::ImageCopy { src_subresource: from.layers(self.aspect), src_offset: from.offset(), @@ -1338,42 +1354,110 @@ impl ImageCopy { } } -/// The slabs a copy between textures runs as, one copy command each: the region cut across -/// `axis` into slabs `thickness` texels thick (the last what is left), from the far end of the -/// axis first where `reversed`. Regions that share no byte copy as one slab. Regions that do are -/// cut ([`cut`]) no thicker than the least shift between them along an axis every shift points -/// the same way along, so that each slab's two regions share no byte, on the axis of fewest -/// slabs; and the slab at the end the shifts point to goes first, so that no slab reads a byte -/// an earlier one wrote. The copy so gives what reading its source whole before writing gives, -/// as `memmove` does. +/// The slabs a copy between textures runs as, one copy command each. Regions that share no byte +/// copy as one slab. Regions that do are cut ([`cut`]) no thicker than the least shift between +/// them along an axis every shift points the same way along, so that each slab's two regions +/// share no byte, on the axis of fewest slabs; and the slab at the end the shifts point to goes +/// first, so that no slab reads a byte an earlier one wrote. Rows on one buffer that no such cut +/// orders go one at a time ([`RowSlabs`]). The copy so gives what reading its source whole before +/// writing gives, as `memmove` does. #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) struct Slabs { - pub(crate) axis: usize, - pub(crate) thickness: u32, - pub(crate) reversed: bool, +pub(crate) enum Slabs { + /// The region cut across `axis` into slabs `thickness` texels thick (the last what is left), + /// from the far end of the axis first where `reversed`. + Across { + axis: usize, + thickness: u32, + reversed: bool, + }, + /// The region's rows one at a time, each cut across as its own shift asks. + Rows(RowSlabs), +} + +/// Rows of two regions on one buffer whose bytes meet in no order a cut along one axis keeps +/// ([`row_by_row`]): a destination row across the end of its own source row and the start of the +/// next, or rows of two pitches. Each row goes before every row whose source its destination +/// lies on: the last row first where each destination row lies on a source row after its own +/// (`reversed`), else the first. A row whose destination is its own source's bytes copies +/// nothing; one whose destination meets its own source is cut across in slabs no wider than the +/// shift between the two in whole texels, the far end of the shift first, as [`Slabs::Across`] +/// cuts; any other row is one slab. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct RowSlabs { + reversed: bool, + /// The first destination row's first byte less the first source row's. + shift: i128, + /// What each row on adds to that shift: the destination's pitch less the source's. + step: i128, + /// A texel's bytes. + texel: u64, +} + +impl RowSlabs { + /// How row `k` of a region `width` texels wide is cut across: the slabs' thickness in texels + /// and whether the far end goes first; `None` for a row whose destination is its own source's + /// bytes. A thickness of 0 is a shift of less than a texel, which [`row_by_row`] refuses. + fn across(self, k: u32, width: u32) -> Option<(u32, bool)> { + let shift = self.shift + i128::from(k) * self.step; + let row = i128::from(width) * i128::from(self.texel); + match shift { + 0 => None, + _ if shift.abs() >= row => Some((width, false)), + _ => { + let thickness = shift.unsigned_abs() / u128::from(self.texel); + Some((u32::try_from(thickness).unwrap_or(width), shift > 0)) + } + } + } } impl Slabs { - /// `placed` cut into its slabs, in the order they run: each one's first texel along the axis - /// from the region's, and its region. - fn of(self, placed: Placed) -> impl Iterator { - let Slabs { - axis, - thickness, - reversed, - } = self; - let extent = placed.size[axis]; - let count = extent.div_ceil(thickness); - (0..count).map(move |n| { - let start = thickness - * match reversed { - true => count - 1 - n, - false => n, - }; - let mut slab = placed; - slab.origin[axis] += start; - slab.size[axis] = thickness.min(extent - start); - (start, slab) + /// `placed` cut into its slabs, in the order they run: each one's first texel from the + /// region's, and its region. + fn of(self, placed: Placed) -> impl Iterator { + let (parts, last_first) = match self { + Slabs::Across { .. } => (1, false), + Slabs::Rows(rows) => (placed.size[1], rows.reversed), + }; + (0..parts).flat_map(move |n| { + let (base, part, across) = match self { + Slabs::Across { + axis, + thickness, + reversed, + } => ([0; 3], placed, Some((axis, thickness, reversed))), + Slabs::Rows(rows) => { + let k = match last_first { + true => parts - 1 - n, + false => n, + }; + let mut row = placed; + row.origin[1] += k; + row.size[1] = 1; + let across = rows.across(k, placed.size[0]); + let across = across.map(|(thickness, reversed)| (0, thickness, reversed)); + ([0, k, 0], row, across) + } + }; + let (axis, thickness, reversed) = across.unwrap_or((0, 1, false)); + let extent = part.size[axis]; + let count = match across { + Some(_) => extent.div_ceil(thickness), + None => 0, + }; + (0..count).map(move |m| { + let start = thickness + * match reversed { + true => count - 1 - m, + false => m, + }; + let mut slab = part; + slab.origin[axis] += start; + slab.size[axis] = thickness.min(extent - start); + let mut at = base; + at[axis] += start; + (at, slab) + }) }) } } @@ -1495,7 +1579,10 @@ impl BufferCopy { ]; self.slabs.into_iter().flat_map(move |slabs| { slabs.of(self.placed).map(move |(start, placed)| { - let offset = self.offset + u64::from(start) * step[slabs.axis]; + let offset = (start.iter().zip(step)) + .fold(self.offset, |offset, (&start, step)| { + offset + u64::from(start) * step + }); let [x, y, z] = placed.origin; let [width, height, depth] = placed.size; (0..depth).map(move |image| vk::BufferImageCopy { @@ -1660,6 +1747,7 @@ pub(crate) fn buffer_copy( [u128::from(bytes), 1, 1], placed.size, ) + .or_else(|| row_by_row(from, to, placed.size, bytes)) } (_, Some(bound)) if bound.meets(region) => None, _ => cut(None, [1; 3], placed.size), @@ -1788,13 +1876,10 @@ pub(crate) fn texture_copy( // A texture in place is of one color texel an element (`Buffer::new_texture`), a unit. (Some(a), Some(b), Some(unit)) if a.buffer == b.buffer => { let texel = u64::from(unit.bytes); - let rows = meeting( - first(a, &from, texel), - first(b, &to, texel), - from.size, - texel, - ); + let (a, b) = (first(a, &from, texel), first(b, &to, texel)); + let rows = meeting(a, b, from.size, texel); cut(rows, [u128::from(texel), 1, 1], from.size) + .or_else(|| row_by_row(a, b, from.size, texel)) } // Memory one of them shares with the other where the device lays its texels out as it // chooses (a texture placed in a heap): no order. @@ -1845,7 +1930,7 @@ fn cut( } } if !any { - return Some(Some(Slabs { + return Some(Some(Slabs::Across { axis: 0, thickness: size[0], reversed: false, @@ -1858,14 +1943,39 @@ fn cut( .filter_map(|axis| { let (least, lower, higher) = along[axis]; let thickness = u32::try_from(least / units[axis]).ok()?; - (thickness > 0 && !(lower && higher)).then_some(Slabs { + (thickness > 0 && !(lower && higher)).then_some((axis, thickness, higher)) + }) + .min_by_key(|&(axis, thickness, _)| size[axis].div_ceil(thickness)) + .map(|(axis, thickness, reversed)| { + Some(Slabs::Across { axis, thickness, - reversed: higher, + reversed, }) }) - .min_by_key(|slabs| size[slabs.axis].div_ceil(slabs.thickness)) - .map(Some) +} + +/// The slabs of a copy of `size` texels of `texel` bytes between two regions of rows on one +/// buffer, from the region whose first texel is at `from` to the one at `to`, where no cut along +/// one axis orders them ([`cut`]): the rows one at a time ([`RowSlabs`]), each before every row +/// whose source its destination lies on. `None` where one destination row lies on a source row +/// before its own and another on one after its own ([`meeting`]'s shifts along y both ways), which +/// no order of whole rows taken from one end keeps, or where a row's destination lies less than a +/// texel from its own source. +fn row_by_row(from: Rows, to: Rows, size: [u32; 3], texel: u64) -> Option> { + let (mut lower, mut higher) = (false, false); + for [_, y, _] in meeting(from, to, size, texel) { + lower |= y < 0; + higher |= y > 0; + } + let rows = RowSlabs { + reversed: higher, + shift: i128::from(to.offset) - i128::from(from.offset), + step: i128::from(to.pitch) - i128::from(from.pitch), + texel, + }; + let thin = (0..size[1]).any(|k| rows.across(k, size[0]).is_some_and(|(t, _)| t == 0)); + (!(lower && higher) && !thin).then_some(Some(Slabs::Rows(rows))) } /// Of a texture whose texels of `texel` bytes lie in `rows`, where the first texel of `placed` diff --git a/crates/metal2vulkan-runtime/src/buffer.rs b/crates/metal2vulkan-runtime/src/buffer.rs index c75edca3a..470c81cfc 100644 --- a/crates/metal2vulkan-runtime/src/buffer.rs +++ b/crates/metal2vulkan-runtime/src/buffer.rs @@ -699,6 +699,19 @@ pub(crate) trait BufferObject { } } + /// Where byte `offset` of an exported surface's memory, whose resources are `aliases`, lies + /// in the `VkBuffer` shaders reach this buffer through, and its offset there, where this + /// buffer is on that memory ([`crate::Surface::buffer`]); `None` for any other buffer. + fn surface_range( + &self, + aliases: &Arc, + offset: u64, + ) -> Option<(vk::Buffer, u64)> { + let inner = self.object(); + let on = (inner.block.aliases.as_ref()).is_some_and(|own| Arc::ptr_eq(own, aliases)); + (on && inner.copy.is_none()).then(|| inner.block.memory.range(offset)) + } + /// Its own resource id, whatever heap it is in. fn id(&self) -> u32 { self.object().id diff --git a/crates/metal2vulkan-runtime/src/d3d11.rs b/crates/metal2vulkan-runtime/src/d3d11.rs index 111ec167c..3f03eb36d 100644 --- a/crates/metal2vulkan-runtime/src/d3d11.rs +++ b/crates/metal2vulkan-runtime/src/d3d11.rs @@ -450,6 +450,7 @@ impl Device { d3d11: true, family: vk::QUEUE_FAMILY_EXTERNAL, linear_attachment: false, + laid: None, }; self.surface_texture(d, plane.index, made, unit, None) } diff --git a/crates/metal2vulkan-runtime/src/error.rs b/crates/metal2vulkan-runtime/src/error.rs index 4d4c0fe86..5975de0b6 100644 --- a/crates/metal2vulkan-runtime/src/error.rs +++ b/crates/metal2vulkan-runtime/src/error.rs @@ -1315,17 +1315,18 @@ pub enum Gap { /// [`BlitCommandEncoder::copy_from_buffer`](crate::BlitCommandEncoder::copy_from_buffer) and /// [`BlitCommandEncoder::copy_from_texture_to_buffer`](crate::BlitCommandEncoder::copy_from_texture_to_buffer) /// and their options forms): rows of a texture in place over a buffer - /// ([`Buffer::new_texture`](crate::Buffer::new_texture)) that meet the other side's rows - /// shifted both ways, or not shifted, along each axis, so that no cut into slabs along one - /// axis keeps the order (a destination row across the end of its own source row and the - /// start of the next; rows of two pitches that meet at their starts); or a texture placed in - /// a heap ([`Device::new_heap_texture`](crate::Device::new_heap_texture)), whose texels the - /// device lays out as it chooses, whose memory meets the other side's. Counted in + /// ([`Buffer::new_texture`](crate::Buffer::new_texture)), or of an exported surface's plane + /// in place and its buffer, that meet the other side's rows so that neither a cut into slabs along one axis + /// nor the rows one at a time keep the order: rows of two pitches where one destination row + /// lies on a source row before its own and another on a source row after its own; or a + /// texture placed in a heap ([`Device::new_heap_texture`](crate::Device::new_heap_texture)), + /// whose texels the device lays out as it chooses, whose memory meets the other side's. + /// Counted in /// [`Metrics::overlapping_copies_refused`](crate::Metrics::overlapping_copies_refused). /// /// Basis: ours (no copy command reads bytes it writes, `VUID-vkCmdCopyImage-pRegions-00124` - /// and its twins; the first shapes are built, cut on two levels, when a replay stream or a - /// walk counts one). + /// and its twins; rows of two pitches that cross are built, in an order of rows from where + /// they cross, when a replay stream or a walk counts one). OverlappingCopy { /// The textures' pixel format. pixel_format: PixelFormat, diff --git a/crates/metal2vulkan-runtime/src/surface.rs b/crates/metal2vulkan-runtime/src/surface.rs index da304b211..55f6599fd 100644 --- a/crates/metal2vulkan-runtime/src/surface.rs +++ b/crates/metal2vulkan-runtime/src/surface.rs @@ -1153,8 +1153,14 @@ impl Device { memory_type: index, }; let source = Source::Opaque(Arc::clone(&block), spec.handle, index); - let images = (made.into_iter().zip(alone)) - .map(|(m, linear)| SurfaceImage { + let planes: Vec = (starts.iter()) + .map(|&offset| PlaneLayout { + offset, + row_pitch: pitch, + }) + .collect(); + let images = (made.into_iter().zip(alone).zip(&planes)) + .map(|((m, linear), &laid)| SurfaceImage { image: m.image, backing: Backing::InBlock, bytes: m.needs.size, @@ -1165,12 +1171,7 @@ impl Device { d3d11: false, family: spec.handle.family(), linear_attachment: linear, - }) - .collect(); - let planes = (starts.iter()) - .map(|&offset| PlaneLayout { - offset, - row_pitch: pitch, + laid: Some(laid), }) .collect(); Ok(Ok(InPlace { @@ -1445,8 +1446,15 @@ impl Device { }; free_block(); step!("export"); + let planes: Vec = (starts.iter()) + .map(|&offset| PlaneLayout { + offset, + row_pitch: pitch, + }) + .collect(); let imports = imported.into_iter().zip(sizes); - for ((m, (memory, size)), (_, _, linear)) in made.into_iter().zip(imports).zip(chosen) { + let made = made.into_iter().zip(imports).zip(chosen).zip(&planes); + for (((m, (memory, size)), (_, _, linear)), &laid) in made { images.push(SurfaceImage { image: m.image, backing: Backing::Import(memory, size), @@ -1458,6 +1466,7 @@ impl Device { d3d11: false, family: spec.handle.family(), linear_attachment: linear, + laid: Some(laid), }); } let export = Export { @@ -1465,12 +1474,6 @@ impl Device { size: len, memory_type: index, }; - let planes = (starts.iter()) - .map(|&offset| PlaneLayout { - offset, - row_pitch: pitch, - }) - .collect(); Ok(Ok(InPlace { images, export, @@ -1541,7 +1544,9 @@ impl Surface { /// **Order.** The surface, its planes (and their views) and its buffer are one resource in /// the runtime's ordering of reads and writes, as a heap's resources are: a write through the /// buffer is ordered before a later read through a plane, and the reverse, as two uses of one - /// texture are. + /// texture are. A blit copy between the buffer and a plane in place takes the plane's rows at + /// their place in the buffer + /// ([`BlitCommandEncoder::copy_from_buffer`](crate::BlitCommandEncoder::copy_from_buffer)). /// /// **Basis.** Vulkan makes the buffer and the planes aliases of the surface's memory /// (section 12.11, Memory Aliasing): "A range of a VkDeviceMemory allocation is aliased if it diff --git a/crates/metal2vulkan-runtime/src/tests/blit.rs b/crates/metal2vulkan-runtime/src/tests/blit.rs index 4ea9e16c2..3215b9a73 100644 --- a/crates/metal2vulkan-runtime/src/tests/blit.rs +++ b/crates/metal2vulkan-runtime/src/tests/blit.rs @@ -295,7 +295,7 @@ fn a_3d_image_copies_to_and_from_a_2d_slice_and_a_cube_face_is_its_slice() { let (axis, thickness, reversed) = slabs; assert_eq!( copy.slabs, - Some(Slabs { + Some(Slabs::Across { axis, thickness, reversed @@ -351,7 +351,7 @@ fn overlapping_regions_copy_in_slabs_the_far_end_of_the_shift_first() { let both = within([4, 4, 0], [8, 6, 1], [5, 2, 0]); assert_eq!( both.slabs, - Some(Slabs { + Some(Slabs::Across { axis: 1, thickness: 2, reversed: false @@ -382,7 +382,7 @@ fn regions_of_two_textures_over_one_buffer_copy_in_slabs_where_their_bytes_meet( }; let slabs = |from: &Side<'_>, to: &Side<'_>, size| copied(copy(from, to, size)).slabs; let cut = |axis, thickness, reversed| { - Some(Slabs { + Some(Slabs::Across { axis, thickness, reversed, @@ -487,7 +487,7 @@ fn textures_whose_memory_meets_where_one_is_laid_out_by_the_device_copy_in_no_or let origin = at(0, 0, [0; 3]); copied(texture_copy(from, origin, size, to, origin, false)).slabs }; - let whole = Some(Slabs { + let whole = Some(Slabs::Across { axis: 0, thickness: 16, reversed: false, @@ -916,7 +916,7 @@ fn a_buffer_copy_over_its_texture_s_own_bytes_copies_in_slabs_or_in_no_order() { |size, lies, into_image| buffer_copy(&texture, at(0, 0, [0; 3]), size, lies, into_image); let slabs = |size, lies, into_image| buffer_copied(copy(size, lies, into_image)).slabs; let cut = |axis, thickness, reversed| { - Some(Slabs { + Some(Slabs::Across { axis, thickness, reversed, diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index 0c0ae0de1..bb2b91f2f 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -59,6 +59,7 @@ use crate::shadow::{self, Area}; use crate::staging::Stage; use crate::submit::Held; use crate::ImportHandle; +use crate::PlaneLayout; use ash::vk; use metal2vulkan::device::FormatUsage; use metal2vulkan::metal::format::{self as formats, bicubic_texels_word, FormatEmulation}; @@ -733,6 +734,10 @@ enum Under { d3d11: bool, family: u32, aliases: Option>, + /// An exported surface's plane's place in the surface's memory, which the surface's + /// buffer spans from its first byte ([`crate::Surface::buffer`]); `None` for a Direct3D + /// 11 plane. + laid: Option, }, } @@ -6171,6 +6176,27 @@ pub(crate) trait TextureObject { }) } + /// Of an exported surface's plane in place (and its views), where its texels lie in + /// `buffer`'s bytes, where `buffer` is on the surface's memory, which the plane's image + /// aliases ([`crate::Surface::buffer`]); `None` for any other texture or buffer. + fn rows_in(&self, buffer: &impl BufferObject) -> Option { + let image = self.object().image_of()?; + let Some(Under::Surface { + aliases: Some(aliases), + laid: Some(laid), + .. + }) = &image._under + else { + return None; + }; + let (buffer, offset) = buffer.surface_range(aliases, laid.offset)?; + Some(Rows { + buffer, + offset, + pitch: laid.row_pitch, + }) + } + /// Where its image's memory lies in the bytes commands reach a block the consumer holds /// through, which other resources there may share: in place over a buffer, from its first /// texel ([`in_place`](Self::in_place)); placed in a heap ([`Device::new_heap_texture`]), @@ -6804,6 +6830,9 @@ pub(crate) struct SurfaceImage { /// `VK_FORMAT_FEATURE_2_LINEAR_COLOR_ATTACHMENT_BIT_NV` (`crate::surface::Query::plane`): /// the image is marked ([`Image`]'s `stand_ins`). pub(crate) linear_attachment: bool, + /// An exported surface's plane's place in the surface's memory; `None` for a Direct3D 11 + /// plane. + pub(crate) laid: Option, } /// Destroys a surface plane's image not wrapped in a texture, and frees its own import. @@ -6958,6 +6987,7 @@ impl Device { d3d11: general, family: made.family, aliases, + laid: made.laid, }), }); let shadow = ShadowView::of(&image, &plan, index, shape.view_type, whole); diff --git a/docs/REFUSALS.md b/docs/REFUSALS.md index 4188ace62..b5a3f4a07 100644 --- a/docs/REFUSALS.md +++ b/docs/REFUSALS.md @@ -360,7 +360,7 @@ Columns: | `CubicSampler` | A sampler state whose minification or magnification filter is cubic (`Device::new_sampler_state`) | every device (a public call's own check of its arguments) | `ours`: the translator's identity domains do not filter bicubically, and Vulkan's cubic filter is not Metal's | 1: `crates/metal2vulkan-runtime/src/sampler.rs (new_sampler_state)` | `-` | | `TextureBytes` | `Texture::replace_region`, `Texture::get_bytes` or a blit copy between a buffer and a texture (`BlitCommandEncoder::copy_from_buffer`) of a... | every device (a public call's own check of its arguments) | `ours`: no copy between such an image and bytes is built, nor a texture buffer's copy as a buffer copy; no recorded stream copies a texture buffer | 5: `crates/metal2vulkan-runtime/src/blit.rs (buffer_copy, 2 sites)`, `crates/metal2vulkan-runtime/src/blit.rs (with_option)` (+2) | `-` | | `TextureCopy` | `BlitCommandEncoder::copy_from_texture` that the floor's `vkCmdCopyImage` does not make | every device (a public call's own check of its arguments) | `ours`: Vulkan 1.3 copies none of these with one command; each is built when a replay stream makes one | 2: `crates/metal2vulkan-runtime/src/blit.rs (copy_from_texture)`, `crates/metal2vulkan-runtime/src/blit.rs (texture_copy)` | `-` | -| `OverlappingCopy` | A blit copy whose source and destination share memory where no copy command orders it as from the source before the copy, which Metal leaves... | every device (a public call's own check of its arguments) | `ours`: no copy command reads bytes it writes, `VUID-vkCmdCopyImage-pRegions-00124` and its twins; the first shapes are built, cut on two levels, when a replay stream or a walk counts one | 2: `crates/metal2vulkan-runtime/src/blit.rs (buffer_copy)`, `crates/metal2vulkan-runtime/src/blit.rs (copy_from_texture)` | `-` | +| `OverlappingCopy` | A blit copy whose source and destination share memory where no copy command orders it as from the source before the copy, which Metal leaves... | every device (a public call's own check of its arguments) | `ours`: no copy command reads bytes it writes, `VUID-vkCmdCopyImage-pRegions-00124` and its twins; rows of two pitches that cross are built, in an order of rows from where they cross, when a replay stream or a walk counts one | 2: `crates/metal2vulkan-runtime/src/blit.rs (buffer_copy)`, `crates/metal2vulkan-runtime/src/blit.rs (copy_from_texture)` | `-` | | `TextureOverBuffer` | A texture over a buffer (`Buffer::new_texture`) the runtime does not make | every device (a public call's own check of its arguments) | `ours`: the in-place linear image and the copy form take one 2D color image, and a texture buffer's view the texel-buffer offset alignment of Vulkan 1.3 | 4: `crates/metal2vulkan-runtime/src/texture.rs (check_over)`, `crates/metal2vulkan-runtime/src/texture.rs (error)` (+2) | `-` | | `HeapTexture` | A heap texture the runtime does not place (`Device::new_heap_texture`, `Device::new_heap_texture_for_views` and `Device::heap_texture_size_and_align`) | every device (a public call's own check of its arguments) | `ours`: heap textures are built for Private heaps of placeable images | 2: `crates/metal2vulkan-runtime/src/texture.rs (heap_fit)`, `crates/metal2vulkan-runtime/src/texture.rs (heap_source)` | `-` | | `SurfaceUsage` | A surface plane in place (`Device::new_exported_surface`) whose texture's uses ask an image usage its linear tiling lacks (storage, for one) | at the device's limit | `device`: the image format query and the format features of the plane's tiling | 3: `crates/metal2vulkan-runtime/src/d3d11.rs (d3d11_plane_form)`, `crates/metal2vulkan-runtime/src/surface.rs (plane)` (+1) | `-` |