From 6717cac4ea8999e0bceb8a366129d38f4c44ffec Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 8 Nov 2026 02:42:33 -0800 Subject: [PATCH] Pooled class: a bound in bytes past which a texture over pooled memory stays a shadow (TextureShadowReason::LinearCost) Measured on the Iris Xe (probe 7933): a small cache's shadow costs a fixed 3.5-4.3 us a command buffer that writes it, which a linear image at Metal's pitch saves, while caches of 1.4 MB and more render and sample 8-18% slower as linear images than as shadows with their copies. Over Private memory both forms give the same answer (no CPU reads the bytes, the check judges the first move), so this chooses between two speeds and takes no FAST-PATHS row or switch. in_place::POOLED_PITCHED_BYTES bounds a texture's rows to their pitch, the same on every device; past it the cause is ShadowCause::LinearCost, which the placement query answers as the new public TextureShadowReason::LinearCost { bytes } through the shared route. docs/API.md (the padding-unobserved reason) and HOWTO say why. The bound's value here (256 KiB) is provisional: the size sweep on the Iris and the 780M places it at the measured crossover in a following commit. Verified: cargo fmt; api signatures re-blessed (one variant added); REFUSALS.md regenerates unchanged; builds follow. (cherry picked from commit 366e098c15c7227006ba7633ecda574da224690e) --- crates/metal2vulkan-runtime/src/in_place.rs | 23 +++++++++++++++++++ .../src/tests/in_place.rs | 4 ++++ .../src/tests/texture_placement_device.rs | 4 +++- crates/metal2vulkan-runtime/src/texture.rs | 11 +++++++-- .../src/texture_placement.rs | 10 ++++++++ .../metal2vulkan-runtime/src/work_counts.rs | 3 +++ crates/metal2vulkan/tests/api_signatures.tsv | 1 + docs/API.md | 6 ++++- docs/HOWTO.md | 4 +++- 9 files changed, 61 insertions(+), 5 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/in_place.rs b/crates/metal2vulkan-runtime/src/in_place.rs index 80abf62a0..034fc0eaf 100644 --- a/crates/metal2vulkan-runtime/src/in_place.rs +++ b/crates/metal2vulkan-runtime/src/in_place.rs @@ -486,8 +486,26 @@ pub(crate) enum ShadowCause { Unchecked(PagesClass, Access, RowEnds), /// 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. + LinearCost(u64), } +/// The most bytes (a texture's rows to their pitch) a texture over the runtime's pooled memory +/// takes a `DRM_FORMAT_MOD_LINEAR` image at Metal's pitch for ([`PagesClass::Pooled`]); past it +/// the texture is a shadow ([`ShadowCause::LinearCost`]). Over `Private` memory no CPU reads the +/// bytes and the device's check judges the image's first move, so both forms give the same +/// answer, and the bound chooses between two speeds: a shadow's copy per command buffer that +/// writes the texture costs a fixed few microseconds, which a small texture's linear image saves, +/// and a large texture's linear image renders and samples slower than the shadow's optimal one. +/// Measured on an Iris Xe (ANV, Mesa 26.2.6), each frame a pass that clears and stores every +/// texture and a draw that reads each whole: textures of 6-47 KB each saved 3.5-4.3 us a texture +/// a frame in place (15 of 172x67 at 704-byte rows: 425.6 us a frame as shadows, 369.6 in place), +/// and textures of 1.4 MB and more lost (4 of 820x410 at 3328: 564.1 us as shadows, 609.2 in +/// place; 2 of 1640x820 at 6592: 826.2 and 977.1). The same bound on every device. +pub(crate) const POOLED_PITCHED_BYTES: u64 = 256 << 10; + /// Why the device makes no image in place over a texture's texels. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum Refusal { @@ -575,6 +593,11 @@ impl ShadowCause { rows.words() ), ShadowCause::Refused(_, why) => why.clone(), + ShadowCause::LinearCost(bytes) => format!( + "its rows take {bytes} bytes to their pitch, past the {POOLED_PITCHED_BYTES} past \ + which a DRM_FORMAT_MOD_LINEAR image at Metal's pitch renders and samples slower \ + than the shadow's own image with its copies" + ), } } } diff --git a/crates/metal2vulkan-runtime/src/tests/in_place.rs b/crates/metal2vulkan-runtime/src/tests/in_place.rs index 5828e7fb8..f12694d06 100644 --- a/crates/metal2vulkan-runtime/src/tests/in_place.rs +++ b/crates/metal2vulkan-runtime/src/tests/in_place.rs @@ -254,6 +254,8 @@ fn every_shadow_cause_names_itself() { ShadowCause::CheckFailed(PagesClass::Pointer, Access::NarrowWrites, inside), ShadowCause::Unchecked(PagesClass::DmaBuf, Access::NarrowBufferWrites, on), ShadowCause::StoreWidth(3), + ShadowCause::Unchecked(PagesClass::Pooled, Access::Renders, inside), + ShadowCause::LinearCost(1 << 20), ]; let words: Vec = causes.iter().map(ShadowCause::words).collect(); for (at, word) in words.iter().enumerate() { @@ -275,6 +277,8 @@ fn every_shadow_cause_names_itself() { assert!(words[12].contains("share a line") && words[13].contains("share no line")); assert!(words[14].contains("over a buffer's rows, beside their padding")); assert!(words[15].contains("not answered") && words[15].contains("over a buffer's rows")); + assert!(words[19].contains("pooled memory")); + assert!(words[20].contains("1048576 bytes") && words[20].contains("slower")); } /// A driver identity, every field set. diff --git a/crates/metal2vulkan-runtime/src/tests/texture_placement_device.rs b/crates/metal2vulkan-runtime/src/tests/texture_placement_device.rs index 90311c615..cc84a179c 100644 --- a/crates/metal2vulkan-runtime/src/tests/texture_placement_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/texture_placement_device.rs @@ -84,7 +84,9 @@ fn expected(placement: TexturePlacement) -> Took { TexturePlacement::Shadow(R::Unchecked { .. }) => Took::Unchecked, TexturePlacement::Shadow(R::Refused(refusal)) => Took::Refused(refusal), TexturePlacement::Shadow(R::DeviceCopy) => Took::DeviceCopy, - TexturePlacement::Shadow(R::PastLength | R::Mipmapped { .. } | R::BytesBetweenRows) + TexturePlacement::Shadow( + R::PastLength | R::Mipmapped { .. } | R::BytesBetweenRows | R::LinearCost { .. }, + ) | TexturePlacement::NotHostPages | TexturePlacement::OtherShape => Took::Uncounted, } diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index 3c6c6dfbf..a63a7ac5f 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -2887,8 +2887,10 @@ impl Device { /// one plane at Metal's pitch (`VkImageDrmFormatModifierExplicitCreateInfoEXT`), no external /// memory (`dma_buf::ModifierMemory::Own`), unbound and made `UNDEFINED` /// (`Device::pitched_image`), for a texture of Metal `usage` whose texels the device's own - /// linear image lays out otherwise: where the device makes such images, and its policy and - /// its check of the pooled class ([`PagesClass::Pooled`]) take each access the usage takes + /// linear image lays out otherwise: where the device makes such images, the texture's rows to + /// their pitch take no more than `in_place::POOLED_PITCHED_BYTES` (past it, a linear image + /// renders and samples slower than the shadow), and its policy and its check of the pooled + /// class ([`PagesClass::Pooled`]) take each access the usage takes /// for texels that lie in its lines as these do (`in_place::in_place`; a pending verdict /// starts the check, and the texture is a shadow meanwhile); the check's own textures whatever /// the verdicts. Why not. @@ -2921,6 +2923,11 @@ 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 { + return Ok(Err(ShadowCause::LinearCost(bytes))); + } let line = self.shared().in_place.line(); let pages = ( PagesClass::Pooled, diff --git a/crates/metal2vulkan-runtime/src/texture_placement.rs b/crates/metal2vulkan-runtime/src/texture_placement.rs index ed0dda095..d5cf771e7 100644 --- a/crates/metal2vulkan-runtime/src/texture_placement.rs +++ b/crates/metal2vulkan-runtime/src/texture_placement.rs @@ -121,6 +121,15 @@ pub enum TextureShadowReason { /// rows the log reports changed (`WorkCounts::device_copy_textures`), the GPU's copy of the two /// a `Managed` resource is on a Mac with a discrete GPU. Not announced. DeviceCopy, + /// Over a buffer of the device's own memory whose rows the device's own linear image lays out + /// 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. + LinearCost { + /// The bytes of the texture's rows, each to its pitch. + bytes: u64, + }, } /// What a texture over host pages does with its image in place, each checked apart by the @@ -271,6 +280,7 @@ impl ShadowCause { ShadowCause::Refused(refusal, _) => { TextureShadowReason::Refused(InPlaceRefusal::of(refusal)) } + ShadowCause::LinearCost(bytes) => TextureShadowReason::LinearCost { bytes }, }; TexturePlacement::Shadow(reason) } diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index e81ded9fb..cfdd6acf2 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -3056,6 +3056,9 @@ impl Counters { C::CheckFailed(..) => &self.host_texture_shadows_check_failed, C::Unchecked(..) => &self.host_texture_shadows_unchecked, C::Refused(refusal, _) => &self.host_texture_shadows_refused[refusal.index()], + // A texture over the runtime's pooled memory, no texture over host pages: counted as + // a shadow (`shadowed_textures`) alone. + C::LinearCost(_) => return, }; counter.fetch_add(1, Ordering::Relaxed); if renders { diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 71fe42e8d..10d041f5d 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -12386,6 +12386,7 @@ variant metal2vulkan-runtime::TextureProblem::ZeroExtent ZeroExtent variant metal2vulkan-runtime::TextureShadowReason::BytesBetweenRows BytesBetweenRows variant metal2vulkan-runtime::TextureShadowReason::CheckFailed CheckFailed { access: InPlaceAccess, row_ends: InPlaceRowEnds } variant metal2vulkan-runtime::TextureShadowReason::DeviceCopy DeviceCopy +variant metal2vulkan-runtime::TextureShadowReason::LinearCost LinearCost { bytes: u64 } variant metal2vulkan-runtime::TextureShadowReason::Mipmapped Mipmapped { levels: u32 } variant metal2vulkan-runtime::TextureShadowReason::PastLength PastLength variant metal2vulkan-runtime::TextureShadowReason::Policy Policy diff --git a/docs/API.md b/docs/API.md index 3162f4c9c..6059c554a 100644 --- a/docs/API.md +++ b/docs/API.md @@ -1610,7 +1610,11 @@ bound. does in the buffer's memory, holding no byte past them, and no `DRM_FORMAT_MOD_LINEAR` image at Metal's pitch serves (the device makes none, its policy is `HostPageTexturePolicy::Shadow`, or its check of such images over its pooled memory has not passed for the texture's accesses: - pending, failed or unchecked, as over host pages); or the buffer is over the caller's + pending, failed or unchecked, as over host pages); 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; + 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 levels past the first, so one image of its own holds every level, its first level's texels diff --git a/docs/HOWTO.md b/docs/HOWTO.md index 25bbde3f1..bfa439559 100644 --- a/docs/HOWTO.md +++ b/docs/HOWTO.md @@ -344,7 +344,9 @@ rows of texels, say) may make it with `BufferDescriptor::with_padding_unobserved extension, no Metal counterpart, refused over the caller's memory (`NoCopy` contents). A texture over it is then a linear image at its pitch wherever the device lays one out so, or else, where the device makes `DRM_FORMAT_MOD_LINEAR` images (`VK_EXT_image_drm_format_modifier`), one at the -pitch where its in-place check passed for its pooled memory (`WorkCounts::pooled_pitched_images`; +pitch where its in-place check passed for its pooled memory and its rows take no more bytes than a +bound past which such an image renders and samples slower than the shadow +(`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 and the bytes between slices may change whenever the texture is made or used, and no other byte