Files
metal2vulkan/crates/metal2vulkan-validation/src/executor_contract/sparse.rs
T
Anees Iqbal e6e70666ed Validation: delete the own-context arms of the nine refused families
What: since the refusal rows (d1266e5a5a, a1a47cb9c5), compute_case and render_case refuse a
case of kernel_function_table, kernel_imageblock, texture_buffer, fragment_imageblock,
sparse_texture, kernel_acceleration_structure, kernel_stage_input, rasterization_rate_map or
dynamic_vertex_stride before anything maps it. The executor's own-context code for these
families is reached by no case, so it goes:
- function tables: engine/tables.rs, ComputeCase.function_tables, StageResources.function_tables,
  CompiledCase (the compile functions now return the pipeline), CompileFailure::Tables,
  VariantRequest, the table allocations of compute_shape, and the tables hash of
  CaseArtifact::record;
- imageblocks and tile functions: engine/imageblock.rs, ComputeCase.imageblock,
  RenderCase.imageblock, RenderOutput::Imageblock, RenderFinals.imageblock,
  variants::compile_tile_for_shape, and the tile attachments of plan_resources;
- texture buffers: texel_guard_reach, buffer_target, the texel-buffer views and guards of
  plan_resources, and texel_buffer_bytes;
- sparse textures: platform/sparse_images.rs, device::SparseImages, the sparse paths of
  create_slot_image, the tile-map emulation of executor_contract::sparse, and the executor's
  sparseBinding, sparseResidencyImage2D and sparseResidencyImage3D enables (ExecutorFeature);
- acceleration structures: platform/acceleration.rs and their plan_resources arms;
- kernel stage inputs: the stage-input fetch, stage_in_region and Fetch::of_stage_input;
- rate maps: rate_map_regions, rate_map_pass_census and the per-region draw (a draw now covers the
  whole target in one recording);
- dynamic vertex strides: VertexBuffer.dynamic, DynamicRecord::Strides and
  bind_vertex_buffers2.
runtime_texture was always true after the rows and the cube-array route, so it goes too, and
runs_through_runtime and runs_render_through_runtime take the mapped case alone.
ExecutedCensus.rate_map_pass stays (deny_unknown_fields keeps old rows parsing); every writer
sets None. Enables' Member.registry is now read by the tests alone, so it is cfg(test).

ENGINE_EXECUTOR_ABI goes to vulkan-engine-v204: every artifact digest drops the tables hash, and
the own context no longer enables the three sparse members. opened_chains.txt is re-blessed for
that: each committed profile's chain has 3 members fewer.

Tests removed with the code they held: a_kernel_calls_through_its_visible_function_table,
a_tables_words_are_its_size_and_the_handles_of_its_functions, the_cells_follow_the_reflected_master,
a_plane_must_be_a_master_member_of_its_size, cells_lay_planes_out_at_master_offsets,
the_cells_are_capped, a_guarded_texture_buffer_is_exactly_its_view,
a_texture_buffer_written_past_its_end_is_guarded, texel_buffers_read_one_view_and_write_the_other,
a_tile_map_emulation_maps_exactly_the_cases_texels,
the_new_list_enables_sparse_images_where_the_device_supports_them,
an_oracle_instance_mask_is_refused_typed, a_rate_mapped_pass_is_planned_region_by_region,
a_dynamic_vertex_layout_takes_the_bound_stride and the region and pass tests of
platform/render (now a_draw_covers_the_whole_target). The visible_table fixtures stay: the
runtime's and the conformance suite's tests use them.

No classification moves: executor_contract/out_of_bounds.rs and sparse.rs lose only functions
nothing called. The compile census's executor key moves with the ABI (bless on development).

Verified: gate m3-max-63421 (homelab-um870-1) at this tree before the bless: fmt, validation
clippy with all features, with --cfg m2v_gpu and with --cfg m2v_cpu_icd, Rustdoc, the ignored
bless_opened_chains (its file is the one committed here), and the validation suite, 1342 passed,
3 skipped. Earlier gates m3-max-63393 and m3-max-63412 listed the compile errors fixed here.
2026-11-21 06:12:04 -08:00

438 lines
17 KiB
Rust

//! What the Metal oracle makes of a sparse texture's mappings.
//!
//! A case names its mapped texels ([`crate::case::sparse`]); the device divides the texture into
//! its own tiles, Metal's sparse tiles (`sparseTileSizeWithTextureType:...` at the heap's page
//! size), with a first level kept in a mip tail (`firstMipmapInTail`). The oracle makes each
//! mapping through [`device_mappings`] under its [`SparseTiling`], so a region that is not a whole
//! number of the device's tiles, or that covers part of a level in its mip tail, is refused before
//! anything runs. A case's mip tail is its slice's one level whole (`case::sparse`): a device that
//! keeps that level in its tail maps the tail, and one that does not maps the level's tiles; a
//! region covering a whole level in the tail maps the tail. On a device that makes every mapping,
//! the mapped texels are exactly the regions and tails the case names ([`mapped_texels`]), which is
//! what the oracle uploads ([`literal_offset`]). The engine's candidate runs no sparse texture
//! (`ToolingRequirement::SparseTexture`).
use crate::case::sparse::{sparse_extent, SparseMapping, LITERAL_LEVELS};
use crate::literal::LiteralTexture;
// A case's mip tail is its slice's one level whole (`case::sparse`): a literal with more levels
// needs the tail stated in texels.
const _: () = assert!(LITERAL_LEVELS == 1);
/// How one device divides a sparse texture.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SparseTiling {
/// A tile's extent in texels (x, y, z): Metal's sparse tile.
pub tile: [u32; 3],
/// The first mipmap level in the mip tail, or the texture's level count when it has no tail
/// (Metal's `firstMipmapInTail`).
pub first_level_in_tail: u32,
}
/// Texels `origin .. origin + size` of one level and slice.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TexelRegion {
pub level: u32,
pub slice: u32,
pub origin: [u32; 3],
pub size: [u32; 3],
}
/// One mapping of a sparse texture as a device makes it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DeviceMapping {
/// A region of whole tiles: its texels, and the same region in tiles (Metal maps a region in
/// tiles; Vulkan binds its texels, each block a whole one or one cut by the level's edge).
Tiles {
texels: TexelRegion,
tile_origin: [u32; 3],
tile_count: [u32; 3],
},
/// The mip tail of slice `slice`, which the device maps as one unit.
MipTail { slice: u32 },
}
/// The extent of level `level` of a texture whose level 0 is `extent`.
pub fn level_extent(extent: [u32; 3], level: u32) -> [u32; 3] {
extent.map(|axis| axis.checked_shr(level).unwrap_or(0).max(1))
}
/// `texture`'s level-0 extent and slice count, or why it is not a sparse texture of the case
/// model.
fn extent_of(texture: &LiteralTexture) -> Result<([u32; 3], u32), String> {
sparse_extent(texture.texture_type, texture.dimensions).ok_or_else(|| {
format!(
"texture {} is a {:?} texture, which the case model does not make sparse",
texture.binding, texture.texture_type
)
})
}
/// `texture`'s mappings as a device with `tiling` makes them, in the case's order, or why that
/// device cannot make one of them: a region that covers part of a level in the mip tail, or that
/// is not a whole number of tiles (its origin a multiple of the tile, its end a multiple of it or
/// the level's edge). A mip tail, and a region covering a whole level the device keeps in its
/// tail, map as the tail where the device keeps level 0 there (the tail is then exactly that one
/// level: the case model's textures have one), and a mip tail as level 0's tiles where it does
/// not. `Ok(None)` for a texture that is not sparse.
pub fn device_mappings(
texture: &LiteralTexture,
tiling: SparseTiling,
) -> Result<Option<Vec<DeviceMapping>>, String> {
let Some(sparse) = &texture.sparse else {
return Ok(None);
};
if tiling.tile.contains(&0) {
return Err(format!(
"texture {}: the device's sparse tile {:?} is empty",
texture.binding, tiling.tile
));
}
let (extent, _) = extent_of(texture)?;
sparse
.mappings
.iter()
.enumerate()
.map(|(index, mapping)| {
let at = format!("texture {} mapping {index}", texture.binding);
match *mapping {
SparseMapping::Region {
level,
slice,
origin,
size,
} => {
let edge = level_extent(extent, level);
if level >= tiling.first_level_in_tail {
// The tail is the levels from its first; a literal's is its one level.
let whole = origin == [0; 3] && size == edge;
let alone =
level == tiling.first_level_in_tail && level + 1 == LITERAL_LEVELS;
if whole && alone {
return Ok(DeviceMapping::MipTail { slice });
}
return Err(format!(
"{at}: texels {origin:?} + {size:?} of level {level} are part of the \
device's mip tail (from level {}), which maps only as a whole",
tiling.first_level_in_tail
));
}
let mut tile_origin = [0; 3];
let mut tile_count = [0; 3];
for axis in 0..3 {
let tile = tiling.tile[axis];
let end = u64::from(origin[axis]) + u64::from(size[axis]);
let whole = end % u64::from(tile) == 0 || end == u64::from(edge[axis]);
if size[axis] == 0 || end > u64::from(edge[axis]) {
return Err(format!(
"{at}: texels {origin:?} + {size:?} are not a region of level \
{level}'s {edge:?}"
));
}
if origin[axis] % tile != 0 || !whole {
return Err(format!(
"{at}: texels {origin:?} + {size:?} of level {level} are not \
whole {:?}-texel tiles of the device (each axis must start on a \
tile and end on one or at the level's edge {edge:?})",
tiling.tile
));
}
tile_origin[axis] = origin[axis] / tile;
tile_count[axis] = size[axis].div_ceil(tile);
}
Ok(DeviceMapping::Tiles {
texels: TexelRegion {
level,
slice,
origin,
size,
},
tile_origin,
tile_count,
})
}
// The case's tail is its one level whole: the device's tail where it keeps that
// level in one, else the level's tiles.
SparseMapping::MipTail { slice } if tiling.first_level_in_tail == 0 => {
Ok(DeviceMapping::MipTail { slice })
}
SparseMapping::MipTail { slice } => {
let size = level_extent(extent, 0);
let mut tile_count = [0; 3];
for axis in 0..3 {
tile_count[axis] = size[axis].div_ceil(tiling.tile[axis]);
}
Ok(DeviceMapping::Tiles {
texels: TexelRegion {
level: 0,
slice,
origin: [0; 3],
size,
},
tile_origin: [0; 3],
tile_count,
})
}
}
})
.collect::<Result<Vec<_>, String>>()
.map(Some)
}
/// The texels `mappings` (of `texture`, made under `tiling`) map, each one level of one slice: a
/// region's own texels, and each level of a slice's mip tail whole. What each executor uploads
/// of the authored bytes, in mapping order.
pub fn mapped_texels(
texture: &LiteralTexture,
tiling: SparseTiling,
mappings: &[DeviceMapping],
) -> Result<Vec<TexelRegion>, String> {
let (extent, _) = extent_of(texture)?;
let mut regions = Vec::new();
for mapping in mappings {
match *mapping {
DeviceMapping::Tiles { texels, .. } => regions.push(texels),
DeviceMapping::MipTail { slice } => {
regions.extend((tiling.first_level_in_tail..LITERAL_LEVELS).map(|level| {
TexelRegion {
level,
slice,
origin: [0; 3],
size: level_extent(extent, level),
}
}));
}
}
}
Ok(regions)
}
/// The byte offset in `texture`'s authored bytes of `region`'s first texel: the literal packs
/// level 0's slices one after another, each its planes (z), each its rows. A copy of the region
/// reads rows of the literal's width and images of its height from there.
pub fn literal_offset(texture: &LiteralTexture, region: &TexelRegion) -> Result<u64, String> {
let (extent, _) = extent_of(texture)?;
if region.level != 0 {
return Err(format!(
"texture {}: the literal holds level 0 only, not level {}",
texture.binding, region.level
));
}
if region.size.contains(&0) {
return Err(format!(
"texture {}: an empty region {region:?}",
texture.binding
));
}
let texel = texture.format.bytes_per_pixel() as u64;
let [width, height, depth] = extent.map(u64::from);
let [x, y, z] = region.origin.map(u64::from);
let offset = ((u64::from(region.slice) * depth + z) * height + y) * width * texel + x * texel;
let [w, h, d] = region.size.map(u64::from);
let last = ((u64::from(region.slice) * depth + z + d - 1) * height + y + h - 1) * width * texel
+ (x + w) * texel;
if last > texture.bytes.len() as u64 {
return Err(format!(
"texture {}: region {region:?} reaches byte {last} of {} authored bytes",
texture.binding,
texture.bytes.len()
));
}
Ok(offset)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::case::sparse::SparseTextureResource;
use crate::case::{ResourceRole, TextureFormat, TextureType};
fn sparse(
texture_type: TextureType,
dimensions: [u32; 3],
mappings: Vec<SparseMapping>,
) -> LiteralTexture {
let layout = crate::literal::texture_layout(texture_type, dimensions, 1).unwrap();
let texels = layout.width * layout.height * layout.depth * layout.array_layers;
LiteralTexture {
binding: 2,
role: ResourceRole::Input,
texture_type,
format: TextureFormat::R32Float,
dimensions,
sample_count: 1,
bytes: vec![0; 4 * texels as usize],
sparse: Some(SparseTextureResource { mappings }),
mip_levels: 1,
}
}
fn region(slice: u32, origin: [u32; 3], size: [u32; 3]) -> SparseMapping {
SparseMapping::Region {
level: 0,
slice,
origin,
size,
}
}
const NO_TAIL: u32 = LITERAL_LEVELS;
/// A region of whole tiles maps as those tiles on any device whose tile divides it; the last
/// tile of an axis may be cut by the level's edge, and the same region is refused where it is
/// not whole tiles (a 128-texel block cuts the 64-texel tiles a 16 KiB page makes).
#[test]
fn a_region_maps_as_whole_tiles_or_is_refused() {
let left = sparse(
TextureType::D2,
[256, 100, 1],
vec![region(0, [0, 0, 0], [128, 100, 1])],
);
let metal = SparseTiling {
tile: [64, 64, 1],
first_level_in_tail: NO_TAIL,
};
assert_eq!(
device_mappings(&left, metal).unwrap().unwrap(),
[DeviceMapping::Tiles {
texels: TexelRegion {
level: 0,
slice: 0,
origin: [0, 0, 0],
size: [128, 100, 1],
},
tile_origin: [0, 0, 0],
tile_count: [2, 2, 1],
}]
);
let vulkan = SparseTiling {
tile: [128, 128, 1],
first_level_in_tail: NO_TAIL,
};
assert!(device_mappings(&left, vulkan).unwrap().is_some());
let quarter = sparse(
TextureType::D2,
[256, 128, 1],
vec![region(0, [64, 0, 0], [64, 64, 1])],
);
assert!(device_mappings(&quarter, metal).unwrap().is_some());
let error = device_mappings(&quarter, vulkan).unwrap_err();
assert!(
error.contains("not whole [128, 128, 1]-texel tiles"),
"{error}"
);
// A region that ends short of a tile inside the level is not whole tiles either.
let short = sparse(
TextureType::D2,
[256, 128, 1],
vec![region(0, [0, 0, 0], [100, 128, 1])],
);
assert!(device_mappings(&short, metal).is_err());
}
/// A case's mip tail is its slice's one level whole on every device: where the device keeps
/// that level in its tail (Metal's 16 KiB page for a 16x16 texture), it maps the tail; where it
/// keeps no tail (lavapipe's 128x128 blocks), it maps the level's one block, cut by the level's
/// edge. The same texels either way. A region covering the whole level maps the tail too, and
/// one covering part of it is refused there.
#[test]
fn a_mip_tail_is_the_whole_level_on_every_device() {
let small = sparse(
TextureType::D2Array,
[16, 16, 2],
vec![SparseMapping::MipTail { slice: 1 }],
);
let tailed = SparseTiling {
tile: [64, 64, 1],
first_level_in_tail: 0,
};
let untailed = SparseTiling {
tile: [128, 128, 1],
first_level_in_tail: NO_TAIL,
};
let level = TexelRegion {
level: 0,
slice: 1,
origin: [0; 3],
size: [16, 16, 1],
};
let mappings = device_mappings(&small, tailed).unwrap().unwrap();
assert_eq!(mappings, [DeviceMapping::MipTail { slice: 1 }]);
assert_eq!(mapped_texels(&small, tailed, &mappings).unwrap(), [level]);
let mappings = device_mappings(&small, untailed).unwrap().unwrap();
assert_eq!(
mappings,
[DeviceMapping::Tiles {
texels: level,
tile_origin: [0; 3],
tile_count: [1, 1, 1],
}]
);
assert_eq!(mapped_texels(&small, untailed, &mappings).unwrap(), [level]);
let whole = sparse(
TextureType::D2Array,
[16, 16, 2],
vec![region(1, [0, 0, 0], [16, 16, 1])],
);
assert_eq!(
device_mappings(&whole, tailed).unwrap().unwrap(),
[DeviceMapping::MipTail { slice: 1 }]
);
assert!(matches!(
device_mappings(&whole, untailed).unwrap().unwrap()[..],
[DeviceMapping::Tiles { .. }]
));
let part = sparse(
TextureType::D2,
[16, 16, 1],
vec![region(0, [0, 0, 0], [16, 8, 1])],
);
let error = device_mappings(&part, tailed).unwrap_err();
assert!(error.contains("part of the device's mip tail"), "{error}");
assert!(device_mappings(&part, untailed).is_err());
}
/// The upload reads each region from the literal's packing: slices, then planes, then rows.
#[test]
fn a_region_is_copied_from_the_literals_packing() {
let array = sparse(TextureType::D2Array, [4, 3, 2], vec![]);
let at = |slice, origin| TexelRegion {
level: 0,
slice,
origin,
size: [1, 1, 1],
};
assert_eq!(literal_offset(&array, &at(0, [0, 0, 0])), Ok(0));
assert_eq!(
literal_offset(&array, &at(0, [1, 2, 0])),
Ok(4 * (2 * 4 + 1))
);
assert_eq!(
literal_offset(&array, &at(1, [3, 2, 0])),
Ok(4 * (12 + 2 * 4 + 3))
);
let volume = sparse(TextureType::D3, [4, 3, 2], vec![]);
assert_eq!(literal_offset(&volume, &at(0, [0, 1, 1])), Ok(4 * (12 + 4)));
let past = TexelRegion {
level: 0,
slice: 1,
origin: [0, 0, 0],
size: [4, 3, 1],
};
assert!(literal_offset(&volume, &past).is_err());
// An ordinary texture has no mappings.
let mut ordinary = sparse(TextureType::D2, [4, 4, 1], vec![]);
ordinary.sparse = None;
assert_eq!(
device_mappings(
&ordinary,
SparseTiling {
tile: [1, 1, 1],
first_level_in_tail: NO_TAIL
}
),
Ok(None)
);
}
}