From 537667dfcf875da6015f9110869d2a9a01b2f616 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 5 Nov 2026 02:06:07 -0800 Subject: [PATCH] Share kept resident uses between plans that list the same uses A pipeline state's draws of different primitive types may take plans of their own. The replayed textured page's point draws take another plan than its other four types, so 2 of every 5 replayed draws switch plan. The uses of what the encoder made resident were kept for one plan only, so each switch: - worked all of them out again (resident_uses_made 10,009,006 a frame for 5001 draws over 5002 resident textures, about 2001 draws' worth); - gave them a new identity, so every per-draw walk memo keyed on it (gathered, noted, unordered writes, indirect reads, geometry writes, attachments, pass reads) walked them again. Those uses are a function of the plan's uses (BindingPlan::uses) and the residency alone (binding::resident_uses_into). So the kept ones now serve any plan whose uses equal those of the plan they were made for. A plan is compared once, and the answer kept for the last five plans compared (ResidentOf::compared). Measured, release, the replayed textured page, interleaved with the commit before over two rounds (3 frames each): - Apple M3 Max, MoltenVK 1.4.2: - host_step_ms 1489-1621 a frame before, 407-468 after; - resident_uses_made 10,009,006 a frame before, 5006 after; - test time 5.1-5.3 s before, 1.8-2.1 s after; - pixels e3306dc52a1c9e86 in both. - RTX 5080 Laptop (b5080 entry resident-x-heap-part-004402, kind t): - host_step_ms 1309-1359 before, 481-525 after; - resident_uses_made 10,009,006 before, 5006 after; - pixels 8e977e2bb4582473 in both. - Against the commit before the heap part: the M3 goes from 10363-10491 to 407-468 ms, the 5080 from 8800-8931 to 481-525. The page's counters are this change's control: the commit before is this one without the sharing. Verified: the checks of the commit before, on this tree, with the same results. The new test passes. On the M3 it passes with the sharing taken out too: the fixture's vertex function writes no point size, so its points and triangles take one plan there. (cherry picked from commit 5ab66c0e6b185cbc2ee0dfb59048bf945bbdd217) --- crates/metal2vulkan-runtime/src/binding.rs | 43 ++++++++++++++---- .../src/tests/resident_uses.rs | 45 +++++++++++++++++++ .../metal2vulkan-runtime/src/work_counts.rs | 5 ++- 3 files changed, 82 insertions(+), 11 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/binding.rs b/crates/metal2vulkan-runtime/src/binding.rs index 2f0471de1..acb88d202 100644 --- a/crates/metal2vulkan-runtime/src/binding.rs +++ b/crates/metal2vulkan-runtime/src/binding.rs @@ -2380,8 +2380,9 @@ pub(super) fn command_uses( /// [`command_uses`] in `into`, over the last command's storage, where the caller keeps it /// (`LiveBind::uses`): its uses of what the encoder made resident, and the keys they reach, kept -/// from the last command where that command's plan was `plan` and its residency of the -/// generation `tables`' is. They are a function of those alone: each use's resource is the +/// from the last command where that command's plan was `plan`, or a plan that lists the same uses +/// (as a pipeline state's plans for draws of different primitive types may), and its residency +/// of the generation `tables`' is. They are a function of those alone: each use's resource is the /// residency's under its token, whose contents a generation names (`residency.rs`), its access /// and stages the plan's, and its key and range the resource's own, which no call changes (a /// resource's identity and length, a texture's levels and slices; `WorkCounts::resident_uses_made` @@ -2423,18 +2424,41 @@ pub(crate) fn kept_resident_checks() -> u64 { /// What the uses of what an encoder made resident at the start of a [`CommandUses`] were made /// for (`command_uses_into`): the plan (held weakly, so its allocation names no other plan while -/// this lives) and the residency's generation. +/// this lives) and the residency's generation; and the other plans found to list the plan's +/// uses, or not to, held weakly likewise. #[derive(Clone)] struct ResidentOf { plan: Weak, generation: Option, + compared: Vec<(Weak, bool)>, } +/// The most plans a [`ResidentOf`] keeps compared with its own: draws of one pipeline state whose +/// primitive types take plans of their own (Metal's five, `MTLPrimitiveType`) find each compared +/// once. +const COMPARED_PLANS: usize = 5; + impl ResidentOf { - /// Whether it was made for `plan` under `generation`: the plan compared by address, which - /// takes no count of its references. - fn holds(&self, plan: &Arc, generation: Option) -> bool { - self.plan.as_ptr() == Arc::as_ptr(plan) && self.generation == generation + /// Whether its uses are those `plan` makes under `generation`: made for it, or for a plan + /// that lists the same uses (`BindingPlan::uses`, which with the residency decide them alone, + /// [`resident_uses_into`]), found once by comparing them. The plans are compared by address, + /// which takes no count of their references. + fn holds(&mut self, plan: &Arc, generation: Option) -> bool { + let at = Arc::as_ptr(plan); + if self.generation != generation { + return false; + } + if self.plan.as_ptr() == at { + return true; + } + if let Some(&(_, alike)) = self.compared.iter().find(|(of, _)| of.as_ptr() == at) { + return alike; + } + let alike = (self.plan.upgrade()).is_some_and(|made| made.uses == plan.uses); + if self.compared.len() < COMPARED_PLANS { + self.compared.push((Arc::downgrade(plan), alike)); + } + alike } } @@ -2458,9 +2482,9 @@ fn uses_into( return Err(internal("a command of 2^32 uses")); } let generation = tables.residency.map(Residency::generation); - let holds = |of: &ResidentOf| kept.is_some_and(|plan| of.holds(plan, generation)); + let holds = |of: &mut ResidentOf| kept.is_some_and(|plan| of.holds(plan, generation)); into.made = 0; - let keeps = into.of.as_ref().is_some_and(holds); + let keeps = into.of.as_mut().is_some_and(holds); match keeps { true => { into.uses.truncate(into.used); @@ -2472,6 +2496,7 @@ fn uses_into( into.of = kept.map(|plan| ResidentOf { plan: Arc::downgrade(plan), generation, + compared: Vec::new(), }); // Kept from one command to the next alone, so the identity of those kept alone // (none where there are none: `prefix` reads it only where there are some). diff --git a/crates/metal2vulkan-runtime/src/tests/resident_uses.rs b/crates/metal2vulkan-runtime/src/tests/resident_uses.rs index 24fd66f96..04773f4cf 100644 --- a/crates/metal2vulkan-runtime/src/tests/resident_uses.rs +++ b/crates/metal2vulkan-runtime/src/tests/resident_uses.rs @@ -319,6 +319,51 @@ fn draws_keep_their_heap_part_until_what_their_encoder_made_resident_changes() { } } +/// Draws of the entry shape that alternate a point list and a triangle list, each binding other +/// buffers than the last: a draw of each type may take a plan of its own, and plans that list the +/// same uses share the uses of what the encoder made resident +/// (`binding::command_uses_into`), so only the first draw works them out; each plan's heap part is +/// worked out at its first draw alone. +#[test] +fn draws_of_two_primitive_types_keep_what_their_encoder_made_resident_alike() { + let scratch = Scratch::new("resident-primitive-types"); + let device = device(&scratch.0, &[], &Heard::default()); + let scene = Scene::new(&device); + let resources = Resources::new(&scene, -0.9); + let queue = device.new_command_queue().unwrap(); + let command_buffer = queue.command_buffer().unwrap(); + let mut encoder = command_buffer + .render_command_encoder(&pass(&scene.target)) + .unwrap(); + resources.resident(&scene, &mut encoder); + encoder.set_render_pipeline_state(&scene.entry).unwrap(); + let mut each = Vec::new(); + for at in 0..POSITIONS { + let table = &resources.tables[(at % 2) as usize]; + let offset = ENTRY * (1 + at % POSITIONS); + (encoder.set_vertex_buffer(Some(table), offset, 0)).unwrap(); + (encoder.set_fragment_buffer(Some(table), offset, 0)).unwrap(); + let primitive = [PrimitiveType::Point, PrimitiveType::Triangle][(at % 2) as usize]; + each.push(drawn(&device, || { + (encoder.draw_primitives(primitive, 0, 3, 1, 0)).unwrap(); + })); + } + encoder.end_encoding().unwrap(); + command_buffer.commit().unwrap(); + command_buffer.wait_until_completed_for(WAIT).unwrap(); + assert_eq!(command_buffer.status(), CommandBufferStatus::Completed); + for (at, drawn) in each.iter().enumerate() { + let what = format!("draw {at}: {drawn:?}"); + assert_eq!(drawn.made > 0, at == 0, "{what}"); + match at { + 0 => assert_eq!(drawn.heap_parts, 1, "{what}"), + // The triangle list's plan, where it takes one of its own. + 1 => assert!(drawn.heap_parts <= 1, "{what}"), + _ => assert_eq!(drawn.heap_parts, 0, "{what}"), + } + } +} + /// Draws of the entry shape after a draw whose fragments write a position the encoder made /// resident to read in the vertex stage: each reads in place, in its vertex stage through what the /// encoder made resident, bytes a fragment stage of the pass wrote (`render/pass_start_reads.rs`), diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index b7070a8f3..7a4e27579 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -5156,8 +5156,9 @@ pub struct WorkCounts { pub draws_rebound: u64, /// The uses of resources a render encoder made resident (`useResource:usage:stages:`, /// `useHeap:stages:`) that its draws' binding plans list, as its draws worked them out for - /// hazard tracking: a draw keeps the last draw's where its plan is the last draw's and what - /// the encoder made resident is unchanged since (no resource made resident, usage or stage + /// hazard tracking: a draw keeps the last draw's where its plan is the last draw's, or lists + /// the same uses as the plan they were worked out for, and what the encoder made resident is + /// unchanged since (no resource made resident, usage or stage /// widened, or heap used), and works out none then, a draw rebound or drawn again included; /// another works out each its plan lists. pub resident_uses_made: u64,