Files
metal2vulkan/crates/metal2vulkan-runtime/src/texture_placement.rs
T
Anees Iqbal 6575c031f5 In place: 1- and 2-byte texels a shader writes, checked apart by the device's check
In every guest session reims measured (Iris Xe and Radeon 780M, macOS 26.7.2 guests),
host_texture_shadows_store_width was 114, from 23 distinct R8Unorm, A8Unorm and R16 textures a
shader writes (32x32 at 256-byte rows among them). They were shadows because the in-place
check's write kernel stored only 4-, 8- and 16-byte texels (STORED_TEXEL_BYTES,
ShadowCause::StoreWidth), so no check covered a driver's writes of narrower ones.

What changes:
- in_place.rs (from selfread's parked 0c587bcad5): Access gains NarrowWrites and
  NarrowBufferWrites, with 36 verdicts instead of 24. Access::written picks the writes check of
  a texel width and padding. STORED_TEXEL_BYTES holds 1, 2, 4, 8 and 16; NARROW_TEXEL_BYTES
  holds 1 and 2. in_place asks the written check in place of Writes. The report names
  "narrow writes" and "narrow buffer writes". CHECK_VERSION goes to 9 (development's dma-buf
  import fix took 8).
- in_place_check.rs: NARROW_STORE_SHAPES, rounds of their own for the narrow accesses, through
  the copy-in kernel's R8_UINT and R16_UINT views (a801dac7d7). The shapes are packed R8 256x16
  and R16 128x16 at 256; R8 61x37 and R16 45x29 at 256, whose rows end inside a word and whose
  heights are odd; the 16- and 64-byte tight pitches; and an R8 1024x1024. The source buffer
  holds a word per texel, the texel's bytes low and zeros above, since CopyIn takes 4*words()
  bytes a texel. A narrow round's change fails only the narrow verdicts, never a 4/8/16-byte
  texture's. A device whose kernel or image is not made (no shaderStorageImageExtendedFormats)
  leaves them unchecked.
- in_place::storable: one helper for a render target's storage usage, used by route.rs and
  storage_form.rs. It asks Access::written(texel, Padding::Kept) instead of Writes, so a narrow
  render target never takes the 4-byte verdict. writes_reach (the storage usage an image that
  does not render takes beyond its usage) and fresh_pages_take ask the written check of the
  texels' width; InPlaceWatch carries the width.
- InPlaceAccess::Writes covers the narrow accesses. Docs (in_place, the check, API.md, HOWTO,
  FAST-PATHS row, runtime README, WorkCounts, HostPageTexturePolicy, TextureShadowReason) say
  1, 2, 4, 8 and 16 bytes, with narrow writes checked apart. The lane's VERDICTS goes to 36.

Tests:
- The decision test runs every width, 3 bytes for StoreWidth among them, against narrow answers
  that differ from their wide twins'. It also checks storable.
- Shape tests cover the narrow set: tight pitches, row ends at 16/64/128/256, free padding.
- New: each_check_of_storage_writes_stores_its_texels_width. A narrow tally case.
- New device test narrow_writes_device: the guest's 32x32 R8Unorm and A8Unorm (and RG8Unorm and
  R16Unorm) at 256-byte rows over a no-copy buffer. Each takes its narrow verdict's path, its
  placement query agrees, a kernel's writes reach the bytes, and the padding and the bytes past
  the last row are kept.
- in_place_storage adds an R8Unorm render target.

Verified: not yet built; the build, clippy, unit and device runs follow as jobs.
(cherry picked from commit dbb985df199b1c46bb783aafbd50efd40bcab187)
2026-11-08 01:47:31 -08:00

264 lines
14 KiB
Rust

//! Where a texture over a buffer's bytes lies, answered before it is made
//! ([`Buffer::texture_placement`](crate::Buffer::texture_placement)): by the route its making
//! takes (`texture/route.rs`), the same function, so the answer and the making never disagree.
use crate::in_place::{self, Refusal, ShadowCause};
use crate::texture::{HostWay, Route};
/// Where a texture over a buffer's bytes lies if made now with the same arguments, as
/// [`Buffer::texture_placement`](crate::Buffer::texture_placement) and
/// [`Buffer::texture_placement_with_iosurface_plane`](crate::Buffer::texture_placement_with_iosurface_plane)
/// answer it. A runtime extension: Metal has no such query, as a Mac's GPU works on a texture over
/// the caller's pages in place, where the runtime takes an image in place only where the device's
/// own check passed (docs/API.md, textures over host pages).
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TexturePlacement {
/// A linear image aliasing the buffer's host pages, in place: the device's check passed for
/// the pages' kind, for every access the texture's usage takes and for texels that lie in the
/// device's lines of memory as its own do, and the device made the image of the texture's
/// plan at its pitch, where its requirements take the bytes' place and memory type (the query
/// made it unbound and released it). Over pages imported as dma-bufs the image's making may
/// still refuse at the bind, where the caller's source gives no one dma-buf of the pages
/// ([`InPlaceRefusal::Source`]) or the driver refuses its import ([`InPlaceRefusal::DmaBuf`]):
/// the texture is then a shadow, its notice (`shadowed-texture`) naming why.
InPlace,
/// A shadow now, as the device's check has not answered for the pages' kind and the
/// texture's accesses: once it does, at the commit of the device's only uncommitted command
/// buffer, the texture moves in place with no copy where the verdicts pass and the device
/// makes its image in place (`WorkCounts::host_textures_moved_in_place`), and stays a shadow
/// where they do not or the device refuses the image, as one made then would be.
/// [`Device::settle_in_place_checks`](crate::Device::settle_in_place_checks) starts the check
/// and says when its verdicts are known; the same query then answers what a texture made after
/// takes.
Pending,
/// A shadow: an image of the runtime's own memory, which the encoders copy the texels into
/// from the buffer before a command buffer's uses and back after its writes, announced
/// (`shadowed-texture`) but for a device copy. Why.
Shadow(TextureShadowReason),
/// The buffer's bytes are no host pages (a buffer of the device's own memory, or the caller's
/// `VkBuffer`): the texture is a linear image aliasing the buffer's memory where the device
/// lays one out as Metal does, else a shadow. The device's check does not decide it. Its rows
/// (and slices) lie one after another, or the buffer declares the bytes between them
/// unobserved ([`BufferDescriptor::padding_unobserved`](crate::BufferDescriptor::padding_unobserved)),
/// which the image then holds; else the answer is
/// [`TextureShadowReason::BytesBetweenRows`].
NotHostPages,
/// A texture of a shape no image in place takes, over any buffer: a texture buffer (its
/// functions view the buffer's bytes, or a staging copy of them), a rotated texture, a
/// two-plane YCbCr or extended-range texture (each an image of its own whose copies turn or
/// convert the texels), or one whose texels Metal lays out apart from an IOSurface plane
/// (memory of its own). [`Buffer::new_texture_with_iosurface_plane`](crate::Buffer::new_texture_with_iosurface_plane)
/// says how each is made.
OtherShape,
}
/// Why a texture over a buffer's bytes is a shadow ([`TexturePlacement::Shadow`]). A runtime
/// extension: Metal has none.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TextureShadowReason {
/// The device's policy asks a shadow of every texture over host pages
/// (`HostPageTexturePolicy::Shadow`, one of `DevicePolicy::exact_answers`).
Policy,
/// A shader writes the texture (`ShaderWrite` or `ShaderAtomic`), and its texels are
/// `texel_bytes` wide, a width of which the check's kernel stores none (it stores 1, 2, 4, 8
/// and 16 bytes, every width a buffer backs): no check covers a driver's writes of such
/// texels.
StoreWidth {
/// The bytes of one texel.
texel_bytes: u64,
},
/// The device's check found `access` over the pages' kind changing a byte the CPU wrote beside
/// the texels, for texels that lie in the device's lines as `row_ends` says.
CheckFailed {
/// The access the check failed for: the first of the texture's whose verdict did not pass.
access: InPlaceAccess,
/// How the texture's texels lie in the device's lines of memory.
row_ends: InPlaceRowEnds,
},
/// The device's check made no image over the pages' kind for `access`, for texels that lie in
/// the device's lines as `row_ends` says: the device makes no such import, image or operation,
/// or the check could not run to its end.
Unchecked {
/// The access the check made no image for: the first of the texture's whose verdict did
/// not pass.
access: InPlaceAccess,
/// How the texture's texels lie in the device's lines of memory.
row_ends: InPlaceRowEnds,
},
/// The device makes no image in place over the texels: why.
Refused(InPlaceRefusal),
/// A tiled texture of `levels` levels, more than one
/// ([`Buffer::new_tiled_texture`](crate::Buffer::new_tiled_texture)), over any buffer: one
/// image of its own holds every level, its first a copy of the buffer's bytes kept in step
/// with them, as no image over the buffer's memory holds levels past the first.
Mipmapped {
/// The texture's level count.
levels: u32,
},
/// The texels run past the buffer's length, which no image over the buffer's memory reaches:
/// the texture's tail holds those past it, zero when made.
PastLength,
/// Over a buffer of the device's own memory ([`TexturePlacement::NotHostPages`]'s): its rows
/// (or slices) lie with bytes of the buffer between them that are not its texels, or share
/// bytes, which an image over the memory would hold, and Vulkan lets an image's layout moves
/// and a render pass's load and store write the bytes it holds; the buffer does not declare
/// the bytes between rows unobserved
/// ([`BufferDescriptor::padding_unobserved`](crate::BufferDescriptor::padding_unobserved),
/// which no rows that share bytes take).
BytesBetweenRows,
/// On a discrete GPU, a `Managed` texture over a no-copy buffer's bytes (not an IOSurface
/// plane's) that only samples or reads, over memory that carries a log of its writes, tracked,
/// one slice laid out as one copy: a copy in the device's memory whose copies in take only the
/// rows the log reports changed (`WorkCounts::device_copy_textures`), the GPU's copy of the two
/// a `Managed` resource is on a Mac with a discrete GPU. Not announced.
DeviceCopy,
}
/// What a texture over host pages does with its image in place, each checked apart by the
/// device's check: the operations that may touch bytes bound to the image beside its texels
/// differ. A runtime extension: Metal has none.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum InPlaceAccess {
/// Its usage samples or reads it alone: its image takes its first layout move.
Reads,
/// Its usage renders into it (`RenderTarget`): its image also takes render passes' load and
/// store operations and the feedback loop moves.
Renders,
/// Its usage writes it through a shader (`ShaderWrite` or `ShaderAtomic`): its image also
/// takes kernels' storage writes of its texels, checked by their width: texels of 1 or 2
/// bytes apart from those of 4, 8 or 16. Of a texture over a buffer's rows whose rows, each
/// to its pitch, start and end on a line, its buffer holding the last row to its pitch, the
/// check that lets those writes change the bytes between a row's texels and its pitch, which
/// Metal's GPUs differ on; else the one that keeps every byte beside the texels.
Writes,
}
/// How a texture's texels lie in the device's lines of memory (`nonCoherentAtomSize` bytes at a
/// multiple of it), each checked apart by the device's check: a device may write back a line that
/// holds a texel whole, putting back an older value over a CPU write beside the texels in that
/// line. A runtime extension: Metal has none.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum InPlaceRowEnds {
/// A line holds a texel and a byte beside the texels: between two rows or two slices, before
/// the first texel or after the last.
InLines,
/// Every line holds texels alone or bytes beside them alone: the texels start and end on a
/// line, and so does each row (or slice) where bytes lie between rows (or slices).
OnLines,
}
/// Why the device makes no image in place over a texture's texels
/// ([`TextureShadowReason::Refused`]), as `WorkCounts::host_texture_shadows_refused_*` counts
/// them. [`Buffer::texture_placement`](crate::Buffer::texture_placement) answers each one the
/// image's making decides before it binds memory, by the same steps: all but `Source`, and
/// `DmaBuf` where the driver refuses the dma-buf's import, which only the bind finds over pages
/// imported as dma-bufs ([`TexturePlacement::InPlace`]). A runtime extension: Metal has none.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum InPlaceRefusal {
/// Its rows share bytes (an IOSurface plane's GPU rows): no image holds a texel twice.
Rows,
/// The buffer's bytes were detached from their pages (`Buffer::detach_backing`): no image is
/// bound to them.
Detached,
/// The device makes no such image of the texture's plan: its format, features, usage or
/// limits, or an import only a dedicated allocation takes.
Image,
/// No image the device makes lays the rows (or slices) at the buffer's pitch.
Pitch,
/// The image's alignment, size or memory type misses the bytes' place, or the buffer's bytes
/// are a copy of the caller's pages in memory of the device's own.
Placement,
/// Over pages imported as dma-bufs: the caller's source made no one dma-buf of the texture's
/// own pages.
Source,
/// Over pages imported as dma-bufs: no `DRM_FORMAT_MOD_LINEAR` image over one dma-buf of the
/// texture's own pages was made for another reason (the modifier's features or limits, the
/// image's memory past the pages or of another type, or the driver's import).
DmaBuf,
}
impl InPlaceAccess {
/// The access `access` is.
fn of(access: in_place::Access) -> InPlaceAccess {
match access {
in_place::Access::Reads => InPlaceAccess::Reads,
in_place::Access::Renders => InPlaceAccess::Renders,
in_place::Access::Writes
| in_place::Access::BufferWrites
| in_place::Access::NarrowWrites
| in_place::Access::NarrowBufferWrites => InPlaceAccess::Writes,
}
}
}
impl InPlaceRowEnds {
/// The row ends `rows` are.
fn of(rows: in_place::RowEnds) -> InPlaceRowEnds {
match rows {
in_place::RowEnds::InLines => InPlaceRowEnds::InLines,
in_place::RowEnds::OnLines => InPlaceRowEnds::OnLines,
}
}
}
impl InPlaceRefusal {
/// The refusal `refusal` is.
fn of(refusal: Refusal) -> InPlaceRefusal {
match refusal {
Refusal::Rows => InPlaceRefusal::Rows,
Refusal::Detached => InPlaceRefusal::Detached,
Refusal::Image => InPlaceRefusal::Image,
Refusal::Pitch => InPlaceRefusal::Pitch,
Refusal::Placement => InPlaceRefusal::Placement,
Refusal::Source => InPlaceRefusal::Source,
Refusal::DmaBuf => InPlaceRefusal::DmaBuf,
}
}
}
impl Route {
/// Where a texture that takes this route lies, as the query answers it.
pub(crate) fn placement(&self) -> TexturePlacement {
let cause = match self {
Route::Levels(levels) => {
let levels = *levels;
return TexturePlacement::Shadow(TextureShadowReason::Mipmapped { levels });
}
Route::Past => return TexturePlacement::Shadow(TextureShadowReason::PastLength),
Route::Device { holds: Ok(()) } => return TexturePlacement::NotHostPages,
Route::Device { holds: Err(_) } => {
return TexturePlacement::Shadow(TextureShadowReason::BytesBetweenRows)
}
Route::Host { way, .. } => match way {
HostWay::InPlace { .. } => return TexturePlacement::InPlace,
HostWay::DeviceCopy => {
return TexturePlacement::Shadow(TextureShadowReason::DeviceCopy)
}
HostWay::Shadow(cause) => cause,
},
};
let reason = match *cause {
ShadowCause::CheckPending(..) => return TexturePlacement::Pending,
ShadowCause::Exact => TextureShadowReason::Policy,
ShadowCause::StoreWidth(texel_bytes) => TextureShadowReason::StoreWidth { texel_bytes },
ShadowCause::CheckFailed(_, access, rows) => TextureShadowReason::CheckFailed {
access: InPlaceAccess::of(access),
row_ends: InPlaceRowEnds::of(rows),
},
ShadowCause::Unchecked(_, access, rows) => TextureShadowReason::Unchecked {
access: InPlaceAccess::of(access),
row_ends: InPlaceRowEnds::of(rows),
},
ShadowCause::Refused(refusal, _) => {
TextureShadowReason::Refused(InPlaceRefusal::of(refusal))
}
};
TexturePlacement::Shadow(reason)
}
}