diff --git a/crates/metal2vulkan-conformance/src/cost.rs b/crates/metal2vulkan-conformance/src/cost.rs index 70398009c..3fcb6b038 100644 --- a/crates/metal2vulkan-conformance/src/cost.rs +++ b/crates/metal2vulkan-conformance/src/cost.rs @@ -478,6 +478,10 @@ const RACY: [(&str, &str); 37] = [ // own work, and `_reused` by counts of what a case's own encoder settled once. ("pipeline_layouts_shared", FOUND), ("shader_modules_shared", FOUND), + // A linked pipeline taken from another variant of the same link: in company, a case's first + // creation of a link finds it linked by a case before it, which moves this up and its partner + // `pipelines_linked`, which stays gated, down. + ("linked_pipelines_shared", FOUND), // An ask for a stage's function answered by the device's memo, a binary archive or the shader // cache on disk. The memo and the cache are the device's and the run's, so whether this case's // *first* ask for a function finds it already translated depends on whether a case before it @@ -760,7 +764,7 @@ impl Region { /// The name prefixes that put a count in [`Region::Translation`]: making a module, a layout or a /// pipeline. -const TRANSLATION: [&str; 19] = [ +const TRANSLATION: [&str; 20] = [ // The AIR decode and specialization a pipeline creation does for its stages, and the question // it asks about a linked function's uses. Translation work by definition -- an entry's // specialization depends on the functions and the constants, never on the pipeline state -- so @@ -796,6 +800,7 @@ const TRANSLATION: [&str; 19] = [ "pipeline_libraries_", "fragment_shader_libraries_", "pipelines_linked", + "linked_pipelines_shared", "dispatch_pipelines_pooled", "tile_pipelines_pooled", "variants_taken_back", diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 444091f98..f0cc20a33 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -122,6 +122,15 @@ Metal's default encoder state. A draw whose key differs makes its own `VkPipelin Tessellated and mesh states, separate depth and stencil formats and fragment functions that write depth make nothing at creation. +Where the device links its render pipelines from graphics pipeline libraries +(`VK_EXT_graphics_pipeline_library` with fast linking; MoltenVK makes them whole), +a linked `VkPipeline` is the device's, found by exactly what its linking takes: its four +libraries and its pipeline layout. Render states whose whole key is equal share it even where +each is a compiled pipeline of its own, whose stages come out as the same words (descriptors that +differ only in the call stack depth of a function that recurses through no visible function, for +one): the first links it, and each later one takes it with no driver call +(`WorkCounts::linked_pipelines_shared`). + ## Examples Each example runs on the system default device and uses only public API. Without an argument it diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs index ea742f485..33cd71ed0 100644 --- a/crates/metal2vulkan-runtime/src/device/mod.rs +++ b/crates/metal2vulkan-runtime/src/device/mod.rs @@ -814,12 +814,10 @@ impl Drop for DeviceShared { lock(&self.render_passes).destroy(self.vk()); // What released variants left (`pipeline/kept.rs`). lock(&self.kept).destroy(self); - // The graphics pipeline libraries, after every pipeline they linked: each linked + // The linked pipelines, then the graphics pipeline libraries they linked: each variant's // pipeline holds the device, and the kept ones went above. if let Some(libraries) = &self.libraries { - for gone in libraries.drain() { - gone.destroy(&*self); - } + libraries.drain(&*self); } // The shader modules, every one the map holds, destroyed here while the device lives: no // variant is left but one dropping this device right now (whose share then calls diff --git a/crates/metal2vulkan-runtime/src/pipeline/graphics.rs b/crates/metal2vulkan-runtime/src/pipeline/graphics.rs index 527560a30..e0f0f6fdf 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/graphics.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/graphics.rs @@ -1201,9 +1201,10 @@ impl VariantObjects { content.with_infos(self, render_pass.raw, |infos| match libraries { Some(libraries) => { let names = content.names(self, pipeline, key); - let linked = claimed.unwrap_or_else(|| libraries.claim(shared, &names)); + let linked = claimed + .unwrap_or_else(|| libraries.claim(shared, &names, self.shared_layout())); self.linked(infos, &names, linked) - .map(|(raw, linked)| (raw, Some(linked))) + .map(|(raw, linked, made)| (raw, Some(linked), made)) } None => { let info = infos.whole(); @@ -1245,22 +1246,29 @@ impl VariantObjects { .copied() .ok_or_else(|| internal("the driver made no pipeline")) }) - .map(|raw| (raw, None)) + .map(|raw| (raw, None, true)) } }) }); shared.counts.driver_diagnostics_withheld(withheld); - let (raw, linked) = created?; + let (raw, linked, made_here) = created?; let created_in = started.elapsed(); let kept_at = split.then(std::time::Instant::now); - if content.blend_flags.contains( - vk::PipelineColorBlendStateCreateFlags::RASTERIZATION_ORDER_ATTACHMENT_ACCESS_EXT, - ) { - shared.name_pipeline(raw, RASTER_ORDER_PIPELINE); - } drop(leader); drop(turn); - shared.counts.variant_pipeline_made(); + // A linked pipeline another variant of the same link made is that variant's making: named + // and counted there, and taken here with no driver call. + match made_here { + true => { + if content.blend_flags.contains( + vk::PipelineColorBlendStateCreateFlags::RASTERIZATION_ORDER_ATTACHMENT_ACCESS_EXT, + ) { + shared.name_pipeline(raw, RASTER_ORDER_PIPELINE); + } + shared.counts.variant_pipeline_made(); + } + false => shared.counts.linked_pipeline_shared(), + } if driver_reports { shared .shader_cache @@ -1289,21 +1297,45 @@ impl VariantObjects { Ok(made) } - /// The pipeline of `infos` linked, without link-time optimization, from `linked`, the - /// libraries of its parts named `names`: each made here where no caller made it or is - /// making it, through the device's shader cache under its name, else waited for. + /// The pipeline of `infos` linked, without link-time optimization, from `linked`'s libraries, + /// those of its parts named `names`, with whether it was linked here: the device's linked + /// pipeline of that link (`libraries.rs`), linked here where no caller linked it or is + /// linking it, else the one another variant of the same link linked, waited for. Linking it + /// makes each library where no caller made it or is making it, through the device's shader + /// cache under its name, else waits for it. fn linked( &self, infos: &Infos<'_>, names: &[Hash256; 4], linked: Linked, - ) -> Result<(vk::Pipeline, Linked), Error> { + ) -> Result<(vk::Pipeline, Linked, bool), Error> { + let split = creation_log::splitting(); + let asked = split.then(std::time::Instant::now); + let mut made = false; + let raw = linked.pipeline().made(|| { + made = true; + self.link(infos, names, &linked) + })?; + if let (false, Some(at)) = (made, asked) { + creation_log::note(|| "link (another caller's)".to_owned(), at.elapsed()); + } + Ok((raw, linked, made)) + } + + /// Link the pipeline of `infos` from `linked`'s libraries, named `names`, as + /// [`linked`](Self::linked) says. + fn link( + &self, + infos: &Infos<'_>, + names: &[Hash256; 4], + linked: &Linked, + ) -> Result { let shared = self.device.shared(); let split = creation_log::splitting(); let mut raws = [vk::Pipeline::null(); 4]; for ((part, library), (raw, name)) in Part::ALL .into_iter() - .zip(&linked.0) + .zip(linked.parts()) .zip(raws.iter_mut().zip(names)) { // The creation log's split: how long the part took, and whether this making made it. @@ -1348,13 +1380,14 @@ impl VariantObjects { } } let mut libraries = vk::PipelineLibraryCreateInfoKHR::default().libraries(&raws); + // The link's layout, which its key names: the variant's own (`infos.layout`). let info = vk::GraphicsPipelineCreateInfo::default() - .layout(infos.layout) + .layout(linked.layout()) .push_next(&mut libraries); let linking = split.then(std::time::Instant::now); - // SAFETY: every pointer in `info` outlives the call, and each library it links is made - // and held by `linked` for as long as the pipeline lives. A driver's making, which the exit - // waits for longer on a pool's thread. + // SAFETY: every pointer in `info` outlives the call, and each library it links and the + // layout it is linked with are made and held by `linked`'s link for as long as the + // pipeline lives. A driver's making, which the exit waits for longer on a pool's thread. let created = crate::exit_guard::driver_making(|| unsafe { shared .vk() @@ -1365,11 +1398,10 @@ impl VariantObjects { } let created = created.map_err(|(_, error)| vulkan(error))?; shared.counts.pipeline_linked(); - let raw = created + created .first() .copied() - .ok_or_else(|| internal("the driver linked no pipeline"))?; - Ok((raw, linked)) + .ok_or_else(|| internal("the driver linked no pipeline")) } } diff --git a/crates/metal2vulkan-runtime/src/pipeline/kept.rs b/crates/metal2vulkan-runtime/src/pipeline/kept.rs index fa058c6e4..77fa70b16 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/kept.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/kept.rs @@ -179,10 +179,22 @@ pub(crate) struct KeptPipeline { pub(crate) place: Place, pub(crate) raw: vk::Pipeline, pub(crate) unannounced: bool, - /// The libraries it was linked from, kept with it (`libraries.rs`). + /// The device's linked pipeline it is, with its libraries, kept with it (`libraries.rs`). pub(crate) linked: Option, } +impl KeptPipeline { + /// Destroy it where it is the variant's own, a pipeline made whole: a linked one is the + /// device's, which other variants may share, destroyed by its map once nothing holds it + /// (`libraries.rs`), and dropping this lets go of its share. + fn destroy(&self, shared: &DeviceShared) { + if self.linked.is_none() { + // SAFETY: released by its last holder, so no pending command uses it. + unsafe { release::object(shared.vk(), self.raw) }; + } + } +} + /// What a variant made again takes back: its instance, objects and pipelines. pub(crate) struct TakenBack { pub(crate) instance: Instance, @@ -201,8 +213,7 @@ impl Entry { /// Destroy its pipelines; dropping it lets go of its objects' shares. fn destroy(&self, shared: &DeviceShared) { for pipeline in &self.pipelines { - // SAFETY: released by its last holder, so no pending command uses it. - unsafe { release::object(shared.vk(), pipeline.raw) }; + pipeline.destroy(shared); } } } @@ -223,8 +234,7 @@ impl Arrival { /// Destroy its pipeline; dropping it lets go of its objects' shares. fn destroy(&self, shared: &DeviceShared) { if let Arrival::Pipeline(_, pipeline) = self { - // SAFETY: released by its last holder. - unsafe { release::object(shared.vk(), pipeline.raw) }; + pipeline.destroy(shared); } } } @@ -295,10 +305,7 @@ impl Entries { Some(entry) if !entry.pipelines.iter().any(|p| p.place == pipeline.place) => { entry.pipelines.push(pipeline); } - _ => { - // SAFETY: released by its last holder. - unsafe { release::object(shared.vk(), pipeline.raw) }; - } + _ => pipeline.destroy(shared), }, } } diff --git a/crates/metal2vulkan-runtime/src/pipeline/libraries.rs b/crates/metal2vulkan-runtime/src/pipeline/libraries.rs index b82864d13..f0a675e96 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/libraries.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/libraries.rs @@ -25,16 +25,28 @@ //! made by the first caller that needs it made ([`Library::made`]); a caller meanwhile waits for //! that making, and one after a failed making makes its own attempt, as a draw's own making does //! (`making.rs`). -//! - **Alive while linked.** A linked pipeline holds its four libraries ([`Linked`]), and a kept -//! pipeline keeps them (`kept.rs`): the device's map destroys a library only once nothing holds -//! it, at a program's settle or the device's teardown, after every pipeline it linked. +//! - **One linked pipeline per link.** A linked pipeline is the device's too, found by exactly +//! what its linking takes ([`Link`]): its four libraries, by their parts' names, and the pipeline +//! layout it is linked with, which no variant of another layout definition shares +//! (`shared_layouts.rs`). So render states whose whole key is equal, each a variant of its own +//! (compiled pipelines of their own whose stages come out as the same words, such as +//! descriptors that differ only in the call stack depth of a function that recurses through no +//! visible function), share one `VkPipeline`: the first links it, and every later one takes it +//! with no driver call (`WorkCounts::linked_pipelines_shared`). Claimed and made as a library +//! is (below). +//! - **Alive while linked.** A linked pipeline holds its four libraries and its layout ([`Link`]), +//! each variant's pipeline holds the link ([`Linked`]), and a kept pipeline keeps it +//! (`kept.rs`): the device's map destroys a linked pipeline only once nothing holds it, and a +//! library only after every pipeline it linked, at a program's settle or the device's teardown, +//! so no variant destroys one another variant draws with. //! - **Cached across processes.** Each library of shaders (pre-rasterization, fragment shader) is //! made through the device's shader cache under a record named by its part //! (`shader_cache/driver.rs`), as a whole pipeline is; the vertex input and fragment output //! libraries hold no shader and keep no record. use super::budget; -use super::content_keyed::{program_call, ContentKeyed, Gone, Owned}; +use super::content_keyed::{program_call, ContentKeyed, Owned}; +use super::shared_layouts::SharedLayout; use crate::device::{lock, DeviceShared}; use crate::error::Error; use crate::release; @@ -188,23 +200,61 @@ impl Owned for Library { } } -/// The four libraries a linked pipeline was linked from, held while it lives. -#[derive(Clone)] -pub(crate) struct Linked(pub(crate) [Arc; 4]); +/// What a linked pipeline's linking takes, exactly: the names of its four parts' libraries, in +/// [`Part::ALL`]'s order, and the pipeline layout it is linked with. The layout goes by its +/// handle, which its [`Link`] holds, so no other layout takes that handle while the link is in +/// the map. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +struct LinkKey { + names: [Hash256; 4], + layout: vk::PipelineLayout, +} -/// The device's libraries, by part and content (`DeviceShared::libraries`). +/// A linked pipeline: the device's, by its [`LinkKey`], made once by the first caller that needs +/// it ([`Library::made`], as a library is), holding what it was linked from while it lives. +pub(crate) struct Link { + pipeline: Library, + /// Its four libraries, in [`Part::ALL`]'s order. + parts: [Arc; 4], + /// The layout it is linked with: Vulkan may read a pipeline's creation's objects for as long + /// as the pipeline lives. + layout: Arc, +} + +impl Owned for Link { + fn destroy(&self, shared: &DeviceShared) { + // As a library's: the map lets it go only when no variant's pipeline, kept pipeline or + // command buffer holds it, or at the device's teardown. + self.pipeline.destroy(shared); + } +} + +/// The linked pipeline of a variant's key, with the libraries it is linked from, held while the +/// variant's pipeline lives: the device's, which other variants of the same link share. +#[derive(Clone)] +pub(crate) struct Linked(Arc); + +/// The device's libraries, by part and content, and its linked pipelines, by link +/// (`DeviceShared::libraries`). #[derive(Default)] pub(crate) struct Libraries { parts: [Mutex>; 4], + links: Mutex>, } impl Libraries { /// The libraries of the four parts `names` names (each its part's digest, in - /// [`Part::ALL`]'s order): the device's, inserted where it holds none in a program's call - /// (`content_keyed::program_call`), else a caller's own on a miss. Each map sweeps. - pub(crate) fn claim(&self, shared: &DeviceShared, names: &[Hash256; 4]) -> Linked { + /// [`Part::ALL`]'s order), and the pipeline linked from them with `layout`: the device's, + /// inserted where it holds none in a program's call (`content_keyed::program_call`), else a + /// caller's own on a miss. Each map sweeps. + pub(crate) fn claim( + &self, + shared: &DeviceShared, + names: &[Hash256; 4], + layout: &Arc, + ) -> Linked { let program = program_call(); - let claimed = std::array::from_fn(|at| { + let parts: [Arc; 4] = std::array::from_fn(|at| { let mut map = lock(&self.parts[at]); let (library, _) = match map.get_or_make(names[at], program, || { Ok::(Library::new()) @@ -218,25 +268,66 @@ impl Libraries { gone.destroy(shared); library }); - Linked(claimed) + let key = LinkKey { + names: *names, + layout: layout.layout(), + }; + let mut map = lock(&self.links); + let (link, _) = match map.get_or_make(key, program, || { + Ok::(Link { + pipeline: Library::new(), + parts, + layout: Arc::clone(layout), + }) + }) { + Ok(found) => found, + Err(never) => match never {}, + }; + let gone = map.sweep(); + drop(map); + gone.destroy(shared); + Linked(link) } - /// Let go of the libraries nothing holds any more: in a program's call, as the device's - /// modules are (`objects.rs`). + /// Let go of the linked pipelines nothing holds any more, then of the libraries no pipeline + /// holds: in a program's call, as the device's modules are (`objects.rs`). pub(crate) fn settle(&self, shared: &DeviceShared) { + let gone = lock(&self.links).settle(); + gone.destroy(shared); for part in &self.parts { let gone = lock(part).settle(); gone.destroy(shared); } } - /// Every library, at the device's teardown, after the kept pipelines it linked. - pub(crate) fn drain(&self) -> Vec> { - self.parts.iter().map(|part| lock(part).drain()).collect() + /// Destroy every linked pipeline, then every library: the device's teardown, after the kept + /// pipelines. + pub(crate) fn drain(&self, shared: &DeviceShared) { + let gone = lock(&self.links).drain(); + gone.destroy(shared); + for part in &self.parts { + let gone = lock(part).drain(); + gone.destroy(shared); + } } } impl Linked { + /// Its libraries, in [`Part::ALL`]'s order. + pub(crate) fn parts(&self) -> &[Arc; 4] { + &self.0.parts + } + + /// Its linked pipeline, made once by the first caller that needs it ([`Library::made`]). + pub(crate) fn pipeline(&self) -> &Library { + &self.0.pipeline + } + + /// The pipeline layout its pipeline is linked with. + pub(crate) fn layout(&self) -> vk::PipelineLayout { + self.0.layout.layout() + } + /// Whether the libraries of its shaders, its pre-rasterization and fragment shader parts, /// are made: a key whose shaders' libraries are made links on the caller's thread, where its /// vertex input and fragment output libraries, which hold no shader, are made in well under a @@ -247,7 +338,7 @@ impl Linked { Part::ALL .into_iter() .filter(|part| part.holds_shaders()) - .all(|part| self.0[part as usize].is_made()) + .all(|part| self.0.parts[part as usize].is_made()) } } diff --git a/crates/metal2vulkan-runtime/src/pipeline/objects.rs b/crates/metal2vulkan-runtime/src/pipeline/objects.rs index 93176f5b4..846e90607 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/objects.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/objects.rs @@ -530,8 +530,9 @@ pub(crate) struct RawPipeline { /// Made at its state's creation, its specialized notices not announced yet: the first draw /// that takes it announces them, with what it binds (`RenderVariant::first_use`). pub(super) unannounced: AtomicBool, - /// The libraries it was linked from, held while it lives (`libraries.rs`); `None` for a - /// pipeline made whole. + /// The device's linked pipeline it is, with the libraries it was linked from, held while it + /// lives (`libraries.rs`): `raw` is that pipeline's, which only the device's map destroys. + /// `None` for a pipeline made whole, which is this variant's own. pub(super) linked: Option, } @@ -552,8 +553,12 @@ impl Drop for RawPipeline { lock(&shared.kept).pipeline_released(instance, kept); return; } - // SAFETY: as for `VariantObjects`. - unsafe { release::object(self.device.shared().vk(), self.raw) }; + // A linked pipeline is the device's, which other variants may share: its map destroys + // it once nothing holds it (`libraries.rs`), and this lets go of its share. + if self.linked.is_none() { + // SAFETY: as for `VariantObjects`. + unsafe { release::object(self.device.shared().vk(), self.raw) }; + } } } @@ -863,6 +868,12 @@ impl VariantObjects { self.layout.layout() } + /// Its share of the device's layout, which a linked pipeline made with it holds + /// (`libraries.rs`). + pub(crate) fn shared_layout(&self) -> &Arc { + &self.layout + } + /// How many sets the layout has. pub(crate) fn set_count(&self) -> usize { self.layout.set_layouts().len() diff --git a/crates/metal2vulkan-runtime/src/pipeline/render.rs b/crates/metal2vulkan-runtime/src/pipeline/render.rs index 82ac35c68..6ea66aba2 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/render.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/render.rs @@ -2948,17 +2948,18 @@ impl RenderVariant { Ok((made, true)) } - /// The libraries of `key`'s parts (`libraries.rs`), claimed on this thread: the device's - /// where this is a program's call. `None` where the device makes its pipelines whole, where + /// The libraries of `key`'s parts and the pipeline linked from them (`libraries.rs`), claimed + /// on this thread: the device's where this is a program's call. `None` where the device makes its pipelines whole, where /// `key`'s pipeline is made whole, or where its state cannot be described (its making then /// says why). pub(crate) fn claim_libraries(&self, key: &GraphicsKey) -> Option { let shared = self.objects.device.shared(); let libraries = shared.libraries.as_ref()?; let content = self.objects.graphics_content(&self.pipeline, key).ok()?; - content - .links() - .then(|| libraries.claim(shared, &content.names(&self.objects, &self.pipeline, key))) + content.links().then(|| { + let names = content.names(&self.objects, &self.pipeline, key); + libraries.claim(shared, &names, self.objects.shared_layout()) + }) } /// The `VkPipeline`s of `keys`, each against its pass (a pass of the key's `pass`), begun at diff --git a/crates/metal2vulkan-runtime/src/tests/linked_pipelines_shared.rs b/crates/metal2vulkan-runtime/src/tests/linked_pipelines_shared.rs new file mode 100644 index 000000000..0dacc209d --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/linked_pipelines_shared.rs @@ -0,0 +1,98 @@ +//! Render states whose whole key is equal, each a variant of its own, share one linked +//! `VkPipeline`: the device's, found by exactly what its linking takes, its four libraries and its +//! pipeline layout (`pipeline/libraries.rs`, `WorkCounts::linked_pipelines_shared`). +//! +//! The siblings here are descriptors of one vertex-and-fragment pair that differ only in +//! `maxFragmentCallStackDepth`, which sizes the call stack of a fragment function's recursion +//! through visible functions: each compiles its fragment function under a depth of its own, and +//! so is a compiled pipeline and a variant of its own, while the fixture's fragment function calls +//! no visible function, so every sibling's stages come out as the first creation's words, and +//! every part of its pipelines, every library and every link, is the first creation's. Each +//! sibling's creation takes the first creation's linked pipelines with no driver call, and each +//! sibling draws with them the pixels the fixture's constants give. +//! +//! A descriptor that differs only in what no compile reads (a buffer's mutability) is not a +//! sibling of this kind: it is the first creation's compiled pipeline, whose live variant it +//! shares whole (`memo.rs`). +//! +//! On a device that makes its render pipelines whole (no `VK_EXT_graphics_pipeline_library` with +//! fast linking: MoltenVK) nothing is linked, so nothing is shared; the run says so and checks +//! the draws alone. +//! +//! A device test: compiled only under the GPU safety cfgs (`m2v_gpu`, `m2v_cpu_icd`), on the kind +//! of device the cfg selects. The functions are the owned fixture +//! `validation/fixtures/public/engine/ubo_bind_offsets.ll`'s (`llvm-as`). + +use super::blit_holds::{device, Scratch}; +use super::descriptor_key_once::descriptor; +use super::uniform_bind_offsets::{draw, idle, Bound}; +use crate::{PipelineOptions, WorkCounts}; +use std::time::Instant; + +/// The siblings made after the first state. +const RUN: u32 = 16; + +/// The draw each state makes: the fragment's constants at byte 0 and the vertex's at byte 256 of +/// the constants buffer, offsets every device's uniform alignment divides. +const CASE: Bound = Bound::Buffer(0, 256); + +#[test] +fn render_states_of_one_whole_key_share_one_linked_pipeline() { + let scratch = Scratch::new("linked-pipelines-shared"); + let device = device(&scratch.0); + let queue = device.new_command_queue().unwrap(); + let base = descriptor(&device); + let options = PipelineOptions::default(); + let first = device.new_render_pipeline_state(&base, &options).unwrap(); + draw(&device, &queue, &first, &[CASE]); + let before = idle(&device); + let mut siblings = Vec::with_capacity(RUN as usize); + let mut times = Vec::with_capacity(RUN as usize); + for i in 0..RUN { + let mut d = base.clone(); + // Past Metal's default, 1, each a depth of its own. + d.max_fragment_call_stack_depth = 2 + i; + let started = Instant::now(); + siblings.push(device.new_render_pipeline_state(&d, &options).unwrap()); + times.push(started.elapsed().as_secs_f64() * 1e3); + } + for sibling in &siblings { + draw(&device, &queue, sibling, &[CASE]); + } + let after = idle(&device); + let made = |of: fn(&WorkCounts) -> u64| of(&after) - of(&before); + let (linked, shared) = ( + made(|c| c.pipelines_linked), + made(|c| c.linked_pipelines_shared), + ); + let libraries = made(|c| c.pipeline_libraries_made); + let pipelines = made(|c| c.variant_pipelines_made); + times.sort_by(f64::total_cmp); + println!( + "LINKED-PIPELINES-SHARED: {RUN} siblings: {linked} linked, {shared} shared, {libraries} \ + libraries made, {pipelines} variant pipelines made; creation median {:.3} ms, max {:.3} ms", + times[times.len() / 2], + times[times.len() - 1], + ); + if device.shared().libraries.is_none() { + assert_eq!( + shared, 0, + "a device that makes its render pipelines whole links none, so shares none" + ); + return; + } + // Each sibling is a variant of its own: it made no library and linked nothing, and took at + // least one linked pipeline, its creation's, from the first state. + assert_eq!( + (linked, libraries, pipelines), + (0, 0, 0), + "every one of {RUN} siblings of one whole key must take the first state's linked \ + pipelines and libraries: {linked} linked, {libraries} libraries and {pipelines} variant \ + pipelines made, {shared} shared" + ); + assert!( + shared >= u64::from(RUN), + "every one of {RUN} siblings must take at least its creation's linked pipeline from the \ + first state: {shared} taken" + ); +} diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index 6a5307ff7..32fa6b409 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -179,6 +179,8 @@ mod layer_claim; mod left; mod linear_attachment; mod linear_plan; +#[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod linked_pipelines_shared; mod making; mod managed; mod memo; diff --git a/crates/metal2vulkan-runtime/src/tests/uniform_bind_offsets.rs b/crates/metal2vulkan-runtime/src/tests/uniform_bind_offsets.rs index a53c2ded6..02848ffa5 100644 --- a/crates/metal2vulkan-runtime/src/tests/uniform_bind_offsets.rs +++ b/crates/metal2vulkan-runtime/src/tests/uniform_bind_offsets.rs @@ -38,7 +38,7 @@ const FLOATS: usize = 256; /// Where a draw reads its constants: the fragment's at byte `.0`, the vertex's at byte `.1`, of /// the constants buffer, or both as inline bytes from those offsets of it. #[derive(Clone, Copy, Debug)] -enum Bound { +pub(super) enum Bound { Buffer(u64, u64), Bytes(u64, u64), } @@ -127,7 +127,7 @@ fn pass(device: &Device) -> (RenderPassDescriptor, Texture) { /// `device`'s work counts once its pipeline pool holds no piece (`WorkCounts::pipeline_pool_busy`): /// a draw that took its key's residual form does not wait for its own key's making. -fn idle(device: &Device) -> crate::WorkCounts { +pub(super) fn idle(device: &Device) -> crate::WorkCounts { let until = std::time::Instant::now() + WAIT; loop { let counts = device.work_counts(); @@ -144,7 +144,12 @@ fn idle(device: &Device) -> crate::WorkCounts { } /// Draw each of `cases` into a target of its own in one command buffer, and check every pixel. -fn draw(device: &Device, queue: &CommandQueue, state: &RenderPipelineState, cases: &[Bound]) { +pub(super) fn draw( + device: &Device, + queue: &CommandQueue, + state: &RenderPipelineState, + cases: &[Bound], +) { let data = constants(); let constants = device .new_buffer_with_bytes(&data, ResourceOptions::shared()) diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 3f93ef134..7843dbf69 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -449,6 +449,7 @@ pub(crate) struct Counters { pipeline_libraries_made: AtomicU64, fragment_shader_libraries_made: AtomicU64, pipelines_linked: AtomicU64, + linked_pipelines_shared: AtomicU64, dispatch_pipelines_pooled: AtomicU64, tile_pipelines_pooled: AtomicU64, upload_chunks_in_device_memory: AtomicU64, @@ -1068,6 +1069,12 @@ impl Counters { self.pipelines_linked.fetch_add(1, Ordering::Relaxed); } + /// A render variant took the device's linked pipeline of its link, which another variant + /// linked (`pipeline/libraries.rs`). + pub(crate) fn linked_pipeline_shared(&self) { + self.linked_pipelines_shared.fetch_add(1, Ordering::Relaxed); + } + /// A variant made again took back a released one's objects (`pipeline/kept.rs`). pub(crate) fn variant_taken_back(&self) { self.variants_taken_back.fetch_add(1, Ordering::Relaxed); @@ -3315,7 +3322,9 @@ pub struct WorkCounts { /// pushed with it (`render/bound.rs`). pub pipeline_layouts_shared: u64, /// `VkPipeline`s pipeline states' variants made (compute regions and their emulated mesh - /// stages, indirect-bindable replays, and draws): none for one a released variant kept. + /// stages, indirect-bindable replays, and draws): none for one a released variant kept, nor + /// for a linked pipeline another variant of the same link linked + /// ([`linked_pipelines_shared`](Self::linked_pipelines_shared)). pub variant_pipelines_made: u64, /// Nanoseconds the runtime's `vkCreateGraphicsPipelines` calls spent inside the driver, from /// the entry to its return (`device/compiles.rs`, the one door every graphics pipeline the @@ -5310,6 +5319,14 @@ pub struct WorkCounts { /// Render variants' `VkPipeline`s linked from libraries (each counted in /// `variant_pipelines_made` too). pub pipelines_linked: u64, + /// Render variants' `VkPipeline`s that took the device's linked pipeline of their exact link + /// (the same four libraries and pipeline layout), linked before by another variant: no driver + /// call, and counted in neither `pipelines_linked` nor `variant_pipelines_made` + /// (`pipeline/libraries.rs`). Render states whose whole key is equal but which are variants + /// of their own share one: compiled pipelines of their own whose stages come out as the same + /// words, such as descriptors that differ only in the call stack depth of a function that + /// recurses through no visible function. + pub linked_pipelines_shared: u64, /// Compute encoders' dispatches that handed the making of a region's `VkPipeline` to the /// device's pool and went on, rather than make it at the dispatch: each counted once per /// region it handed. @@ -6159,6 +6176,7 @@ impl Device { .fragment_shader_libraries_made .load(Ordering::Relaxed), pipelines_linked: counters.pipelines_linked.load(Ordering::Relaxed), + linked_pipelines_shared: counters.linked_pipelines_shared.load(Ordering::Relaxed), dispatch_pipelines_pooled: counters.dispatch_pipelines_pooled.load(Ordering::Relaxed), tile_pipelines_pooled: counters.tile_pipelines_pooled.load(Ordering::Relaxed), upload_chunks_in_device_memory: (counters.upload_chunks_in_device_memory) diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 2abb79c2d..454869fd0 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2762,6 +2762,7 @@ field metal2vulkan-runtime::WorkCounts.kept_bytes_passes pub kept_bytes_passes: field metal2vulkan-runtime::WorkCounts.line_band_draws pub line_band_draws: u64 field metal2vulkan-runtime::WorkCounts.linear_passes_in_place pub linear_passes_in_place: u64 field metal2vulkan-runtime::WorkCounts.linear_passes_through_copies pub linear_passes_through_copies: u64 +field metal2vulkan-runtime::WorkCounts.linked_pipelines_shared pub linked_pipelines_shared: u64 field metal2vulkan-runtime::WorkCounts.linked_use_questions_asked pub linked_use_questions_asked: u64 field metal2vulkan-runtime::WorkCounts.live_tables_uploaded pub live_tables_uploaded: u64 field metal2vulkan-runtime::WorkCounts.long_queue_settles pub long_queue_settles: u64