diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs index ecf71347b..042cb2895 100644 --- a/crates/metal2vulkan-runtime/src/device/mod.rs +++ b/crates/metal2vulkan-runtime/src/device/mod.rs @@ -654,6 +654,13 @@ pub(super) struct DeviceShared { /// `tests/dma_buf_plane_offset.rs` on a driver that answers the offset asked. #[cfg(test)] pub(crate) plane_route_forced: std::sync::atomic::AtomicU8, + /// Where not 0, no image lies in place over host pages or a copy of them whose rows are not + /// that many bytes apart or whose texels do not start at that many bytes' multiple in their + /// buffer, whatever the driver answers (`Device::unplaced`): RADV's rule for a + /// `DRM_FORMAT_MOD_LINEAR` plane (256 on GFX9 to GFX11), for the tests of + /// `tests/read_twin_device.rs` on a driver that lays such rows in place. + #[cfg(test)] + pub(crate) linear_pitch_rule_forced: AtomicU64, /// The logs of its shadows over host pages a consumer logs the writes to /// (`dirty_pages.rs`). pub(super) dirty: super::dirty_pages::DirtyShadows, @@ -3151,6 +3158,8 @@ impl Device { store_out_unfaulted: Default::default(), #[cfg(test)] plane_route_forced: Default::default(), + #[cfg(test)] + linear_pitch_rule_forced: Default::default(), dirty: Default::default(), standings: Default::default(), fresh: Default::default(), diff --git a/crates/metal2vulkan-runtime/src/read_twin.rs b/crates/metal2vulkan-runtime/src/read_twin.rs index 8a6d8d5ee..4867d97c3 100644 --- a/crates/metal2vulkan-runtime/src/read_twin.rs +++ b/crates/metal2vulkan-runtime/src/read_twin.rs @@ -34,11 +34,28 @@ //! unknown access being a write), and every descriptor of it is that image, none an element //! of the heap, a texel buffer or a plane. The slot then binds the texture's read twin: the //! runtime's texture of the same descriptor over the twin's bytes, made with the texture and -//! kept only where it lies in place over them, the device's check of textures in place -//! passed (`Buffer::make_twin_texture`; else `WorkCounts::read_twin_textures_not_in_place`), -//! and only where it serves the slot's bindings with no shadow of its usage. A slot read -//! through a snapshot, narrowed to levels, or presenting an attachment of the draw's instance -//! binds the texture itself. Counted: `WorkCounts::read_twin_slot_reads`. +//! kept where it lies in place over them, the device's check of textures in place passed +//! (`Buffer::make_twin_texture`; else `WorkCounts::read_twin_textures_not_in_place`), and +//! only where it serves the slot's bindings with no shadow of its usage. A slot read through a +//! snapshot, narrowed to levels, or presenting an attachment of the draw's instance binds the +//! texture itself. Counted: `WorkCounts::read_twin_slot_reads`. +//! - the same slot, where the twin's texture lies in place over neither the twin's bytes nor +//! the texture over the copy's (a driver that lays no `DRM_FORMAT_MOD_LINEAR` plane at the +//! texture's pitch or offset, as RADV lays none whose rows or first byte are off a multiple +//! of 256): the twin's texture is then a shadow of the twin's bytes, kept while the texture is +//! a shadow too (`WorkCounts::read_twin_texture_shadows`), and the slot binds it. The command +//! copies it in from the twin, as it copies a bound texture's own shadow in +//! (`shadow::shadow_uses_of` lists it beside the texture's): one copy of the texels from the +//! pages in place (`vkCmdCopyBufferToImage`, which reads its source alone) at its first use +//! in each command buffer, where the slot would have copied the texture's own shadow in from +//! the copy, which never stands either (`shadow_standing.rs`); where the pages' memory came +//! with a log of the writes to them, only the rows of the pages it reports changed +//! (`dirty_pages.rs`). On a device without unified memory the copy lies in system memory +//! (`MemoryClass::Shared`), as the pages do, so the copy in crosses to the device either way; +//! what goes is the commit's comparison of the copy with the pages. The shadow is the +//! runtime's own image, which no call returns; a slot that only reads the texture reads it, +//! so nothing copies it back, and a copy back would write the twin's key, which every +//! encoder refuses. It costs an image of the texture's size while the texture lives. //! - a buffer slot of an encoder's own draw or dispatch bound to the buffer, where the command //! reaches no memory through a root table (so no address of the buffer reaches its //! functions, and no loaded address reaches the twin) and only reads the slot: every use of @@ -224,23 +241,36 @@ impl Buffer { }; let counts = &self.generation().shared().counts; let made = twin.new_runtime_texture("make_twin_texture", descriptor, offset, bytes_per_row); + let in_place = made.as_ref().is_ok_and(Texture::aliases_buffer); + // Not in place, a shadow of the twin's bytes serves where the texture is itself a shadow + // (of its copy): its read slots copy the twin's shadow in where they copied the + // texture's (the module docs). + let shadow = + !in_place && texture.shadows_bytes() && made.as_ref().is_ok_and(Texture::shadows_bytes); + if !in_place { + let made = (made.as_ref()).map(|_| ()); + let shape = (descriptor, offset, bytes_per_row); + crate::shadow_log::twin_not_in_place(texture, (self, &twin), shape, (made, shadow)); + counts.read_twin_texture(false); + } let made = match made { - Ok(made) if made.aliases_buffer() => made, - not => { - let made = (not.as_ref()).map(|_| ()); - let shape = (descriptor, offset, bytes_per_row); - crate::shadow_log::twin_not_in_place(texture, (self, &twin), shape, made); - counts.read_twin_texture(false); - return; - } + Ok(made) if in_place || shadow => made, + _ => return, }; + if shadow { + if let Some(copies) = made.shadow() { + texture.keep_twin_shadow(copies); + } + counts.read_twin_texture_shadow(); + } else { + counts.read_twin_texture(true); + } let weak: Weak = texture.downgrade(); cell.keep_twin_texture(TextureTwin { of: texture.object_id(), texture: weak, twin: Arc::new(made), }); - counts.read_twin_texture(true); } } @@ -288,7 +318,9 @@ impl Texture { let twin = kept.downcast_ref::()?.clone(); let shadowed = (kinds.storage && twin.needs_usage_shadow(true)) || (kinds.sampled && twin.needs_usage_shadow(false)); - if shadowed || !twin.aliases_buffer() { + // In place over the twin's bytes, or a shadow of them where the texture is a shadow too. + let serves = twin.aliases_buffer() || (twin.shadows_bytes() && self.shadows_bytes()); + if shadowed || !serves { return None; } let counts = &command_buffer.generation().counts; diff --git a/crates/metal2vulkan-runtime/src/shadow.rs b/crates/metal2vulkan-runtime/src/shadow.rs index 1ebcdf010..62f05569e 100644 --- a/crates/metal2vulkan-runtime/src/shadow.rs +++ b/crates/metal2vulkan-runtime/src/shadow.rs @@ -2847,13 +2847,15 @@ pub(super) fn shadow_uses_of<'a>( copies_used(copies.iter().chain(resident), uses) } -/// A texture's own shadow's copies, and those of the one a binding beyond its usage made +/// A texture's own shadow's copies, those of the one a binding beyond its usage made /// (`texture/usage_shadow.rs`; a texture with faces has its attached reads' beside it, -/// `texture/faces.rs`), added to `copies`. +/// `texture/faces.rs`), and those of its read twin's shadow, which a slot that only reads it +/// binds in its place (`read_twin.rs`), added to `copies`. fn texture_copies(texture: &Texture, copies: &mut Vec>) { copies.extend(texture.shadow().cloned()); copies.extend(texture.usage_shadow_copies()); copies.extend(texture.usage_shadow_copies_beside()); + copies.extend(texture.twin_shadow_copies()); } /// The shadows' copies of the textures a residency made resident ([`texture_copies`] of each, in diff --git a/crates/metal2vulkan-runtime/src/shadow_log.rs b/crates/metal2vulkan-runtime/src/shadow_log.rs index a9485d0ac..a3f6e834b 100644 --- a/crates/metal2vulkan-runtime/src/shadow_log.rs +++ b/crates/metal2vulkan-runtime/src/shadow_log.rs @@ -32,7 +32,9 @@ //! (`read_twin.rs`) whose twin texture is not in place over the twin's bytes, as its making //! leaves it: the texture, its buffer, and why, the twin's placement (its query's answer) where //! the making made a shadow, whose own line (`make_twin_texture made ...`) names the twin's bytes -//! and the shadow's cause in full, or the making's error ([`twin_not_in_place`]). +//! and the shadow's cause in full, or the making's error ([`twin_not_in_place`]); and whether +//! that shadow of the twin's pages serves the slots that only read the texture (where the +//! texture is a shadow too) or they bind the texture. //! - **Off, it costs nothing.** The switch is read once; no line is formatted, and a draw or a //! dispatch names no texture. @@ -342,12 +344,12 @@ fn samplers_named( /// `buffer` at `offset` with rows of `bytes_per_row` bytes, is not in place over the twin's bytes /// (the module's **A read twin not in place**): `made` is the making's answer, a shadow over the /// twin, `twin` (`Ok`), or its error; a shadow's placement is asked of the twin again, as its query -/// answers it. +/// answers it. `served`: that shadow serves the slots that only read the texture. pub(super) fn twin_not_in_place( texture: &Texture, (buffer, twin): (&Buffer, &Buffer), (descriptor, offset, bytes_per_row): (&TextureDescriptor, u64, u64), - made: Result<(), &Error>, + (made, served): (Result<(), &Error>, bool), ) { if !enabled() { return; @@ -370,10 +372,14 @@ pub(super) fn twin_not_in_place( }, Err(error) => format!("its making was refused: {error}"), }; + let serves = match served { + true => "its shadow of the twin's pages serves the slots that only read the texture", + false => "the slots that only read the texture bind the texture", + }; let text = format!( "metal2vulkan: shadow: the read twin of the {:?} texture of {}x{} and {} levels, object \ {}, over the buffer object {} at offset {offset} with rows of {bytes_per_row} bytes, is \ - not in place: {why}", + not in place: {why}; {serves}", texture.pixel_format(), texture.width(), texture.height(), diff --git a/crates/metal2vulkan-runtime/src/tests/read_twin_device.rs b/crates/metal2vulkan-runtime/src/tests/read_twin_device.rs index d2d5ae0e4..612654fe2 100644 --- a/crates/metal2vulkan-runtime/src/tests/read_twin_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/read_twin_device.rs @@ -28,7 +28,20 @@ //! the pages in place again. So for the screen-sized buffer's texture at a 3648-texel pitch, //! where the device's linear image of it lies within the buffer's bytes; where it runs past //! them (lavapipe's takes two rows more), neither the texture nor a twin lies in place, and the -//! texture is read through its shadow as before. +//! kernel reads the twin's shadow of the pages, as below. +//! - **A texture no image lies over, read through a shadow of its twin.** With RADV's rule for a +//! `DRM_FORMAT_MOD_LINEAR` plane forced on the test's device (rows and a first byte at +//! multiples of 256, `DeviceShared::linear_pitch_rule_forced`), a window server's shapes (48 x +//! 48 at rows of 192 bytes; 128 x 128 at rows of 512 bytes from 32 bytes in) lie in place over +//! neither the buffer's copy nor its twin's pages: the texture is a shadow of the copy, and its +//! twin, kept, a shadow of the pages (`read_twin_texture_shadows`). A draw that samples it and a +//! kernel that reads it bind the twin's shadow, copied in from the pages: the texels are the +//! pages', a CPU write reaches the next command buffer, and no commit compares the copy; after +//! a fill of the buffer in the same command buffer they read the texture itself, the fill read +//! back, the pages kept. Over pages whose memory came with a log of their writes, the twin's +//! shadow copies in every row at the first command buffer, nothing at the next where the log +//! reports no write (`dirty_copies_in_skipped`, `dirty_copy_in_bytes_copied`), and the rows of a +//! told write alone. //! - **A texture over it, sampled in place by a draw.** A small buffer's texture sampled by a //! draw reads through its twin, the pass's pixels those of a draw sampling a texture of the //! device's own holding the same texels, and the commit compares nothing; after a fill of the @@ -48,6 +61,7 @@ //! that imports no host memory, and a system that maps read-only views (macOS, whose buffers take //! no twin), say so and pass. +use super::dirty_pages_device::Log; use crate::{ Buffer, CommandBufferStatus, ComputePipelineState, Device, DeviceKind, DeviceOptions, HostMemory, ImportGate, ImportRequest, MessageSink, Origin, Region, ResourceOptions, Size, @@ -123,6 +137,23 @@ impl Pages { } .expect("a buffer of bit 24 over the pages") } + + /// A no-copy buffer of Metal's private option bit 24 over the pages, whose writes `log` logs + /// (`HostMemory::with_dirty_pages`, `dirty_pages.rs`). + fn logged_private_bit_buffer(&self, device: &Device, log: &Arc) -> Buffer { + // SAFETY: the pages outlive the buffer and the device (dropped after both). + let memory = unsafe { HostMemory::new(self.pointer) }.with_dirty_pages(Arc::clone(log)); + // SAFETY: as above. + unsafe { + device.new_buffer_with_bytes_no_copy( + memory, + self.layout.size() as u64, + private_bit(), + None, + ) + } + .expect("a logged buffer of bit 24 over the pages") + } } impl Drop for Pages { @@ -639,11 +670,12 @@ fn no_twin_where_the_policy_contains_no_access() { fn placed(before: &WorkCounts, after: &WorkCounts) -> String { let delta = |of: fn(&WorkCounts) -> u64| of(after) - of(before); format!( - "twins {} (not in place {}), in place over host pages {}, over copies {}, pitched {}, \ - shadowed {}: pending {} failed {} unchecked {} refused rows {} image {} pitch {} \ - placement {} source {}", + "twins {} (not in place {}, shadows {}), in place over host pages {}, over copies {}, \ + pitched {}, shadowed {}: pending {} failed {} unchecked {} refused rows {} image {} \ + pitch {} placement {} source {}", delta(|c| c.read_twin_textures_made), delta(|c| c.read_twin_textures_not_in_place), + delta(|c| c.read_twin_texture_shadows), delta(|c| c.host_textures_in_place), delta(|c| c.copied_textures_in_place), delta(|c| c.pitched_host_images), @@ -748,10 +780,11 @@ fn texture_over( } /// The screen's texture (`WIDTH` x `HEIGHT` `BGRA8Unorm`, rows `ROW` bytes apart, `ShaderRead`) -/// over a buffer of bit 24 over `pages`, and whether it took a read twin: one where the device -/// lays its linear image in the buffer's bytes. Where it does not (lavapipe's image takes two rows -/// more), neither the texture nor its twin lies in place, and the texture is read through its -/// shadow, its texels the pages'. +/// over a buffer of bit 24 over `pages`, and whether it took a read twin that serves: one where +/// the device lays its linear image in the buffer's bytes, or where it does not (lavapipe's image +/// takes two rows more) and the texture is a shadow of its copy, a shadow of the twin's pages. +/// Where neither (the texture in place over its copy, its twin not over the pages), the texture +/// is read through itself, its texels the pages'. fn screen_texture(device: &Device, pages: &Pages) -> (Buffer, Texture, bool) { let buffer = pages.private_bit_buffer(device, None); assert!(buffer.read_twin().is_some(), "a twin made"); @@ -763,7 +796,8 @@ fn screen_texture(device: &Device, pages: &Pages) -> (Buffer, Texture, bool) { "READ-TWIN: the screen's texture: {}", placed(&before, &after) ); - let twinned = after.read_twin_textures_made > before.read_twin_textures_made; + let twinned = after.read_twin_textures_made > before.read_twin_textures_made + || after.read_twin_texture_shadows > before.read_twin_texture_shadows; if !twinned { let refused = after.host_texture_shadows_refused_placement - before.host_texture_shadows_refused_placement; @@ -797,7 +831,7 @@ fn a_screen_sized_private_bit_buffer_s_texture_is_read_in_place_by_a_kernel() { ); return; } - read_by_a_kernel(&device, (&pages, &buffer), &texture, ROW); + read_by_a_kernel(&device, (&pages, &buffer), &texture, (0, ROW)); drop((texture, buffer)); } @@ -820,7 +854,12 @@ fn a_screen_sized_private_bit_buffer_s_texture_is_sampled_in_place_by_a_draw() { assert!(drawn == expected, "the pages' texels sampled"); return; } - sampled_in_place(&device, (&pages, &buffer), &texture, shape); + sampled_in_place( + &device, + (&pages, &buffer), + &texture, + (0, WIDTH, HEIGHT, ROW), + ); drop((texture, buffer)); } @@ -841,7 +880,7 @@ fn a_private_bit_buffer_s_texture_is_read_in_place_by_a_kernel() { "the texture's twin made, in place: {}", placed(&before, &after) ); - read_by_a_kernel(&device, (&pages, &buffer), &texture, SIDE * 4); + read_by_a_kernel(&device, (&pages, &buffer), &texture, (0, SIDE * 4)); drop((texture, buffer)); } @@ -942,18 +981,157 @@ fn the_shadow_log_names_a_twin_not_in_place_and_a_dispatch_s_first_read() { drop((texture, buffer)); } -/// A kernel reads `texture`, over `buffer`'s bytes (of `pages`) in rows `row` bytes apart, which -/// took a read twin: through its twin, the commit comparing nothing; a CPU write of the pages -/// read in place; after a fill of the buffer in the same command buffer, through the texture -/// itself, the fill read back; and the next command buffer through its twin again. +/// A texture of `shape` (`BGRA8Unorm`, `ShaderRead`, `width` x `height` at `offset` with rows +/// `row` bytes apart) over `buffer`, of bit 24, under RADV's rule for a `DRM_FORMAT_MOD_LINEAR` +/// plane, forced on the test's device (`DeviceShared::linear_pitch_rule_forced`, 256): no image of +/// it lies over the buffer's copy or its twin's pages, so the texture is a shadow of the copy and +/// its twin, kept, a shadow of the pages (`WorkCounts::read_twin_texture_shadows`). +fn refused_twin( + device: &Device, + buffer: &Buffer, + (offset, width, height, row): (u64, u64, u64, u64), +) -> Texture { + (device.shared().linear_pitch_rule_forced).store(256, Ordering::Relaxed); + assert!(buffer.read_twin().is_some(), "a twin made"); + let mut descriptor = + TextureDescriptor::texture_2d(PixelFormat::BGRA8Unorm, width, height, false); + descriptor.usage = TextureUsage::SHADER_READ; + descriptor.resource_options = ResourceOptions::shared(); + let before = device.work_counts(); + let texture = buffer.new_texture(&descriptor, offset, row).unwrap(); + let after = device.work_counts(); + eprintln!( + "READ-TWIN: a {width}x{height} texture at offset {offset}, rows {row} bytes apart, under \ + the rule: {}", + placed(&before, &after) + ); + let delta = |of: fn(&WorkCounts) -> u64| of(&after) - of(&before); + assert!( + texture.shadow().is_some(), + "the texture a shadow of its copy" + ); + assert_eq!( + [ + delta(|c| c.read_twin_textures_made), + delta(|c| c.read_twin_textures_not_in_place), + delta(|c| c.read_twin_texture_shadows), + ], + [0, 1, 1], + "its twin not in place, kept as a shadow of the pages: (made, not in place, shadows)" + ); + texture +} + +/// The shapes of a window server's textures over buffers of bit 24 that RADV lays no image of: 48 +/// x 48 at rows of 192 bytes, and 128 x 128 at rows of 512 bytes from 32 bytes in. +const REFUSED: [(u64, u64, u64, u64); 2] = [(0, 48, 48, 192), (32, 128, 128, 512)]; + +#[test] +fn a_private_bit_buffer_s_texture_no_image_lies_over_is_sampled_through_a_shadow_of_its_twin() { + let pages = [Pages::new(2 * SMALL, 43), Pages::new(2 * SMALL, 47)]; + let Some(device) = device(None) else { + return; + }; + for (pages, shape) in pages.iter().zip(REFUSED) { + let buffer = pages.private_bit_buffer(&device, None); + let texture = refused_twin(&device, &buffer, shape); + sampled_in_place(&device, (pages, &buffer), &texture, shape); + drop((texture, buffer)); + } +} + +#[test] +fn a_private_bit_buffer_s_texture_no_image_lies_over_is_read_through_a_shadow_of_its_twin() { + let pages = Pages::new(2 * SMALL, 53); + let Some(device) = device(None) else { + return; + }; + let buffer = pages.private_bit_buffer(&device, None); + let (offset, width, height, row) = REFUSED[1]; + let texture = refused_twin(&device, &buffer, (offset, width, height, row)); + read_by_a_kernel(&device, (&pages, &buffer), &texture, (offset, row)); + drop((texture, buffer)); +} + +/// The gated copies in of shadows over logged pages between two counts: gated, whole, skipped, +/// the bytes a whole copy would move, and those moved (`dirty_pages.rs`). +fn gated(before: &WorkCounts, after: &WorkCounts) -> [u64; 5] { + [ + after.dirty_copies_in_gated - before.dirty_copies_in_gated, + after.dirty_copies_in_whole - before.dirty_copies_in_whole, + after.dirty_copies_in_skipped - before.dirty_copies_in_skipped, + after.dirty_copy_in_bytes_whole - before.dirty_copy_in_bytes_whole, + after.dirty_copy_in_bytes_copied - before.dirty_copy_in_bytes_copied, + ] +} + +/// Over pages whose memory came with a log of the writes to them (`HostMemory::with_dirty_pages`), +/// the twin's pages carry the log too: a kernel that reads the texture through the twin's shadow +/// copies every row in at the first command buffer, nothing at the next where the log reports no +/// write, and only the rows it reports after a CPU write it was told of; no commit compares. +#[test] +fn a_shadow_of_a_twin_over_logged_pages_copies_in_only_what_the_log_reports() { + let pages = Pages::new(2 * SMALL, 59); + let log = Arc::new(Log::default()); + let Some(device) = device(None) else { + return; + }; + let buffer = pages.logged_private_bit_buffer(&device, &log); + let (offset, width, height, row) = REFUSED[1]; + let texture = refused_twin(&device, &buffer, (offset, width, height, row)); + let state = invert(&device); + let out = square(&device, PixelFormat::BGRA8Unorm); + let texels = |bytes: Vec| bytes[offset as usize..].to_vec(); + let frame = |what: &str| { + let before = device.work_counts(); + let status = invert_texels(&device, &state, (&texture, &out), None); + let after = device.work_counts(); + assert_eq!(status, CommandBufferStatus::Completed); + assert_eq!(after.read_twin_slot_reads - before.read_twin_slot_reads, 1); + assert_eq!( + compared(&before, &after), + [0, 0, 0], + "{what}: nothing compared" + ); + assert!( + square_texels(&out) == inverted(&texels(pages.bytes()), row), + "{what}: the pages' texels" + ); + let moved = gated(&before, &after); + eprintln!( + "READ-TWIN: over logged pages, {what}: (gated, whole, skipped, bytes whole, copied) \ + {moved:?}" + ); + moved + }; + let first = frame("the first command buffer"); + assert_eq!(first[0], 1, "the first copy in is gated"); + assert!(first[4] > 0 && first[4] == first[3], "every row"); + let unchanged = frame("no write since"); + assert_eq!(unchanged[2], 1, "the copy in skipped"); + assert_eq!(unchanged[4], 0, "nothing moved"); + // Row 10's first texels, written and told. + let at = offset + 10 * row; + pages.write(at as usize, &[0x42; 64]); + log.told(at..at + 64, 0); + let told = frame("a told write"); + assert!(told[4] > 0 && told[4] < told[3], "the told rows alone"); + drop((texture, buffer)); +} + +/// A kernel reads `texture`, over `buffer`'s bytes (of `pages`) from `offset` in rows `row` bytes +/// apart, which took a read twin: through its twin, the commit comparing nothing; a CPU write of +/// the pages read in place; after a fill of the buffer in the same command buffer, through the +/// texture itself, the fill read back; and the next command buffer through its twin again. fn read_by_a_kernel( device: &Device, (pages, buffer): (&Pages, &Buffer), texture: &Texture, - row: u64, + (offset, row): (u64, u64), ) { let state = invert(device); let out = square(device, PixelFormat::BGRA8Unorm); + let texels = |bytes: Vec| bytes[offset as usize..].to_vec(); let before = device.work_counts(); let status = invert_texels(device, &state, (texture, &out), None); let after = device.work_counts(); @@ -966,22 +1144,22 @@ fn read_by_a_kernel( compared(&before, &after) ); assert!( - square_texels(&out) == inverted(&pages.bytes(), row), + square_texels(&out) == inverted(&texels(pages.bytes()), row), "the pages' texels" ); // A CPU write of the pages reaches the next command buffer's read in place. - pages.write(0, &[0x42; 64]); + pages.write(offset as usize, &[0x42; 64]); let status = invert_texels(device, &state, (texture, &out), None); assert_eq!(status, CommandBufferStatus::Completed); assert!( - square_texels(&out) == inverted(&pages.bytes(), row), + square_texels(&out) == inverted(&texels(pages.bytes()), row), "the CPU's write read" ); // A fill of the buffer, then the kernel, in one command buffer: it reads the texture itself, // the fill read back, and the commit compares the copy. let kept = pages.bytes(); let before = device.work_counts(); - let fill = Some((buffer, 0..row * SIDE, 0x5a)); + let fill = Some((buffer, 0..offset + row * SIDE, 0x5a)); let status = invert_texels(device, &state, (texture, &out), fill); let after = device.work_counts(); assert_eq!(status, CommandBufferStatus::Completed); @@ -1004,7 +1182,7 @@ fn read_by_a_kernel( assert_eq!(after.read_twin_slot_reads - before.read_twin_slot_reads, 1); assert_eq!(compared(&before, &after), [0, 0, 0]); assert!( - square_texels(&out) == inverted(&kept, row), + square_texels(&out) == inverted(&texels(kept), row), "the pages read again" ); assert_eq!(after.read_twin_writes_refused, 0); @@ -1140,17 +1318,21 @@ fn reference(device: &Device, (width, height, row): (u64, u64, u64), bytes: &[u8 } /// A draw samples `texture` (`BGRA8Unorm`, `width` x `height`, rows `row` bytes apart over -/// `buffer`'s bytes, of `pages`), which took a read twin: through its twin, the commit comparing -/// nothing; after a fill of the buffer in the same command buffer, through the texture itself, -/// the fill read back; and the next command buffer through its twin again. +/// `buffer`'s bytes from `offset`, of `pages`), which took a read twin: through its twin, the +/// commit comparing nothing; after a fill of the buffer in the same command buffer, through the +/// texture itself, the fill read back; and the next command buffer through its twin again. fn sampled_in_place( device: &Device, (pages, buffer): (&Pages, &Buffer), texture: &Texture, - (width, height, row): (u64, u64, u64), + (offset, width, height, row): (u64, u64, u64, u64), ) { let target = draw_target(device); - let own = reference(device, (width, height, row), &pages.bytes()); + let own = reference( + device, + (width, height, row), + &pages.bytes()[offset as usize..], + ); let expected = sampled(device, &own, &target); let before = device.work_counts(); let drawn = sampled(device, texture, &target); @@ -1171,7 +1353,7 @@ fn sampled_in_place( let expected_filled = sampled(device, &own, &target); let kept = pages.bytes(); let before = device.work_counts(); - let fill = Some((buffer, 0..bytes, 0x5a)); + let fill = Some((buffer, 0..offset + bytes, 0x5a)); let status = draw_sampled(device, (texture, &target), fill); let after = device.work_counts(); assert_eq!(status, CommandBufferStatus::Completed); @@ -1208,7 +1390,12 @@ fn a_private_bit_buffer_s_texture_is_sampled_in_place_by_a_draw() { 1, "the texture's twin made, in place" ); - sampled_in_place(&device, (&pages, &buffer), &texture, (SIDE, SIDE, SIDE * 4)); + sampled_in_place( + &device, + (&pages, &buffer), + &texture, + (0, SIDE, SIDE, SIDE * 4), + ); drop((texture, buffer)); } diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index aa05178f7..a8e37a104 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -552,6 +552,12 @@ pub(super) struct TextureParts { /// of it binds them, not the shadow. `None` for every other texture, a view of one among /// them. rows_in_place: Option, + /// The copies of its read twin's shadow, held weakly, where the texture is a shadow over a + /// no-copy buffer whose GPU writes are dropped and its read twin is a shadow of the twin's + /// bytes (`read_twin.rs`): a slot that only reads the texture binds the twin in its place, + /// and the command copies the twin's shadow in as it copies a bound texture's own + /// (`shadow::shadow_uses_of`). Set once, as the twin is made; it goes with the twin. + twin_shadow: OnceLock>, } impl std::ops::Deref for TextureShared { @@ -5674,6 +5680,7 @@ impl Texture { facts: OnceLock::new(), planes_in_place, rows_in_place, + twin_shadow: OnceLock::new(), }), retain: weak, record, @@ -5978,6 +5985,26 @@ impl Texture { matches!(self.storage(), Storage::Alias { .. }) && self.linear_image().unwrap_or(false) } + /// Whether the texture is a shadow of its buffer's bytes now (`shadow.rs`): its own image, + /// copied in from the bytes and back, not an image over them. + pub(super) fn shadows_bytes(&self) -> bool { + matches!(self.storage(), Storage::Shadow { .. }) + } + + /// Keep `copies`, those of its read twin's shadow of the twin's bytes (`read_twin.rs`), held + /// weakly, so that a command that binds the twin in its place copies the twin's shadow in + /// ([`twin_shadow_copies`](Self::twin_shadow_copies)). + pub(super) fn keep_twin_shadow(&self, copies: &Arc) { + // Set once, as the twin is made with the texture. + let _ = self.s().twin_shadow.set(Arc::downgrade(copies)); + } + + /// The copies of its read twin's shadow (`read_twin.rs`), while the twin lives: the shadow a + /// slot that only reads the texture binds in its place. + pub(super) fn twin_shadow_copies(&self) -> Option> { + self.s().twin_shadow.get()?.upgrade() + } + /// Whether a pass that renders the texture renders its buffer's bytes: in place, its image /// lying over them now ([`image_over_buffer`](Self::image_over_buffer)), or through the /// shadow image the encoders copy them into and back (`shadow.rs`), which the pass renders. diff --git a/crates/metal2vulkan-runtime/src/texture/in_place_image.rs b/crates/metal2vulkan-runtime/src/texture/in_place_image.rs index 1f863969c..caa7d7516 100644 --- a/crates/metal2vulkan-runtime/src/texture/in_place_image.rs +++ b/crates/metal2vulkan-runtime/src/texture/in_place_image.rs @@ -660,6 +660,10 @@ impl Device { class: PagesClass, facts: &ImageFacts<'_>, ) -> Result, Error> { + #[cfg(test)] + if let Some(refused) = self.pitch_rule_forced(texels) { + return Ok(Err(refused)); + } match in_place_memory(texels, class)? { ModifierMemory::Own => self.copy_unplaced(plan, texels, facts), ModifierMemory::DmaBuf => self.dma_buf_unplaced(plan, texels, facts), @@ -670,6 +674,26 @@ impl Device { } } + /// A test's stand-in for a driver whose `DRM_FORMAT_MOD_LINEAR` plane takes rows and a + /// first byte only at multiples of `DeviceShared::linear_pitch_rule_forced` (RADV's 256): + /// the refusal of `texels` where they lie otherwise in their buffer. + #[cfg(test)] + fn pitch_rule_forced(&self, texels: &Texels) -> Option<(Refusal, String)> { + let rule = + (self.shared().linear_pitch_rule_forced).load(std::sync::atomic::Ordering::Relaxed); + let layout = texels.layout; + let lies_apart = + !layout.row_pitch.is_multiple_of(rule) || !layout.offset.is_multiple_of(rule); + (rule != 0 && lies_apart).then(|| { + let why = format!( + "a test refuses a plane at offset {} with rows {} bytes apart: the driver's rule \ + takes multiples of {rule}", + layout.offset, layout.row_pitch + ); + (Refusal::Pitch, why) + }) + } + /// [`unplaced`](Self::unplaced) over a copied host buffer's copy of the caller's pages /// (`copied.rs`), which the GPU uses in the stead of the pages, in memory the device /// allocates (a copy in fresh pages the runtime imported takes diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index ebc15636d..2a2ac0270 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -508,6 +508,7 @@ pub(crate) struct Counters { read_twin_writes_refused: AtomicU64, read_twin_textures_made: AtomicU64, read_twin_textures_not_in_place: AtomicU64, + read_twin_texture_shadows: AtomicU64, read_twin_slot_reads: AtomicU64, host_ram_copies: AtomicU64, host_ram_copy_bytes: AtomicU64, @@ -4643,7 +4644,7 @@ impl Counters { } /// A texture over a no-copy buffer with a read twin took a read twin of its own, in place - /// over the twin's bytes (`made`), or took none as the twin's texture would not lie in place + /// over the twin's bytes (`made`), or its twin's texture would not lie in place there /// (`read_twin.rs`). pub(crate) fn read_twin_texture(&self, made: bool) { match made { @@ -4653,6 +4654,12 @@ impl Counters { .fetch_add(1, Ordering::Relaxed); } + /// Of the textures whose read twin is not in place, one whose twin is kept as a shadow of + /// the twin's bytes, the texture being a shadow too (`read_twin.rs`). + pub(crate) fn read_twin_texture_shadow(&self) { + (self.read_twin_texture_shadows).fetch_add(1, Ordering::Relaxed); + } + /// Whether any buffer took a read twin (`read_twin.rs`), a texture's twin among them: else no /// command's slots look for one. pub(crate) fn read_twins_any(&self) -> bool { @@ -9364,10 +9371,15 @@ pub struct WorkCounts { /// a no-copy buffer with a read twin, the runtime's texture over the twin's bytes in place, /// which the slots that only read the texture bind instead. pub read_twin_textures_made: u64, - /// The textures over a no-copy buffer with a read twin that took no read twin because the - /// twin's texture would not lie in place over its bytes (a shadow, pending or refused by the - /// device's check of textures in place). + /// The textures over a no-copy buffer with a read twin whose twin's texture would not lie in + /// place over the twin's bytes (a shadow, pending or refused by the device's check of + /// textures in place). pub read_twin_textures_not_in_place: u64, + /// Of `read_twin_textures_not_in_place`, those whose twin is kept as a shadow of the twin's + /// bytes, the texture itself being a shadow (of its buffer's copy): the slots that only read + /// the texture bind it, copied in from the pages in place, where they copied the texture's + /// own shadow in from the copy, which their commits compared (`read_twin.rs`). + pub read_twin_texture_shadows: u64, /// The slot bindings made through a read twin, in place, one per slot and command (a draw or /// a dispatch): a texture slot's through its texture's twin, a buffer slot's through its /// buffer's. @@ -11885,6 +11897,7 @@ impl Device { read_twin_textures_made: counters.read_twin_textures_made.load(Ordering::Relaxed), read_twin_textures_not_in_place: (counters.read_twin_textures_not_in_place) .load(Ordering::Relaxed), + read_twin_texture_shadows: counters.read_twin_texture_shadows.load(Ordering::Relaxed), read_twin_slot_reads: counters.read_twin_slot_reads.load(Ordering::Relaxed), copied_base_host_bytes, copied_base_device_bytes, diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 1fa690cdd..50120c2b7 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 522; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2305; +pub const RUNTIME_VERSION: u32 = 2306; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 27b71d0b3..94693a099 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -3392,6 +3392,7 @@ field metal2vulkan-runtime::WorkCounts.rate_map_straddling_triangles pub rate_ma field metal2vulkan-runtime::WorkCounts.read_twin_blit_reads pub read_twin_blit_reads: u64 field metal2vulkan-runtime::WorkCounts.read_twin_reads_after_writes pub read_twin_reads_after_writes: u64 field metal2vulkan-runtime::WorkCounts.read_twin_slot_reads pub read_twin_slot_reads: u64 +field metal2vulkan-runtime::WorkCounts.read_twin_texture_shadows pub read_twin_texture_shadows: u64 field metal2vulkan-runtime::WorkCounts.read_twin_textures_made pub read_twin_textures_made: u64 field metal2vulkan-runtime::WorkCounts.read_twin_textures_not_in_place pub read_twin_textures_not_in_place: u64 field metal2vulkan-runtime::WorkCounts.read_twin_writes_refused pub read_twin_writes_refused: u64 diff --git a/docs/API.md b/docs/API.md index 929691983..b55d159c6 100644 --- a/docs/API.md +++ b/docs/API.md @@ -3813,7 +3813,12 @@ bound. `read_twin_reads_after_writes`, the copy). So does a texture slot of an encoder's own draw or dispatch that only reads a texture over the buffer's rows, through the texture's read twin over the twin's bytes, made with it where it lies in place (`read_twin_textures_made`, - `read_twin_textures_not_in_place`, `read_twin_slot_reads`), and a buffer slot of such a draw or + `read_twin_textures_not_in_place`, `read_twin_slot_reads`); where it lies in place over neither + the twin's bytes nor the copy (RADV lays no `DRM_FORMAT_MOD_LINEAR` plane whose rows or first + byte are off a multiple of 256), through the twin's shadow of the pages, copied in from them at + its first use in each command buffer as the texture's own shadow of the copy would be, only the + changed rows where the pages carry a log of their writes (`read_twin_texture_shadows`), and a + buffer slot of such a draw or dispatch that only reads the buffer, of a command that reaches no memory through a root table (`read_twin_slot_reads`). No command writes a twin, and one that would fails its command buffer (`read_twin_writes_refused`). On macOS the pages are imported through a read-only view of them. diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index 28e5d1f79..10cd9445e 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -1070,8 +1070,14 @@ driver's. Each is open work. and so does a texture slot of an encoder's own draw or dispatch that only reads the program's texture over the buffer's rows (every use the plan lists reads, every descriptor is that image), through the texture's own read twin over the twin's bytes, made with it where the device's check - of textures in place passed (`read_twin_textures_made`, `read_twin_slot_reads`), and a buffer - slot of such a draw or dispatch, of a command that reaches no memory through a root table, that + of textures in place passed (`read_twin_textures_made`, `read_twin_slot_reads`), or, where it + lies in place over neither the twin's bytes nor the copy (RADV lays no `DRM_FORMAT_MOD_LINEAR` + plane whose rows or first byte are off a multiple of 256), through the twin's shadow of the + pages (`read_twin_texture_shadows`): copied in from the pages in place at its first use in each + command buffer, where the texture's shadow of the copy was copied in from the copy, and only the + changed rows where the pages carry a log of their writes; it holds an image of the texture's + size while the texture lives, and on a device without unified memory both copies in read + system memory across the bus; and a buffer slot of such a draw or dispatch, of a command that reaches no memory through a root table, that only reads the buffer (`read_twin_slot_reads`); a command buffer that reads the buffer only so uses none of its copy and its commit compares none of it (`WorkCounts::read_twin_blit_reads`, `copied_copy_ins_compared`); after a command of its command buffer that may have written the