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,