From c8d8a2063d35b28f42370b75a67f0043c136c8a0 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 12 Nov 2026 07:24:32 -0800 Subject: [PATCH] An A8Unorm texture over a buffer that only reads takes R8Unorm's linear image, read through (0, 0, 0, R) Where the device reports A8_UNORM's linear tiling without what its optimal tiling has for the texture's usage, and R8_UNORM's linear tiling with all of it (vkGetPhysicalDeviceFormatProperties2's linearTilingFeatures and optimalTilingFeatures, read through linear_plan::linear_lack), an A8Unorm texture over a buffer's bytes that only reads (no RenderTarget, ShaderWrite, ShaderAtomic or BlockWritesOnly usage; one level, one sample; its alpha not read as one, not rotated) is made as an R8Unorm texture's storage over the same bytes: the image in place over host pages, an image aliasing the buffer's memory, or that texture's shadow where it would be one. Its record reads the image as R8Unorm through the swizzle metal::format::swizzle_over composes ((Zero, Zero, Zero, Red) composed with the texture's own), as an A8Unorm view of an R8Unorm texture is read (ViewRecord::read_as): the same byte, the same filter, the channels moved by the view's component mapping. NVIDIA's linear A8_UNORM has no sampling, linear filtering or transfer features (the consumer's RTX 2080 walk kept 195 glyph-sized A8Unorm textures over Shared buffers as shadows): those are now in place where an R8Unorm texture's image is. A texture that renders or is written keeps the A8_UNORM route. Buffer::texture_placement answers the same route. Counted: WorkCounts::alpha_textures_held_in_red (api_signatures.tsv regenerated). a8_over_buffers asserts no shadow and the device's own A8Unorm texture's reads; device runs pending. --- .../examples/host_ab/runtime.rs | 1 + .../src/tests/a8_over_buffers.rs | 13 ++- crates/metal2vulkan-runtime/src/texture.rs | 21 ++++- .../src/texture/red_held_alpha.rs | 94 +++++++++++++++++++ .../metal2vulkan-runtime/src/texture/route.rs | 7 ++ .../metal2vulkan-runtime/src/work_counts.rs | 16 ++++ crates/metal2vulkan/tests/api_signatures.tsv | 1 + 7 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 crates/metal2vulkan-runtime/src/texture/red_held_alpha.rs diff --git a/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs b/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs index d556adf14..b7e3532cf 100644 --- a/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs +++ b/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs @@ -2090,6 +2090,7 @@ impl Gpu for Runtime { ("shadow_texels_kept", c.shadow_texels_kept), ("bounded_shadow_copies", c.bounded_shadow_copies), ("views_through_swizzle", c.views_through_swizzle), + ("alpha_textures_held_in_red", c.alpha_textures_held_in_red), ("view_shadows", c.view_shadows), ( "attachment_reads_through_copies", diff --git a/crates/metal2vulkan-runtime/src/tests/a8_over_buffers.rs b/crates/metal2vulkan-runtime/src/tests/a8_over_buffers.rs index d1667bc65..47dd7a57e 100644 --- a/crates/metal2vulkan-runtime/src/tests/a8_over_buffers.rs +++ b/crates/metal2vulkan-runtime/src/tests/a8_over_buffers.rs @@ -134,9 +134,11 @@ fn over( let texture = buffer.new_texture(&descriptor(), 0, WIDTH).unwrap(); let after = device.work_counts(); let shadows = after.shadowed_textures - before.shadowed_textures; + let held = after.alpha_textures_held_in_red - before.alpha_textures_held_in_red; let format = texture.image_plan().map(|plan| plan.format); println!( - "{TEST} on {}: over {what}: {placement:?}, {shadows} shadow(s) made, its image {format:?}", + "{TEST} on {}: over {what}: {placement:?}, {shadows} shadow(s) made, {held} held in red, \ + its image {format:?}", device.name() ); let reads = read(device, state, &texture); @@ -144,6 +146,15 @@ fn over( shadows, 0, "{TEST}: an A8Unorm texture over {what} that only reads is no shadow ({placement:?})" ); + // A device that holds no `A8_UNORM` image at all holds every `A8Unorm` texture in red, uncounted. + let profile = device.compile_device().profile(); + let everywhere = metal2vulkan::metal::format::alpha_in_red(PixelFormat::A8Unorm, profile); + let red = format.is_ok_and(|format| format == metal2vulkan::vk::Format::R8_UNORM); + assert_eq!( + held, + u64::from(red && !everywhere), + "{TEST}: an image of R8_UNORM is counted held in red" + ); reads } diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index acc81e462..c449b6639 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -175,6 +175,7 @@ pub(crate) mod host_shadow; mod in_place_image; pub(super) mod kept_image; pub(super) mod linear_alignment; +mod red_held_alpha; pub(super) mod requirements; #[cfg(unix)] mod rotation; @@ -2452,10 +2453,26 @@ impl Device { layout: &TextureLayout, id_use: IdUse, ) -> Result { - let record = ViewRecord::of_descriptor(layout, descriptor); + let mut record = ViewRecord::of_descriptor(layout, descriptor); let mut pending = None; let over = (Arc::new(texels), &mut pending); - let storage = self.storage_over_buffer(call, over, descriptor, layout)?; + // An `A8Unorm` texture that only reads, where the device's linear `A8_UNORM` image lacks + // what its optimal one does, is an `R8Unorm` texture's storage read through a swizzle + // (`texture/red_held_alpha.rs`). + let facts = self + .shared() + .textures + .facts(self.compile_device().profile()); + let storage = match red_held_alpha::red_held_alpha(descriptor, layout, &facts) { + Some(held) => { + let storage = + self.storage_over_buffer(call, over, &held.descriptor, &held.layout)?; + record.read_as = Some(held.read_as); + self.shared().counts.alpha_texture_held_in_red(); + storage + } + None => self.storage_over_buffer(call, over, descriptor, layout)?, + }; let texture = Texture::made(self, storage, record, None, id_use); // A shadow made before its pages' (or its pooled memory's) in-place verdict moves in // place once it passes. diff --git a/crates/metal2vulkan-runtime/src/texture/red_held_alpha.rs b/crates/metal2vulkan-runtime/src/texture/red_held_alpha.rs new file mode 100644 index 000000000..a37193d3d --- /dev/null +++ b/crates/metal2vulkan-runtime/src/texture/red_held_alpha.rs @@ -0,0 +1,94 @@ +//! An `A8Unorm` texture over a buffer's bytes whose image is an `R8Unorm` texture's, read +//! through `(Zero, Zero, Zero, Red)`: where the device's linear `A8_UNORM` image lacks what its +//! optimal image does for the texture's usage, and its linear `R8_UNORM` image lacks nothing. +//! +//! - **Why.** A texture over a buffer's bytes is a linear image over them where the device makes +//! one that does all its optimal image does (`linear_plan::linear_lack`), else a shadow the +//! encoders copy the bytes into and back (`shadow.rs`). NVIDIA's drivers report `A8_UNORM`'s +//! optimal tiling with sampling, linear filtering and transfers, and its linear tiling (and its +//! `DRM_FORMAT_MOD_LINEAR` modifier) with none of them, where `R8_UNORM`'s linear tiling has +//! them all (`vkGetPhysicalDeviceFormatProperties2`'s `linearTilingFeatures` and +//! `optimalTilingFeatures`): every glyph-sized `A8Unorm` texture over a guest's `Shared` +//! buffers was a shadow there. +//! - **What.** The texture's storage is the one an `R8Unorm` texture of the same descriptor makes +//! over the same bytes (`Device::storage_over_buffer`): the image in place over host pages, an +//! image aliasing the buffer's memory, or a shadow where that texture would be one. Its record +//! reads that image in `R8Unorm`'s format through the swizzle `metal::format::swizzle_over` +//! composes with the texture's own (`ViewRecord::read_as`), as an `A8Unorm` view of an +//! `R8Unorm` texture reads its image (`texture/view_shadow.rs`): the same byte a texel, sampled +//! through the same filter, the channels moved by the view's component mapping, which Vulkan +//! applies to gathered and border texels as to any other ("Texel Input Operations"), so its +//! reads, samples and gathers are Metal's `(0, 0, 0, a)`. Its copies and CPU access move the +//! bytes, which are the same in either format. Counted: +//! `WorkCounts::alpha_textures_held_in_red`. +//! - **Which.** A 2D texture (or array) of one level and one sample that only reads: no +//! `RenderTarget`, `ShaderWrite`, `ShaderAtomic` or `BlockWritesOnly` usage (`Unknown` usage +//! takes them all), its alpha not read as one and not rotated, on a device that takes a view of +//! the composed mapping (`swizzle_over`). A texture that renders or is written keeps the +//! `A8_UNORM` route. A device that holds no `A8_UNORM` image at all already holds every +//! `A8Unorm` texture in `R8_UNORM` (`metal::format::alpha_in_red`), and its plan lacks nothing +//! here. + +use super::super::image_plan::{plan_image, ImageFacts}; +use super::super::linear_plan::linear_lack; +use super::super::texture_descriptor::{texture_usage, TextureDescriptor, TextureLayout}; +use metal2vulkan::metal::format::{alpha_in_red_format, direct_format, swizzle_over}; +use metal2vulkan::metal::{PixelFormat, TextureSwizzleChannels, TextureType, TextureUsage}; + +/// An `A8Unorm` texture made over a buffer as an `R8Unorm` texture's storage: that texture's +/// descriptor and layout, and the format and swizzle its bindings read the image through. +pub(super) struct RedHeld { + pub(super) descriptor: TextureDescriptor, + pub(super) layout: TextureLayout, + pub(super) read_as: (PixelFormat, TextureSwizzleChannels), +} + +/// The usages that write a texture: the texture keeps its own format's route. +const WRITES: TextureUsage = TextureUsage::RENDER_TARGET + .union(TextureUsage::SHADER_WRITE) + .union(TextureUsage::SHADER_ATOMIC) + .union(TextureUsage::BLOCK_WRITES_ONLY); + +/// Whether a texture of `descriptor` (checked as `layout`) over a buffer's bytes is made as the +/// format its alpha is held in the red of (the module docs), on the device of `facts`: that +/// texture's descriptor and layout and the read it takes; `None` where it keeps its own route. +pub(super) fn red_held_alpha( + descriptor: &TextureDescriptor, + layout: &TextureLayout, + facts: &ImageFacts<'_>, +) -> Option { + let pf = layout.pixel_format; + let red = alpha_in_red_format(pf)?; + let usage = texture_usage(descriptor.usage, descriptor.swizzle); + let one = matches!( + layout.texture_type, + TextureType::Type2D | TextureType::Type2DArray + ) && (layout.levels, layout.samples) == (1, 1); + if !one || usage.intersects(WRITES) || descriptor.alpha_reads_one || descriptor.rotation != 0 { + return None; + } + let own = + (PixelFormat::ALL.iter().copied()).find(|&other| direct_format(other) == Some(red))?; + // The texture's own plan's linear image lacks what its optimal one does, and the red + // format's lacks nothing. + let asked = plan_image(layout, usage, false, facts).ok()?; + linear_lack(&asked, facts)?; + let held = TextureLayout { + pixel_format: own, + ..*layout + }; + let plan = plan_image(&held, usage, false, facts).ok()?; + if linear_lack(&plan, facts).is_some() { + return None; + } + let over = swizzle_over(pf, descriptor.swizzle, own, facts.profile)?; + Some(RedHeld { + descriptor: TextureDescriptor { + pixel_format: own, + swizzle: TextureSwizzleChannels::DEFAULT, + ..*descriptor + }, + layout: held, + read_as: (own, over), + }) +} diff --git a/crates/metal2vulkan-runtime/src/texture/route.rs b/crates/metal2vulkan-runtime/src/texture/route.rs index 977e5c0c5..0f55123a1 100644 --- a/crates/metal2vulkan-runtime/src/texture/route.rs +++ b/crates/metal2vulkan-runtime/src/texture/route.rs @@ -94,6 +94,13 @@ impl Device { .shared() .textures .facts(self.compile_device().profile()); + // An `A8Unorm` texture made as an `R8Unorm` texture's storage takes that texture's route + // (`texture/red_held_alpha.rs`). + let held = super::red_held_alpha::red_held_alpha(descriptor, layout, &facts); + let (descriptor, layout) = match &held { + Some(held) => (&held.descriptor, &held.layout), + None => (descriptor, layout), + }; let (usage, plan) = self.declared_plan(call, descriptor, layout, &facts)?; let route = match self.route_over_buffer(texels, usage, (layout.levels, past)) { Route::Host { diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 337d9034d..83753f3ac 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -585,6 +585,7 @@ pub(crate) struct Counters { shadow_texels_kept: AtomicU64, bounded_shadow_copies: AtomicU64, views_through_swizzle: AtomicU64, + alpha_textures_held_in_red: AtomicU64, view_shadows: AtomicU64, attachment_read_splits: AtomicU64, attachment_read_clears: AtomicU64, @@ -4465,6 +4466,13 @@ impl Counters { self.views_through_swizzle.fetch_add(1, Ordering::Relaxed); } + /// An `A8Unorm` texture over a buffer's bytes was made as an `R8Unorm` texture's storage, + /// read through a swizzle (`texture/red_held_alpha.rs`). + pub(crate) fn alpha_texture_held_in_red(&self) { + self.alpha_textures_held_in_red + .fetch_add(1, Ordering::Relaxed); + } + /// A texture view's shadow image was made (`texture/view_shadow.rs`): the view's own, or /// the render shadow of a view its bindings present through a swizzle. pub(crate) fn view_shadow_made(&self) { @@ -8547,6 +8555,12 @@ pub struct WorkCounts { /// own format: a view in other channels of its image's bits (an `A8Unorm` view of an /// `R8Unorm` texture), which neither copies nor announces (`texture/view_shadow.rs`). pub views_through_swizzle: u64, + /// `A8Unorm` textures over a buffer's bytes that only read, made as an `R8Unorm` texture's + /// storage over them (in place where that texture is) and read through + /// `(Zero, Zero, Zero, Red)`, where the device's linear `A8_UNORM` image lacks what its + /// optimal one does and its linear `R8_UNORM` image lacks nothing (NVIDIA's linear + /// `A8_UNORM` samples, filters and copies nothing): `texture/red_held_alpha.rs`. + pub alpha_textures_held_in_red: u64, /// Shadow images of texture views made (`texture/view_shadow.rs`, `shadowed-texture-view`): /// a view's own, or the render shadow of one of `views_through_swizzle`, made at the first /// pass that renders into it. @@ -10077,6 +10091,8 @@ impl Device { new_buffer_bytes_cleared: counters.new_buffer_bytes_cleared.load(Ordering::Relaxed), new_buffer_zero_fills: counters.new_buffer_zero_fills.load(Ordering::Relaxed), views_through_swizzle: counters.views_through_swizzle.load(Ordering::Relaxed), + alpha_textures_held_in_red: (counters.alpha_textures_held_in_red) + .load(Ordering::Relaxed), view_shadows: counters.view_shadows.load(Ordering::Relaxed), attachment_read_splits: counters.attachment_read_splits.load(Ordering::Relaxed), attachment_read_clears: counters.attachment_read_clears.load(Ordering::Relaxed), diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index ab8b38f42..575e6bc8d 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2587,6 +2587,7 @@ field metal2vulkan-runtime::WorkCounts.addressable_buffers_made pub addressable_ field metal2vulkan-runtime::WorkCounts.ahead_framebuffers_made pub ahead_framebuffers_made: u64 field metal2vulkan-runtime::WorkCounts.ahead_nanos pub ahead_nanos: u64 field metal2vulkan-runtime::WorkCounts.ahead_render_passes_made pub ahead_render_passes_made: u64 +field metal2vulkan-runtime::WorkCounts.alpha_textures_held_in_red pub alpha_textures_held_in_red: u64 field metal2vulkan-runtime::WorkCounts.arena_pools_made pub arena_pools_made: u64 field metal2vulkan-runtime::WorkCounts.argument_objects_retained pub argument_objects_retained: u64 field metal2vulkan-runtime::WorkCounts.argument_samplers_taken pub argument_samplers_taken: u64