From d158a960ddcfe2f8d1d4bcaa432299ad1bdaf9f6 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 7 Nov 2026 06:11:40 -0800 Subject: [PATCH] Read the sparse move rows through uncompressed textures The move family's rows (inside-the-level, past-the-destination's-row, clipped-source) read a tile a move rebound as 0 in 3,968 of its 4,096 words on Metal and whole on ours. probes/sparse_move_compression.m (job m3-max-3570, an M3 Max under macOS 27.0.1, five descriptors, two runs each, one outcome) shows that is lossless compression's: under the case's own descriptor (ShaderRead, allowGPUOptimizedContents YES) a moved tile kept its word in 128 texels, the first 4 by 2 of each 16 by 16 block, and with allowGPUOptimizedContents off, ShaderWrite or PixelFormatView usage both moved tiles read whole. Controls: untouched tiles whole, moved-from tiles zero, every command buffer completed, usedSize unchanged. A move is documented to move the tile's mapping, and the rows read which tiles it moved, so they now make both textures with allowGPUOptimizedContents off: - SparseTextureSpec gains gpu_optimized (Metal's default, true) and unoptimized(); both sides forward it to their descriptor. An unoptimized texture adds a byte to its fingerprint and a site suffix where the default adds none, so every other sparse case's recording keeps its fingerprint. - moved() builds its two textures unoptimized; the four move rows' recordings are re-taken on the M3 in the next commit. - docs/API.md's accepted differences gain the compressed case: Metal's moved tile may read only part of its texels, ours reads all of them under every descriptor; the runtime does not reproduce the loss. - The probe's header records its results. (cherry picked from commit b3f8f28dbf18ce6756587702dd012ebd282c4f3a) --- .../probes/sparse_move_compression.m | 17 ++++++++++++ .../src/cases/sparse_mappings.rs | 11 +++++++- .../src/metal_side.rs | 1 + .../src/runtime_side.rs | 1 + .../src/sparse_query.rs | 26 +++++++++++++++++-- docs/API.md | 8 ++++++ 6 files changed, 61 insertions(+), 3 deletions(-) diff --git a/crates/metal2vulkan-conformance/probes/sparse_move_compression.m b/crates/metal2vulkan-conformance/probes/sparse_move_compression.m index 2bcbd1775..ff0134059 100644 --- a/crates/metal2vulkan-conformance/probes/sparse_move_compression.m +++ b/crates/metal2vulkan-conformance/probes/sparse_move_compression.m @@ -40,6 +40,23 @@ // "$TMPDIR/sparse_move_compression" $v; "$TMPDIR/sparse_move_compression" $v; done // // Nothing here dispatches, draws or loops on a GPU; every region lies inside its level. +// +// RESULTS, an Apple M3 Max (`AGXG15CDevice`) under macOS 27.0.1, plain Metal, each variant run +// twice with one outcome both times: +// +// read B(3,3) holds A(0,0)'s word in 128 of its 4,096 texels (the first 4 +// by 2 of each 16 by 16 block) and 0 in the other 3,968; B(0,0) holds +// A(1,1)'s in all 4,096. +// read-unoptimized Both moved tiles hold their word in all 4,096 texels. +// read-write The same. +// read-write-unoptimized The same. +// read-view The same. +// +// In every variant A's two untouched tiles read their own words throughout, both moved-from tiles +// read 0 throughout, every command buffer completed with no error, and `usedSize` stayed 262,144 +// (16 pages) across the moves. So the partial read-back is lossless compression's: each switch that +// turns compression off, `allowGPUOptimizedContents = NO` included, lets a moved tile carry all of +// its texels. The conformance case's move rows make their textures with that switch off. #import #import diff --git a/crates/metal2vulkan-conformance/src/cases/sparse_mappings.rs b/crates/metal2vulkan-conformance/src/cases/sparse_mappings.rs index 56139f26a..c1d019924 100644 --- a/crates/metal2vulkan-conformance/src/cases/sparse_mappings.rs +++ b/crates/metal2vulkan-conformance/src/cases/sparse_mappings.rs @@ -746,6 +746,14 @@ fn tiles_of(rect: &TileRect) -> Vec<[u64; 2]> { /// reading after it a measurement: four tiles holding four distinct markers say the fills landed /// and say which tile each one is, so a later all-zero reading is a tile the move did not reach /// rather than a fill that never ran. +/// +/// Both textures are made with `allowGPUOptimizedContents` off. Under Metal's default this oracle +/// keeps such a shader-read texture losslessly compressed, and a tile a move rebound in one then +/// reads its own texels in only 128 of its 4,096 words and 0 in the rest, where every descriptor +/// that turns compression off reads all 4,096 (`probes/sparse_move_compression.m`). A move is +/// documented to move the tile's mapping, and the rows read which tiles it moved, so they read +/// them where compression's own state cannot hide one; docs/API.md's accepted differences say +/// what a compressed texture reads. fn moved( held: &[(usize, TileRect)], moves: MappingStep, @@ -796,7 +804,8 @@ fn moved( }); } steps.push(MappingStep::Commit); - program(vec![plain(), plain()], Vec::new(), 0, steps) + let texture = plain().unoptimized(); + program(vec![texture, texture], Vec::new(), 0, steps) } /// A move of `size` tiles at `from` of level 0 slice 0 of [`SOURCE`] onto `to` of [`DESTINATION`]. diff --git a/crates/metal2vulkan-conformance/src/metal_side.rs b/crates/metal2vulkan-conformance/src/metal_side.rs index 2c27e81ea..e1c350a97 100644 --- a/crates/metal2vulkan-conformance/src/metal_side.rs +++ b/crates/metal2vulkan-conformance/src/metal_side.rs @@ -7288,6 +7288,7 @@ fn sparse_texture_descriptor(spec: &SparseTextureSpec) -> Retained TextureDescriptor { TextureUsage::SHADER_READ }; descriptor.resource_options = ResourceOptions::private(); + descriptor.allow_gpu_optimized_contents = spec.gpu_optimized; descriptor } diff --git a/crates/metal2vulkan-conformance/src/sparse_query.rs b/crates/metal2vulkan-conformance/src/sparse_query.rs index 94798f777..31553d00b 100644 --- a/crates/metal2vulkan-conformance/src/sparse_query.rs +++ b/crates/metal2vulkan-conformance/src/sparse_query.rs @@ -221,6 +221,11 @@ pub struct SparseTextureSpec { /// Whether the texture is a render target as well as shader-read, which a multisampled sparse /// texture must be. pub render_target: bool, + /// `allowGPUOptimizedContents`: whether Metal may keep the texture losslessly compressed. + /// Metal's default, `YES`, lets it compress this shader-read texture, and a tile a move + /// rebound then reads back whole for only a fraction of its texels + /// (`probes/sparse_move_compression.m`), so the rows that read moved texels turn it off. + pub gpu_optimized: bool, } impl SparseTextureSpec { @@ -235,6 +240,7 @@ impl SparseTextureSpec { samples: 1, array_length: 1, render_target: false, + gpu_optimized: true, } } @@ -258,11 +264,18 @@ impl SparseTextureSpec { self } + /// The texture with `allowGPUOptimizedContents` off, which keeps Metal from compressing it. + #[must_use] + pub const fn unoptimized(mut self) -> Self { + self.gpu_optimized = false; + self + } + /// The site name the texture's answers sit at: every field that can change the tail /// arithmetic. pub fn site(&self) -> String { format!( - "{}/{:?}/{}x{}x{}/levels-{}/samples-{}/slices-{}{}", + "{}/{:?}/{}x{}x{}/levels-{}/samples-{}/slices-{}{}{}", self.format.name(), self.texture_type, self.size[0], @@ -272,10 +285,16 @@ impl SparseTextureSpec { self.samples, self.array_length, if self.render_target { "/target" } else { "" }, + if self.gpu_optimized { + "" + } else { + "/unoptimized" + }, ) } - /// The bytes the texture fingerprints as. + /// The bytes the texture fingerprints as. An unoptimized texture adds a byte where Metal's + /// default adds none, so the fingerprints taken before the field existed still hold. pub fn words(&self) -> Vec { let mut out = Vec::new(); out.extend(self.texture_type.as_raw().to_le_bytes()); @@ -287,6 +306,9 @@ impl SparseTextureSpec { out.extend(self.samples.to_le_bytes()); out.extend(self.array_length.to_le_bytes()); out.push(u8::from(self.render_target)); + if !self.gpu_optimized { + out.push(0xA0); + } out } } diff --git a/docs/API.md b/docs/API.md index 7ca8d3eda..90d61dc24 100644 --- a/docs/API.md +++ b/docs/API.md @@ -4243,6 +4243,14 @@ each item named states the rule in full. update_texture_mapping`), and leaves every texel as it was: Vulkan binds nothing past an image's layers (`VUID-VkSparseImageMemoryBindInfo-subresource-01723`), and which texels Metal's page reaches is not derivable from what was measured. +- **A sparse tile a move rebound reads all its texels, compressed or not.** Under Metal's default + `allowGPUOptimizedContents`, Metal may keep a sparse texture losslessly compressed, and a tile + `moveTextureMappingsFromTexture:…` rebound into one can then read its moved texels in only part + of the tile: on an Apple M3 Max under macOS 27.0.1 a `ShaderRead` `R32Float` tile held them in + 128 of its 4,096 texels (the first 4 by 2 of each 16 by 16 block) and 0 in the rest. With + `allowGPUOptimizedContents` off, `ShaderWrite` or `PixelFormatView` usage, Metal's moved tile + read all 4,096. The runtime's moved tile holds all of its texels under every descriptor; it + does not reproduce the loss. - **A heap's buffer and texture over the same bytes alias them, in the device's layout**, not Apple's tiled or compressed one.