From b5f8b080e9e66c080792a8d3cb7413926d301e4f Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 8 Nov 2026 10:15:31 -0800 Subject: [PATCH] WorkCounts: why direct draws keep nothing, find their kept draw unlike, or stop a rebind A guest's composite (the 780M glass drag) rebound 0 of about 261 draws, though some of its gaps between draws call no forgetting call. These counts tell why: draws_unkept, a direct draw that recorded and kept nothing, with the groups of conditions it met (a step of its own or a new instance, a barrier, a shadow or snapshot, a heap, handle, sampler or root table or push hole, an index copy, a write, a pipeline still being made, or none of those); draws_unlike_kept, a draw whose encoder kept a draw of another call, arguments, state, command buffer or Under; and draws_rebind_stopped, a draw of its kept draw again after calls that took every step. The switching test checks the first two on its draws. The API signatures table is blessed. (cherry picked from commit a027a28691a3cb6d4022e320047e16cbeaa5d5a3) --- crates/metal2vulkan-runtime/src/render.rs | 21 ++++ .../src/render/draw_again.rs | 9 +- .../src/tests/draw_again.rs | 11 +- .../metal2vulkan-runtime/src/work_counts.rs | 119 ++++++++++++++++++ crates/metal2vulkan/tests/api_signatures.tsv | 11 ++ 5 files changed, 168 insertions(+), 3 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index 29ff94b0e..11b575c6e 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -353,6 +353,7 @@ use crate::device::functions::Vk; use crate::work_counts::Begun; use crate::work_counts::NotInline; use crate::work_counts::Split; +use crate::work_counts::UnkeptDraw; use ash::vk; pub(crate) use draw_shape::KeptShapes; use draw_shape::{LayoutView, PipelineInputs, ShapeInputs, ShapeMemo, ShapeToken, ShapedState}; @@ -10799,6 +10800,26 @@ impl RenderLowering { .as_ref() .and_then(RenderPipelineState::downgrade); let keeps = plain && !self.replaying && self.core.noted_now(&live.noted); + // What kept a direct draw of its own from being kept (`WorkCounts::draws_unkept`). + if !keeps && !self.replaying && !self.rerouting && !empty && !culled { + let unkept = UnkeptDraw { + instance: !stepless, + barrier: barriered, + shadowed: !draw_shadows.is_empty() + || snapshot.is_some() + || !live.state.extended_shadows.is_empty(), + tables: heap.is_some() + || texture_table.is_some() + || sampler_table.is_some() + || !commands.root_tables.is_empty() + || !commands.push_holes.is_empty() + || unlisted.is_some(), + index_copied: !index.is_none_or(DrawSource::in_place), + writing: !unwritten, + making: !matches!(draw_pipeline, DrawPipeline::Made(_)), + }; + self.core.counts().draw_unkept(unkept); + } if let (true, Some(drawn_by)) = (keeps, drawn_by) { let execution = records.then(|| draw_again::KeptExecution { pipeline: ExecutedPipeline::Render(Arc::clone(variant.shared_pipeline())), diff --git a/crates/metal2vulkan-runtime/src/render/draw_again.rs b/crates/metal2vulkan-runtime/src/render/draw_again.rs index f2b74ee93..5307d447c 100644 --- a/crates/metal2vulkan-runtime/src/render/draw_again.rs +++ b/crates/metal2vulkan-runtime/src/render/draw_again.rs @@ -349,14 +349,21 @@ impl RenderLowering { && kept.command_buffer == self.current.draws && kept.under == self.under(); if !same { + if buffers.indirect.is_none() { + self.core.counts().draw_unlike_kept(); + } return Again::No; } let stamps = self.table_stamps(); if kept.stamps != stamps || kept.between { - return match kept.rebinds { + let again = match kept.rebinds { true => self.draw_rebound(live, call, (assembly, draw), buffers, stamps, checked), false => Again::No, }; + if matches!(again, Again::No) { + self.core.counts().draw_rebind_stopped(); + } + return again; } #[cfg(debug_assertions)] { diff --git a/crates/metal2vulkan-runtime/src/tests/draw_again.rs b/crates/metal2vulkan-runtime/src/tests/draw_again.rs index 59c4a8ea5..0fdc8c712 100644 --- a/crates/metal2vulkan-runtime/src/tests/draw_again.rs +++ b/crates/metal2vulkan-runtime/src/tests/draw_again.rs @@ -759,8 +759,10 @@ fn switching_rectangles( /// (`WorkCounts::draws_rebound`), with the same commands of every kind, state commands and pixels /// as the same draws each taking every step, after a call between each that changes nothing for /// them (`set_blend_color` of the color the encoder holds), which forgets the kept draws of both -/// states. And a draw that keeps nothing (one that writes memory, `cfb_dn_count`) forgets the -/// other state's kept draw: draws switching with it take every step. +/// states. Only the pass's first draw keeps nothing (`WorkCounts::draws_unkept`, as it begins +/// the pass's instance), and no draw finds its kept draw unlike it or stops its rebind. And a draw +/// that keeps nothing (one that writes memory, `cfb_dn_count`, counted so) forgets the other +/// state's kept draw: draws switching with it take every step. #[test] fn draws_switching_pipeline_states_rebind_each_state_s_last_draw() { let scratch = Scratch::new("draw-again-states"); @@ -795,6 +797,10 @@ fn draws_switching_pipeline_states_rebind_each_state_s_last_draw() { assert_eq!(rebound.words, every.words, "the pixels of the draws"); let last = SIDE as usize - 1; assert_ne!(rebound.words[0], rebound.words[last], "the draws' offsets"); + assert_eq!(rebound.counted(|counts| counts.draws_unkept), 1); + assert_eq!(rebound.counted(|counts| counts.draws_unkept_instance), 1); + let missed = |counts: &WorkCounts| counts.draws_unlike_kept + counts.draws_rebind_stopped; + assert_eq!(rebound.counted(missed), 0); // Between the adding state's draws, draws that count their fragments in a buffer. let counting = state_counting(&device); let counter = device.new_buffer(4, ResourceOptions::shared()).unwrap(); @@ -817,6 +823,7 @@ fn draws_switching_pipeline_states_rebind_each_state_s_last_draw() { }); assert_eq!(ran.counted(rebound_count), 0); assert_eq!(ran.counted(|counts| counts.draws_as_before), 0); + assert_eq!(ran.counted(|counts| counts.draws_unkept_writing), DRAWS / 2); let counted = DRAWS / 2 * SIDE * SIDE; assert_eq!(words_of(&counter), [counted as u32]); } diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index e564beb64..93767663b 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -598,6 +598,38 @@ struct CommandCounters { ycbcr_planes_images_bound: AtomicU64, residual_form_draws: AtomicU64, residual_form_makings: AtomicU64, + draws_unkept: AtomicU64, + draws_unkept_instance: AtomicU64, + draws_unkept_barrier: AtomicU64, + draws_unkept_shadowed: AtomicU64, + draws_unkept_tables: AtomicU64, + draws_unkept_index_copied: AtomicU64, + draws_unkept_writing: AtomicU64, + draws_unkept_making: AtomicU64, + draws_unkept_other: AtomicU64, + draws_unlike_kept: AtomicU64, + draws_rebind_stopped: AtomicU64, +} + +/// What kept a direct draw that recorded from being kept to draw again +/// (`WorkCounts::draws_unkept`): each a group of the conditions a kept draw meets +/// (`render/draw_again.rs`), any number of them at once. +#[derive(Clone, Copy, Default)] +pub(crate) struct UnkeptDraw { + /// A step of its own beside its Vulkan draw, or a new render pass instance. + pub(crate) instance: bool, + /// A barrier in its instance. + pub(crate) barrier: bool, + /// A shadowed texture among its uses, a snapshot or an extended-range shadow. + pub(crate) shadowed: bool, + /// A heap, texture or sampler table, a root table, a push hole, or argument samplers unlisted. + pub(crate) tables: bool, + /// Its index buffer read through a copy. + pub(crate) index_copied: bool, + /// A use that writes, or reads indirect arguments. + pub(crate) writing: bool, + /// Its `VkPipeline` still being made. + pub(crate) making: bool, } /// The command counts by who adds them ([`CommandCounters`]): open encoders' tallies, and @@ -961,6 +993,44 @@ impl Counting<'_> { Self::kept_draw(&counts, kept); } + /// A render encoder's direct draw that recorded and kept nothing to draw again, for `why` + /// (`WorkCounts::draws_unkept`). + pub(crate) fn draw_unkept(&self, why: UnkeptDraw) { + let counts = self.0; + counts.add(|counts| &counts.draws_unkept, 1); + let groups: [(bool, fn(&CommandCounters) -> &AtomicU64); 7] = [ + (why.instance, |c| &c.draws_unkept_instance), + (why.barrier, |c| &c.draws_unkept_barrier), + (why.shadowed, |c| &c.draws_unkept_shadowed), + (why.tables, |c| &c.draws_unkept_tables), + (why.index_copied, |c| &c.draws_unkept_index_copied), + (why.writing, |c| &c.draws_unkept_writing), + (why.making, |c| &c.draws_unkept_making), + ]; + let mut any = false; + for (met, count) in groups { + if met { + counts.add(count, 1); + any = true; + } + } + if !any { + counts.add(|c| &c.draws_unkept_other, 1); + } + } + + /// A render encoder's direct draw after its encoder kept a draw, of another call, arguments, + /// pipeline state, command buffer or `Under` (`WorkCounts::draws_unlike_kept`). + pub(crate) fn draw_unlike_kept(&self) { + self.0.add(|counts| &counts.draws_unlike_kept, 1); + } + + /// A render encoder's direct draw of its kept draw again after calls, that took every step + /// (`WorkCounts::draws_rebind_stopped`). + pub(crate) fn draw_rebind_stopped(&self) { + self.0.add(|counts| &counts.draws_rebind_stopped, 1); + } + /// A render draw rebound that was moved (`WorkCounts::draws_moved`). pub(crate) fn draw_moved(&self) { self.0.add(|counts| &counts.draws_moved, 1); @@ -5808,6 +5878,44 @@ pub struct WorkCounts { /// instance's gathered uses, so the draw notes, adds and gathers none of them, comparing none /// (`uses_noted_covered` counts none of them). pub draws_moved_inside: u64, + /// Render encoders' direct draws that recorded a Vulkan draw and kept nothing to draw again + /// or rebind (`render/draw_again.rs`), so their encoder's next draw takes every step; with, + /// in the counts below, what kept each from being kept, a draw counted in every group it met + /// (the conditions `draws_as_before` lists). A draw replayed from an execution's commands is + /// not counted. + pub draws_unkept: u64, + /// Of `draws_unkept`, those that took a step of their own beside their Vulkan draw (a copy, + /// rewrite, index bounds, stand-in, mesh or tessellation work, a pipeline being made inline) + /// or recorded into a new render pass instance, as a pass's first draw does. + pub draws_unkept_instance: u64, + /// Of `draws_unkept`, those that recorded after a barrier in their render pass instance. + pub draws_unkept_barrier: u64, + /// Of `draws_unkept`, those with a shadowed texture among their uses, or that read through a + /// snapshot or an extended-range color's shadow. + pub draws_unkept_shadowed: u64, + /// Of `draws_unkept`, those that read a heap's set, a texture or sampler table, a root table + /// or a push hole, or whose argument samplers left states out. + pub draws_unkept_tables: u64, + /// Of `draws_unkept`, those that read their index buffer through a copy. + pub draws_unkept_index_copied: u64, + /// Of `draws_unkept`, those with a use that writes memory or reads indirect arguments. + pub draws_unkept_writing: u64, + /// Of `draws_unkept`, those that recorded against a `VkPipeline` still being made. + pub draws_unkept_making: u64, + /// Of `draws_unkept`, those that met none of the groups above (another condition a kept draw + /// meets: a split, a color group, an extended-range color, a rate map, a notice announced at + /// each occurrence, among others). + pub draws_unkept_other: u64, + /// Render encoders' direct draws whose encoder kept a draw, of another call, arguments, + /// index buffer or offset, pipeline state or command buffer, or after a no-copy buffer moved, + /// the residency, host aliases, parts or held images or the sampler IDs changed + /// (`render/draw_again.rs`): each takes every step. + pub draws_unlike_kept: u64, + /// Render encoders' direct draws of their kept draw again after calls, that took every step + /// rather than being rebound (`draws_rebound`): a kept draw that keeps nothing to rebind, or a + /// rebind that stopped (`render/draw_again/rebound.rs`'s stops). Builds with debug + /// assertions' checked draws are not counted. + pub draws_rebind_stopped: 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, or lists @@ -7544,6 +7652,17 @@ impl Device { draws_rebound: commands(|counts| &counts.draws_rebound), draws_moved: commands(|counts| &counts.draws_moved), draws_moved_inside: commands(|counts| &counts.draws_moved_inside), + draws_unkept: commands(|counts| &counts.draws_unkept), + draws_unkept_instance: commands(|counts| &counts.draws_unkept_instance), + draws_unkept_barrier: commands(|counts| &counts.draws_unkept_barrier), + draws_unkept_shadowed: commands(|counts| &counts.draws_unkept_shadowed), + draws_unkept_tables: commands(|counts| &counts.draws_unkept_tables), + draws_unkept_index_copied: commands(|counts| &counts.draws_unkept_index_copied), + draws_unkept_writing: commands(|counts| &counts.draws_unkept_writing), + draws_unkept_making: commands(|counts| &counts.draws_unkept_making), + draws_unkept_other: commands(|counts| &counts.draws_unkept_other), + draws_unlike_kept: commands(|counts| &counts.draws_unlike_kept), + draws_rebind_stopped: commands(|counts| &counts.draws_rebind_stopped), resident_uses_made: commands(|counts| &counts.resident_uses_made), resident_keys_settled: commands(|counts| &counts.resident_keys_settled), resident_uses_gathered: commands(|counts| &counts.resident_uses_gathered), diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 8fa3850c8..4d27a4983 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2684,8 +2684,19 @@ field metal2vulkan-runtime::WorkCounts.draws_dropped_unmade pub draws_dropped_un field metal2vulkan-runtime::WorkCounts.draws_moved pub draws_moved: u64 field metal2vulkan-runtime::WorkCounts.draws_moved_inside pub draws_moved_inside: u64 field metal2vulkan-runtime::WorkCounts.draws_pushing_set_zero pub draws_pushing_set_zero: u64 +field metal2vulkan-runtime::WorkCounts.draws_rebind_stopped pub draws_rebind_stopped: u64 field metal2vulkan-runtime::WorkCounts.draws_rebound pub draws_rebound: u64 field metal2vulkan-runtime::WorkCounts.draws_settled pub draws_settled: u64 +field metal2vulkan-runtime::WorkCounts.draws_unkept pub draws_unkept: u64 +field metal2vulkan-runtime::WorkCounts.draws_unkept_barrier pub draws_unkept_barrier: u64 +field metal2vulkan-runtime::WorkCounts.draws_unkept_index_copied pub draws_unkept_index_copied: u64 +field metal2vulkan-runtime::WorkCounts.draws_unkept_instance pub draws_unkept_instance: u64 +field metal2vulkan-runtime::WorkCounts.draws_unkept_making pub draws_unkept_making: u64 +field metal2vulkan-runtime::WorkCounts.draws_unkept_other pub draws_unkept_other: u64 +field metal2vulkan-runtime::WorkCounts.draws_unkept_shadowed pub draws_unkept_shadowed: u64 +field metal2vulkan-runtime::WorkCounts.draws_unkept_tables pub draws_unkept_tables: u64 +field metal2vulkan-runtime::WorkCounts.draws_unkept_writing pub draws_unkept_writing: u64 +field metal2vulkan-runtime::WorkCounts.draws_unlike_kept pub draws_unlike_kept: u64 field metal2vulkan-runtime::WorkCounts.driver_diagnostics_withheld pub driver_diagnostics_withheld: u64 field metal2vulkan-runtime::WorkCounts.empty_memory_blocks_live pub empty_memory_blocks_live: u64 field metal2vulkan-runtime::WorkCounts.encoder_command_buffers_begun pub encoder_command_buffers_begun: u64