From 2ef8af3abe5b9d3e347cb53fcc83393455e8a0bb Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 9 Nov 2026 07:53:08 -0800 Subject: [PATCH] A pass-start read takes the copy cleared by MoltenVK's clear command, which is Metal's clear What: on MoltenVK (DriverFact::ColorClearIsMetals) a draw that samples a colour its pass clears, after a memory barrier at the pass's start, split the pass again (attachment_read_splits, a second render pass instance, the render-pass-split notice) instead of reading a copy of the attachment cleared to the clear before the pass (attachment_read_clears). Texture::cleared_copy refused a clear whose device-color-conversion note the load operation withholds (ColorTarget::withheld_clear), because the copy is cleared by vkCmdClearColorImage and only the load operation was measured to store Metal's texel there. Since 876cfd1402 every clear of a normalized channel off a whole code is in question (the spec's rule), so the pass-start clears of the lanes and the conformance cases became withheld on MoltenVK and split. The driver's clear command is measured to store Metal's texel too, so the fact now covers it: - Texture::cleared_copy takes a withheld clear (no note owed: the clear command's texel is the load operation's, Metal's). - A memoryless attachment's backing cleared after the first instance, and an attachment the first instance leaves out cleared before the pass by the clear command (cleared_ahead), owe no note either; the same clear by a render pass of the attachment alone, or as sample planes, still announces it. - DriverFact::ColorClearIsMetals, ColorTarget::withheld_clear, texture/attached_reads.rs and docs/API.md's device-color-conversion entry say so. Elsewhere withheld_clear is None (the note is announced as the pass is laid out), so nothing changes on other drivers. Why, measured on the M3 Max (macOS 27.0.1, MoltenVK 1.4.2): - The cause, by a probe of each condition of cleared_copy over 22,482 runs of attachment_reads::a_draw_after_a_barrier_at_the_pass_start_reads_the_clear on development at 1571c220d7: every run failed the same way, (cleared copies, splits, begun) = (0, 1, 2), with withheld_clear Some([200, 40, 120, 160] / 255) the only condition that refused (not load, not timing). - The clear command: each of the 71 colour clears Metal was measured with (renders.rs COLOR_CLEARS: ties of 8-, 10- and 16-bit unorm and snorm channels, sRGB, packed 16-bit, half and small floats, shared exponents, NaNs) by a plain fill of a whole 1x1 texture, which is vkCmdClearColorImage of the load operation's clear value (blit_fill.rs): 71 of 71 Metal's bytes, and equal to the load operation's 71. runtime_lane::renders::clears_are_the_device_s_conversion_of_the_clear_value now checks this on a driver with the fact. Ran on the M3 Max (macOS 27.0.1, MoltenVK 1.4.2) at this change over 693fd3f96d, under --cfg m2v_gpu: the whole validation suite, 3398 of 3400 passed (attachment_reads and renders among them); the two failures are the runtime-sources pin, which the landing sets, and degrade_notices::a_creation_s_notices_and_messages_reach_the_sink_on_the_asking_thread at device creation (creation_heard_checked), before any pass is recorded. conformance cost --check over the whole set: no gated count and no ceiling moved (the pass-start-barrier cases read the cleared copy again, as recorded); conformance replay of the 14 pass-start-barrier cases: rc 0, each difference an accepted one. On this base only driver_facts.rs merged (beside ClearedDepthAtStandardPositions); the other five files are unchanged since 693fd3f96d. RUNTIME: the runtime's pass-start reads on MoltenVK change (a cleared copy instead of a split). (cherry picked from commit 25063af705d28d07b7332cc1b35a5cf1beedc6bb) --- crates/metal2vulkan-runtime/src/render.rs | 37 ++++++------ .../metal2vulkan-runtime/src/render_pass.rs | 7 ++- .../src/texture/attached_reads.rs | 18 +++--- .../src/runtime_lane/renders.rs | 58 ++++++++++++++++--- .../metal2vulkan/src/device/driver_facts.rs | 16 +++-- docs/API.md | 7 ++- 6 files changed, 98 insertions(+), 45 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index 48b101f88..172b0c878 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -5293,16 +5293,12 @@ impl RenderLowering { )?, false => None, }; + // A backing is cleared after the pass's first instance by the device's clear command of + // its whole level and layer, not by a load operation, which on a driver whose clears are + // Metal's stores the load operation's texel: it owes no note the load operation does not + // (`ColorTarget::withheld_clear`, `DriverFact::ColorClearIsMetals`). if let Some(clears) = &backings_after_first { uses.extend(clears.uses.iter().cloned()); - // A backing is cleared after the pass's first instance, not by a load operation: the - // note that operation does not owe is (`ColorTarget::withheld_clear`). - for color in layout.colors.iter().flatten().filter(|c| c.memoryless) { - if let Some(clear) = color.withheld_clear { - let pf = color.target.texture.pixel_format(); - super::render_pass::degrade_clear_conversion(core.device(), pf, clear); - } - } } let backings_after_first = backings_after_first.filter(|_| inlined == 1); let mut copied_back = @@ -5345,20 +5341,23 @@ impl RenderLowering { .get(index as usize) .and_then(Option::as_ref) .ok_or_else(|| internal("a storage attachment the pass does not have"))?; - // Cleared before the pass, not by its load operation: the note that operation does - // not owe is (`ColorTarget::withheld_clear`). - if let Some(clear) = color - .withheld_clear - .filter(|_| color.load == LoadAction::Clear) - { + if color.load != LoadAction::Clear { + continue; + } + // Cleared before the pass, not by its load operation. By the device's clear command + // of its whole level and layer, which on a driver whose clears are Metal's stores the + // load operation's texel (`DriverFact::ColorClearIsMetals`), it owes no note that + // operation does not; as sample planes, or by a render pass of the attachment alone + // (`clear_reach.rs`), it owes that note (`ColorTarget::withheld_clear`). + let by_command = color.planes.is_none() && cleared_ahead(&color.target, layout.extent)?; + if let Some(clear) = color.withheld_clear.filter(|_| !by_command) { let pf = color.target.texture.pixel_format(); super::render_pass::degrade_clear_conversion(core.device(), pf, clear); } - if color.load == LoadAction::Clear && color.planes.is_none() { - match cleared_ahead(&color.target, layout.extent)? { - true => clears.push((&color.target, color.clear)), - false => left_out.push(index), - } + match (color.planes.is_none(), by_command) { + (true, true) => clears.push((&color.target, color.clear)), + (true, false) => left_out.push(index), + (false, _) => {} } } let clears = Clears::plan(clears)?; diff --git a/crates/metal2vulkan-runtime/src/render_pass.rs b/crates/metal2vulkan-runtime/src/render_pass.rs index 648071470..83000df9a 100644 --- a/crates/metal2vulkan-runtime/src/render_pass.rs +++ b/crates/metal2vulkan-runtime/src/render_pass.rs @@ -758,8 +758,11 @@ pub(super) struct ColorTarget { /// The clear color of a `Clear` load the device converts with its rounding in question /// (`clear_value::rounds_apart`), whose `device-color-conversion` the pass's load operation /// does not owe: on a driver whose load operation clears as Metal's `Clear` load action does - /// (`DriverFact::ColorClearIsMetals`), of an attachment the pass's render area covers. The - /// clear made otherwise, before the pass, of an attachment its first instance leaves out + /// (`DriverFact::ColorClearIsMetals`), of an attachment the pass's render area covers. Its + /// clear command of a whole level and layer stores the same texel, so a clear made by it + /// (an attachment the first instance leaves out cleared before the pass, a memoryless + /// backing, a copy of the attachment cleared for a read after a barrier) owes none either; + /// a clear made otherwise, as sample planes or by a render pass of the attachment alone /// (`render.rs`), announces it ([`degrade_clear_conversion`]). `None` elsewhere: such a /// clear's note is announced as the pass is laid out ([`layout`]). pub(super) withheld_clear: Option<[f64; 4]>, diff --git a/crates/metal2vulkan-runtime/src/texture/attached_reads.rs b/crates/metal2vulkan-runtime/src/texture/attached_reads.rs index 68d7e34f6..c5100fa17 100644 --- a/crates/metal2vulkan-runtime/src/texture/attached_reads.rs +++ b/crates/metal2vulkan-runtime/src/texture/attached_reads.rs @@ -40,10 +40,12 @@ //! Before any draw of the pass has recorded, the copy holds nothing but the clear there, so //! where it can be cleared to it ([`Texture::cleared_copy`]: the copy is an image of its own, //! the texture a single-sampled 2D one of one level and slice that one color renders whole, in -//! place, in the image's format, inside the render area, with no clear whose note the load -//! withholds, `ColorTarget::withheld_clear`), the copy in before the pass is a clear of it to the -//! pass's clear value (`vkCmdClearColorImage`, which Vulkan converts as the load operation, the -//! pass's `device-color-conversion` note the same), and the draw stays in the pass's first +//! place, in the image's format, inside the render area), the copy in before the pass is a clear +//! of it to the pass's clear value (`vkCmdClearColorImage`, which Vulkan converts as the load +//! operation, the pass's `device-color-conversion` note the same; on a driver whose clears are +//! Metal's, `DriverFact::ColorClearIsMetals`, the clear command's texel is the load +//! operation's, Metal's, so a clear whose note the load withholds owes none here either, +//! `ColorTarget::withheld_clear`), and the draw stays in the pass's first //! instance (`WorkCounts::attachment_read_clears`): no instance of the clear alone, no store and //! load of the attachment between two, and no copy. Later draws of that instance read the same //! copy, as they would read the copy after the clear's instance. Otherwise, and once a draw has @@ -207,9 +209,10 @@ impl Texture { /// single-sampled 2D one of one level and slice, presented whole in its image's format, that /// exactly one color attachment of `layout` renders in place, all of it inside the render /// area, in that format, loading it `Clear` with a value the device's own clear converts as - /// its load operation does (no clear whose note the load withholds, - /// `ColorTarget::withheld_clear`, nor an extended-range route) and no stand-in or sample - /// planes. `None` elsewhere: the pass splits as before. + /// its load operation does (no extended-range route; a clear whose note the load withholds, + /// `ColorTarget::withheld_clear`, on a driver whose clear command stores the load operation's + /// texel, `DriverFact::ColorClearIsMetals`, owes none) and no stand-in or sample planes. + /// `None` elsewhere: the pass splits as before. pub(crate) fn cleared_copy( &self, layout: &PassLayout, @@ -245,7 +248,6 @@ impl Texture { && !color.memoryless && color.stand_in.is_none() && color.planes.is_none() - && color.withheld_clear.is_none() && color.straight_clear.is_none(); if rendering.next().is_some() || !whole || !plain { return None; diff --git a/crates/metal2vulkan-validation/src/runtime_lane/renders.rs b/crates/metal2vulkan-validation/src/runtime_lane/renders.rs index 5fa986ff8..fa00172c4 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/renders.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/renders.rs @@ -830,6 +830,32 @@ fn clear_color(device: &Device, texture: &Texture, color: [f64; 4]) -> Result<() Ok(()) } +/// The bytes of `texture` (one texel of `pixel` bytes) after a plain fill of the whole of it to +/// `color`: the device's clear command of the clear value a `Clear` load of it takes +/// (`blit_fill.rs`). +fn clear_command(device: &Device, texture: &Texture, color: [f64; 4], pixel: u64) -> Vec { + let command_buffer = device + .new_command_queue() + .unwrap() + .command_buffer() + .unwrap(); + let mut blit = command_buffer.blit_command_encoder().unwrap(); + let [red, green, blue, alpha] = color; + let (width, height) = (texture.width(), texture.height()); + blit.fill_texture_color( + texture, + 0, + 0, + Region::new_2d(0, 0, width, height), + ClearColor::new(red, green, blue, alpha), + None, + ) + .unwrap(); + blit.end_encoding().unwrap(); + run(&command_buffer); + blit_read(device, texture, pixel) +} + /// Whether `channel`'s bits, `exponent` exponent bits above `mantissa` mantissa bits, are a NaN. pub(super) fn small_nan(channel: u32, exponent: u32, mantissa: u32) -> bool { let all = (1 << exponent) - 1; @@ -940,15 +966,22 @@ fn a_code_apart(pf: PixelFormat, got: &[u8], want: &[u8]) -> bool { /// (`device-color-conversion`; a `Depth16Unorm` depth's `unorm-depth-rounding`), and a NaN of a /// half-float or small-float channel, which the device's clear converts as it likes, announced /// (`device-nan-encoding`); each printed. A format the device cannot render to is counted, not -/// run. On a driver whose load operation clears as Metal's does -/// (`DriverFact::ColorClearIsMetals`), every color clear is Metal's bytes and none announces -/// `device-color-conversion`. +/// run. On a driver whose clears are Metal's (`DriverFact::ColorClearIsMetals`), every color +/// clear is Metal's bytes and none announces `device-color-conversion`, and so is the device's +/// clear command of the load operation's clear value over the whole of a texture: a plain fill +/// of it (`BlitCommandEncoder::fill_texture_color` with no pixel format, whose fill announces its +/// own notes, taken apart). #[test] fn clears_are_the_device_s_conversion_of_the_clear_value() { let messages = Messages::default(); let cache = ScratchCache::new().unwrap(); let device = validated_device(TEST_DEVICE, &messages, cache.path()).unwrap(); + let metals = device + .properties() + .driver_facts + .contains(&metal2vulkan::device::DriverFact::ColorClearIsMetals); let (mut checked, mut unsupported, mut wrong) = (0usize, Vec::new(), Vec::new()); + let mut commanded = 0usize; let (mut announced, mut converted, mut rounded) = (false, false, 0usize); for &(pf, color, expected) in COLOR_CLEARS { let texture = match attachment(&device, pf, [1, 1], ResourceOptions::private()) { @@ -991,6 +1024,17 @@ fn clears_are_the_device_s_conversion_of_the_clear_value() { )); } checked += 1; + if metals { + let filled = attachment(&device, pf, [1, 1], ResourceOptions::private()).unwrap(); + let bytes = clear_command(&device, &filled, color, expected.len() as u64); + messages.take_notices(); + if bytes != expected { + wrong.push(format!( + "{pf:?} {color:?}, the clear command: {bytes:02x?}, Metal {expected:02x?}" + )); + } + commanded += 1; + } } // Depth: through `float`, `Depth16Unorm` truncating, and outside [0, 1] the nearer end (Metal // probe case `depth_clear_range_load` on an M4, which Metal's validation stops); stencil: the @@ -1086,15 +1130,11 @@ fn clears_are_the_device_s_conversion_of_the_clear_value() { checked += 1; } eprintln!( - "clears on {}: {checked} checked, {rounded} a code from Metal's, {} formats the device \ - cannot render to", + "clears on {}: {checked} checked, {rounded} a code from Metal's, {commanded} by the clear \ + command, {} formats the device cannot render to", device.name(), unsupported.len() ); - let metals = device - .properties() - .driver_facts - .contains(&metal2vulkan::device::DriverFact::ColorClearIsMetals); assert!( !(metals && converted), "{}: a color clear announced device-color-conversion where the load operation clears as \ diff --git a/crates/metal2vulkan/src/device/driver_facts.rs b/crates/metal2vulkan/src/device/driver_facts.rs index 8fbdad8ac..fd2629d4c 100644 --- a/crates/metal2vulkan/src/device/driver_facts.rs +++ b/crates/metal2vulkan/src/device/driver_facts.rs @@ -979,16 +979,22 @@ pub enum DriverFact { /// closest to a normalized channel's scaled value and leaves its sRGB encode and a float's /// rounding to the device, so elsewhere a clear whose rounding is in question (a normalized /// channel's value off a whole code among them) is announced (`device-color-conversion`, - /// `render_pass.rs`); on a driver with it the runtime announces none for a clear by the load - /// operation of an attachment the pass's render area covers, and announces it for the same - /// clear made otherwise (before the pass, of an attachment the pass's first instance leaves - /// out). Measured by + /// `render_pass.rs`). The driver's clear command (`vkCmdClearColorImage`) of a whole level and + /// layer with the same clear value stores the same texel. On a driver with it the runtime + /// announces none for a clear by the load operation of an attachment the pass's render area + /// covers, nor for the same clear made by the clear command (an attachment the pass's first + /// instance leaves out, cleared before the pass; a memoryless attachment's backing; the copy a + /// draw after a memory barrier at the pass's start reads, `texture/attached_reads.rs`), and + /// announces it for the same clear made otherwise (a render pass of the attachment alone, or + /// its sample planes). Measured by /// `runtime_lane::renders::clears_are_the_device_s_conversion_of_the_clear_value`, which on /// such a driver also holds that the note stays unannounced: MoltenVK 1.4.2 on the M3 Max under /// macOS 27.0.1, each of the 71 colour clears Metal was measured with (ties of 8-bit, 10-bit /// and 16-bit unorm and snorm channels, sRGB channels, the packed 16-bit formats, half and /// small floats, shared exponents and NaNs among them) into a 1x1 attachment, its 11 depth and - /// 4 stencil clears beside them: all 86 texels Metal's bytes, none a code apart. + /// 4 stencil clears beside them: all 86 texels Metal's bytes, none a code apart; and each of + /// the 71 by the clear command of the load operation's clear value over the whole 1x1 texture + /// (a plain fill of it): all 71 Metal's bytes. ColorClearIsMetals, /// An optimal colour image may keep its compression state apart from the memory it is bound /// to, so a write through a buffer or another image over its bytes can leave its texels as diff --git a/docs/API.md b/docs/API.md index 066f1e203..a30fdcbb8 100644 --- a/docs/API.md +++ b/docs/API.md @@ -3804,8 +3804,11 @@ bound. measured exception: not for a clear by the load operation of an attachment the pass's render area covers on a driver whose load operation clears as Metal's `Clear` load action does (`DriverFact::ColorClearIsMetals`: MoltenVK on the Apple9 GPUs under macOS 27, measured on the M3 - Max); a clear made before the pass, of an attachment its first instance leaves out, is announced - there too. Also a plain color fill (`BlitCommandEncoder::fill_texture_color` without a pixel + Max), nor for the same clear by that driver's clear command of a whole level and layer (an + attachment the pass's first instance leaves out, cleared before the pass; a memoryless + attachment's backing; the copy a draw sampling the attachment after a memory barrier at the + pass's start reads), measured to store Metal's bytes too; the same clear made otherwise there (a + render pass of the attachment alone, or its sample planes) is announced. Also a plain color fill (`BlitCommandEncoder::fill_texture_color` without a pixel format) of a texture of such a format the device renders, which the device's clear of the color writes, to a color whose conversion is in question against Metal's fill packing (a normalized channel's scaled value off a whole code, an sRGB channel inside its range, a value a half, small