mirror of
https://github.com/CrispyPin/julia-fractal-renderer.git
synced 2025-06-07 22:55:31 +02:00
multithread rendering with rayon
This commit is contained in:
parent
59739cfc1c
commit
fc209a78ad
4 changed files with 156 additions and 56 deletions
113
src/generate.rs
113
src/generate.rs
|
@ -1,13 +1,14 @@
|
|||
use eframe::epaint::Vec2;
|
||||
use image::{Rgb, RgbImage};
|
||||
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct RenderOptions {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub unit_width: f64,
|
||||
pub iterations: u32,
|
||||
pub max_iter: u16,
|
||||
pub cx: f64,
|
||||
pub cy: f64,
|
||||
pub fill_style: FillStyle,
|
||||
|
@ -25,7 +26,7 @@ impl Default for RenderOptions {
|
|||
width: 512,
|
||||
height: 512,
|
||||
unit_width: 4.0,
|
||||
iterations: 128,
|
||||
max_iter: 128,
|
||||
cx: 0.4,
|
||||
cy: -0.2,
|
||||
fill_style: FillStyle::Bright,
|
||||
|
@ -33,57 +34,79 @@ impl Default for RenderOptions {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn view_point(q: &RenderOptions, image: RgbImage) -> RgbImage {
|
||||
apply_fn(image, q, |x, y| {
|
||||
let len = (Vec2::new(x as f32, y as f32) - Vec2::new(q.cx as f32, q.cy as f32)).length();
|
||||
if len < 0.03 {
|
||||
Some(Rgb([0, 120, 120]))
|
||||
} else if len < 0.04 {
|
||||
Some(Rgb([255; 3]))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
pub fn render_c(q: &RenderOptions, mut image: RgbImage) -> RgbImage {
|
||||
let width = q.width as f32;
|
||||
let height = q.height as f32;
|
||||
let ppu = width / (q.unit_width as f32);
|
||||
|
||||
pub fn render(q: &RenderOptions, color: (u8, u8, u8)) -> RgbImage {
|
||||
let img = RgbImage::new(q.width, q.height);
|
||||
apply_fn(img, q, |x, y| {
|
||||
let i = julia(x, y, q.cx, q.cy, q.iterations);
|
||||
if q.fill_style == FillStyle::Black && i == q.iterations {
|
||||
None
|
||||
} else {
|
||||
let i = i.min(255) as u8;
|
||||
Some(Rgb([
|
||||
i.saturating_mul(color.0),
|
||||
i.saturating_mul(color.1),
|
||||
i.saturating_mul(color.2),
|
||||
]))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn apply_fn<F>(mut image: RgbImage, q: &RenderOptions, op: F) -> RgbImage
|
||||
where
|
||||
F: Fn(f64, f64) -> Option<Rgb<u8>>,
|
||||
{
|
||||
let width = q.width as f64;
|
||||
let height = q.height as f64;
|
||||
let ppu = width / q.unit_width;
|
||||
let target = Vec2::new(q.cx as f32, q.cy as f32);
|
||||
|
||||
for y in 0..q.height {
|
||||
for x in 0..q.width {
|
||||
let sx = (x as f64 - width / 2.0) / ppu;
|
||||
let sy = (y as f64 - height / 2.0) / ppu;
|
||||
if let Some(pixel) = op(sx, sy) {
|
||||
image.put_pixel(x, y, pixel);
|
||||
let sx = (x as f32 - width / 2.0) / ppu;
|
||||
let sy = (y as f32 - height / 2.0) / ppu;
|
||||
|
||||
let len = (Vec2::new(sx, sy) - target).length();
|
||||
if len < 0.03 {
|
||||
image.put_pixel(x as u32, y as u32, Rgb([0, 120, 120]));
|
||||
} else if len < 0.04 {
|
||||
image.put_pixel(x as u32, y as u32, Rgb([255; 3]));
|
||||
}
|
||||
}
|
||||
}
|
||||
image
|
||||
}
|
||||
|
||||
fn julia(mut x: f64, mut y: f64, cx: f64, cy: f64, max_iter: u32) -> u32 {
|
||||
pub fn color_iteration(iter: u16, color: (u8, u8, u8)) -> Rgb<u8> {
|
||||
let i = iter.min(255) as u8;
|
||||
Rgb([
|
||||
i.saturating_mul(color.0),
|
||||
i.saturating_mul(color.1),
|
||||
i.saturating_mul(color.2),
|
||||
])
|
||||
}
|
||||
|
||||
pub fn render_julia(q: &RenderOptions, color: (u8, u8, u8)) -> RgbImage {
|
||||
let mut image = RgbImage::new(q.width as u32, q.height as u32);
|
||||
|
||||
let width = q.width as f64;
|
||||
let height = q.height as f64;
|
||||
let ppu = width / q.unit_width;
|
||||
|
||||
let fill = match q.fill_style {
|
||||
FillStyle::Black => Rgb([0; 3]),
|
||||
FillStyle::Bright => color_iteration(q.max_iter, color),
|
||||
};
|
||||
|
||||
(0..q.height)
|
||||
.into_par_iter()
|
||||
.map(|y| {
|
||||
let mut row = Vec::with_capacity(q.width);
|
||||
for x in 0..q.width {
|
||||
let sx = (x as f64 - width / 2.0) / ppu;
|
||||
let sy = (y as f64 - height / 2.0) / ppu;
|
||||
let i = julia(sx, sy, q.cx, q.cy, q.max_iter);
|
||||
|
||||
if i == q.max_iter {
|
||||
row.push(fill);
|
||||
} else {
|
||||
row.push(color_iteration(i, color));
|
||||
}
|
||||
}
|
||||
row
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.for_each(|(y, row)| {
|
||||
for (x, i) in row.into_iter().enumerate() {
|
||||
image.put_pixel(x as u32, y as u32, i);
|
||||
}
|
||||
});
|
||||
image
|
||||
}
|
||||
|
||||
fn julia(mut x: f64, mut y: f64, cx: f64, cy: f64, max_iter: u16) -> u16 {
|
||||
let mut iter = 0;
|
||||
while (x * x + y * y) < 4.0 && iter < max_iter {
|
||||
(x, y) = (
|
||||
|
|
22
src/main.rs
22
src/main.rs
|
@ -13,7 +13,7 @@ use eframe::{
|
|||
epaint::{TextureHandle, Vec2},
|
||||
Frame, NativeOptions,
|
||||
};
|
||||
use generate::{render, view_point, FillStyle, RenderOptions};
|
||||
use generate::{render_c, render_julia, FillStyle, RenderOptions};
|
||||
use image::EncodableLayout;
|
||||
use native_dialog::FileDialog;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
@ -47,7 +47,7 @@ struct JuliaGUI {
|
|||
#[serde(skip)]
|
||||
export_render_ms: Option<f64>,
|
||||
export_res_power: u8,
|
||||
export_iterations: u32,
|
||||
export_max_iter: u16,
|
||||
#[serde(skip)]
|
||||
export_path: PathBuf,
|
||||
#[serde(skip)]
|
||||
|
@ -77,7 +77,7 @@ impl Default for JuliaGUI {
|
|||
preview_render_ms: 0.0,
|
||||
export_render_ms: None,
|
||||
export_res_power: 3,
|
||||
export_iterations: 512,
|
||||
export_max_iter: 512,
|
||||
export_path: "".into(),
|
||||
settings_changed: true,
|
||||
preview_point: false,
|
||||
|
@ -113,7 +113,7 @@ impl JuliaGUI {
|
|||
RenderJob::Exit => break,
|
||||
RenderJob::Render(path, options, color) => {
|
||||
let start_time = SystemTime::now();
|
||||
let image = render(&options, color);
|
||||
let image = render_julia(&options, color);
|
||||
if let Err(err) = image.save(&path) {
|
||||
println!("Failed to save render: {err}");
|
||||
}
|
||||
|
@ -142,9 +142,9 @@ impl JuliaGUI {
|
|||
|
||||
fn update_preview(&mut self) {
|
||||
let start_time = SystemTime::now();
|
||||
let mut frame = render(&self.settings, self.color);
|
||||
let mut frame = render_julia(&self.settings, self.color);
|
||||
if self.preview_point {
|
||||
frame = view_point(&self.settings, frame);
|
||||
frame = render_c(&self.settings, frame);
|
||||
}
|
||||
|
||||
if let Some(preview) = &mut self.preview {
|
||||
|
@ -166,7 +166,7 @@ impl JuliaGUI {
|
|||
let settings = RenderOptions {
|
||||
width: self.settings.width * res_mul,
|
||||
height: self.settings.height * res_mul,
|
||||
iterations: self.export_iterations,
|
||||
max_iter: self.export_max_iter,
|
||||
..self.settings.clone()
|
||||
};
|
||||
|
||||
|
@ -272,8 +272,8 @@ impl eframe::App for JuliaGUI {
|
|||
let set_blue = ui.add(Slider::new(&mut self.color.2, 0..=16));
|
||||
|
||||
ui.label("Preview iterations:");
|
||||
let set_iter = ui
|
||||
.add(Slider::new(&mut self.settings.iterations, 5..=256).clamp_to_range(false));
|
||||
let set_iter =
|
||||
ui.add(Slider::new(&mut self.settings.max_iter, 5..=256).clamp_to_range(false));
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Preview resolution:");
|
||||
|
@ -313,7 +313,7 @@ impl eframe::App for JuliaGUI {
|
|||
});
|
||||
|
||||
ui.label("Render iterations:");
|
||||
ui.add(Slider::new(&mut self.export_iterations, 5..=1024).clamp_to_range(false));
|
||||
ui.add(Slider::new(&mut self.export_max_iter, 5..=1024).clamp_to_range(false));
|
||||
ui.label("Render resolution:");
|
||||
ui.add(Slider::new(&mut self.export_res_power, 0..=6).clamp_to_range(false));
|
||||
ui.label(format!(
|
||||
|
@ -350,7 +350,7 @@ impl eframe::App for JuliaGUI {
|
|||
|
||||
let predicted_render_time = (self.preview_render_ms
|
||||
* (1 << (self.export_res_power * 2)) as f64
|
||||
* (self.export_iterations as f64 / self.settings.iterations as f64)
|
||||
* (self.export_max_iter as f64 / self.settings.max_iter as f64)
|
||||
/ 1000.0)
|
||||
.floor();
|
||||
if predicted_render_time < 60.0 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue