From 6532be41b287e065fc84f8512205a301e1e83240 Mon Sep 17 00:00:00 2001 From: Johan Walles Date: Fri, 30 Jun 2023 20:15:36 +0200 Subject: [PATCH] Improve idle performance By only doing flames maths for pixels that can potentially contain flames. --- README.md | 5 +++-- libloadviz/src/renderer.rs | 34 ++++++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 9348fcb..f309a5d 100644 --- a/README.md +++ b/README.md @@ -74,14 +74,13 @@ How would you visualize this? ## TODO -- Improve idle-system performance by only trying to render clouds where they - could be, and the same thing for fire. - Make the macOS About dialog show the latest `macos-` version number rather than a date - Make an icon - Make sure the icon is visible in the Finder - Make sure the icon is visible in the About dialog - Try increasing the fire distortion radius when flames are higher +- Try distorting the fire more vertically than horizontally - Animate the screenshot in a cross faded loop. Try 10s first and see if that's enough. - Make a Dock icon visualization @@ -153,5 +152,7 @@ How would you visualize this? - Make the clouds look more realistic: - Have less details in the clouds - Animate the clouds slower +- Improve idle-system performance by only trying to render clouds where they + could be, and the same thing for the flames. [create new release]: https://github.com/walles/loadviz/releases/new diff --git a/libloadviz/src/renderer.rs b/libloadviz/src/renderer.rs index bf5fc55..68b054a 100644 --- a/libloadviz/src/renderer.rs +++ b/libloadviz/src/renderer.rs @@ -91,8 +91,8 @@ impl Renderer { width: usize, height: usize, ) -> Option<[u8; 3]> { - // Higher scale number = more details. - let scale = 5.0 / width as f32; + // Higher number = more details. + let detail = 5.0 / width as f32; // Higher speed number = faster cloud turbulence. let speed = 0.3; @@ -109,8 +109,8 @@ impl Renderer { // Noise output is -1 to 1, deciphered from here: // https://github.com/amethyst/bracket-lib/blob/0d2d5e6a9a8e7c7ae3710cfef85be4cab0109a27/bracket-noise/examples/simplex_fractal.rs#L34-L39 let noise_m1_to_1 = self.noise.get_noise3d( - scale * pixel_x as f32, - scale * pixel_y_from_top as f32, + detail * pixel_x as f32, + detail * pixel_y_from_top as f32, speed * dt_seconds, ); @@ -146,11 +146,25 @@ impl Renderer { width: usize, height: usize, ) -> Option<[u8; 3]> { - // Higher scale number = more details. - let scale = 10.0 / width as f32; + // Higher number = more details. + let detail = 10.0 / width as f32; let distortion_pixel_radius = width.min(height) as f32 / 10.0; + // Check whether we should even try to do flames maths. This improves + // our idle-system benchmark by 63%. + let highest_load_0_to_1 = viz_loads + .iter() + .map(|load| load.user_0_to_1) + .max_by(|a, b| a.partial_cmp(b).unwrap()) + .unwrap(); + let highest_possible_flame_height_pixels = + highest_load_0_to_1 * height as f32 + distortion_pixel_radius; + if pixel_y_from_bottom as f32 > highest_possible_flame_height_pixels { + // We're above the flames, no need for any (costly) noise maths + return None; + } + // Starting at this fraction of each flame pillar's height, the color will // start fading towards the background color. let fadeout_fraction = 0.8; @@ -158,13 +172,13 @@ impl Renderer { // Noise output is -1 to 1, deciphered from here: // https://github.com/amethyst/bracket-lib/blob/0d2d5e6a9a8e7c7ae3710cfef85be4cab0109a27/bracket-noise/examples/simplex_fractal.rs#L34-L39 let noise1_m1_to_1 = self.noise.get_noise3d( - scale * pixel_x as f32, - scale * pixel_y_from_bottom as f32, + detail * pixel_x as f32, + detail * pixel_y_from_bottom as f32, dt_seconds, ); let noise2_m1_to_1 = self.noise.get_noise3d( - scale * pixel_x as f32, - scale * pixel_y_from_bottom as f32, + detail * pixel_x as f32, + detail * pixel_y_from_bottom as f32, -dt_seconds - 1.0, );