diff --git a/crates/metal2vulkan-runtime/src/render/draw_again.rs b/crates/metal2vulkan-runtime/src/render/draw_again.rs index 47940ea69..dcda0cb8a 100644 --- a/crates/metal2vulkan-runtime/src/render/draw_again.rs +++ b/crates/metal2vulkan-runtime/src/render/draw_again.rs @@ -117,6 +117,7 @@ use crate::noted_apart::NotedInto; use crate::pipeline::{RenderPipelineState, RenderStateKind, WeakRenderPipelineState}; use crate::resource::ResourceKey; use crate::tracker::{Use, UseRange}; +use crate::work_counts::UnlikeKept; use metal2vulkan::metal::RenderStages; /// The encoder's last direct draw, where it is kept (the module docs). @@ -207,6 +208,11 @@ pub(super) struct KeptIndex { } impl KeptIndex { + /// The Metal buffer, by key, and the `VkBuffer` it is read from, its offsets aside. + fn buffer(self) -> (ResourceKey, vk::Buffer) { + (self.key, self.raw) + } + /// `source`'s, where it is read in place; `None` for a copy, which each draw takes. pub(super) fn of(source: &DrawSource<'_>) -> Option { source.in_place().then(|| KeptIndex { @@ -218,6 +224,39 @@ impl KeptIndex { } } +/// How `draw` differs from `kept`, a kept draw's call: in its counts and starts, in its index +/// buffer's offset, and in its index buffer or index type. +fn apart(kept: &DrawCall, draw: &DrawCall) -> (bool, bool, bool) { + match (kept, draw) { + ( + DrawCall::Indexed { + index_type: kept_type, + index_buffer: kept_buffer, + index_count: kept_count, + index_offset: kept_offset, + instance_count: kept_instances, + base_vertex: kept_base, + base_instance: kept_first, + }, + DrawCall::Indexed { + index_type, + index_buffer, + index_count, + index_offset, + instance_count, + base_vertex, + base_instance, + }, + ) => ( + (kept_count, kept_instances, kept_base, kept_first) + != (index_count, instance_count, base_vertex, base_instance), + kept_offset != index_offset, + kept_type != index_type || kept_buffer != index_buffer, + ), + (kept, draw) => (kept != draw, false, false), + } +} + /// An execution record's parts (`Execution::new`): the same where its pipeline and shape are /// the same objects. pub(super) struct KeptExecution { @@ -405,7 +444,24 @@ impl RenderLowering { && kept.under == self.under(); if !same { if buffers.indirect.is_none() { - self.core.counts().draw_unlike_kept(); + let (counts, index_offset, index_buffer) = apart(&kept.draw, draw); + let copied = matches!(index, Some(None)); + let index = index.flatten(); + let index_buffer = index_buffer + || copied + || kept.index.map(KeptIndex::buffer) != index.map(KeptIndex::buffer); + let why = UnlikeKept { + pipeline: !self.pipeline.as_ref().is_some_and(drawn_by), + call: kept.call != call || kept.assembly != assembly, + counts, + index_buffer, + index_offset: !index_buffer && (index_offset || kept.index != index), + tessellation: buffers.patch_indices.is_some() + || buffers.control_point_indices.is_some(), + command_buffer: kept.command_buffer != self.current.draws, + under: kept.under != self.under(), + }; + self.core.counts().draw_unlike_kept(why); } return Again::No; } diff --git a/crates/metal2vulkan-runtime/src/tests/draw_again.rs b/crates/metal2vulkan-runtime/src/tests/draw_again.rs index 61ddaf209..76c2cd05c 100644 --- a/crates/metal2vulkan-runtime/src/tests/draw_again.rs +++ b/crates/metal2vulkan-runtime/src/tests/draw_again.rs @@ -1438,3 +1438,49 @@ fn draws_after_a_compositor_s_gaps_rebind_what_every_step_binds() { let missed = |counts: &WorkCounts| counts.draws_unlike_kept + counts.draws_rebind_stopped; assert_eq!(rebound.counted(missed), 0); } + +/// A draw unlike the draw its encoder kept is counted in each part it differed by +/// (`WorkCounts::draws_unlike_kept_*`): the pass's first draw kept, a draw of other counts +/// alone, its arguments alone (`draws_unlike_kept_arguments_alone`), and then one of another +/// primitive type, which only its assembly sets apart from that one. +#[test] +fn a_draw_unlike_its_kept_draw_is_counted_by_what_differed() { + let scratch = Scratch::new("draw-again-unlike"); + let device = device(&scratch.0, &[], &Heard::default()); + let state = adding_state(&device); + let draws = |encoder: &mut RenderCommandEncoder| { + set_rectangle(encoder, &state); + rectangle(encoder); + // Its first triangle: other counts alone. + let drawn = encoder.draw_primitives(PrimitiveType::Triangle, 0, 3, 1, 0); + assert_eq!(drawn, Ok(())); + // The same three vertices as a strip: another assembly alone. + let drawn = encoder.draw_primitives(PrimitiveType::TriangleStrip, 0, 3, 1, 0); + assert_eq!(drawn, Ok(())); + }; + warm(&device, draws); + let ran = run(&device, draws); + let parts: [(&str, fn(&WorkCounts) -> u64, u64); 10] = [ + ("unlike", |c| c.draws_unlike_kept, 2), + ("counts", |c| c.draws_unlike_kept_counts, 1), + ( + "arguments alone", + |c| c.draws_unlike_kept_arguments_alone, + 1, + ), + ("call", |c| c.draws_unlike_kept_call, 1), + ("pipeline", |c| c.draws_unlike_kept_pipeline, 0), + ("index buffer", |c| c.draws_unlike_kept_index_buffer, 0), + ("index offset", |c| c.draws_unlike_kept_index_offset, 0), + ("tessellation", |c| c.draws_unlike_kept_tessellation, 0), + ("command buffer", |c| c.draws_unlike_kept_command_buffer, 0), + ("under", |c| c.draws_unlike_kept_under, 0), + ]; + for (part, count, expected) in parts { + assert_eq!( + ran.counted(count), + expected, + "draws unlike their kept draw: {part}" + ); + } +} diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 56bd92455..6e2a73d31 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -624,6 +624,15 @@ struct CommandCounters { draws_unkept_making: AtomicU64, draws_unkept_other: AtomicU64, draws_unlike_kept: AtomicU64, + draws_unlike_kept_pipeline: AtomicU64, + draws_unlike_kept_call: AtomicU64, + draws_unlike_kept_counts: AtomicU64, + draws_unlike_kept_index_buffer: AtomicU64, + draws_unlike_kept_index_offset: AtomicU64, + draws_unlike_kept_tessellation: AtomicU64, + draws_unlike_kept_command_buffer: AtomicU64, + draws_unlike_kept_under: AtomicU64, + draws_unlike_kept_arguments_alone: AtomicU64, draws_rebind_stopped: AtomicU64, draws_restated: AtomicU64, } @@ -656,6 +665,30 @@ pub(crate) struct UnkeptDraw { pub(crate) making: bool, } +/// How a direct draw differed from the draw its encoder kept (`WorkCounts::draws_unlike_kept`): +/// each a part of what a draw again compares (`render/draw_again.rs`), any number of them at +/// once. +#[derive(Clone, Copy, Default)] +pub(crate) struct UnlikeKept { + /// Another pipeline state, or none. + pub(crate) pipeline: bool, + /// Another call or primitive assembly. + pub(crate) call: bool, + /// Other vertex or index counts or starts, instance count, base vertex or base instance. + pub(crate) counts: bool, + /// Another index buffer or index type, or an index buffer read through a copy. + pub(crate) index_buffer: bool, + /// The same index buffer at another offset. + pub(crate) index_offset: bool, + /// Patch or control-point index buffers (a tessellation draw). + pub(crate) tessellation: bool, + /// Another command buffer, the `VkCommandBuffer` its instance records into. + pub(crate) command_buffer: bool, + /// A no-copy buffer moved, or the residency, the host aliases, parts or held images, or the + /// sampler IDs changed (`Under`). + pub(crate) under: bool, +} + /// The command counts by who adds them ([`CommandCounters`]): open encoders' tallies, and /// threads' shards for the counts made with no encoder's core at hand. #[derive(Default)] @@ -1066,10 +1099,35 @@ impl Counting<'_> { } } - /// 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 after its encoder kept a draw, unlike it as `why` says + /// (`WorkCounts::draws_unlike_kept`). + pub(crate) fn draw_unlike_kept(&self, why: UnlikeKept) { + let counts = self.0; + counts.add(|counts| &counts.draws_unlike_kept, 1); + let arguments = why.counts || why.index_offset; + let alone = arguments + && !(why.pipeline + || why.call + || why.index_buffer + || why.tessellation + || why.command_buffer + || why.under); + let parts: [(bool, fn(&CommandCounters) -> &AtomicU64); 9] = [ + (why.pipeline, |c| &c.draws_unlike_kept_pipeline), + (why.call, |c| &c.draws_unlike_kept_call), + (why.counts, |c| &c.draws_unlike_kept_counts), + (why.index_buffer, |c| &c.draws_unlike_kept_index_buffer), + (why.index_offset, |c| &c.draws_unlike_kept_index_offset), + (why.tessellation, |c| &c.draws_unlike_kept_tessellation), + (why.command_buffer, |c| &c.draws_unlike_kept_command_buffer), + (why.under, |c| &c.draws_unlike_kept_under), + (alone, |c| &c.draws_unlike_kept_arguments_alone), + ]; + for (met, count) in parts { + if met { + counts.add(count, 1); + } + } } /// A render encoder's direct draw of its kept draw again after calls, that took every step @@ -6083,6 +6141,30 @@ pub struct WorkCounts { /// 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, + /// Of `draws_unlike_kept`, those through another pipeline state than the kept draw's (or + /// none); a draw is counted in each of the parts below it met. + pub draws_unlike_kept_pipeline: u64, + /// Of `draws_unlike_kept`, those of another call or primitive assembly. + pub draws_unlike_kept_call: u64, + /// Of `draws_unlike_kept`, those of other vertex or index counts or starts, instance count, + /// base vertex or base instance. + pub draws_unlike_kept_counts: u64, + /// Of `draws_unlike_kept`, those of another index buffer or index type, or whose index buffer + /// is read through a copy. + pub draws_unlike_kept_index_buffer: u64, + /// Of `draws_unlike_kept`, those of the same index buffer at another offset. + pub draws_unlike_kept_index_offset: u64, + /// Of `draws_unlike_kept`, tessellation draws: of patch or control-point index buffers. + pub draws_unlike_kept_tessellation: u64, + /// Of `draws_unlike_kept`, those recorded into another `VkCommandBuffer` than the kept draw. + pub draws_unlike_kept_command_buffer: u64, + /// Of `draws_unlike_kept`, those after a no-copy buffer moved, or the residency, the host + /// aliases, parts or held images, or the sampler IDs changed. + pub draws_unlike_kept_under: u64, + /// Of `draws_unlike_kept`, those unlike only by their counts and starts or their index + /// buffer's offset: the same pipeline state, call, assembly, index buffer and type, command + /// buffer and the rest the kept draw read. + pub draws_unlike_kept_arguments_alone: 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 @@ -7993,6 +8075,25 @@ impl Device { 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_unlike_kept_pipeline: commands(|counts| &counts.draws_unlike_kept_pipeline), + draws_unlike_kept_call: commands(|counts| &counts.draws_unlike_kept_call), + draws_unlike_kept_counts: commands(|counts| &counts.draws_unlike_kept_counts), + draws_unlike_kept_index_buffer: commands(|counts| { + &counts.draws_unlike_kept_index_buffer + }), + draws_unlike_kept_index_offset: commands(|counts| { + &counts.draws_unlike_kept_index_offset + }), + draws_unlike_kept_tessellation: commands(|counts| { + &counts.draws_unlike_kept_tessellation + }), + draws_unlike_kept_command_buffer: commands(|counts| { + &counts.draws_unlike_kept_command_buffer + }), + draws_unlike_kept_under: commands(|counts| &counts.draws_unlike_kept_under), + draws_unlike_kept_arguments_alone: commands(|counts| { + &counts.draws_unlike_kept_arguments_alone + }), 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), diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index d59316b76..a7c68a315 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2713,6 +2713,15 @@ field metal2vulkan-runtime::WorkCounts.draws_unkept_tables_writing pub draws_unk field metal2vulkan-runtime::WorkCounts.draws_unkept_writing pub draws_unkept_writing: u64 field metal2vulkan-runtime::WorkCounts.draws_unkept_writing_resident pub draws_unkept_writing_resident: u64 field metal2vulkan-runtime::WorkCounts.draws_unlike_kept pub draws_unlike_kept: u64 +field metal2vulkan-runtime::WorkCounts.draws_unlike_kept_arguments_alone pub draws_unlike_kept_arguments_alone: u64 +field metal2vulkan-runtime::WorkCounts.draws_unlike_kept_call pub draws_unlike_kept_call: u64 +field metal2vulkan-runtime::WorkCounts.draws_unlike_kept_command_buffer pub draws_unlike_kept_command_buffer: u64 +field metal2vulkan-runtime::WorkCounts.draws_unlike_kept_counts pub draws_unlike_kept_counts: u64 +field metal2vulkan-runtime::WorkCounts.draws_unlike_kept_index_buffer pub draws_unlike_kept_index_buffer: u64 +field metal2vulkan-runtime::WorkCounts.draws_unlike_kept_index_offset pub draws_unlike_kept_index_offset: u64 +field metal2vulkan-runtime::WorkCounts.draws_unlike_kept_pipeline pub draws_unlike_kept_pipeline: u64 +field metal2vulkan-runtime::WorkCounts.draws_unlike_kept_tessellation pub draws_unlike_kept_tessellation: u64 +field metal2vulkan-runtime::WorkCounts.draws_unlike_kept_under pub draws_unlike_kept_under: 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