Files
metal2vulkan/crates/metal2vulkan-runtime/src/tests/texture.rs
T
Anees Iqbal b7a62be64b Drop the exact tile store: a loaded snorm or RGB9E5Float attachment keeps the device's load and store
One work packet, landed as 7 commits (c18e40911e..53fcb867b4 on archive/development-linear-2026-10-28), in order:

* Drop the exact tile store: a loaded snorm or RGB9E5Float attachment keeps the device's load and store

The owner's ruling (2026-10-27): the GPU's native answers are the contract,
with no exact emulation. TileStorePolicy and DevicePolicy::tile_store go,
with the load pass's rewrite of a signed normalized attachment's minimum
codes and an RGB9E5Float attachment's unnormalized words, and the
unused-bits module's two rules that served only it. The load pass keeps the
extended-range, padded and bfloat attachments, whose raw codes no device
holds as Metal's (capability). snorm-minimum-kept and shared-exponent-kept
still announce the native answer at each such draw; DriverFact::TileStoreIsMetals
still keeps them quiet on MoltenVK.

Docs: the ledger row and docs/API.md's policy bullet go; the catalogue
entries and the notices' docs drop the switch. The validation lane tests the
one form.

Verified: cargo clippy --workspace --all-targets --all-features -D warnings;
nextest of bind::unused_bits, tests::fast_paths, tests::owned_divisions,
api_surface and the runtime's degradation and catalogue tests (49 passed).

* Drop the exact color resolve: the device's average resolves every attachment it can

The owner's ruling (2026-10-27): the GPU's native answers are the contract.
ResolvePolicy and DevicePolicy::resolve go, with the crate-owned resolve
drawn after the pass for a format of Metal's rules on a device whose average
is not measured to be Metal's. The device's AVERAGE in the pass is the one
form there, announced device-rounded-resolve as before. The crate-owned
resolve keeps the resolves no device's average can write: an extended-range
attachment's raw codes, a resolve into the attachment's format's sRGB twin
(VUID-VkSubpassDescription-pResolveAttachments-00850), and a fetching pass's
sample planes, read in place of merging them back.

Docs: the ledger row and docs/API.md's policy bullet go; the shader-resolve,
device-rounded-resolve and unmeasured-resolve entries describe what remains
(unmeasured-resolve's cost text with them).

Validation: the lanes ran under exact_answers, so their expectations follow
the device's resolve: resolve_kind is device-rounded-resolve wherever the
average is not measured to be Metal's; the exact form's tests
(color_resolves_against_metals_rules, the caller's-image copy path, its cost
scout) go; the Maps-form and probe-6 resolves hold Metal's texels where the
fact holds and the announced bound elsewhere; shader-resolve's lane site
moves to the extended-range resolve lane.

Verified: cargo clippy --workspace --all-targets --all-features -D warnings,
plain and under --cfg m2v_gpu; nextest of fast_paths, the degradation and
catalogue tests, the runtime's resolve, sample-plane, framebuffer-fetch and
stand-in tests, and api_surface (129 passed). The device lanes were not run.

* Drop the depth twin: every Depth32Float texture is its D32_SFLOAT depth image

The owner's ruling: promise the GPU's native arithmetic, no exact emulation.
A Depth32Float texture the program only reads was held in an R32_SFLOAT
image with a D32_SFLOAT twin for comparisons, on a driver not measured to
keep every float in a depth image (plan_depth_twin), and a multisampled one
in R32_SFLOAT under MultisampleDepthTwinPolicy::Exact. Both forms exist only
to keep Metal's texel bits; the native form is the depth image, whose
possible clamp or denormal flush the existing notices already announce at
the write (restricted-depth-upload, depth-denormals).

Removed:
- MultisampleDepthTwinPolicy and DevicePolicy::multisample_depth_twin, its
  FAST-PATHS row, its known-gap paragraph and its fast_paths choice.
- image_plan::plan_images, plan_depth_twin, holds_depth_in_colour,
  holds_multisample_depth_in_colour, ImageFacts::depth_loss and
  ImageFacts::multisample_depth_twin.
- The twin image of a texture (Image::twin), its copies in replace_region,
  blits and host copies, Owner::HeapTwin and the heap plan's twin (heap_extent
  now takes the image alone; check_heap_base the route alone; Mine is gone).
- The depth-twin degrade kind (Degradation::DepthTwin) and its docs/API.md
  catalogue entry.
- The bind side: TextureFacts::depth_twin, TextureShape::Texture::depth_twin,
  ImageViewDesc::twin and sampled_view's twin argument.
- A sparse Depth32Float texture is no longer refused for needing a twin.
- copy_unit refuses any depth format held in colour (none is now).

Tests: the twin tests are replaced by one that holds a Depth32Float texture
in its D32_SFLOAT image on every committed profile (single-sampled,
array, cube, render target, 2 and 4 samples); the heap and copy-plan unit
tests follow the one-image forms. The validation lanes that expected a twin
(texture_blits' upload lane, clear_reach, resolves' read-only depth pass,
texture_fills, heap_textures) now expect the depth image and its notices;
conformance's fill cases allow Depth32Float's digest unchecked where
restricted-depth-upload fires, as Depth32Float_Stencil8's already is.

RUNTIME_VERSION 1656: what a run of an unchanged pipeline does with such a
texture changes.

Verified: cargo clippy --workspace --all-targets --all-features -D warnings,
plain and with --cfg m2v_gpu; nextest of the translator and runtime
image_plan, heap, copy_plan, texture_facts, families, texture, degradation,
catalogue, fast_paths, profile, api_surface, bind and driver_facts tests
(973 passed). Device lanes not run.

* Drop the magnitude stand-in and the sign strip: a stored signed depth is tested natively

On the owner's ruling to promise the GPU's native arithmetic, a render target's
32-bit float depth that may hold a negative value or a NaN is no longer
switched on to render over a magnitude stand-in, and no pass strips the signs
of the words it did not write. The device's depth test compares the stored
value as it is and its pass keeps every bit; the runtime announces that
answer.

What goes:
- texture/signed_depth.rs, with the switch-on flags, the stand-in plans, the
  write-backs and the sign strips in render.rs and render_pass.rs;
- Cause::Magnitude in attachment_stand_in.rs;
- the translator's DepthMagnitudeModule (bind/depth_magnitude.rs) and the
  DepthStencilCopy::{DepthMagnitude, MagnitudeBits} draws;
- OwnedModule::DepthMagnitude;
- copy_plan's holds_unlike_its_stand_in;
- WorkCounts' signed_depth_switch_ons, magnitude_stand_in_passes,
  magnitude_write_backs and sign_strips (and their counters).

What stays, describing the native answer:
- signed-depth-test is announced where a write (a host upload, a blit, a copy,
  a written NaN clear, a new target's pattern loaded) may store such a value in
  a render target's Depth32Float or Depth32Float_Stencil8 depth on a device
  that keeps it and whose test is not Metal's (no
  DriverFact::DepthTestComparesMagnitudes);
- where the device's test is Metal's own (MoltenVK), signed-depth-test is
  announced only at a pass that loads and stores the depth and sets a state
  with depth writes but draws under none, which keeps the signs.

The denormal notice no longer mentions the stand-in.

Docs:
- FAST-PATHS drops "A stored negative or NaN depth is tested by its magnitude"
  and the matching known gap;
- FAST-PATHS also drops "A Depth16Unorm depth whose code the fragment stage
  writes is tested after the stage" (the shader write is the translator's to
  remove);
- API.md's depth-denormals and signed-depth-test entries now state the native
  answer;
- doc comments the earlier steps left past 100 columns are reflowed, and the
  remaining depth-twin mentions (bind docs, fast_paths' comment, three
  conformance known-gap todos) are dropped.

Validation:
- depth_denormals' lanes check every read, sample and comparison against
  Metal's, the depth tests only where the device's test is Metal's own, and
  the notices where they now fall;
- the sign-strip lane is deleted;
- texture_blits expects signed-depth-test at the render-target upload.

Verified: clippy with all targets and all features, plain and with
--cfg m2v_gpu, across the workspace; rustdoc with -D warnings; the runtime's
nextest suite (1150 passed); the translator's touched tests (fast_paths,
catalogue, degradation, api_surface, depth_stencil_copy, owned_divisions,
profile, driver_facts, bind, policy: 671 passed); validation and conformance
non-device tests in the touched areas (230 passed). Device lanes were not run.

* Re-bless the API signatures and regenerate the refusal surface

The lane's removals leave the public surface without:
- MultisampleDepthTwinPolicy, TileStorePolicy and ResolvePolicy, with their
  DevicePolicy fields;
- TextureFacts::depth_twin, TextureShape::Texture's depth_twin and
  ImageViewDesc::twin;
- DepthMagnitudeModule and depth_magnitude_module;
- DepthStencilCopy::{DepthMagnitude, MagnitudeBits};
- Degradation::DepthTwin;
- WorkCounts' signed_depth_switch_ons, magnitude_stand_in_passes,
  magnitude_write_backs and sign_strips.

The refusal surface loses four sites:
- the sparse depth-twin refusal (SparseHeaps, texture.rs plan_sparse_texture);
- the host heap's twin refusal (HeapTextureMemory, heap.rs host_heap_plan);
- the heap twin's DetachTextures site (heap.rs heap_texture_at);
- the sign strip's SeparateMultisampledDepthStencil site
  (signed_depth.rs sample_strip).

Verified: python3 scripts/api-signatures/signatures.py --bless and
python3 scripts/refusal-surface/refusal_surface.py, then the api_signatures,
refusal, api_surface, published_crates, fast_paths and degradation_catalogue
tests (81 passed).

* Format the policy import lists after the half switches' removal

The rebase onto development (where the half-precision lane removed three policies) kept both lanes' removals; cargo fmt re-flows the shortened lists.

Verified: clippy (workspace, all targets, all features) clean; 709 touched tests (fast_paths, api_surface, api_signatures, catalogue, degradation, profile, bind, image_plan, published_crates, policy) pass.

* Re-pin the runtime sources at the landing tip

The landed packet changed a runtime-sources input, so the committed digest no

longer described the sources and `the_runtime_sources_are_the_committed_ones`

would fail. Recomputed over the same inputs and committed by land-pkt.sh.
2026-10-28 17:24:20 +02:00

251 lines
10 KiB
Rust

//! The texture object's pure parts: image aspects, Metal regions in Vulkan's counts, the copy
//! regions of CPU copies, and the device's texture state.
use super::super::copy_plan::ImageRegion;
use super::super::texture::{
buffer_image_copy, image_aspects, narrow_range, texture_region, TextureState,
};
use super::super::values::Region;
use super::image_plan::committed;
use ash::vk;
use metal2vulkan::vk::{Format, ImageAspectFlags};
#[test]
fn images_carry_their_formats_aspects() {
use ImageAspectFlags as A;
assert_eq!(image_aspects(Format::R8G8B8A8_UNORM), A::COLOR);
assert_eq!(image_aspects(Format::BC1_RGBA_UNORM_BLOCK), A::COLOR);
assert_eq!(image_aspects(Format::D32_SFLOAT), A::DEPTH);
assert_eq!(image_aspects(Format::S8_UINT), A::STENCIL);
assert_eq!(
image_aspects(Format::D32_SFLOAT_S8_UINT),
A::DEPTH | A::STENCIL
);
}
#[test]
fn metal_regions_narrow_to_vulkans_counts_or_refuse() {
let r = texture_region(&Region::new_3d(1, 2, 3, 4, 5, 6), 7, 8).unwrap();
assert_eq!(
(r.level, r.slice, r.origin, r.size),
(7, 8, [1, 2, 3], [4, 5, 6])
);
assert!(texture_region(&Region::new_2d(1 << 32, 0, 1, 1), 0, 0).is_none());
assert!(texture_region(&Region::new_1d(0, 1), 0, 1 << 40).is_none());
assert_eq!(narrow_range(&(2..5)), Some(2..5));
assert_eq!(narrow_range(&(0..(1 << 33))), None);
}
#[test]
fn cpu_copies_read_a_tightly_packed_buffer() {
let copy = buffer_image_copy(&ImageRegion {
aspect: ImageAspectFlags::COLOR,
level: 2,
layer: 5,
layers: 1,
origin: [1, 2, 0],
size: [3, 4, 1],
});
assert_eq!(
(
copy.buffer_offset,
copy.buffer_row_length,
copy.buffer_image_height
),
(0, 0, 0)
);
let s = copy.image_subresource;
assert_eq!(
(
s.aspect_mask.as_raw(),
s.mip_level,
s.base_array_layer,
s.layer_count
),
(vk::ImageAspectFlags::COLOR.as_raw(), 2, 5, 1)
);
assert_eq!(
(
copy.image_offset.x,
copy.image_offset.y,
copy.image_offset.z
),
(1, 2, 0)
);
assert_eq!(
(
copy.image_extent.width,
copy.image_extent.height,
copy.image_extent.depth
),
(3, 4, 1)
);
}
#[test]
fn the_granularity_is_a_whole_power_of_two() {
let (_, desc, _) = committed().into_iter().next().unwrap();
assert_eq!(TextureState::new(&desc, 0, false).granularity(), 1);
assert_eq!(TextureState::new(&desc, 1024, false).granularity(), 1024);
assert_eq!(TextureState::new(&desc, 1000, false).granularity(), 1024);
}
#[test]
fn copy_units_are_the_images_aspects_blocks() {
use super::super::texture::copy_units;
use metal2vulkan::metal::PixelFormat;
use ImageAspectFlags as A;
assert_eq!(
copy_units(Format::R8G8B8A8_UNORM, PixelFormat::RGBA8Unorm),
vec![(A::COLOR, 4, [1, 1])]
);
assert_eq!(
copy_units(Format::BC1_RGBA_UNORM_BLOCK, PixelFormat::BC1RGBA),
vec![(A::COLOR, 8, [4, 4])]
);
assert_eq!(
copy_units(
Format::D32_SFLOAT_S8_UINT,
PixelFormat::Depth32FloatStencil8
),
vec![(A::DEPTH, 4, [1, 1]), (A::STENCIL, 1, [1, 1])]
);
assert_eq!(
copy_units(Format::D16_UNORM, PixelFormat::Depth16Unorm),
vec![(A::DEPTH, 2, [1, 1])]
);
}
/// Host copies reach a block format's image only where the driver's copies of such an image are
/// its own: the usage puts the image in host-visible memory, and MoltenVK copies the whole image
/// out of that memory as the memory is bound, through a pitch its own check then rejects
/// (`RuntimeDriverDefect::HostTransferImagesCopiedAtBind`; `texture/host_copy.rs` and
/// `driver_defects.rs` carry the backtrace). Every other driver keeps the fast path.
///
/// The row the image's own format gives is a row of **blocks**, which is the pitch that driver
/// calls short: the bytes a row of a 16 x 16 `BC1_RGBA` level holds are its 4 blocks of 8 bytes,
/// 32, not its 16 texels of 8, 128. The second half of this test holds the copy to that row, so a
/// pitch that ever came out of the texels instead would fail here with no device.
#[test]
fn host_copies_reach_a_block_format_only_off_the_bind_copy_defect() {
use super::super::texture::host_copy::reaches_format as reaches;
use super::super::texture::{copy_units, zero_copies};
use metal2vulkan::error::RuntimeDriverDefect as D;
use metal2vulkan::metal::PixelFormat as P;
let none = std::collections::BTreeSet::new();
// A driver with the defect, and one with another defect, so the test fails if the set is
// consulted for the wrong member.
let bind: std::collections::BTreeSet<D> = [D::HostTransferImagesCopiedAtBind].into();
let other: std::collections::BTreeSet<D> = [D::LazilyAllocatedSubresourceViews].into();
// Every format whose block holds one texel, including the uncompressed carriers that hold a
// compressed format's blocks in their texels: the image is the carrier's, and so is its row.
for format in [
Format::R8_UNORM,
Format::R8G8B8A8_UNORM,
Format::B8G8R8A8_SRGB,
Format::R32G32B32A32_UINT,
Format::R16G16B16A16_SFLOAT,
Format::D16_UNORM,
Format::D32_SFLOAT,
Format::S8_UINT,
Format::D32_SFLOAT_S8_UINT,
Format::A2B10G10R10_UNORM_PACK32,
// The registry records the IMG PVRTC formats as one texel in a block of 8 bytes, which no
// other compressed format is, so the rule here lets them through. Nothing reaches it: a
// PVRTC texture's image never takes host-transfer usage (`Device::new_image_starting`
// leaves it out by `is_pvrtc`), and on a device without the format its blocks lie in an
// uncompressed carrier whose own block is one texel.
Format::PVRTC1_4BPP_UNORM_BLOCK_IMG,
Format::PVRTC2_2BPP_SRGB_BLOCK_IMG,
] {
let block = format.block().expect("an image format has a block");
assert_eq!(block.texels, 1, "{format:?}");
for defects in [&none, &bind, &other] {
assert!(reaches(format, defects), "{format:?}");
}
}
// Every format whose block holds more than one texel, by family: BC, BC6H and BC7, ETC2 and
// EAC, ASTC. Each reaches host copies but on the one driver that copies such an image out of
// its memory as it is bound.
for format in [
Format::BC1_RGBA_UNORM_BLOCK,
Format::BC1_RGBA_SRGB_BLOCK,
Format::BC2_UNORM_BLOCK,
Format::BC3_SRGB_BLOCK,
Format::BC4_UNORM_BLOCK,
Format::BC4_SNORM_BLOCK,
Format::BC5_UNORM_BLOCK,
Format::BC5_SNORM_BLOCK,
Format::BC6H_SFLOAT_BLOCK,
Format::BC7_UNORM_BLOCK,
Format::ETC2_R8G8B8_UNORM_BLOCK,
Format::ETC2_R8G8B8A1_SRGB_BLOCK,
Format::EAC_R11_UNORM_BLOCK,
Format::EAC_R11G11_SNORM_BLOCK,
Format::ASTC_4X4_UNORM_BLOCK,
Format::ASTC_12X12_SRGB_BLOCK,
] {
let block = format.block().expect("an image format has a block");
assert!(block.texels > 1, "{format:?}");
assert!(reaches(format, &none), "{format:?}");
assert!(reaches(format, &other), "{format:?}");
assert!(!reaches(format, &bind), "{format:?}");
}
// A format no image has takes no host copies on any driver.
for defects in [&none, &bind, &other] {
assert!(!reaches(Format::UNDEFINED, defects));
}
// The row of a block format's level is its blocks, not its texels: the copy a host write of a
// 16 x 16 `BC1_RGBA` level reads is 4 blocks of 8 bytes a row, and the whole level 128 bytes.
let units = copy_units(Format::BC1_RGBA_UNORM_BLOCK, P::BC1RGBA);
assert_eq!(units, vec![(ImageAspectFlags::COLOR, 8, [4, 4])]);
let (length, copies) = zero_copies([16, 16, 1], 1, 1, false, &units, 4 << 20);
assert_eq!(copies.len(), 1);
assert_eq!(copies[0].size, [16, 16, 1]);
let row_blocks = 16u64.div_ceil(u64::from(units[0].2[0]));
assert_eq!(row_blocks * u64::from(units[0].1), 32);
assert_eq!(length, 32 * 4);
assert_ne!(length, 16 * 8 * 16);
// The copy is tightly packed, so the row length Vulkan is given is the level's own.
let copy = buffer_image_copy(&copies[0]);
assert_eq!((copy.buffer_row_length, copy.buffer_image_height), (0, 0));
}
#[test]
fn zero_copies_cover_every_texel_once_in_bands() {
use super::super::texture::zero_copies;
// A 2D array of 3 layers, 9x7 with 3 levels, of 4-byte texels, in bands of at most 2 rows
// (2 rows of 36 bytes fit 80 bytes, 3 do not).
let unit = [(ImageAspectFlags::COLOR, 4, [1, 1])];
let (length, copies) = zero_copies([9, 7, 1], 3, 3, false, &unit, 80);
assert_eq!(length, 72);
let mut covered = std::collections::BTreeMap::new();
for c in &copies {
assert_eq!(
(c.layers, c.origin[0], c.origin[2], c.size[2]),
(1, 0, 0, 1)
);
let [w, h, _] = [9u32, 7, 1].map(|a| (a >> c.level).max(1));
assert_eq!(c.size[0], w);
assert!(u64::from(c.size[0]) * 4 * u64::from(c.size[1]) <= length);
for y in c.origin[1]..c.origin[1] + c.size[1] {
assert!(y < h);
*covered.entry((c.level, c.layer, y)).or_insert(0) += 1;
}
}
let rows: u32 = (0..3).map(|l| (7u32 >> l).max(1)).sum();
assert_eq!(covered.len() as u32, rows * 3);
assert!(covered.values().all(|&n| n == 1));
// Compressed blocks: bands of block rows, a partial last block row reaching the edge.
let bc = [(ImageAspectFlags::COLOR, 8, [4, 4])];
let (length, copies) = zero_copies([10, 10, 1], 1, 1, false, &bc, 24);
assert_eq!(length, 24);
let bands: Vec<(u32, u32)> = copies.iter().map(|c| (c.origin[1], c.size[1])).collect();
assert_eq!(bands, vec![(0, 4), (4, 4), (8, 2)]);
// A 3D level: each depth slice; a row larger than the budget is one band of one row.
let (length, copies) = zero_copies([64, 2, 3], 1, 1, true, &unit, 16);
assert_eq!(length, 256);
assert_eq!(copies.len(), 6);
assert!(copies.iter().all(|c| c.size[1] == 1));
}