From 5916155b8ea8c08192c3d1e661ce74b199922e66 Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Mon, 1 Mar 2021 23:49:34 +0530 Subject: [PATCH] Refactored the program before reimplementing BVH, Updated dependencies --- Cargo.lock | 151 ++++++++++++++++---------------- Cargo.toml | 7 +- src/camera.rs | 2 +- src/demo.rs | 31 ++++--- src/{types => }/hitable.rs | 2 +- src/{types => }/hitable_list.rs | 8 +- src/main.rs | 12 +-- src/types/material.rs | 5 +- src/{types.rs => types/mod.rs} | 4 - src/types/moving_sphere.rs | 5 +- src/types/sphere.rs | 5 +- 11 files changed, 121 insertions(+), 111 deletions(-) rename src/{types => }/hitable.rs (96%) rename src/{types => }/hitable_list.rs (74%) rename src/{types.rs => types/mod.rs} (65%) diff --git a/Cargo.lock b/Cargo.lock index 655eb86..326dacf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,10 +1,12 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "autocfg" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "bitflags" @@ -19,74 +21,78 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] -name = "crossbeam-deque" -version = "0.7.3" +name = "cfg-if" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "crossbeam-channel" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775" dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" +dependencies = [ + "cfg-if 1.0.0", "crossbeam-epoch", "crossbeam-utils", - "maybe-uninit", ] [[package]] name = "crossbeam-epoch" -version = "0.8.2" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" +checksum = "2584f639eb95fea8c798496315b297cf81b9b58b6d30ab066a75455333cf4b12" dependencies = [ - "autocfg", - "cfg-if", + "cfg-if 1.0.0", "crossbeam-utils", "lazy_static", - "maybe-uninit", "memoffset", "scopeguard", ] -[[package]] -name = "crossbeam-queue" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c695eeca1e7173472a32221542ae469b3e9aac3a4fc81f7696bcad82029493db" -dependencies = [ - "cfg-if", - "crossbeam-utils", -] - [[package]] name = "crossbeam-utils" -version = "0.7.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +checksum = "e7e9d99fa91428effe99c5c6d4634cdeba32b8cf784fc428a2a687f61a952c49" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 1.0.0", "lazy_static", ] [[package]] name = "either" -version = "1.5.3" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" [[package]] name = "getrandom" -version = "0.1.14" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "wasi", ] [[package]] name = "hermit-abi" -version = "0.1.8" +version = "0.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1010591b26bbfe835e9faeabeb11866061cc7dcebffd56ad7d0942d0e61aefd8" +checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" dependencies = [ "libc", ] @@ -99,30 +105,24 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.68" +version = "0.2.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dea0c0405123bba743ee3f91f49b1c7cfb684eef0da0a50110f758ccf24cdff0" - -[[package]] -name = "maybe-uninit" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" [[package]] name = "memoffset" -version = "0.5.4" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4fc2c02a7e374099d4ee95a193111f72d2110197fe200272371758f6c3643d8" +checksum = "157b4208e3059a8f9e78d559edc658e13df41410cb3ae03979c83130067fdd87" dependencies = [ "autocfg", ] [[package]] name = "num_cpus" -version = "1.12.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46203554f085ff89c235cd12f7075f3233af9b11ed7c9e16dfe2560d03313ce6" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" dependencies = [ "hermit-abi", "libc", @@ -130,29 +130,27 @@ dependencies = [ [[package]] name = "ppv-lite86" -version = "0.2.6" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" [[package]] name = "rand" -version = "0.7.3" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" dependencies = [ - "getrandom", "libc", "rand_chacha", "rand_core", "rand_hc", - "rand_pcg", ] [[package]] name = "rand_chacha" -version = "0.2.2" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" dependencies = [ "ppv-lite86", "rand_core", @@ -160,37 +158,29 @@ dependencies = [ [[package]] name = "rand_core" -version = "0.5.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" dependencies = [ "getrandom", ] [[package]] name = "rand_hc" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" -dependencies = [ - "rand_core", -] - -[[package]] -name = "rand_pcg" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" dependencies = [ "rand_core", ] [[package]] name = "rayon" -version = "1.3.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db6ce3297f9c85e16621bb8cca38a06779ffc31bb8184e1be4bed2be4678a098" +checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674" dependencies = [ + "autocfg", "crossbeam-deque", "either", "rayon-core", @@ -198,12 +188,12 @@ dependencies = [ [[package]] name = "rayon-core" -version = "1.7.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08a89b46efaf957e52b18062fb2f4660f8b8a4dde1807ca002690868ef2c85a9" +checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a" dependencies = [ + "crossbeam-channel", "crossbeam-deque", - "crossbeam-queue", "crossbeam-utils", "lazy_static", "num_cpus", @@ -226,9 +216,9 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "sdl2" -version = "0.33.0" +version = "0.34.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f74124048ea86b5cd50236b2443f6f57cf4625a8e8818009b4e50dbb8729a43" +checksum = "fcbb85f4211627a7291c83434d6bbfa723e28dcaa53c7606087e3c61929e4b9c" dependencies = [ "bitflags", "lazy_static", @@ -238,16 +228,23 @@ dependencies = [ [[package]] name = "sdl2-sys" -version = "0.33.0" +version = "0.34.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2e1deb61ff274d29fb985017d4611d4004b113676eaa9c06754194caf82094e" +checksum = "28d81feded049b9c14eceb4a4f6d596a98cebbd59abdba949c5552a015466d33" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", + "version-compare", ] [[package]] -name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" +name = "version-compare" +version = "0.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +checksum = "d63556a25bae6ea31b52e640d7c41d1ab27faba4ccb600013837a3d0b3994ca1" + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/Cargo.toml b/Cargo.toml index 8ea7aac..b6666d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,9 +6,10 @@ edition = "2018" [features] +default = ["gui"] gui = ["sdl2"] [dependencies] -sdl2 = { version = "0.33.0", optional = true } -rand = { version = "0.7.3", features = ["small_rng"] } -rayon = "1.3.0" +sdl2 = { version = "0.34.0", optional = true } +rand = { version = "0.8.3", features = ["small_rng"] } +rayon = "1.5.0" diff --git a/src/camera.rs b/src/camera.rs index 8901532..44dc032 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -71,7 +71,7 @@ impl Camera { pub fn get_ray(&self, u: f64, v: f64, rng: &mut R) -> Ray { let rd = random_in_unit_disk(rng) * self.lens_radius; let offset = self.u * rd.x() + self.v * rd.y(); - let time = self.shutter_open + rng.gen::() * (self.shutter_close - self.shutter_open); + let time = rng.gen_range(self.shutter_open..=self.shutter_close); Ray::new( self.origin + offset, self.lower_left_corner + self.horizontal * u + self.vertical * v - self.origin - offset, diff --git a/src/demo.rs b/src/demo.rs index 3ae7db7..a3e745e 100644 --- a/src/demo.rs +++ b/src/demo.rs @@ -2,9 +2,9 @@ use { crate::{ types::{ material::{Dielectric, Lambertian, Metal}, - Hitable, HitableList, MovingSphere, Ray, Sphere, Vec3, + MovingSphere, Ray, Sphere, Vec3, }, - Camera, HORIZONTAL_PARTITION, VERTICAL_PARTITION, + Camera, Hitable, HitableList, HORIZONTAL_PARTITION, VERTICAL_PARTITION, }, rand::{rngs::SmallRng, Rng, SeedableRng}, rayon::prelude::*, @@ -38,6 +38,9 @@ impl Display for Chunk { } } +pub trait ParallelHit: Hitable + Send + Sync {} +impl ParallelHit for T {} + pub struct Demo; impl Demo { @@ -45,7 +48,7 @@ impl Demo { "motion_blur" } - fn world(&self) -> HitableList { + fn world(&self) -> impl Hitable { let mut world = HitableList { list: Vec::with_capacity(500), }; @@ -53,7 +56,7 @@ impl Demo { let mut rng = rand::thread_rng(); let mut rng = SmallRng::from_rng(&mut rng).unwrap(); - world.push(Box::new(Sphere::new( + world.push(Arc::new(Sphere::new( Vec3::new(0.0, -1000.0, 0.0), 1000.0, Lambertian::new(Vec3::new(0.5, 0.5, 0.5)), @@ -72,7 +75,7 @@ impl Demo { if (center - l).length() > 0.9 { if choose_material_probability < 0.8 { // diffuse material - world.push(Box::new(MovingSphere::new( + world.push(Arc::new(MovingSphere::new( center, center + Vec3::new(0.0, 0.5 * rng.gen::(), 0.0), 0.0, @@ -86,7 +89,7 @@ impl Demo { ))); } else if choose_material_probability < 0.95 { // metal material - world.push(Box::new(Sphere::new( + world.push(Arc::new(Sphere::new( center, radius, Metal::with_fuzz( @@ -100,23 +103,23 @@ impl Demo { ))); } else { // glass material - world.push(Box::new(Sphere::new(center, radius, Dielectric::new(1.5)))); + world.push(Arc::new(Sphere::new(center, radius, Dielectric::new(1.5)))); } } } } - world.push(Box::new(Sphere::new( + world.push(Arc::new(Sphere::new( Vec3::new(0.0, 1.0, 0.0), 1.0, Dielectric::new(1.5), ))); - world.push(Box::new(Sphere::new( + world.push(Arc::new(Sphere::new( Vec3::new(-4.0, 1.0, 0.0), 1.0, Lambertian::new(Vec3::new(0.4, 0.2, 0.1)), ))); - world.push(Box::new(Sphere::new( + world.push(Arc::new(Sphere::new( Vec3::new(4.0, 1.0, 0.0), 1.0, Metal::with_fuzz(Vec3::new(0.7, 0.6, 0.5), 0.0), @@ -143,7 +146,7 @@ impl Demo { ) } - fn render_chunk(&self, chunk: &mut Chunk, camera: &Camera, world: &HitableList, samples: u8) { + fn render_chunk(&self, chunk: &mut Chunk, camera: &Camera, world: &impl Hitable, samples: u8) { let &mut Chunk { num: _, x, @@ -168,7 +171,7 @@ impl Demo { let v = (j as f64 + rng.gen::()) / y as f64; let ray = camera.get_ray(u, v, &mut rng); - color += calc_color(ray, &world, 0, &mut rng); + color += calc_color(ray, world, 0, &mut rng); } color /= samples as f64; @@ -270,14 +273,14 @@ impl Demo { } } -fn calc_color(ray: Ray, world: &HitableList, depth: u32, rng: &mut SmallRng) -> Vec3 { +fn calc_color(ray: Ray, world: &T, depth: u32, rng: &mut SmallRng) -> Vec3 { if let Some(hit_rec) = world.hit(&ray, 0.001, std::f64::MAX) { if depth >= 50 { Vec3::new(0.0, 0.0, 0.0) } else { let material = hit_rec.material; if let (attenuation, Some(scattered_ray)) = material.scatter(&ray, &hit_rec, rng) { - calc_color(scattered_ray, &world, depth + 1, rng) * attenuation + calc_color(scattered_ray, world, depth + 1, rng) * attenuation } else { Vec3::new(0.0, 0.0, 0.0) } diff --git a/src/types/hitable.rs b/src/hitable.rs similarity index 96% rename from src/types/hitable.rs rename to src/hitable.rs index c67dd9a..625fd24 100644 --- a/src/types/hitable.rs +++ b/src/hitable.rs @@ -21,7 +21,7 @@ pub struct HitRecord<'a> { pub material: &'a dyn Material, } -pub trait Hitable: Send + Sync { +pub trait Hitable { fn hit(&self, _ray: &Ray, _t_min: f64, _t_max: f64) -> Option { None } diff --git a/src/types/hitable_list.rs b/src/hitable_list.rs similarity index 74% rename from src/types/hitable_list.rs rename to src/hitable_list.rs index f2fac57..d2f52cd 100644 --- a/src/types/hitable_list.rs +++ b/src/hitable_list.rs @@ -1,7 +1,9 @@ -use crate::types::{HitRecord, Hitable, Ray}; +use std::sync::Arc; + +use crate::{demo::ParallelHit, types::Ray, HitRecord, Hitable}; pub struct HitableList { - pub list: Vec>, + pub list: Vec>, } impl Hitable for HitableList { @@ -19,7 +21,7 @@ impl Hitable for HitableList { } impl HitableList { - pub fn push(&mut self, obj: Box) { + pub fn push(&mut self, obj: Arc) { self.list.push(obj); } } diff --git a/src/main.rs b/src/main.rs index b0259b0..261fcc1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,22 @@ #![allow(clippy::suspicious_arithmetic_impl)] -#![feature(test)] -extern crate test; mod camera; mod demo; +mod hitable; +mod hitable_list; mod types; pub use camera::Camera; +pub use hitable::{HitRecord, Hitable}; +pub use hitable_list::HitableList; use std::time::Instant; -const NUM_SAMPLES: u8 = 100; +const NUM_SAMPLES: u8 = 5; const VERTICAL_PARTITION: usize = 8; const HORIZONTAL_PARTITION: usize = 8; -const WIDTH: usize = 800; -const HEIGHT: usize = 800; +const WIDTH: usize = 1920; +const HEIGHT: usize = 1080; fn main() -> Result<(), String> { run(WIDTH, HEIGHT) diff --git a/src/types/material.rs b/src/types/material.rs index 6da58c9..2380508 100644 --- a/src/types/material.rs +++ b/src/types/material.rs @@ -1,5 +1,8 @@ use { - crate::types::{HitRecord, Ray, Vec3}, + crate::{ + types::{Ray, Vec3}, + HitRecord, + }, rand::{rngs::SmallRng, Rng}, }; diff --git a/src/types.rs b/src/types/mod.rs similarity index 65% rename from src/types.rs rename to src/types/mod.rs index e864b9e..d05bb9a 100644 --- a/src/types.rs +++ b/src/types/mod.rs @@ -1,13 +1,9 @@ -mod hitable; -mod hitable_list; pub mod material; mod moving_sphere; mod ray; mod sphere; mod vec3; -pub use hitable::{HitRecord, Hitable}; -pub use hitable_list::HitableList; pub use material::Material; pub use moving_sphere::MovingSphere; pub use ray::Ray; diff --git a/src/types/moving_sphere.rs b/src/types/moving_sphere.rs index 2791a4e..c680a9e 100644 --- a/src/types/moving_sphere.rs +++ b/src/types/moving_sphere.rs @@ -1,4 +1,7 @@ -use crate::types::{HitRecord, Hitable, Material, Ray, Vec3}; +use crate::{ + types::{Material, Ray, Vec3}, + HitRecord, Hitable, +}; pub struct MovingSphere { radius: f64, diff --git a/src/types/sphere.rs b/src/types/sphere.rs index 10a6b87..1df8d74 100644 --- a/src/types/sphere.rs +++ b/src/types/sphere.rs @@ -1,4 +1,7 @@ -use crate::types::{HitRecord, Hitable, Material, Ray, Vec3}; +use crate::{ + types::{Material, Ray, Vec3}, + HitRecord, Hitable, +}; pub struct Sphere { center: Vec3,