perf: reduce png compression

This commit is contained in:
2026-09-30 19:26:25 +02:00
parent 91360a1e1c
commit 52e1814266
5 changed files with 53 additions and 12 deletions
+3 -1
View File
@@ -95,7 +95,9 @@ state is a pure function of `t` (`apply_frame`), so all frames'
`FractalApp::reference_job`s are snapshotted up front and `RefJob::compute`d
by a worker pool. The main thread renders them on the GPU as they arrive
(out of order), and another pool PNG-encodes and writes them
(`encode_png`, `Compression::Fast`). Channels are bounded. Once orbits and
(`encode_png`). PNGs (stills too) default to `Compression::Fast`
(`--png-compression`): the default zlib level took longer than a deep
render itself, for ~7% smaller files. Channels are bounded. Once orbits and
encoding are off the main thread, the GPU is usually the bottleneck.
There's no GPU in most sandboxes: `cargo check`/`cargo test --test shader_valid`
+8 -4
View File
@@ -1719,10 +1719,13 @@ impl FractalApp {
&device, &queue, &handles, w, h, uniforms, &reference, &bla, &lights,
);
let sh = Arc::clone(&shared);
let png =
crate::fractal::export_to_png_blocking(&device, &queue, &er, |phase, f| {
set_progress(&sh, phase, f)
});
let png = crate::fractal::export_to_png_blocking(
&device,
&queue,
&er,
png::Compression::Fast,
|phase, f| set_progress(&sh, phase, f),
);
set_progress(&shared, "Saving", 0.98);
let result = std::fs::write(&name, &png)
@@ -1779,6 +1782,7 @@ impl FractalApp {
er.height,
er.padded_bpr,
er.swap_rb,
png::Compression::Fast,
|f| set_progress(&sh, "Encoding", RENDER_END + (0.97 - RENDER_END) * f),
)
};
+24
View File
@@ -107,6 +107,12 @@ pub struct Cli {
#[arg(long, value_name = "PATH")]
pub export_path: Option<String>,
/// PNG compression for --headless output. "fast" encodes several times
/// quicker (at 1080p it otherwise takes longer than a deep render
/// itself) for somewhat larger files; "balanced"/"high" for smaller ones.
#[arg(long, value_enum, default_value_t = PngCompressionArg::Fast)]
pub png_compression: PngCompressionArg,
/// End view for an animation: "re,im,half_height[,iterations]", the same
/// syntax as --view. Combine with --view (or --share, --kind, --julia...)
/// for the start view; headless then renders a sequence of frames
@@ -244,6 +250,24 @@ pub enum KindArg {
ComplexMultibrot,
}
#[derive(Copy, Clone, Debug, Default, ValueEnum)]
pub enum PngCompressionArg {
#[default]
Fast,
Balanced,
High,
}
impl From<PngCompressionArg> for png::Compression {
fn from(c: PngCompressionArg) -> Self {
match c {
PngCompressionArg::Fast => png::Compression::Fast,
PngCompressionArg::Balanced => png::Compression::Balanced,
PngCompressionArg::High => png::Compression::High,
}
}
}
#[derive(Copy, Clone, Debug, ValueEnum)]
pub enum RenderingKindArg {
Classic,
+13 -3
View File
@@ -2070,6 +2070,7 @@ pub fn export_to_png_blocking(
device: &wgpu::Device,
queue: &wgpu::Queue,
er: &ExportRender,
compression: png::Compression,
mut on_progress: impl FnMut(&'static str, f32),
) -> Vec<u8> {
// Progress budget: rendering fills [0, RENDER_END], encoding the rest.
@@ -2099,9 +2100,15 @@ pub fn export_to_png_blocking(
.slice(..)
.get_mapped_range()
.expect("map readback buffer");
encode_png_with_progress(&data, er.width, er.height, er.padded_bpr, er.swap_rb, |f| {
on_progress("Encoding", RENDER_END + (0.97 - RENDER_END) * f)
})
encode_png_with_progress(
&data,
er.width,
er.height,
er.padded_bpr,
er.swap_rb,
compression,
|f| on_progress("Encoding", RENDER_END + (0.97 - RENDER_END) * f),
)
};
er.readback().unmap();
png
@@ -2205,6 +2212,7 @@ pub fn encode_png_with_progress(
height: u32,
padded_bpr: u32,
swap_rb: bool,
compression: png::Compression,
mut on_progress: impl FnMut(f32),
) -> Vec<u8> {
use std::io::Write as _;
@@ -2215,6 +2223,7 @@ pub fn encode_png_with_progress(
let mut encoder = png::Encoder::new(&mut out, width, height);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
encoder.set_compression(compression);
let mut writer = encoder.write_header().expect("png header");
let mut stream = writer.stream_writer().expect("png stream");
let mut line = vec![0u8; row];
@@ -2405,6 +2414,7 @@ const BAND_TARGET_SECS: f64 = 0.05;
#[derive(Clone, Copy, Debug)]
pub struct BandSizer {
rows: u32,
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
min_rows: u32,
}
+5 -4
View File
@@ -54,6 +54,7 @@ pub fn run(cli: Cli) -> Result<(), String> {
// consumes `cli` to build the start state.
let targets = AnimTargets::from_cli(&cli)?;
let export_path = cli.export_path.clone();
let compression = png::Compression::from(cli.png_compression);
if export_path.as_deref() == Some(STDOUT_PATH) {
check_stdout_piped()?;
}
@@ -67,7 +68,7 @@ pub fn run(cli: Cli) -> Result<(), String> {
app.set_output_size(width, height);
if targets.any() {
return run_animation(app, targets, width, height, export_path);
return run_animation(app, targets, width, height, export_path, compression);
}
let export_path = export_path.unwrap_or_else(|| format!("fractal-{}.png", unix_timestamp()));
@@ -101,7 +102,7 @@ pub fn run(cli: Cli) -> Result<(), String> {
}
eprintln!("rendering {width}×{height}…");
let png = export_to_png_blocking(&device, &queue, &er, |phase, fraction| {
let png = export_to_png_blocking(&device, &queue, &er, compression, |phase, fraction| {
eprint!("\r{phase} {:>3.0}%", fraction * 100.0);
});
eprintln!();
@@ -240,6 +241,7 @@ fn run_animation(
width: u32,
height: u32,
export_path: Option<String>,
compression: png::Compression,
) -> Result<(), String> {
let fps = targets.fps;
let frames = match targets.frames {
@@ -479,8 +481,7 @@ fn run_animation(
let _ = raw_tx.send((i, raw));
continue;
}
let png =
encode_png(&padded, width, height, bpr, swap_rb, png::Compression::Fast);
let png = encode_png(&padded, width, height, bpr, swap_rb, compression);
let path = format!("{out_dir}/frame-{:05}.png", first as usize + i + 1);
if let Err(e) = std::fs::write(&path, &png) {
fail(format!("save failed: {e}"));