diff --git a/crates/metal2vulkan-runtime/src/tests/heap_tiled.rs b/crates/metal2vulkan-runtime/src/tests/heap_tiled.rs index 3bed8e459..0f6961aa3 100644 --- a/crates/metal2vulkan-runtime/src/tests/heap_tiled.rs +++ b/crates/metal2vulkan-runtime/src/tests/heap_tiled.rs @@ -9,8 +9,8 @@ use crate::copy_plan::block_bytes; use crate::metal_level::MetalLevel; use crate::texture::heap_tiled::{ fresh_pattern, level_bytes, lossless_answer, metal_compresses, metal_heap_layout, - pattern_levels, sample_grid, shadow_tiles, BlockPlacement, Elements, FreshDepth, FreshPattern, - GpuRows, HeapLayout, TiledLayout, Untiled, + own_heap_layout, pattern_levels, sample_grid, shadow_tiles, BlockPlacement, Elements, + FreshDepth, FreshPattern, GpuRows, HeapLayout, TiledLayout, Untiled, }; use crate::texture_descriptor::TextureLayout; use metal2vulkan::metal::format::byte_format; @@ -1418,3 +1418,70 @@ fn pattern_levels_are_the_levels_of_16_or_more() { assert_eq!(got, levels, "{samples}x {w}x{h}"); } } + +/// The per-texture notice of a heap texture whose bytes the runtime holds Metal's layout of +/// itself (`own_heap_layout`, `texture/heap.rs`): a multisample texture whose samples Metal lays +/// out in the heap's bytes announces nothing where its image is a shadow of those tiles, and +/// `heap-texture-untiled-layout` where it lies in memory of its own apart from them, which holds +/// none of them; the uncompressed tiles of a texture Metal keeps losslessly compressed announce +/// `heap-texture-lossless-layout`, by its sample count; a layout that is not Metal's carries its +/// own kind; and a route of its own that is measured announces nothing. +#[test] +fn a_multisample_heap_texture_apart_from_metal_s_tiles_is_announced_untiled() { + use crate::degradation::Degradation as D; + const M4: MetalLevel = MetalLevel::MacOS27_0_1; + let read = TextureUsage::SHADER_READ; + let format = Format::R8G8B8A8_UNORM; + // A 4-sample texture of a sample grid of 8 x 8, under the 16 Metal compresses from: Metal + // keeps it uncompressed and lays its samples out in the heap's bytes. + let ms = texture( + T::Type2DMultisample, + PixelFormat::RGBA8Unorm, + [4, 4, 1], + (1, 1, 4), + ); + assert_eq!(sample_grid(4, [4, 4]), Some([8, 8])); + let metal = metal_heap_layout(&ms, read, true, format, M4); + assert!(matches!(metal.layout, HeapLayout::Tiled(_))); + // A shadow of Metal's own tiles holds the texture as Metal lays it out: no notice. + assert_eq!(own_heap_layout(&metal.layout, 4, false), None); + // Its image in memory of its own holds none of them: announced, as the layout Metal's tiles + // are not measured against is (`multisample_heap_layout`'s). + let untiled = own_heap_layout(&metal.layout, 4, true); + assert_eq!(untiled, Some(Untiled::SAMPLES_APART)); + assert_eq!( + untiled.map(|untiled| untiled.kind), + Some(D::HeapTextureUntiledLayout) + ); + // A layout that is not Metal's carries its own kind, wherever the image lies. + assert_eq!( + own_heap_layout(&HeapLayout::Untiled(Untiled::DRIVER), 4, false), + Some(Untiled::DRIVER) + ); + // A texture Metal keeps losslessly compressed: the uncompressed tiles a shadow holds it in, + // announced lossless by its sample count, wherever its image lies. + let big = texture( + T::Type2DMultisample, + PixelFormat::RGBA8Unorm, + [64, 64, 1], + (1, 1, 4), + ); + let compressed = metal_heap_layout(&big, read, true, format, M4); + let HeapLayout::Untiled(lossless) = compressed.layout else { + panic!("Metal keeps a 64 x 64 4-sample texture compressed"); + }; + assert_eq!(lossless, Untiled::LOSSLESS_MULTISAMPLE); + let tiles = shadow_tiles(&big, format, M4, &HeapLayout::Untiled(lossless)) + .expect("a compressed multisample texture has a shadow's uncompressed tiles"); + assert!(matches!(tiles, HeapLayout::Uncompressed(_))); + for apart in [false, true] { + assert_eq!( + own_heap_layout(&tiles, 4, apart), + Some(Untiled::LOSSLESS_SAMPLES) + ); + assert_eq!(own_heap_layout(&tiles, 1, apart), Some(Untiled::LOSSLESS)); + // A route of its own that is measured (a 4:2:2 texture's rows, a two-plane texture's + // planes) lays the texture out as Metal does: nothing to announce. + assert_eq!(own_heap_layout(&HeapLayout::Measured, 4, apart), None); + } +} diff --git a/crates/metal2vulkan-runtime/src/texture/heap.rs b/crates/metal2vulkan-runtime/src/texture/heap.rs index ae16fbb4a..0960a9ea7 100644 --- a/crates/metal2vulkan-runtime/src/texture/heap.rs +++ b/crates/metal2vulkan-runtime/src/texture/heap.rs @@ -207,8 +207,8 @@ use super::first_move::FirstMove; use super::heap_tiled::{ - footprint_size, metal_compresses_layout, metal_heap_layout, modelled_size, shadow_tiles, - HeapLayout, HeapTiledCopies, TiledTexture, Untiled, + footprint_size, metal_compresses_layout, metal_heap_layout, modelled_size, own_heap_layout, + shadow_tiles, HeapLayout, HeapTiledCopies, TiledTexture, Untiled, }; use super::heap_two_planes::{HeapPlaneCopies, TwoPlaneHeapLayout}; use super::stand_in::StandIn; @@ -3390,21 +3390,12 @@ impl Device { self.degrade_heap_layout(untiled, texture.object_id()); } } else { - match metal_layout { - HeapLayout::Tiled(_) if route == AliasRoute::Tiled => { - self.shared().counts.heap_texture_tiled(); - } - HeapLayout::Uncompressed(_) => { - let lossless = match p.layout.samples { - 1 => Untiled::LOSSLESS, - _ => Untiled::LOSSLESS_SAMPLES, - }; - self.degrade_heap_layout(lossless, texture.object_id()); - } - HeapLayout::Untiled(untiled) => { - self.degrade_heap_layout(untiled, texture.object_id()); - } - HeapLayout::Tiled(_) | HeapLayout::Measured => {} + if route == AliasRoute::Tiled && matches!(metal_layout, HeapLayout::Tiled(_)) { + self.shared().counts.heap_texture_tiled(); + } + let apart = route == AliasRoute::Lossless; + if let Some(untiled) = own_heap_layout(&metal_layout, p.layout.samples, apart) { + self.degrade_heap_layout(untiled, texture.object_id()); } } Ok(texture) diff --git a/crates/metal2vulkan-runtime/src/texture/heap_tiled.rs b/crates/metal2vulkan-runtime/src/texture/heap_tiled.rs index a5c279e6c..e5cafb7ee 100644 --- a/crates/metal2vulkan-runtime/src/texture/heap_tiled.rs +++ b/crates/metal2vulkan-runtime/src/texture/heap_tiled.rs @@ -982,6 +982,15 @@ impl Untiled { multisample texture's samples out in its tiles, which no driver fact measures as Metal's \ for it (DriverFact::MultisampleColorImagesLieAsMetalHeapTextures, of a colour one)", ); + /// A multisample texture Metal keeps uncompressed, whose samples it lays out in the heap's + /// bytes, held in memory of its own apart from them (`AliasRoute::Lossless`, `heap.rs`): its + /// image holds other bytes than Metal's samples, the device cannot move them to and from the + /// heap's bytes, or their layout is not measured (`heap-texture-untiled-layout`). + pub(crate) const SAMPLES_APART: Untiled = Untiled::other( + "Metal lays the multisample texture's samples out in the heap's bytes, which its image, \ + in memory of its own, does not hold: it holds other bytes than Metal's samples, the \ + device cannot move them to and from the heap's bytes, or their layout is not measured", + ); /// A block-compressed texture whose placement the device's Metal level does not measure /// (`heap-texture-block-compressed-layout`). const BLOCK_COMPRESSED: Untiled = Untiled { @@ -1004,16 +1013,6 @@ impl Untiled { /// The reason of a texture whose format's layout in Metal's heaps is not measured. pub(crate) const UNMEASURED_FORMAT: &str = "its format's layout in Metal's heaps is not measured"; -/// The reason of a multisample texture Metal keeps uncompressed whose image lies in memory of -/// its own apart from the heap's bytes (`heap.rs`): its image holds other bytes than Metal's -/// samples, the device cannot move them to and from the heap's bytes, or their layout is not -/// measured. -const MULTISAMPLE_SAMPLES: &str = "Metal lays the multisample texture's samples out in the heap's \ - bytes, which its image, in memory of its own, does not hold: \ - it holds other bytes than Metal's samples, the device cannot \ - move them to and from the heap's bytes, or their layout is \ - not measured"; - /// A texture's elements in Metal's tiles: one layout of all of them ([`TiledTexture::one`]), or a /// `Depth32Float_Stencil8` texture's two planes, each one aspect's elements laid out as a texture /// of that aspect's format and the texture's shape is (the module docs). @@ -1083,6 +1082,27 @@ impl HeapLayout { } } +/// The per-texture notice of a heap texture whose bytes the runtime holds Metal's layout of +/// itself (`heap.rs`): one in a heap over host pages, one whose image is a shadow of Metal's tiles +/// in the heap's bytes, and a multisample one whose image lies in memory of its own apart from +/// them (`apart`, `AliasRoute::Lossless`), where Metal lays it out as `metal` and it has `samples` +/// samples. `None` where its bytes are Metal's layout of its elements: a shadow of Metal's own +/// tiles, and a texture laid out by a measured route of its own ([`HeapLayout::Measured`]). +pub(crate) fn own_heap_layout(metal: &HeapLayout, samples: u32, apart: bool) -> Option { + match metal { + // Its image holds the uncompressed tiles of a texture Metal keeps losslessly compressed. + HeapLayout::Uncompressed(_) => Some(match samples { + 1 => Untiled::LOSSLESS, + _ => Untiled::LOSSLESS_SAMPLES, + }), + HeapLayout::Untiled(untiled) => Some(*untiled), + // Metal lays its samples out in the heap's bytes, which its image, in memory of its own, + // does not hold: the heap's bytes keep what they held. + HeapLayout::Tiled(_) if apart => Some(Untiled::SAMPLES_APART), + HeapLayout::Tiled(_) | HeapLayout::Measured => None, + } +} + /// The layout a shadow of the heap's bytes holds a texture of `layout` (its image of `format`, at /// the Metal level `level`) in, where Metal lays it out as `metal`: Metal's tiles where it tiles /// the texture ([`HeapLayout::Tiled`]); where it keeps the texture losslessly compressed @@ -1399,7 +1419,7 @@ fn multisample_heap_layout( let samples = layout.samples; let apart = |answer| MetalHeapLayout { answer, - layout: HeapLayout::Untiled(Untiled::other(MULTISAMPLE_SAMPLES)), + layout: HeapLayout::Untiled(Untiled::SAMPLES_APART), }; let Some(grid) = sample_grid(samples, [width, height]) else { return apart(None); diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 39f0f48c2..39951bfbd 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -93,7 +93,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 449; /// 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 = 1597; +pub const RUNTIME_VERSION: u32 = 1598; /// 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