diff --git a/crates/metal2vulkan-runtime/src/encoder.rs b/crates/metal2vulkan-runtime/src/encoder.rs index d9c14ccb1..cb1c2cd6b 100644 --- a/crates/metal2vulkan-runtime/src/encoder.rs +++ b/crates/metal2vulkan-runtime/src/encoder.rs @@ -2388,9 +2388,10 @@ impl EncoderCore { shadows: &[ShadowUse], uses: &[Use], ) -> Result, Error> { - // What a copy in records beyond what the texture holds from its making: a converted + // What a copy records beyond what the texture holds from its making: a converted // two-plane YCbCr texture's conversion, made at its first use of its image; a 4:2:2 - // texture's set over its buffer's `VkBuffer` now, held until the command buffer completes. + // texture's set over its buffer's `VkBuffer` now, held until the command buffer completes; + // a tiled heap texture's kernel and sets of its moves, made at its first use. for shadow in shadows { if let Some(held) = shadow.copies.prepare()? { self.retain_shared(held); diff --git a/crates/metal2vulkan-runtime/src/shadow.rs b/crates/metal2vulkan-runtime/src/shadow.rs index 7387406d0..b4551694c 100644 --- a/crates/metal2vulkan-runtime/src/shadow.rs +++ b/crates/metal2vulkan-runtime/src/shadow.rs @@ -883,22 +883,29 @@ impl ShadowCopies { } } - /// Make what its copies in record where they record more than the texture holds from its + /// Make what its copies record where they record more than the texture holds from its /// making: a conversion's staging buffer, sets and refresh (`ycbcr.rs`, /// `YCbCrConversion::make`), which a texture whose planes every function reads in place never /// needs, or a 4:2:2 texture's conversion's set over its buffer's `VkBuffer` now /// (`PairsConversion::make`), which the command buffer of the copy's command holds until it - /// completes (returned). Refused as the conversion's making is. + /// completes (returned); a tiled heap texture's kernel and sets of its moves, which its making + /// leaves to its first use (`HeapTiledCopies::make`). Refused as the conversion's or the + /// moves' making is. pub(super) fn prepare(&self) -> Result>, Error> { - let ShadowSource::Converted { - conversion, buffer, .. - } = &self.source - else { - return Ok(None); - }; - match conversion { - Conversion::Planes(conversion) => conversion.make().map(|()| None), - Conversion::Pairs(conversion) => conversion.make(buffer.raw()), + match &self.source { + ShadowSource::Converted { + conversion, buffer, .. + } => match conversion { + Conversion::Planes(conversion) => conversion.make().map(|()| None), + Conversion::Pairs(conversion) => conversion.make(buffer.raw()), + }, + ShadowSource::Bytes { + target: ShadowTarget::Tiled { staging, tiles, .. }, + .. + } => tiles.make(staging.buffer.generation()).map(|()| None), + ShadowSource::Bytes { .. } + | ShadowSource::Image { .. } + | ShadowSource::YCbCrWrite { .. } => Ok(None), } } diff --git a/crates/metal2vulkan-runtime/src/texture/heap_tiled.rs b/crates/metal2vulkan-runtime/src/texture/heap_tiled.rs index 3fed80718..da595f9d5 100644 --- a/crates/metal2vulkan-runtime/src/texture/heap_tiled.rs +++ b/crates/metal2vulkan-runtime/src/texture/heap_tiled.rs @@ -134,7 +134,11 @@ //! tight buffer, the kernel into the staging buffer (each element's bytes; every other byte as //! the heap held it), and each level's bytes of each chain on into the heap. Each step is //! ordered after the one before by a full barrier, and the buffers are the texture's for its -//! life. The texture's CPU copies move each element between the heap's bytes and the caller's +//! life. The kernel and the texture's two sets of it are made at its first use, as an encoder +//! plans a command that uses it (`ShadowCopies::prepare`), before any of its copies is recorded, +//! never as the texture is made: a texture's making compiles no pipeline. So the device's kernel +//! is compiled at the first encode that needs any of its tiled shadows, once, as every kernel of +//! the runtime's own is (`owned_kernel.rs`). The texture's CPU copies move each element between the heap's bytes and the caller's //! as laid out (`TiledLayout::element`); a command that writes the bytes two ways merges as //! any shadow's (`shadow_merge.rs`), the shadow's texels laid out tiled for it //! (`HeapTiledCopies::record_out`). @@ -190,12 +194,13 @@ use super::copy_images::{Reused, UseMaker}; use super::heap::HeapPlane; use super::{copy_units, each_copy_unit, texel_units, Texture, TextureShared}; use crate::device::functions::Vk; +use crate::device::lock; use ash::vk; use metal2vulkan::bind::{TiledLevelMove, TiledTexelsModule}; use metal2vulkan::metal::format::{aspects, byte_format, is_depth_stencil, srgb_raw_twin}; use metal2vulkan::metal::{PixelFormat, TextureType, TextureUsage}; use metal2vulkan::vk::{Format, ImageAspectFlags}; -use std::sync::{OnceLock, Weak}; +use std::sync::{Mutex, OnceLock, Weak}; /// The bytes of Metal's largest tile, whatever its elements' size. const TILE_BYTES: u64 = 16 << 10; @@ -1601,7 +1606,7 @@ impl Scratch { /// A heap texture's moves between its image and its elements tiled in the heap's bytes (the /// module docs): the tight buffer of the image's elements, the shapes, and the kernel's two sets -/// (into the image, then back). +/// (into the image, then back), made at the texture's first use ([`make`](Self::make)). pub(crate) struct HeapTiledCopies { /// Metal's layout of the texture's elements. pub(crate) tiles: TiledTexture, @@ -1623,8 +1628,13 @@ pub(crate) struct HeapTiledCopies { /// The `VkBuffer` of the two shapes: its staging buffer's too. shapes_raw: vk::Buffer, moves: [Move; 2], - kernel: OwnedKernel, - sets: OwnedSets, + /// What each of the kernel's two sets binds, in binding order: the tight elements, the tiled + /// bytes and the move's shape (into the image, then back). + binds: [[(vk::Buffer, u64, u64); 3]; 2], + /// The kernel and its two sets, made at the texture's first use ([`make`](Self::make)). + made: OnceLock<(OwnedKernel, OwnedSets)>, + /// Held while they are made, so one use makes them. + making: Mutex<()>, } /// A multisample heap texture's moves between its image and the tight buffer, one way, which @@ -1839,7 +1849,6 @@ impl HeapTiledCopies { }; let (into, back) = (step(true)?, step(false)?); let shape_bytes = 4 * u64::from(TiledTexelsModule::SHAPE_WORDS); - let kernel = device.owned_kernel(OwnedModule::TiledTexels)?; // Each set binds the tight elements, the tiled bytes and the shape, in binding order. let set = |step: &Move| { [ @@ -1848,7 +1857,7 @@ impl HeapTiledCopies { (raw, step.at, shape_bytes), ] }; - let sets = OwnedSets::write(device, &kernel, &[set(&into), set(&back)])?; + let binds = [set(&into), set(&back)]; for region in &mut regions { region.buffer_offset += tight_at; } @@ -1861,11 +1870,31 @@ impl HeapTiledCopies { tight_at, shapes_raw: raw, moves: [into, back], - kernel, - sets, + binds, + made: OnceLock::new(), + making: Mutex::new(()), }) } + /// Make the device's kernel of the moves (`OwnedModule::TiledTexels`) and the texture's two + /// sets of it, once: at the texture's first use, as an encoder plans a command that uses it + /// (`ShadowCopies::prepare`), before any of its copies is recorded (the module docs), never + /// as it is made. Refused as the kernel's or the sets' making is; a later use tries again. + pub(crate) fn make(&self, device: &Device) -> Result<(), Error> { + if self.made.get().is_some() { + return Ok(()); + } + let _making = lock(&self.making); + if self.made.get().is_some() { + return Ok(()); + } + let kernel = device.owned_kernel(OwnedModule::TiledTexels)?; + let sets = OwnedSets::write(device, &kernel, &self.binds)?; + // Set once, under the lock. + let _ = self.made.set((kernel, sets)); + Ok(()) + } + /// Record the dispatch of the move `into` the image, or back, after writing its shape. /// /// # Safety @@ -1873,9 +1902,19 @@ impl HeapTiledCopies { /// `command_buffer` is recording, outside a render pass, with every write the dispatch /// reads made visible to it; the buffers and the sets are live until it completes. unsafe fn dispatch(&self, device: &Vk, command_buffer: vk::CommandBuffer, into: bool) { + // Every command that records the moves used the texture first, which made them + // (`ShadowCopies::prepare`). + let made = self.made.get(); + debug_assert!( + made.is_some(), + "a tiled heap texture's moves recorded before a use made their kernel" + ); + let Some((kernel, sets)) = made else { + return; + }; let (step, set) = match into { - true => (&self.moves[0], self.sets.sets[0]), - false => (&self.moves[1], self.sets.sets[1]), + true => (&self.moves[0], sets.sets[0]), + false => (&self.moves[1], sets.sets[1]), }; // SAFETY: the caller's contract; the shape fits its slot, and the sets bind the // texture's own buffers. @@ -1885,12 +1924,12 @@ impl HeapTiledCopies { device.cmd_bind_pipeline( command_buffer, vk::PipelineBindPoint::COMPUTE, - self.kernel.pipeline, + kernel.pipeline, ); device.cmd_bind_descriptor_sets( command_buffer, vk::PipelineBindPoint::COMPUTE, - self.kernel.layout, + kernel.layout, 0, &[set], &[], diff --git a/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs b/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs index bd1fd4b8f..421989848 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/first_use.rs @@ -13,7 +13,9 @@ //! each copies to the buffer's own bytes by the next submission), and a no-copy buffer with a //! texture over it is rebound. The end mark reads 0 as each call returns. Afterwards every //! texture and buffer holds what Metal's holds: zero where new, the bytes it was made with, the -//! bytes it lies over where it aliases, what a blit wrote. +//! bytes it lies over where it aliases, what a blit wrote. No making builds a kernel of the +//! runtime's own (`WorkCounts::owned_kernels_made`), a pipeline's compile: a tiled heap +//! texture's is built at its first use. //! - **The zeros come before every use, on every queue** (the coordinator's conditions). A new //! `Private` buffer whose memory held other bytes is read through its address unnamed, and //! through its heap made resident, by a command buffer on another queue, after a command @@ -516,6 +518,8 @@ struct Calls<'a> { announced_wait: Option<(String, Duration, Degradation)>, /// The degrade notices taken from the device's messages by [`Calls::time_announcing`]. notices: Vec, + /// The runtime's own kernels each call built (`WorkCounts::owned_kernels_made`). + built: Vec<(String, u64)>, } impl<'a> Calls<'a> { @@ -530,6 +534,7 @@ impl<'a> Calls<'a> { queue_lock_wait: None, announced_wait: None, notices: Vec::new(), + built: Vec::new(), } } @@ -561,11 +566,14 @@ impl<'a> Calls<'a> { if let Some((messages, _)) = announcing { self.notices.extend(messages.take_notices()); } - let before = self.device.work_counts().internal_submissions; + let counts = self.device.work_counts(); + let (before, built) = (counts.internal_submissions, counts.owned_kernels_made); let (at, begun) = (Instant::now(), host_time()); let made = call(); let (took, returned) = (at.elapsed(), host_time()); - let internal = self.device.work_counts().internal_submissions - before; + let counts = self.device.work_counts(); + let internal = counts.internal_submissions - before; + (self.built).push((what.clone(), counts.owned_kernels_made - built)); let announced = announcing.and_then(|(messages, kind)| { let notices = messages.take_notices(); let announced = (notices.iter()).any(|message| { @@ -627,6 +635,23 @@ impl<'a> Calls<'a> { } } + /// Check that no call built a kernel of the runtime's own (`WorkCounts::owned_kernels_made`), + /// each a pipeline's creation, tens of milliseconds in the driver: Metal's makings compile + /// nothing. A tiled heap texture's kernel is built at its first use + /// (`runtime/src/texture/heap_tiled.rs`). + fn built_nothing(&self) { + let built: Vec = (self.built.iter()) + .filter(|(_, kernels)| *kernels > 0) + .map(|(what, kernels)| format!("{what} ({kernels})")) + .collect(); + assert!( + built.is_empty(), + "calls on {} built kernels of the runtime's own, compiling pipelines: {}", + self.device.name(), + built.join(", ") + ); + } + /// How the calls returned against the busy work, for the test's summary line. fn waits(&self) -> String { match (&self.queue_lock_wait, &self.announced_wait) { @@ -798,11 +823,13 @@ fn new_resources_wait_for_no_gpu_work() { took.join(", ") ); release(&calls.mark); + calls.built_nothing(); busy_work.wait_until_completed_for(WAIT).unwrap(); assert_eq!(busy_work.error(), None); assert_eq!(read_mark(&calls.mark), ENDED, "the busy work's end mark"); // What each holds, as Metal's does. + let kernels = device.work_counts().owned_kernels_made; let written = pattern(6); let command_buffer = queue.command_buffer().unwrap(); copy_in(&command_buffer, &private, &written); @@ -861,6 +888,13 @@ fn new_resources_wait_for_no_gpu_work() { command_buffer.commit().unwrap(); command_buffer.wait_until_completed_for(WAIT).unwrap(); assert_eq!(command_buffer.error(), None, "on {name}"); + let counts = device.work_counts(); + println!( + "FIRST-USE on {name}: heap textures tiled {}; the makings built no kernel, the uses \ + built {}", + counts.heap_textures_tiled, + counts.owned_kernels_made - kernels + ); for (what, out, expected) in outs { let len = expected.len() as u64; assert!( diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 42f1ebfac..13d43cbbe 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 523; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2372; +pub const RUNTIME_VERSION: u32 = 2373; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles