From 22fa0997b1ce2e30ee09974da1d4f7010bd24b0e Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 14 Nov 2026 13:11:54 -0800 Subject: [PATCH] RUNTIME 2312: A write-only argument-buffer use of a texture whose writes Metal drops names none of its image What. A command that makes a texture resident whose writes Metal drops (metal::format::writes_dropped: the 4:2:2 formats, the subsampled and packed two-plane YCbCr ones) names no use of it where its binding plan's heap holds it at no element (no WriteSource::HeapEntry of its token): every heap storage array holds such a texture as its neutral (bind::heap's Fit::Dropped), and a kernel that only writes it declares no sampled array, so its function reaches none of the texture's image (binding.rs, resident_uses_into; the storage-only test holds_write_shadow becomes heap_holds, by class access or any). Why. A kernel that only wrote a YCBCR8_420_2P texture through an argument buffer (useResource WRITE) named the texture's image as a written use. That use matched the texture's converted shadow, so the command's shadow planning made and recorded the planes' conversion (ShadowCopies::prepare -> YCbCrConversion::make), and on a device that converts 4:2:0 itself (Iris Xe: the consumer's pin 92 Iris leg, writes_through_argument_buffers_of_two_plane_ ycbcr_textures_are_the_slots) that first conversion announced ycbcr-filter-not-exact though nothing read the texture: relayed in default mode, fatal in strict mode. The write itself was dropped, as Metal drops it. 4:4:4 textures already named only their write shadow. Now such a command converts nothing and announces nothing; a command whose function reads the texture through the argument buffer (a sampled array holds it) converts it and announces the device's conversion as before. Test: runtime_lane::ycbcr_argument_writes::a_kernel_that_only_writes_a_held_ 420_texture_converts_none_of_its_planes (lavapipe and GPU lanes): resident WRITE and READ|WRITE, the write-only kernel records no conversion (ycbcr_refreshes + ycbcr_whole_conversions), announces no ycbcr-filter-not-exact and leaves the planes' bytes; a kernel reading it through the argument buffer records one conversion and announces the notice where the device converted the texture itself. lavapipe converts 4:2:0 through Metal's arithmetic, so there the conversion count is the check that fails without the fix. docs/API.md's catalogue entry and ycbcr.rs's writes bullet say so. Verified (lane ycbcrnote, at 12dbbcc67e on fe38b678dc): - msi lavapipe m3-max-30151: touched lane tests 173/173 and runtime cpu_icd tests 238/238; clippy -D warnings plain and cpu_icd exit 0. Negative control in the same job (binding.rs back to fe38b678dc): the new test fails at "the writing command converted the planes". - RTX 5080 (b5080 entry ycbcrnote-held420-130832, prebuilt m2v_gpu archive): passes; device conversion true, write-only commands announce nothing, the reading command announces YCbCrFilterNotExact once. - msi m3-max-30159: clippy -D warnings under m2v_gpu and rustdoc -D warnings exit 0. Expected, not run: the consumer's Iris strict test passes. RUNTIME_VERSION moves: the same artifact's write-only commands now record and announce nothing where they converted and announced before. --- crates/metal2vulkan-runtime/src/binding.rs | 25 ++- crates/metal2vulkan-runtime/src/ycbcr.rs | 6 +- .../src/runtime_lane/ycbcr_argument_writes.rs | 167 ++++++++++++++---- crates/metal2vulkan/src/cache/mod.rs | 2 +- docs/API.md | 4 +- 5 files changed, 161 insertions(+), 43 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/binding.rs b/crates/metal2vulkan-runtime/src/binding.rs index 86caaf5a8..5ff9af77d 100644 --- a/crates/metal2vulkan-runtime/src/binding.rs +++ b/crates/metal2vulkan-runtime/src/binding.rs @@ -78,6 +78,7 @@ use metal2vulkan::bind::{ NeutralResource, PushConstantWrite, PushHole, ResourceUse, RootTable, SamplerDescriptor, SamplerSource, UseTarget, ViewSetup, WriteSource, }; +use metal2vulkan::metal::format::writes_dropped; use metal2vulkan::metal::sampler::{AddressModeMap, OpaqueBlackBorder, SamplerUse}; use metal2vulkan::pipeline::{ HandleClass, ImageAccess, SlotClass, SlotRef, Stage, StorageAttachmentView, @@ -3242,6 +3243,17 @@ fn resident_uses_into( Some(Used::Heap { bytes, .. }) => bytes, Some(Used::IndirectCommandBuffer(icb)) => icb.storage(), Some(Used::Texture(texture)) => { + // A texture whose writes Metal drops (`metal::format::writes_dropped`: the 4:2:2 + // formats, the subsampled and packed two-plane YCbCr ones) is its neutral in every + // heap storage array (`bind::heap`), which a function's writes through its handle + // reach, as a slot that only writes it takes its neutral. A command whose heap + // holds it at no element, no sampled array either, reaches none of its image and + // names no use of it: a kernel that only writes a two-plane YCbCr texture through + // an argument buffer converts none of its planes, and so announces no conversion + // of them (`ycbcr.rs`, `ycbcr-filter-not-exact`). + if writes_dropped(texture.pixel_format()) && !heap_holds(plan, token, None) { + continue; + } let (read, write) = image_access(resource.class)?; let (read, write) = ( access(read, resource.access.read), @@ -3254,7 +3266,7 @@ fn resident_uses_into( let shadowed = resource.class == AccessClass::StorageImage && resource.access.write && texture.takes_ycbcr_writes(true) - && holds_write_shadow(plan, token); + && heap_holds(plan, token, Some(ImageAccess::Storage)); if shadowed { let used = texture.ycbcr_write_use("useResource:usage:", write)?; named.insert(used.key); @@ -3294,10 +3306,11 @@ fn resident_uses_into( Ok(()) } -/// Whether `plan` holds the texture it makes resident as token `token` in a storage heap array -/// (a `WriteSource::HeapEntry` of a storage class): for a 4:4:4 two-plane YCbCr texture, its write -/// shadow's view (`bind::heap`), which a stage of the plan writes. -fn holds_write_shadow(plan: &BindingPlan, token: u32) -> bool { +/// Whether `plan` holds the texture it makes resident as token `token` at an element of a heap +/// array of `access` (a `WriteSource::HeapEntry` of such a class), or of any class where `None`. +/// In a storage array a 4:4:4 two-plane YCbCr texture is its write shadow's view (`bind::heap`), +/// which a stage of the plan writes. +fn heap_holds(plan: &BindingPlan, token: u32, access: Option) -> bool { plan.descriptors.iter().any(|d| { matches!( d.source, @@ -3305,7 +3318,7 @@ fn holds_write_shadow(plan: &BindingPlan, token: u32) -> bool { class: HandleClass::Image(class), source: HeapSource::Used(used), .. - } if used == token && class.access == ImageAccess::Storage + } if used == token && access.is_none_or(|access| class.access == access) ) }) } diff --git a/crates/metal2vulkan-runtime/src/ycbcr.rs b/crates/metal2vulkan-runtime/src/ycbcr.rs index 575679f98..d2fd0a397 100644 --- a/crates/metal2vulkan-runtime/src/ycbcr.rs +++ b/crates/metal2vulkan-runtime/src/ycbcr.rs @@ -182,8 +182,10 @@ //! `TexelsPastBufferLength`). A //! function's writes to a 4:2:0, 4:2:2 or packed format are dropped, as Metal drops them (the //! packed 4:4:4 ones on the M3 Max under macOS 27.0.1, where an M4 stores them; -//! `metal::format::writes_dropped`); a texture's over plane 1 (not measured) and a slot's that -//! reads the texture too are refused. +//! `metal::format::writes_dropped`), so a command whose function only writes such a texture +//! through an argument buffer uses none of its image and converts none of its planes +//! (`binding.rs`); a texture's over plane 1 (not measured) and a slot's that reads the texture +//! too are refused. //! - **Writes** (on an Apple M4 under macOS 27.0.1, on an Apple M3 Max under macOS 26.7, byte for //! byte alike; the `_sRGB` twins' on an Apple M3 Max under macOS 27.0.1). Metal stores a //! function's writes to `YCBCR8_444_2P`, `YCBCR10_444_2P`, their `_sRGB` twins, `RGB8_444_2P` diff --git a/crates/metal2vulkan-validation/src/runtime_lane/ycbcr_argument_writes.rs b/crates/metal2vulkan-validation/src/runtime_lane/ycbcr_argument_writes.rs index 63a9a1f72..3f4c9805a 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/ycbcr_argument_writes.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/ycbcr_argument_writes.rs @@ -9,11 +9,15 @@ //! not mark the texels it stores leaves the planes as they were. //! //! A command whose argument buffer names no such texture specializes the kernel as before the -//! writes were built (`SpecKey::HeapYCbCrWrites` 0): it makes no new pipeline. +//! writes were built (`SpecKey::HeapYCbCrWrites` 0): it makes no new pipeline. One whose function +//! only writes a `YCBCR8_420_2P` texture through its handle uses none of the texture's image +//! (every heap element of it is a neutral): it converts none of its planes, and announces no +//! conversion of them. use super::buffer_with; -use super::degrade_notices::on_device; +use super::degrade_notices::{counts, on_device}; use super::read_buffer; +use super::ycbcr_device_reads::device_conversions; use super::ycbcr_srgb_twins::{ complete, library, surface_bytes, twin_texture, COLORS, H, LENGTH, W, }; @@ -23,12 +27,14 @@ use metal2vulkan::metal::{PixelFormat, ResourceUsage, TextureUsage}; use metal2vulkan::pipeline::{ComputePipelineDescriptor, StageSource}; use metal2vulkan_devtools::llvm_assemble_text; use metal2vulkan_runtime::{ - Buffer, CommandQueue, ComputePipelineState, Device, Error, Library, PipelineOptions, Size, - Texture, TextureDescriptor, + Buffer, CommandQueue, ComputePipelineState, Degradation, Device, Error, Library, + PipelineOptions, ResourceOptions, Size, Texture, TextureDescriptor, }; /// Synthetic AIR written for this lane: kernel `write_held` writes buffer 0's `float4` `gid.x` to -/// texel `gid` of the `texture2d` the argument buffer at buffer 1 holds. +/// texel `gid` of the `texture2d` the argument buffer at buffer 1 holds; kernel +/// `read_held` stores texel `gid` of the `texture2d` the argument buffer at buffer 1 +/// holds to buffer 0's `float4` `8 gid.y + gid.x`. const HELD: &str = r#"; Owned synthetic shader. %struct.Held = type { ptr addrspace(1) } @@ -44,9 +50,28 @@ entry: ret void } -declare void @air.write_texture_2d.v4f32(ptr addrspace(1), <2 x i32>, <4 x float>, i32, i32) +define void @read_held(ptr addrspace(2) noalias %held, ptr addrspace(1) noalias %out, <2 x i32> %gid) { +entry: + %pt = getelementptr inbounds %struct.Held, ptr addrspace(2) %held, i64 0, i32 0 + %t = load ptr addrspace(1), ptr addrspace(2) %pt, align 8 + %rs = tail call ptr addrspace(2) @air.get_read_sampler() + %r = tail call { <4 x float>, i8 } @air.read_texture_2d.v4f32(ptr addrspace(1) %t, ptr addrspace(2) %rs, <2 x i32> %gid, <2 x i32> zeroinitializer, i32 0, i32 1) + %v = extractvalue { <4 x float>, i8 } %r, 0 + %x = extractelement <2 x i32> %gid, i64 0 + %y = extractelement <2 x i32> %gid, i64 1 + %row = shl i32 %y, 3 + %i = add i32 %row, %x + %j = zext i32 %i to i64 + %p = getelementptr inbounds <4 x float>, ptr addrspace(1) %out, i64 %j + store <4 x float> %v, ptr addrspace(1) %p, align 16 + ret void +} -!air.kernel = !{!0} +declare void @air.write_texture_2d.v4f32(ptr addrspace(1), <2 x i32>, <4 x float>, i32, i32) +declare ptr addrspace(2) @air.get_read_sampler() +declare { <4 x float>, i8 } @air.read_texture_2d.v4f32(ptr addrspace(1), ptr addrspace(2), <2 x i32>, <2 x i32>, i32, i32) + +!air.kernel = !{!0, !10} !air.version = !{!97} !air.language_version = !{!98} !0 = !{ptr @write_held, !1, !2} @@ -57,6 +82,12 @@ declare void @air.write_texture_2d.v4f32(ptr addrspace(1), <2 x i32>, <4 x float !5 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.write", !"air.arg_type_name", !"texture2d", !"air.arg_name", !"t"} !6 = !{i32 1, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 16, !"air.arg_type_name", !"float4", !"air.arg_name", !"colors"} !7 = !{i32 2, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint2", !"air.arg_name", !"gid"} +!10 = !{ptr @read_held, !1, !11} +!11 = !{!12, !15, !7} +!12 = !{i32 0, !"air.indirect_buffer", !"air.buffer_size", i32 8, !"air.location_index", i32 1, i32 1, !"air.read", !"air.address_space", i32 2, !"air.struct_type_info", !13, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"Read", !"air.arg_name", !"held"} +!13 = !{i32 0, i32 8, i32 0, !"texture2d", !"t", !"air.indirect_argument", !14} +!14 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.read", !"air.arg_type_name", !"texture2d", !"air.arg_name", !"t"} +!15 = !{i32 1, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 16, !"air.arg_type_name", !"float4", !"air.arg_name", !"out"} !97 = !{i32 2, i32 8, i32 0} !98 = !{!"Metal", i32 4, i32 0, i32 0} "#; @@ -109,12 +140,13 @@ fn colors(device: &Device) -> Buffer { buffer_with(device, &bytes).unwrap() } -/// Run `state` on `device`'s `queue` over the whole of `texture`, with `values` at buffer 0: the texture at slot 0, or, -/// with `held`, its resource ID in a buffer at buffer 1, the texture resident for writing. -fn write( +/// Run `state` on `device`'s `queue` over the whole of `texture`, with `values` at buffer 0: the +/// texture at slot 0, or, with `held`, its resource ID in a buffer at buffer 1, the texture +/// resident with that usage. +fn run( (device, queue): (&Device, &CommandQueue), state: &ComputePipelineState, - (texture, held): (&Texture, bool), + (texture, held): (&Texture, Option), values: &Buffer, ) { let ids = buffer_with(device, &texture.gpu_resource_id().0.to_le_bytes()).unwrap(); @@ -123,13 +155,11 @@ fn write( encoder.set_compute_pipeline_state(state).unwrap(); encoder.set_buffer(Some(values), 0, 0).unwrap(); match held { - true => { + Some(usage) => { encoder.set_buffer(Some(&ids), 0, 1).unwrap(); - encoder - .use_resource(texture.as_resource(), ResourceUsage::WRITE) - .unwrap(); + encoder.use_resource(texture.as_resource(), usage).unwrap(); } - false => encoder.set_texture(Some(texture), 0).unwrap(), + None => encoder.set_texture(Some(texture), 0).unwrap(), } encoder .dispatch_threads(Size::new(W, H, 1), Size::new(W, H, 1)) @@ -154,12 +184,12 @@ fn argument_buffer_writes_to_ycbcr_textures_store_the_slot_s_bytes() { let usage = TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE; for (seed, (pf, ostype, stored)) in (0x5300..).zip(FORMATS) { let bytes = surface_bytes(seed); - let after: Vec> = [(&slot, false), (&held, true)] + let after: Vec> = [(&slot, None), (&held, Some(ResourceUsage::WRITE))] .into_iter() .map(|(state, through)| { let surface = buffer_with(device, &bytes).unwrap(); let texture = twin_texture(&surface, pf, ostype, usage).unwrap(); - write((device, &queue), state, (&texture, through), &values); + run((device, &queue), state, (&texture, through), &values); read_buffer(&surface, 0, LENGTH).unwrap() }) .collect(); @@ -203,29 +233,27 @@ fn argument_buffer_writes_without_a_ycbcr_member_make_no_new_pipeline() { let other = buffer_with(device, &surface_bytes(0x5311)).unwrap(); let shadowed = twin_texture(&other, PixelFormat::YCBCR84442P, "444v", usage).unwrap(); let made = || device.work_counts().variant_pipelines_made; - write( - (device, &queue), - &held, - (&plain(PixelFormat::RGBA8Unorm), true), - &values, - ); + let write = |texture: &Texture| { + run( + (device, &queue), + &held, + (texture, Some(ResourceUsage::WRITE)), + &values, + ) + }; + write(&plain(PixelFormat::RGBA8Unorm)); let first = made(); - write( - (device, &queue), - &held, - (&plain(PixelFormat::RGBA16Float), true), - &values, - ); - write((device, &queue), &held, (&dropped, true), &values); + write(&plain(PixelFormat::RGBA16Float)); + write(&dropped); assert_eq!( made(), first, "a command naming no shadowed texture made a pipeline" ); - write((device, &queue), &held, (&shadowed, true), &values); + write(&shadowed); let shadowing = made(); assert!(shadowing > first, "the shadowed texture's variant was made"); - write((device, &queue), &held, (&shadowed, true), &values); + write(&shadowed); assert_eq!( made(), shadowing, @@ -237,3 +265,76 @@ fn argument_buffer_writes_without_a_ycbcr_member_make_no_new_pipeline() { ); }); } + +/// A kernel that only writes a `YCBCR8_420_2P` texture (whose writes Metal drops) through an +/// argument buffer, the texture resident for writing or for reading and writing, uses none of its +/// image: the heap holds it at no element but neutrals (`bind::heap`'s storage arrays hold their +/// neutral for it, and the kernel reads no sampled array). Its commands record no conversion of +/// the planes (`WorkCounts::ycbcr_refreshes`, `ycbcr_whole_conversions`), announce no +/// `ycbcr-filter-not-exact`, and leave the planes' bytes as they were. A kernel that reads the +/// texture through the argument buffer converts it, once, and announces the notice where the +/// device converts the texture itself (`ycbcr_device_reads`), as a slot's read does. +#[test] +fn a_kernel_that_only_writes_a_held_420_texture_converts_none_of_its_planes() { + on_device(|device, messages| { + let library = held_library(device); + let Some(write) = state(device, &library, "write_held") else { + return; + }; + let read = state(device, &library, "read_held").unwrap(); + let queue = device.new_command_queue().unwrap(); + let values = colors(device); + let conversions = || { + let counts = device.work_counts(); + counts.ycbcr_refreshes + counts.ycbcr_whole_conversions + }; + let bytes = surface_bytes(0x5320); + let surface = buffer_with(device, &bytes).unwrap(); + messages.take_notices(); + let converted = device_conversions(device); + let usage = TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE; + let texture = twin_texture(&surface, PixelFormat::YCBCR84202P, "420v", usage).unwrap(); + let by_device = device_conversions(device) > converted; + for usage in [ + ResourceUsage::WRITE, + ResourceUsage::READ | ResourceUsage::WRITE, + ] { + let before = conversions(); + run((device, &queue), &write, (&texture, Some(usage)), &values); + assert_eq!( + conversions(), + before, + "resident {usage:?}: the writing command converted the planes" + ); + } + assert!( + read_buffer(&surface, 0, LENGTH).unwrap() == bytes, + "the writes stored codes" + ); + let written = counts(&messages.take_notices()); + assert_eq!( + written.get(&Degradation::YCbCrFilterNotExact), + None, + "{written:?}" + ); + let out = device + .new_buffer(16 * W * H, ResourceOptions::shared()) + .unwrap(); + let before = conversions(); + let usage = Some(ResourceUsage::READ); + run((device, &queue), &read, (&texture, usage), &out); + let reading = conversions() - before; + assert_eq!(reading, 1, "the reading command's conversions"); + let noted = counts(&messages.take_notices()); + assert_eq!( + noted.get(&Degradation::YCbCrFilterNotExact), + by_device.then_some(&1), + "{noted:?}" + ); + println!( + "YCBCR-HELD-NOTE {}: device conversion {by_device}; writing commands converted \ + nothing, notices {written:?}; the reading one {reading}, notices {noted:?}", + device.name() + ); + }); +} diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 2900173e6..98febaac4 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 522; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2311; +pub const RUNTIME_VERSION: u32 = 2312; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles diff --git a/docs/API.md b/docs/API.md index 121ff9660..87e904197 100644 --- a/docs/API.md +++ b/docs/API.md @@ -3375,7 +3375,9 @@ bound. through a linear chroma filter among them for a 4:2:0 or 4:2:2 one, and has no defect in it: MoltenVK's 4:2:0 and 4:2:2 formats and lavapipe's, RADV's and NVIDIA's 10-bit ones aside) reads the device's conversion of its planes, announced at the texture's first conversion (a texture whose planes every - function reads in place converts none, and announces nothing); so do the samples of a texture whose planes + function reads in place converts none, and announces nothing, nor does a command whose function + only writes a texture of a format whose writes Metal drops through an argument buffer, which + uses none of its image); so do the samples of a texture whose planes are read in place through the device's conversion of a linear image of them (where the device's linear tiling of the 4:2:0 format has Metal's reconstruction), the device's filter of the codes too, announced where the image is made; and a pipeline stage's filtered sample of a