From 508e9249cd7c047371bfa41bbbb1d70dd969555b Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 8 Nov 2026 18:33:47 -0800 Subject: [PATCH] clear_reach: a half NaN clear holds the device's NaN, announced, beside Metal's green What the device does: ANV on the Iris Xe (Mesa 26.2.6) stores the clear (NaN, 1.0, 0.5, NaN) of an RG16Float attachment as 0xfe00 in red, where Metal stores its canonical quiet NaN 0x7e00 (development's Iris leg, m3-max-10311 at 846f2112f5). Why the old expectation was wrong: it demanded 0x7e00 through an integer scratch clear the runtime no longer makes. Under the native-arithmetic rule the clear value goes to the device's conversion, and the runtime announces device-nan-encoding for every half NaN clear (nan_clear::degrade_nan_clears). The test now takes, in each texel the clear reaches, a half NaN of any encoding in red with Metal's 0x3c00 in green. Every other texel keeps its old bytes exactly, and the notice must be announced on every device. It prints how many texels hold the device's NaN (NAN-CLEAR). Verified: the Iris run follows. (cherry picked from commit 5c254fccdeefb567f1836f9d4831a058ad25166d) --- .../src/runtime_lane/clear_reach.rs | 53 +++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/crates/metal2vulkan-validation/src/runtime_lane/clear_reach.rs b/crates/metal2vulkan-validation/src/runtime_lane/clear_reach.rs index 64964fd96..6f98bea03 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/clear_reach.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/clear_reach.rs @@ -725,17 +725,31 @@ fn colors_whose_clears_reach_alike_record_inline_over_their_reach() { quiet(lane, &messages); } -/// A clear of a `half` NaN writes Metal's canonical quiet NaN (Metal probe case `nan_1_half_nan` on -/// the M3 Max and an M4: `0x7e00`) over the reach any clear has, through the scratch image of an -/// integer format the runtime clears it in (`nan_clear.rs`): to the tiles in a pass with a draw -/// call, over the whole attachment in one without, and through a stand-in. A 4-sample attachment's -/// clear runs valid (Metal's resolve of its NaN samples is unmeasured). +/// A clear of a `half` NaN reaches as any clear does: to the tiles in a pass with a draw call, over +/// the whole attachment in one without, and through a stand-in. Metal writes its canonical quiet +/// NaN (Metal probe case `nan_1_half_nan` on the M3 Max and an M4: `0x7e00`); the device converts +/// the clear value to the format's NaN as it likes, which the runtime announces as +/// `device-nan-encoding` on every device (`nan_clear::degrade_nan_clears`). So a cleared texel's +/// red holds a `half` NaN, Metal's or the device's (ANV on the Iris Xe, Mesa 26.2.6, stores +/// `0xfe00`), and its green, 1.0, Metal's `0x3c00` exactly; every texel past the reach keeps its +/// old bytes. A 4-sample attachment's clear runs valid (Metal's resolve of its NaN samples is +/// unmeasured). #[test] fn a_nan_clear_reaches_as_metal_s() { let (messages, _cache, lane) = lane(); let rg16 = PixelFormat::RG16Float; let pipeline = lane.pipeline(&[rg16], (None, None), 1); let nan = [0x00, 0x7e, 0x00, 0x3c]; + // A texel `got` holds Metal's `want`, or, where Metal's is the clear's, a `half` NaN of the + // device's encoding in red beside Metal's green. + let holds = |got: &[u8], want: &[u8]| { + got == want + || (want == nan + && super::driver_deviations::half_nan(u16::from_le_bytes([got[0], got[1]])) + && got[2..] == want[2..]) + }; + let _ = messages.take_notices(); + let mut device_nans = 0; let pass = |texture: &Texture| { let mut descriptor = color_pass(texture, LoadAction::Clear, [AREA, AREA]); descriptor.color_attachments[0].clear_color = ClearColor::new(f64::NAN, 1.0, 0.5, f64::NAN); @@ -764,11 +778,23 @@ fn a_nan_clear_reaches_as_metal_s() { for (name, usage, draw, reach) in cases { let texture = lane.old_color(rg16, PLAIN, usage); lane.run(&pass(&texture), draw, Some(&pipeline)); - assert_eq!( - lane.read(&texture, (0, 0), 4, BlitOption::None), - expected(SIDE, [AREA, AREA], reach, (None, &nan, &OLD)), - "{name}" + let got = lane.read(&texture, (0, 0), 4, BlitOption::None); + let want = expected(SIDE, [AREA, AREA], reach, (None, &nan, &OLD)); + assert_eq!(got.len(), want.len(), "{name}"); + let apart: Vec = (0..got.len() / 4) + .filter(|&t| !holds(&got[4 * t..4 * t + 4], &want[4 * t..4 * t + 4])) + .collect(); + assert!( + apart.is_empty(), + "{name}: {} texels hold neither Metal's nor a NaN of the device's beside Metal's \ + green, the first {:?} at texel {}", + apart.len(), + apart.first().map(|&t| &got[4 * t..4 * t + 4]), + apart.first().copied().unwrap_or(0) ); + device_nans += (0..got.len() / 4) + .filter(|&t| got[4 * t..4 * t + 4] != want[4 * t..4 * t + 4]) + .count(); } let four = Shape { samples: 4, @@ -776,6 +802,15 @@ fn a_nan_clear_reaches_as_metal_s() { }; let multisampled = lane.texture(rg16, four, TextureUsage::RENDER_TARGET); lane.run(&pass(&multisampled), Draw::None, None); + let announced = super::degrade_notices::counts(&messages.take_notices()); + eprintln!( + "NAN-CLEAR on {}: {device_nans} cleared texels hold the device's NaN, not Metal's 0x7e00", + lane.device.name() + ); + assert!( + announced.contains_key(&Degradation::DeviceNanEncoding), + "a half NaN clear announces device-nan-encoding: {announced:?}" + ); quiet(lane, &messages); }