From 72562e193a81a3e20bfc689210f4e487bf265bb0 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Fri, 13 Nov 2026 00:34:30 -0800 Subject: [PATCH] The pre-import placement's docs, and its test compares refusals too What: Buffer::texture_placement, TexturePlacement::InPlace, InPlaceRefusal and route.rs say what the query answers before it makes images and what host_pages_texture_placement answers before the buffer; docs/API.md, docs/HOWTO.md and the runtime README name the new query. The test compares both calls' results, refusals included (a plane past the buffer's length is refused as PastBuffer by both, not answered PastLength), and asks rows of 8 bytes too. Why: lavapipe m3-max-24732: every shape both queries decide answered alike (CheckFailed by pointer, Unchecked over dma-bufs, InPlace elsewhere), but the past-length shape is a refusal of both calls, which the test unwrapped. Verified: pending the lavapipe run. --- crates/metal2vulkan-runtime/README.md | 19 +++++- .../src/buffer_texture.rs | 60 +++++++++++-------- .../src/tests/host_pages_placement.rs | 45 +++++++------- .../metal2vulkan-runtime/src/texture/route.rs | 9 ++- .../src/texture_placement.rs | 14 ++++- docs/API.md | 16 ++++- docs/HOWTO.md | 5 +- 7 files changed, 110 insertions(+), 58 deletions(-) diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index df0ffdacd..35655892f 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -686,12 +686,25 @@ Calls Metal does not have, for a consumer that emulates a Metal device: `new_texture_with_iosurface_plane` makes of the same arguments lies if made now: in place over host pages, a shadow pending the device's in-place check, a shadow and why, no host pages, or another shape. The making's own route answers it, after every check the making runs before it - makes anything, so the same arguments are refused with the same errors; an answer in place runs - the image's making up to its bind (one or two unbound `VkImage`s, released), so a pitch or an - image the device refuses answers the shadow the texture then is. + makes anything, so the same arguments are refused with the same errors. An answer in place asks + the device's format, feature and limit queries first, which make no image; where they refuse + every plan the making would try, that refusal is the answer. Otherwise the query runs the image's + making up to its bind (one or two unbound `VkImage`s, released), so a pitch or an image the device + refuses answers the shadow the texture then is. `Device::settle_in_place_checks` starts the check and says when its verdicts are known (a zero timeout waits for nothing). `Buffer::tiled_texture_placement` answers it for `new_tiled_texture`'s arguments. +- `Device::host_pages_texture_placement`: the same answer for a texture + `new_texture_with_iosurface_plane` would make over a no-copy buffer of host pages (`start`, + `length`) imported by pointer or as dma-bufs (`HostBacking::ImportedByPointer`, + `ImportedAsDmaBuf`), asked before the buffer exists. It makes no buffer, import or image. Where + the steps before an import or an image decide the answer (the texture's shape and refusals, the + policy, the texel width, the check's verdicts for texels laid out from `start`, texels past + `length`, rows that share bytes, and the refusals of the device's format, feature and limit + queries), it answers what `texture_placement_with_iosurface_plane` answers once the buffer is + made. Where only an image or the import decides (the driver's linear image's layout, a + `DRM_FORMAT_MOD_LINEAR` plane, their requirements against the import, the dma-buf's source and + import), it answers `InPlace`. No Metal counterpart. - `HostRam`, `Device::use_host_ram` (with `HostRamUse`) and `HostMemory::with_runs` (with `RunResolver`): a guest's RAM imported whole, in 64 MiB chunks, for a device that refuses views of guest pages made through dma-bufs; a no-copy object whose pages the consumer resolves to one diff --git a/crates/metal2vulkan-runtime/src/buffer_texture.rs b/crates/metal2vulkan-runtime/src/buffer_texture.rs index 24ff3e11e..1fc35b261 100644 --- a/crates/metal2vulkan-runtime/src/buffer_texture.rs +++ b/crates/metal2vulkan-runtime/src/buffer_texture.rs @@ -774,12 +774,17 @@ impl Buffer { /// 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 + /// Where the route is the image in place, the query asks first what the device answers + /// without an image (rows that share bytes, and its format, feature and limit queries for each + /// plan the making would try), and where those refuse every plan answers that refusal, making + /// no image; else it makes the image 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. Before a no-copy buffer is made, + /// [`Device::host_pages_texture_placement`] answers by the same steps up to the import. 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 @@ -1064,28 +1069,35 @@ impl Device { /// The answer is [`Buffer::texture_placement_with_iosurface_plane`]'s over such a buffer /// wherever the steps before an import or an image decide it, by the same functions: the /// texture's shape and the call's refusals (the same errors), [`TexturePlacement::OtherShape`], - /// the device's policy ([`TextureShadowReason::Policy`]), a shader's writes of a texel width - /// the device's check stores none of ([`TextureShadowReason::StoreWidth`]), the check's - /// verdicts for the pages' kind, the texture's accesses and how its texels lie in the device's - /// lines of memory from `start` ([`TexturePlacement::Pending`], - /// [`TextureShadowReason::CheckFailed`], [`TextureShadowReason::Unchecked`]), texels past - /// `length` ([`TextureShadowReason::PastLength`]), rows that share bytes - /// ([`InPlaceRefusal::Rows`]), and the refusals the device's format, feature and limit queries - /// give before it makes an image ([`InPlaceRefusal::Image`], [`InPlaceRefusal::Pitch`], and - /// over dma-bufs [`InPlaceRefusal::DmaBuf`]). Where only an image or the import decides (the - /// driver's linear image's layout, a `DRM_FORMAT_MOD_LINEAR` image's plane, their - /// requirements against the import's offset, size and memory type, the source's dma-buf and - /// the driver's import of it), it answers [`TexturePlacement::InPlace`]: the texture made then - /// may be a shadow for one of those, as the query of the buffer once made answers. It makes - /// no buffer, import or image, and binds or submits nothing; 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). The buffer's resource options are not asked: a texture over an - /// IOSurface plane takes its descriptor's. + /// the device's policy ([`Policy`]), a shader's writes of a texel width the device's check + /// stores none of ([`StoreWidth`]), the check's verdicts for the pages' kind, the texture's + /// accesses and how its texels lie in the device's lines of memory from `start` + /// ([`TexturePlacement::Pending`], [`CheckFailed`], [`Unchecked`]), texels past `length` + /// ([`PastLength`]), rows that share bytes ([`Rows`]), and the refusals the device's format, + /// feature and limit queries give before it makes an image ([`Image`], [`Pitch`], and over + /// dma-bufs [`DmaBuf`]). Where only an image or the import decides (the driver's linear + /// image's layout, a `DRM_FORMAT_MOD_LINEAR` image's plane, their requirements against the + /// import's offset, size and memory type, the source's dma-buf and the driver's import of + /// it), it answers [`TexturePlacement::InPlace`]: the texture made then may be a shadow for + /// one of those, as the query of the buffer once made answers. It makes no buffer, import or + /// image, and binds or submits nothing; a pending verdict starts the device's check, as the + /// making's does ([`Device::settle_in_place_checks`] says when its verdicts are known). The + /// buffer's resource options are not asked: a texture over an IOSurface plane takes its + /// descriptor's. /// /// Refused as `new_texture_with_iosurface_plane` refuses its arguments, as /// `new_buffer_with_bytes_no_copy` refuses `length` (`Nil`, `BufferLength`), and (`Invalid`, /// `HostPagesBacking`) for another `backing`. + /// + /// [`Policy`]: crate::TextureShadowReason::Policy + /// [`StoreWidth`]: crate::TextureShadowReason::StoreWidth + /// [`CheckFailed`]: crate::TextureShadowReason::CheckFailed + /// [`Unchecked`]: crate::TextureShadowReason::Unchecked + /// [`PastLength`]: crate::TextureShadowReason::PastLength + /// [`Rows`]: crate::InPlaceRefusal::Rows + /// [`Image`]: crate::InPlaceRefusal::Image + /// [`Pitch`]: crate::InPlaceRefusal::Pitch + /// [`DmaBuf`]: crate::InPlaceRefusal::DmaBuf pub fn host_pages_texture_placement( &self, backing: HostBacking, diff --git a/crates/metal2vulkan-runtime/src/tests/host_pages_placement.rs b/crates/metal2vulkan-runtime/src/tests/host_pages_placement.rs index 47105a0d3..a77eb285f 100644 --- a/crates/metal2vulkan-runtime/src/tests/host_pages_placement.rs +++ b/crates/metal2vulkan-runtime/src/tests/host_pages_placement.rs @@ -6,8 +6,9 @@ //! `Placement`, `Source`, `DmaBuf`). //! //! Over a spread of shapes: texels of 1, 4 and 16 bytes; rows at their own width and padded past -//! it; at the pages' first byte and past a page; `ShaderRead` and `Unknown` usage; texels past the -//! buffer's length. By pointer, on the device the cfg selects once its check settled, and under +//! it; rows of 8 bytes (a plane under 16 bytes a row, `linear_plan.rs`); at the pages' first byte +//! and past a page; `ShaderRead` and `Unknown` usage; and a plane past the buffer's length, +//! refused alike. By pointer, on the device the cfg selects once its check settled, and under //! `HostPageTexturePolicy::Shadow`; as dma-bufs on Linux, on a device made without //! `VK_EXT_external_memory_host` (`tests/no_pointer_import.rs`), where `/dev/udmabuf` opens. Each //! pre-import answer makes no `VkImage` on the asking thread @@ -18,7 +19,7 @@ use super::blit_holds::{device, over_host, shadow_device, Host, Scratch, BYTES}; use crate::{ - Buffer, Device, HostBacking, InPlaceRefusal, IoSurfacePlane, ResourceOptions, + Buffer, Device, Error, HostBacking, InPlaceRefusal, IoSurfacePlane, ResourceOptions, TextureDescriptor, TexturePlacement, TextureShadowReason, }; use metal2vulkan::metal::{PixelFormat, TextureUsage}; @@ -30,11 +31,13 @@ use std::time::Duration; const SETTLE: Duration = Duration::from_secs(120); /// A shape: its format, width and height, the plane's `bytesPerRow`, and its offset from the -/// pages' first byte (`None`: the last 4096 bytes of `length`, so its texels run past it). +/// pages' first byte (`None`: the last 4096 bytes of `length`, so its texels run past it, which +/// both calls refuse alike). type Shape = (PixelFormat, u64, u64, u64, Option); /// The shapes asked, each with `ShaderRead` and `Unknown` usage. -const SHAPES: [Shape; 7] = [ +const SHAPES: [Shape; 8] = [ + (PixelFormat::R8Unorm, 8, 8, 8, Some(0)), (PixelFormat::R8Unorm, 64, 32, 64, Some(0)), (PixelFormat::R8Unorm, 60, 32, 64, Some(4096)), (PixelFormat::RGBA8Unorm, 64, 32, 256, Some(0)), @@ -62,14 +65,14 @@ fn import_decides(after: TexturePlacement) -> bool { /// Ask every shape of [`SHAPES`] of `device` before (`host_pages_texture_placement`, `backing`, /// from `start`) and after (`buffer`, of the same `length` bytes over the same pages) the buffer, -/// `label` naming the run: each pre-import answer made no image, and equals the buffer's where it -/// is not `InPlace`. The pre-import answers. +/// `label` naming the run: each pre-import answer made no image, and equals the buffer's (a +/// refusal included) where it is not `InPlace`. The pre-import answers. fn ask_each( device: &Device, label: &str, (backing, buffer): (HostBacking, &Buffer), (start, length): (u64, u64), -) -> Vec { +) -> Vec> { let mut answers = Vec::new(); for (format, width, height, bytes_per_row, offset) in SHAPES { let offset = offset.unwrap_or(length - 4096); @@ -81,23 +84,21 @@ fn ask_each( let made = || device.work_counts().object_churn.vk_images_made_this_thread; let images = made(); let before = - (device.host_pages_texture_placement(backing, &descriptor, plane, start, length)) - .unwrap_or_else(|error| panic!("{label}: refused before the buffer: {error}")); + device.host_pages_texture_placement(backing, &descriptor, plane, start, length); let images = made() - images; - let after = (buffer.texture_placement_with_iosurface_plane(&descriptor, plane)) - .unwrap_or_else(|error| panic!("{label}: refused after the buffer: {error}")); + let after = buffer.texture_placement_with_iosurface_plane(&descriptor, plane); println!( "HOST-PAGES-PLACEMENT {} {label}: {format:?} {width}x{height} at {offset}, {bytes_per_row} \ a row, {usage:?}: before the buffer {before:?}, after {after:?}", device.name() ); assert_eq!(images, 0, "{label}: the pre-import answer made an image"); - match before { - TexturePlacement::InPlace => assert!( - import_decides(after), + match (&before, &after) { + (Ok(TexturePlacement::InPlace), Ok(after)) => assert!( + import_decides(*after), "{label}: {format:?} {width}x{height} at {offset}: InPlace before, {after:?} after" ), - before => assert_eq!( + _ => assert_eq!( before, after, "{label}: {format:?} {width}x{height} at {offset}, {usage:?}" ), @@ -144,13 +145,12 @@ fn before_the_buffer_the_placement_is_the_buffer_s_where_no_import_decides() { }; let pages = (HostBacking::ImportedByPointer, &buffer); let answers = ask_each(&device, label, pages, (start, length)); - let past = TexturePlacement::Shadow(TextureShadowReason::PastLength); assert!( - answers.contains(&past), - "{label}: no shape ran past the length" + answers.iter().any(Result::is_err), + "{label}: no shape was refused" ); if shadow { - let policy = TexturePlacement::Shadow(TextureShadowReason::Policy); + let policy = Ok(TexturePlacement::Shadow(TextureShadowReason::Policy)); assert!( answers.contains(&policy), "{label}: the policy answered nothing" @@ -210,10 +210,9 @@ fn before_the_buffer_the_placement_over_dma_bufs_is_the_buffer_s_where_no_import } let dma_buf = (HostBacking::ImportedAsDmaBuf, &buffer); let answers = ask_each(&device, "as dma-bufs", dma_buf, (start, length)); - let past = TexturePlacement::Shadow(TextureShadowReason::PastLength); assert!( - answers.contains(&past), - "as dma-bufs: no shape ran past the length" + answers.iter().any(Result::is_err), + "as dma-bufs: no shape was refused" ); drop(buffer); } diff --git a/crates/metal2vulkan-runtime/src/texture/route.rs b/crates/metal2vulkan-runtime/src/texture/route.rs index 6aed4e990..1212535a1 100644 --- a/crates/metal2vulkan-runtime/src/texture/route.rs +++ b/crates/metal2vulkan-runtime/src/texture/route.rs @@ -5,9 +5,12 @@ //! levels past the first, texels past the buffer's length, a buffer of no host pages, a device //! copy, bytes detached, and over host pages (or a copied host buffer's copy of them) the in-place //! decision (`in_place::in_place`) against the device's check's verdicts. Where that takes the image in -//! place, the query also runs the image's making up to the bind (`texture/in_place_image.rs`), so -//! a pitch, an image, a placement or rows the device refuses answer the shadow the texture then -//! is. +//! place, the query asks what the device answers before it makes an image +//! (`Device::in_place_unmade`), then runs the image's making up to the bind +//! (`texture/in_place_image.rs`), so a pitch, an image, a placement or rows the device refuses +//! answer the shadow the texture then is. Over host pages not imported yet +//! (`Device::host_pages_texture_placement`) the same functions answer up to the import: +//! [`Device::host_route`] for the route, and that image-free answer alone. use super::image_error; use crate::buffer::CallerMemory; diff --git a/crates/metal2vulkan-runtime/src/texture_placement.rs b/crates/metal2vulkan-runtime/src/texture_placement.rs index cf5742283..56410fe4d 100644 --- a/crates/metal2vulkan-runtime/src/texture_placement.rs +++ b/crates/metal2vulkan-runtime/src/texture_placement.rs @@ -27,7 +27,13 @@ pub enum TexturePlacement { /// the texture is then a shadow, its notice (`shadowed-texture`) naming why. Over a copied host /// buffer ([`HostBacking::Copied`](crate::HostBacking::Copied)), the image lies over the copy /// the GPU uses in the pages' stead, in the copy's memory, where the check passed for the - /// pages' kind and that memory's (`WorkCounts::copied_textures_in_place`). + /// pages' kind and that memory's (`WorkCounts::copied_textures_in_place`). As + /// [`Device::host_pages_texture_placement`](crate::Device::host_pages_texture_placement) + /// answers it, before any buffer of the pages: every step before an import or an image took + /// the image in place, and only an image or the import decides the rest (the driver's linear + /// image's layout, a `DRM_FORMAT_MOD_LINEAR` plane, their requirements against the import, the + /// source's dma-buf and the driver's import of it), as the buffer's query answers once it is + /// made. InPlace, /// A shadow now, as the device's check has not answered for the pages' kind and the /// texture's accesses: once it does, at the commit of the device's only uncommitted command @@ -177,7 +183,11 @@ pub enum InPlaceRowEnds { /// them. [`Buffer::texture_placement`](crate::Buffer::texture_placement) answers each one the /// image's making decides before it binds memory, by the same steps: all but `Source`, and /// `DmaBuf` where the driver refuses the dma-buf's import, which only the bind finds over pages -/// imported as dma-bufs ([`TexturePlacement::InPlace`]). A runtime extension: Metal has none. +/// imported as dma-bufs ([`TexturePlacement::InPlace`]). +/// [`Device::host_pages_texture_placement`](crate::Device::host_pages_texture_placement), before +/// any buffer of the pages, answers `Rows`, and `Image`, `Pitch` and `DmaBuf` where the device's +/// format, feature and limit queries decide them without an image. A runtime extension: Metal has +/// none. #[non_exhaustive] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum InPlaceRefusal { diff --git a/docs/API.md b/docs/API.md index d4d454dc8..acd100a63 100644 --- a/docs/API.md +++ b/docs/API.md @@ -478,8 +478,12 @@ else a `DRM_FORMAT_MOD_LINEAR` image at Metal's pitch where the device's check p memory; a texture over that memory that takes neither answers its shadow's cause); or a shape no image in place takes; `Buffer::tiled_texture_placement` answers it for `new_tiled_texture`'s. The making's own route answers it (`texture/route.rs`), after every check the making runs before it -makes anything, so the same arguments are refused with the same errors; where the route is the image in -place, the query runs the image's making up to the bind (`texture/in_place_image.rs`): it makes the +makes anything, so the same arguments are refused with the same errors. Where the route is the image +in place, the query asks first what the device answers without an image: rows that share bytes, and +the format, feature and limit queries the image steps run before `vkCreateImage`, for every plan the +making would try. Where those refuse every plan, that refusal is the answer and no image is +made. Otherwise the query runs the image's making up to the bind +(`texture/in_place_image.rs`): it makes the driver's linear image, and the `DRM_FORMAT_MOD_LINEAR` one where the driver lays the first out at another pitch, checks their requirements against the bytes' place and memory type, and releases them unbound, so a pitch, an image, a placement or rows the device refuses answer @@ -492,6 +496,14 @@ verdict starts the check, as a texture's making does. `Device::settle_in_place_c check and says when the verdicts are known (a zero timeout waits for nothing). They become known once per `VkDevice`: a GPU recovery's new one reads them from the record as it is made, or checks again where the lost one's check left none. +`Device::host_pages_texture_placement` answers the same question for a texture +`new_texture_with_iosurface_plane` would make over a no-copy buffer of host pages not made yet +(`start` and `length`, imported by pointer or as dma-bufs), by the same steps up to the import: +where those decide (the shape and the call's refusals, the policy, the texel width, the verdicts +for texels laid out from `start`, texels past `length`, rows that share bytes, and the device's +format, feature and limit queries), its answer is the one `texture_placement_with_iosurface_plane` +gives once the buffer is made; where only an image or the import decides, `InPlace`. It makes no +buffer, import or image. `Buffer::new_tiled_texture` (Metal's private `newTiledTextureWithDescriptor:offset:bytesPerRow:`) makes what `new_texture` makes, and a 2D texture of more than one level, which is a copy of the buffer's rows at the offset and pitch, taken at its first use, as on an x86 Mac (an AMD Radeon Pro diff --git a/docs/HOWTO.md b/docs/HOWTO.md index d4be5ec6e..cbe814d0e 100644 --- a/docs/HOWTO.md +++ b/docs/HOWTO.md @@ -386,7 +386,10 @@ verdicts are known. A program that routes each surface either to a texture over copy of its own asks `Buffer::texture_placement` (or `texture_placement_with_iosurface_plane`) with the arguments it would make the texture with: the answer (`TexturePlacement`: in place, pending, a shadow and why, no host pages, another shape) is the making's own decision, so no report -string needs parsing. A texture a shader writes of texels of a width the check stores none of +string needs parsing. Before it makes the no-copy buffer, `Device::host_pages_texture_placement` +answers for a surface's plane over the pages, by the same steps up to the import: `InPlace` there +means nothing before the import refuses the image, which the buffer's own query, once it is made, +answers in full. A texture a shader writes of texels of a width the check stores none of (`host_texture_shadows_store_width`), one the check failed or could not cover, and one no image in place serves (a pitch the device's linear images do not take -- `host_texture_shadows_refused_pitch`; over dma-buf pages, a source that makes no one dma-buf of