From e8418615f118a3530d9145e07fde2d833ecf0fcf Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 11 Nov 2026 09:08:24 -0800 Subject: [PATCH] Translator: a storage slot its function only queries binds a swizzled texture unswizzled A guest's OpenCL kernel that only queries a write-only image Apple's OpenCL binds as a swizzled view (CL_BGRA over an RGBA8Unorm view, and on an M3 Max every CL_UNORM_INT8 order but RGBA and RGBx) failed: the plan joins a storage slot to the swizzled-storage variant only where its function reads or writes texels, so a query-only slot kept the texture's swizzle on its storage view, which Vulkan cannot swizzle, and the runtime refused the binding (NotImplemented(TextureSwizzle)). The guest then read zeros. A query reads no texel, and what it answers (the view's size, levels and layers; an OpenCL image's order and data type, which the third hidden buffer holds) no swizzle reaches. Measured on an M3 Max, macOS 27.0.1 (probe opencl_swizzled_images, job m3-max-16336): plain Metal's get_width/get_height of a swizzled view at an access::write slot answer the view's size, and OpenCL mode's width, height, order and data-type queries answer the size and the hidden words, on every swizzle measured. So the plan now binds such a slot's storage view with no swizzle (ImageViewDesc::shader_swizzle, under no variant): exact. The same probe confirms the rest of the contract the crate already builds, in plain Metal and OpenCL mode alike: reads (read(), read_write, sample, read_imagef with and without a sampler) answer the swizzled texel, and writes (access::write, read_write, write_imagef) store it with the swizzle undone. The channel order table and the swizzle never both apply: the table answers queries only, the swizzle moves texels only. Docs: ImageViewDesc::shader_swizzle, the runtime's binding_view and view_plan, docs/API.md's OpenCL-mode images. REFUSALS.md regenerated, unchanged. SEMANTICS_VERSION 2033. --- crates/metal2vulkan-runtime/src/binding_view.rs | 7 ++++--- crates/metal2vulkan-runtime/src/view_plan.rs | 3 ++- crates/metal2vulkan/src/bind/plan.rs | 4 +++- crates/metal2vulkan/src/bind/plan/resources.rs | 8 +++++++- crates/metal2vulkan/src/cache/mod.rs | 2 +- docs/API.md | 9 ++++++++- 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/binding_view.rs b/crates/metal2vulkan-runtime/src/binding_view.rs index 2d46f23bc..8d33a9fe8 100644 --- a/crates/metal2vulkan-runtime/src/binding_view.rs +++ b/crates/metal2vulkan-runtime/src/binding_view.rs @@ -115,9 +115,10 @@ impl VkView { /// (`NotImplemented(TextureView)`); a swizzle on a storage image, whose view Vulkan cannot swizzle /// (`NotImplemented(TextureSwizzle)`), but where the binding plan's function applies it /// (`ImageViewDesc::shader_swizzle`: a swizzled texture or view at a storage slot, whose reads the -/// function swizzles and whose writes it unswizzles; and the sampled view of a swizzled -/// extended-range texture, whose decoded texels the function swizzles), which leaves a texture made -/// resident for a function reading handles, or a `ulong` atomic's slot. +/// function swizzles and whose writes it unswizzles, or one the function only queries, which no +/// swizzle reaches; and the sampled view of a swizzled extended-range texture, whose decoded texels +/// the function swizzles), which leaves a texture made resident for a function reading handles, or +/// a `ulong` atomic's slot. pub(super) fn binding_view( image: &ImagePlan, record: &ViewRecord, diff --git a/crates/metal2vulkan-runtime/src/view_plan.rs b/crates/metal2vulkan-runtime/src/view_plan.rs index 5723c497f..e905da63c 100644 --- a/crates/metal2vulkan-runtime/src/view_plan.rs +++ b/crates/metal2vulkan-runtime/src/view_plan.rs @@ -50,7 +50,8 @@ //! of a texture with `ShaderWrite`; plain Metal reads it swizzled and writes through it with the //! swizzle undone (Metal probe cases `rw.view.read` and `rw.view.write`, the M3 Max and the M4). //! A Vulkan storage view takes no swizzle, so at a storage slot the function applies it to its -//! reads and undoes it on its writes, and the view takes none (`binding_view`); a texture made +//! reads and undoes it on its writes, and the view takes none (`binding_view`), as it takes none +//! at a storage slot the function only queries, whose answers no swizzle reaches; a texture made //! resident for a function reading handles stays the runtime's gap //! (`NotImplemented(TextureSwizzle)`). A texture's usage keeps the bits Metal's texture drops //! (`TextureUsage::DROPPED`), and so do its views; Metal asserts on a swizzled view of a texture diff --git a/crates/metal2vulkan/src/bind/plan.rs b/crates/metal2vulkan/src/bind/plan.rs index a5bfdc969..1bfd3fdc9 100644 --- a/crates/metal2vulkan/src/bind/plan.rs +++ b/crates/metal2vulkan/src/bind/plan.rs @@ -897,7 +897,9 @@ pub struct ImageViewDesc { /// `VariantKey::swizzled_storage`, which a Vulkan storage view could not swizzle, and /// for the sampled view of a swizzled extended-range texture at a slot of it, whose raw codes /// the view's swizzle would move before the function decodes them, and of a swizzled packed - /// normalized texture, whose codes the function converts by each channel's own scale. `false` + /// normalized texture, whose codes the function converts by each channel's own scale. Set too, + /// under no variant, for the storage view of a swizzled texture at a slot the function only + /// queries: it reads and writes no texel, and no swizzle reaches what a query answers. `false` /// for every other view, on which the consumer composes the texture's swizzle with /// `components` (and a swizzled storage view is the consumer's refusal). #[cfg_attr(feature = "serde", serde(default))] diff --git a/crates/metal2vulkan/src/bind/plan/resources.rs b/crates/metal2vulkan/src/bind/plan/resources.rs index c000dc178..168871a5a 100644 --- a/crates/metal2vulkan/src/bind/plan/resources.rs +++ b/crates/metal2vulkan/src/bind/plan/resources.rs @@ -1220,6 +1220,12 @@ impl ResourcePlanner<'_> { if shader_swizzle { self.variant.swizzled_storage.insert(slot); } + // A storage slot its function only queries reads and writes no texel, and what a + // query answers (the view's size, levels and layers, an OpenCL image's format + // words, which the third hidden buffer holds) no swizzle reaches: plain Metal's and + // OpenCL mode's queries of a swizzled view answer so (our probe + // `opencl_swizzled_images`, on an M3 Max). Its view takes none, under no variant. + let queried = storage && !t.access.read && !t.access.write && !swizzle.is_default(); let view = if storage { let format = match choice { _ if ulong_atomic => vk::Format::R64_UINT, @@ -1244,7 +1250,7 @@ impl ResourcePlanner<'_> { layout: vk::ImageLayout::GENERAL, components: vk::ComponentMapping::IDENTITY, level: None, - shader_swizzle, + shader_swizzle: shader_swizzle || queried, } } else { match self.texel_view(pixel_format, &class)? { diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 77d116ff2..9e8a539bd 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -60,7 +60,7 @@ mod tests; /// unchanged version, and its bless records the version. Independent of the crate's version and of /// [`ARTIFACT_FORMAT_VERSION`]. The commit that bumps it says what changed; this comment keeps no /// history. -pub const SEMANTICS_VERSION: u32 = 2032; +pub const SEMANTICS_VERSION: u32 = 2033; /// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the /// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another diff --git a/docs/API.md b/docs/API.md index 9af2413e7..96d3001ca 100644 --- a/docs/API.md +++ b/docs/API.md @@ -768,7 +768,14 @@ layout stride of `None` is `MTLBufferLayoutStrideDynamic`: the stride comes from height 1 for an `image1d_array_t` (`texture1d_array`). In the mode the kernel's slot is that texture's type (`TextureType::Type2D`, `Type2DArray`), and each read, sample and write of the image, `get_image_width` and `get_image_array_size` is the 2D texture's at row 0 (a sampled - coordinate at the row's centre), as both Macs answer. + coordinate at the row's centre), as both Macs answer. Apple's OpenCL binds an image of most + channel orders as a swizzled view (on an M3 Max, macOS 27.0.1, with `CL_UNORM_INT8`: `CL_R`, + `CL_RG`, `CL_A`, `CL_RA` and `CL_INTENSITY` as `R8Unorm` or `RG8Unorm` views, `CL_BGRA`, + `CL_ARGB` and `CL_RGB` as `RGBA8Unorm` views). A read (with a sampler or without) answers the + swizzled texel, a write stores it with the swizzle undone (each channel from the first lane + whose source it is), and a query answers the view's size and the hidden buffer's words, which + no swizzle reaches, as plain Metal answers for the same view; the order is never applied twice. + A storage slot the kernel only queries binds the texture's view with no swizzle. - OpenCL C's `native_` and `half_` of `float` types (`air.native_.` and `air.half_.`, for `cos`, `exp`, `exp10`, `exp2`, `log`, `log10`, `log2`, `powr`, `rsqrt`, `sin`, `sqrt` and `tan`, scalar and of every vector