From 2491d49bf5d3e238b45e78095f1ce68d1bf7e0aa Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Thu, 15 Oct 2026 00:25:51 +0100 Subject: [PATCH] perf: reuse entity material batching scratch buffers --- crates/horizon-client/src/gfx.rs | 53 ++-- crates/horizon-client/src/gfx/entity_batch.rs | 229 ++++++++++++++++++ 2 files changed, 245 insertions(+), 37 deletions(-) create mode 100644 crates/horizon-client/src/gfx/entity_batch.rs diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 2b47e2a89..abe2da7dd 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -31,32 +31,8 @@ fn entity_shader() -> String { crate::overlay::wgsl_constants() + include_str!("entity.wgsl") } -// Separate special materials without changing entity order or their overlays. -fn batch_entities( - entities: &[Vertex], - additive: &[Vertex], - hand: &[Vertex], -) -> (Vec, [u32; 4]) { - let mut combined = Vec::with_capacity(entities.len() + additive.len() + hand.len()); - let mut counts = [0u32; 4]; - for (i, layer) in [0, 11, 12, 4].into_iter().enumerate() { - for quad in entities.as_chunks::<4>().0 { - let material = match quad[0].layer { - 11 => 11, - 12 => 12, - 4 => 4, - _ => 0, - }; - if material == layer { - combined.extend_from_slice(quad); - counts[i] += 1; - } - } - } - combined.extend_from_slice(additive); - combined.extend_from_slice(hand); - (combined, counts) -} +mod entity_batch; +use entity_batch::EntityBatch; /// Window.getAppropriateLineWidth, in framebuffer pixels. pub fn outline_width(width: u32) -> f32 { @@ -263,6 +239,7 @@ pub struct Renderer { hand_globals: wgpu::Buffer, entity_buffer: wgpu::Buffer, entity_capacity: u64, + entity_batch: EntityBatch, group: wgpu::BindGroup, globals: wgpu::Buffer, lightmap: wgpu::Texture, @@ -1413,6 +1390,7 @@ impl Renderer { hand_globals, entity_buffer, entity_capacity, + entity_batch: EntityBatch::default(), group, globals, lightmap, @@ -2117,18 +2095,17 @@ impl Renderer { self.queue .write_buffer(&self.outline, 0, bytemuck::cast_slice(&lines)); } - let ( - combined, - [ - entity_quads, - crumbling_quads, - translucent_crumbling_quads, - cloud_quads, - ], - ) = batch_entities(frame.entities, frame.additive, frame.hand); + let [ + entity_quads, + crumbling_quads, + translucent_crumbling_quads, + cloud_quads, + ] = self + .entity_batch + .update(frame.entities, frame.additive, frame.hand); let world_quads = entity_quads + crumbling_quads + translucent_crumbling_quads + cloud_quads; - let all_entities = &combined; + let all_entities = &self.entity_batch.combined; let hand_quads = (frame.hand.len() / 4) as u32; let additive_quads = (frame.additive.len() / 4) as u32; let entity_vertices = all_entities.len() as u64; @@ -2612,7 +2589,9 @@ mod tests { .into_iter() .flat_map(|(layer, id)| quad(layer, id)) .collect(); - let (vertices, counts) = super::batch_entities(&entities, &quad(3, 5), &quad(0, 6)); + let mut batch = super::EntityBatch::default(); + let counts = batch.update(&entities, &quad(3, 5), &quad(0, 6)); + let vertices = &batch.combined; assert_eq!(counts, [2, 1, 1, 2]); assert_eq!( vertices diff --git a/crates/horizon-client/src/gfx/entity_batch.rs b/crates/horizon-client/src/gfx/entity_batch.rs new file mode 100644 index 000000000..484b0b7bc --- /dev/null +++ b/crates/horizon-client/src/gfx/entity_batch.rs @@ -0,0 +1,229 @@ +//! Stable entity material grouping with renderer-owned, reusable CPU scratch. +use super::Vertex; + +#[derive(Default)] +pub(super) struct EntityBatch { + // Quad indices avoid copying vertices twice while grouping materials. + // Keep initialized high-water storage; local counts delimit active entries. + // Each bucket can hold all quads, removing per-quad Vec growth checks. + buckets: [Vec; 4], + pub(super) combined: Vec, +} + +impl EntityBatch { + pub(super) fn update( + &mut self, + entities: &[Vertex], + additive: &[Vertex], + hand: &[Vertex], + ) -> [u32; 4] { + let quads = entities.as_chunks::<4>().0; + for bucket in &mut self.buckets { + if bucket.len() < quads.len() { + bucket.resize(quads.len(), 0); + } + } + let mut counts = [0usize; 4]; + for (index, quad) in quads.iter().enumerate() { + let material = match quad[0].layer { + 11 => 1, + 12 => 2, + 4 => 3, + _ => 0, + }; + self.buckets[material][counts[material]] = index; + counts[material] += 1; + } + let entity_vertices = quads.len() * 4; + self.combined.reserve( + (entity_vertices + additive.len() + hand.len()).saturating_sub(self.combined.len()), + ); + self.combined + .resize(entity_vertices, bytemuck::Zeroable::zeroed()); + if counts.contains(&quads.len()) { + // Uniform materials are already ordered; copy the whole batch. + self.combined.copy_from_slice(&entities[..entity_vertices]); + } else { + let mut offset = 0; + for (bucket, &count) in self.buckets.iter().zip(&counts) { + let bucket = &bucket[..count]; + let end = offset + count * 4; + for (output, &index) in self.combined[offset..end] + .as_chunks_mut::<4>() + .0 + .iter_mut() + .zip(bucket) + { + *output = quads[index]; + } + offset = end; + } + } + self.combined.extend_from_slice(additive); + self.combined.extend_from_slice(hand); + counts.map(|count| count as u32) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::{hint::black_box, time::Instant}; + + // Retain the original implementation as the equivalence/benchmark oracle. + fn baseline( + entities: &[Vertex], + additive: &[Vertex], + hand: &[Vertex], + ) -> (Vec, [u32; 4]) { + let mut combined = Vec::with_capacity(entities.len() + additive.len() + hand.len()); + let mut counts = [0; 4]; + for (i, layer) in [0, 11, 12, 4].into_iter().enumerate() { + for quad in entities.as_chunks::<4>().0 { + let material = match quad[0].layer { + 11 => 11, + 12 => 12, + 4 => 4, + _ => 0, + }; + if material == layer { + combined.extend_from_slice(quad); + counts[i] += 1; + } + } + } + combined.extend_from_slice(additive); + combined.extend_from_slice(hand); + (combined, counts) + } + + fn geometry(quads: usize, mixed: bool) -> Vec { + (0..quads * 4) + .map(|index| Vertex { + pos: [index as f32, 2.0, 3.0], + uv: [index as f32, 5.0], + layer: if mixed { + [7, 11, 12, 4, 0][index / 4 % 5] + } else { + 0 + }, + color: index as u32, + light: (index * 3) as u32, + normal: [1, 2, 3, 4], + }) + .collect() + } + + fn equivalent( + batch: &mut EntityBatch, + entities: &[Vertex], + additive: &[Vertex], + hand: &[Vertex], + ) { + let (expected, counts) = baseline(entities, additive, hand); + assert_eq!(batch.update(entities, additive, hand), counts); + assert_eq!( + bytemuck::cast_slice::<_, u8>(&batch.combined), + bytemuck::cast_slice::<_, u8>(&expected) + ); + } + + #[test] + fn stable_material_order_tails_and_reuse() { + let mut batch = EntityBatch::default(); + let mut entities = geometry(4096, true); + // Only the first vertex determines a quad's material. + entities[1].layer = 12; + let additive = geometry(3, true); + let hand = geometry(2, false); + // Warm every bucket and the combined buffer to their maximum sizes. + equivalent(&mut batch, &entities, &additive, &hand); + let capacities = batch.buckets.each_ref().map(|b| b.capacity()); + let capacity = batch.combined.capacity(); + let pointer = batch.combined.as_ptr(); + for length in [0, 1, 3, 4, 511, entities.len(), 17, 0, entities.len()] { + equivalent(&mut batch, &entities[..length], &additive[..5], &hand[..3]); + assert_eq!(batch.buckets.each_ref().map(|b| b.capacity()), capacities); + assert_eq!(batch.combined.capacity(), capacity); + assert_eq!(batch.combined.as_ptr(), pointer); + } + equivalent(&mut batch, &[], &[], &[]); + assert!(batch.combined.is_empty()); + // Grow again, then confirm empty/small frames retain the new storage. + let larger = geometry(8192, true); + equivalent(&mut batch, &larger, &additive, &hand); + let capacities = batch.buckets.each_ref().map(|b| b.capacity()); + let capacity = batch.combined.capacity(); + for entities in [&larger[..7], &larger[..], &[]] { + equivalent(&mut batch, entities, &[], &[]); + assert_eq!(batch.buckets.each_ref().map(|b| b.capacity()), capacities); + assert_eq!(batch.combined.capacity(), capacity); + } + } + + #[test] + #[ignore = "manual production-helper microbenchmark"] + fn benchmark_entity_batch() { + let additive = geometry(16, false); + let hand = geometry(8, false); + for (name, mixed, quads) in [ + ("mixed", true, 65_536), + ("single", false, 65_536), + ("runs", true, 65_536), + ("mixed", true, 262_144), + ("single", false, 262_144), + ] { + let mut entities = geometry(quads, mixed); + if name == "runs" { + // Model/material runs: normal 16, glint 4, overlay 4, shadow 2. + for (index, quad) in entities.as_chunks_mut::<4>().0.iter_mut().enumerate() { + let layer = match index % 26 { + 0..16 => 0, + 16..20 => 11, + 20..24 => 12, + _ => 4, + }; + for vertex in quad { + vertex.layer = layer; + } + } + } + let mut batch = EntityBatch::default(); + equivalent(&mut batch, &entities, &additive, &hand); + for _ in 0..50 { + black_box(baseline(black_box(&entities), &additive, &hand)); + black_box(batch.update(black_box(&entities), &additive, &hand)); + black_box(&batch.combined); + } + let mut before = Vec::new(); + let mut after = Vec::new(); + for _ in 0..15 { + let start = Instant::now(); + for _ in 0..100 { + black_box(baseline(black_box(&entities), &additive, &hand)); + } + before.push(start.elapsed().as_nanos() / 100); + let start = Instant::now(); + for _ in 0..100 { + black_box(batch.update(black_box(&entities), &additive, &hand)); + black_box(&batch.combined); + } + after.push(start.elapsed().as_nanos() / 100); + } + before.sort_unstable(); + after.sort_unstable(); + println!( + "{name}: {quads} quads, baseline={} ns/frame optimized={} ns/frame speedup={:.2}x scratch={} bytes", + before[7], + after[7], + before[7] as f64 / after[7] as f64, + batch.combined.capacity() * std::mem::size_of::() + + batch + .buckets + .iter() + .map(|b| b.capacity() * std::mem::size_of::()) + .sum::() + ); + } + } +}