Skip to content

Commit

Permalink
Move buffer conversion to worker thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Oom committed Jul 5, 2024
1 parent b19d7c0 commit fe1aa7c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
16 changes: 3 additions & 13 deletions pathtracer-gui/src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -19,28 +19,18 @@ 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<RenderResult>,
render_ptr: Arc<Mutex<Option<RenderState>>>,
image_ptr: Arc<Mutex<Option<egui::ColorImage>>>,
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();
}
}
Expand Down
32 changes: 22 additions & 10 deletions pathtracer-gui/src/workers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(),
));
}
}

Expand Down

0 comments on commit fe1aa7c

Please sign in to comment.