From d3c3d502f3827eebe2ea6dc57619ee5363536675 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Fri, 13 Nov 2026 00:40:48 -0800 Subject: [PATCH] The merged form's first draw is empty where the total holds no whole chunk The last merge pass wrote the first draw as a chunk's 2^31 - 2 vertices in no instance whenever the total was below a chunk. It now writes no vertex in no instance there. The vertex variant reads the chunk from the header, not from the draw, so nothing else reads the change. Measured on the 2080 (NVIDIA 620.70.06, m3-max-24755): the 4-command trail test's command buffer went from 0.707 to 0.526 ms of GPU time. The 8162-command indexed one stayed at 18.6 ms, so a zero-instance chunk is not what costs that one. The interpreter's merge tests (50 bind::replay tests) and clippy pass (m3-max-24757). The trail tests' expected lines now read the first draw as 0 vertices x 0. --- .../metal2vulkan-runtime/src/tests/trail_gpu_forms.rs | 4 ++-- crates/metal2vulkan/src/bind/replay/draws/tests.rs | 6 +++++- crates/metal2vulkan/src/bind/replay/merged.rs | 10 +++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/tests/trail_gpu_forms.rs b/crates/metal2vulkan-runtime/src/tests/trail_gpu_forms.rs index 8cb6fd593..4af7f6312 100644 --- a/crates/metal2vulkan-runtime/src/tests/trail_gpu_forms.rs +++ b/crates/metal2vulkan-runtime/src/tests/trail_gpu_forms.rs @@ -178,7 +178,7 @@ fn a_merged_gpu_form_names_its_range_and_draws_as_its_scratch_holds_them() { "an execution's GPU form, recorded at commit: merged into one draw, of primitive type \ Triangle, over 4 command(s) from 0, its range read from a buffer at offset 0; read while \ the work is pending: the range copied 1+2,", - " the merged draws [2147483646 vertices x 0 from vertex 0, instance 0; 48 vertices x 1 \ + " the merged draws [0 vertices x 0 from vertex 0, instance 0; 48 vertices x 1 \ from vertex 2147483648, instance 0],", " 2 argument(s) that draw, 50 vertices or indices times instances, the most command 2's: \ 12 x 3 from 0, instance 0", @@ -240,7 +240,7 @@ fn an_indexed_merged_gpu_form_through_a_range_draws_no_whole_chunk_below_one() { "an execution's GPU form, recorded at commit: merged into one draw, indexed, of primitive \ type Triangle, over 8162 command(s) from 0, its range read from a buffer at offset 0; \ read while the work is pending: the range copied 0+86,", - " the merged draws [2147483646 vertices x 0 from vertex 0, instance 0; 22446 vertices x 1 \ + " the merged draws [0 vertices x 0 from vertex 0, instance 0; 22446 vertices x 1 \ from vertex 2147483648, instance 0],", " 86 argument(s) that draw, 22446 vertices or indices times instances, the most command \ 85's: 516 x 1 from 0, instance 0 (600 whole indices of 4 byte(s))", diff --git a/crates/metal2vulkan/src/bind/replay/draws/tests.rs b/crates/metal2vulkan/src/bind/replay/draws/tests.rs index cbe32e585..b9727fbb3 100644 --- a/crates/metal2vulkan/src/bind/replay/draws/tests.rs +++ b/crates/metal2vulkan/src/bind/replay/draws/tests.rs @@ -699,7 +699,11 @@ fn decoded_over( }; let [first_vertices, chunks, first_first, first_base] = draw(0); let [rest, rest_instances, rest_first, rest_base] = draw(1); - assert_eq!((first_vertices, first_first, first_base), (chunk, 0, 0)); + let first_chunk = if chunks == 0 { 0 } else { chunk }; + assert_eq!( + (first_vertices, first_first, first_base), + (first_chunk, 0, 0) + ); assert_eq!( (rest_instances, rest_first, rest_base), (1, MERGED_REST_FIRST, 0) diff --git a/crates/metal2vulkan/src/bind/replay/merged.rs b/crates/metal2vulkan/src/bind/replay/merged.rs index 92f6c12c4..2a0a97b3e 100644 --- a/crates/metal2vulkan/src/bind/replay/merged.rs +++ b/crates/metal2vulkan/src/bind/replay/merged.rs @@ -24,8 +24,8 @@ //! guest's two executions' totals read `2^33` above the sum of their commands' corners (no sum //! of whole triangles), so each first draw drew 4 chunks, seconds of vertices. //! - **The merged draw** is two draws, recorded in this order: the first draws -//! [`MERGED_CHUNK`] vertices in each of `⌊total / MERGED_CHUNK⌋` instances (none where the total -//! is below it), the second the remaining `total mod MERGED_CHUNK` vertices from first vertex +//! [`MERGED_CHUNK`] vertices in each of `⌊total / MERGED_CHUNK⌋` instances (where the total is +//! below a chunk, no vertex in no instance), the second the remaining `total mod MERGED_CHUNK` vertices from first vertex //! [`MERGED_REST_FIRST`] in one instance. An invocation's corner is `InstanceIndex · //! MERGED_CHUNK + VertexIndex` in the first and `⌊total / MERGED_CHUNK⌋ · MERGED_CHUNK + //! (VertexIndex - MERGED_REST_FIRST)` in the second (a `VertexIndex` at or past @@ -789,8 +789,12 @@ pub fn typed_module(phase: MergePhase) -> BResult { let chunks_at = at(fb, header, H_CHUNKS)?; let kept_chunks = fb.select(fits, chunks32, zero)?; store32(fb, t, chunks_at, kept_chunks)?; + // The first draw draws nothing at all, no vertex and no instance, where the total + // holds no whole chunk. + let whole_chunks = fb.binary(BinOp::INotEqual, chunks32, zero)?; + let first_vertices = fb.select(whole_chunks, chunk, zero)?; let words = [ - [chunk, chunks32, zero, zero], + [first_vertices, chunks32, zero, zero], [rest32, one, rest_first, zero], ]; for (d, w) in words.into_iter().enumerate() {