perf: improve animation performance

This commit is contained in:
2026-09-30 21:05:10 +02:00
parent 289cfb2976
commit 833859b974
4 changed files with 70 additions and 31 deletions
+1 -1
View File
@@ -272,7 +272,7 @@ pixel is a handful of `f32` complex multiplies.
what makes deep zoom fast: every pixel follows the reference for tens of
thousands of steps before diverging (Mandelbrot 1e-200: 12.7 s → 0.7 s
at 960×540). Level `l` node `i` covers steps `1 + i·2^l ..`, and only
levels ≥ 3 are uploaded (bindings 4/5, 48 B nodes, `bla_meta` =
levels ≥ 3 are uploaded (built across cores, `par_map`; bindings 4/5, 48 B nodes, `bla_meta` =
`[min_level, levels, offsets…]`). Coefficients are floatexp (f32
mantissas + i32 exponent), so a jump in the deep prologue lands straight
at its output scale. `R` depends on the view's largest `|dc|`
+24 -10
View File
@@ -585,32 +585,46 @@ pub fn build(
// First stored level, folded straight from single steps.
let run = 1usize << min_level;
let mut level: Vec<Node> = (0..steps >> min_level)
.map(|i| {
let mut level: Vec<Node> = par_map(steps >> min_level, |i| {
let start = 1 + i * run;
(1..run).fold(Node::step(map, point(start)), |acc, k| {
acc.then(Node::step(map, point(start + k)), dc_max)
})
})
.collect();
});
let mut nodes = Vec::new();
let mut meta = vec![min_level, 0];
while !level.is_empty() {
meta.push(nodes.len() as u32);
nodes.extend(level.iter().map(|n| n.gpu()));
level = level
.as_chunks::<2>()
.0
.iter()
.map(|[x, y]| x.then(*y, dc_max))
.collect();
level = par_map(level.len() / 2, |i| {
level[2 * i].then(level[2 * i + 1], dc_max)
});
}
meta[1] = (meta.len() - 2) as u32;
meta.push(nodes.len() as u32);
BlaTable { nodes, meta }
}
/// `(0..n).map(f).collect()`, split across cores when `n` is large enough to
/// pay for the threads (a 900k-step table took ~60 ms on one core).
fn par_map<T: Send>(n: usize, f: impl Fn(usize) -> T + Sync) -> Vec<T> {
#[cfg(not(target_arch = "wasm32"))]
if n >= 4096 {
let threads = std::thread::available_parallelism().map_or(1, |t| t.get());
let chunk = n.div_ceil(threads);
let f = &f;
return std::thread::scope(|scope| {
let parts: Vec<_> = (0..n)
.step_by(chunk)
.map(|a| scope.spawn(move || (a..(a + chunk).min(n)).map(f).collect::<Vec<T>>()))
.collect();
parts.into_iter().flat_map(|p| p.join().unwrap()).collect()
});
}
(0..n).map(f).collect()
}
#[cfg(test)]
mod tests {
use super::*;
+36 -11
View File
@@ -1915,17 +1915,37 @@ impl ExportRender {
} else {
1
};
banded_data_pass(
device,
queue,
&mut encoder,
// Timed bands, as the export's: the worst-case bound alone cut deep
// probes (~1M `max_iter`) into one-row submissions, costlier than the
// full-size render.
let mut sizer = BandSizer::new(height.div_ceil(band_count(
width,
height,
self.uniforms.max_iter,
samples,
)));
let mut y0 = 0;
while y0 < height {
let y1 = (y0 + sizer.rows()).min(height);
let start = std::time::Instant::now();
let mut band_encoder = device.create_command_encoder(&Default::default());
band_pass(
&mut band_encoder,
"export ci probe pass",
&data,
iterate,
&[&self.bind_group],
[width, height],
band_count(width, height, self.uniforms.max_iter, samples),
Some((width, y0, y1)),
y0 == 0,
);
queue.submit([band_encoder.finish()]);
let _ = device.poll(wgpu::PollType::Wait {
submission_index: None,
timeout: None,
});
sizer.observe(y1 - y0, start.elapsed().as_secs_f64());
y0 = y1;
}
histogram.record(device, &mut encoder, &data, width, height, &hist, &staging);
queue.submit(std::iter::once(encoder.finish()));
@@ -1948,11 +1968,7 @@ impl ExportRender {
/// Band sizing for rendering this export, starting at the worst-case
/// height; feed it each band's GPU time ([`BandSizer::observe`]).
pub fn band_sizer(&self) -> BandSizer {
let rows = self.first_band_rows.max(1);
BandSizer {
rows,
min_rows: rows,
}
BandSizer::new(self.first_band_rows)
}
/// Render rows `[y0, y1)` into the export texture and submit them. The
@@ -2447,6 +2463,15 @@ pub struct BandSizer {
}
impl BandSizer {
/// Starting (and minimum) band height: the worst-case safe one.
fn new(rows: u32) -> Self {
let rows = rows.max(1);
Self {
rows,
min_rows: rows,
}
}
pub fn rows(&self) -> u32 {
self.rows
}
+1 -1
View File
@@ -669,7 +669,7 @@ impl<'a> Exporter<'a> {
self.height,
uniforms,
app.reference_points(),
// Needs this frame's uniforms; ~15 ms per 100k points.
// Needs this frame's uniforms; ~15 ms per 100k points on one core.
&bla::for_uniforms(app.reference_points(), &uniforms, app.use_bla()),
app.lights(),
);