perf: dedicated Face3D shader — N draw calls → 2

3DFACE entities previously created one WireGpu buffer per entity,
resulting in N separate draw calls per frame. For files with large
triangle meshes this was the dominant GPU CPU overhead.

New pipeline:
- face3d.wgsl: simple flat-color vertex + fragment shader (MVP only,
  no quad expansion, no pattern distance computation)
- Face3DGpu: batches ALL Face3D entities into a single TriangleList
  buffer (6 vertices per face, per-vertex color) — 1 draw call
- WireGpu::from_batch: merges N Face3D edge wire models into one
  quad-expanded buffer for edge rendering — 1 draw call
- split_face3d_wires: separates face3d from other wires at Primitive
  build time via document handle lookup (O(N), once per epoch)

Hit-testing via WireModel (CPU) is unaffected — face3d wires remain
in entity_wires_arc() and hit_test_wires() as before.

Before: N 3DFACE → N draw calls, N × 96 B/vertex WireVertex
After:  N 3DFACE → 2 draw calls (fill + edges), 28 B/vertex fill

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Hakan Seven 2026-04-28 21:53:31 +03:00
commit 298098e764
5 changed files with 385 additions and 4 deletions

View file

@ -125,6 +125,82 @@ impl WireGpu {
Self::build(device, wire, [r, g, b, a * alpha])
}
/// Merge multiple WireModels into a single GPU buffer (1 draw call for all).
/// Each wire keeps its own color and pattern — they're stored per-vertex.
/// Returns None if the combined vertex list is empty.
pub fn from_batch(device: &wgpu::Device, wires: &[WireModel]) -> Option<Self> {
let total_segs: usize = wires.iter().map(|w| w.points.len().saturating_sub(1)).sum();
if total_segs == 0 {
return None;
}
let mut vertices: Vec<WireVertex> = Vec::with_capacity(total_segs * 6);
for wire in wires {
let color = wire.color;
let pat0 = [wire.pattern[0], wire.pattern[1], wire.pattern[2], wire.pattern[3]];
let pat1 = [wire.pattern[4], wire.pattern[5], wire.pattern[6], wire.pattern[7]];
let half_width = wire.line_weight_px * 0.5;
let n = wire.points.len();
// Cumulative arc-length per point.
let mut dists = vec![0.0_f32; n];
for i in 1..n {
let p = wire.points[i - 1];
let q = wire.points[i];
if !p[0].is_finite() || !q[0].is_finite() {
dists[i] = dists[i - 1];
} else {
let dx = q[0] - p[0];
let dy = q[1] - p[1];
let dz = q[2] - p[2];
dists[i] = dists[i - 1] + (dx * dx + dy * dy + dz * dz).sqrt();
}
}
for i in 0..n.saturating_sub(1) {
let a = wire.points[i];
let b = wire.points[i + 1];
if !a[0].is_finite() || !a[1].is_finite() || !a[2].is_finite()
|| !b[0].is_finite() || !b[1].is_finite() || !b[2].is_finite()
{
continue;
}
let dist_a = dists[i];
let dist_b = dists[i + 1];
let make = |which_end: f32, side: f32| -> WireVertex {
let dist = if which_end < 0.5 { dist_a } else { dist_b };
WireVertex {
pos_a: a, pos_b: b, which_end, side, color,
distance: dist, half_width,
pattern_length: wire.pattern_length,
_pad: 0.0, pat0, pat1,
}
};
vertices.push(make(0.0, -1.0));
vertices.push(make(1.0, -1.0));
vertices.push(make(1.0, 1.0));
vertices.push(make(0.0, -1.0));
vertices.push(make(1.0, 1.0));
vertices.push(make(0.0, 1.0));
}
}
if vertices.is_empty() {
return None;
}
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("wire.batch.vbuf"),
contents: bytemuck::cast_slice(&vertices),
usage: wgpu::BufferUsages::VERTEX,
});
Some(Self {
vertex_buffer,
vertex_count: vertices.len() as u32,
})
}
fn build(device: &wgpu::Device, wire: &WireModel, color: [f32; 4]) -> Self {
let pat0 = [
wire.pattern[0],