Replace unnecessary Arcs with Boxes.

This commit is contained in:
Matthew Gordon 2020-03-27 20:34:19 -04:00
parent 50d9848faa
commit bbeb39ba5d
4 changed files with 24 additions and 33 deletions

View File

@ -17,18 +17,18 @@ fn simple_scene(bencher: &mut Criterion) {
let image_width = 6; let image_width = 6;
let image_height = 6; let image_height = 6;
let scene = Arc::new(Scene { let scene = Scene {
camera_location: Point3::new(-2.0, 1.0, -5.0), camera_location: Point3::new(-2.0, 1.0, -5.0),
objects: vec![ objects: vec![
Arc::new(Plane::new( Box::new(Plane::new(
Vector3::new(0.0, 1.0, 0.0), Vector3::new(0.0, 1.0, 0.0),
-2.0, -2.0,
Arc::new(LambertianMaterial { Arc::new(LambertianMaterial {
colour: ColourRgbF::new(0.55, 0.27, 0.04), colour: ColourRgbF::new(0.55, 0.27, 0.04),
diffuse_strength: 0.1, diffuse_strength: 0.1,
}), }),
)) as Arc<dyn Primitive<f64>>, )) as Box<dyn Primitive<f64>>,
Arc::new(Sphere::new( Box::new(Sphere::new(
Point3::new(-6.25, -0.5, 1.0), Point3::new(-6.25, -0.5, 1.0),
1.0, 1.0,
Arc::new(LambertianMaterial { Arc::new(LambertianMaterial {
@ -36,7 +36,7 @@ fn simple_scene(bencher: &mut Criterion) {
diffuse_strength: 0.1, diffuse_strength: 0.1,
}), }),
)), )),
Arc::new(Sphere::new( Box::new(Sphere::new(
Point3::new(-4.25, -0.5, 2.0), Point3::new(-4.25, -0.5, 2.0),
1.0, 1.0,
Arc::new(ReflectiveMaterial { Arc::new(ReflectiveMaterial {
@ -45,7 +45,7 @@ fn simple_scene(bencher: &mut Criterion) {
reflection_strength: 0.99, reflection_strength: 0.99,
}), }),
)), )),
Arc::new(Sphere::new( Box::new(Sphere::new(
Point3::new(-5.0, 1.5, 1.0), Point3::new(-5.0, 1.5, 1.0),
1.0, 1.0,
Arc::new(PhongMaterial { Arc::new(PhongMaterial {
@ -55,7 +55,7 @@ fn simple_scene(bencher: &mut Criterion) {
specular_strength: 1.0, specular_strength: 1.0,
}), }),
)), )),
Arc::new(BoundingVolumeHierarchy::build( Box::new(BoundingVolumeHierarchy::build(
&load_obj( &load_obj(
Path::new("/home/matthew/Downloads/bunny.obj"), Path::new("/home/matthew/Downloads/bunny.obj"),
Arc::new(PhongMaterial { Arc::new(PhongMaterial {
@ -68,19 +68,17 @@ fn simple_scene(bencher: &mut Criterion) {
.unwrap(), .unwrap(),
)), )),
], ],
}); };
bencher.bench_function("simple_scene", move |b| { bencher.bench_function("simple_scene", |b| {
let scene = Arc::clone(&scene); b.iter(|| {
b.iter(move || {
let scene = Arc::clone(&scene);
let tile = Tile { let tile = Tile {
start_column: 0, start_column: 0,
end_column: image_width, end_column: image_width,
start_row: 0, start_row: 0,
end_row: image_height, end_row: image_height,
}; };
partial_render_scene(scene, tile, image_height, image_width); partial_render_scene(&scene, tile, image_height, image_width);
}) })
}); });
} }

View File

@ -10,8 +10,6 @@ use super::util::Tile;
use crate::Real; use crate::Real;
use std::sync::Arc;
struct ImageSampler<T: Real> { struct ImageSampler<T: Real> {
image_height_pixels: usize, image_height_pixels: usize,
image_width_pixels: usize, image_width_pixels: usize,
@ -70,7 +68,7 @@ impl<T: Real> ImageSampler<T> {
const RECURSION_LIMIT: u16 = 32; const RECURSION_LIMIT: u16 = 32;
pub fn partial_render_scene<T: Real>( pub fn partial_render_scene<T: Real>(
scene: Arc<Scene<T>>, scene: &Scene<T>,
tile: Tile, tile: Tile,
height: usize, height: usize,
width: usize, width: usize,

View File

@ -11,8 +11,7 @@ use std::time::Duration;
use nalgebra::{Point3, Vector3}; use nalgebra::{Point3, Vector3};
use std::path::Path; use std::path::Path;
use std::sync::mpsc; use std::sync::{mpsc, Arc};
use std::sync::Arc;
use vanrijn::camera::partial_render_scene; use vanrijn::camera::partial_render_scene;
use vanrijn::colour::{ColourRgbF, NamedColour}; use vanrijn::colour::{ColourRgbF, NamedColour};
@ -68,18 +67,18 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
image_height as u32, image_height as u32,
)?; )?;
let scene = Arc::new(Scene { let scene = Scene {
camera_location: Point3::new(-2.0, 1.0, -5.0), camera_location: Point3::new(-2.0, 1.0, -5.0),
objects: vec![ objects: vec![
Arc::new(Plane::new( Box::new(Plane::new(
Vector3::new(0.0, 1.0, 0.0), Vector3::new(0.0, 1.0, 0.0),
-2.0, -2.0,
Arc::new(LambertianMaterial { Arc::new(LambertianMaterial {
colour: ColourRgbF::new(0.55, 0.27, 0.04), colour: ColourRgbF::new(0.55, 0.27, 0.04),
diffuse_strength: 0.1, diffuse_strength: 0.1,
}), }),
)) as Arc<dyn Primitive<f64>>, )) as Box<dyn Primitive<f64>>,
Arc::new(Sphere::new( Box::new(Sphere::new(
Point3::new(-6.25, -0.5, 1.0), Point3::new(-6.25, -0.5, 1.0),
1.0, 1.0,
Arc::new(LambertianMaterial { Arc::new(LambertianMaterial {
@ -87,7 +86,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
diffuse_strength: 0.1, diffuse_strength: 0.1,
}), }),
)), )),
Arc::new(Sphere::new( Box::new(Sphere::new(
Point3::new(-4.25, -0.5, 2.0), Point3::new(-4.25, -0.5, 2.0),
1.0, 1.0,
Arc::new(ReflectiveMaterial { Arc::new(ReflectiveMaterial {
@ -96,7 +95,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
reflection_strength: 0.99, reflection_strength: 0.99,
}), }),
)), )),
Arc::new(Sphere::new( Box::new(Sphere::new(
Point3::new(-5.0, 1.5, 1.0), Point3::new(-5.0, 1.5, 1.0),
1.0, 1.0,
Arc::new(PhongMaterial { Arc::new(PhongMaterial {
@ -106,7 +105,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
specular_strength: 1.0, specular_strength: 1.0,
}), }),
)), )),
Arc::new(BoundingVolumeHierarchy::build( Box::new(BoundingVolumeHierarchy::build(
&load_obj( &load_obj(
Path::new("/home/matthew/Downloads/bunny.obj"), Path::new("/home/matthew/Downloads/bunny.obj"),
Arc::new(ReflectiveMaterial { Arc::new(ReflectiveMaterial {
@ -118,21 +117,19 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
.unwrap(), .unwrap(),
)), )),
], ],
}); };
let mut event_pump = sdl_context.event_pump()?; let mut event_pump = sdl_context.event_pump()?;
let (tile_tx, tile_rx) = mpsc::channel(); let (tile_tx, tile_rx) = mpsc::channel();
let mut tile_rx = Some(tile_rx); let mut tile_rx = Some(tile_rx);
let worker_boss = std::thread::spawn(move || { let worker_boss = std::thread::spawn(move|| {
TileIterator::new(image_width as usize, image_height as usize, 32) TileIterator::new(image_width as usize, image_height as usize, 32)
.map(move |tile| (tile, tile_tx.clone())) .map(move |tile| (tile, tile_tx.clone()))
.par_bridge() .par_bridge()
.try_for_each(|(tile, tx)| { .try_for_each(|(tile, tx)| {
let scene_ptr = scene.clone(); let rendered_tile = partial_render_scene(&scene, tile, image_height, image_width);
let rendered_tile =
partial_render_scene(scene_ptr, tile, image_height, image_width);
// There's nothing we can do if this fails, and we're already // There's nothing we can do if this fails, and we're already
// at the end of the function anyway, so just ignore result. // at the end of the function anyway, so just ignore result.

View File

@ -3,9 +3,7 @@ use nalgebra::{Point3};
use crate::raycasting::Primitive; use crate::raycasting::Primitive;
use crate::Real; use crate::Real;
use std::sync::Arc;
pub struct Scene<T: Real> { pub struct Scene<T: Real> {
pub camera_location: Point3<T>, pub camera_location: Point3<T>,
pub objects: Vec<Arc<dyn Primitive<T>>>, pub objects: Vec<Box<dyn Primitive<T>>>,
} }