diff --git a/crates/metal2vulkan-runtime/src/tests/linear_plan.rs b/crates/metal2vulkan-runtime/src/tests/linear_plan.rs index fba7e530b..eb932cd0d 100644 --- a/crates/metal2vulkan-runtime/src/tests/linear_plan.rs +++ b/crates/metal2vulkan-runtime/src/tests/linear_plan.rs @@ -186,7 +186,8 @@ fn buffers_back_exactly_the_formats_metal_answers_for() { // Probe 9 took none of the formats Metal makes no texture of alone (the 69 two-plane YCbCr // formats and three more): a buffer's texture of one is refused before this rule // (`texture_over`). It did take `R10Unorm_PACKED` and `RG10Unorm_PACKED`, which answer 16 for - // the alignment and nil for every texture, so they stay in the sweep. + // the alignment and whose texture over a buffer Metal does make + // (`metal::format::packed_codes`), so they stay in the sweep. let skipped = |pf: P| no_texture_alone(pf) && !matches!(pf, P::R10UnormPACKED | P::RG10UnormPACKED); let unprobed = P::ALL.iter().copied().filter(|&pf| skipped(pf)).count(); diff --git a/crates/metal2vulkan-runtime/src/tests/texture_descriptor.rs b/crates/metal2vulkan-runtime/src/tests/texture_descriptor.rs index 9c309192e..3cac18963 100644 --- a/crates/metal2vulkan-runtime/src/tests/texture_descriptor.rs +++ b/crates/metal2vulkan-runtime/src/tests/texture_descriptor.rs @@ -731,9 +731,11 @@ fn texture_types_follow_probe_5s_verdicts_per_format() { // Probe 5 took no format Metal makes no texture of from a descriptor alone, which // answers nil for every type here, but `AnyDepth` and `AnyStencil`, whose 1D, 1D array // and 3D textures Metal stops. `R10Unorm_PACKED` and `RG10Unorm_PACKED` are here too: - // Metal takes a descriptor naming either in every type and makes a texture of neither - // (an M3 Max under macOS 27.0.1, the `format-footprints-packed-without-textures` - // recording). + // Metal takes a descriptor naming either in every type and the **device's** creator, + // which these descriptors ask in `Shared` storage, makes a texture of neither (an M3 Max + // under macOS 27.0.1, the `format-footprints-packed-without-textures` recording). The + // creators that lay out no image storage of their own do make one, which + // `a_packed_ten_bit_descriptor_is_made_where_metal_lays_out_no_storage` holds. if no_texture_alone(pf) { let any_depth = matches!(pf, PixelFormat::AnyDepth | PixelFormat::AnyStencil); for ty in TYPES { @@ -1241,3 +1243,110 @@ fn two_plane_ycbcr_textures_follow_metal_s_rules() { ); } } + +/// A packed 10-bit format's descriptor is made from exactly the creators that lay out no image +/// storage of their own, and answers Metal's nil from the two that lay the image out themselves. +/// +/// Metal lays out no image storage for `R10Unorm_PACKED` or `RG10Unorm_PACKED` +/// (`metal::format::packed_codes`, which carries the measurement), so the rule is one rule and +/// not five special cases: a placement heap's offset, the shared creator's 16 bytes, a buffer's +/// or an IOSurface plane's own memory and `Memoryless` storage each need no layout and Metal +/// makes a texture of either format from them; the device's own creator and an automatic heap +/// would each have to lay it out, and Metal answers nil. The heap size query makes nothing, so it +/// keeps the nil and answers the footprint's 0 bytes. +/// +/// `check_made`'s `Ok` here is not a texture: the creator's own image or texel planning answers +/// `Unsupported(Format, NotImplemented)` after it, this crate holding no image of a packed code +/// stream. What this test pins is that the descriptor is no longer refused as **Metal's** nil +/// where Metal makes one. +/// +/// Every storage mode is asked of every creator, in two texture types, with `RGBA8Unorm` beside +/// each cell: a mode's or a type's refusal would show in the control first and the cell would say +/// nothing. +#[test] +fn a_packed_ten_bit_descriptor_is_made_where_metal_lays_out_no_storage() { + use PixelFormat as P; + let modes = [ + StorageMode::Shared, + StorageMode::Managed, + StorageMode::Private, + StorageMode::Memoryless, + ]; + let creators = [ + Creator::Device, + Creator::Placement, + Creator::Shared, + Creator::Buffer, + Creator::OverSurface, + Creator::SizeQuery, + ]; + // The cells plain Metal answered a texture in, one entry per (creator, storage) it was asked + // at: a placement heap and a buffer in either mode, the shared creator in `Private` (its other + // modes are nil for every format), and the device's own creator memoryless. A heap holds no + // memoryless texture and no buffer is memoryless, so those two cells are the rule carried + // where nothing was asked, as `no_storage_laid_out` says. + const MADE_WITHOUT_STORAGE: &[(Creator, StorageMode)] = &[ + (Creator::Placement, StorageMode::Shared), + (Creator::Placement, StorageMode::Managed), + (Creator::Placement, StorageMode::Private), + (Creator::Placement, StorageMode::Memoryless), + (Creator::Buffer, StorageMode::Shared), + (Creator::Buffer, StorageMode::Managed), + (Creator::Buffer, StorageMode::Private), + (Creator::Buffer, StorageMode::Memoryless), + (Creator::Shared, StorageMode::Private), + (Creator::Device, StorageMode::Memoryless), + ]; + let mut made = 0usize; + let mut nil = 0usize; + for ty in [TextureType::Type2D, TextureType::Type2DArray] { + for mode in modes { + let options = ResourceOptions::new( + mode, + CpuCacheMode::DefaultCache, + HazardTrackingMode::Default, + ); + for creator in creators { + let of_format = |pf| TextureDescriptor { + pixel_format: pf, + resource_options: options, + usage: TextureUsage::SHADER_READ, + ..of(ty, [8, 8, 1]) + }; + // The control: this creator, this mode and this type make an ordinary texture, so + // nothing below is a refusal of the cell's shape. + assert_eq!( + of_format(P::RGBA8Unorm) + .check_made(creator, APPLE) + .map(|_| ()), + Ok(()), + "the control at {creator:?} {mode:?} {ty:?}" + ); + for pf in [P::R10UnormPACKED, P::RG10UnormPACKED] { + // Metal's answer, read off the measurement rather than off the rule under + // test: the pairs below are the cells it made a texture in, and a rule that + // agreed with itself but not with them fails here. + let no_layout = MADE_WITHOUT_STORAGE.contains(&(creator, mode)); + let want = if no_layout { + Ok(()) + } else { + Err(Refused::Nil(TextureProblem::NoTextureOfFormat)) + }; + assert_eq!( + of_format(pf).check_made(creator, APPLE).map(|_| ()), + want, + "{pf:?} at {creator:?} {mode:?} {ty:?}" + ); + if no_layout { + made += 1; + } else { + nil += 1; + } + } + } + } + } + // 2 formats x 2 types x (a placement heap and a buffer in 4 modes, the shared creator in + // `Private`, the device's in `Memoryless`) made; the rest Metal's nil. + assert_eq!((made, nil), (40, 56)); +} diff --git a/crates/metal2vulkan-runtime/src/texture/heap.rs b/crates/metal2vulkan-runtime/src/texture/heap.rs index 0960a9ea7..f5defaa4a 100644 --- a/crates/metal2vulkan-runtime/src/texture/heap.rs +++ b/crates/metal2vulkan-runtime/src/texture/heap.rs @@ -2313,9 +2313,13 @@ impl Device { offset: Option, planned: &mut Option, ) -> Result { - let (call, heap_type) = match offset { - Some(_) => ("new_texture_at_offset", HeapType::Placement), - None => ("new_texture", HeapType::Automatic), + let (call, heap_type, creator) = match offset { + Some(_) => ( + "new_texture_at_offset", + HeapType::Placement, + Creator::Placement, + ), + None => ("new_texture", HeapType::Automatic, Creator::Device), }; let bytes = heap.check_type(call, heap_type)?.clone(); // A detach waits for none being made in the heap (`detach.rs`), nor does a rebind of it @@ -2324,7 +2328,7 @@ impl Device { // The descriptor first: what plain Metal answers nil for (a format of which it makes no // texture alone, `Unspecialized`) comes before the heap's storage mode and // `framebufferOnly`, which Metal stops for another format. - let layout = match self.heap_layout(call, descriptor, Creator::Device) { + let layout = match self.heap_layout(call, descriptor, creator) { // A placement heap makes a two-plane extended-range cube or cube array, which the // device and an automatic heap answer nil for: the runtime's gap. Err(Error::Nil(Nil { diff --git a/crates/metal2vulkan-runtime/src/texture_descriptor.rs b/crates/metal2vulkan-runtime/src/texture_descriptor.rs index 98ebe663e..e82153273 100644 --- a/crates/metal2vulkan-runtime/src/texture_descriptor.rs +++ b/crates/metal2vulkan-runtime/src/texture_descriptor.rs @@ -88,8 +88,15 @@ pub(super) const MAX_CUBE_ARRAY_LENGTH: u64 = MAX_ARRAY_LENGTH / 6; /// same, but for the order of a few and what each makes. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(super) enum Creator { - /// The device's or a heap's (and a sparse heap's, and a wrapped image's). + /// The device's, an automatic heap's, a sparse heap's, or a wrapped image's: the creators + /// that lay the image out themselves. Device, + /// A placement heap's `newTextureWithDescriptor:offset:`, which lays the texture at the + /// caller's offset. It takes every rule [`Creator::Device`] takes; the one difference + /// measured is a format Metal lays out no image storage for, which it makes here and answers + /// nil for from the device's own creator and from an automatic heap + /// (`metal::format::packed_codes`). + Placement, /// A buffer's, or an IOSurface plane's without its YCbCr facts: the linear creators, whose /// own rules stop a texture of more than one level (`linear_plan`). Buffer, @@ -527,15 +534,16 @@ impl TextureDescriptor { if odd_sided_chroma_pairs(pf, [w, h], ty, self.usage) { return Err(Refused::Nil(TextureProblem::ExtentForFormat)); } - // Plain Metal makes a memoryless texture of a format whose heap footprint counts no - // bytes, where `newTextureWithDescriptor:` answers nil for one of every mode that holds - // memory. The device's creation refuses it as this crate's own gap, holding no image of - // either format ([`made_only_memoryless`], `Device::new_texture`). The other creators - // keep the nil: the shared creator answers nil for every format outside `Private` - // storage, no buffer and no IOSurface plane is memoryless, and the heap size query - // answers 0 bytes for these formats (`metal::format::Footprint::NoTexture`). - let made_memoryless = memoryless && creator == Creator::Device && made_only_memoryless(pf); - if no_texture_alone(pf) && !(over_surface && ycbcr(pf)) && !made_memoryless { + // Metal lays out no image storage for a packed 10-bit format + // (`metal::format::packed_codes`), so it makes a texture of one from exactly the creators + // that need no image laid out and answers nil from the two that would lay it out + // themselves ([`no_storage_laid_out`]). Where Metal makes one, the answer past this carve + // is this crate's own `Unsupported(Format, NotImplemented)` out of the creator's image or + // texel planning, which is true of the format in every shape; Metal's nil would claim + // Metal refuses the descriptor, which the measurement shows it does not. + let storage = self.resource_options.storage_mode().ok(); + let no_storage = no_storage_laid_out(pf, creator, storage); + if no_texture_alone(pf) && !(over_surface && ycbcr(pf)) && !no_storage { return Err(Refused::Nil(TextureProblem::NoTextureOfFormat)); } // Plain Metal makes no `Unspecialized` texture and answers nil, whatever the storage @@ -547,7 +555,10 @@ impl TextureDescriptor { // answers nil for passed: a texture buffer of more than one level from the creators // that make one (a buffer's stops it, `linear_plan`), and a swizzle that writes. The // heap size query answers for both, as Metal's does. - let made_here = matches!(creator, Creator::Device | Creator::Shared); + let made_here = matches!( + creator, + Creator::Device | Creator::Placement | Creator::Shared + ); if ty == T::TextureBuffer && levels > 1 && made_here { return Err(Refused::NotYet(RuntimeGap::TextureBufferLevels)); } @@ -822,9 +833,10 @@ pub enum TextureProblem { /// answers **nil** for every one, while the heap size query answers 0 bytes /// (`metal::format::Footprint::NoTexture`; an Apple M3 Max under macOS 27.0.1, plain Metal, every /// type in `Shared` and `Private` storage, the `format-footprints-packed-without-textures` -/// recording). `Managed` storage answers nil with them, and `Memoryless` storage does **not**: it -/// answers a texture of both, which [`made_only_memoryless`] carries and `check_made` carves out -/// of this rule for the device's creator. +/// recording). `Managed` storage answers nil with them. The creators that lay out no image +/// storage of their own do **not**: a placement heap, the shared creator, a buffer, an IOSurface +/// plane and `Memoryless` storage each make a texture of both, which [`no_storage_laid_out`] +/// carries and `check_made` carves out of this rule. pub(super) fn no_texture_alone(pf: PixelFormat) -> bool { ycbcr_format(pf).is_some() || matches!(footprint(pf), Some(Footprint::NoTexture)) @@ -905,25 +917,52 @@ fn two_planes(pf: PixelFormat) -> bool { ExtendedRange::of(pf).is_some_and(|f| !f.holds_metal_bytes()) } -/// Whether plain Metal makes a texture of `pf` in `Memoryless` storage alone, answering nil from -/// `newTextureWithDescriptor:` in every mode that holds memory: the formats whose heap footprint -/// counts no bytes (`metal::format::Footprint::NoTexture`, `R10Unorm_PACKED` and -/// `RG10Unorm_PACKED`), whose packed 10-bit codes cross byte boundaries, so there is no element -/// size to count. +/// Whether plain Metal makes a texture of `pf` from `creator` in `storage` because it lays out no +/// image storage for the format and that creator needs none laid out +/// ([`Footprint::NoTexture`], whose whole membership they are: `R10Unorm_PACKED` and +/// `RG10Unorm_PACKED`, whose packed 10-bit codes cross byte boundaries, so the format has no +/// element size to lay out and Metal lays out none; `metal::format::packed_codes` states the +/// measurement). /// -/// Measured on an Apple M3 Max under macOS 27.0.1 (26A434), plain Metal, one cell per fresh -/// process and four processes a cell, with `RGBA8Unorm` and `R10Unorm_X6` made beside each cell in -/// the same mode and usage so that a mode's nil could not be read as a format's: for a 2D 8x8 -/// texture of either format, `Private`, `Shared` and `Managed` storage answered nil 4 of 4 under -/// each of `ShaderRead`, `ShaderRead | ShaderWrite`, `ShaderRead | RenderTarget` and -/// `RenderTarget` usage, and `Memoryless` storage answered a **texture** 4 of 4 under all four -/// (`conformance/probes/packed10_texture_alone.m`). The other texture types are not measured -/// memoryless; `check_made` carves them in too, because the answer past the carve is this crate's -/// own `Unsupported(Format, NotImplemented)`, which is true of the format in every type, where -/// the nil would claim Metal refuses the descriptor. -fn made_only_memoryless(pf: PixelFormat) -> bool { - use metal2vulkan::metal::format::Footprint; +/// `false` for every other format: this is the one family measured to divide the creators this +/// way, and the one whose heap footprint counts no bytes. +/// +/// Which creators, measured on an Apple M3 Max under macOS 27.0.1 (26A434), plain Metal with no +/// API validation layer, one cell per fresh process and four identical runs, with an +/// `RGBA8Unorm` control beside every cell so that a cell's nil could not be read as its storage +/// mode's (`metal::format::packed_codes` carries the figures): +/// +/// - A **placement** heap's `newTextureWithDescriptor:offset:` at offset 0 makes one in `Shared` +/// and in `Private` storage, `allocatedSize` 0; an **automatic** heap answers nil in both, as +/// the device's own creator does in `Shared`, `Managed` and `Private`. +/// - `newSharedTextureWithDescriptor:` makes one in `Private` storage, `allocatedSize` 16 where +/// the control's is 16528. It answers nil in `Shared` and `Managed` for the control too, which +/// is the storage mode's rule and not the format's, and `Device::new_shared_texture` answers +/// that nil before this descriptor is checked at all. +/// - A buffer's `newTextureWithDescriptor:offset:bytesPerRow:` makes one over the buffer's own +/// bytes in `Shared` and in `Private` storage, and an IOSurface plane's does too; the texture's +/// `allocatedSize` is the buffer's. A plane's YCbCr creator takes no such format +/// ([`Creator::OverSurface`]). +/// - `Memoryless` storage from the device's own creator makes one, where every mode that holds +/// memory answers nil: a 2D 8x8 texture of either format, 4 of 4 runs under each of +/// `ShaderRead`, `ShaderRead | ShaderWrite`, `ShaderRead | RenderTarget` and `RenderTarget` +/// usage (`conformance/probes/packed10_texture_alone.m`). A memoryless texture holds no memory +/// either, which is why it is the same rule and not a mode's exception. +/// - The heap size query answers 0 bytes rather than a texture, so it keeps the nil +/// (`Device::heap_texture_size_and_align` answers the footprint for it). +/// +/// Only a 2D texture is measured per creator, and only `Shared` and `Private` for the buffer's; +/// `check_made` carves every type and mode of a creator that makes one in together, because the +/// answer past the carve is this crate's own `Unsupported(Format, NotImplemented)`, which is true +/// of the format in every shape, where the nil would claim Metal refuses the descriptor. +fn no_storage_laid_out(pf: PixelFormat, creator: Creator, storage: Option) -> bool { matches!(footprint(pf), Some(Footprint::NoTexture)) + && match creator { + Creator::Device => storage == Some(StorageMode::Memoryless), + Creator::Shared => storage == Some(StorageMode::Private), + Creator::Placement | Creator::Buffer => true, + Creator::OverSurface | Creator::SizeQuery => false, + } } /// Whether `ty` in `pf` is a two-plane extended-range cube or cube array, which plain Metal makes diff --git a/crates/metal2vulkan-validation/build.rs b/crates/metal2vulkan-validation/build.rs index 84f7c4c91..a5e312e23 100644 --- a/crates/metal2vulkan-validation/build.rs +++ b/crates/metal2vulkan-validation/build.rs @@ -158,6 +158,7 @@ fn main() { "metal2vulkan/src/metal/format/extended_srgb.rs", "metal2vulkan/src/metal/format/footprint.rs", "metal2vulkan/src/metal/format/mod.rs", + "metal2vulkan/src/metal/format/packed_codes.rs", "metal2vulkan/src/metal/format/padded_srgb.rs", "metal2vulkan/src/metal/format/word.rs", "metal2vulkan/src/metal/generated.rs", diff --git a/crates/metal2vulkan-validation/runtime-sources.sha256 b/crates/metal2vulkan-validation/runtime-sources.sha256 index 9362967d3..032ed7cfd 100644 --- a/crates/metal2vulkan-validation/runtime-sources.sha256 +++ b/crates/metal2vulkan-validation/runtime-sources.sha256 @@ -1 +1 @@ -adc88681b722a4addf4675c1887d62c5e1026ccdd2f2a0cc2f01031952e38fcc +55858c5c48db8fd0f4f3d0a1386d70c7eb9e7e49e5a73a16b102fd7a9ce3fa3f diff --git a/crates/metal2vulkan-validation/src/runtime_lane/heap_textures.rs b/crates/metal2vulkan-validation/src/runtime_lane/heap_textures.rs index 765810999..2d95ddb7a 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/heap_textures.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/heap_textures.rs @@ -1286,9 +1286,11 @@ fn tiling_refusal(error: &Error) -> Option { /// (`TextureProblem::Unspecialized`, Metal's nil; an M4 under macOS 27.0.1, every /// usage). A format Metal makes no texture of from a descriptor alone, such as a two-plane YCbCr /// one, takes Metal's answers too: 0 bytes from the query, at 16 for a two-plane YCbCr 2D texture -/// and 128 for another, and nil from the call (`Error::Nil`, `TextureProblem::NoTextureOfFormat`); -/// so do `R10Unorm_PACKED` and `RG10Unorm_PACKED`, whose footprint is Metal's `NoTexture` shape, -/// at 16. +/// and 128 for another, and nil from the call (`Error::Nil`, `TextureProblem::NoTextureOfFormat`). +/// `R10Unorm_PACKED` and `RG10Unorm_PACKED` take the query's 0 bytes at 16 and the **gap's** +/// refusal from the call: plain Metal lays out no image storage for either, which is the 0 bytes, +/// and a placement heap of its makes a texture of either at offset 0 all the same +/// (`Footprint::NoTexture`), so a nil here would claim a refusal Metal does not make. /// A format whose heap footprint its own structure decides and of which this crate holds no image /// (`metal::format::footprint`) answers that footprint, non-zero, and refuses the call alone /// `NotImplemented(Format)`: on an Apple M3 Max under macOS 27.0.1 (26A434) plain Metal answers @@ -1380,15 +1382,13 @@ fn shared_heap_textures_are_made_or_refused_for_linear_tiling() { (Ok(answer), Err(error)) if error == no_texture => { let pf = descriptor.pixel_format; // The two-plane YCbCr formats, `RGBA16Float_XR`, `AnyDepth` and `AnyStencil` - // (`no_texture_alone`), and the two packed 10-bit formats, whose footprint is - // Metal's `NoTexture` shape: a packed 10-bit code crosses byte boundaries, so - // there is no element to count and Metal counts none. A **placement** heap of - // plain Metal's answers one of the packed pair a texture of 0 allocated bytes - // at offset 0 all the same, where its device and its automatic heap answer - // nil; this crate's nil here is its own gap, not Metal's answer. - let packed = footprint(pf) == Some(Footprint::NoTexture); - assert!(no_texture_alone(pf) || packed, "{what}: {error:?}"); - let rows = metal2vulkan::metal::format::ycbcr_format(pf).is_some() || packed; + // (`no_texture_alone`). The two packed 10-bit formats are **not** here: a + // placement heap of plain Metal's answers one of them a texture of 0 + // allocated bytes at offset 0, where the device's creator and an automatic + // heap answer nil, so this creator's refusal of them is this crate's own gap + // and reaches the arm below. + assert!(no_texture_alone(pf), "{what}: {error:?}"); + let rows = metal2vulkan::metal::format::ycbcr_format(pf).is_some(); let align = if rows { 16 } else { 128 }; assert_eq!((answer.size, answer.align), (0, align), "{what}") } @@ -1404,12 +1404,26 @@ fn shared_heap_textures_are_made_or_refused_for_linear_tiling() { let Some(shape) = footprint(pf) else { panic!("{what}: answered {answer:?} with no footprint of its own") }; - assert_ne!( - shape, - Footprint::NoTexture, - "{what}: a format Metal makes no texture of answered {answer:?}" - ); - assert!(answer.size > 0, "{what}: answered {answer:?}"); + // A packed 10-bit format is the one whose footprint is `NoTexture`, 0 bytes + // at 16: Metal lays out no image storage for it anywhere, which is why the + // query counts nothing, and a placement heap makes a texture of it over the + // caller's offset all the same (`Footprint::NoTexture`'s own docs). So its 0-byte + // answer stands beside the refusal here, where every other format of this arm + // answers a footprint of its own. + if shape == Footprint::NoTexture { + assert_eq!( + (answer.size, answer.align), + (0, 16), + "{what}: answered {answer:?}" + ); + } else { + assert_ne!( + shape, + Footprint::NoTexture, + "{what}: a format Metal makes no texture of answered {answer:?}" + ); + assert!(answer.size > 0, "{what}: answered {answer:?}"); + } no_image.push(format!("{what}: {answer:?}")); } (Ok(answer), Err(error)) => panic!("{what}: answered {answer:?}, made {error:?}"), diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index a4d9e00ac..ec8711dbe 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 = 450; /// 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 = 1625; +pub const RUNTIME_VERSION: u32 = 1627; /// 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/src/metal/format/footprint.rs b/crates/metal2vulkan/src/metal/format/footprint.rs index ea8075395..b13ba30e8 100644 --- a/crates/metal2vulkan/src/metal/format/footprint.rs +++ b/crates/metal2vulkan/src/metal/format/footprint.rs @@ -21,6 +21,7 @@ //! `tests::heap_footprint_formats` replays. use super::block_extent; +use super::packed_codes; use crate::metal::PixelFormat; /// The bytes of one ASTC block, whatever its texel extent. @@ -60,8 +61,18 @@ pub enum Footprint { /// already answers for the extended-range twins of it /// ([`ExtendedRange::Planes`](super::ExtendedRange::Planes)). TwoPlanes, - /// Metal describes the format, takes a descriptor naming it, and makes no texture of it in - /// any type: the query answers 0 bytes. + /// Metal describes the format, takes a descriptor naming it, and lays out no image storage + /// for it anywhere: the query answers 0 bytes. It does make a **texture** of such a format + /// from every creator that needs no image laid out — a placement heap's offset, the shared + /// creator, a buffer, an IOSurface plane and `Memoryless` storage — and answers nil from the + /// device's own creator and from an automatic heap, which would each have to lay the image + /// out. + /// + /// Its whole membership is `R10Unorm_PACKED` and `RG10Unorm_PACKED`, whose texels are a + /// stream of 10-bit codes three to a 32-bit word: a code crosses byte boundaries, so the + /// format has no element size to lay out and Metal lays out none. This crate holds no image + /// of such a stream, so a texture of one is refused `NotImplemented(Format)` from the + /// creators Metal makes it from, rather than answered Metal's nil. NoTexture, } @@ -138,13 +149,12 @@ pub fn footprint(pf: PixelFormat) -> Option { P::RGB10A82P | P::RGB10A82PSRGB | P::RGB10A82PPQ | P::RGB10A82PHLG => { Some(Footprint::TwoPlanes) } - // `R10Unorm_PACKED` and `RG10Unorm_PACKED`: Metal takes a descriptor naming either and - // makes no texture of it in any type in a mode that holds memory, so the query answers 0 - // bytes. A packed 10-bit code crosses byte boundaries, so there is no element size to count - // and Metal counts none. `Memoryless` storage is the one mode where - // `newTextureWithDescriptor:` answers a texture of either, which is why this crate's - // refusal there names the format and is its own gap rather than Metal's nil. - P::R10UnormPACKED | P::RG10UnormPACKED => Some(Footprint::NoTexture), + // `R10Unorm_PACKED` and `RG10Unorm_PACKED`: Metal lays out no image storage for either + // (`packed_codes`), so the query answers 0 bytes. A packed 10-bit code crosses byte + // boundaries, so there is no element size to count and Metal counts none. The creators + // that need no image laid out do make a texture of both, which is why this crate's + // refusal from each of them names the format and is its own gap rather than Metal's nil. + _ if packed_codes(pf) => Some(Footprint::NoTexture), _ => None, } } diff --git a/crates/metal2vulkan/src/metal/format/mod.rs b/crates/metal2vulkan/src/metal/format/mod.rs index 5de5e2691..4b3b03ac6 100644 --- a/crates/metal2vulkan/src/metal/format/mod.rs +++ b/crates/metal2vulkan/src/metal/format/mod.rs @@ -36,9 +36,11 @@ use crate::vk; pub(crate) mod extended_srgb; mod footprint; +mod packed_codes; pub(crate) mod padded_srgb; mod word; pub use footprint::{footprint, Footprint}; +pub(crate) use packed_codes::packed_codes; pub use word::{WordAttachment, WordField}; /// How a Metal pixel format is represented on a device, for a set of usages. diff --git a/crates/metal2vulkan/src/metal/format/packed_codes.rs b/crates/metal2vulkan/src/metal/format/packed_codes.rs new file mode 100644 index 000000000..7e1ea8fdb --- /dev/null +++ b/crates/metal2vulkan/src/metal/format/packed_codes.rs @@ -0,0 +1,108 @@ +//! The two formats whose texels are a stream of packed 10-bit codes: `R10Unorm_PACKED` (588) and +//! `RG10Unorm_PACKED` (589). +//! +//! A code crosses byte boundaries, so a texel has no whole number of bytes: the codes run three +//! to a little-endian 32-bit word, from the word's low bits up (bits 0-9, 10-19, 20-29, the top +//! two unread), and a texel takes **one** code of that stream for `R10Unorm_PACKED` and **two** +//! for `RG10Unorm_PACKED`. So three texels of the first lie in one word and three of the second in +//! two — 4/3 and 8/3 bytes a texel — and a row of either starts a fresh stream. +//! +//! **What the two anchors below fix, and what they do not.** The codes of texels 0 and 1 are +//! measured, and they are bits 0-9 and 10-19 of the row's first word, which a word of three codes +//! and an unpadded bit stream of codes agree on. The first texel that tells those two readings +//! apart is `R10Unorm_PACKED`'s texel 3 — the fourth code, at the second word's bit 0 under three +//! to a word and at the stream's bit 30 without the padding — and no code of ours is measured +//! there. So the two unread bits at the top of each word are this module's reading of the +//! measurement's own words ("three 10-bit codes per 32-bit word") and not a figure held to a +//! texel, and a row of four or more texels is where an implementation of the read has to be +//! measured before it is believed. +//! +//! **Metal lays out no image storage for them.** That one rule is what its creators show: every +//! creator that would have to lay the image out itself answers nil, and every creator that makes +//! one either holds nothing (0 or 16 bytes) or lies in memory the caller already owns. +//! [`packed_codes`] is that rule's structural test, and +//! [`Footprint::NoTexture`](super::Footprint::NoTexture) — which it is the whole membership of — +//! is how a caller outside this crate asks it, as well as being the 0 bytes the heap size query +//! counts for such a format. +//! +//! # Metal's answers +//! +//! Measured on an Apple M3 Max under macOS 27.0.1 (26A434), plain Metal with no API validation +//! layer, a 64 x 64 2D texture under `ShaderRead` usage, one cell per fresh process and four +//! identical runs, with an `RGBA8Unorm` control made beside every cell so that a cell's nil could +//! not be read as its storage mode's: +//! +//! | creator | storage | answer | +//! |---|---|---| +//! | `newTextureWithDescriptor:` | `Shared`, `Managed`, `Private` | nil | +//! | an **automatic** heap's `newTextureWithDescriptor:` | `Shared`, `Private` | nil | +//! | a **placement** heap's `newTextureWithDescriptor:offset:` at 0 | `Shared`, `Private` | a texture, `allocatedSize` **0** | +//! | `newSharedTextureWithDescriptor:` | `Private` | a texture, `allocatedSize` **16** (the control's 16528) | +//! | a buffer's `newTextureWithDescriptor:offset:bytesPerRow:` at 256 | `Shared`, `Private` | a texture, `allocatedSize` 65536, the buffer's own | +//! +//! `newSharedTextureWithDescriptor:` answers nil in `Shared` and `Managed` storage for these +//! formats and for the control alike, which is the storage mode's rule and says nothing about the +//! format. The shared creator's texture answers `iosurface` nil, `buffer` nil and a **non-nil** +//! `newSharedTextureHandle`, so its 16 bytes are not an IOSurface wrapper. +//! +//! `Memoryless` storage from the device's own creator answers a **texture** of both, where every +//! mode that holds memory answers nil (an earlier probe on the same venue: a 2D 8 x 8 texture, 4 +//! of 4 runs under each of `ShaderRead`, `ShaderRead | ShaderWrite`, +//! `ShaderRead | RenderTarget` and `RenderTarget` usage). A memoryless texture holds no memory +//! either, so it is the same rule. +//! +//! # What a made texture does +//! +//! Over a `Shared` buffer at 256 bytes a row, and over an IOSurface at 4 bytes a texel and 256 +//! bytes a row, each row filled with the byte pattern `(i ^ (i >> 8)) & 0xff` from the row's +//! start (row `r` at `r * 256`), on the same venue: +//! +//! - **A read and a sample are the code stream.** `R10Unorm_PACKED`'s texel 0 reads +//! `0x100 / 1023` (0.250244) out of the row's first word `0x03020100` and its texel 1 +//! `0x080 / 1023`, which is the word's bits 0-9 and 10-19; `RG10Unorm_PACKED` takes two codes +//! a texel from the same stream. `read` equals `sample` at every texel measured, and the +//! IOSurface's texture reads the buffer's value for value. +//! - **A blit moves 64 bytes a row** for **both** formats at width 64 (4096 bytes of 16384), the +//! source's own bytes. +//! - **`getBytes:` writes nothing**: 0 bytes of 16384, the destination's 0xEE fill left whole, +//! where the `RGBA8Unorm` control writes 16320. +//! - **A row pitch has a floor Metal asserts on**, its own validation with no layer: 64 bytes at +//! width 64 for `R10Unorm_PACKED` (63, 48, 32 and 16 each end the process) and 128 for +//! `RG10Unorm_PACKED`, so one and two bytes a texel. +//! - **A render pass into one hangs the GPU.** A pass into `R10Unorm_PACKED` over a buffer left +//! its command buffer in status 5 with `kIOGPUCommandBufferCallbackErrorHang`. No +//! `RG10Unorm_PACKED` and no IOSurface render cell was run, and none may be: the hang takes +//! unrelated work on the machine down with it. +//! +//! # Metal's own answers disagree with each other +//! +//! Two of those rows contradict Metal's other answers for the same format, which is Metal's +//! inconsistency and not a layout a rule could be fitted to: +//! +//! - A blit of `RG10Unorm_PACKED` moves 64 bytes a row where the same format's row pitch has to +//! be at least 128: Metal's copy and its own stride validation disagree about the format's +//! bytes a texel. +//! - `getBytes:` writes nothing for either format, where a blit of the same texture moves its +//! rows: Metal's CPU and GPU paths disagree about whether the format has a layout at all. +//! +//! Both floors are below the stream's own 4/3 and 8/3 bytes a texel, so Metal's stride validation +//! undercounts the layout its own reads use. + +use crate::metal::PixelFormat; + +/// Whether `pf` is one of the two packed 10-bit formats, for which Metal lays out no image +/// storage (the module docs): `R10Unorm_PACKED` and `RG10Unorm_PACKED`. +/// +/// This is the structural test a creator's rule asks: Metal makes a texture of such a format +/// exactly where the creator needs no image laid out — a placement heap's offset, a shared +/// texture of 16 bytes, a texture over the caller's buffer or IOSurface plane, and `Memoryless` +/// storage — and answers nil from the device's own creator and from an automatic heap, which +/// would each have to lay the image out. It is **not** support for the format: this crate holds +/// no image of a packed code stream, so a texture of one answers +/// `Unsupported(Format, NotImplemented)` from the creators Metal makes one from. +pub(crate) fn packed_codes(pf: PixelFormat) -> bool { + matches!( + pf, + PixelFormat::R10UnormPACKED | PixelFormat::RG10UnormPACKED + ) +}