Files
metal2vulkan/crates/metal2vulkan-runtime/src/texture/shared.rs
T
Anees Iqbal e453efbf0a RUNTIME 2309: a texture whose rows are in place makes its shadow at the first reach of its image
A device-gap texture over a buffer (its image refused for a reason that leaves its rows in
place: format, pitch, placement, source, dma-buf) was a shadow made at new_texture: its
VkImage, memory and views, its notice and shadow-log line, and its slot in the texture table
and every heap set. A slot whose function only reads, writes or asks its size binds the rows
in place, so a texture only ever bound that way paid for a shadow no command used (reims's
gbs91 on the 780M: hundreds of such textures, 8 to 52 MB each).

Now such a texture (rows in place, no pending in-place verdict, not over a heap's buffer) is
made with its shadow left unmade (texture/lazy_shadow.rs): the storage accessor makes it at
the first reach, once, under a lock, with the making's notice, log line and counts, then
places its slot (its resource ID reads the neutral until then). Paths that must not make it
read the recipe instead (rows binding, placement and heap-set writes, CPU copies, blits on
the bytes, shape and plan questions, logging). The reaches that make it: a slot that binds
the image (before the command's copies are planned from its uses), use_resource, a pass's
attachment, an execution of commands inheriting the bindings, a view, its VkImage handed out.
An out-of-memory making fails the reaching call, as new_texture did.

WorkCounts::shadows_left_unmade and shadows_made_at_use count both halves; docs/API.md says
when the shadowed-texture notice is given.

Not in scope: two kinds still make their shadow at new_texture. A texture waiting on its pages'
in-place verdict may move in place. A texture over a heap's buffer is made resident by use_heap
through a path that checks the shadow.

RUNTIME_VERSION moves because the run's announcements change. The shadowed-texture notice and the
shadow-log line come at the making, and a texture only ever bound through its rows gives neither.
A resource-ID read of an unmade texture gets the neutral. No new Degradation kind. No FAST-PATHS
row: the copy work follows uses, not the shadow's existence (an unbound shadow was copied 0 times
per commit before and after), so no accuracy is traded. The saving is the making: the VkImage, its
memory, views and heap-set slots.

Test: tests::texel_rows_device::a_shadow_is_made_at_the_first_reach_of_its_image, on a copied
buffer, since over the pages lavapipe lays the rows in place. Three cases:
- a written-only texture: shadows_left_unmade +1, none made, shadowed_textures 0,
  shadow_copies 0, texels inverted in the pages;
- a rows write then a sample in one command buffer: made at the sample, copies > 0, pixels equal
  to a device-own texture's;
- use_resource: made at the call.
joined_residency's helper asserts shadows_bytes() where it asserted the shadow at new_texture.

Verified, msi m3-max-30024 at 088a73d3b7:
- clippy -D warnings, runtime crate, all targets and features: plain and --cfg m2v_cpu_icd exit 0;
- lavapipe 206/206: texel_rows, shadow*, read_twin*, joined_residency, padding/kept_padding,
  exported_*, first_host_texture, surface_shapes, attached_read*, copied_device, fresh_pages,
  texture::*;
- translator api_signatures, api_surface and fast_paths: 47/47;
- the negative control in the same job (lazy = false): the test fails at "Copy: left unmade",
  (0,0,1) against (1,0,0).
2026-11-14 12:15:15 -08:00

237 lines
11 KiB
Rust

//! Shared textures and their in-process handles (`newSharedTextureWithDescriptor:`,
//! `isShareable`, `newSharedTextureHandle`, `newSharedTextureWithHandle:`).
//!
//! A handle names a texture's image within this process: opening it gives a new texture object
//! over the same image, as Metal's does. Cross-process export is not built (not scheduled). Metal's
//! answers, measured on an Apple M3 Max (macOS 26.7, plain and under API validation):
//! - only a `Private` texture is made shareable: any other storage mode answers nil, and API
//! validation stops it ("Requested storage mode is not allowed for Shared Textures"); a texture
//! buffer and a multisample texture are made, and a texture opened from a texture buffer's handle
//! reads what a kernel wrote through the texture buffer (a Metal probe on the M3 Max and an M4).
//! The storage rule was re-read on the M3 Max under macOS 27.0.1, one cell per process and eight
//! processes a cell: `Shared`, `Managed` and `Memoryless` answered nil 8 of 8 for `RGBA8Unorm`,
//! `R16Uint` and each of the four padded 10-bit formats, and `Private` a texture 8 of 8 for
//! each, the padded ones among them;
//! - a texture made otherwise, a texture over a buffer, and a view of a shareable texture are
//! not shareable, and their handle is nil;
//! - each open answers a new texture object, shareable, with the texture's properties (its
//! label too) and no parent, and a resource ID of its own.
//!
//! The opened texture and the texture share one image, or a texture buffer's one buffer of
//! texels, and so one resource for hazard tracking: the runtime orders their accesses as one
//! texture's.
use super::super::address::RangeToken;
use super::super::buffer_texture::TexelsHome;
use super::super::error::{nil, Error, Problem};
use super::super::image_plan::plan_image;
use super::super::resource_ids::IdPlacement;
use super::super::texture_descriptor::TextureDescriptor;
use super::super::values::StorageMode;
use super::super::view_plan::ViewRecord;
use super::super::Device;
use super::{image_error, IdUse, Initial, Owner, Storage, Stored, Texture};
use metal2vulkan::metal::TextureType;
use std::sync::Arc;
/// An in-process handle of a shareable texture (`MTLSharedTextureHandle`): opened with
/// [`Device::new_shared_texture_with_handle`], it gives a texture over the same image. Cheap to
/// clone (a retain); `Send + Sync`.
#[derive(Clone)]
pub struct SharedTextureHandle(Texture);
impl std::fmt::Debug for SharedTextureHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SharedTextureHandle")
.field("texture", &self.0)
.finish()
}
}
impl SharedTextureHandle {
/// `device`: the device the texture was made by, the only one that opens the handle: an owned
/// handle of it, the caller's ([`Texture::device`]).
pub fn device(&self) -> Device {
self.0.device()
}
}
impl Device {
/// `newSharedTextureWithDescriptor:`: a texture of `descriptor` that has handles
/// ([`Texture::new_shared_texture_handle`]).
///
/// A texture buffer is made over a buffer of its own, whose handles open texture buffers over
/// the same texels.
///
/// Refused: as [`Device::new_texture`] refuses the descriptor; storage other than `Private`
/// (`Nil`, `StorageMode`), which plain Metal answers nil and its validation stops, before every
/// rule of the descriptor; a format of which Metal makes no texture alone (`Nil`) before the
/// multisample sample count's rule; a swizzle with writes under `write_swizzle_enabled` too
/// (`Invalid`, `SwizzleWithWrites`), which Metal's validation stops here alone.
pub fn new_shared_texture(&self, descriptor: &TextureDescriptor) -> Result<Texture, Error> {
self.at_serving(|device| device.new_shared_texture_now(descriptor))
}
/// [`Device::new_shared_texture`] on the generation that serves (`device/line.rs`).
pub(crate) fn new_shared_texture_now(
&self,
descriptor: &TextureDescriptor,
) -> Result<Texture, Error> {
const CALL: &str = "new_shared_texture";
// Plain Metal makes a shareable texture without `framebufferOnly`, whatever the
// descriptor says, so a rotated one asserts as one without it does.
let descriptor = &TextureDescriptor {
framebuffer_only: false,
..self.texture_options(CALL, descriptor)?
};
self.check_texture_placement(
CALL,
descriptor.placement,
descriptor.usage,
descriptor.texture_type,
)?;
let options = descriptor.resource_options;
let storage = options.storage_mode()?;
let hazard = options.hazard_tracking_mode()?;
options.cpu_cache_mode()?;
// Plain Metal answers nil for storage other than `Private` before any rule of the
// descriptor it stops (a width of 0, a side past the largest, too many levels, a swizzle
// with writes, one sample in a multisample type: nil in `Shared`, `Managed` and
// `Memoryless` storage, a stop in `Private`; an M3 Max under macOS 26.7 and an M4 under
// 27.0.1).
if storage != StorageMode::Private {
return Err(nil(CALL, Problem::StorageMode));
}
let layout = descriptor
.check_shared(self.properties().texture_rules())
.map_err(|refused| refused.error(CALL))?;
if self.is_lost() {
return Err(self.lost_refusal());
}
let id_use = IdUse::Program(descriptor.placement);
if layout.texture_type == TextureType::TextureBuffer {
let texture =
self.new_texture_buffer(descriptor, &layout, id_use, TexelsHome::Shared)?;
self.take_device_sampler();
return Ok(texture);
}
let facts = self
.shared()
.textures
.facts(self.compile_device().profile());
let plan = plan_image(&layout, descriptor.image_usage(), false, &facts)
.map_err(|refusal| image_error(CALL, refusal))?;
let mut image =
self.new_texture_image(plan, &layout, descriptor, (hazard, Initial::AsMetal))?;
if let Owner::Runtime { shareable, .. } = &mut image.owner {
*shareable = true;
}
let image = self.with_decoded_twin(image, &layout, descriptor, true)?;
let record = ViewRecord::of_descriptor(&layout, descriptor);
let image = image.shared();
// Its last step that may fail, before the texture takes its ID (`resource_ids.rs`).
image.start_decoded_twin()?;
let texture = Texture::made(self, Storage::Image(image), record, None, id_use);
// A shared texture takes the device's sampler ID as any texture of the device's does
// (`resource_ids.rs`); one opened from a handle needs none, the handle's texture having
// taken it on this device.
self.take_device_sampler();
Ok(texture)
}
/// [`Device::new_shared_texture`] with `token` (a runtime extension), dropped once the
/// runtime frees the texture's image ([`RangeToken`]): after the texture, every view of it,
/// every texture opened from its handles, and the command buffers that hold any of them are
/// gone; a texture buffer's once its texels' buffer is free. A refused call's drops before it
/// returns.
pub fn new_shared_texture_with_range_token(
&self,
descriptor: &TextureDescriptor,
token: RangeToken,
) -> Result<Texture, Error> {
let texture = self.at_serving(|device| device.new_shared_texture_now(descriptor))?;
texture.keep_range_token(token)?;
Ok(texture)
}
/// `newSharedTextureWithHandle:`: a new texture over the image (or a texture buffer's texels)
/// of `handle`'s texture, with its properties, its label at the open, and no parent,
/// shareable as it is, sharing its purgeable state; its resource ID at `placement`
/// (`resource_ids.rs`), as `newSharedTextureWithHandle:withResourceIndex:` (Apple's private
/// call, a runtime extension) places it.
///
/// Refused: a handle of another device's texture (`Invalid`, `ForeignObject`): Metal's handle
/// "can only be used with this device" (`MTLTexture.h`); a placement as
/// [`Device::new_texture`] refuses one.
pub fn new_shared_texture_with_handle(
&self,
handle: &SharedTextureHandle,
placement: IdPlacement,
) -> Result<Texture, Error> {
self.at_serving(|device| device.new_shared_texture_with_handle_now(handle, placement))
}
/// [`Device::new_shared_texture_with_handle`] on the generation that serves (`device/line.rs`).
pub(crate) fn new_shared_texture_with_handle_now(
&self,
handle: &SharedTextureHandle,
placement: IdPlacement,
) -> Result<Texture, Error> {
const CALL: &str = "new_shared_texture_with_handle";
let texture = &handle.0;
if !texture.generation().same(self) {
return Err(texture.generation().foreign(CALL, self));
}
let record = &texture.0.record;
self.check_texture_placement(CALL, placement, record.usage, record.texture_type)?;
let storage = match texture.storage()? {
Storage::Image(image) => Storage::Image(Arc::clone(image)),
Storage::Texels(texels) => Storage::Texels(Arc::clone(texels)),
Storage::Alias { .. } | Storage::Shadow { .. } => {
unreachable!("only a texture with an image or texels of its own has a handle")
}
};
let id_use = IdUse::Program(placement);
let opened = Texture::made(self, storage, texture.0.record, None, id_use);
// Metal's opened texture starts with the texture's label, and the two change apart.
if let Some(label) = texture.label() {
opened.set_label(&label);
}
Ok(opened)
}
}
impl Texture {
/// `isShareable`: a texture of [`Device::new_shared_texture`], or one opened from a handle;
/// never a view.
pub fn is_shareable(&self) -> bool {
let _held = self.hold();
self.0.parent.is_none()
&& match self.stored() {
Stored::Made(Storage::Image(image)) => {
matches!(
image.owner,
Owner::Runtime {
shareable: true,
..
}
)
}
Stored::Made(Storage::Texels(texels)) => texels.home == TexelsHome::Shared,
Stored::Made(Storage::Alias { .. } | Storage::Shadow { .. })
| Stored::Unmade(_) => false,
}
}
/// `newSharedTextureHandle`: an in-process handle of this texture.
///
/// Refused for a texture that is not shareable (`Nil`, `NotShareable`), for which Metal
/// answers nil.
pub fn new_shared_texture_handle(&self) -> Result<SharedTextureHandle, Error> {
if !self.is_shareable() {
return Err(nil("new_shared_texture_handle", Problem::NotShareable));
}
Ok(SharedTextureHandle(self.clone()))
}
}