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

@ -0,0 +1,93 @@
// Face3D GPU buffer — batches all DXF 3DFACE entities into a single
// TriangleList buffer for efficient rendering.
//
// Each Face3D quad (4 corners) produces 2 triangles → 6 vertices.
// All entities are merged into one wgpu::Buffer → 1 draw call total.
//
// Vertex layout (28 bytes):
// position [f32; 3] offset 0 12 B
// color [f32; 4] offset 12 16 B
// ------
// 28 B / vertex
use crate::scene::wire_model::WireModel;
use iced::wgpu;
use iced::wgpu::util::DeviceExt;
// ── Vertex layout ──────────────────────────────────────────────────────────
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Face3DVertex {
pub position: [f32; 3],
pub color: [f32; 4],
}
impl Face3DVertex {
pub fn layout<'a>() -> wgpu::VertexBufferLayout<'a> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Face3DVertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: 12,
shader_location: 1,
format: wgpu::VertexFormat::Float32x4,
},
],
}
}
}
// ── GPU handle ─────────────────────────────────────────────────────────────
pub struct Face3DGpu {
pub vertex_buffer: wgpu::Buffer,
pub vertex_count: u32,
}
impl Face3DGpu {
/// Build a batched GPU buffer from Face3D wire models.
///
/// Each WireModel's `key_vertices` holds the 4 corners in local space
/// (world_offset already applied by tessellate.rs). Two triangles are
/// emitted per face: (p0,p1,p2) and (p0,p2,p3).
pub fn from_wires(device: &wgpu::Device, wires: &[WireModel]) -> Self {
let mut vertices: Vec<Face3DVertex> = Vec::with_capacity(wires.len() * 6);
for wire in wires {
// key_vertices has exactly 4 entries for Face3D (p0..p3).
if wire.key_vertices.len() < 4 {
continue;
}
let color = wire.color;
let p = &wire.key_vertices;
let v = |i: usize| Face3DVertex { position: p[i], color };
// Triangle 1: p0, p1, p2
vertices.push(v(0));
vertices.push(v(1));
vertices.push(v(2));
// Triangle 2: p0, p2, p3
vertices.push(v(0));
vertices.push(v(2));
vertices.push(v(3));
}
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("face3d.vbuf"),
contents: bytemuck::cast_slice(&vertices),
usage: wgpu::BufferUsages::VERTEX,
});
Self {
vertex_buffer,
vertex_count: vertices.len() as u32,
}
}
}