From 8158fdf96365d554669319adf526630f4cb18b1f Mon Sep 17 00:00:00 2001 From: Personed Date: Wed, 25 Nov 2026 03:03:24 -0300 Subject: [PATCH] Plan checked mesh-compute dispatch sizes --- .../victory-shader/src/emit/mesh_compute.rs | 69 ++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/crates/victory-shader/src/emit/mesh_compute.rs b/crates/victory-shader/src/emit/mesh_compute.rs index f7123e7..f3ace74 100644 --- a/crates/victory-shader/src/emit/mesh_compute.rs +++ b/crates/victory-shader/src/emit/mesh_compute.rs @@ -44,6 +44,17 @@ pub struct MeshComputeLayout { pub primitive_params: Vec<(u32, u64)>, } +/// Checked resource and draw sizes for one mesh-compute dispatch. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct MeshComputeDispatch { + /// Number of mesh workgroups represented by this dispatch. + pub groups: u32, + /// Bytes needed for all workgroup output slots. + pub output_bytes: u64, + /// Vertex count for the fallback's single non-indexed triangle draw. + pub draw_vertices: u32, +} + /// Computes the 16-byte-aligned slot layout used by the mesh-as-compute /// fallback. Parameter locations are sorted and duplicate rows are stored /// only once. @@ -121,9 +132,29 @@ pub fn mesh_compute_layout( }) } +/// Computes the output-buffer allocation and non-indexed draw count for +/// `groups` workgroups. Returns `None` if the allocation overflows `u64` or +/// the draw count cannot be represented by D3D12's `DrawInstanced` arguments. +pub fn mesh_compute_dispatch( + layout: &MeshComputeLayout, + groups: u32, +) -> Option { + let output_bytes = layout.slot_bytes.checked_mul(u64::from(groups))?; + let draw_vertices = u64::from(groups) + .checked_mul(u64::from(layout.max_primitives))? + .checked_mul(3)?; + Some(MeshComputeDispatch { + groups, + output_bytes, + draw_vertices: u32::try_from(draw_vertices).ok()?, + }) +} + #[cfg(test)] mod tests { - use super::{MeshComputeLayout, mesh_compute_layout}; + use super::{ + MeshComputeDispatch, MeshComputeLayout, mesh_compute_dispatch, mesh_compute_layout, + }; #[test] fn aligns_records_and_sorts_unique_parameter_locations() { @@ -162,4 +193,40 @@ mod tests { fn rejects_layouts_whose_size_overflows() { assert!(mesh_compute_layout(u32::MAX, u32::MAX, u32::MAX, 0, &[], &[]).is_none()); } + + #[test] + fn sizes_the_output_buffer_and_raster_draw_for_all_groups() { + let layout = mesh_compute_layout(2, 3, 5, 1, &[8, 2, 8], &[6, 4, 6]).unwrap(); + assert_eq!( + mesh_compute_dispatch(&layout, 4), + Some(MeshComputeDispatch { + groups: 4, + output_bytes: 1408, + draw_vertices: 36, + }) + ); + } + + #[test] + fn zero_groups_need_no_buffer_or_draw() { + let layout = mesh_compute_layout(2, 3, 0, 0, &[], &[]).unwrap(); + assert_eq!( + mesh_compute_dispatch(&layout, 0), + Some(MeshComputeDispatch { + groups: 0, + output_bytes: 0, + draw_vertices: 0, + }) + ); + } + + #[test] + fn rejects_overflow_and_unrepresentable_draw_counts() { + let mut layout = mesh_compute_layout(1, 1, 0, 0, &[], &[]).unwrap(); + layout.slot_bytes = u64::MAX; + assert!(mesh_compute_dispatch(&layout, 2).is_none()); + + let layout = mesh_compute_layout(1, u32::MAX, 0, 0, &[], &[]).unwrap(); + assert!(mesh_compute_dispatch(&layout, 1).is_none()); + } }