use glam::Vec3;
use indicatrix::{
geometry::{StoneGeometry, ToolPrimitive, plane::GpuFacetPlane},
optics::raytracer::{Camera, DEFAULT_FOV_DEG},
renderer::{
denoise::AtrousDenoiser,
frame_denoise::{DenoiseScratch, FirstHitSnapshot, denoise_and_tonemap_frame},
guide_pass::generate_guide_buffers_cancellable_geom_with_index,
},
};
use indicatrix_net::SceneState;
use std::{
sync::{
Arc,
atomic::{AtomicBool, Ordering},
mpsc::{self, RecvTimeoutError, TryRecvError},
},
thread,
time::Duration,
};
pub(in crate::stream_emit) struct DisplayPicture {
pub(in crate::stream_emit) samples: u32,
pub(in crate::stream_emit) rgba: Vec<u8>,
}
pub(in crate::stream_emit) enum DisplayUpdate {
Picture(DisplayPicture),
Plain,
Nothing,
}
struct Job {
samples: u32,
sum: Vec<Vec3>,
}
struct GuideInputs {
width: u32,
height: u32,
yaw: f32,
pitch: f32,
distance: f32,
planes: Vec<GpuFacetPlane>,
tools: Vec<ToolPrimitive>,
n_d: f32,
}
pub(in crate::stream_emit) struct DisplayDenoiser {
jobs: mpsc::Sender<Job>,
pictures: mpsc::Receiver<DisplayPicture>,
cancel: Arc<AtomicBool>,
in_flight: bool,
dirty: bool,
gone: bool,
}
impl DisplayDenoiser {
pub(in crate::stream_emit) fn spawn(scene: &SceneState) -> Self {
let (jobs, job_rx) = mpsc::channel::<Job>();
let (picture_tx, pictures) = mpsc::channel::<DisplayPicture>();
let cancel = Arc::new(AtomicBool::new(false));
let inputs = GuideInputs {
width: scene.width,
height: scene.height,
yaw: scene.yaw,
pitch: scene.pitch,
distance: scene.distance,
planes: scene.planes.clone(),
tools: scene.tools.clone(),
n_d: scene.material.dispersion.n_d(),
};
let flag = Arc::clone(&cancel);
let spawned = thread::Builder::new()
.name("display-denoise".to_string())
.spawn(move || run(&inputs, &flag, &job_rx, &picture_tx));
if let Err(e) = &spawned {
tracing::warn!(
"display frames go out un-denoised: could not start the denoise thread: {e}"
);
}
Self {
jobs,
pictures,
cancel,
in_flight: false,
dirty: false,
gone: spawned.is_err(),
}
}
pub(in crate::stream_emit) fn tick(
&mut self,
fresh: bool,
samples_done: u32,
total: &[Vec3],
) -> DisplayUpdate {
self.dirty |= fresh;
let finished = self.try_picture();
if self.gone {
let plain = self.dirty && samples_done > 0;
self.dirty = false;
return if plain {
DisplayUpdate::Plain
} else {
DisplayUpdate::Nothing
};
}
if self.dirty && samples_done > 0 && self.submit(samples_done, total) {
self.dirty = false;
}
finished.map_or(DisplayUpdate::Nothing, DisplayUpdate::Picture)
}
pub(in crate::stream_emit) fn finish<E>(
&mut self,
samples: u32,
total: &[Vec3],
heartbeat_every: Duration,
mut heartbeat: impl FnMut() -> Result<(), E>,
) -> Result<Option<Vec<u8>>, E> {
while self.in_flight {
if self.wait(heartbeat_every, &mut heartbeat)?.is_none() {
return Ok(None);
}
}
if samples == 0 || !self.submit(samples, total) {
return Ok(None);
}
Ok(self
.wait(heartbeat_every, &mut heartbeat)?
.map(|picture| picture.rgba))
}
fn try_picture(&mut self) -> Option<DisplayPicture> {
if self.gone {
return None;
}
match self.pictures.try_recv() {
Ok(picture) => {
self.in_flight = false;
Some(picture)
}
Err(TryRecvError::Empty) => None,
Err(TryRecvError::Disconnected) => {
self.gone = true;
self.in_flight = false;
None
}
}
}
fn wait<E>(
&mut self,
every: Duration,
heartbeat: &mut impl FnMut() -> Result<(), E>,
) -> Result<Option<DisplayPicture>, E> {
loop {
match self.pictures.recv_timeout(every) {
Ok(picture) => {
self.in_flight = false;
return Ok(Some(picture));
}
Err(RecvTimeoutError::Timeout) => heartbeat()?,
Err(RecvTimeoutError::Disconnected) => {
self.gone = true;
self.in_flight = false;
return Ok(None);
}
}
}
}
fn submit(&mut self, samples: u32, total: &[Vec3]) -> bool {
if self.gone || self.in_flight {
return false;
}
let job = Job {
samples,
sum: total.to_vec(),
};
if self.jobs.send(job).is_err() {
self.gone = true;
return false;
}
self.in_flight = true;
true
}
}
impl Drop for DisplayDenoiser {
fn drop(&mut self) {
self.cancel.store(true, Ordering::Relaxed);
}
}
fn run(
inputs: &GuideInputs,
cancel: &AtomicBool,
jobs: &mpsc::Receiver<Job>,
pictures: &mpsc::Sender<DisplayPicture>,
) {
let camera = Camera::new(inputs.yaw, inputs.pitch, inputs.distance, DEFAULT_FOV_DEG);
let Some(guides) = generate_guide_buffers_cancellable_geom_with_index(
inputs.width,
inputs.height,
&camera,
StoneGeometry {
planes: &inputs.planes,
tools: &inputs.tools,
},
inputs.n_d,
cancel,
) else {
return;
};
let mut denoiser = AtrousDenoiser::new();
let (mut avg_color_buf, mut filtered_buf) = (Vec::new(), Vec::new());
while let Ok(job) = jobs.recv() {
if cancel.load(Ordering::Relaxed) {
return;
}
let rgba = denoise_and_tonemap_frame(
FirstHitSnapshot {
width: inputs.width,
height: inputs.height,
current_sample_count: job.samples,
accum_buffer: &job.sum,
first_hit_depth: &guides.depth,
first_hit_normal: &guides.normal,
first_hit_facet_id: &guides.facet_id,
first_hit_path_sig: &guides.path_sig,
},
&mut DenoiseScratch {
denoiser: &mut denoiser,
avg_color_buf: &mut avg_color_buf,
filtered_buf: &mut filtered_buf,
},
);
let picture = DisplayPicture {
samples: job.samples,
rgba,
};
if pictures.send(picture).is_err() {
return;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use indicatrix::{
geometry::cuts::StandardGemCuts,
optics::{materials::GemMaterial, raytracer::LightingPreset},
renderer::guide_pass::generate_guide_buffers_with_index,
};
use std::convert::Infallible;
fn scene() -> SceneState {
SceneState {
width: 12,
height: 9,
yaw: 0.4,
pitch: 0.3,
distance: 3.0,
light_yaw: 0.85,
light_pitch: 0.95,
exposure: 1.0,
max_bounces: 4,
lighting_preset: LightingPreset::Daylight,
material: GemMaterial::diamond(),
planes: StandardGemCuts::standard_round_brilliant(),
girdle_frosted: false,
backdrop: 0.0,
environment: indicatrix_net::scene::SceneEnvironment::Studio,
surface_glare: 1.0,
tools: Vec::new(),
fluorescence: indicatrix::optics::fluorescence::Fluorescence::default(),
head_shadow_deg: 16.0,
}
}
fn sum(len: usize, scale: f32) -> Vec<Vec3> {
(0..len)
.map(|i| Vec3::new((i % 7) as f32, (i % 5) as f32, (i % 3) as f32) * scale)
.collect()
}
fn reference(scene: &SceneState, samples: u32, total: &[Vec3]) -> Vec<u8> {
let camera = Camera::new(scene.yaw, scene.pitch, scene.distance, DEFAULT_FOV_DEG);
let guides = generate_guide_buffers_with_index(
scene.width,
scene.height,
&camera,
&scene.planes,
scene.material.dispersion.n_d(),
);
denoise_and_tonemap_frame(
FirstHitSnapshot {
width: scene.width,
height: scene.height,
current_sample_count: samples,
accum_buffer: total,
first_hit_depth: &guides.depth,
first_hit_normal: &guides.normal,
first_hit_facet_id: &guides.facet_id,
first_hit_path_sig: &guides.path_sig,
},
&mut DenoiseScratch {
denoiser: &mut AtrousDenoiser::new(),
avg_color_buf: &mut Vec::new(),
filtered_buf: &mut Vec::new(),
},
)
}
fn next_picture(
denoiser: &mut DisplayDenoiser,
samples: u32,
total: &[Vec3],
) -> DisplayPicture {
for _ in 0..500 {
if let DisplayUpdate::Picture(p) = denoiser.tick(false, samples, total) {
return p;
}
thread::sleep(Duration::from_millis(10));
}
panic!("no display picture within 5 s");
}
#[test]
fn pictures_match_the_viewers_pipeline_and_never_overlap() {
let scene = scene();
let len = (scene.width * scene.height) as usize;
let (first, second) = (sum(len, 1.0), sum(len, 3.0));
let mut denoiser = DisplayDenoiser::spawn(&scene);
assert!(matches!(
denoiser.tick(true, 2, &first),
DisplayUpdate::Nothing
));
assert!(denoiser.in_flight, "the first fresh tick submits");
let first_picture = match denoiser.tick(true, 4, &second) {
DisplayUpdate::Picture(p) => p,
_ => next_picture(&mut denoiser, 4, &second),
};
assert_eq!(first_picture.samples, 2);
assert_eq!(first_picture.rgba, reference(&scene, 2, &first));
assert!(denoiser.in_flight, "the remembered newer sum was submitted");
let second_picture = next_picture(&mut denoiser, 4, &second);
assert_eq!(second_picture.samples, 4);
assert_eq!(second_picture.rgba, reference(&scene, 4, &second));
let final_rgba = denoiser
.finish(6, &second, Duration::from_millis(50), || {
Ok::<(), Infallible>(())
})
.unwrap()
.expect("the thread is alive");
assert_eq!(final_rgba, reference(&scene, 6, &second));
}
#[test]
fn a_tick_without_new_samples_submits_nothing() {
let scene = scene();
let total = sum((scene.width * scene.height) as usize, 1.0);
let mut denoiser = DisplayDenoiser::spawn(&scene);
assert!(matches!(
denoiser.tick(false, 3, &total),
DisplayUpdate::Nothing
));
assert!(!denoiser.in_flight);
assert!(matches!(
denoiser.tick(true, 0, &total),
DisplayUpdate::Nothing
));
assert!(!denoiser.in_flight, "no picture of zero samples");
}
}