Skip to content

Commit

Permalink
🎨 A start at denoising
Browse files Browse the repository at this point in the history
  • Loading branch information
sakarias88 committed Dec 8, 2023
1 parent 93be85c commit 206ca4e
Show file tree
Hide file tree
Showing 25 changed files with 547 additions and 119 deletions.
7 changes: 6 additions & 1 deletion racer-tracer/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct SharedCamera {

#[derive(Clone)]
pub struct CameraSharedData {
origin: Vec3,
pub origin: Vec3,
upper_left_corner: Vec3,
forward: Vec3,
right: Vec3,
Expand All @@ -80,6 +80,11 @@ impl SharedCamera {
}
}

#[allow(dead_code)]
pub fn pos(&self) -> &Vec3 {
&self.data.origin
}

pub fn data(&self) -> &CameraSharedData {
&self.data
}
Expand Down
14 changes: 10 additions & 4 deletions racer-tracer/src/data_bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ use bus::{Bus, BusReader};
use crate::error::TracerError;

#[derive(Clone)]
pub struct DataWriter<T> {
pub struct DataWriter<T>
where
T: Clone,
{
channel_name: String,
sender: Sender<T>,
}

impl<T> DataWriter<T> {
impl<T> DataWriter<T>
where
T: Clone,
{
fn new(channel_name: String, sender: Sender<T>) -> Self {
Self {
channel_name,
Expand Down Expand Up @@ -59,7 +65,7 @@ impl<T: Clone + Sync> DataReader<T> {

match res {
Ok(_) => Ok(buf),
Err(e) if e == RecvTimeoutError::Timeout => Ok(buf),
Err(RecvTimeoutError::Timeout) => Ok(buf),
Err(e) => Err(TracerError::BusReadError(
self.channel_name.clone(),
e.to_string(),
Expand Down Expand Up @@ -106,7 +112,7 @@ impl<T: Clone + Sync> DataBus<T> {

match res {
Ok(_) => Ok(()),
Err(e) if e == TracerError::BusTimeoutError() => Ok(()),
Err(TracerError::BusTimeoutError()) => Ok(()),
Err(e) => Err(TracerError::RecieveError(e.to_string())),
}
}
Expand Down
2 changes: 1 addition & 1 deletion racer-tracer/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
#[derive(Error, Debug, Eq, PartialEq)]
pub enum TracerError {
#[error("Failed to create window: {0}")]
FailedToCreateWindow(String),
Expand Down
11 changes: 10 additions & 1 deletion racer-tracer/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ pub struct HitRecord {
pub material: Arc<dyn Material>,
pub u: f64,
pub v: f64,
pub obj_id: usize,
}

impl HitRecord {
fn new(point: Vec3, t: f64, material: Arc<dyn Material>, u: f64, v: f64) -> Self {
fn new(
point: Vec3,
t: f64,
material: Arc<dyn Material>,
u: f64,
v: f64,
obj_id: usize,
) -> Self {
Self {
point,
normal: Vec3::default(),
Expand All @@ -34,6 +42,7 @@ impl HitRecord {
material,
u,
v,
obj_id,
}
}

Expand Down
1 change: 1 addition & 0 deletions racer-tracer/src/geometry/box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl HittableSceneObject for Boxx {
ray: &crate::ray::Ray,
t_min: f64,
t_max: f64,
_obj_id: usize,
) -> Option<super::HitRecord> {
let mut rec = None;
let mut closes_so_far = t_max;
Expand Down
11 changes: 9 additions & 2 deletions racer-tracer/src/geometry/moving_sphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ impl MovingSphere {
}

impl HittableSceneObject for MovingSphere {
fn obj_hit(&self, obj: &SceneObject, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
fn obj_hit(
&self,
obj: &SceneObject,
ray: &Ray,
t_min: f64,
t_max: f64,
obj_id: usize,
) -> Option<HitRecord> {
let oc = ray.origin() - self.pos(obj, ray.time());
let a = ray.direction().length_squared();
let half_b = oc.dot(ray.direction());
Expand All @@ -68,7 +75,7 @@ impl HittableSceneObject for MovingSphere {
let outward_normal = (point - self.pos(obj, ray.time())) / self.radius;
let (u, v) = Sphere::get_sphere_uv(&point);

let mut hit_record = HitRecord::new(point, root, obj.material(), u, v);
let mut hit_record = HitRecord::new(point, root, obj.material(), u, v, obj_id);
hit_record.set_face_normal(ray, outward_normal);
Some(hit_record)
}
Expand Down
9 changes: 8 additions & 1 deletion racer-tracer/src/geometry/rotate_y.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ impl RotateY {
}

impl HittableSceneObject for RotateY {
fn obj_hit(&self, _obj: &SceneObject, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
fn obj_hit(
&self,
_obj: &SceneObject,
ray: &Ray,
t_min: f64,
t_max: f64,
_obj_id: usize,
) -> Option<HitRecord> {
let mut origin = *ray.origin();
let mut direction = *ray.direction();

Expand Down
11 changes: 9 additions & 2 deletions racer-tracer/src/geometry/sphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ impl Sphere {
}

impl HittableSceneObject for Sphere {
fn obj_hit(&self, obj: &SceneObject, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
fn obj_hit(
&self,
obj: &SceneObject,
ray: &Ray,
t_min: f64,
t_max: f64,
obj_id: usize,
) -> Option<HitRecord> {
let oc = ray.origin() - obj.pos();
let a = ray.direction().length_squared();
let half_b = oc.dot(ray.direction());
Expand All @@ -55,7 +62,7 @@ impl HittableSceneObject for Sphere {
let outward_normal = (point - obj.pos()) / self.radius;
let (u, v) = Sphere::get_sphere_uv(&outward_normal);

let mut hit_record = HitRecord::new(point, root, obj.material(), u, v);
let mut hit_record = HitRecord::new(point, root, obj.material(), u, v, obj_id);
hit_record.set_face_normal(ray, outward_normal);
Some(hit_record)
}
Expand Down
1 change: 1 addition & 0 deletions racer-tracer/src/geometry/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl HittableSceneObject for Translate {
ray: &crate::ray::Ray,
t_min: f64,
t_max: f64,
_obj_id: usize,
) -> Option<HitRecord> {
let moved = Ray::new(ray.origin() - self.offset, *ray.direction(), ray.time());
match self.object.hit(&moved, t_min, t_max) {
Expand Down
3 changes: 2 additions & 1 deletion racer-tracer/src/geometry/xy_rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl HittableSceneObject for XyRect {
ray: &crate::ray::Ray,
t_min: f64,
t_max: f64,
obj_id: usize,
) -> Option<HitRecord> {
let origin = ray.origin();
let direction = ray.direction();
Expand All @@ -40,7 +41,7 @@ impl HittableSceneObject for XyRect {
}
let u = (x - self.x0) / (self.x1 - self.x0);
let v = (y - self.y0) / (self.y1 - self.y0);
let mut hit_record = HitRecord::new(ray.at(t), t, obj.material(), u, v);
let mut hit_record = HitRecord::new(ray.at(t), t, obj.material(), u, v, obj_id);
hit_record.set_face_normal(ray, Vec3::new(0.0, 0.0, 1.0));

Some(hit_record)
Expand Down
3 changes: 2 additions & 1 deletion racer-tracer/src/geometry/xz_rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl HittableSceneObject for XzRect {
ray: &crate::ray::Ray,
t_min: f64,
t_max: f64,
obj_id: usize,
) -> Option<HitRecord> {
let origin = ray.origin();
let direction = ray.direction();
Expand All @@ -41,7 +42,7 @@ impl HittableSceneObject for XzRect {

let u = (x - self.x0) / (self.x1 - self.x0);
let v = (z - self.z0) / (self.z1 - self.z0);
let mut hit_record = HitRecord::new(ray.at(t), t, obj.material(), u, v);
let mut hit_record = HitRecord::new(ray.at(t), t, obj.material(), u, v, obj_id);
hit_record.set_face_normal(ray, Vec3::new(0.0, 1.0, 0.0));

Some(hit_record)
Expand Down
3 changes: 2 additions & 1 deletion racer-tracer/src/geometry/yz_rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl HittableSceneObject for YzRect {
ray: &crate::ray::Ray,
t_min: f64,
t_max: f64,
obj_id: usize,
) -> Option<HitRecord> {
let origin = ray.origin();
let direction = ray.direction();
Expand All @@ -41,7 +42,7 @@ impl HittableSceneObject for YzRect {

let u = (y - self.y0) / (self.y1 - self.y0);
let v = (z - self.z0) / (self.z1 - self.z0);
let mut hit_record = HitRecord::new(ray.at(t), t, obj.material(), u, v);
let mut hit_record = HitRecord::new(ray.at(t), t, obj.material(), u, v, obj_id);
hit_record.set_face_normal(ray, Vec3::new(1.0, 0.0, 0.0));

Some(hit_record)
Expand Down
6 changes: 3 additions & 3 deletions racer-tracer/src/image.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::image_buffer::ImageBufferWriter;
use crate::data_bus::DataWriter;

#[derive(Clone)]
pub struct Image {
Expand All @@ -23,12 +23,12 @@ impl Image {
}
}

pub struct SubImage {
pub struct SubImage<T: Clone> {
pub x: usize,
pub y: usize,
pub screen_width: usize,
pub screen_height: usize,
pub width: usize,
pub height: usize,
pub writer: ImageBufferWriter,
pub writer: DataWriter<T>,
}
1 change: 0 additions & 1 deletion racer-tracer/src/image_action/png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ impl ImageAction for SavePng {
) -> Result<(), TracerError> {
match &config.image_output_dir {
Some(image_dir) => {
println!("A");
let png_data = screen_buffer
.iter()
.map(|v| {
Expand Down
38 changes: 4 additions & 34 deletions racer-tracer/src/image_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// TODO : Rename this file

use crate::{
data_bus::{DataBus, DataReader, DataWriter},
error::TracerError,
Expand Down Expand Up @@ -86,8 +84,8 @@ impl ImageBuffer {
}

#[allow(unused)]
pub fn get_writer(&self) -> ImageBufferWriter {
ImageBufferWriter::new(self.bus.get_writer())
pub fn get_writer(&self) -> DataWriter<ImageBufferEvent> {
self.bus.get_writer()
}

pub fn get_reader(&mut self) -> ImageBufferReader {
Expand All @@ -103,34 +101,6 @@ impl ImageBuffer {
}
}

#[derive(Clone)]
pub struct ImageBufferWriter {
writer: DataWriter<ImageBufferEvent>,
}

impl ImageBufferWriter {
pub fn new(writer: DataWriter<ImageBufferEvent>) -> Self {
Self { writer }
}

pub fn write(
&mut self,
rgb: Vec<Color>,
r: usize,
c: usize,
width: usize,
height: usize,
) -> Result<(), TracerError> {
self.writer.write(ImageBufferEvent::BufferUpdate {
rgb,
r,
c,
width,
height,
})
}
}

// The point of this is to combine all sources to the finished image source
pub struct ScreenBuffer {
buffer: Vec<Color>,
Expand Down Expand Up @@ -199,8 +169,8 @@ impl ScreenBuffer {
})
}

pub fn get_writer(&self) -> ImageBufferWriter {
ImageBufferWriter::new(self.bus.get_writer())
pub fn get_writer(&self) -> DataWriter<ImageBufferEvent> {
self.bus.get_writer()
}

pub fn rgb(&self) -> &[Color] {
Expand Down
11 changes: 0 additions & 11 deletions racer-tracer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@ mod tone_map;
mod util;
mod vec3;

// TODO:
// - Implement SVGF
// https://github.com/TheVaffel/spatiotemporal-variance-guided-filtering
// https://scholarship.tricolib.brynmawr.edu/bitstream/handle/10066/24508/2022HuangH.pdf?sequence=1&isAllowed=y
// https://teamwisp.github.io/research/svfg.html
// https://research.nvidia.com/sites/default/files/pubs/2017-07_Spatiotemporal-Variance-Guided-Filtering%3A//svgf_preprint.pdf
// Misc: https://cs.dartmouth.edu/wjarosz/publications/mara17towards.pdf
// - Renderers should not directly convert to the image output format.
// Don't call to_color
// - Tone mapping must be moved and used as a separate last step on the image as a whole.

extern crate image as img;

#[macro_use]
Expand Down
7 changes: 2 additions & 5 deletions racer-tracer/src/material/lambertian.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use crate::{
geometry::HitRecord,
material::Material,
ray::Ray,
texture::{solid_color::SolidColor, Texture},
Expand All @@ -22,11 +23,7 @@ impl Lambertian {
}

impl Material for Lambertian {
fn scatter(
&self,
ray: &crate::ray::Ray,
rec: &crate::geometry::HitRecord,
) -> Option<(Ray, Color)> {
fn scatter(&self, ray: &Ray, rec: &HitRecord) -> Option<(Ray, Color)> {
let mut scatter_direction = rec.normal + random_unit_vector();

// Catch bad scatter direction
Expand Down
Loading

0 comments on commit 206ca4e

Please sign in to comment.