From 2416a42d3677e4f79019786a7f5ef2bd2baf0b1e Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 2 Dec 2026 19:34:03 -0800 Subject: [PATCH] rt: copies between a heap texture and memory it aliases are Metal's own refusal, but a whole level between two heap textures Row L212. Metal's API validation stops a copy between aliases in a placement heap unless it copies between buffers, between linear textures of one row stride, or entire texture surfaces. Source: probe overlapping_alias_copies.m on an Apple M3 Max, macOS 27.0.1, jobs m3-max-81486 and m3-max-81493. The runtime refused all of these as Gap::OverlappingCopy. It now refuses what Metal stops as Refusal::AliasingCopy, and records nothing for it. Form (blit.rs): - buffer_copy: a region that meets a heap texture's memory, either way, is Refusal::AliasingCopy (Metal: "blitting between aliasing resources is defined on a placement heap only when using a blit method copying entire texture surfaces, or when copying between buffers"). - texture_copy: two textures whose memory meets, one placed in a heap, that are not both heap textures copying a whole level into a whole level (whole_level: origin 0, size the level's extent, the view's first level taken off) are Refusal::AliasingCopy. A texture in place over a heap buffer against a heap texture is refused even for a whole level (Metal: "... only when both textures are linear and have the same bufferBytesPerRow value"). - The whole level between two heap textures stays Gap::OverlappingCopy, counted, per the planner's ruling. That is how the consumer reaches copyFromTexture:toTexture:, which Metal's validation takes and the M3 answers with the source's bytes at 0, 128 and 8320 bytes apart. No copy command and no cut reads no byte it writes (VUID-vkCmdCopyImage-pRegions-00124). The form through an image of the runtime's, counted, is built once a guest's run counts the gap. Docs: blit.rs's module and copy method docs; error.rs's Gap::OverlappingCopy (REFUSALS.md regenerated); Metrics::overlapping_copies_refused; the runtime README's blit encoders and their known gaps, which carry the M3's answer and the VUID. The README also cites the M3's answer for a surface's buffer and its own plane (job m3-max-81486). No conformance ledger row covers these shapes. Verified: refusal_surface.py --check exits 0. Green under the validation layer, which fails a test on any message: the three tests pass, with the rest of the 27 blit tests, at the series' tip on lavapipe (llvmpipe, LLVM 23.1.4; job m3-max-81597), the AMD Radeon 780M (RADV PHOENIX; m3-max-81592), the NVIDIA GeForce RTX 2080 (m3-max-81600) and the AMD Radeon RX 7900 XTX (RADV NAVI31; m3-max-81671). The runs built the series before its rebase onto development; the patches are the same. --- crates/metal2vulkan-runtime/README.md | 46 +++++++++++------ crates/metal2vulkan-runtime/src/blit.rs | 56 +++++++++++++++++---- crates/metal2vulkan-runtime/src/counters.rs | 4 +- crates/metal2vulkan-runtime/src/error.rs | 17 +++++-- docs/REFUSALS.md | 7 +-- 5 files changed, 95 insertions(+), 35 deletions(-) diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 0f0757a99..a782c42dd 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -3328,11 +3328,19 @@ while the driver's exit handler waited on a lock, with every runtime thread alre 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, - and the copy is `Gap::OverlappingCopy`. Two slices or levels of one heap texture are one - image's, and copy as any texture's. +- Two textures whose memory meets where one is placed in a heap (`Device::new_heap_texture`) are + aliases. Metal's validation layer takes a copy between aliases in a placement heap only of + entire texture surfaces (`copyFromTexture:toTexture:`, which the consumer copies as whole + levels), or between two linear textures of one row stride, which are the textures in place + above. So a copy between two heap textures that is not a whole level into a whole level, and + any copy between a heap texture and a texture in place over a buffer, is refused + (`Refusal::AliasingCopy`), as Metal's validation stops it: "blitting between aliasing resources + is defined on a placement heap only when using a blit method copying entire texture surfaces, + or when copying between buffers", and "... only when both textures are linear and have the same + bufferBytesPerRow value" (an Apple M3 Max, macOS 27.0.1, probe `overlapping_alias_copies.m` in + the conformance crate, jobs m3-max-81486 and m3-max-81493). A whole level into a whole level + between two heap textures is `Gap::OverlappingCopy` (below). Two slices or levels of one heap + texture are one image's, and copy as any texture's. - `copy_from_buffer` is `copyFromBuffer:sourceOffset:sourceBytesPerRow:sourceBytesPerImage:` `sourceSize:toTexture:destinationSlice:destinationLevel:destinationOrigin:` and `copy_from_texture_to_buffer` is `copyFromTexture:sourceSlice:sourceLevel:sourceOrigin:` @@ -3366,11 +3374,17 @@ while the driver's exit handler waited on a lock, with every runtime thread alre 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, 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. + plane in place are a buffer and a texture in place over it so: an Apple M3 Max gives the + source's bytes from before the copy between an IOSurface's no-copy buffer and its own plane's + texture, a row on each way and 8 texels along, into the plane and out of it, its validation + layer passing each (probe `overlapping_alias_copies.m`, job m3-max-81486). Where rows of two + pitches cross, the copy is `Gap::OverlappingCopy` (below). Where the region's bytes meet the + memory of a texture placed in a heap, the two are aliases in a placement heap, and the copy is + refused (`Refusal::AliasingCopy`), as Metal's validation stops it, in the words quoted for two + textures above (job m3-max-81486); with the layer off an Apple M3 Max gives the source's bytes. + Metal leaves a copy between a buffer and a texture over its bytes 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 of no whole number of a format's texels, rows or images closer than the region's, and a region past the buffer's end. Metal reads no row stride of a region one row high and no image stride of a @@ -3437,10 +3451,14 @@ while the driver's exit handler waited on a lock, with every runtime thread alre 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 - emulated. +- A copy of a whole level into a whole level between two textures placed in a heap whose memory + meets is refused alike (`Gap::OverlappingCopy`). Metal's validation takes it as a copy of entire + texture surfaces, and an Apple M3 Max gives the source's bytes from before the copy with the + two textures 0, 128 and 8320 bytes apart (jobs m3-max-81486 and m3-max-81493). No direct form + exists: the two images' memory overlaps and the device lays each one's texels out as it + chooses, so no copy command, nor any cut of the copy into several, is known to read no byte it + writes (`VUID-vkCmdCopyImage-pRegions-00124`). The form through an image of the runtime's, + copied in and then out, counted, is built once a guest's run counts this gap. - Measured on R8Unorm at 1920 x 1080 on a Radeon 780M under RADV (Mesa 26.2.6): a copy from a buffer into part of a one-sample colour texture (`copy_from_buffer`; `replace_region` of part of a texture records the same copy, not measured) can leave one aligned 4 x 2 cell of the diff --git a/crates/metal2vulkan-runtime/src/blit.rs b/crates/metal2vulkan-runtime/src/blit.rs index f17499212..ecdd8d7f5 100644 --- a/crates/metal2vulkan-runtime/src/blit.rs +++ b/crates/metal2vulkan-runtime/src/blit.rs @@ -10,7 +10,9 @@ //! 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 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 +//! texture placed in a heap shares with the other side is an alias of it, which Metal's +//! validation copies between only for entire texture surfaces (`Refusal::AliasingCopy`), and a +//! whole level into a whole level between two heap textures is in no order a copy command keeps //! (`Gap::OverlappingCopy`). //! //! A copy between a buffer and a texture is the same: `vkCmdCopyBufferToImage` or @@ -198,9 +200,12 @@ impl BlitCommandEncoder { /// [`Refusal::TextureCopyMismatch`]; [`Gap::TextureCopy`]; [`Gap::OverlappingCopy`], counted, /// 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)); with a texture buffer, + /// and another on one after its own), and for a whole level into a whole level between two + /// textures placed in a heap + /// ([`Device::new_heap_texture`](crate::Device::new_heap_texture)) whose memory meets, which + /// Metal's validation takes as a copy of entire texture surfaces; [`Refusal::AliasingCopy`] + /// for any other copy between two textures whose memory meets where one is placed in a heap, + /// which Metal's validation stops; with a texture buffer, /// what those calls refuse ([`Refusal::BufferCopyOverlap`] for regions whose bytes meet); /// [`Error::Vulkan`] where the command buffer cannot record. pub fn copy_from_texture( @@ -466,7 +471,9 @@ impl BlitCommandEncoder { /// 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 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; + /// them; [`Refusal::AliasingCopy`] for a texture placed in a heap whose memory meets the + /// region's bytes, as Metal's validation stops a copy between a buffer and a texture that + /// alias in a placement heap; /// [`Refusal::BufferCopyOverlap`] for a texture buffer whose bytes meet the region's; /// [`Error::Vulkan`] where the command buffer cannot record. pub fn copy_from_buffer( @@ -1787,7 +1794,7 @@ impl BufferCopy { /// and a row stride of whole blocks ([`Gap::BufferCopyAlignment`]). /// /// Err: [`Gap::TextureBytes`]; [`Refusal::TextureRegion`]; -/// [`Refusal::BufferCopyLayout`]; [`Gap::BufferCopyAlignment`]. +/// [`Refusal::BufferCopyLayout`]; [`Gap::BufferCopyAlignment`]; [`Refusal::AliasingCopy`]. pub(crate) fn buffer_copy( texture: &Side<'_>, at: TexturePlace, @@ -1892,8 +1899,10 @@ pub(crate) fn buffer_copy( // with it. No command's source and destination share a byte // (`VUID-vkCmdCopyBufferToImage-pRegions-00173`, `VUID-vkCmdCopyImageToBuffer-pRegions-00184`): // rows of a texture in place cut into slabs as two textures over one buffer do (`meeting`), - // the region's rows `bytes_per_row` apart; bytes of an image the device lays out as it - // chooses, none. + // the region's rows `bytes_per_row` apart. Bytes of a texture placed in a heap, whose texels + // the device lays out as it chooses, are an alias of the region's, and Metal's validation + // stops a copy between a buffer and a texture that alias in a placement heap (probe + // `overlapping_alias_copies.m` in the conformance crate, job m3-max-81486). let region = Span { buffer, base: start, @@ -1919,7 +1928,9 @@ pub(crate) fn buffer_copy( ) .or_else(|| row_by_row(from, to, placed.size, bytes)) } - (_, Some(bound)) if bound.meets(region) => None, + (_, Some(bound)) if bound.meets(region) => { + return Err(Error::Refused(Refusal::AliasingCopy { pixel_format })); + } _ => cut(None, [1; 3], placed.size), }; let slabs = match slabs { @@ -1940,7 +1951,8 @@ pub(crate) fn buffer_copy( /// they are one texture. `None` where it copies nothing; slabs of `None` where the two regions' /// bytes meet in no order a copy command keeps. /// -/// Err: [`Refusal::TextureCopyMismatch`]; [`Gap::TextureCopy`]; [`Refusal::TextureRegion`]. +/// Err: [`Refusal::TextureCopyMismatch`]; [`Gap::TextureCopy`]; [`Refusal::TextureRegion`]; +/// [`Refusal::AliasingCopy`]. pub(crate) fn texture_copy( source: &Side<'_>, from: TexturePlace, @@ -2052,8 +2064,22 @@ pub(crate) fn texture_copy( .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. + // chooses (a texture placed in a heap), which the two then alias. Metal's validation takes + // a copy between aliases in a placement heap only between buffers, between linear + // textures of one row stride (rows in place, above), or of entire texture surfaces + // (probe `overlapping_alias_copies.m` in the conformance crate, jobs m3-max-81486 and + // m3-max-81493): anything else is refused as Metal refuses it. A whole level into a whole + // level between two heap textures, which is how `copyFromTexture:toTexture:` reaches the + // runtime: no order. _ if !same && (source.bound.zip(destination.bound)).is_some_and(|(a, b)| a.meets(b)) => { + let surface = |side: &Side<'_>, placed: &Placed| { + side.rows.is_none() && whole_level(side.descriptor, side.base[0], placed) + }; + if !(surface(source, &from) && surface(destination, &to)) { + return Err(Error::Refused(Refusal::AliasingCopy { + pixel_format: formats.0, + })); + } None } _ => cut(None, [1; 3], from.size), @@ -2148,6 +2174,14 @@ fn row_by_row(from: Rows, to: Rows, size: [u32; 3], texel: u64) -> Option