Files
metal2vulkan/crates/metal2vulkan-runtime/src/buffer_texture.rs
T
Anees Iqbal 2b8b56617d An object a recovery carries answers the caller's own device handle
What: the public device() of every object whose borrow reached a handle a
recovery lets go of answers an owned Device, a clone of the generation
handle it read: Buffer, Texture, Heap, IndirectCommandBuffer, Event, Fence,
SamplerState, Library, Function, DepthStencilState, AddressPool,
DynamicLibrary, BinaryArchive and SharedTextureHandle (whose device is its
texture's). The runtime's own reads keep a borrow through a crate-private
generation() on the types they read it of (Buffer, Texture, Heap,
IndirectCommandBuffer, SamplerState, Function, BinaryArchive): 301 call
sites, so no hot path pays a clone. Each changed item's doc says the handle
is the caller's and keeps its generation, VkDevice included, while held,
past a recovery that carries the object on; MadeOn's doc states that no
borrow of it reaches the program. The validation lane's four callers that
passed the borrow on take a reference of the handle. Signature table
re-blessed (14 rows).

Why: these accessors answered a borrow into device/carry.rs's MadeOn (a
Buffer's or Event's parts, or a LineDevice's GenerationLink), whose
let_go drops that Device at the lost generation's rest while the object
lives. Safe code that held the borrow across a recovery and that rest read
a dropped Device. Metal's device property answers a retained device. The
other objects' device() (queues, command buffers, argument encoders, rate
maps, pipeline states and their function tables) answer a borrow of a
handle the object owns for its life, which no recovery lets go of.

Verified: the lane that follows runs tests::device_recovery_handles before
(it fails: one live VkDevice while the borrow is held) and after the fix;
a workspace check of the accessor change was clean under the plain cfg and
found the four validation callers under m2v_gpu and m2v_cpu_icd (job
m3-max-23584 on a probe commit).
2026-11-12 18:11:56 -08:00

2671 lines
133 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Textures over buffers: `Buffer::new_texture`, its SPI form `Buffer::new_linear_texture`, and the
//! texture buffers `Device::new_texture` makes. A 2D texture or array over a buffer is a shadow
//! image (`shadow.rs`) over these texels.
//!
//! - **Metal's rules** decide which textures a buffer backs and where they lie in it
//! (`linear_plan.rs`); what Metal stops is refused `Invalid`, and an IOSurface plane past its
//! surface of a shape that keeps no tail (below), which Metal makes, `NotImplemented`.
//! - **Tails.** A 2D texture, an array or a texture buffer whose texels run past its buffer's
//! length, which plain Metal makes up to the buffer's allocation (`linear_plan.rs`'s
//! **Length**), and a colour texture of an IOSurface plane whose texels run past the buffer
//! standing for its surface (`linear_plan.rs`'s **Beyond the plane's rows**: announced
//! `SurfaceTexelsTail`), keep the texels' bytes past the length in a tail of their own
//! ([`Texels::past`]): a runtime `Shared` buffer of its bytes from the first to the last (a
//! texture buffer's from its functions' first, its offset rounded down to 16), zero when made.
//! Its texels inside the length are the buffer's bytes, as every texture's over a buffer are.
//! A 2D texture or array is a shadow image whose copies go through the tail, as a staging
//! buffer: in, the buffer's bytes inside the length into the tail and the tail into the image;
//! back, the image into the tail and the tail's rows inside the length into the buffer. A
//! texture buffer's bindings view the tail, as a staged texture buffer's view its staging
//! buffer, with the same copies in and back but the image. A merge back takes its current
//! bytes from both and gives them back to both (`shadow_merge.rs`). The CPU's `replace_region` and `get_bytes`, and a
//! blit's rows, take each row's bytes inside the length from the buffer and the rest from the
//! tail; a copy between such texels and an image goes through a scratch buffer of the
//! encoder, as those rows (`blit_texture.rs`). The tail's uses are the buffer's bytes past its
//! length, by the same key, so the tracker orders them as the texels' own. Metal's texels there
//! are the memory after the buffer's, another allocation's bytes (docs/API.md's accepted
//! differences); the tail answers every path alike and reaches no allocation but its own.
//! - **Texture buffers** are the buffer's bytes ([`Texels`]), viewed as a texel buffer where a
//! function binds them: in the format the binding plan names, over the range it gives, made at
//! the first bind that needs it and kept with the texture. Their uses are uses of the buffer's
//! bytes, so the tracker orders them against every other use of the buffer. A
//! `Device::new_texture` texture buffer is a texture buffer over a new buffer of the
//! descriptor's options, as long as its row of texels, outside the Metal address space
//! (`address.rs`): Metal shows no address of a texture's texels, which a function reaches
//! through the texture's binding or its resource ID, so no program buffer's address depends on
//! it.
//! - **CPU access** (`replace_region`, `get_bytes`) reads and writes the buffer's bytes where
//! the texture lies in them, as the buffer's own `contents` would, for every texture over a
//! buffer.
//! - **A texel buffer view in a format of one kind** (a format the device takes for one kind of
//! texel buffer and not the other, as NVIDIA takes `R64_UINT` for storage texel buffers only):
//! the runtime's buffers are made for both kinds, and Vulkan views a buffer only in a format
//! that serves every usage the view has, so such a view names the one usage it needs
//! (`VkBufferUsageFlags2CreateInfoKHR`), which `maintenance5` allows ([`view_usage`]).
//! - **Not yet** (`NotImplemented`): that view on a device that did not enable `maintenance5`;
//! views, blits and mipmaps of textures over buffers.
use super::address::{AddressPlacement, ObjectsToken};
use super::apple_only::AppleOnly;
use super::buffer::{Buffer, ReadApart};
use super::chroma_pairs;
use super::copy_plan::{
block_extent, surface_depth_unit, CopyProblem, CopyUnit, HostLayout, ImageRegion,
};
use super::degradation::Degradation;
use super::device::lock;
use super::error::{internal, invalid, nil, vulkan, Error, Problem, RuntimeGap};
use super::exit_guard;
use super::in_place::{Padding, RowEnds};
use super::key_hash::KeyMap;
use super::linear_plan::{
apart_from_plane, is_linear, plan_buffer_texture, plane_rows_share_bytes, BufferFacts,
BufferLayout, BufferRefusal, BufferTexture, BufferTextureCall, BufferTextureProblem, Placed,
LINEAR_ALIGNMENT,
};
use super::resource::{Identity, ResourceKey};
use super::resource_ids::IdPlacement;
use super::rotated::{self, Frame, RotatedCopies};
use super::shadow::{
Backing, ShadowBuffer, ShadowCopies, ShadowSource, ShadowStaging, ShadowTarget,
};
use super::shadow_copy_back::WordView;
use super::shadow_merge::ScratchSpares;
use super::shadow_merge::UnormView;
use super::shadow_standing::StandingAcross;
use super::texture::carry::Remake;
use super::texture::surface_two_planes;
use super::texture::{IdUse, Texture};
use super::texture_descriptor::{
Creator, Refused, TextureDescriptor, TextureLayout, TextureProblem,
};
use super::texture_placement::TexturePlacement;
use super::tracker::{Access, Use, UseRange};
use super::two_planes;
use super::values::{CpuCacheMode, HazardTrackingMode, ResourceOptions, StorageMode};
use super::view_plan::ViewRecord;
use super::ycbcr::{surface_planes, IoSurfaceYCbCr};
use super::Device;
use crate::release;
use ash::vk;
use metal2vulkan::bind::{BoundTexture, BufferRange};
use metal2vulkan::device::{FormatUsage, VkFeature};
use metal2vulkan::error::{Feature, Unsupported};
use metal2vulkan::metal::format::{byte_format, ycbcr, ycbcr_format};
use metal2vulkan::metal::{PixelFormat, TextureSwizzleChannels, TextureType, TextureUsage};
use metal2vulkan::vk::{Format, ImageAspectFlags};
use std::collections::HashMap;
use std::sync::atomic::AtomicU64;
use std::sync::{Arc, Mutex};
/// A texture's texels in a buffer's bytes, where Metal lays them out.
pub(super) struct Texels {
pub(super) buffer: Buffer,
/// Where the GPU's commands find the texels in the buffer.
pub(super) layout: BufferLayout,
/// Where the CPU's `replace_region` and `get_bytes` find them: `layout`, but for an IOSurface
/// plane whose offset or pitch is not a multiple of 16 (`linear_plan.rs`).
cpu: BufferLayout,
/// Where a function reads and writes a texture buffer's texels: `layout`, but from its offset
/// rounded down to 16 bytes, as plain Metal's functions read and write a texture buffer at an
/// offset off 16 ([`Texels::into_texture_buffer`]).
shader: BufferLayout,
/// The bytes of one texel: of one block of a block-compressed texture (`blocks`).
texel_bytes: u64,
/// A block-compressed texture's blocks (a BC texture over an IOSurface plane's rows,
/// `linear_plan.rs`): `layout`'s rows are rows of them; `None` for every other texture,
/// whose rows are rows of texels.
blocks: Option<Blocks>,
pub(super) allow_gpu_optimized_contents: bool,
/// The texture's resource options, its hazard tracking mode resolved, as `resourceOptions`
/// reports them: the buffer's, which Metal's texture over a buffer takes, but an IOSurface
/// plane's descriptor's (`Buffer::new_texture_with_iosurface_plane`). Its hazard tracking
/// mode is its uses' (`Texels::tracked`).
options: ResourceOptions,
/// The texel buffer views bindings needed, by format, kind (storage or uniform) and range,
/// made at their first bind. A finite set per texture: the formats its plans view it as.
views: Arc<TexelViews>,
/// A texture buffer's staging buffer and its copies, where Vulkan views no texels at the
/// texels' offset in the buffer's `VkBuffer` (`Texels::into_texture_buffer`): its bindings view
/// the staging buffer, a shadow the encoders copy the texels into and back (`shadow.rs`).
staged: Option<Arc<ShadowCopies>>,
/// What made `buffer` for the texels, where that is not the program.
pub(super) home: TexelsHome,
/// A texture buffer's attachment twins, by the pixel format a render pass renders it in
/// (`Texture::attachment_twin`), made at the first pass that needs each.
twins: Mutex<HashMap<PixelFormat, Texture>>,
/// The alpha plane a two-plane extended-range texture over a surface's plane 0 keeps its
/// alpha bytes in, where Metal's GPU finds them, `layout` holding its color words
/// (`texture/surface_two_planes.rs`); `None` for every other texture.
alpha: Option<BufferLayout>,
/// The texture objects over the texels as a texture buffer's (`Storage::Texels`), and the
/// caller's token for a texture buffer's range (`Device::new_texture_with_range_token`).
pub(super) objects: ObjectsToken,
/// The texels lie in an IOSurface's plane (`Buffer::new_texture_with_iosurface_plane`).
surface: bool,
/// The tail of a 2D texture, an array or a texture buffer whose texels run past the buffer's
/// length (`linear_plan.rs`'s **Length**), which holds the texels' bytes there: a runtime
/// `Shared` buffer of the bytes from the texels' first to their last, laid out as in the
/// buffer from its start (`Texels::tail_base`), zero when made. Its bytes inside the buffer's
/// length mirror the buffer's for the shadow's copies, which go through it
/// ([`Texels::shadow_copies`], `into_texture_buffer`); every other access to the texels (the
/// CPU's, a blit's rows) takes the bytes inside the length from the buffer and those past it
/// from here ([`Texels::past`]). `None` for texels inside their buffer.
tail: Option<Buffer>,
/// Where the texels' bytes in place end: the buffer's length, but for a 2D array of more than
/// one slice over an IOSurface plane, whose slices past the first lie in the tail from their
/// first byte on (`linear_plan::Placed::kept`). The tail holds the texels' bytes from here.
kept: u64,
}
/// Where a texture's texels past its buffer's length lie ([`Texels::past`]): the buffer's bytes
/// from `length` on are the tail's, byte `v` of the buffer's space at `v - rebase` of the tail.
/// `length` is the buffer's own, but a 2D array's over an IOSurface plane whose slices past the
/// first lie in the tail ([`Texels::kept`]), where it is the second slice's first byte.
#[derive(Clone, Copy)]
pub(super) struct Past<'a> {
pub(super) tail: &'a Buffer,
pub(super) length: u64,
pub(super) rebase: u64,
}
/// A block-compressed texture's blocks in its texels' layout ([`Texels::blocks`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Blocks {
/// The texels of a block, across and down.
pub(super) extent: [u32; 2],
/// The texture's own texels, across and down, which its last block of a row or of a column
/// may cover only part of.
pub(super) texels: [u32; 2],
}
/// A texture's texel buffer views, by format, kind (storage or uniform) and range; destroyed
/// with it. Over a buffer's host pages, a rebind empties them, the views of the old pages going
/// with those pages (`rebind.rs`), and the next bind makes them again over the new ones.
pub(super) struct TexelViews {
device: Device,
views: Mutex<KeyMap<(Format, bool, u64, u64), vk::BufferView>>,
}
impl TexelViews {
/// The views made so far, for the pages they view to keep: none from now on.
pub(super) fn park(&self) -> ViewObjects {
let views = lock(&self.views).drain().map(|(_, view)| view).collect();
ViewObjects {
device: self.device.clone(),
views,
}
}
}
impl Drop for TexelViews {
fn drop(&mut self) {
// Once the process is exiting, the views are left to its end (`exit_guard`).
let Some(_busy) = exit_guard::enter() else {
return;
};
let raw = self.device.shared().vk();
// SAFETY: no pending command buffer uses a view: a committed command buffer holds every
// texture it binds until it completes.
unsafe {
for &view in lock(&self.views).values() {
release::object(raw, view);
}
}
}
}
/// Texel buffer views of host pages a rebind replaced, destroyed with those pages
/// ([`TexelViews::park`]).
pub(super) struct ViewObjects {
device: Device,
views: Vec<vk::BufferView>,
}
impl Drop for ViewObjects {
fn drop(&mut self) {
let Some(_busy) = exit_guard::enter() else {
return;
};
let raw = self.device.shared().vk();
// SAFETY: the pages that keep them go once every command buffer that used them
// completed.
unsafe {
for &view in &self.views {
release::object(raw, view);
}
}
}
}
/// Where an IOSurface's plane lies in the buffer that stands for the surface
/// ([`Buffer::new_texture_with_iosurface_plane`], the stand-in for Metal's
/// `newTextureWithDescriptor:iosurface:plane:`, which reads these from the `IOSurfaceRef`).
///
/// Made with [`IoSurfacePlane::new`] and the `with_` methods (the struct is `#[non_exhaustive]`, so
/// a field added later breaks no caller).
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct IoSurfacePlane {
/// The plane's first byte in the buffer, whose first byte is the surface's base:
/// `IOSurfaceGetBaseAddressOfPlane` less `IOSurfaceGetBaseAddress`, or 0 for a surface
/// without planes.
pub offset: u64,
/// The plane's `bytesPerRow`: `IOSurfaceGetBytesPerRowOfPlane`, or `IOSurfaceGetBytesPerRow`
/// for a surface without planes.
pub bytes_per_row: u64,
/// Whether the surface has planes: `IOSurfaceGetPlaneCount` above 0, a surface made with
/// plane info (`kIOSurfacePlaneInfo`, one plane dictionary or more).
pub planar: bool,
/// The value of `kIOSurfaceClientDirection` the surface was made with (an IOSurface
/// property outside the public SDK's headers), `None` for a surface made without the key. A
/// surface with it is refused, whatever the value
/// ([`AppleOnly::SurfaceClientDirection`]).
pub client_direction: Option<u64>,
/// The surface's facts a two-plane YCbCr texture over it reads (its OSType, its chroma plane,
/// its YCbCr matrix), `None` where the caller gives none: a texture of such a format is then
/// refused. A two-plane extended-range texture (`RGB10A8_2P_XR10`, `RGB10A8_2P_XR10_sRGB`)
/// reads plane 1's offset and `bytesPerRow` from them too (`chroma_offset`,
/// `chroma_bytes_per_row`). Other formats ignore it.
pub ycbcr: Option<IoSurfaceYCbCr>,
}
impl IoSurfacePlane {
/// Refused: a surface made with `kIOSurfaceClientDirection`, whatever the value
/// ([`AppleOnly::SurfaceClientDirection`]).
fn refuse_client_direction(&self) -> Result<(), Error> {
match self.client_direction {
Some(_) => Err(Error::AppleOnly(AppleOnly::SurfaceClientDirection)),
None => Ok(()),
}
}
/// A plane at `offset` of the buffer standing for the surface, rows `bytes_per_row` apart,
/// of a surface with planes or not (`planar`), made without `kIOSurfaceClientDirection`,
/// with no YCbCr facts.
pub const fn new(offset: u64, bytes_per_row: u64, planar: bool) -> IoSurfacePlane {
IoSurfacePlane {
offset,
bytes_per_row,
planar,
client_direction: None,
ycbcr: None,
}
}
/// This plane of a surface made with `kIOSurfaceClientDirection` of `direction`.
pub const fn with_client_direction(mut self, direction: u64) -> IoSurfacePlane {
self.client_direction = Some(direction);
self
}
/// This plane of a surface with `ycbcr`'s facts.
pub const fn with_ycbcr(mut self, ycbcr: IoSurfaceYCbCr) -> IoSurfacePlane {
self.ycbcr = Some(ycbcr);
self
}
}
/// What made the buffer a texture's texels are the bytes of.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum TexelsHome {
/// The program's buffer, or one the device made for a texture buffer
/// (`newTextureWithDescriptor:`).
Buffer,
/// A heap, which placed it for a texture buffer (`texture/heap.rs`): the texture answers the
/// buffer's heap, offset and aliasing as its own.
Heap,
/// The device, for a shareable texture buffer (`newSharedTextureWithDescriptor:`,
/// `texture/shared.rs`): a texture opened from its handle is the same texels.
Shared,
}
/// The bytes of one texel of `pf`, the block of its direct Vulkan format (the bytes Metal's
/// format has, for every format the runtime holds exactly); `None` without one.
pub(super) fn texel_bytes(pf: PixelFormat) -> Option<u64> {
byte_format(pf)
.and_then(|format| format.block())
.map(|block| u64::from(block.size))
}
/// The error of `refusal` in `call`.
fn refusal_error(call: &'static str, refusal: BufferRefusal) -> Error {
match refusal {
BufferRefusal::Metal(problem @ BufferTextureProblem::PastAllocation { .. }) => {
nil(call, Problem::BufferTexture(problem))
}
BufferRefusal::Metal(problem) => invalid(call, Problem::BufferTexture(problem)),
BufferRefusal::TexelsPastLength => {
Error::NotImplemented(RuntimeGap::TexelsPastBufferLength)
}
BufferRefusal::OverlappingRows => Error::NotImplemented(RuntimeGap::OverlappingSurfaceRows),
BufferRefusal::MemorylessPlane => Error::NotImplemented(RuntimeGap::MemorylessSurfacePlane),
BufferRefusal::PlaneShape => Error::NotImplemented(RuntimeGap::SurfacePlaneShape),
}
}
/// Whose a texture over a buffer is, for its resource ID (`resource_ids.rs`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Whose {
/// The program's: its descriptor's placement gives its ID.
Program,
/// The runtime's own (`Buffer::new_runtime_texture`): no ID.
Runtime,
}
/// What a texture over a buffer's making does (`Buffer::make_texture_over`): make the texture, or
/// answer where it would lie ([`Buffer::texture_placement`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Goal {
Make,
Ask,
}
/// What a texture over a buffer's making gives for its [`Goal`].
enum Over {
Made(Texture),
Asked(TexturePlacement),
}
impl Whose {
/// How a texture of a descriptor at `placement` takes its ID.
fn id_use(self, placement: IdPlacement) -> IdUse {
match self {
Whose::Program => IdUse::Program(placement),
Whose::Runtime => IdUse::Runtime,
}
}
}
impl Buffer {
/// `newTextureWithDescriptor:offset:bytesPerRow:`: a texture of `descriptor` whose texels
/// are the buffer's bytes from `offset`, rows `bytes_per_row` apart: a 2D texture or a
/// texture buffer. The texture has the buffer's resource options, whatever the descriptor
/// names, as a plain run of Metal's has (Metal's validation alone stops a descriptor's storage
/// or CPU cache mode other than the buffer's).
///
/// A 2D texture is a linear image aliasing the bytes where the device lays one out as Metal
/// does, else an image the encoders copy the texels into before the commands that use it
/// and back after the ones that write it; a texture buffer is the bytes themselves. A texture
/// buffer may lie at any offset: at one off 16 bytes, which Metal's validation alone stops,
/// functions read and write it from the offset rounded down to 16, and blits and the CPU at the
/// offset, as on plain Metal.
///
/// Refused: what Metal stops (`Invalid`, with a [`TextureProblem`]
/// or a [`BufferTextureProblem`]); a format of which plain
/// Metal makes no texture alone (`Nil`, `TextureProblem::NoTextureOfFormat` or
/// `Unspecialized`, once every rule Metal stops passed and before the buffer's own: offset,
/// row pitch, bytes past the buffer); a format with no exact Vulkan format (a 4:2:2 one among
/// them, which plain Metal makes over a buffer), and an image the device lacks (`Unsupported`);
/// a 2D texture of a format the device cannot copy (`NotImplemented`); a swizzle that writes
/// under `write_swizzle_enabled`, which Metal makes (`NotImplemented(WriteSwizzle)`). A texture
/// buffer of more than one level is stopped (`BufferTextureProblem::MipmapLevelCount`), as
/// Metal's validation stops it here.
///
/// A 2D texture or a texture buffer whose texels run past the buffer's length, which plain
/// Metal makes up to the buffer's `allocatedSize` (and answers nil past it, `Nil` with
/// `BufferTextureProblem::PastAllocation`), is made: its texels inside the length are the
/// buffer's bytes, and those past it lie in a tail the texture keeps, zero when made, which
/// its functions, blits and CPU calls read and write alike. Metal's texels there are the
/// memory after the buffer's, another allocation's bytes, which its writes there change
/// (docs/API.md's accepted differences). Such a 2D texture is a shadow image, announced
/// `shadowed-texture`; such a texture buffer's functions view its tail, announced
/// `staged-texture-buffer`.
pub fn new_texture(
&self,
descriptor: &TextureDescriptor,
offset: u64,
bytes_per_row: u64,
) -> Result<Texture, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
self.texture_over(
"new_texture",
descriptor,
offset,
bytes_per_row,
(BufferTextureCall::Texture, Whose::Program),
None,
)
}
/// [`new_texture`](Self::new_texture) of a texture of the runtime's own, which no program
/// made: it takes no resource ID (`IdUse::Runtime`, `resource_ids.rs`), whatever the
/// descriptor's placement, so it moves no ID the program's textures take, on whichever thread
/// it is made. The device's check of textures in place makes its textures here
/// (`in_place_check.rs`).
pub(crate) fn new_runtime_texture(
&self,
descriptor: &TextureDescriptor,
offset: u64,
bytes_per_row: u64,
) -> Result<Texture, Error> {
self.texture_over(
"new_texture",
descriptor,
offset,
bytes_per_row,
(BufferTextureCall::Texture, Whose::Runtime),
None,
)
}
/// `newLinearTextureWithDescriptor:offset:bytesPerRow:bytesPerImage:` (SPI): as
/// [`new_texture`](Self::new_texture), and also a 2D array, whose slices Metal lays out
/// itself: each starts `bytes_per_row` × height, rounded up to 128 bytes, after the one
/// before. The bytes-per-image argument is ignored, as plain Metal ignores it: its
/// validation alone asserts that it is a multiple of 128.
///
/// Refused as [`new_texture`](Self::new_texture) is; also an array whose slices end past the
/// buffer's `allocatedSize` (`Nil`, as Metal answers nil). An array whose texels run past the
/// buffer's length short of that is made as `new_texture` makes a 2D texture there, its
/// texels past the length in its tail.
pub fn new_linear_texture(
&self,
descriptor: &TextureDescriptor,
offset: u64,
bytes_per_row: u64,
_bytes_per_image: u64,
) -> Result<Texture, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
self.texture_over(
"new_linear_texture",
descriptor,
offset,
bytes_per_row,
(BufferTextureCall::Linear, Whose::Program),
None,
)
}
/// `newTiledTextureWithDescriptor:offset:bytesPerRow:` (SPI; the device's
/// `newTiledTextureWithBytesNoCopy:length:descriptor:offset:bytesPerRow:` makes its texture
/// over a no-copy buffer through it): as [`new_texture`](Self::new_texture), and also a 2D
/// texture of more than one level, a copy of the buffer's bytes from `offset`, rows
/// `bytes_per_row` apart, taken at its first use. A texture of one level is the one
/// `new_texture` makes, in place where that is.
///
/// A texture of more levels is one image of its own holding every level, in the `GENERAL`
/// layout like every image here, as an x86 Mac's is (an AMD Radeon Pro 5300M and an Intel UHD
/// Graphics 630 under macOS 26.7.1, which agreed on both SPI forms, `Shared` and `Managed`
/// descriptors, and `RGBA8Unorm` and `BGRA8Unorm`):
///
/// - **Taken at use.** The first command of an encoder that uses any of its levels after its
/// making, or after [`Texture::did_modify_data`](crate::Texture::did_modify_data) or a
/// `replace_region` of its first level, takes the copy: before that command, the bytes are
/// copied into its first level and every later level is made from the first as
/// `generate_mipmaps` makes them for its format: the device's blit chain or the compute
/// filter (both announced as `device-filtered-mipmaps`), or for an integer format plain
/// Metal's texel picks. The copy is taken as that command is encoded and runs on the GPU in
/// its command buffer's order, so it reads the bytes as they are when the command buffer
/// runs: a CPU write made after the making and before then is in every level (on the Macs,
/// at once or 100 ms after the making). Between takes nothing moves: a CPU write of the
/// bytes reaches no level, nor does a GPU write of them (not measured on a Mac), and what
/// the GPU writes into any level (a blit, a pass, a function's write) stays in the texture
/// and never reaches the bytes or, written into the first, the later levels.
/// - **CPU calls.** `get_bytes` and `replace_region` of the first level read and write the
/// buffer's bytes, as the Macs' do, whatever the GPU wrote into that level; a
/// `replace_region` there makes the next use take the copy again, as `did_modify_data`
/// does. Of a later level they read and write the image, whose texels the GPU reads until
/// the next take makes that level again. What differs from a Mac: there a `replace_region`
/// of a later level never reaches the GPU, and what `get_bytes` reads there is not
/// measured.
/// - **Formats.** For a format `generate_mipmaps` leaves as it is (a depth or stencil one) or
/// refuses on the device, and where the scratch of its compute filter or its picks passes
/// the device's limits, the copy moves the first level alone and the later levels keep what
/// they hold (docs/FAST-PATHS.md, known gaps).
/// - **What else differs.** It takes the buffer's resource options, as `new_texture`'s
/// texture does, so a texture over a `Shared` buffer answers `Shared`, as the Macs do. A
/// view in a format the image takes no Vulkan view of, and a binding beyond the texture's
/// usage, are refused (`Unsupported`), where a texture of one level over a buffer takes them
/// through a shadow of the bytes. While the buffer's bytes are detached (`detach_backing`)
/// it keeps its image, every level of which its commands read and write, as a Mac's
/// texture's own memory, and its CPU calls the levels past the first; theirs of the first
/// level, the bytes, are refused meanwhile (`NotImplemented(DetachedTexelAccess)`), as a
/// one-level texture's are. A take while they are detached is a copy out of them, a command
/// that names the buffer (`detach.rs`).
///
/// Announced once (`shadowed-texture`, naming the levels), counted in
/// `WorkCounts::shadowed_textures`, each take's copy in `WorkCounts::shadow_texels_copied_in`
/// (its first level's texels alone) and a take by the compute filter in
/// `WorkCounts::mipmaps_filtered`; nothing is ever counted in
/// `WorkCounts::shadow_texels_copied_back`. A take whose compute filter or picks find no room
/// for their scratch in the device's memory fails its command buffer.
///
/// Refused as [`new_texture`](Self::new_texture) is, but for the levels: more levels than the
/// extent's full chain (`Invalid`, `TextureProblem`), and a texture buffer of more than one
/// (`BufferTextureProblem::MipmapLevelCount`). A texture buffer's offset is held to 16 bytes,
/// as [`new_linear_texture`](Self::new_linear_texture)'s is: the SPI's is not measured.
pub fn new_tiled_texture(
&self,
descriptor: &TextureDescriptor,
offset: u64,
bytes_per_row: u64,
) -> Result<Texture, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
self.texture_over(
"new_tiled_texture",
descriptor,
offset,
bytes_per_row,
(BufferTextureCall::Tiled, Whose::Program),
None,
)
}
/// The stand-in for `newTextureWithDescriptor:iosurface:plane:` over the caller's memory: a
/// texture of `descriptor` whose texels are `plane`'s, the buffer standing for the IOSurface
/// (a no-copy buffer over the surface's pages, `IOSurfaceGetBaseAddress` for
/// `IOSurfaceGetAllocSize` bytes, or a buffer holding them): a 2D texture or a texture buffer,
/// and over a plane of a surface with planes also a 1D texture whose row fits and a 2D array
/// of one slice, laid out as the 2D texture of their rows (a texture buffer's texels run on
/// from such a plane's offset however wide). Metal's IOSurface rules apply, measured on the M3
/// Max and an M4, not [`new_texture`](Self::new_texture)'s buffer rules:
///
/// - the texture takes and reports the descriptor's resource options, whatever the buffer's
/// (its default hazard tracking mode resolves to tracked), as an M4's plain run and its
/// validation do for `Shared` and `Managed`. Plain Metal also makes a `Private` one, whose
/// GPU and CPU read and write the surface's bytes as a `Shared` one's do, and so does the
/// runtime, over a buffer the CPU reaches; Metal's validation stops it;
/// - a plane of a planar surface takes any offset and any `bytes_per_row` that covers its
/// row; a surface without planes, a `bytes_per_row` that is a multiple of 16, at an offset
/// that is one too (Metal asserts on another pitch). The row the pitch covers is the
/// **texture's** own, whatever the plane holds and whatever the rotation: a rotated
/// texture's plane holds the turned frame, and Metal still holds the pitch to the texture's
/// unturned row (measured on the M3 Max, plain Metal with no validation layer: a 4x8 plane
/// at its tight stride of 16 behind an 8x4 texture ends the process, "bytesPerRow (16) must
/// be greater or equal to (32) bytes");
/// - `get_bytes` and `replace_region` read and write the plane where the surface lays it out:
/// from its offset, rows `bytes_per_row` apart;
/// - the GPU's commands read and write it as Metal's GPU does: from the offset rounded down to
/// 16, rows `bytes_per_row` rounded down to 16 apart, which are other bytes than the CPU's
/// where either is not a multiple of 16;
/// - under 16 bytes a row, the GPU's rows are 16 bytes apart. There Metal's GPU reads every
/// row past the first as memory-dependent bytes; the runtime's reads them where its writes
/// put them, a near-equivalent announced once
/// ([`Degradation::NarrowSurfaceRows`]);
/// - where that pitch is below the row, the GPU's rows share bytes: 1 to 15 of them, a whole
/// number of texels, between consecutive rows. A plain plane is made, its shadow's copies
/// reproducing the sharing with two regions a slice each way, so that a read sees it as
/// Metal's does and the shared bytes keep the earlier row's where two of its rows write
/// them. Which row Metal leaves there is the order its own command wrote the rows in, which
/// follows the command, the GPU pitch and the row count (`linear_plan.rs`'s **Rows that
/// overlap**); a kernel, the only writer such a plane takes here, moves its winner with the
/// threadgroup shape, so the earlier row's bytes are one of its answers. Announced once by
/// [`Degradation::CollidingSurfaceRowWrites`];
/// - a colour 2D texture whose rows are wider than the `bytes_per_row` of a plane of a
/// surface with planes at an offset and pitch that are multiples of 16, and a colour 1D
/// texture wider than one row there, are made as the M3 Max lays them out on every path:
/// row `y` from the offset plus `y` x `bytes_per_row`, the whole row, for the GPU and the
/// CPU alike, so the 2D texture's rows share bytes, any whole number of texels, a byte
/// shared by as many rows as reach it. They are held as the rows above that share bytes
/// are, announced once by the same notice.
///
/// Refused: a surface made with `kIOSurfaceClientDirection`, SPI
/// ([`AppleOnly::SurfaceClientDirection`]); what
/// Metal stops (`Invalid`, with a [`TextureProblem`] or a
/// [`BufferTextureProblem`]: over a surface without planes a
/// type other than 2D or a texture buffer, more than one level, a pitch below the row, another
/// pitch or offset, or rows that end past it, which plain Metal asserts on); what Metal makes
/// over a plane of a surface with planes and lays out no one way across the GPUs measured
/// (`NotImplemented(SurfacePlaneShape)`, which carries the measurements: rows wider than the
/// plane's of a format but a colour one or off 16 bytes, more than one level, a 3D or
/// multisample texture of a format but a colour one, a 2D array of slices of such a format
/// or off 16 bytes, another type, a BC texture of 16-byte blocks at an offset or pitch off
/// 16 bytes and one of 8-byte blocks off them or under 16 bytes a row; a cube or cube array
/// there plain Metal asserts on, `Invalid`); `Memoryless` storage, which plain
/// Metal makes and serves on the CPU while its GPU uses fail the command buffer
/// (`NotImplemented(MemorylessSurfacePlane)`, which carries the measurement); rows that
/// overlap on Metal's GPU where no shadow of the runtime's holds them
/// (`NotImplemented(OverlappingSurfaceRows)`: a plane a render pass stores into, a two-plane
/// YCbCr, two-plane extended-range, 4:2:2, depth or stencil one, and a blit that addresses
/// such a plane's rows as bytes; `linear_plan.rs`); a plane's own rows or its GPU texels past
/// the buffer, of a shape that keeps no tail (`NotImplemented(TexelsPastBufferLength)`: a
/// two-plane YCbCr, two-plane extended-range, 4:2:2, depth, stencil, BC or rotated one); and
/// as [`new_texture`](Self::new_texture) refuses a format or an image.
///
/// A colour texture of a plane of a surface with planes whose texels run past the buffer
/// standing for the surface is made: Metal makes it and lays those texels out in no one place
/// (its blits read zeros there, its kernels zeros and the plane's rows again 32 KiB on, its
/// `getBytes` other memory, and its writes keep to the surface; an Apple M3 Max under macOS
/// 27.0.1). The runtime keeps them in a tail of the texture's own, zero when made, which every
/// path reads and writes alike, and its texels inside the surface are the buffer's bytes
/// (the module's **Tails**). Announced once by [`Degradation::SurfaceTexelsTail`].
///
/// A colour 2D texture of more than one level, a 3D texture and a 2D multisample one over a
/// plane of a surface with planes are made in memory of their own, zero when made in every
/// storage mode, as [`Device::new_texture`](crate::Device::new_texture) makes one otherwise:
/// Metal makes each and lays out its texels in no one place there, its first level's and
/// slice's among them (an Apple M3 Max under macOS 27.0.1, whose GPU reads them from other
/// bytes than its writes and its CPU put them; the x86 Macs keep the first level and slice
/// over the plane). Announced once by [`Degradation::SurfaceTexelsApart`].
///
/// A colour 2D array of more than one slice over a plane of a surface with planes whose
/// offset and `bytesPerRow` are multiples of 16 is made with its first slice over the plane,
/// the buffer's bytes as a 2D texture's there, and its slices past the first in a tail of the
/// texture's own, zero when made, which every path reads and writes alike: Metal keeps the
/// first slice over the plane on every path and lays the others out in no one place (the M3
/// Max's blits and `getBytes` read the second at the plane's offset, its kernels one image
/// on, and its writes land at the plane's offset). Announced once by
/// [`Degradation::SurfaceTexelsTail`].
///
/// A depth or stencil texture (`Depth16Unorm`, `Depth32Float`, `Depth32Float_Stencil8`,
/// `Stencil8`, `X32_Stencil8`) over a plane of a surface with planes is made as Metal makes
/// it, one aspect's unit a texel in the plane (its depth, else its stencil; the stencil of
/// `Depth32Float_Stencil8` is not in the plane and reads 0): its reads, samples, blits and CPU
/// access take the plane's bytes, and its render passes store it there in Metal's tiled layout,
/// its elements' bytes alone (`texture/surface_depth.rs`), a pass loading the stencil of
/// `Depth32Float_Stencil8` loads 0, and its views render as it does. A blit of that stencil is
/// the runtime's gap (`NotImplemented(SurfaceDepthStencil)`), with what
/// `texture/surface_depth.rs` lists. Over a surface without planes Metal stops it (`Invalid`,
/// `BufferTextureProblem::NotLinear`).
///
/// A BC1 to BC7 texture over a surface's rows (2D, over a plane of a surface with planes at an
/// offset and pitch that are multiples of 16, or for BC1 and BC4, whose blocks are 8 bytes,
/// at an offset and pitch the block divides, at least 16 a row) is made as the x86 Macs make
/// it, where the M3 Max answers nil: row `j` of its blocks at the plane's offset + `j` x
/// `bytes_per_row`, unrounded, which its samples, blits and CPU calls take (`linear_plan.rs`'s
/// **Block-compressed surfaces**; at an offset off 16 the UHD 630's CPU calls take the plane
/// from byte 0).
/// Over a surface without planes Metal stops a row of blocks wider than the surface's
/// (`RowPitch`) and rows of texels past it (`PastBuffer`). Every other compressed format is
/// stopped as over a buffer (`NotLinear`).
///
/// A plane of a 4:2:2 format (`GBGR422`, `BGRG422`: a CoreVideo `2vuy` or `yuvs` surface's)
/// is made as Metal makes it, 2 bytes a texel, copied as a 4:2:2 texture of the device's is
/// (`linear_plan.rs`).
///
/// A two-plane YCbCr texture of 8 or 10 bits, converted (`YCBCR8_420_2P`,
/// `YCBCR10_444_2P`, ...), through the sRGB curve (their `_sRGB` twins) or read as codes
/// (`RGB8_420_2P`, `RGB10_420_2P`), and the packed 10-bit ones (`YCBCR10_420_2P_PACKED`, ...)
/// and their `_sRGB` twins, over any plane of a surface of any OSType but the lossless
/// compressed ones, or over plane 0 of a surface without planes, given the surface's facts
/// ([`IoSurfacePlane::ycbcr`]), samples the texels Metal reads of the planes, and its copies
/// and CPU calls move the luma codes, as Metal's do (`ycbcr.rs` states its rules and
/// refusals: the luma plane the call's, the chroma plane the facts' plane 1 under plane 0 of a
/// surface with planes, chroma codes 0 under any other, bytes past the buffer zeros). A packed
/// texture's CPU calls move nothing, as Metal's; its blits with a buffer are refused
/// (`NotImplemented(Format)`). A render target of a 4:2:0 or 4:2:2 format of an odd width or
/// height is Metal's nil (`TextureProblem::ExtentForFormat`). Metal makes every two-plane
/// YCbCr format over a surface's plane; another is the runtime's gap
/// (`NotImplemented(Format)`).
///
/// An `RGB10A8_2P_XR10` or `RGB10A8_2P_XR10_sRGB` texture over plane 0 of a two-plane
/// `'b3a8'` surface, given plane 1's offset and `bytesPerRow` in the surface's facts
/// ([`IoSurfacePlane::ycbcr`]: `chroma_offset`, `chroma_bytes_per_row`), keeps each texel's
/// color word in plane 0 and its alpha byte in plane 1, where Metal's GPU reads and writes
/// them; its CPU calls and its blits with a buffer move plane 0's color words, as Metal's do,
/// and its fills and its blits with another such texture move the surface's bytes as Metal's
/// do. Without the facts, over another surface or plane, and rotated, it is refused
/// (`NotImplemented(Format)`, naming why).
///
/// Another format of which plain Metal makes no texture alone
/// (`RGBA16Float_XR`, `AnyDepth`, `AnyStencil`, `Unspecialized`) answers `Nil`, as Metal
/// answers nil over a surface's plane, once every rule Metal stops passed
/// and before the surface's own.
pub fn new_texture_with_iosurface_plane(
&self,
descriptor: &TextureDescriptor,
plane: IoSurfacePlane,
) -> Result<Texture, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
plane.refuse_client_direction()?;
let texture = self.texture_over(
"new_texture_with_iosurface_plane",
descriptor,
plane.offset,
plane.bytes_per_row,
(
BufferTextureCall::IoSurfacePlane {
planar: plane.planar,
},
Whose::Program,
),
plane.ycbcr,
)?;
// Metal's device takes a sampler ID as it makes a surface's texture, as for a texture of
// its own (unlike a buffer's: `resource_ids.rs`).
self.generation().take_device_sampler();
Ok(texture)
}
/// Where a texture [`new_texture`](Self::new_texture) makes of the same arguments lies if
/// made now: in place over the buffer's host pages, a shadow pending the device's check, a
/// shadow and why, or a texture the device's check does not decide ([`TexturePlacement`]). A
/// runtime extension: Metal has no such query, as a Mac's GPU works on a texture over the
/// caller's pages in place.
///
/// The answer is the making's own decision, the same functions' (the device's policy, the
/// texture's usage and texel size, the buffer's bytes detached, copied or past their length,
/// a discrete GPU's device copy, the device's check's verdicts for the pages' kind, the
/// texture's accesses and how its texels lie in the device's lines of memory, and the image
/// in place's making up to its bind: the driver's linear image at the texture's pitch, else
/// the `DRM_FORMAT_MOD_LINEAR` one, its requirements against the bytes' place and memory
/// type, rows that share bytes), taken after every check the making runs before it makes
/// anything: an argument `new_texture` refuses is refused here alike, with the same error.
/// Where the image in place is the answer, the query makes it unbound and releases it: one or
/// two `VkImage`s where the texture's declared plan is made in place, a pair more for each
/// wider plan the making would try first (the feedback loop usage, and the storage usage)
/// where it is refused; nothing is bound or submitted. What only the bind shows is not known
/// here: over pages imported as dma-bufs, the caller's source's dma-buf and the driver's
/// import of it ([`TexturePlacement::InPlace`]); and a making that runs out of memory or meets
/// a lost device fails, as this query may in making those images. The answer is the buffer's
/// and the device's state as the call reads them: a detach, a rebind, or the check's verdicts
/// after it may change what a texture made later takes. A pending verdict starts the device's
/// check, as the making's does
/// ([`Device::settle_in_place_checks`](crate::Device::settle_in_place_checks) says when its
/// verdicts are known). It counts and announces nothing.
pub fn texture_placement(
&self,
descriptor: &TextureDescriptor,
offset: u64,
bytes_per_row: u64,
) -> Result<TexturePlacement, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
let over = (offset, bytes_per_row);
let asked = (BufferTextureCall::Texture, Whose::Program, Goal::Ask);
self.placement_over("new_texture", descriptor, over, asked, None)
}
/// Where a texture [`new_tiled_texture`](Self::new_tiled_texture) makes of the same arguments
/// lies if made now, as [`texture_placement`](Self::texture_placement) answers it for
/// [`new_texture`](Self::new_texture): for a texture of more than one level,
/// `Shadow(TextureShadowReason::Mipmapped)` over any buffer, its own copy of the bytes taken at
/// use. Refused as that call refuses its arguments. A runtime extension: Metal has no such
/// query.
pub fn tiled_texture_placement(
&self,
descriptor: &TextureDescriptor,
offset: u64,
bytes_per_row: u64,
) -> Result<TexturePlacement, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
let over = (offset, bytes_per_row);
let asked = (BufferTextureCall::Tiled, Whose::Program, Goal::Ask);
self.placement_over("new_tiled_texture", descriptor, over, asked, None)
}
/// Where a texture [`new_texture_with_iosurface_plane`](Self::new_texture_with_iosurface_plane)
/// makes of the same arguments lies if made now, as
/// [`texture_placement`](Self::texture_placement) answers it for
/// [`new_texture`](Self::new_texture), and refused as that call refuses its arguments. A
/// runtime extension: Metal has no such query.
pub fn texture_placement_with_iosurface_plane(
&self,
descriptor: &TextureDescriptor,
plane: IoSurfacePlane,
) -> Result<TexturePlacement, Error> {
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
let _held = self.held_parts();
plane.refuse_client_direction()?;
let over = (plane.offset, plane.bytes_per_row);
let form = BufferTextureCall::IoSurfacePlane {
planar: plane.planar,
};
let asked = (form, Whose::Program, Goal::Ask);
let call = "new_texture_with_iosurface_plane";
self.placement_over(call, descriptor, over, asked, plane.ycbcr)
}
/// The placement [`make_texture_over`](Self::make_texture_over) answers for `asked`, for
/// Metal's `call`.
fn placement_over(
&self,
call: &'static str,
descriptor: &TextureDescriptor,
over: (u64, u64),
asked: (BufferTextureCall, Whose, Goal),
ycbcr_surface: Option<IoSurfaceYCbCr>,
) -> Result<TexturePlacement, Error> {
match self.make_texture_over(call, descriptor, over, asked, ycbcr_surface)? {
Over::Asked(placement) => Ok(placement),
Over::Made(_) => Err(internal("a texture's placement made the texture")),
}
}
/// The texture of `descriptor` over the buffer that `call` (made through `form`) makes, over
/// a surface of `surface`'s YCbCr facts where given, `whose` it is. Over a heap's buffer it is
/// among the heap's textures while it lives: `useHeap:` makes every resource the heap holds
/// resident, so an encoder that used the heap makes it resident (`residency.rs`), and a
/// shadow's texels are copied in for its commands.
fn texture_over(
&self,
call: &'static str,
descriptor: &TextureDescriptor,
offset: u64,
bytes_per_row: u64,
(form, whose): (BufferTextureCall, Whose),
ycbcr_surface: Option<IoSurfaceYCbCr>,
) -> Result<Texture, Error> {
let over = (offset, bytes_per_row);
let made = (form, whose, Goal::Make);
let texture = match self.make_texture_over(call, descriptor, over, made, ycbcr_surface)? {
Over::Made(texture) => texture,
Over::Asked(_) => return Err(internal("a texture's making answered a placement")),
};
if let Some(heap) = self.heap() {
heap.hold_texture(&texture);
}
if whose == Whose::Runtime {
return Ok(texture);
}
let remake = Remake::Over {
buffer: self.clone(),
call,
descriptor: *descriptor,
offset,
bytes_per_row,
form,
ycbcr: ycbcr_surface,
};
let device = texture.generation().clone();
Ok(device.kept_texture(texture, remake))
}
/// The texture over the buffer that `call` made through `form`, of `descriptor` at `over`
/// (its offset and bytes per row), over a surface of `ycbcr_surface`'s facts where given, made
/// again as the runtime's own, which takes no ID: a program's texture over the buffer, on
/// the generation a recovery carried the buffer to (`texture/carry.rs`).
pub(super) fn texture_over_again(
&self,
call: &'static str,
descriptor: &TextureDescriptor,
(offset, bytes_per_row): (u64, u64),
form: BufferTextureCall,
ycbcr_surface: Option<IoSurfaceYCbCr>,
) -> Result<Texture, Error> {
let made = (form, Whose::Runtime);
self.texture_over(call, descriptor, offset, bytes_per_row, made, ycbcr_surface)
}
/// [`texture_over`](Self::texture_over)'s texture, at `offset` with `bytes_per_row` (`over`),
/// made where `goal` makes it; else where it would lie, answered after every check its making
/// runs before it makes anything ([`texture_placement`](Self::texture_placement)).
fn make_texture_over(
&self,
call: &'static str,
descriptor: &TextureDescriptor,
(offset, bytes_per_row): (u64, u64),
(form, whose, goal): (BufferTextureCall, Whose, Goal),
ycbcr_surface: Option<IoSurfaceYCbCr>,
) -> Result<Over, Error> {
// A detach waits for none being made over the bytes (`detach.rs`), nor does a rebind of
// them while they are detached, so whether they are holds while it is made. A question
// holds nothing: its answer is the bytes' state as it reads them.
let _making = match goal {
Goal::Make => self.making_texture(),
Goal::Ask => None,
};
let detached = self.detached();
// A YCbCr format is made over a surface's planes with their facts (`ycbcr.rs`).
let asked_format = descriptor.pixel_format;
let surface = matches!(form, BufferTextureCall::IoSurfacePlane { .. });
// A two-plane extended-range format reads the surface's plane 1 from the same facts.
let surface_facts = ycbcr_surface;
let ycbcr_surface = ycbcr_surface.filter(|_| ycbcr(asked_format) && surface);
let device = self.generation();
let asked = device.texture_options(call, descriptor)?;
device.check_texture_placement(call, asked.placement, asked.usage, asked.texture_type)?;
let options = asked.resource_options;
let storage = options.storage_mode()?;
let cache = options.cpu_cache_mode()?;
let hazard = options.hazard_tracking_mode()?;
// A plain `newTextureWithDescriptor:offset:bytesPerRow:` takes the buffer's options
// whatever the descriptor names (probe 9d); an IOSurface plane takes its descriptor's, its
// default hazard tracking mode tracked, as the M4's plain run and validation do
// (the plan refuses a memoryless one).
let options = match form {
BufferTextureCall::IoSurfacePlane { .. } => {
let hazard = match hazard {
HazardTrackingMode::Default => HazardTrackingMode::Tracked,
hazard => hazard,
};
ResourceOptions::new(storage, cache, hazard)
}
BufferTextureCall::Texture | BufferTextureCall::Linear | BufferTextureCall::Tiled => {
self.resource_options()
}
};
let descriptor = &TextureDescriptor {
resource_options: options,
..asked
};
let id_use = whose.id_use(descriptor.placement);
// A texture buffer of more than one level is stopped by `linear_plan`, as Metal's
// buffer creator stops it, not refused as the device's gap.
let rules = self.generation().properties().texture_rules();
let checked = match ycbcr_surface {
Some(_) => descriptor.check_over_surface(rules),
None => descriptor.check_made(Creator::Buffer, rules),
};
// A format of which plain Metal makes no texture alone answers nil over a buffer (the
// linear SPI's too) and, but for a two-plane YCbCr one, over a surface's plane, once every
// rule Metal stops passed and before the buffer's or the surface's own. Metal makes every
// two-plane YCbCr format over a surface's plane (`YCBCR8_420_2P` and `YCBCR10_420_2P` over
// a 'BGRA' surface's); the runtime makes those `ycbcr.rs` reads with the surface's facts,
// and the others are its gap.
let layout = match checked {
Err(Refused::Nil(TextureProblem::NoTextureOfFormat))
if surface && ycbcr_format(asked_format).is_some() =>
{
return Err(Error::Unsupported(
Unsupported::not_implemented(Feature::Format).naming_format(asked_format),
));
}
checked => checked.map_err(|refused| refused.error(call))?,
};
// Only an IOSurface plane takes a framebuffer-only texture: plain Metal asserts
// "framebufferOnly == NO" over a buffer, or on its rotation.
if descriptor.framebuffer_only && !surface {
return Err(invalid(
call,
Problem::Texture(TextureProblem::FramebufferOnly),
));
}
let buffer = BufferFacts {
length: self.length(),
};
let planar = matches!(form, BufferTextureCall::IoSurfacePlane { planar: true });
let converted = match &ycbcr_surface {
Some(facts) => Some(surface_planes(
&layout,
([offset, bytes_per_row], planar),
facts,
self.length(),
device,
)?),
None => None,
};
// Metal makes no render target of a 4:2:0 or 4:2:2 format whose chroma blocks do not
// tile it (an odd width, or an odd height; plain Metal answers nil, the M3 Max under
// macOS 27.0.1), and makes any other.
if let Some((planes, _)) = &converted {
let halved = planes.codes.subsampling.halved() != [false, false];
let odd = planes.size.iter().any(|n| n % 2 != 0);
if halved && odd && descriptor.usage.contains(TextureUsage::RENDER_TARGET) {
return Err(Refused::Nil(TextureProblem::ExtentForFormat).error(call));
}
}
// A two-plane extended-range texture over a surface's plane 0 keeps its color words in
// the plane and its alpha bytes in the surface's plane 1 (`texture/surface_two_planes.rs`).
let alpha_plane = match form {
BufferTextureCall::IoSurfacePlane { planar }
if two_planes::holds_planes(layout.pixel_format) =>
{
Some(surface_two_planes::alpha_plane(
layout.pixel_format,
planar,
surface_facts,
)?)
}
_ => None,
};
// A format no buffer backs is Metal's refusal, whatever its Vulkan format, but for a
// 4:2:2 one, which a plain run makes over a buffer (its validation asserts; probe 9
// `fmt.240`, `fmt.241`) and the runtime holds over none: its textures' carrier
// (`chroma_pairs.rs`) is an image of their own. An IOSurface plane's (a CoreVideo `2vuy` or
// `yuvs` surface's) is made, as Metal makes it, 2 bytes a texel in the carrier
// (`linear_plan.rs`).
let pf = layout.pixel_format;
let four_two_two = chroma_pairs::is_four_two_two(pf);
// A depth or stencil texture over a surface's plane (`linear_plan.rs` stops every other
// buffer's) keeps one aspect's unit a texel there.
let depth_unit = surface_depth_unit(pf).filter(|_| surface);
let texel_bytes = match (texel_bytes(pf), &converted) {
_ if depth_unit.is_some() => depth_unit.map_or(0, |unit| u64::from(unit.bytes)),
(Some(bytes), _) => bytes,
// Its texels are the luma plane's codes, a code's bytes a texel; packed codes' units
// of three (`ycbcr.rs`).
(None, Some((planes, _))) => u64::from(planes.codes.bytes),
// Its texels are plane 0's color words (`two_planes.rs`).
(None, None) if alpha_plane.is_some() => u64::from(two_planes::UNIT.bytes),
(None, None) if four_two_two && surface => u64::from(chroma_pairs::TEXEL_BYTES),
(None, None) if !is_linear(pf) && !four_two_two => 0,
(None, None) => {
return Err(Error::Unsupported(
Unsupported::not_implemented(Feature::Format).naming_format(pf),
))
}
};
// A packed format's luma rows are units of three codes, `⌈w / 3⌉` of them a row: the
// plane is planned as those units.
let planned = match &converted {
Some((planes, _)) if planes.codes.packed => {
let [w, h, d] = layout.extent;
let across = planes.codes.units(u64::from(w)) as u32;
TextureLayout {
extent: [across, h, d],
..layout
}
}
_ => layout,
};
let texture = BufferTexture {
layout: planned,
texel_bytes,
storage,
offset,
bytes_per_row,
call: form,
usage: descriptor.usage,
};
// A rotated texture: its plane holds it turned, as Metal checks it, its CPU access and
// blits address the plane as the texture's own extent at the plane's rows, and its
// functions the stored frame (`texture/rotation.rs`).
// A rotated two-plane YCbCr texture is not measured: the runtime's gap. Nor is a rotated
// two-plane extended-range one over a surface.
if (converted.is_some() || alpha_plane.is_some()) && descriptor.rotation != 0 {
return Err(Error::Unsupported(
Unsupported::not_implemented(Feature::Format).naming_format(pf),
));
}
// Over detached bytes the runtime makes no rotated texture, nor one of a surface's two
// planes (`stand_in.rs`): a two-plane YCbCr texture's converted texels have no stand-in
// of zero bytes.
let apart = converted.is_some() || alpha_plane.is_some() || descriptor.rotation != 0;
if detached && apart {
return Err(Error::NotImplemented(RuntimeGap::DetachTextures));
}
let rotation = match u8::try_from(descriptor.rotation) {
Ok(0) => None,
Ok(rotation) => Some(turned_plane(call, &texture, rotation, &buffer)?),
Err(_) => return Err(internal("a rotation past the descriptor's check")),
};
if let Some((rotation, stored)) = rotation {
if self.generation().is_lost() {
return Err(self.generation().lost_refusal());
}
if goal == Goal::Ask {
return Ok(Over::Asked(TexturePlacement::OtherShape));
}
let rows = Texels::new(
self.clone(),
rotated_rows(&texture),
texel_bytes,
descriptor,
);
let stored = Texels::placed(self.clone(), stored, texel_bytes, descriptor);
return self
.generation()
.new_rotated_texture(
call,
[rows, stored].map(|texels| texels.with_options(options).over_surface(surface)),
descriptor,
(&layout, rotation),
id_use,
)
.map(Over::Made);
}
// A texture over a plane whose texels Metal lays out in no one place, its first level's
// and slice's among them, lies in memory of its own (`linear_plan.rs`'s **Beyond the
// plane's rows**).
if apart_from_plane(&texture) {
if storage == StorageMode::Memoryless {
return Err(Error::NotImplemented(RuntimeGap::MemorylessSurfacePlane));
}
if goal == Goal::Ask {
return Ok(Over::Asked(TexturePlacement::OtherShape));
}
let made = self
.generation()
.surface_texture_apart(call, descriptor, id_use)?;
self.degrade_apart(&layout, offset, bytes_per_row);
return Ok(Over::Made(made));
}
let placed = plan_buffer_texture(&texture, &buffer)
.map_err(|refusal| refusal_error(call, refusal))?;
if self.generation().is_lost() {
return Err(self.generation().lost_refusal());
}
// A texture of a surface's two planes, a texture buffer and a converted texture take no
// image in place.
let other = alpha_plane.is_some()
|| converted.is_some()
|| layout.texture_type == TextureType::TextureBuffer;
if goal == Goal::Ask && other {
return Ok(Over::Asked(TexturePlacement::OtherShape));
}
if let Some(alpha) = alpha_plane {
let texels = Texels::placed(self.clone(), placed, texel_bytes, descriptor)
.with_options(options)
.over_surface(surface);
return self
.generation()
.new_surface_planes_texture(call, texels, descriptor, &layout, id_use, alpha)
.map(Over::Made);
}
if let Some((planes, span)) = converted {
let texels = Texels::placed(self.clone(), placed, texel_bytes, descriptor)
.with_options(options)
.over_surface(surface);
return self
.generation()
.new_converted_texture(call, texels, descriptor, &layout, id_use, (&planes, span))
.map(Over::Made);
}
if layout.texture_type == TextureType::TextureBuffer {
// Over a surface's plane, of the descriptor's options (`Buffer::new_texture_with_iosurface_plane`):
// its CPU calls at the plane's offset and its functions from the offset rounded down
// to 16 (`into_texture_buffer`), as Metal reads a plane's.
let texels = Texels::new(self.clone(), placed.cpu, texel_bytes, descriptor)
.with_options(options)
.over_surface(surface)
.into_texture_buffer()?;
let tail = surface && texels.tail.is_some();
let texture = texels.texture(descriptor, &layout, id_use);
if tail {
self.degrade_surface_tail(&layout, &placed);
}
Ok(Over::Made(texture))
} else {
let texels = Texels::placed(self.clone(), placed, texel_bytes, descriptor)
.with_options(options)
.over_surface(surface);
// Where it would lie: the route its making takes (`texture/route.rs`), with no tail
// made for texels past the buffer's length, which the route asks alone.
if goal == Goal::Ask {
let past = texels.runs_past();
let asked = (descriptor, &layout);
let placement = self
.generation()
.placement_over_buffer(call, &texels, asked, past)?;
return Ok(Over::Asked(placement));
}
// Texels past the buffer's length lie in a tail of their own (`linear_plan.rs`).
let texels = texels.with_tail()?;
let tail = surface && texels.tail.is_some();
let texture = self
.generation()
.new_texture_over_buffer(call, texels, descriptor, &layout, id_use)?;
if tail {
self.degrade_surface_tail(&layout, &placed);
}
if placed.narrow {
self.degrade_narrow_rows(&layout, &placed);
}
if placed.gpu.shared_bytes() != 0 {
self.degrade_colliding_rows(&layout, &placed);
}
Ok(Over::Made(texture))
}
}
}
/// The plane of a texture of `rotation` made as `texture` (of a rotated descriptor, over an
/// IOSurface plane): `rotation` and where the turned texels lie, checked as Metal checks them, as
/// a 2D texture of the extent it stores (`rotated::stored_extent`) at the plane's offset and
/// rows, with the pitch held to the texture's own row ([`covers_its_row`]), which the stored
/// extent's plan does not hold it to. Refused as that texture is, and
/// (`NotImplemented(RotatedTexture)`) for a plane whose offset or `bytesPerRow` is off 16 bytes,
/// or a format of a texel size other than 1, 2, 4, 8 or 16 bytes (a 4:2:2 one among them), whose
/// rotation is not measured; and (`NotImplemented(TexelsPastBufferLength)`) for a stored frame
/// past the buffer standing for the surface, whose turned texels are not measured there.
///
/// A rotated plane's `bytesPerRow` must be a multiple of 16 here, so its GPU rows lie at the
/// pitch itself and never share bytes, which no shadow of a turned frame holds
/// (`linear_plan.rs`'s **Rows that overlap**).
///
/// It reads nothing of the buffer but its length, so a test reaches every refusal from the
/// input's own shape with no device — which the pitch rule needs, the texture it refuses being
/// one that ends Metal's process.
pub(super) fn turned_plane(
call: &'static str,
texture: &BufferTexture,
rotation: u8,
buffer: &BufferFacts,
) -> Result<(u8, Placed), Error> {
let measured = texture.offset.is_multiple_of(LINEAR_ALIGNMENT)
&& texture.bytes_per_row.is_multiple_of(LINEAR_ALIGNMENT)
&& [1, 2, 4, 8, 16].contains(&texture.texel_bytes)
&& !chroma_pairs::is_four_two_two(texture.layout.pixel_format);
if !measured {
return Err(Error::NotImplemented(RuntimeGap::RotatedTexture));
}
covers_its_row(texture).map_err(|problem| refusal_error(call, problem.into()))?;
let [width, height, depth] = texture.layout.extent;
let [width, height] = rotated::stored_extent(rotation, [width, height]);
let stored = BufferTexture {
layout: TextureLayout {
extent: [width, height, depth],
..texture.layout
},
..*texture
};
let placed =
plan_buffer_texture(&stored, buffer).map_err(|refusal| refusal_error(call, refusal))?;
// A stored frame past the buffer standing for the surface has no tail: its turned texels
// are not measured there.
if placed.gpu.end().max(placed.cpu.end()) > buffer.length {
return Err(Error::NotImplemented(RuntimeGap::TexelsPastBufferLength));
}
Ok((rotation, placed))
}
/// Whether `texture`'s `bytesPerRow` covers the **texture's** own packed row, which is Metal's
/// rule for a texture over an IOSurface plane whatever the plane holds and whatever the rotation.
///
/// Measured on the M3 Max under plain Metal with no validation layer: a 4x8 surface at its own
/// tight stride of 16 behind an 8x4 texture ends the process,
/// `_mtlValidateStrideTextureParameters:1938: failed assertion 'Texture Descriptor Validation
/// IOSurface texture: bytesPerRow (16) must be greater or equal to (32) bytes'` — 32 being the
/// texture's row, 8 texels of 4 bytes, not the plane's 16. A rotated texture's plane holds the
/// turned frame, so a quarter turn's plane row is the shorter of the two and
/// [`plan_buffer_texture`]'s own row check, which runs against the stored extent
/// (`Buffer::turned_plane`), passes a pitch Metal asserts on. This rule is the texture's extent
/// alone, so a test reaches it from the input's own shape with no device — which is the only way
/// it can be tested: building the texture Metal asserts on would end the test process.
fn covers_its_row(texture: &BufferTexture) -> Result<(), BufferTextureProblem> {
let row_bytes = u64::from(texture.layout.extent[0]).saturating_mul(texture.texel_bytes);
match texture.bytes_per_row >= row_bytes {
true => Ok(()),
false => Err(BufferTextureProblem::RowPitch {
bytes_per_row: texture.bytes_per_row,
row_bytes,
}),
}
}
/// The bytes a rotated texture made as `texture` has its CPU access and blits address: its own
/// extent at the plane's offset and rows, as plain Metal addresses them, which may run past the
/// stored frame's last row. One layout for the GPU and the CPU alike, and its rows never share a
/// byte: [`covers_its_row`] holds the pitch to the texture's own row
/// (`tests::buffer_texture::no_rotated_plane_s_gpu_rows_share_bytes`).
pub(super) fn rotated_rows(texture: &BufferTexture) -> BufferLayout {
let [width, height, _] = texture.layout.extent.map(u64::from);
BufferLayout {
offset: texture.offset,
row_pitch: texture.bytes_per_row,
slice_pitch: texture.bytes_per_row.saturating_mul(height),
row_bytes: width.saturating_mul(texture.texel_bytes),
rows: height,
slices: 1,
}
}
impl Buffer {
/// Announce `NarrowSurfaceRows` for an IOSurface plane of `layout` placed as `placed`, once
/// it is made (`linear_plan.rs`).
fn degrade_narrow_rows(&self, layout: &TextureLayout, placed: &Placed) {
self.generation()
.degrade(Degradation::NarrowSurfaceRows, || {
let [width, height, _] = layout.extent;
let object = format!(
"{width}x{height} {:?} texture of an IOSurface plane at offset {}, {} bytes a row",
layout.pixel_format, placed.cpu.offset, placed.cpu.row_pitch
);
let reason = format!(
"Metal's GPU writes its rows {} bytes apart from byte {} and reads every row past \
the first as memory-dependent bytes",
placed.gpu.row_pitch, placed.gpu.offset
);
(object, reason)
});
}
/// Announce `SurfaceTexelsApart` for a texture of `layout` over the IOSurface plane at `offset`,
/// `bytes_per_row` a row, made in memory of its own (`linear_plan.rs`'s **Beyond the plane's
/// rows**).
fn degrade_apart(&self, layout: &TextureLayout, offset: u64, bytes_per_row: u64) {
self.generation()
.degrade(Degradation::SurfaceTexelsApart, || {
let [width, height, depth] = layout.extent;
let object = format!(
"{width}x{height}x{depth} {:?} {:?} texture of {} levels and {} samples over an \
IOSurface plane at offset {offset}, {bytes_per_row} bytes a row",
layout.texture_type, layout.pixel_format, layout.levels, layout.samples
);
let reason =
"Metal lays such a texture's texels over a plane in no one place, its \
first level's and slice's among them: its GPU reads them from other \
bytes than its writes and its CPU put them"
.to_owned();
(object, reason)
});
}
/// Announce `SurfaceTexelsTail` for an IOSurface plane of `layout` placed as `placed`, whose
/// texels run past the buffer standing for its surface, or a 2D array's slices past its
/// first, into the texture's tail, once it is made (`linear_plan.rs`'s **Beyond the plane's
/// rows**).
fn degrade_surface_tail(&self, layout: &TextureLayout, placed: &Placed) {
self.generation()
.degrade(Degradation::SurfaceTexelsTail, || {
let [width, height, _] = layout.extent;
let slices = match layout.layers {
1 => String::new(),
layers => format!(" of {layers} slices"),
};
let object = format!(
"{width}x{height} {:?} texture{slices} of an IOSurface plane at offset {}, {} \
bytes a row",
layout.pixel_format, placed.cpu.offset, placed.cpu.row_pitch
);
let end = placed.gpu.end().max(placed.cpu.end());
let reason = match placed.kept {
Some(kept) => format!(
"Metal lays its slices past the first in no one place: they lie from byte \
{kept} of the buffer's space on, in the tail"
),
None => format!(
"its texels run to byte {end} of the {} bytes standing for its surface, and \
Metal lays those past the surface in no one place",
self.length()
),
};
(object, reason)
});
}
/// Announce `CollidingSurfaceRowWrites` for an IOSurface plane of `layout` placed as `placed`,
/// whose GPU rows share bytes, once it is made (`linear_plan.rs`'s **Rows that overlap**).
fn degrade_colliding_rows(&self, layout: &TextureLayout, placed: &Placed) {
self.generation()
.degrade(Degradation::CollidingSurfaceRowWrites, || {
let [width, height, _] = layout.extent;
let object = format!(
"{width}x{height} {:?} texture of an IOSurface plane at offset {}, {} bytes a \
row",
layout.pixel_format, placed.cpu.offset, placed.cpu.row_pitch
);
let reason = format!(
"Metal's GPU reads and writes its rows {} bytes apart from byte {}, {} bytes \
of texels each, so consecutive rows share {} bytes, and which row's writes \
stand there depends on the order Metal writes them in",
placed.gpu.row_pitch,
placed.gpu.offset,
placed.gpu.row_bytes,
placed.gpu.shared_bytes(),
);
(object, reason)
});
}
}
impl Texture {
/// The texture a render pass renders into for this texture buffer (or view of one), as an
/// attachment or a resolve target: a 2D texture of one row in the texture's pixel format
/// over the same bytes of the same buffer, with an aliasing image or a shadow the encoder
/// copies in and back (`render/shadows.rs`), made at the first pass that needs it and kept with
/// the texels. Metal renders into a texture buffer plain, its texels the buffer's bytes and the
/// bytes around them untouched (a probe, an M3 Max and an M4: `tb_device`, `tb_over_buffer`,
/// `tb_over_buffer_load`, `tb_view_r32uint`, `tb_resolve`); its API validation stops a texture
/// buffer made with render target usage ("MTLTextureUsageRenderTarget is not valid for a
/// texture buffer").
///
/// Refused `NotImplemented(Render)` past the device's largest framebuffer width, where Metal
/// fails the command buffer (`tb_wide`, 20000 texels: an internal error), and for a texture
/// buffer at an offset off 16 bytes, whose rendering is not measured (its functions read and
/// write from the offset rounded down, its blits at the offset); otherwise as a 2D texture over
/// a buffer is.
pub(super) fn attachment_twin(&self) -> Result<Texture, Error> {
const CALL: &str = "render_command_encoder";
let texels = self
.texels()
.ok_or_else(|| internal("an attachment twin of a texture with an image"))?;
let record = self.record();
let pixel_format = record.pixel_format;
texels.twin(pixel_format, || {
let device = texels.buffer.generation();
let width = u64::from(record.extent()[0]);
let unmeasured = !texels.layout.offset.is_multiple_of(LINEAR_ALIGNMENT);
if width > u64::from(device.shared().max_framebuffer[0]) || unmeasured {
return Err(Error::NotImplemented(RuntimeGap::Render));
}
let mut descriptor = TextureDescriptor::texture_2d(pixel_format, width, 1, false);
descriptor.usage = TextureUsage::RENDER_TARGET;
descriptor.resource_options = texels.options;
descriptor.allow_gpu_optimized_contents = texels.allow_gpu_optimized_contents;
let layout = descriptor
.check(device.properties().texture_rules())
.map_err(|refused| refused.error(CALL))?;
let row_bytes = width.saturating_mul(texels.texel_bytes);
let placed = BufferLayout {
offset: texels.layout.offset,
row_pitch: row_bytes,
slice_pitch: row_bytes,
row_bytes,
rows: 1,
slices: 1,
};
let over = Texels::new(
texels.buffer.clone(),
placed,
texels.texel_bytes,
&descriptor,
)
.with_options(texels.options)
.over_surface(texels.surface);
device.new_texture_over_buffer(CALL, over, &descriptor, &layout, IdUse::Runtime)
})
}
}
impl Device {
/// `newTextureWithDescriptor:` of a texture buffer (`layout`, checked from `descriptor`): a
/// texture buffer over a new buffer of the descriptor's options, as long as its texels;
/// `newSharedTextureWithDescriptor:` of one where `home` is `Shared`.
///
/// A `Memoryless` descriptor, of which plain Metal makes a texture (an Apple M3 Max under
/// macOS 27.0.1: a 64-texel `RGBA8Unorm` texture buffer of `ShaderRead` usage answers
/// `storageMode` 3, its other getters its descriptor's), lies over a `Private` buffer: no
/// Vulkan buffer takes memory lazily. The texture answers the descriptor's mode, and its uses
/// are a memoryless texture's (`texture.rs`): a command that uses it fails its command
/// buffer, as plain Metal's (a dispatch that reads it and a blit out of it, measured), its
/// views are nil, and its CPU access, at which plain Metal's process faults, is refused.
/// Announced `memoryless-texture-buffer`: the bytes are memory Metal spends none of.
pub(super) fn new_texture_buffer(
&self,
descriptor: &TextureDescriptor,
layout: &TextureLayout,
id_use: IdUse,
home: TexelsHome,
) -> Result<Texture, Error> {
let row_bytes = texture_buffer_bytes(layout)?;
let options = descriptor.resource_options;
let bytes = match options.storage_mode()? {
StorageMode::Memoryless => {
self.degrade(Degradation::MemorylessTextureBuffer, || {
let object = format!(
"a memoryless {:?} texture buffer of {} texels",
layout.pixel_format, layout.extent[0]
);
let why = format!("a Private buffer of its {row_bytes} bytes of texels");
(object, why)
});
options.with_storage_mode(StorageMode::Private)
}
_ => options,
};
// Metal shows no address of a texture's texels: they lie outside the space (`address.rs`).
let buffer = self.new_buffer_placed(row_bytes, bytes, AddressPlacement::Outside)?;
texture_buffer_over(buffer, descriptor, layout, id_use, home)
}
}
/// The bytes of one texel of a texture buffer of `layout`, over a buffer of its own (a device's,
/// a shareable texture's or a heap's). Refused (`Unsupported`) for a format without a texel
/// size, which no texture buffer has.
fn texture_buffer_texel(layout: &TextureLayout) -> Result<u64, Error> {
texel_bytes(layout.pixel_format).ok_or(Error::Unsupported(
Unsupported::not_implemented(Feature::Format).naming_format(layout.pixel_format),
))
}
/// The bytes of a texture buffer of `layout`: its row of texels. Refused as
/// `texture_buffer_texel` refuses the format.
pub(super) fn texture_buffer_bytes(layout: &TextureLayout) -> Result<u64, Error> {
Ok(u64::from(layout.extent[0]) * texture_buffer_texel(layout)?)
}
/// The texture buffer of `descriptor` (checked as `layout`) over the first bytes of `buffer`, as
/// many as `texture_buffer_bytes` answers, its ID as `id_use` says; `home` made `buffer` for it.
/// Refused as `new_buffer` refuses the texels' staging buffer.
pub(super) fn texture_buffer_over(
buffer: Buffer,
descriptor: &TextureDescriptor,
layout: &TextureLayout,
id_use: IdUse,
home: TexelsHome,
) -> Result<Texture, Error> {
let texel_bytes = texture_buffer_texel(layout)?;
let row_bytes = u64::from(layout.extent[0]) * texel_bytes;
let placed = BufferLayout {
offset: 0,
row_pitch: row_bytes,
slice_pitch: row_bytes,
row_bytes,
rows: 1,
slices: 1,
};
let mut texels = Texels::new(buffer, placed, texel_bytes, descriptor).into_texture_buffer()?;
texels.home = home;
// A memoryless texture buffer lies over a `Private` buffer (`Device::new_texture_buffer`) and
// answers `Memoryless`, as Metal's does, with the CPU cache and hazard tracking modes the
// buffer resolved: Metal resolves a texture buffer's default tracking to tracked (an M3 Max
// answers `hazardTrackingMode` 2 for a memoryless one). Every other texture buffer takes its
// buffer's options whole, which `Buffer::new_texture` has already made the descriptor's.
if descriptor.resource_options.storage_mode()? == StorageMode::Memoryless {
let options = texels.options.with_storage_mode(StorageMode::Memoryless);
texels = texels.with_options(options);
}
Ok(texels.texture(descriptor, layout, id_use))
}
impl Texels {
/// The texels of a texture of `descriptor` that `placed` lays out in `buffer`, `texel_bytes`
/// each: for the GPU at its GPU layout, for the CPU at its CPU one.
fn placed(
buffer: Buffer,
placed: Placed,
texel_bytes: u64,
descriptor: &TextureDescriptor,
) -> Texels {
let mut texels = Texels::new(buffer, placed.gpu, texel_bytes, descriptor);
texels.cpu = placed.cpu;
if let Some(kept) = placed.kept {
texels.kept = kept.min(texels.kept);
}
texels
}
/// The texels a rotated texture's render passes turn theirs into, these being the texels of
/// its other uses (`width` by `height` of a 2D texture at its plane's offset and rows): the
/// plane's rows from the same offset, of the extent Metal stores the texture as at
/// `rotation`. Both lie on one layout for the GPU and the CPU (the plane's offset and rows
/// are multiples of 16 bytes, `Buffer::turned_plane`).
pub(super) fn turned(&self, rotation: u8, [width, height]: [u32; 2]) -> Texels {
let [width, height] = rotated::stored_extent(rotation, [width, height]);
let row_bytes = u64::from(width) * self.texel_bytes;
let layout = BufferLayout {
row_bytes,
rows: u64::from(height),
slice_pitch: self.layout.row_pitch * u64::from(height),
..self.layout
};
let descriptor = TextureDescriptor {
allow_gpu_optimized_contents: self.allow_gpu_optimized_contents,
..TextureDescriptor::default()
};
Texels::new(self.buffer.clone(), layout, self.texel_bytes, &descriptor)
.with_options(self.options)
.over_surface(self.surface)
}
/// These texels' buffer's bytes `span` as one row of `unit`-byte texels, of these texels'
/// options: a depth or stencil texture's tiled twin's (`texture/surface_depth.rs`), whose
/// bytes are Metal's tiles there.
pub(super) fn tiled_span(&self, span: std::ops::Range<u64>, unit: u64) -> Texels {
let bytes = span.end - span.start;
let layout = BufferLayout {
offset: span.start,
row_pitch: bytes,
slice_pitch: bytes,
row_bytes: bytes,
rows: 1,
slices: 1,
};
let descriptor = TextureDescriptor {
allow_gpu_optimized_contents: self.allow_gpu_optimized_contents,
..TextureDescriptor::default()
};
Texels::new(self.buffer.clone(), layout, unit, &descriptor)
.with_options(self.options)
.over_surface(self.surface)
}
/// The attachment twin of `pixel_format` kept with the texels (a texture buffer's, or a
/// rotated texture's), made by `make` at the first pass that needs it, with the twins'
/// lock not held: a twin over dma-buf pages calls the program's source (`dma_buf.rs`). Where
/// another pass made one meanwhile, that one is kept and `make`'s goes, with no lock held.
pub(super) fn twin(
&self,
pixel_format: PixelFormat,
make: impl FnOnce() -> Result<Texture, Error>,
) -> Result<Texture, Error> {
if let Some(twin) = lock(&self.twins).get(&pixel_format) {
return Ok(twin.clone());
}
let made = make()?;
let kept = (lock(&self.twins).entry(pixel_format))
.or_insert_with(|| made.clone())
.clone();
Ok(kept)
}
/// The copies of a rotated texture's image of its pass's frame (`image` of `identity`, of
/// `extent`) over these texels, the plane's rows that hold it turned at `rotation`
/// (`rotated.rs`): through a staging buffer of their bytes and the rotation kernel. Refused
/// as `RotatedCopies::new` refuses them.
pub(super) fn rotated_copies(
&self,
image: vk::Image,
identity: Identity,
rotation: u8,
extent: [u32; 2],
) -> Result<ShadowCopies, Error> {
let layout = &self.layout;
let (buffer, at) = self.buffer.at_for(layout.offset, ReadApart::Texture);
let staging = self.staging(layout, at)?;
let frame = Frame {
rotation,
extent,
texel_bytes: self.texel_bytes,
row_pitch: layout.row_pitch,
span: layout.end() - layout.offset,
};
let hazard = self.options.hazard_tracking_mode()?;
let turn = Box::new(RotatedCopies::new(
self.buffer.generation(),
&staging,
frame,
hazard,
)?);
let (bytes, tracked) = self.buffer.tracked(layout.offset..layout.end());
Ok(ShadowCopies {
source: ShadowSource::Bytes {
buffer: ShadowBuffer::of(&self.buffer, buffer),
key: bytes.key,
bytes: tracked,
target: ShadowTarget::Rotated {
image,
staging,
turn,
},
alias: self.buffer.alias().map(|alias| alias.key),
},
shadow_key: identity.key,
shadow_range: UseRange::Image {
aspects: vk::ImageAspectFlags::COLOR,
levels: 0..1,
layers: 0..1,
},
tracked: self.tracked(),
drops_writes: false,
levels: None,
backing: self.backing(),
merge_scratch: ScratchSpares::default(),
stood: AtomicU64::new(0),
standing: Default::default(),
})
}
/// The bytes of one texel.
pub(super) fn texel_bytes(&self) -> u64 {
self.texel_bytes
}
/// Over a copied host buffer (`copied.rs`), how the texels lie in the device's lines of memory
/// of `line` bytes over the caller's pages themselves, from their address
/// (`in_place::RowEnds::of`), and whether a shader's writes may change their rows' padding
/// there (`in_place::Padding::of`): as [`rows`](Self::rows) and [`padding`](Self::padding)
/// answer them over an import of the pages, which the buffer's rebind may make, where
/// [`rows`](Self::rows) answers them in its copy. `None` over any other buffer.
pub(super) fn over_caller_pages(&self, line: u64) -> Option<(RowEnds, Padding)> {
if !self.buffer.copied() {
return None;
}
let start = self.buffer.mapped_contents()?.as_ptr().addr() as u64;
let padding = Padding::of(start, &self.layout, line, self.padding_owned());
Some((
RowEnds::of(start, &self.layout, line),
padding.declared(self.buffer.padding_unobserved()),
))
}
/// Whether the texels' buffer is a copied host buffer (`copied.rs`) whose copy lies in fresh
/// pages the runtime mapped and imported by their pointer (`fresh_pages.rs`), not in memory
/// the device allocates.
pub(super) fn copy_in_fresh_pages(&self) -> bool {
self.buffer.copied()
&& (self.buffer.host_pages()).is_some_and(|pages| pages.over_own_pages())
}
/// Where the buffer's bytes start, for the device's lines of memory: their address, where the
/// CPU addresses them; else, in the runtime's own memory (a `Private` buffer's, or a copied
/// host buffer's copy, which the GPU uses in the stead of the caller's pages, `copied.rs`),
/// their offset in its memory object, from whose start the lines count (a
/// `VkMappedMemoryRange`'s offset is a multiple of `nonCoherentAtomSize` from it, and the
/// runtime maps a block from its start); `None` over the caller's `VkBuffer`.
fn line_start(&self) -> Option<u64> {
if self.buffer.copied() {
return Some(self.buffer.memory().1);
}
match self.buffer.mapped_contents() {
Some(at) => Some(at.as_ptr().addr() as u64),
None if self.buffer.caller_memory().is_none() => Some(self.buffer.memory().1),
None => None,
}
}
/// How the texels lie in the device's lines of memory of `line` bytes
/// (`in_place::RowEnds::of`): from where the buffer's bytes start in them
/// ([`line_start`](Self::line_start)) and where the GPU's commands find the texels
/// (`layout`). Over the caller's `VkBuffer` they share a line as far as anything shows.
pub(super) fn rows(&self, line: u64) -> RowEnds {
match self.line_start() {
Some(start) => RowEnds::of(start, &self.layout, line),
None => RowEnds::InLines,
}
}
/// Whether a shader's writes may change the texels' rows' padding, in the device's lines of
/// memory of `line` bytes (`in_place::Padding::of`): rows over a buffer's bytes of the CPU's
/// or the runtime's memory ([`line_start`](Self::line_start)) whose padding is the buffer's
/// own ([`Texels::padding_owned`]: an IOSurface plane's only where the buffer declares it
/// unobserved), whose rows, each to its pitch, share no line with bytes past them; and a
/// render pass's too, where the buffer declares that padding unobserved
/// (`in_place::Padding::Unobserved`, `BufferDescriptor::padding_unobserved`).
pub(super) fn padding(&self, line: u64) -> Padding {
match self.line_start() {
Some(start) => {
let padding = Padding::of(start, &self.layout, line, self.padding_owned());
padding.declared(self.buffer.padding_unobserved())
}
None => Padding::Kept,
}
}
/// Whether the texels' rows' padding is their buffer's own (`in_place::Padding`): the buffer
/// holds their last row to its pitch, and they lie over a buffer's rows, or over an IOSurface
/// plane's where the buffer declares the bytes between a texture's rows unobserved
/// (`BufferDescriptor::padding_unobserved`). Metal keeps a plane's padding under every
/// command, but bytes its maker declares nothing reads cannot tell keeping from changing;
/// without the declaration a plane's padding is kept.
pub(super) fn padding_owned(&self) -> bool {
let to_pitch = BufferLayout {
row_bytes: self.layout.row_pitch,
..self.layout
};
(!self.surface || self.buffer.padding_unobserved())
&& to_pitch.end() <= self.buffer.length()
}
/// The texture's resource options (the `options` field), its hazard tracking mode resolved.
pub(super) fn options(&self) -> ResourceOptions {
self.options
}
/// Whether the texture's uses are tracked: its own hazard tracking mode, the buffer's but for
/// an IOSurface plane's (whose descriptor's Metal takes). A use of the texels keys on the
/// buffer's bytes whatever the mode, so a tracked texture's uses order against the buffer's
/// tracked uses, and an untracked texture's are the program's to order, as Metal's.
pub(super) fn tracked(&self) -> bool {
self.options.hazard_tracking_mode().ok() == Some(HazardTrackingMode::Tracked)
}
/// These texels as a texture of `options` (its hazard tracking mode resolved) has them.
fn with_options(self, options: ResourceOptions) -> Texels {
Texels { options, ..self }
}
/// These texels, in an IOSurface's plane where `surface`.
fn over_surface(self, surface: bool) -> Texels {
Texels { surface, ..self }
}
/// Every byte of the buffer the texels lie in, for the GPU or the CPU: from the first to the
/// last of either layout, and of the alpha plane where the texels have one.
pub(super) fn span(&self) -> std::ops::Range<u64> {
let (gpu, cpu, shader) = (&self.layout, &self.cpu, &self.shader);
let start = gpu.offset.min(cpu.offset).min(shader.offset);
let end = gpu.end().max(cpu.end()).max(shader.end());
match &self.alpha {
Some(alpha) => start.min(alpha.offset)..end.max(alpha.end()),
None => start..end,
}
}
/// Whether the texels lie in the buffer, or past its length in their tail (the module's
/// **Tails**): every texture's but a rotated one's whose rows run past it (`rotated_rows`).
pub(super) fn reached(&self) -> bool {
self.tail.is_some() || self.span().end <= self.buffer.length()
}
/// Whether Metal's GPU reads and writes two of the texels' rows over bytes they share: an
/// IOSurface plane's at its pitch rounded down to 16, or wider than its pitch, the shapes
/// that do ([`plane_rows_share_bytes`]). The shadow's copies hold the sharing
/// ([`plane_copy_regions`]); a blit that would write the rows as bytes does not and is refused
/// instead, as is a copy between them and an image texture either way, while a blit that reads
/// them into a buffer is served: a write must settle which of two rows leaves the shared bytes
/// behind and Metal settles it no one way, while a read settles nothing (`blit_texture.rs`).
pub(super) fn plane_rows_overlap(&self) -> bool {
plane_rows_share_bytes(&self.layout)
}
/// The texels of a texture of `descriptor` at `placed` in `buffer`, `texel_bytes` each.
fn new(
buffer: Buffer,
placed: BufferLayout,
texel_bytes: u64,
descriptor: &TextureDescriptor,
) -> Texels {
let views = Arc::new(TexelViews {
device: buffer.generation().clone(),
views: Mutex::new(KeyMap::default()),
});
// A rebind of the buffer's host pages empties the views (`rebind.rs`).
buffer.watch_views(&views);
// The descriptor's checks bound a texture's extent to `u32`s.
let blocks = block_extent(descriptor.pixel_format).map(|extent| Blocks {
extent,
texels: [descriptor.width as u32, descriptor.height as u32],
});
Texels {
layout: placed,
cpu: placed,
shader: placed,
texel_bytes,
blocks,
allow_gpu_optimized_contents: descriptor.allow_gpu_optimized_contents,
options: buffer.resource_options(),
views,
staged: None,
home: TexelsHome::Buffer,
twins: Mutex::new(HashMap::new()),
alpha: None,
objects: ObjectsToken::default(),
surface: false,
tail: None,
kept: buffer.length(),
buffer,
}
}
/// These texels with their tail where they run past the bytes they keep in place (the
/// buffer's length, or a 2D array's second slice, [`Texels::kept`]), made as
/// [`Device::own_buffer`] makes a buffer: all zero.
fn with_tail(self) -> Result<Texels, Error> {
if !self.runs_past() {
return Ok(self);
}
let end = self.layout.end().max(self.cpu.end()).max(self.shader.end());
let hazard = self.options.hazard_tracking_mode()?;
let options = ResourceOptions::new(StorageMode::Shared, CpuCacheMode::DefaultCache, hazard);
let span = end - self.tail_base();
let tail = (self.buffer.generation()).own_buffer(span.next_multiple_of(4), options)?;
Ok(Texels {
tail: Some(tail),
..self
})
}
/// Whether the texels run past the bytes they keep in place (the buffer's length, or a 2D
/// array's second slice, [`Texels::kept`]): where [`with_tail`](Self::with_tail) gives them a
/// tail.
pub(super) fn runs_past(&self) -> bool {
self.layout.end().max(self.cpu.end()).max(self.shader.end()) > self.kept
}
/// Where the texels' bytes past the buffer's length lie, where they run past it: from the
/// buffer's length on (a 2D array's second slice on, [`Texels::kept`]), each byte `v` of the
/// buffer's space at `v - rebase` of the tail.
pub(super) fn past(&self) -> Option<Past<'_>> {
self.tail.as_ref().map(|tail| Past {
tail,
length: self.kept,
rebase: self.tail_base(),
})
}
/// The byte of the buffer's space the tail's first byte stands for: the texels' first, as
/// the GPU, the CPU or a function lays them out (a texture buffer's functions read it from its
/// offset rounded down to 16 bytes, `into_texture_buffer`).
fn tail_base(&self) -> u64 {
self.layout
.offset
.min(self.cpu.offset)
.min(self.shader.offset)
}
/// What holds the texels' bytes, as the CPU may write them (`shadow::Backing`).
pub(super) fn backing(&self) -> Backing {
Backing::of(&self.buffer, self.surface)
}
/// Where the CPU's `replace_region` and `get_bytes` find the texels.
pub(super) fn cpu_layout(&self) -> BufferLayout {
self.cpu
}
/// The texels' two layouts, `(gpu, cpu)`: where Metal's GPU finds them and where the CPU does
/// (`linear_plan::Placed`), which together say whether its rows share bytes
/// ([`plane_rows_share_bytes`]). A row copy of the texels as the buffer's bytes takes both
/// (`blit_plan::plan_texels_rows_copy`).
pub(super) fn row_layouts(&self) -> (&BufferLayout, &BufferLayout) {
(&self.layout, &self.cpu)
}
/// The alpha plane of a two-plane extended-range texture over a surface's plane 0, where
/// Metal's GPU finds it (`texture/surface_two_planes.rs`); `None` for every other texture.
pub(super) fn alpha(&self) -> Option<&BufferLayout> {
self.alpha.as_ref()
}
/// These texels with the alpha plane `alpha` (a two-plane extended-range texture's over a
/// surface's plane 0, `texture/surface_two_planes.rs`).
pub(super) fn with_alpha(self, alpha: BufferLayout) -> Texels {
Texels {
alpha: Some(alpha),
..self
}
}
/// These texels as a texture buffer's, of their options and surface. A function reads and
/// writes them from their offset rounded down to 16 bytes, as
/// plain Metal's do where its validation alone stops an offset off 16 (an M4 under macOS
/// 27.0.1), and blits and the CPU at the offset itself (`layout`). Viewed in place where the
/// device's `minTexelBufferOffsetAlignment`, which the binding plan holds a view's offset to,
/// divides the functions' offset in the buffer's `VkBuffer`; else through a staging buffer of
/// their own, at its start (a no-copy buffer's bytes, or Metal's offset, need not lie at a
/// multiple of it), and through their tail where they run past the buffer's length, which no
/// view of the buffer reaches. Refused as `new_buffer` refuses the staging buffer.
fn into_texture_buffer(self) -> Result<Texels, Error> {
let mut texels = self;
let placed = texels.layout;
texels.shader = BufferLayout {
offset: placed.offset - placed.offset % LINEAR_ALIGNMENT,
..placed
};
// Texels past the buffer's length lie in a tail of their own (the module's **Tails**),
// which their bindings view, as a staging buffer's: no view of the buffer reaches them.
let mut texels = texels.with_tail()?;
let (raw, at) = (texels.buffer).at_for(texels.shader.offset, ReadApart::Texture);
let device = texels.buffer.generation();
let limits = device.compile_device().profile().limits();
let past = texels.tail.is_some();
if !past && at.is_multiple_of(limits.min_texel_buffer_offset_alignment.max(1)) {
return Ok(texels);
}
let staging = texels.staging(&texels.shader, at)?;
let layout = &texels.shader;
let shadow = staging.buffer.identity();
let (bytes, tracked) = texels.buffer.tracked(layout.offset..layout.end());
// Every byte of the texels' its bindings view, those past the length among them.
let shadow_range = UseRange::Bytes(0..layout.end() - layout.offset);
let copies = ShadowCopies {
source: ShadowSource::Bytes {
buffer: ShadowBuffer::of(&texels.buffer, raw),
key: bytes.key,
bytes: tracked,
target: ShadowTarget::Buffer(staging),
alias: texels.buffer.alias().map(|alias| alias.key),
},
shadow_key: shadow.key,
shadow_range,
tracked: texels.tracked(),
drops_writes: false,
levels: None,
backing: texels.backing(),
merge_scratch: ScratchSpares::default(),
stood: AtomicU64::new(0),
standing: Default::default(),
};
texels.staged = Some(Arc::new(copies));
let (offset, alignment) = (
texels.layout.offset,
limits.min_texel_buffer_offset_alignment,
);
let length = texels.buffer.length();
texels
.buffer
.generation()
.degrade(Degradation::StagedTextureBuffer, || {
let object = format!("texture buffer over a buffer at offset {offset}");
let reason = match past {
true => format!(
"its texels run past the buffer's {length} bytes, where no view of the \
buffer's VkBuffer reaches: those lie in a tail of the texture's own"
),
false => format!(
"its texels start at byte {at} of the buffer's VkBuffer, and the device \
views texels only at multiples of {alignment} bytes \
(minTexelBufferOffsetAlignment)"
),
};
(object, reason)
});
Ok(texels)
}
/// A texture buffer's staging copies, where its bindings view a staging buffer.
pub(super) fn staged(&self) -> Option<&Arc<ShadowCopies>> {
self.staged.as_ref()
}
/// The `VkBuffer` the texels' bindings view, and the texels' offset in it.
fn viewed(&self) -> (vk::Buffer, u64) {
match self.staged.as_deref().and_then(ShadowCopies::target) {
Some(ShadowTarget::Buffer(staging)) => (staging.raw(), staging.into.dst_offset),
_ => (self.buffer).at_for(self.shader.offset, ReadApart::Texture),
}
}
/// The texture buffer of `descriptor` (checked as `layout`) these texels are, its ID as
/// `id_use` says.
fn texture(
self,
descriptor: &TextureDescriptor,
layout: &TextureLayout,
id_use: IdUse,
) -> Texture {
let record = ViewRecord::of_descriptor(layout, descriptor);
Texture::of_texels(self, record, id_use)
}
/// The copies of a shadowed texture over the texels (`shadow.rs`), whose image is `image`
/// of `identity`, with `aspects`: one region per slice, the slice's rows of texels. Where
/// the texels start in the buffer's `VkBuffer` at an offset that is not a multiple of their
/// size, which Vulkan copies texels at (a no-copy buffer's bytes need not start at one), the
/// copies go through a staging buffer of the texture's ([`ShadowStaging`]). Its copies back
/// dispatch through `words`, its image's integer view, where given and its one slice is
/// copied in place (`shadow_copy_back.rs`), and its merges read the texels through `unorm`,
/// its image's view of its own 8-bit unsigned normalized format, where given so
/// (`shadow_merge.rs`). Refused as `new_buffer` refuses the staging buffer.
/// Whether these texels take a device copy on `device` (`texture.rs`, `dirty_pages.rs`): on
/// a discrete GPU, a `Managed` texture over a buffer's bytes (not an IOSurface plane's),
/// tracked, over host pages whose memory carries a log, where the shadow's copy in is one
/// region straight from the texels' place in the buffer's `VkBuffer` (one slice, rows that
/// share no bytes, a first texel at a multiple of the texel's size there), the copy in a log
/// decides. A `Managed` resource on a Mac with a discrete GPU is two copies, the system
/// memory's and the GPU's own; a `Shared` texture and an IOSurface's are the system memory's
/// alone, which that GPU reads across the bus, so they take none.
pub(super) fn device_copy(&self, device: &Device) -> bool {
let layout = &self.layout;
// The looks at the pages last: each takes their lock (`WorkCounts::no_copy_lies_read`),
// which a texture on any other GPU never needs.
device.properties().kind == crate::DeviceKind::Discrete
&& !self.surface
&& matches!(self.options.storage_mode(), Ok(StorageMode::Managed))
&& self.tracked()
&& layout.slices == 1
&& layout.shared_bytes() == 0
&& (self.buffer.at_for(layout.offset, ReadApart::Texture).1)
.is_multiple_of(self.texel_bytes)
&& (self.buffer.host_pages()).is_some_and(|pages| pages.dirty().is_some())
}
pub(super) fn shadow_copies(
&self,
image: vk::Image,
identity: Identity,
aspects: vk::ImageAspectFlags,
(words, unorm): (Option<WordView>, Option<UnormView>),
) -> Result<ShadowCopies, Error> {
let layout = &self.layout;
let (buffer, at) = self.buffer.at_for(layout.offset, ReadApart::Texture);
// Vulkan copies a depth or stencil aspect at multiples of 4 bytes only.
let depth = vk::ImageAspectFlags::DEPTH | vk::ImageAspectFlags::STENCIL;
let align = match aspects.intersects(depth) {
true => 4,
false => self.texel_bytes,
};
// Texels past the buffer's length go through their tail, which holds them.
let staging = if at.is_multiple_of(align) && self.tail.is_none() {
None
} else {
Some(self.staging(layout, at)?)
};
let start = staging
.as_ref()
.map_or(at, |staging| staging.into.dst_offset);
// The descriptor's checks bound a texture's extent and slices to `u32`s.
let slices = layout.slices as u32;
let (regions, back) =
plane_copy_regions(layout, (self.texel_bytes, self.blocks), start, aspects);
// A copy back dispatches, and a merge fetches, over one region copied in place alone.
let one_region = regions.len() == 1 && slices == 1 && staging.is_none();
let (bytes, tracked) = self.buffer.tracked(layout.offset..layout.end());
Ok(ShadowCopies {
source: ShadowSource::Bytes {
buffer: ShadowBuffer::of(&self.buffer, buffer),
key: bytes.key,
bytes: tracked,
target: ShadowTarget::Image {
words: words.filter(|_| one_region),
unorm: unorm.filter(|_| one_region),
image,
regions,
back,
staging,
// A block-compressed texture is no color attachment, whose copies alone are
// bounded.
texel: self.blocks.is_none().then_some(self.texel_bytes),
},
alias: self.buffer.alias().map(|alias| alias.key),
},
shadow_key: identity.key,
shadow_range: UseRange::Image {
aspects,
levels: 0..1,
layers: 0..slices,
},
tracked: self.tracked(),
drops_writes: false,
levels: None,
backing: self.backing(),
merge_scratch: ScratchSpares::default(),
stood: AtomicU64::new(0),
standing: StandingAcross::over(&self.buffer),
})
}
/// The staging buffer of a shadowed texture or a texture buffer over the texels laid out as
/// `layout`, which start at `at` of the buffer's `VkBuffer`: the texels' bytes from the first
/// to the last, at its start, of the texture's hazard tracking, in a buffer of whole 4-byte
/// words (the rotation kernel's, `rotated.rs`, binds them all). Texels past the buffer's
/// length take their tail, whose bytes past the length are the texels' own: the copies
/// between it and the buffer move the bytes inside the length alone ([`inside_rows`]).
fn staging(&self, layout: &BufferLayout, at: u64) -> Result<ShadowStaging, Error> {
let span = layout.end() - layout.offset;
let buffer = match &self.tail {
Some(tail) => tail.clone(),
None => {
let hazard = self.options.hazard_tracking_mode()?;
let options =
ResourceOptions::new(StorageMode::Private, CpuCacheMode::DefaultCache, hazard);
(self.buffer.generation()).own_buffer(span.next_multiple_of(4), options)?
}
};
let (_, start) = buffer.at(0);
let inside = match self.tail {
Some(_) => self.kept.saturating_sub(layout.offset).min(span),
None => span,
};
let back = inside_rows(staged_rows_back(layout, start, at), at, inside);
Ok(ShadowStaging {
into: vk::BufferCopy {
src_offset: at,
dst_offset: start,
size: inside,
},
back,
buffer,
})
}
/// The copy unit of CPU access to the texels: one texel, or one block of a block-compressed
/// texture.
pub(super) fn unit(&self) -> CopyUnit {
CopyUnit {
bytes: self.texel_bytes as u32,
extent: self.blocks.map_or([1, 1], |blocks| blocks.extent),
aspect: ImageAspectFlags::COLOR,
}
}
/// What a binding plan reads of a texture buffer of `pixel_format` and `swizzle` over these
/// texels: the texels' offset in the `VkBuffer` its bindings view (`Texels::viewed`), which
/// the plan's view ranges address, and the swizzle a function's reads apply.
pub(super) fn bound(
&self,
pixel_format: PixelFormat,
swizzle: TextureSwizzleChannels,
) -> BoundTexture {
BoundTexture::Buffer {
pixel_format,
offset: self.viewed().1,
length: self.shader.row_bytes,
bytes_per_row: self.shader.row_pitch,
resource_id: None,
swizzle,
}
}
/// A use of the texels' bytes of the buffer, reading by `read` and writing by `write`; of
/// the staging buffer, for a texture buffer viewed through one (a shadow's use).
pub(super) fn bound_use(&self, read: Access, write: Access) -> Use<ResourceKey> {
if let Some(copies) = &self.staged {
return Use {
key: copies.shadow_key,
tracked: copies.tracked,
range: copies.shadow_range.clone(),
read,
write,
};
}
let shader = &self.shader;
let (identity, range) = self.buffer.tracked(shader.offset..shader.end());
Use {
key: identity.key,
tracked: self.tracked(),
range: UseRange::Bytes(range),
read,
write,
}
}
/// The texel buffer view of `range` of the buffer in `format`, a storage texel buffer's
/// (`storage`) or a uniform one's, made at its first use, with the usage [`view_usage`]
/// gives it.
pub(super) fn view(
&self,
format: Format,
storage: bool,
range: BufferRange,
) -> Result<vk::BufferView, Error> {
self.view_of(format, storage, (range.offset, range.range))
}
/// [`view`](Self::view) of `range` bytes from `offset` in the viewed `VkBuffer`.
pub(super) fn view_of(
&self,
format: Format,
storage: bool,
(offset, range): (u64, u64),
) -> Result<vk::BufferView, Error> {
let key = (format, storage, offset, range);
let mut views = lock(&self.views.views);
if let Some(&view) = views.get(&key) {
return Ok(view);
}
let device = self.buffer.generation();
let profile = device.compile_device().profile();
let usage = view_usage(
|usage| profile.supports_format(format, usage),
storage,
device.enabled().features.contains(&VkFeature::Maintenance5),
)?;
// The range is the viewed `VkBuffer`'s already ([`Texels::bound`]).
let (buffer, _) = self.viewed();
let mut only = vk::BufferUsageFlags2CreateInfoKHR::default();
let mut info = vk::BufferViewCreateInfo::default()
.buffer(buffer)
.format(format.into())
.offset(offset)
.range(range);
if let Some(named) = usage.named() {
only = only.usage(named);
info = info.push_next(&mut only);
}
// SAFETY: the buffer is live and made for both texel buffer usages; `format` serves the
// usages the view has (both, or the one its `VkBufferUsageFlags2CreateInfoKHR` names
// under the `maintenance5` the device enabled, [`view_usage`]); the binding plan aligned
// the range and bounded its texels (`bind::instantiate::texel_ranges`).
let view =
unsafe { device.shared().vk().create_buffer_view(&info, None) }.map_err(vulkan)?;
views.insert(key, view);
Ok(view)
}
/// The bytes of `region` (in units of one texel) at the texels' place in the buffer, as a
/// layout of the buffer's bytes from the region's first, which starts that many bytes into
/// the buffer.
fn region_layout(&self, region: &ImageRegion, host: &HostLayout) -> (u64, HostLayout) {
let layout = &self.cpu;
let [x, y, _] = region.origin.map(u64::from);
// A block-compressed texture's region starts on a block (the copy's check).
let [w, h] = self.unit().extent.map(u64::from);
let (x, y) = (x / w, y / h);
let start = layout.offset
+ layout.slice_pitch * u64::from(region.layer)
+ layout.row_pitch * y
+ self.texel_bytes * x;
(
start,
HostLayout {
bytes_per_row: layout.row_pitch,
bytes_per_image: layout.slice_pitch,
..*host
},
)
}
/// The buffer's bytes `start..start + len` (`None` for more than `u64::MAX`), which lie in it:
/// their first byte and their end. Refused for a buffer the CPU does not reach (`Private`).
fn bytes(
&self,
call: &'static str,
start: u64,
len: Option<u64>,
) -> Result<(*mut u8, u64), Error> {
let contents = self
.buffer
.mapped_contents()
.ok_or_else(|| invalid(call, Problem::Copy(CopyProblem::CpuAccessPrivate)))?;
// The texture lies in the buffer, and so do the rows of a region checked in its level.
let end = len.and_then(|len| start.checked_add(len));
let Some(end) = end.filter(|&end| end <= self.buffer.length()) else {
return Err(internal("a texture's rows past its buffer"));
};
// SAFETY: `start` lies in the buffer's mapped bytes (checked above).
Ok((unsafe { contents.as_ptr().add(start as usize) }, end))
}
/// `replaceRegion` of `region` from `bytes` laid out as `host` (checked by the texture): into
/// the buffer's mapped bytes, and from there to its own where it has a host copy (a `Managed`
/// buffer on a device without unified memory, `Buffer::upload`), as Metal's `replaceRegion`
/// updates a managed texture for the GPU.
pub(super) fn write(
&self,
call: &'static str,
region: &ImageRegion,
host: &HostLayout,
bytes: &[u8],
) -> Result<(), Error> {
let mut packed = vec![0u8; host.packed_len() as usize];
host.pack(bytes, &mut packed);
let (start, inside) = self.region_layout(region, host);
if let Some(past) = self.past() {
return self.write_past(call, (start, &inside), &packed, past);
}
let (target, end) = self.bytes(call, start, inside.host_len())?;
let n = host.row_bytes as usize;
for (at, from) in inside.rows() {
// SAFETY: every row lies in the buffer's mapped bytes from `start` (`bytes`); a write
// racing the GPU's use of them is the program's, as through `contents`.
unsafe { std::ptr::copy_nonoverlapping(packed[from..].as_ptr(), target.add(at), n) };
}
// A write of host pages a consumer logs is the runtime's own, which its log does not see:
// reported, so the copies of the pages take it (`dirty_pages.rs`).
let at = target as u64;
self.buffer.wrote_host(at..at + (end - start));
self.buffer.modified(start..end);
Ok(())
}
/// The mapped bytes the CPU reaches the texels' bytes `start..start + len` of the buffer's
/// space through, where the texels run past its length (`past`): the part inside the length
/// in the buffer's, the rest in the tail's, each `(where its bytes go in the run, its first
/// byte, its length)`. Refused as [`bytes`](Self::bytes) refuses the buffer's.
fn past_runs(
&self,
call: &'static str,
(start, len): (u64, u64),
past: Past<'_>,
) -> Result<Vec<(usize, *mut u8, usize)>, Error> {
let private = || invalid(call, Problem::Copy(CopyProblem::CpuAccessPrivate));
let end = start + len;
let mut runs = Vec::with_capacity(2);
if start < past.length {
let contents = self.buffer.mapped_contents().ok_or_else(private)?;
// SAFETY: `start..past.length` lies in the buffer's mapped bytes.
let first = unsafe { contents.as_ptr().add(start as usize) };
runs.push((0, first, (end.min(past.length) - start) as usize));
}
if end > past.length {
let from = start.max(past.length);
let contents = past.tail.mapped_contents().ok_or_else(private)?;
let at = from - past.rebase;
if at + (end - from) > past.tail.length() {
return Err(internal("a texture's rows past its tail"));
}
// SAFETY: `at..at + (end - from)` lies in the tail's mapped bytes (checked above).
let first = unsafe { contents.as_ptr().add(at as usize) };
runs.push(((from - start) as usize, first, (end - from) as usize));
}
Ok(runs)
}
/// [`write`](Self::write) of texels that run past the buffer's length: each row's bytes
/// inside the length into the buffer's, the rest into the tail's.
fn write_past(
&self,
call: &'static str,
(start, inside): (u64, &HostLayout),
packed: &[u8],
past: Past<'_>,
) -> Result<(), Error> {
let n = inside.row_bytes;
let mut end = start;
for (at, from) in inside.rows() {
let row = start + at as u64;
for (into, target, len) in self.past_runs(call, (row, n), past)? {
// SAFETY: each run lies in the buffer's or the tail's mapped bytes
// (`past_runs`); a write racing the GPU's use of them is the program's.
unsafe {
std::ptr::copy_nonoverlapping(packed[from + into..].as_ptr(), target, len)
};
}
end = end.max(row + n);
}
// The buffer's own bytes written, as `write` reports and uploads them.
let end = end.min(past.length);
if start < end {
let contents = self
.buffer
.mapped_contents()
.ok_or_else(|| invalid(call, Problem::Copy(CopyProblem::CpuAccessPrivate)))?;
let at = contents.as_ptr() as u64 + start;
self.buffer.wrote_host(at..at + (end - start));
self.buffer.modified(start..end);
}
Ok(())
}
/// `getBytes` of `region` into `bytes` laid out as `host` (checked by the texture): from the
/// buffer's mapped bytes, a host copy's where it has one, which `synchronize_resource` and
/// `synchronize_texture` update, as Metal's managed texture needs `synchronizeTexture` on a
/// device without unified memory.
pub(super) fn read(
&self,
call: &'static str,
region: &ImageRegion,
host: &HostLayout,
bytes: &mut [u8],
) -> Result<(), Error> {
let mut packed = vec![0u8; host.packed_len() as usize];
let (start, inside) = self.region_layout(region, host);
if let Some(past) = self.past() {
for (at, to) in inside.rows() {
let row = start + at as u64;
for (into, source, len) in self.past_runs(call, (row, host.row_bytes), past)? {
// SAFETY: as in `write_past`.
unsafe {
std::ptr::copy_nonoverlapping(source, packed[to + into..].as_mut_ptr(), len)
};
}
}
host.unpack(&packed, bytes);
return Ok(());
}
let (source, _) = self.bytes(call, start, inside.host_len())?;
let n = host.row_bytes as usize;
for (at, to) in inside.rows() {
// SAFETY: as in `write`.
unsafe { std::ptr::copy_nonoverlapping(source.add(at), packed[to..].as_mut_ptr(), n) };
}
host.unpack(&packed, bytes);
Ok(())
}
}
/// The usage a texel buffer view of a format of the device's takes, beside the buffer's.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum ViewUsage {
/// The buffer's: the format serves both kinds of texel buffer the buffer is made for.
Buffer,
/// Only the view's own kind, a storage texel buffer (`storage`) or a uniform one
/// (`VkBufferUsageFlags2CreateInfoKHR`): the format serves it and not the other.
Only { storage: bool },
}
impl ViewUsage {
/// The usage a view of [`ViewUsage::Only`] names, or `None` for the buffer's.
fn named(self) -> Option<vk::BufferUsageFlags2KHR> {
match self {
ViewUsage::Buffer => None,
ViewUsage::Only { storage: true } => {
Some(vk::BufferUsageFlags2KHR::STORAGE_TEXEL_BUFFER)
}
ViewUsage::Only { storage: false } => {
Some(vk::BufferUsageFlags2KHR::UNIFORM_TEXEL_BUFFER)
}
}
}
}
/// The usage of a texel buffer view of a storage texel buffer (`storage`) or a uniform one, in a
/// format `supports` says which kinds of texel buffer the device takes it for, on a device that
/// enabled `maintenance5` or not. The runtime makes every buffer for both kinds (`buffer::usage`),
/// and a view's format must serve every texel buffer usage the view has
/// (`VUID-VkBufferViewCreateInfo-format-08778`, `-08779`): the buffer's where the format serves
/// both, else only the view's own kind, which `maintenance5` lets a view name
/// (`VUID-VkBufferViewCreateInfo-pNext-08780`, `-08781`). Without `maintenance5` that view is
/// not made (`NotImplemented(Textures)`), nor one in a format that does not serve the view's
/// kind, which no binding plan names.
pub(super) fn view_usage(
supports: impl Fn(FormatUsage) -> bool,
storage: bool,
maintenance5: bool,
) -> Result<ViewUsage, Error> {
let (own, other) = if storage {
(
FormatUsage::StorageTexelBuffer,
FormatUsage::UniformTexelBuffer,
)
} else {
(
FormatUsage::UniformTexelBuffer,
FormatUsage::StorageTexelBuffer,
)
};
match (supports(own), supports(other)) {
(true, true) => Ok(ViewUsage::Buffer),
(true, false) if maintenance5 => Ok(ViewUsage::Only { storage }),
_ => Err(Error::NotImplemented(RuntimeGap::Textures)),
}
}
/// The regions that copy the texels laid out as `layout` from a staging buffer, where they start at
/// `start`, back into the texture's `VkBuffer`, where they start at `at` (`ShadowStaging::back`):
/// one a row, but one for rows that lie one after another or share bytes.
///
/// A row that shares bytes with the one above (an IOSurface plane's on Metal's GPU,
/// `linear_plan::iosurface_gpu_layout`) extends that row's region rather than opening one of its
/// own: a region of each would write the shared bytes twice in one `vkCmdCopyBuffer`, which no
/// two destination regions of one may do (`VUID-vkCmdCopyBuffer-pRegions-12482`; older releases
/// read it off `-00117`'s union of the destination regions). The staging buffer holds the plane's
/// bytes as they lie, the shadow's copy back having written each of them once
/// ([`plane_copy_regions`]), so one region of the rows' whole span moves them as they are.
pub(super) fn staged_rows_back(layout: &BufferLayout, start: u64, at: u64) -> Vec<vk::BufferCopy> {
let rows = (0..layout.slices).flat_map(|slice| {
(0..layout.rows).map(move |row| slice * layout.slice_pitch + row * layout.row_pitch)
});
let mut back: Vec<vk::BufferCopy> = Vec::new();
for row in rows {
let end = start + row + layout.row_bytes;
match back.last_mut() {
Some(last) if last.src_offset + last.size >= start + row => {
last.size = end - last.src_offset;
}
_ => back.push(vk::BufferCopy {
src_offset: start + row,
dst_offset: at + row,
size: layout.row_bytes,
}),
}
}
back
}
/// `back`, copies of rows from a staging buffer into a buffer's `VkBuffer` whose texels start at
/// its byte `at`, cut to the texels' first `inside` bytes: a texture's whose texels run past the
/// buffer's length, whose bytes past it its tail keeps ([`Texels::staging`]). A row past them is
/// dropped, and one across them cut there.
pub(super) fn inside_rows(back: Vec<vk::BufferCopy>, at: u64, inside: u64) -> Vec<vk::BufferCopy> {
let end = at + inside;
back.into_iter()
.filter(|row| row.dst_offset < end)
.map(|row| vk::BufferCopy {
size: row.size.min(end - row.dst_offset),
..row
})
.collect()
}
/// The regions that copy the texels laid out as `layout`, `texel_bytes` each (of a block each,
/// where `blocks` gives a block-compressed texture's: its rows of blocks are copied as the texels
/// they hold, its last block of a row and of a column covering its texels alone), starting at
/// `start` of the `VkBuffer` the copies address, into a 2D texture's shadow image of `aspects`, and the
/// regions that copy them back out of it (`shadow.rs`, [`Texels::shadow_copies`]): one region a
/// slice each way, the slice's rows of texels, where each row lies a row apart from the one above
/// or further. Metal's slices lie multiples of 128 bytes apart and its rows multiples of 16, so
/// of every texel size.
///
/// Where consecutive rows share bytes (an IOSurface plane's on Metal's GPU,
/// `linear_plan::iosurface_gpu_layout`, and a heap's one-plane YCbCr texture's,
/// `texture/heap.rs`'s `heap_planes`: 1 to 15 bytes, a whole number of texels, between
/// consecutive rows alone; and a plane's whose rows are wider than its pitch, any whole number
/// of texels, a byte shared by as many rows as reach it), no one region states a slice: a region
/// addresses its rows one `bufferRowLength` apart, which is at least its width
/// (`VUID-vkCmdCopyBufferToImage-bufferRowLength-00195`), and the shared bytes hold a texel of
/// each of two rows or more. Each slice takes two regions instead, split at the texels the
/// sharing covers:
///
/// - going in, every row's shared head (in pieces no wider than the pitch, where the head is
/// wider: a region's `bufferRowLength` covers its width) and the rest of every row, which is
/// one pitch wide. Their sources share the bytes, which a copy into an image allows: a memory
/// location may not be read by a source region and written by a destination region of one copy
/// (`VUID-vkCmdCopyBufferToImage-pRegions-00173`), nor written by two destination regions
/// (`-12483`), and the regions' image texels are disjoint and together every texel;
/// - going back, the rest of every row, and the head of row 0 alone (one row, its
/// `bufferRowLength` 0 where the head is wider than the pitch). Those tile the slice's bytes
/// exactly once, so no byte is written by two destination regions
/// (`VUID-vkCmdCopyImageToBuffer-pRegions-12485`), and every shared byte takes the earliest
/// row's that reaches it. Metal leaves there whichever row its own command wrote last, an order
/// that follows the command, the GPU pitch and the row count rather than one rule
/// (`linear_plan.rs`'s **Rows that overlap**); a kernel, the only GPU writer such a plane
/// takes, moves its winner with the threadgroup shape, so the earliest row's bytes are one of
/// its answers ([`Degradation::CollidingSurfaceRowWrites`]).
pub(super) fn plane_copy_regions(
layout: &BufferLayout,
(texel_bytes, blocks): (u64, Option<Blocks>),
start: u64,
aspects: vk::ImageAspectFlags,
) -> (Vec<vk::BufferImageCopy>, Vec<vk::BufferImageCopy>) {
let shared = layout.shared_bytes();
let width = (layout.row_bytes / texel_bytes) as u32;
let pitch = (layout.row_pitch / texel_bytes) as u32;
// The descriptor's checks bound a texture's extent and slices to `u32`s.
let (slices, rows) = (layout.slices as u32, layout.rows as u32);
// A block-compressed texture's rows of blocks, as Vulkan copies them: its pitch and offsets
// in texels, its extent its own texels (`VUID-vkCmdCopyBufferToImage-imageExtent-00207`).
let [bw, bh] = blocks.map_or([1, 1], |blocks| blocks.extent);
let texels = |units: u32, side: usize| {
let texels = units * [bw, bh][side];
blocks.map_or(texels, |blocks| texels.min(blocks.texels[side]))
};
let region = |slice: u32, at: u64, x: u32, units: u32, height: u32| vk::BufferImageCopy {
buffer_offset: at + layout.slice_pitch * u64::from(slice),
buffer_row_length: pitch * bw,
buffer_image_height: 0,
image_subresource: vk::ImageSubresourceLayers {
aspect_mask: aspects,
mip_level: 0,
base_array_layer: slice,
layer_count: 1,
},
image_offset: vk::Offset3D {
x: (x * bw) as i32,
y: 0,
z: 0,
},
image_extent: vk::Extent3D {
width: texels(x + units, 0) - x * bw,
height: texels(height, 1),
depth: 1,
},
};
if shared == 0 {
let regions: Vec<vk::BufferImageCopy> = (0..slices)
.map(|slice| region(slice, start, 0, width, rows))
.collect();
return (regions.clone(), regions);
}
let head = (shared / texel_bytes) as u32;
let (mut into, mut back) = (Vec::new(), Vec::new());
for slice in 0..slices {
// The head in pieces no wider than the pitch, where rows wider than their pitch share
// more than a pitch's bytes: a region's rows lie its `bufferRowLength` apart.
let mut x = 0;
while x < head {
let units = pitch.min(head - x);
into.push(region(
slice,
start + u64::from(x) * texel_bytes,
x,
units,
rows,
));
x += units;
}
into.push(region(slice, start + shared, head, width - head, rows));
// Row 0's head is one row, whose bytes lie one after another however wide it is.
let row_zero = region(slice, start, 0, head, 1);
back.push(match head > pitch {
true => vk::BufferImageCopy {
buffer_row_length: 0,
..row_zero
},
false => row_zero,
});
back.push(region(slice, start + shared, head, width - head, rows));
}
(into, back)
}