diff --git a/pathtracer-gui/src/gui.rs b/pathtracer-gui/src/gui.rs index 16cf0295..3ba5808b 100644 --- a/pathtracer-gui/src/gui.rs +++ b/pathtracer-gui/src/gui.rs @@ -10,7 +10,7 @@ use std::{ use egui::Vec2; use nalgebra::Vector3; use scene::camera::{Camera, Pinhole}; -use tracing::{image_buffer::ImageBuffer, pathtracer::Pathtracer}; +use tracing::pathtracer::Pathtracer; use crate::workers::{spawn_worker, RenderResult}; @@ -19,15 +19,6 @@ struct RenderState { duration: time::Duration, } -fn convert(iterations: u16, buffer: ImageBuffer) -> egui::ColorImage { - let size = [buffer.width as usize, buffer.height as usize]; - let pixels = buffer - .into_rgba_iter(iterations) - .map(|p| egui::Color32::from_rgba_premultiplied(p[0], p[1], p[2], p[3])) - .collect(); - egui::ColorImage { size, pixels } -} - fn receiver_thread( rx: Receiver, render_ptr: Arc>>, @@ -35,12 +26,11 @@ fn receiver_thread( ctx: egui::Context, ) { while let Ok(result) = rx.recv() { - let image = convert(result.iteration, result.image); render_ptr.lock().unwrap().replace(RenderState { - iteration: result.iteration, + iteration: result.iterations, duration: result.duration, }); - image_ptr.lock().unwrap().replace(image); + image_ptr.lock().unwrap().replace(result.image); ctx.request_repaint(); } } diff --git a/pathtracer-gui/src/workers.rs b/pathtracer-gui/src/workers.rs index c414f608..ab244ed0 100644 --- a/pathtracer-gui/src/workers.rs +++ b/pathtracer-gui/src/workers.rs @@ -20,9 +20,25 @@ use tracing::{ }; pub struct RenderResult { - pub iteration: u16, + pub iterations: u16, pub duration: time::Duration, - pub image: ImageBuffer, + pub image: egui::ColorImage, +} + +impl RenderResult { + fn from_buffer(iterations: u16, duration: time::Duration, buffer: ImageBuffer) -> Self { + let size = [buffer.width as usize, buffer.height as usize]; + let pixels = buffer + .into_rgba_iter(iterations) + .map(|p| egui::Color32::from_rgba_premultiplied(p[0], p[1], p[2], p[3])) + .collect(); + let image = egui::ColorImage { size, pixels }; + RenderResult { + iterations, + duration, + image, + } + } } fn render_subdivided( @@ -105,22 +121,18 @@ fn worker_loop( let (duration, buffer) = measure(|| { render_subdivided(&pathtracer, &pinhole_small, pinhole_small.size() / 3) }); - let _ = tx.send(RenderResult { - iteration: 1, - duration, - image: buffer, - }); + let _ = tx.send(RenderResult::from_buffer(1, duration, buffer)); } let (duration, buffer) = measure(|| render_subdivided(&pathtracer, &pinhole, pinhole.size() / 4)); combined_buffer += buffer; iteration += 1; - let _ = tx.send(RenderResult { + let _ = tx.send(RenderResult::from_buffer( iteration, duration, - image: combined_buffer.clone(), - }); + combined_buffer.clone(), + )); } }