A render target's channel-order sibling views are views of its image

A pass rendering into a BGRA8Unorm or BGRA8Unorm_sRGB view of an
RGBA8Unorm texture made without PixelFormatView usage (and the same for
BGRA8Unorm and the sRGB twins) rendered through a shadow image of the
view's format, copied in before the pass and back after it
(shadowed-texture-view, about 50-95 us a command buffer at 64x64): the
texture's image was mutable only for its sRGB twin's formats, so Vulkan
made no view of it in the sibling's format.

Basis, documented on both sides:
- Vulkan: VkImageFormatListCreateInfo (core 1.2), pViewFormats
  "specifying all formats which can be used when creating views of this
  image", with MUTABLE_FORMAT; the formats are of one class.
- Metal: MTLTextureUsage.pixelFormatView, "Don't set this option if your
  texture view only converts between linear space and sRGB. For example,
  if your texture uses the rgba8Unorm pixel format and your texture view
  uses bgra8Unorm_srgb." Plain Metal makes the BGRA8Unorm view too and
  renders through it (its API validation stops it; view_attachments).

Change (image_plan.rs): a color texture of those four formats with
RenderTarget usage lists, besides its sRGB twin's views, the views of
its sibling and the sibling's twin (reordered_views, reordered_formats),
in the single-sample and the multisample plan; a word attachment's view
is left out as for the twin. A texture that renders nothing lists none:
its sibling views already read the image through a swizzle. The view's
other uses then view the image too (takes_views_of).

Compression: expected, not measured. Mesa's sources suggest RADV before
GFX11 keeps no DCC for a list whose formats' channel orders differ, ANV
keeps CCS_E for formats of the same channel bits, RADV GFX11+ keeps DCC
for any list; NVIDIA documents nothing. The A/B lane
view_attachments::reordered_view_render_costs (the parent commit)
measures it per driver.

Tests: image_plan::render_targets_take_their_channel_order_sibling_s_views
(every committed profile: the sibling and its twin taken where the
device renders them directly, none for a texture that only reads, none
for RGBA16Float); runtime_lane::view_attachments::
a_render_target_s_reordered_views_render_in_place (draws through both
views, Metal's bytes, no shadowed-texture-view).
Docs: the ShadowedTextureView notice (degradation.rs, API.md), the
FAST-PATHS view bullet, view_shadow.rs.

Verified: not yet built.
This commit is contained in:
Anees Iqbal committed 2026-11-12 03:56:51 -08:00
1 parent 99d83d06f4
commit cdcd787fb8
7 files changed
+159 -15

No files matched your search

@@ -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
@@ -390,6 +390,27 @@ pub(super) fn srgb_twin(pf: PixelFormat) -> Option<PixelFormat> {
})
}
/// 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<PixelFormat> {
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<Format> {
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,
@@ -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
@@ -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:
//!
@@ -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
+4 -2
View File
@@ -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
+4 -1
View File
@@ -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