diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index ef13fdbb6..07769d370 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -506,7 +506,9 @@ 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. + 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. `Device::settle_in_place_checks` starts the check and says when its verdicts are known (a zero timeout waits for nothing). - `HostRam`, `Device::use_host_ram` (with `HostRamUse`) and `HostMemory::with_runs` (with diff --git a/crates/metal2vulkan-runtime/src/buffer_texture.rs b/crates/metal2vulkan-runtime/src/buffer_texture.rs index 536ba4d62..38b01ef21 100644 --- a/crates/metal2vulkan-runtime/src/buffer_texture.rs +++ b/crates/metal2vulkan-runtime/src/buffer_texture.rs @@ -655,19 +655,25 @@ impl Buffer { /// 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 function's (the device's policy, the + /// 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, and 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), taken after - /// every check the making runs before it makes anything: an argument `new_texture` refuses - /// is refused here alike, with the same error. Only what making the texture shows is not - /// known here: a `TexturePlacement::InPlace` texture whose image the device then refuses is - /// a shadow, and a making that runs out of memory or meets a lost device fails. 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 makes and counts nothing, and announces nothing. + /// 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 may 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, diff --git a/crates/metal2vulkan-runtime/src/dma_buf.rs b/crates/metal2vulkan-runtime/src/dma_buf.rs index 5fd6efd4c..f833505e8 100644 --- a/crates/metal2vulkan-runtime/src/dma_buf.rs +++ b/crates/metal2vulkan-runtime/src/dma_buf.rs @@ -858,12 +858,20 @@ pub(super) enum DmaBufRefusal { /// An image over dma-buf pages made and not yet bound: its requirements, and the request for /// its pages. -struct Unbound { +pub(super) struct Unbound { raw: vk::Image, requirements: vk::MemoryRequirements, request: DmaBufRequest, } +impl Unbound { + /// Release the image, which no command uses and no memory is bound to, unbound. + pub(super) fn release(self, device: &Device) { + // SAFETY: the image is live and unbound, and no command uses it. + unsafe { release::object(device.shared().vk(), self.raw) }; + } +} + impl Device { /// The image of `plan` (a heap texture's, viewed as `facts` says) over `texels`, where the /// texels lie in the caller's dma-buf pages: a `DRM_FORMAT_MOD_LINEAR` image @@ -920,7 +928,7 @@ impl Device { /// [`Device::dma_buf_image`]'s image made and checked, unbound, with the request for its /// pages; why not where the device makes none before the source is asked. - fn dma_buf_image_unbound( + pub(super) fn dma_buf_image_unbound( &self, plan: &ImagePlan, facts: &ImageFacts<'_>, @@ -1034,7 +1042,7 @@ impl Device { /// `unbound`'s image bound to a dedicated import of the dma-buf the caller's source makes of /// its pages, of `texels`' memory type, and counted; why not, the image released, where the /// source fails or answers against its contract or the driver refuses the import or binding. - fn bind_dma_buf_image( + pub(super) fn bind_dma_buf_image( &self, unbound: Unbound, texels: &DmaBufTexels<'_>, diff --git a/crates/metal2vulkan-runtime/src/tests/texture_placement_device.rs b/crates/metal2vulkan-runtime/src/tests/texture_placement_device.rs index 788586d5c..3de65050e 100644 --- a/crates/metal2vulkan-runtime/src/tests/texture_placement_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/texture_placement_device.rs @@ -18,6 +18,15 @@ //! buffer's bytes are not reached here. //! - **Refusals** are the making's own: a pitch below the row, texels past the allocation, and an //! IOSurface plane's client direction give the same error to both. +//! - **Pitches the device lays no image at.** Once the check settled, `BGRA8Unorm` textures over a +//! no-copy buffer at pitches past their rows (a 61-texel row of 244 bytes at 256, 272, 320, 512 +//! and 1024 bytes, `ShaderRead`; the consumer's 3664-texel surface of 14656 bytes a row at 14656, +//! 14672 and 14848, `ShaderRead | RenderTarget`) each answer what the making takes, the image's +//! making up to its bind included: where the device lays no image at the pitch (its linear image +//! laid out at another, and no `DRM_FORMAT_MOD_LINEAR` image at it), `Shadow(Refused(Pitch))`, +//! counted in `host_texture_shadows_refused_pitch`. At least one is: 272 bytes, 16 past a +//! 256-byte row, is a pitch no driver measured lays rows at (llvmpipe's linear images step by +//! 64 bytes, MoltenVK lays them at their own pitch alone, RADV's modifier images step by 256). //! //! A device test: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`, //! `m2v_cpu_icd`), on the kind of device the cfg selects. Printed (`TEXTURE-PLACEMENT`): each @@ -505,3 +514,52 @@ fn the_answer_is_the_route_the_texture_takes() { assert_eq!(answer, TexturePlacement::Shadow(due), "{case}"); } } + +#[test] +fn a_pitch_the_device_lays_no_image_at_answers_its_shadow() { + // The pages, declared before the device that uses them, so dropped after it. + let host = Host::new(); + let scratch = Scratch::new("texture-placement-pitch"); + let device = device(&scratch.0); + let runs = Arc::new(AtomicUsize::new(0)); + let Some(buffer) = over_host(&device, &host, &runs) else { + return; + }; + assert!(device.settle_in_place_checks(SETTLE), "the check settled"); + let options = buffer.resource_options(); + let reads = TextureUsage::SHADER_READ; + let renders = TextureUsage::SHADER_READ | TextureUsage::RENDER_TARGET; + let cases = [ + ((61, 16), reads, 256), + ((61, 16), reads, 272), + ((61, 16), reads, 320), + ((61, 16), reads, 512), + ((61, 16), reads, 1024), + ((3664, 16), renders, 14656), + ((3664, 16), renders, 14672), + ((3664, 16), renders, 14848), + ]; + let mut refused = 0; + for (at, &(size, usage, bytes_per_row)) in cases.iter().enumerate() { + let shaped = descriptor(PixelFormat::BGRA8Unorm, size, usage, options); + let offset = at as u64 * (256 << 10); + let ask = || buffer.texture_placement(&shaped, offset, bytes_per_row); + let make = || buffer.new_texture(&shaped, offset, bytes_per_row); + let case = format!("{}x{} at {bytes_per_row} bytes a row", size.0, size.1); + let (answer, texture) = made_as_asked(&device, &case, ask, make); + let pitch = TextureShadowReason::Refused(InPlaceRefusal::Pitch); + if answer == TexturePlacement::Shadow(pitch) { + assert_eq!(path(&texture), "shadow", "{case}"); + refused += 1; + } + } + eprintln!( + "TEXTURE-PLACEMENT on {}: {refused} of {} pitches refused", + device.name(), + cases.len() + ); + assert!( + refused > 0, + "no pitch was refused: the test reached no refusal" + ); +} diff --git a/crates/metal2vulkan-runtime/src/texture/in_place_image.rs b/crates/metal2vulkan-runtime/src/texture/in_place_image.rs index 5de95a069..e99fbea6f 100644 --- a/crates/metal2vulkan-runtime/src/texture/in_place_image.rs +++ b/crates/metal2vulkan-runtime/src/texture/in_place_image.rs @@ -72,6 +72,59 @@ pub(super) type InPlace = Result<(Arc, bool), (Refusal, String)>; /// An unbound image in place, or why none is. type Unbound = Result<(LinearMade, bool), (Refusal, String)>; +/// An image in place made and checked before any memory is bound to it +/// ([`Device::unplaced`]), which [`Device::in_place_image`] binds and +/// [`Device::in_place_image_made`] releases. +enum Unplaced { + /// Over pages imported by their pointer: the image made for host allocations, whether it is + /// the `DRM_FORMAT_MOD_LINEAR` one, and the buffer's memory and the offset in it where its + /// requirements were checked to take it (`placed`). + Host { + made: LinearMade, + pitched: bool, + memory: vk::DeviceMemory, + at: u64, + }, + /// Over pages imported as dma-bufs: the image over them, checked against their pages, before + /// the caller's source is asked; with the pages, their imports and memory type, and the host + /// address of the texels' first byte. + #[cfg(unix)] + DmaBuf { + unbound: crate::dma_buf::Unbound, + pages: Arc, + imports: Arc, + type_index: u32, + at: u64, + }, +} + +/// Why no image in place holds texels laid out as `layout`, where their rows share bytes (an +/// IOSurface plane's GPU rows): no image holds a texel twice. +fn shared_rows(layout: &BufferLayout) -> Option<(Refusal, String)> { + let shared_bytes = layout.shared_bytes(); + (shared_bytes != 0).then(|| { + ( + Refusal::Rows, + format!( + "its rows lie {} bytes apart, {} bytes of texels each, so consecutive rows share \ + {shared_bytes} bytes: no image holds a texel twice", + layout.row_pitch, layout.row_bytes + ), + ) + }) +} + +/// The refusal of an image in place over dma-buf pages of `kind` (`dma_buf.rs`). +#[cfg(unix)] +fn dma_buf_refusal(kind: crate::dma_buf::DmaBufRefusal) -> Refusal { + use crate::dma_buf::DmaBufRefusal as D; + match kind { + D::Pitch => Refusal::Pitch, + D::Source => Refusal::Source, + D::Image | D::Pages | D::Import => Refusal::DmaBuf, + } +} + impl Texture { /// Make the first move of the texture's image in place now, by an internal submission the /// call waits for (`Image::settle`): the device's check's round (`in_place_check.rs`). `None` @@ -246,9 +299,12 @@ impl Texture { impl Device { /// The image in place of a texture of `plan` (its declared usage's, with the feedback loop /// usage where the device makes it so, and the storage usage too where `storable`: the module - /// docs) made with `descriptor` over `texels`, host pages of `class`, with whether it is a `DRM_FORMAT_MOD_LINEAR` image at Metal's pitch - /// made for host allocations; why not where the device makes none, the declared plan's - /// refusal (the module docs). Over a no-copy buffer's pages (or a no-copy heap's) it is noted + /// docs) made with `descriptor` over `texels`, host pages of `class`, with whether it is a + /// `DRM_FORMAT_MOD_LINEAR` image at Metal's pitch made for host allocations; why not where the + /// device makes none, the declared plan's refusal (the module docs). Each plan is made and + /// checked unbound ([`unplaced`](Self::unplaced)), as + /// [`in_place_image_made`](Self::in_place_image_made) asks it, then bound + /// ([`place`](Self::place)). Over a no-copy buffer's pages (or a no-copy heap's) it is noted /// for a rebind to make again. Its first move is noted, for the device's next submission that /// takes it (`texture/first_move.rs`). pub(super) fn in_place_image( @@ -259,33 +315,15 @@ impl Device { facts: &ImageFacts<'_>, ) -> Result { let layout = texels.layout; - let shared_bytes = layout.shared_bytes(); - if shared_bytes != 0 { - return Ok(Err(( - Refusal::Rows, - format!( - "its rows lie {} bytes apart, {} bytes of texels each, so consecutive rows \ - share {shared_bytes} bytes: no image holds a texel twice", - layout.row_pitch, layout.row_bytes - ), - ))); + if let Some(refused) = shared_rows(&layout) { + return Ok(Err(refused)); } - let make = |plan: &ImagePlan| match class { - PagesClass::DmaBuf => self.dma_buf_in_place(plan, descriptor, texels, facts), - PagesClass::Pointer | PagesClass::Fresh => { - self.host_in_place(plan, descriptor, texels, class, facts) - } + let make = |plan: &ImagePlan| match self.unplaced(plan, texels, class, facts)? { + Ok(unplaced) => self.place(unplaced, plan, descriptor, texels, class), + Err(refused) => Ok(Err(refused)), }; - // The wider plans, widest first: the feedback loop usage, and with it the storage usage - // where `storable`. - let profile = self.compile_device().profile(); - let feedback = feedback_candidate(plan, self); - let storage = feedback - .as_ref() - .filter(|_| storable) - .and_then(|feedback| storage_candidate(feedback, profile)); let mut made = None; - for wider in storage.iter().chain(&feedback) { + for wider in &self.wider_in_place_plans(plan, storable) { if let Ok(image) = make(wider)? { made = Some(image); break; @@ -314,17 +352,78 @@ impl Device { Ok(Ok((image, pitched))) } - /// [`in_place_image`](Self::in_place_image) over pages imported by their pointer: the image - /// made for host allocations, checked against the texels' place in the buffer's memory, and - /// bound there. - fn host_in_place( + /// Whether [`in_place_image`](Self::in_place_image) makes an image in place of the same + /// arguments, its declared plan's refusal where it makes none: the same steps up to the bind, + /// each image it makes released unbound. The declared plan is asked first, so where it is made + /// in place the answer costs its one or two images (the driver's linear image, and the + /// `DRM_FORMAT_MOD_LINEAR` one where the driver lays the first out otherwise); the wider plans + /// are asked only where it is refused, as the making takes the first plan made of them all. + /// Over pages imported as dma-bufs it asks no source of the caller's: the source's answer and + /// the driver's import of its dma-buf are known only at the bind. + pub(crate) fn in_place_image_made( + &self, + plan: &ImagePlan, + (texels, class, storable): (&Texels, PagesClass, bool), + facts: &ImageFacts<'_>, + ) -> Result, Error> { + if let Some(refused) = shared_rows(&texels.layout) { + return Ok(Err(refused)); + } + let refused = match self.unplaced(plan, texels, class, facts)? { + Ok(unplaced) => { + self.release_unplaced(unplaced); + return Ok(Ok(())); + } + Err(refused) => refused, + }; + for wider in &self.wider_in_place_plans(plan, storable) { + if let Ok(unplaced) = self.unplaced(wider, texels, class, facts)? { + self.release_unplaced(unplaced); + return Ok(Ok(())); + } + } + Ok(Err(refused)) + } + + /// The plans wider than `plan` an image in place is tried with before it, widest first: the + /// feedback loop usage where the device makes it, and with it the storage usage where + /// `storable` (the module docs). + fn wider_in_place_plans(&self, plan: &ImagePlan, storable: bool) -> Vec { + let profile = self.compile_device().profile(); + let feedback = feedback_candidate(plan, self); + let storage = feedback + .as_ref() + .filter(|_| storable) + .and_then(|feedback| storage_candidate(feedback, profile)); + storage.into_iter().chain(feedback).collect() + } + + /// The image in place of `plan` over `texels`, host pages of `class`, made and checked before + /// any memory is bound to it; why not. Over pages imported by their pointer, the image made + /// for host allocations ([`in_place_unbound`](Self::in_place_unbound)) whose requirements + /// take its place in the buffer's memory ([`placed`]); over pages imported as dma-bufs, the + /// `DRM_FORMAT_MOD_LINEAR` image over them, checked against their pages, before the caller's + /// source is asked (`dma_buf.rs`). + fn unplaced( &self, plan: &ImagePlan, - descriptor: &TextureDescriptor, texels: &Texels, class: PagesClass, facts: &ImageFacts<'_>, - ) -> Result { + ) -> Result, Error> { + match class { + PagesClass::DmaBuf => self.dma_buf_unplaced(plan, texels, facts), + PagesClass::Pointer | PagesClass::Fresh => self.host_unplaced(plan, texels, facts), + } + } + + /// [`unplaced`](Self::unplaced) over pages imported by their pointer. + fn host_unplaced( + &self, + plan: &ImagePlan, + texels: &Texels, + facts: &ImageFacts<'_>, + ) -> Result, Error> { let layout = texels.layout; let (memory, buffer_offset, type_index) = texels.buffer.memory(); let at = buffer_offset + layout.offset; @@ -333,50 +432,27 @@ impl Device { Ok(made) => made, Err(refused) => return Ok(Err(refused)), }; - let raw = made.raw; - let raw_device = self.shared().vk(); if let Err(refused) = placed(&made.requirements, at, room, type_index) { // SAFETY: the image is live and unbound, and no command uses it. - unsafe { release::object(raw_device, raw) }; + unsafe { release::object(self.shared().vk(), made.raw) }; return Ok(Err(refused)); } - // SAFETY: the image is live and unbound, made for host allocations; the memory is the - // buffer's import of host pages (or its fresh pages'), which the image holds, in a type the - // image takes, at an offset that meets its alignment, its size inside the memory - // (`placed`). - if let Err(error) = unsafe { crate::creation::bind_image(raw_device, raw, memory, at) } { - // SAFETY: the image is live and unbound, and no command uses it. - unsafe { release::object(raw_device, raw) }; - return Err(vulkan(error)); - } - let image = self.in_place_of(Raw::new(raw), plan.clone(), descriptor, texels); - // Over a no-copy buffer's pages, its move notes them (a rebind may swap the image out of - // them); fresh pages are the buffer's for good. - let pages = match class { - PagesClass::Fresh => None, - _ => texels.buffer.host_pages(), - }; - let first = FirstMove::new( - raw, - image.plan().format, - made.initial, - pages.as_ref().and_then(pages_hold), - ); - *lock(&image.first_move) = Some(first); - Ok(Ok((image.shared(), pitched))) + Ok(Ok(Unplaced::Host { + made, + pitched, + memory, + at, + })) } - /// [`in_place_image`](Self::in_place_image) over pages imported as dma-bufs: the - /// `DRM_FORMAT_MOD_LINEAR` image at the texture's pitches over a dma-buf of its own pages, - /// which the pages' source makes (called with no lock of the runtime's held). + /// [`unplaced`](Self::unplaced) over pages imported as dma-bufs. #[cfg(unix)] - fn dma_buf_in_place( + fn dma_buf_unplaced( &self, plan: &ImagePlan, - descriptor: &TextureDescriptor, texels: &Texels, facts: &ImageFacts<'_>, - ) -> Result { + ) -> Result, Error> { let layout = texels.layout; let Some(pages) = texels.buffer.host_pages() else { return Ok(Err(( @@ -392,53 +468,141 @@ impl Device { }; let base = pages.memory().1; let (_, buffer_offset, _) = texels.buffer.memory(); + let at = start + (buffer_offset + layout.offset - base); let dma_texels = crate::dma_buf::DmaBufTexels { pages: &imports, type_index, - at: start + (buffer_offset + layout.offset - base), + at, row_pitch: layout.row_pitch, slice_pitch: layout.slice_pitch, }; - let made = match self.dma_buf_image_or_why(plan, facts, &dma_texels)? { - Ok(made) => made, - Err((kind, why)) => { - use crate::dma_buf::DmaBufRefusal as D; - let kind = match kind { - D::Pitch => Refusal::Pitch, - D::Source => Refusal::Source, - D::Image | D::Pages | D::Import => Refusal::DmaBuf, - }; - return Ok(Err((kind, why))); - } - }; - let owner = Owner::Buffer { - memory: Mutex::new(texels.buffer.memory_hold()), - }; - let made_with = (texels.options(), descriptor.allow_gpu_optimized_contents); - let identity = crate::resource::Identity { - tracked: texels.tracked(), - ..texels.buffer.identity() - }; - let image = - self.dma_buf_texture_image(made, plan.clone(), owner, made_with, identity, &pages); - Ok(Ok((image.shared(), false))) + match self.dma_buf_image_unbound(plan, facts, &dma_texels)? { + Ok(unbound) => Ok(Ok(Unplaced::DmaBuf { + unbound, + pages, + imports, + type_index, + at, + })), + Err((kind, why)) => Ok(Err((dma_buf_refusal(kind), why))), + } } /// No pages are imported as dma-bufs on this system. #[cfg(not(unix))] - fn dma_buf_in_place( + fn dma_buf_unplaced( &self, _plan: &ImagePlan, - _descriptor: &TextureDescriptor, _texels: &Texels, _facts: &ImageFacts<'_>, - ) -> Result { + ) -> Result, Error> { Ok(Err(( Refusal::Image, "the system imports no dma-bufs".to_owned(), ))) } + /// `unplaced`, an image in place of `plan` over `texels` (host pages of `class`), bound: over + /// pages imported by their pointer, in the buffer's memory where it was checked to lie, its + /// first move noted; over pages imported as dma-bufs, to a dedicated import of the dma-buf the + /// caller's source makes of its pages (called with no lock of the runtime's held), which may + /// still refuse it. With whether it is a `DRM_FORMAT_MOD_LINEAR` image made for host + /// allocations. + fn place( + &self, + unplaced: Unplaced, + plan: &ImagePlan, + descriptor: &TextureDescriptor, + texels: &Texels, + class: PagesClass, + ) -> Result { + match unplaced { + Unplaced::Host { + made, + pitched, + memory, + at, + } => { + let raw = made.raw; + let raw_device = self.shared().vk(); + // SAFETY: the image is live and unbound, made for host allocations; the memory is + // the buffer's import of host pages (or its fresh pages'), which the image holds, + // in a type the image takes, at an offset that meets its alignment, its size inside + // the memory (`placed`). + if let Err(error) = + unsafe { crate::creation::bind_image(raw_device, raw, memory, at) } + { + // SAFETY: the image is live and unbound, and no command uses it. + unsafe { release::object(raw_device, raw) }; + return Err(vulkan(error)); + } + let image = self.in_place_of(Raw::new(raw), plan.clone(), descriptor, texels); + // Over a no-copy buffer's pages, its move notes them (a rebind may swap the image + // out of them); fresh pages are the buffer's for good. + let pages = match class { + PagesClass::Fresh => None, + _ => texels.buffer.host_pages(), + }; + let first = FirstMove::new( + raw, + image.plan().format, + made.initial, + pages.as_ref().and_then(pages_hold), + ); + *lock(&image.first_move) = Some(first); + Ok(Ok((image.shared(), pitched))) + } + #[cfg(unix)] + Unplaced::DmaBuf { + unbound, + pages, + imports, + type_index, + at, + } => { + let layout = texels.layout; + let dma_texels = crate::dma_buf::DmaBufTexels { + pages: &imports, + type_index, + at, + row_pitch: layout.row_pitch, + slice_pitch: layout.slice_pitch, + }; + let made = match self.bind_dma_buf_image(unbound, &dma_texels)? { + Ok(made) => made, + Err((kind, why)) => return Ok(Err((dma_buf_refusal(kind), why))), + }; + let owner = Owner::Buffer { + memory: Mutex::new(texels.buffer.memory_hold()), + }; + let made_with = (texels.options(), descriptor.allow_gpu_optimized_contents); + let identity = crate::resource::Identity { + tracked: texels.tracked(), + ..texels.buffer.identity() + }; + let image = self.dma_buf_texture_image( + made, + plan.clone(), + owner, + made_with, + identity, + &pages, + ); + Ok(Ok((image.shared(), false))) + } + } + } + + /// Release `unplaced`'s image, which no command uses and no memory is bound to. + fn release_unplaced(&self, unplaced: Unplaced) { + match unplaced { + // SAFETY: the image is live and unbound, and no command uses it. + Unplaced::Host { made, .. } => unsafe { release::object(self.shared().vk(), made.raw) }, + #[cfg(unix)] + Unplaced::DmaBuf { unbound, .. } => unbound.release(self), + } + } + /// The image of `raw` over `texels`, bound to the buffer's memory, of `plan` and made with /// `descriptor`: its hazard identity the buffer's bytes', tracked as the texture is. fn in_place_of( diff --git a/crates/metal2vulkan-runtime/src/texture/route.rs b/crates/metal2vulkan-runtime/src/texture/route.rs index 48f2cc23d..e3145890d 100644 --- a/crates/metal2vulkan-runtime/src/texture/route.rs +++ b/crates/metal2vulkan-runtime/src/texture/route.rs @@ -4,6 +4,9 @@ //! [`Device::route_over_buffer`], so the answer is the making's own decision: texels past the //! buffer's length, a buffer of no host pages, a device copy, bytes detached or copied, and over //! host pages 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. use super::image_error; use crate::buffer::CallerMemory; @@ -65,9 +68,11 @@ impl Device { /// Where a texture of `descriptor` (checked as `layout`) over `texels` lies if made now, for /// Metal's `call`: the route its making takes, `past` where its texels run past the buffer's - /// length (`Texels::runs_past`, where the making gives them their tail). Refused as the making - /// refuses the texture's declared plan. A pending verdict starts the device's check, as the - /// making's does; nothing is made or counted. + /// length (`Texels::runs_past`, where the making gives them their tail); and where that is the + /// image in place, whether the device makes it, by the making's own steps up to the bind + /// (`Device::in_place_image_made`), each image they make released unbound. Refused as the + /// making refuses the texture's declared plan or fails those steps. A pending verdict starts + /// the device's check, as the making's does; nothing is bound, submitted or counted. pub(crate) fn placement_over_buffer( &self, call: &'static str, @@ -79,8 +84,23 @@ impl Device { .shared() .textures .facts(self.compile_device().profile()); - let (usage, _) = self.declared_plan(call, descriptor, layout, &facts)?; - Ok(self.route_over_buffer(texels, usage, past).placement()) + let (usage, plan) = self.declared_plan(call, descriptor, layout, &facts)?; + let route = match self.route_over_buffer(texels, usage, past) { + Route::Host { + class, + rows, + way: HostWay::InPlace { storable }, + } => { + let made = (texels, class, storable); + let way = match self.in_place_image_made(&plan, made, &facts)? { + Ok(()) => HostWay::InPlace { storable }, + Err((refusal, why)) => HostWay::Shadow(ShadowCause::Refused(refusal, why)), + }; + Route::Host { class, rows, way } + } + route => route, + }; + Ok(route.placement()) } /// The route of a texture of Metal `usage` (as the image is planned for it) over `texels`, diff --git a/crates/metal2vulkan-runtime/src/texture_placement.rs b/crates/metal2vulkan-runtime/src/texture_placement.rs index 5c6f25390..a480fa6ad 100644 --- a/crates/metal2vulkan-runtime/src/texture_placement.rs +++ b/crates/metal2vulkan-runtime/src/texture_placement.rs @@ -16,11 +16,12 @@ use crate::texture::{HostWay, Route}; pub enum TexturePlacement { /// A linear image aliasing the buffer's host pages, in place: the device's check passed for /// the pages' kind, for every access the texture's usage takes and for texels that lie in the - /// device's lines of memory as its own do. The image's making may still refuse (the device - /// makes no image of the texture's format, usage or pitch there, the image misses the bytes' - /// place or memory type, the texture's rows share bytes, or over dma-buf pages the caller's - /// source gives no one dma-buf of them): the texture is then a shadow, its notice - /// (`shadowed-texture`) naming why, counted in `WorkCounts::host_texture_shadows_refused_*`. + /// device's lines of memory as its own do, and the device made the image of the texture's + /// plan at its pitch, where its requirements take the bytes' place and memory type (the query + /// made it unbound and released it). Over pages imported as dma-bufs the image's making may + /// still refuse at the bind, where the caller's source gives no one dma-buf of the pages + /// ([`InPlaceRefusal::Source`]) or the driver refuses its import ([`InPlaceRefusal::DmaBuf`]): + /// the texture is then a shadow, its notice (`shadowed-texture`) naming why. InPlace, /// A shadow now, as the device's check has not answered for the pages' kind and the /// texture's accesses: once it does, at the commit of the device's only uncommitted command @@ -129,9 +130,10 @@ pub enum InPlaceRowEnds { /// Why the device makes no image in place over a texture's texels /// ([`TextureShadowReason::Refused`]), as `WorkCounts::host_texture_shadows_refused_*` counts -/// them. [`Buffer::texture_placement`](crate::Buffer::texture_placement) answers `Detached` and -/// `Placement` (a copied buffer's bytes), which it knows before any image is made; the others only -/// the image's making finds ([`TexturePlacement::InPlace`]). A runtime extension: Metal has none. +/// them. [`Buffer::texture_placement`](crate::Buffer::texture_placement) answers each one the +/// image's making decides before it binds memory, by the same steps: all but `Source`, and +/// `DmaBuf` where the driver refuses the dma-buf's import, which only the bind finds over pages +/// imported as dma-bufs ([`TexturePlacement::InPlace`]). A runtime extension: Metal has none. #[non_exhaustive] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum InPlaceRefusal { @@ -152,7 +154,8 @@ pub enum InPlaceRefusal { /// own pages. Source, /// Over pages imported as dma-bufs: no `DRM_FORMAT_MOD_LINEAR` image over one dma-buf of the - /// texture's own pages was made for another reason. + /// texture's own pages was made for another reason (the modifier's features or limits, the + /// image's memory past the pages or of another type, or the driver's import). DmaBuf, } diff --git a/docs/API.md b/docs/API.md index feb0c6e7a..85f561224 100644 --- a/docs/API.md +++ b/docs/API.md @@ -392,8 +392,16 @@ where the verdict passes; a shadow and why (`TextureShadowReason`: the policy, t check's failed or unchecked verdict with its access and row ends, a refusal, texels past the buffer's length, a device copy); no host pages; or a shape no image in place takes. 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; what only making the texture -shows (the device refusing the image in place, memory, a lost device) it does not know. A pending +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 +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 +`Shadow(Refused(..))`, the shadow the texture then is. That is one or two `VkImage`s where the +declared plan is made in place, and a pair more for each wider plan the making tries first where it +is refused; nothing is bound or submitted. Only the bind shows the rest: over pages imported as +dma-bufs, the caller's source's dma-buf and the driver's import of it; and memory or a lost device, +which fail the making. A pending verdict starts the check, as a texture's making does. `Device::settle_in_place_checks` starts the 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