Skip to content

Commit

Permalink
Merge pull request #196 from Walther/2024-02-06
Browse files Browse the repository at this point in the history
2024-02-06 work
  • Loading branch information
Walther authored Feb 9, 2024
2 parents e4df3c6 + a3f337c commit 99c4b6e
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 20 deletions.
27 changes: 20 additions & 7 deletions clovers-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ publish = false
name = "clovers-cli"
path = "src/main.rs"

[lib]
# For testing purposes
name = "clovers_draw_cpu"
path = "src/draw_cpu.rs"

[dependencies]
# Internal
clovers = { path = "../clovers", features = [
Expand All @@ -18,22 +23,30 @@ clovers = { path = "../clovers", features = [
], default-features = false }

# External
clap = { version = "4.4.7", features = ["std", "derive"] }
clap = { version = "4.4.18", features = ["std", "derive"] }
human_format = "1.0.3"
humantime = "2.1.0"
image = { version = "0.24.7", features = ["png"], default-features = false }
image = { version = "0.24.8", features = ["png"], default-features = false }
img-parts = "0.3.0"
indicatif = { version = "0.17.7", features = [
"rayon",
], default-features = false }
palette = { version = "0.7.3", features = ["serializing"] }
palette = { version = "0.7.4", features = ["serializing"] }
rand = { version = "0.8.5", features = [
"small_rng",
"getrandom",
], default-features = false }
rayon = "1.8.0"
serde = { version = "1.0.190", features = ["derive"], default-features = false }
rayon = "1.8.1"
serde = { version = "1.0.196", features = ["derive"], default-features = false }
serde_json = { version = "1.0", features = ["alloc"], default-features = false }
time = { version = "0.3.30", default-features = false }
time = { version = "0.3.34", default-features = false }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.17", features = ["time"] }
tracing-subscriber = { version = "0.3.18", features = ["time"] }

[dev-dependencies]
divan = "0.1.11"
proptest = "1"

[[bench]]
name = "draw_cpu"
harness = false
37 changes: 37 additions & 0 deletions clovers-cli/benches/draw_cpu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use clovers::scenes::{initialize, Scene, SceneFile};
use clovers::RenderOpts;
use clovers_draw_cpu::draw;
use divan::{black_box, AllocProfiler};

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::main();
}

const WIDTH: u32 = 256;
const HEIGHT: u32 = 256;
const OPTS: RenderOpts = RenderOpts {
width: WIDTH,
height: HEIGHT,
samples: 1,
max_depth: 100,
quiet: true,
normalmap: false,
};

#[divan::bench]
fn draw_cornell(bencher: divan::Bencher) {
bencher
.with_inputs(get_cornell)
.counter(1u32)
.bench_values(|scene| black_box(draw(OPTS, &scene)))
}

fn get_cornell<'scene>() -> Scene<'scene> {
const INPUT: &str = include_str!("../../scenes/cornell.json");
let scene_file: SceneFile = serde_json::from_str(INPUT).unwrap();
let scene: Scene = initialize(scene_file, WIDTH, HEIGHT);
scene
}
6 changes: 3 additions & 3 deletions clovers-cli/src/draw_cpu.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{colorize::colorize, normals::normal_map, ray::Ray, scenes, Float};
use clovers::RenderOpts;
use clovers::{
colorize::colorize, normals::normal_map, ray::Ray, scenes::Scene, Float, RenderOpts,
};
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
use palette::chromatic_adaptation::AdaptInto;
use palette::convert::IntoColorUnclamped;
Expand All @@ -8,7 +9,6 @@ use palette::{IntoColor, LinSrgb, Srgb, Xyz};
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use rayon::prelude::*;
use scenes::Scene;

/// The main drawing function, returns a `Vec<Srgb>` as a pixelbuffer.
pub fn draw(opts: RenderOpts, scene: &Scene) -> Vec<Srgb<u8>> {
Expand Down
10 changes: 7 additions & 3 deletions clovers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ traces = ["tracing"]

[dependencies]
enum_dispatch = "0.3.12"
gltf = { version = "1.3.0", optional = true }
gltf = { version = "1.4.0", optional = true }
nalgebra = { version = "0.32.3" }
palette = { version = "0.7.3", features = ["serializing"] }
palette = { version = "0.7.4", features = ["serializing"] }
rand = { version = "0.8.5", features = ["small_rng"], default-features = false }
rand_distr = "0.4.3"
serde = { version = "1.0.190", features = [
serde = { version = "1.0.196", features = [
"derive",
], default-features = false, optional = true }
stl_io = { version = "0.7.0", optional = true }
Expand Down Expand Up @@ -53,3 +53,7 @@ harness = false
[[bench]]
name = "spectrum"
harness = false

[[bench]]
name = "triangle"
harness = false
9 changes: 8 additions & 1 deletion clovers/benches/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use clovers::interval::Interval;
use clovers::ray::Ray;
use clovers::wavelength::random_wavelength;
use clovers::{aabb::*, Vec3};
use divan::black_box;
use divan::{black_box, AllocProfiler};
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::main();
}
Expand All @@ -19,20 +22,23 @@ fn new(bencher: divan::Bencher) {
let mut rng = SmallRng::from_entropy();
random_intervals(&mut rng)
})
.counter(1u32)
.bench_values(|(ab, cd, ef)| black_box(AABB::new(ab, cd, ef)))
}

#[divan::bench]
fn hit(bencher: divan::Bencher) {
bencher
.with_inputs(random_aabb_and_ray)
.counter(1u32)
.bench_values(|(aabb, ray)| black_box(aabb.hit(&ray, NEG_INFINITY, INFINITY)))
}

#[divan::bench]
fn hit_old(bencher: divan::Bencher) {
bencher
.with_inputs(random_aabb_and_ray)
.counter(1u32)
.bench_values(|(aabb, ray)| {
#[allow(deprecated)]
black_box(aabb.hit_old(&ray, NEG_INFINITY, INFINITY))
Expand All @@ -43,6 +49,7 @@ fn hit_old(bencher: divan::Bencher) {
fn hit_new(bencher: divan::Bencher) {
bencher
.with_inputs(random_aabb_and_ray)
.counter(1u32)
.bench_values(|(aabb, ray)| {
#[allow(deprecated)]
black_box(aabb.hit_new(&ray, NEG_INFINITY, INFINITY))
Expand Down
9 changes: 8 additions & 1 deletion clovers/benches/interval.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use clovers::interval::*;
use divan::black_box;
use divan::{black_box, AllocProfiler};
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::main();
}
Expand All @@ -14,6 +17,7 @@ fn new(bencher: divan::Bencher) {
let mut rng = SmallRng::from_entropy();
(rng.gen(), rng.gen())
})
.counter(1u32)
.bench_values(|(a, b)| black_box(Interval::new(a, b)))
}

Expand All @@ -26,6 +30,7 @@ fn new_from_intervals(bencher: divan::Bencher) {
let cd = random_interval(&mut rng);
(ab, cd)
})
.counter(1u32)
.bench_values(|(ab, cd)| black_box(Interval::new_from_intervals(ab, cd)))
}

Expand All @@ -38,6 +43,7 @@ fn expand(bencher: divan::Bencher) {
let delta = rng.gen();
(ab, delta)
})
.counter(1u32)
.bench_values(|(ab, delta)| black_box(ab.expand(delta)))
}

Expand All @@ -48,6 +54,7 @@ fn size(bencher: divan::Bencher) {
let mut rng = SmallRng::from_entropy();
random_interval(&mut rng)
})
.counter(1u32)
.bench_values(|ab| black_box(ab.size()))
}

Expand Down
9 changes: 8 additions & 1 deletion clovers/benches/random.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use clovers::{random::*, Vec3};
use divan::black_box;
use divan::{black_box, AllocProfiler};
use rand::rngs::SmallRng;
use rand::SeedableRng;

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::main();
}
Expand All @@ -11,27 +14,31 @@ fn main() {
fn unit_vector(bencher: divan::Bencher) {
bencher
.with_inputs(SmallRng::from_entropy)
.counter(1u32)
.bench_values(|mut rng| random_unit_vector(black_box(&mut rng)))
}

#[divan::bench]
fn unit_disk(bencher: divan::Bencher) {
bencher
.with_inputs(SmallRng::from_entropy)
.counter(1u32)
.bench_values(|mut rng| random_in_unit_disk(black_box(&mut rng)))
}

#[divan::bench]
fn cosine_direction(bencher: divan::Bencher) {
bencher
.with_inputs(SmallRng::from_entropy)
.counter(1u32)
.bench_values(|mut rng| random_cosine_direction(black_box(&mut rng)))
}

#[divan::bench]
fn hemisphere(bencher: divan::Bencher) {
bencher
.with_inputs(SmallRng::from_entropy)
.counter(1u32)
.bench_values(|mut rng| {
let normal = Vec3::new(1.0, 0.0, 0.0);
random_on_hemisphere(normal, black_box(&mut rng))
Expand Down
5 changes: 5 additions & 0 deletions clovers/benches/spectrum.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use clovers::spectrum::*;
use clovers::wavelength::*;
use divan::black_box;
use divan::AllocProfiler;
use palette::white_point::E;
use palette::Xyz;
use rand::rngs::SmallRng;
use rand::SeedableRng;

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::main();
}
Expand All @@ -19,5 +23,6 @@ fn xyz_to_p(bencher: divan::Bencher) {
let xyz: Xyz<E> = Xyz::new(1.0, 1.0, 1.0);
(wave, xyz)
})
.counter(1u32)
.bench_values(|(wave, xyz)| black_box(spectrum_xyz_to_p(wave, xyz)))
}
82 changes: 82 additions & 0 deletions clovers/benches/triangle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::f32::{INFINITY, NEG_INFINITY};

use clovers::hitable::HitableTrait;
use clovers::materials::Material;
use clovers::objects::Triangle;
use clovers::ray::Ray;
use clovers::wavelength::random_wavelength;
use clovers::Vec3;
use divan::black_box;
use divan::AllocProfiler;
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();

fn main() {
divan::main();
}

#[divan::bench]
fn new(bencher: divan::Bencher) {
bencher
.with_inputs(random_triangle_ingredients)
.counter(1u32)
.bench_values(|(a, b, c, material)| black_box(Triangle::new(a, b, c, material)))
}

#[divan::bench]
fn hit(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let rng = SmallRng::from_entropy();
let (triangle, ray) = random_triangle_and_ray();
(triangle, ray, rng)
})
.counter(1u32)
.bench_values(|(triangle, ray, mut rng)| {
black_box(
triangle
.hit(&ray, NEG_INFINITY, INFINITY, &mut rng)
.is_some(),
)
})
}

fn random_vec(rng: &mut SmallRng) -> Vec3 {
Vec3::new(rng.gen(), rng.gen(), rng.gen())
}

fn random_3_coords(rng: &mut SmallRng) -> (Vec3, Vec3, Vec3) {
let a = random_vec(rng);
let b = random_vec(rng);
let c = random_vec(rng);
(a, b, c)
}

fn random_triangle_ingredients() -> (Vec3, Vec3, Vec3, &'static Material) {
let mut rng = SmallRng::from_entropy();
let material: &'static Material = Box::leak(Box::default());
let (a, b, c) = random_3_coords(&mut rng);
(a, b, c, material)
}

fn random_triangle() -> Triangle<'static> {
let (a, b, c, material) = random_triangle_ingredients();
Triangle::new(a, b, c, material)
}

fn random_ray() -> Ray {
let mut rng = SmallRng::from_entropy();
black_box(Ray {
origin: Vec3::new(0.0, 0.0, 0.0),
direction: Vec3::new(rng.gen(), rng.gen(), rng.gen()),
time: rng.gen(),
wavelength: random_wavelength(&mut rng),
})
}

fn random_triangle_and_ray() -> (Triangle<'static>, Ray) {
(random_triangle(), random_ray())
}
Loading

0 comments on commit 99c4b6e

Please sign in to comment.