diff --git a/CLAUDE.md b/CLAUDE.md index e2f1b6b..df40565 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -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` diff --git a/src/app.rs b/src/app.rs index de59721..7bd0a63 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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), ) }; diff --git a/src/cli.rs b/src/cli.rs index ed1ff59..37c6172 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -107,6 +107,12 @@ pub struct Cli { #[arg(long, value_name = "PATH")] pub export_path: Option, + /// 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 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, diff --git a/src/fractal/renderer.rs b/src/fractal/renderer.rs index d92809a..0ddeecc 100644 --- a/src/fractal/renderer.rs +++ b/src/fractal/renderer.rs @@ -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 { // 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 { 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, } diff --git a/src/headless.rs b/src/headless.rs index 88decfc..1bb6c8c 100644 --- a/src/headless.rs +++ b/src/headless.rs @@ -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, + 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}"));