diff --git a/crates/metal2vulkan-runtime/src/buffer_texture.rs b/crates/metal2vulkan-runtime/src/buffer_texture.rs index 13cd3fed3..10b68f581 100644 --- a/crates/metal2vulkan-runtime/src/buffer_texture.rs +++ b/crates/metal2vulkan-runtime/src/buffer_texture.rs @@ -1809,6 +1809,19 @@ impl Texels { Backing::of(&self.buffer, self.surface) } + /// Whether only the runtime's commands write the texels' bytes, as a shadow of them finds + /// (`ShadowCopies::only_commands_write`): a tracked texture's over a buffer of private or + /// memoryless storage the runtime allocated. A shadow of these may stand across command + /// buffers, copied in once (`shadow_standing.rs`); a shadow of others copies them in at every + /// command buffer that uses it, as the CPU may have written them. + pub(super) fn only_commands_write(&self) -> bool { + let private = matches!( + self.backing(), + Backing::PrivateBuffer | Backing::PrivateSurface + ); + self.tracked() && private + } + /// Where the CPU's `replace_region` and `get_bytes` find the texels. pub(super) fn cpu_layout(&self) -> BufferLayout { self.cpu diff --git a/crates/metal2vulkan-runtime/src/in_place.rs b/crates/metal2vulkan-runtime/src/in_place.rs index c32eeaa6d..c14bf48e6 100644 --- a/crates/metal2vulkan-runtime/src/in_place.rs +++ b/crates/metal2vulkan-runtime/src/in_place.rs @@ -537,8 +537,10 @@ pub(crate) enum ShadowCause { /// The device makes no image over its texels as Metal lays them out: why, and the words. Refused(Refusal, String), /// Over the runtime's pooled memory ([`PagesClass::Pooled`]), its rows to their pitch take - /// this many bytes, past [`POOLED_PITCHED_BYTES`]: a `DRM_FORMAT_MOD_LINEAR` image of them - /// renders and samples slower than the shadow's own image with its copies. + /// this many bytes, past [`POOLED_PITCHED_BYTES`], and it renders or is written, or it only + /// samples bytes only the runtime's commands write, which its shadow is copied in from once: a + /// `DRM_FORMAT_MOD_LINEAR` image of them renders and samples slower than the shadow's own + /// image with its copies. LinearCost(u64), } @@ -561,9 +563,19 @@ pub(crate) enum ShadowCause { /// 177.7/197.6, 198.3/306.7, 290.5/743.3; 250x250 at 1024 (256 KB) 187.8/190.1, 208.2/258.9, /// 242.0/430.8, 388.3/1114.9. A texture of up to 47 KB drawn at most once a frame runs faster in /// place; past that one draw a frame runs faster as a shadow, and four or more draws a frame do at -/// every size measured. A Radeon 780M (RADV) makes no such image at a pitch off a multiple of 256 -/// bytes, at which its own linear image lays rows, so no texture there takes one. The same bound -/// on every device. +/// every size measured. A texture that only samples, over bytes more than the runtime's commands +/// may write (a `Shared` or `Managed` buffer's, which the CPU writes, or an untracked texture's: +/// `Texels::only_commands_write`), takes the image at any size: its shadow is copied in whole at +/// every command buffer that samples it, which costs more than the linear image's sampling saves. +/// Measured the same way, +/// each frame a command buffer that samples each of four such textures whole after the CPU wrote +/// a texel of each (`runtime_lane::pooled_draw_costs::pooled_sample_costs`), a frame's GPU time +/// in microseconds as shadows at a padded pitch against linear images at the tight one, over +/// `Shared` buffers: 1308x512 (2.7 MB) 327.8/215.9, 1324x940 (5.0 MB) 802.2/380.1, 3648x2038 (30 +/// MB) 5789.3/2601.6; over `Private` ones, whose shadows stand copied in once, 237.7/216.2, +/// 404.4/409.4 and 2028.1/2433.2, so a texture there that only samples keeps the bound. A Radeon +/// 780M (RADV) makes no such image at a pitch off a multiple of 256 bytes, at which its own linear +/// image lays rows, so no texture there takes one. The same bound on every device. pub(crate) const POOLED_PITCHED_BYTES: u64 = 48 << 10; /// Why the device makes no image in place over a texture's texels. diff --git a/crates/metal2vulkan-runtime/src/tests/padding_unobserved.rs b/crates/metal2vulkan-runtime/src/tests/padding_unobserved.rs index 99792eebe..86c963b15 100644 --- a/crates/metal2vulkan-runtime/src/tests/padding_unobserved.rs +++ b/crates/metal2vulkan-runtime/src/tests/padding_unobserved.rs @@ -17,6 +17,12 @@ //! (`in_place::Access::UnobservedRenders`: its rows, each to its pitch, share no line with bytes //! past them), else a shadow; through the image, the texels the CPU wrote read back, a pass's //! clear reaches the buffer's rows, and the bytes after the last row keep the CPU's. +//! - **Past the pooled bound** (`in_place::POOLED_PITCHED_BYTES`). A texture that renders whose +//! rows to their pitch take more bytes takes no pitched image and, where one inside the bound +//! took one, answers `Shadow(LinearCost)`. One that only samples takes the pitched image over a +//! `Shared` buffer where the device's check of reads over its pooled memory passed, and never +//! answers `Shadow(LinearCost)` there; over a `Private` buffer it answers `Shadow(LinearCost)` +//! where the device makes such images. //! - **Undeclared.** The same texture over a buffer made without the declaration answers //! `Shadow(BytesBetweenRows)` and is a shadow (`shadowed_textures`), at every pitch. //! - **Over the caller's memory**, the declaration is refused: `Invalid` @@ -417,6 +423,97 @@ fn a_rendered_texture_past_the_pooled_bound_stays_a_shadow_of_its_linear_cost() } } +/// A texture of shader read usage alone past the pooled bound, 164x82 `BGRA8Unorm` at 704-byte +/// rows (57,728 bytes): over a `Shared` buffer, whose bytes the CPU writes, it takes a +/// `DRM_FORMAT_MOD_LINEAR` image at its pitch wherever the device makes one for a texture inside +/// the bound and its check of reads over its pooled memory passed, and is never a shadow of its +/// linear cost; over a `Private` buffer, whose shadow is copied in once, it stays a shadow of its +/// linear cost where the device makes such images. +#[test] +fn a_sampled_texture_past_the_pooled_bound_lies_in_place_over_bytes_the_cpu_writes() { + use crate::in_place::{Access, PagesClass, RowEnds, Verdict, POOLED_PITCHED_BYTES}; + let scratch = Scratch::new("padding-unobserved-sampled"); + let device = device(&scratch.0); + assert!( + device.settle_in_place_checks(CHECK_WAIT), + "the check settles" + ); + let [inside, (width, height, pitch)] = BOUNDED; + let bytes = pitch * height; + assert!(bytes > POOLED_PITCHED_BYTES); + // The texture made over a new buffer of `options` of `usage`: its answer, and whether it + // took a pitched image and was counted a shadow. + let made = |(width, height, pitch): (u64, u64, u64), options, usage| { + let descriptor = BufferDescriptor::new(pitch * height, options, BufferContents::Zero) + .with_padding_unobserved(); + let buffer = device.new_buffer_with_descriptor(descriptor).unwrap(); + let mut descriptor = + TextureDescriptor::texture_2d(PixelFormat::BGRA8Unorm, width, height, false); + descriptor.usage = usage; + descriptor.resource_options = buffer.resource_options(); + let answer = buffer.texture_placement(&descriptor, 0, pitch).unwrap(); + let before = device.work_counts(); + let texture = buffer.new_texture(&descriptor, 0, pitch).unwrap(); + let after = device.work_counts(); + let pitched = after.pooled_pitched_images - before.pooled_pitched_images == 1; + let shadow = after.shadowed_textures - before.shadowed_textures == 1; + (buffer, texture, answer, pitched, shadow) + }; + let rendered = TextureUsage::SHADER_READ | TextureUsage::RENDER_TARGET; + let (_, _, _, takes, _) = made(inside, ResourceOptions::shared(), rendered); + let read = TextureUsage::SHADER_READ; + let shared = made((width, height, pitch), ResourceOptions::shared(), read); + let private = made((width, height, pitch), ResourceOptions::private(), read); + // The check the texture over the `Shared` buffer asks: of reads, of rows that lie as its own. + let layout = crate::linear_plan::BufferLayout { + offset: 0, + row_pitch: pitch, + slice_pitch: pitch * height, + row_bytes: width * 4, + rows: height, + slices: 1, + }; + let line = device.shared().in_place.line(); + let start = shared.0.contents().unwrap().as_ptr().addr() as u64; + let rows = RowEnds::of(start, &layout, line); + let reads = device + .shared() + .in_place + .verdict(PagesClass::Pooled, Access::Reads, rows); + for (form, (_, texture, answer, pitched, shadow)) in + [("shared", &shared), ("private", &private)] + { + eprintln!( + "POOLED-SAMPLED on {}: {width}x{height} BGRA8Unorm at {pitch} ({bytes} bytes, the \ + bound {POOLED_PITCHED_BYTES}) sampled over a {form} buffer: answer {answer:?}; \ + pitched {pitched}, shadow {shadow}, aliases {}; inside the bound rendered pitched \ + {takes}; pooled reads verdict {reads:?} ({rows:?})", + device.name(), + texture.aliases_buffer(), + ); + } + let linear_cost = TexturePlacement::Shadow(TextureShadowReason::LinearCost { bytes }); + assert_ne!( + shared.2, linear_cost, + "over the Shared buffer: never its linear cost" + ); + if takes && reads == Verdict::Passed { + assert!(shared.3, "over the Shared buffer: a pitched image"); + assert_eq!( + shared.2, + TexturePlacement::NotHostPages, + "over the Shared buffer" + ); + } + if takes { + assert_eq!( + private.2, linear_cost, + "over the Private buffer: the answer" + ); + assert!(!private.3 && private.4, "over the Private buffer: a shadow"); + } +} + #[test] fn padding_unobserved_is_refused_over_the_callers_memory() { let scratch = Scratch::new("padding-unobserved-no-copy"); diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index a63a7ac5f..786820955 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -2925,7 +2925,13 @@ impl Device { if !in_place::checking() { let layout = &texels.layout; let bytes = layout.slice_pitch * (layout.slices - 1) + layout.row_pitch * layout.rows; - if bytes > in_place::POOLED_PITCHED_BYTES { + // The bound weighs a linear image against a shadow that renders into its own image, + // or samples it copied in once: a texture that only samples bytes more than the + // runtime's commands may write takes the image at any size, as its shadow would be + // copied in whole at every command buffer that samples it. + let sampled_apart = + in_place::Access::Reads.takes(usage) && !texels.only_commands_write(); + if bytes > in_place::POOLED_PITCHED_BYTES && !sampled_apart { return Ok(Err(ShadowCause::LinearCost(bytes))); } let line = self.shared().in_place.line(); diff --git a/crates/metal2vulkan-runtime/src/texture_placement.rs b/crates/metal2vulkan-runtime/src/texture_placement.rs index 7cdefd67f..0c1545135 100644 --- a/crates/metal2vulkan-runtime/src/texture_placement.rs +++ b/crates/metal2vulkan-runtime/src/texture_placement.rs @@ -125,7 +125,10 @@ pub enum TextureShadowReason { /// otherwise than Metal: the texture's rows take `bytes` bytes to their pitch, past the bound /// up to which it takes a `DRM_FORMAT_MOD_LINEAR` image at Metal's pitch /// ([`TexturePlacement::NotHostPages`]), as such an image renders and samples slower than the - /// shadow's own image with its copies past it. The same bound on every device. + /// shadow's own image with its copies past it. The same bound on every device. A texture that + /// only samples a `Shared` or `Managed` buffer's bytes, or an untracked texture that only + /// samples, takes the image at any size: its shadow would be copied in at every command + /// buffer. LinearCost { /// The bytes of the texture's rows, each to its pitch. bytes: u64, diff --git a/docs/API.md b/docs/API.md index e79b84e52..952e62c40 100644 --- a/docs/API.md +++ b/docs/API.md @@ -1625,7 +1625,10 @@ bound. change, as the declaration does, and no byte past the last row's pitch); past a bound in bytes of its rows to their pitch, the same on every device, such a texture stays a shadow too (`TextureShadowReason::LinearCost`): there a linear image renders and samples slower than the - shadow's own image with its copies, and over the device's own memory both give the same answer; + shadow's own image with its copies, and over the device's own memory both give the same answer + (a texture that only samples a `Shared` or `Managed` buffer's bytes, or an untracked one that + only samples, takes the image at any size, as its shadow would be copied in at every command + buffer); or the buffer is over the caller's `VkBuffer`, whose memory backs no image of the runtime's; or it is a tiled texture of more than one level (`Buffer::new_tiled_texture`), over any buffer: no image over the buffer's memory holds diff --git a/docs/HOWTO.md b/docs/HOWTO.md index cbecfb7af..43edee76a 100644 --- a/docs/HOWTO.md +++ b/docs/HOWTO.md @@ -347,7 +347,9 @@ the device makes `DRM_FORMAT_MOD_LINEAR` images (`VK_EXT_image_drm_format_modifi pitch where its in-place check passed for its pooled memory (for a texture that renders, the check of renders that lets a pass change those bytes, as the declaration does, and no byte past the last row's pitch) and its rows take no more bytes than a -bound past which such an image renders and samples slower than the shadow +bound past which such an image renders and samples slower than the shadow (a texture that only +samples a `Shared` or `Managed` buffer's bytes takes it at any size, as its shadow would be copied +in at every command buffer) (`WorkCounts::pooled_pitched_images`; `TextureShadowReason::LinearCost` past it; `HostPageTexturePolicy::Shadow` turns it off), its subresources holding each row's bytes after its texels to its pitch (the last row's included) and nothing past the last row's pitch; those bytes