library: render: pathtracer: draft parallelize

This commit is contained in:
Antoine Martin 2020-04-07 22:19:38 +02:00
parent c0c34fba7d
commit 3f2c7b40e8
4 changed files with 36 additions and 35 deletions

View file

@ -1,4 +1,5 @@
reflection_limit: 5 reflection_limit: 5
shot_rays: 50
camera: camera:
origin: [0.0, 1.0, 0.0] origin: [0.0, 1.0, 0.0]

View file

@ -1,4 +1,6 @@
use super::super::utils::{buffer_to_image, prepare_buffer, sample_hemisphere}; use rayon::prelude::*;
use super::super::utils::{buffer_to_image, sample_hemisphere};
use super::super::Renderer; use super::super::Renderer;
use crate::{ use crate::{
core::LinearColor, core::LinearColor,
@ -35,29 +37,44 @@ impl Pathtracer {
self.scene.camera.film().height(), self.scene.camera.film().height(),
); );
let total = width * height; let total = width * height;
let mut buffer = prepare_buffer(total);
// (total passes, film) let p = super::super::progress::get_progressbar(self.scene.shot_rays as u64);
// FIXME: use MultiProgress because of rendering issues
let (pa, pb) = super::super::progress::get_multiple_progress(total, self.scene.shot_rays);
// Ensure at least one round of shots // Ensure at least one round of shots
for _ in 0..self.scene.shot_rays.max(1) { let img_buf = (0..self.scene.shot_rays.max(1))
pb.reset(); // We're rendering the whole film again, reset the pixel counter .into_par_iter()
for y in 0..self.scene.camera.film().height() { .map(|_| {
for x in 0..self.scene.camera.film().width() { let mut buffer: Vec<LinearColor> = Vec::new();
let i = x + y * self.scene.camera.film().width(); buffer.resize_with(total as usize, LinearColor::black);
buffer[i as usize] += self.pixel_ray(x as f32, y as f32);
pb.inc(1); for y in 0..self.scene.camera.film().height() {
for x in 0..self.scene.camera.film().width() {
let i = x + y * self.scene.camera.film().width();
buffer[i as usize] += self.pixel_ray(x as f32, y as f32);
}
} }
} p.inc(1); // Increment the number of passes
pa.inc(1); // Increment the number of passes
}
pa.finish(); buffer
pb.finish_and_clear(); })
.reduce(
|| {
let mut vec = Vec::new();
vec.resize_with(total as usize, LinearColor::black);
vec
},
|mut acc, buf| {
for (i, pixel) in buf.into_iter().enumerate() {
acc[i] += pixel;
}
buffer_to_image(buffer, self.scene.shot_rays, width, height) acc
},
);
p.finish();
buffer_to_image(img_buf, self.scene.shot_rays, width, height)
} }
fn pixel_ray(&self, x: f32, y: f32) -> LinearColor { fn pixel_ray(&self, x: f32, y: f32) -> LinearColor {

View file

@ -8,12 +8,3 @@ pub fn get_progressbar(total: u64) -> ProgressBar {
)); ));
pb pb
} }
pub fn get_multiple_progress(total: u32, passes: u32) -> (ProgressBar, ProgressBar) {
let pb = ProgressBar::new(passes as u64);
pb.set_draw_delta((passes as u64 / 100).max(1));
pb.set_style(indicatif::ProgressStyle::default_bar().template(
"{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {percent:>3}%: {pos}/{len} passes (ETA: {eta})",
));
(pb, get_progressbar(total as u64))
}

View file

@ -104,14 +104,6 @@ pub fn sample_hemisphere(normal: Unit<Vector>) -> (Unit<Vector>, f32) {
(scattered, 1. / scattered.dot(&normal)) (scattered, 1. / scattered.dot(&normal))
} }
pub fn prepare_buffer(total: u32) -> Vec<LinearColor> {
let mut ans = Vec::with_capacity(total as usize);
for _ in 0..total {
ans.push(LinearColor::black());
}
ans
}
pub fn buffer_to_image(buffer: Vec<LinearColor>, passes: u32, width: u32, height: u32) -> RgbImage { pub fn buffer_to_image(buffer: Vec<LinearColor>, passes: u32, width: u32, height: u32) -> RgbImage {
let mut image = RgbImage::new(width, height); let mut image = RgbImage::new(width, height);