diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index 1e88fce2e..719a4831a 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -1075,8 +1075,10 @@ degradations! { /// that image takes no Vulkan view of its format: a view of another component layout on /// a device whose Vulkan views reinterpret no image's components (a portability subset /// without `imageViewFormatReinterpretation`, or an adopted device whose consumer did not - /// say it enabled it), or a view of another format than the texture's own and its sRGB - /// twin's of a texture made without `PixelFormatView` usage (`texture/view_shadow.rs`). + /// say it enabled it), or a view of a texture made without `PixelFormatView` usage in + /// another format than the texture's own, its sRGB twin's and, where the texture renders + /// and is `RGBA8Unorm`, `BGRA8Unorm` or a twin of one, its channel-order sibling's and + /// that sibling's twin's (`texture/view_shadow.rs`). /// A view in other channels of its image's bits (an `A8Unorm` view of an `R8Unorm` /// texture) is the image itself to its bindings, through a swizzle, and takes a shadow /// only for a pass that renders into it: Vulkan views the image in no format of the diff --git a/crates/metal2vulkan-runtime/src/image_plan.rs b/crates/metal2vulkan-runtime/src/image_plan.rs index 92d1fb0a4..b162be311 100644 --- a/crates/metal2vulkan-runtime/src/image_plan.rs +++ b/crates/metal2vulkan-runtime/src/image_plan.rs @@ -390,6 +390,27 @@ pub(super) fn srgb_twin(pf: PixelFormat) -> Option { }) } +/// The other member of `pf`'s channel-order pair (`RGBA8Unorm` and `BGRA8Unorm`, and their sRGB +/// twins), and that member's sRGB twin: views of a texture made without `PixelFormatView` usage. +/// Apple documents the second as needing no such usage (`MTLTextureUsage.pixelFormatView`: "if +/// your texture uses the `rgba8Unorm` pixel format and your texture view uses `bgra8Unorm_srgb`"); +/// plain Metal makes the first too and renders through it, which its API validation stops +/// (`runtime_lane::view_attachments`). Empty for any other format. +pub(super) fn reordered_views(pf: PixelFormat) -> Vec { + use PixelFormat as P; + let sibling = match pf { + P::RGBA8Unorm => P::BGRA8Unorm, + P::BGRA8Unorm => P::RGBA8Unorm, + P::RGBA8UnormSRGB => P::BGRA8UnormSRGB, + P::BGRA8UnormSRGB => P::RGBA8UnormSRGB, + _ => return Vec::new(), + }; + [Some(sibling), srgb_twin(sibling)] + .into_iter() + .flatten() + .collect() +} + /// The member of `pf`'s compressed family that names it: block-compressed formats whose texels /// are the same blocks read as sRGB or linear, signed or unsigned, or HDR or LDR. Metal views a /// texture as another member of its family with `PixelFormatView` usage (`BC4_RUnorm` as @@ -855,6 +876,7 @@ fn plan_held( .filter(|&f| compatible(format, f, true) && Some(f) != twin_word), ); } + views.extend(reordered_formats(pf, format, usage, usage_flags, profile)); views.insert(format); // A usage the image's own format lacks, which a view format has (a storage alias, a sampled // twin, a word attachment), needs extended usage. @@ -998,6 +1020,7 @@ fn word_samples( .filter(|&f| compatible(format, f, true) && Some(f) != twin_word), ); } + views.extend(reordered_formats(pf, format, usage, usage_flags, profile)); let extended = [ (ImageUsageFlags::SAMPLED, FormatUsage::SampledImage), ( @@ -1036,6 +1059,34 @@ fn word_samples( }) } +/// The formats of the views of `pf`'s channel-order sibling and its twin ([`reordered_views`]) +/// that an image of `format` lists for a texture of Metal `usage` that renders (`RenderTarget`), +/// planned with `usage_flags`: a pass renders into such a view, which Metal makes without +/// `PixelFormatView` usage, through a Vulkan view of the image itself rather than a shadow +/// (`texture/view_shadow.rs`), and its other uses view the image too. Not the view of a sibling's +/// word (`WordAttachment`), as for the sRGB twin. None for a texture that renders nothing: its +/// sibling's views present the image through a swizzle where they can. +fn reordered_formats( + pf: PixelFormat, + format: Format, + usage: TextureUsage, + usage_flags: ImageUsageFlags, + profile: &DeviceProfile, +) -> Vec { + if !usage.contains(TextureUsage::RENDER_TARGET) { + return Vec::new(); + } + reordered_views(pf) + .into_iter() + .flat_map(|view| { + let word = WordAttachment::of(view, profile).map(|w| w.word_format()); + view_formats(view, usage_flags, profile) + .into_iter() + .filter(move |&f| compatible(format, f, true) && Some(f) != word) + }) + .collect() +} + /// The Vulkan usages [`view_formats`] reads: the views it lists follow these alone. pub(super) const VIEWED_USAGES: [ImageUsageFlags; 4] = [ ImageUsageFlags::SAMPLED, diff --git a/crates/metal2vulkan-runtime/src/tests/image_plan.rs b/crates/metal2vulkan-runtime/src/tests/image_plan.rs index 5e79e1979..6f9a26490 100644 --- a/crates/metal2vulkan-runtime/src/tests/image_plan.rs +++ b/crates/metal2vulkan-runtime/src/tests/image_plan.rs @@ -7,8 +7,8 @@ use super::super::copy_plan::{copy_unit, BlitOption, ImageRegion}; use super::super::driver_defects; use super::super::image_plan::{ block_family, check_image_limits, compatible, cube_compatible, holds_srgb_twin, plan_image, - sample_count, srgb_twin, view_formats, AnyViews, DepthImageFacts, ImageFacts, ImagePlan, - ImageRefusal, VIEWED_USAGES, + reordered_views, sample_count, srgb_twin, takes_views_of, view_formats, AnyViews, + DepthImageFacts, ImageFacts, ImagePlan, ImageRefusal, VIEWED_USAGES, }; use super::super::memory::MemoryClass; use super::super::pvrtc::{holds_blocks, image_buffer_region, image_region, CARRIERS}; @@ -621,6 +621,53 @@ fn srgb_twins_share_one_mutable_image() { } } +/// A texture of an 8-bit four-channel format that renders lists the views of its channel-order +/// sibling and the sibling's twin (`BGRA8Unorm` and `BGRA8Unorm_sRGB` of an `RGBA8Unorm` texture), +/// which Metal makes without `PixelFormatView` usage, where the device renders them directly: its +/// image takes them, and a pass renders into such a view through a view of the image itself. One +/// that renders nothing takes none of them, nor does a format without a sibling. +#[test] +fn render_targets_take_their_channel_order_sibling_s_views() { + use PixelFormat as P; + let rt = TextureUsage::SHADER_READ | TextureUsage::RENDER_TARGET; + for (name, desc, device) in committed() { + let facts = facts(&desc, &device); + let profile = device.profile(); + for (pf, sibling) in [ + (P::RGBA8Unorm, P::BGRA8Unorm), + (P::BGRA8Unorm, P::RGBA8Unorm), + (P::RGBA8UnormSRGB, P::BGRA8UnormSRGB), + (P::BGRA8UnormSRGB, P::RGBA8UnormSRGB), + ] { + let what = format!("{name}: {pf:?}"); + let views = reordered_views(pf); + assert_eq!(views, [sibling, srgb_twin(sibling).unwrap()], "{what}"); + let l = layout(TextureType::Type2D, pf, 1, 1); + let Ok(plan) = plan_image(&l, rt, false, &facts) else { + continue; + }; + check_plan(&name, pf, rt, &plan, &facts); + for &view in &views { + let direct = WordAttachment::of(view, profile).is_none(); + let listed = view_formats(view, plan.usage, profile) + .iter() + .all(|&f| compatible(plan.format, f, true)); + assert_eq!( + takes_views_of(&plan, view, profile), + direct && listed, + "{what}: {view:?} -> {plan:?}" + ); + } + let read = plan_image(&l, TextureUsage::SHADER_READ, false, &facts).expect(&what); + assert!(!takes_views_of(&read, sibling, profile), "{what}: {read:?}"); + } + let l = layout(TextureType::Type2D, P::RGBA16Float, 1, 1); + assert!(reordered_views(P::RGBA16Float).is_empty()); + let plan = plan_image(&l, rt, false, &facts).expect(&name); + assert!(plan.view_formats.is_empty(), "{name}: {plan:?}"); + } +} + /// A heap's texture without GPU-optimized contents takes the image of one of `PixelFormatView` /// usage (`MUTABLE_FORMAT`, no list of formats), as Metal lays out a color texture with the /// property off; with it on, the image of its usage, an sRGB twin's list kept. A device's own diff --git a/crates/metal2vulkan-runtime/src/texture/view_shadow.rs b/crates/metal2vulkan-runtime/src/texture/view_shadow.rs index 49bd90551..6895783a1 100644 --- a/crates/metal2vulkan-runtime/src/texture/view_shadow.rs +++ b/crates/metal2vulkan-runtime/src/texture/view_shadow.rs @@ -8,9 +8,10 @@ //! view's format (`view_plan.rs`). //! - **Another format of a texture made without `PixelFormatView` usage**: plain Metal makes the //! view (its API validation stops it) and renders through it the same; the texture's image was -//! made mutable, if at all, only for its sRGB twin's formats (`image_plan.rs`), which keeps the -//! driver's compression of it, so a view of another format of its class (`BGRA8Unorm` of an -//! `RGBA8Unorm` texture) is not a view of that image. +//! made mutable, if at all, only for its sRGB twin's formats and, where it renders, its +//! channel-order sibling's and that sibling's twin's (`image_plan::reordered_views`), so a view +//! of another format of its class (`R32Uint` of an `RGBA8Unorm` texture, or `BGRA8Unorm` of one +//! that renders nothing) is not a view of that image. //! //! Such a view: //! diff --git a/crates/metal2vulkan-validation/src/runtime_lane/view_attachments.rs b/crates/metal2vulkan-validation/src/runtime_lane/view_attachments.rs index 91591450b..78aac9fd5 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/view_attachments.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/view_attachments.rs @@ -39,11 +39,11 @@ use metal2vulkan::pipeline::{ColorAttachmentDescriptor, RenderPipelineDescriptor use metal2vulkan_devtools::llvm_assemble; use metal2vulkan_runtime::{ Attachment, BlitCommandEncoder, BlitOption, Buffer, ClearColor, CommandBuffer, - CommandBufferStatus, CommandQueue, CpuCacheMode, Device, Error, HazardTrackingMode, - HeapDescriptor, HeapType, Invalid, Library, LoadAction, Origin, PipelineOptions, Problem, - Region, RenderCommandEncoder, RenderPassDescriptor, RenderPipelineState, ResourceOptions, - ScissorRect, Size, StorageMode, StoreAction, Texture, TextureDescriptor, TextureProblem, - ViewProblem, + CommandBufferStatus, CommandQueue, CpuCacheMode, Degradation, Device, Error, + HazardTrackingMode, HeapDescriptor, HeapType, Invalid, Library, LoadAction, Origin, + PipelineOptions, Problem, Region, RenderCommandEncoder, RenderPassDescriptor, + RenderPipelineState, ResourceOptions, ScissorRect, Size, StorageMode, StoreAction, Texture, + TextureDescriptor, TextureProblem, ViewProblem, }; use std::path::Path; @@ -1497,6 +1497,44 @@ fn a_layered_pass_renders_a_line_s_slices() { }); } +/// An `RGBA8Unorm` texture that renders, made without `PixelFormatView` usage, renders into its +/// `BGRA8Unorm` and `BGRA8Unorm_sRGB` views in place: its image lists their formats, so each is a +/// view of the image (no `shadowed-texture-view`), and stores its format's bytes as Metal's does +/// (the probe's `v_*` bytes, in the view's channel order). Apple documents the `bgra8Unorm_srgb` +/// view as needing no `PixelFormatView` usage; plain Metal makes the `BGRA8Unorm` one too. +#[test] +fn a_render_target_s_reordered_views_render_in_place() { + const BGRA_UNORM: ([u8; 4], [u8; 4]) = ([0x99, 0x66, 0x33, 0xcc], [0xbf, 0x40, 0x80, 0xff]); + let messages = Messages::default(); + let cache = ScratchCache::new().unwrap(); + { + let device = validated_device(TEST_DEVICE, &messages, cache.path()).unwrap(); + let lane = Lane::new(&device); + for (pf, want, slack) in [ + (PixelFormat::BGRA8Unorm, BGRA_UNORM, 0), + (PixelFormat::BGRA8UnormSRGB, BGRA_SRGB, 1), + ] { + let texture = lane.texture(PixelFormat::RGBA8Unorm, RT, PLAIN); + let viewed = texture.new_texture_view(pf).unwrap(); + let pipeline = lane.solid(pf, 1, false); + lane.run( + &pass(&viewed, (0, 0), LoadAction::Clear, None), + |l, e, h| l.left(e, h, &pipeline, COLOR), + ); + let what = format!("{}: a {pf:?} view", device.name()); + check(&lane.bytes(&texture, 0, 0), &halves(want), slack, &what); + } + let notices = counts(&messages.take_notices()); + assert_eq!( + notices.get(&Degradation::ShadowedTextureView), + None, + "{}: {notices:?}", + device.name() + ); + } + layer_reported_nothing_at_all(&messages); +} + /// What a 2048x2048 `RGBA8Unorm` render target made without `PixelFormatView` usage costs the /// GPU, without the validation layer: a pass that clears it and stores, a blit copying it into /// another such texture, and a pass that clears its `BGRA8Unorm` view and stores, each its own diff --git a/docs/API.md b/docs/API.md index 7dcb49bfa..4fd5e5f8d 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2632,8 +2632,10 @@ bound. is an image of its own, a shadow of the viewed image's texels, where that image takes no Vulkan view of its format: a view of another component layout on a device whose Vulkan views reinterpret no image's components (a portability subset without `imageViewFormatReinterpretation`, or an - adopted device whose consumer did not say it enabled it), or a view of another format than the - texture's own and its sRGB twin's of a texture made without `PixelFormatView` usage. A view in + adopted device whose consumer did not say it enabled it), or a view of a texture made without + `PixelFormatView` usage in another format than the texture's own, its sRGB twin's and, where the + texture renders and is `RGBA8Unorm`, `BGRA8Unorm` or a twin of one, its channel-order sibling's + and that sibling's twin's. A view in other channels of its image's bits (an `A8Unorm` view of an `R8Unorm` texture) is the image itself to its bindings, through a swizzle, and takes a shadow only for a pass that renders into it: Vulkan views the image in no format of the view's. Cost: its texels are copied from its diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index f69ddbca5..2822be4e7 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -376,7 +376,10 @@ recorded here. 3.0-6.1 and 4.6-8.6 ms. An ordinary buffer's packed rows avoid it. - **A view of another format without `PixelFormatView` usage** is a shadow image copied at each use (`shadowed-texture-view`): about 50 to 95 µs a command buffer for a 64x64 texture. Making the - texture with `PixelFormatView` usage avoids it. A view in other channels of its image's bits (an + texture with `PixelFormatView` usage avoids it. An `RGBA8Unorm` or `BGRA8Unorm` texture, or a + twin of one, that renders lists its channel-order sibling's formats and that sibling's twin's in + its image (`BGRA8Unorm` and `BGRA8Unorm_sRGB` of an `RGBA8Unorm` texture), so its views in those + formats are views of the image itself. A view in other channels of its image's bits (an `A8Unorm` view of an `R8Unorm` texture) binds the image itself through a swizzle, and only a pass that renders into it renders through a shadow. - **A pass that loads a texture over a buffer and does not store it** renders through an image its