From 203b045da90c6fffc6b38c6a3f3efbba5285c72d Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 22 Nov 2026 03:57:05 -0800 Subject: [PATCH] Tests: a smaller pass resolves through the resolve image only within its render area The resolve image moves away from UNDEFINED at each pass, so whatever it held from an earlier pass is discarded. The copy back must therefore cover only the pass's render area, or it would write stale texels over the buffer. It does: the region is Pass::reach from the origin, where the resolve writes every texel. every_device_resolves_a_loaded_pass_into_a_shader_read_texture_over_a_buffer now checks this on every row: - The host refills the buffer with 0xee, and a 300 x 300 pass (renderTargetWidth and Height) resolves into the texture. - The 320 x 320 texels of Vulkan's render area (300 rounded up to pass::REACH) read the clear within one code. - Every other texel byte, and every byte past the rows, keeps 0xee. - Through the resolve image, the copy is 409,600 bytes. Docs: the README's two Render encoders paragraphs, RenderPassKind::ResolveUsage and resolve_attachment's doc now say exactly which view stays refused. It is a view of a texture in place whose linear image takes no attachment usage. Where the linear image takes the usage (the first fix, 7193df2244), resolve_attachment views the view's texels through attachment_view on the parent's linear image, so a view resolves in place as its texture does. --- crates/metal2vulkan-runtime/README.md | 10 +-- crates/metal2vulkan-runtime/src/error.rs | 4 +- .../metal2vulkan-runtime/src/tests/resolve.rs | 64 ++++++++++++++++++- crates/metal2vulkan-runtime/src/texture.rs | 3 +- 4 files changed, 73 insertions(+), 8 deletions(-) diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 5f7ede023..0cc078f5d 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -3574,7 +3574,9 @@ cache is answered compile-required on both. without `RenderTarget` usage whose image has no attachment usage and that has no resolve image is a known gap (`RenderPassKind::ResolveUsage`): a depth or stencil texture, a colour texture of another type or of a format the device's optimal image does not render, a texture in place over - a dma-buf's import, and a view of a texture in place over a buffer. Refused as + a dma-buf's import, and a view of a texture in place over a buffer whose linear image takes no + attachment usage (where the linear image takes it, a view resolves in place, as its texture + does). Refused as Metal refuses it: a resolving action with no resolve texture (`Refusal::ResolveTexture`, also where an encoder sets it later) and an integer format's resolve (`Refusal::ResolveFormat`). `Metrics::resolves` counts the attachments passes resolved. @@ -3978,9 +3980,9 @@ cache is answered compile-required on both. it does not, or where its subresource reaches past its own rows or its buffer, the pass resolves into the texture's resolve image and copies it into the buffer, above), and a depth or stencil texture does not, nor a texture in place over a dma-buf's import, nor a view of a texture in - place; the usage grows a 1920 x 1080 `RGBA8Unorm` optimal image of one level 1.0056 times on - the Radeon 780M and changes no linear image's layout, `RenderPassKind::ResolveUsage` has the - measures), + place whose linear image takes no attachment usage; the usage grows a 1920 x 1080 `RGBA8Unorm` + optimal image of one level 1.0056 times on the Radeon 780M and changes no linear image's layout, + `RenderPassKind::ResolveUsage` has the measures), a depth or stencil resolve the device's resolve modes do not offer (`ResolveFilter`: `Min` or `Max` where `supportedDepthResolveModes` lacks it, the stencil filter `DepthResolvedSample`, which no Vulkan resolve mode is, and two aspects paired where `independentResolve` or diff --git a/crates/metal2vulkan-runtime/src/error.rs b/crates/metal2vulkan-runtime/src/error.rs index efba7adae..b90a6479d 100644 --- a/crates/metal2vulkan-runtime/src/error.rs +++ b/crates/metal2vulkan-runtime/src/error.rs @@ -1794,7 +1794,9 @@ pub enum RenderPassKind { /// whose format the device's optimal image does not render, by its format features or its /// image format query at the texture's extent; a texture in place over a dma-buf's import (an /// imported surface's memory, whose bytes reach its image through the import's acquire); and - /// a view of a texture in place over a buffer. A texture in place over a buffer of the + /// a view of a texture in place over a buffer whose linear image takes no attachment usage + /// (where it takes the usage, a view resolves in place, as its texture does). A texture in + /// place over a buffer of the /// runtime's own memory whose linear image takes none (of a format the device's linear tiling /// does not render, by its linear features or its image format query at the texture's extent, /// measured on the RTX 3090 and KosmicKrisp for every format; or whose subresource reaches diff --git a/crates/metal2vulkan-runtime/src/tests/resolve.rs b/crates/metal2vulkan-runtime/src/tests/resolve.rs index 6e84ca5d1..4eb098822 100644 --- a/crates/metal2vulkan-runtime/src/tests/resolve.rs +++ b/crates/metal2vulkan-runtime/src/tests/resolve.rs @@ -28,7 +28,8 @@ //! none, the pass resolves into the texture's resolve image and copies its texels into the //! buffer, one copy of 1,440,000 bytes counted. The buffer reads the clear within one code and //! keeps the bytes past each row's texels; a pass after it in the same command buffer reads the -//! clear through the texture; a second resolve into it makes no image and no allocation. A depth +//! clear through the texture; a second resolve into it makes no image and no allocation; a pass +//! of a smaller area copies its render area alone, the texels past it untouched. A depth //! resolve into a `Depth32Float` texture of `ShaderRead` usage is the gap `ResolveUsage`. //! - Under a device cfg, on every device of the cfg's kind, whether it has //! `shaderStorageImageMultisample` or not: a 4-sample `RGBA8Unorm` texture made for shader @@ -726,12 +727,14 @@ mod on_device { /// are counted. The buffer's bytes read the clear within one code, and the bytes past each /// row's texels keep the host's. A pass after it in the same command buffer reads the texture /// at each pixel, its centre the clear within one code. A second resolve into the texture, in - /// a command buffer of its own, makes no image and no allocation. + /// a command buffer of its own, makes no image and no allocation. A pass of a 300 x 300 area + /// resolves Vulkan's 320 x 320 render area alone, the texels past it keeping the host's bytes. #[test] fn every_device_resolves_a_loaded_pass_into_a_shader_read_texture_over_a_buffer() { const WALK: u32 = 600; const ROW: u64 = WALK as u64 * 4; const TEXELS: u64 = WALK as u64 * WALK as u64 * 4; + const AREA: u32 = 300; let Some(library) = examples() else { return; }; @@ -942,6 +945,63 @@ mod on_device { {made} images and {allocated} allocations, resolve images {images} to \ {live}; bytes copied {copied}, resolve copies {again}" ); + // A pass of a smaller area resolves and copies Vulkan's render area alone: + // 300 x 300, rounded up to 320 (`pass::REACH`). The texels past it keep the + // host's bytes. + // SAFETY: the buffer's bytes, which no command uses now. + unsafe { contents.as_ptr().write_bytes(0xee, len as usize) }; + if managed { + (buffer.did_modify_range(0, len)).expect("a modified range"); + } + let reach = AREA.next_multiple_of(crate::pass::REACH); + let mut small = walk.clone(); + small.render_target_width = AREA; + small.render_target_height = AREA; + let command_buffer = queue.command_buffer().expect("a command buffer"); + let before = device.counters().metrics(); + let encoder = (command_buffer.render_command_encoder(&small)) + .unwrap_or_else(|e| panic!("{name}: {kind} at {pitch}: {e}")); + encoder.end_encoding(); + if managed { + let mut blit = command_buffer.blit_command_encoder().expect("a blit"); + (blit.synchronize_resource(Resource::Buffer(&buffer))) + .unwrap_or_else(|error| panic!("{name}: {error}")); + blit.end_encoding(); + } + run(name, &command_buffer); + let after = device.counters().metrics(); + let copy_bytes = after.resolve_copy_bytes - before.resolve_copy_bytes; + let area_bytes = u64::from(reach) * u64::from(reach) * 4; + assert_eq!( + copy_bytes, + u64::from(through) * area_bytes, + "{name}: {kind} at {pitch}: {form}, the smaller pass's copy" + ); + // SAFETY: the buffer's bytes, which no command uses now. + let held = + unsafe { std::slice::from_raw_parts(contents.as_ptr(), len as usize) }; + let (mut inside, mut outside) = (Vec::new(), 0); + for (y, r) in held.chunks(pitch_bytes).enumerate() { + for (x, texel) in r[..row].chunks(4).enumerate() { + match x < reach as usize && y < reach as usize { + true => inside.extend_from_slice(texel), + false => outside += texel.iter().filter(|&&b| b != 0xee).count(), + } + } + } + let past = (held.chunks(pitch_bytes)).flat_map(|r| &r[row..]); + let past = past.filter(|&&byte| byte != 0xee).count(); + cleared(name, &inside); + assert_eq!( + (outside, past), + (0, 0), + "{name}: {kind} at {pitch}: {form}, bytes past the {reach} x {reach} area" + ); + println!( + "resolve_over: {name}: {kind} pitch {pitch}: a {AREA} x {AREA} pass \ + resolved {reach} x {reach} texels; {outside} bytes of the texels past \ + them and {past} past the rows changed; resolve copy bytes {copy_bytes}" + ); drop((over, buffer, block)); } } diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index f8bff143f..e09c67ef2 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -4668,7 +4668,8 @@ pub(crate) trait TextureObject { /// none and that has no resolve image: a depth or stencil texture made without `RenderTarget` /// usage, a colour texture of a type but 2D, a 2D array, a cube or a cube array or whose format /// the device's optimal image does not render, a texture in place over a dma-buf's import, and - /// a view of a texture in place; [`Refusal::AttachmentPlace`] past a texture over a buffer's + /// a view of a texture in place whose linear image takes none; [`Refusal::AttachmentPlace`] + /// past a texture over a buffer's /// one level and slice; [`Error::Vulkan`] where its resolve image cannot be made; as /// [`attachment`] for the rest. ///