forked from 1b-agent/opencadstudio-upstream
fix(render): cap wire segments to prevent GPU memory exhaustion
Adds two-layer protection against large entities crashing the renderer: - MAX_SEGS_PER_WIRE (100K): per-entity hard cap, excess segments truncated - MAX_TOTAL_SEGS (3M): scene-level budget in upload_wires, excess wires skipped Both limits log a warning to stderr when triggered. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
1 parent
8e154e2e9e
commit
321433ee45
2 files changed
+51
-5
No files matched your search
@@ -600,7 +600,33 @@ impl Pipeline {
|
||||
}
|
||||
|
||||
pub fn upload_wires(&mut self, device: &wgpu::Device, wires: &[WireModel]) {
|
||||
self.gpu_wires = wires.iter().map(|w| WireGpu::new(device, w)).collect();
|
||||
// Total segment budget: prevents a large file from exhausting GPU memory.
|
||||
// Each segment → 6 GPU vertices × 96 bytes = 576 bytes; 3M segs ≈ 1.6 GB.
|
||||
const MAX_TOTAL_SEGS: usize = 3_000_000;
|
||||
let mut total_segs = 0usize;
|
||||
let mut skipped = 0usize;
|
||||
|
||||
self.gpu_wires = wires
|
||||
.iter()
|
||||
.filter(|w| {
|
||||
let segs = w.points.len().saturating_sub(1).min(wire_gpu::MAX_SEGS_PER_WIRE);
|
||||
if total_segs + segs > MAX_TOTAL_SEGS {
|
||||
skipped += 1;
|
||||
false
|
||||
} else {
|
||||
total_segs += segs;
|
||||
true
|
||||
}
|
||||
})
|
||||
.map(|w| WireGpu::new(device, w))
|
||||
.collect();
|
||||
|
||||
if skipped > 0 {
|
||||
eprintln!(
|
||||
"[H7CAD] upload_wires: skipped {skipped} wire(s) — total segment budget ({MAX_TOTAL_SEGS}) exceeded"
|
||||
);
|
||||
}
|
||||
|
||||
// Full-brightness copies of selected wires, drawn on top of everything
|
||||
// in the selection overlay pass so they're always visible.
|
||||
self.gpu_selected_wires = wires
|
||||
|
||||
@@ -27,6 +27,10 @@ use crate::scene::wire_model::WireModel;
|
||||
use iced::wgpu;
|
||||
use iced::wgpu::util::DeviceExt;
|
||||
|
||||
/// Hard cap on segments per wire entity. Entities with more segments are
|
||||
/// silently truncated to prevent a single complex object from exhausting GPU memory.
|
||||
pub const MAX_SEGS_PER_WIRE: usize = 100_000;
|
||||
|
||||
// ── Vertex layout ─────────────────────────────────────────────────────────
|
||||
|
||||
#[repr(C)]
|
||||
@@ -146,10 +150,19 @@ impl WireGpu {
|
||||
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();
|
||||
let raw_segs = n.saturating_sub(1);
|
||||
let capped_segs = raw_segs.min(MAX_SEGS_PER_WIRE);
|
||||
if raw_segs > MAX_SEGS_PER_WIRE {
|
||||
eprintln!(
|
||||
"[H7CAD] wire '{}': {} segments exceeds limit ({MAX_SEGS_PER_WIRE}), truncating",
|
||||
wire.name, raw_segs
|
||||
);
|
||||
}
|
||||
|
||||
// Cumulative arc-length per point.
|
||||
let mut dists = vec![0.0_f32; n];
|
||||
for i in 1..n {
|
||||
let pts_len = capped_segs + 1;
|
||||
let mut dists = vec![0.0_f32; pts_len];
|
||||
for i in 1..pts_len {
|
||||
let p = wire.points[i - 1];
|
||||
let q = wire.points[i];
|
||||
if !p[0].is_finite() || !q[0].is_finite() {
|
||||
@@ -167,7 +180,7 @@ impl WireGpu {
|
||||
}
|
||||
}
|
||||
|
||||
for i in 0..n.saturating_sub(1) {
|
||||
for i in 0..capped_segs {
|
||||
let a = wire.points[i];
|
||||
let b = wire.points[i + 1];
|
||||
if !a[0].is_finite() || !a[1].is_finite() || !a[2].is_finite()
|
||||
@@ -236,7 +249,14 @@ impl WireGpu {
|
||||
let half_width = wire.line_weight_px * 0.5;
|
||||
|
||||
let n = wire.points.len();
|
||||
let seg_count = if n >= 2 { n - 1 } else { 0 };
|
||||
let raw_seg_count = if n >= 2 { n - 1 } else { 0 };
|
||||
let seg_count = raw_seg_count.min(MAX_SEGS_PER_WIRE);
|
||||
if raw_seg_count > MAX_SEGS_PER_WIRE {
|
||||
eprintln!(
|
||||
"[H7CAD] wire '{}': {} segments exceeds limit ({MAX_SEGS_PER_WIRE}), truncating",
|
||||
wire.name, raw_seg_count
|
||||
);
|
||||
}
|
||||
let mut vertices: Vec<WireVertex> = Vec::with_capacity(seg_count * 6);
|
||||
|
||||
// Precompute cumulative arc-length at each point.
|
||||
|
||||
Reference in new issue
Block a user