Renderer 2, now with 100% less threading tools
I've rewritten the renderer to see if I can make a better model the second time around. I was having a rough time untangling parts and refactoring it piece-by-piece. Next is to hook up the new rendering parts into a single-threaded build. Once the parts work again, I can look into thread pooling machinery.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
mod primitives;
|
mod primitives;
|
||||||
mod renderer;
|
|
||||||
mod scene;
|
mod scene;
|
||||||
|
mod renderer;
|
||||||
|
|
||||||
use crate::primitives::Vec3;
|
use crate::primitives::Vec3;
|
||||||
use crate::scene::{
|
use crate::scene::{
|
||||||
@@ -46,11 +46,11 @@ fn main() {
|
|||||||
// Iterate lines, submitting them as tasks to the thread.
|
// Iterate lines, submitting them as tasks to the thread.
|
||||||
println!("P3\n{} {}\n255", image.0, image.1);
|
println!("P3\n{} {}\n255", image.0, image.1);
|
||||||
let context = renderer::RenderContext {
|
let context = renderer::RenderContext {
|
||||||
camera: cam,
|
camera: &scene.camera,
|
||||||
image,
|
image,
|
||||||
max_depth,
|
max_depth,
|
||||||
samples_per_pixel,
|
samples_per_pixel,
|
||||||
world,
|
world: scene.world,
|
||||||
};
|
};
|
||||||
|
|
||||||
thread::scope(|s| {
|
thread::scope(|s| {
|
||||||
|
|||||||
@@ -17,6 +17,96 @@ use rand::Rng;
|
|||||||
use rand::rngs::SmallRng;
|
use rand::rngs::SmallRng;
|
||||||
use rand::distributions::Uniform;
|
use rand::distributions::Uniform;
|
||||||
|
|
||||||
|
pub type Vec2i = Vec2<i32>;
|
||||||
|
pub type Vec2f = Vec2<f32>;
|
||||||
|
|
||||||
|
#[derive (Clone, Copy, PartialEq, PartialOrd, Debug)]
|
||||||
|
pub struct Vec2<T>{
|
||||||
|
pub x: T,
|
||||||
|
pub y: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Vec2<f32> {
|
||||||
|
pub fn zero() -> Vec2<f32> {
|
||||||
|
Vec2{ x: 0.0, y: 0.0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ones() -> Vec2<f32> {
|
||||||
|
Vec2{ x: 1.0, y: 1.0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rand(srng: &mut SmallRng, distrib: Uniform<f32>) -> Vec2<f32> {
|
||||||
|
Vec2 { x: srng.sample(distrib), y: srng.sample(distrib) }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <T> Vec2<T>
|
||||||
|
where T: std::ops::Mul{
|
||||||
|
pub fn new(x: T, y: T) -> Vec2<T> {
|
||||||
|
Vec2{x, y}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <T> Add for Vec2 <T>
|
||||||
|
where T: std::ops::Add<Output = T>{
|
||||||
|
type Output = Vec2<T>;
|
||||||
|
fn add(self, other: Vec2<T>) -> Vec2<T> {
|
||||||
|
Vec2 { x: self.x + other.x, y: self.y + other.y }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <T> Mul for Vec2<T>
|
||||||
|
where T: std::ops::Mul<Output = T>{
|
||||||
|
type Output = Vec2<T>;
|
||||||
|
fn mul(self, other: Vec2<T>) -> Vec2<T> {
|
||||||
|
Vec2 {
|
||||||
|
x: self.x * other.x,
|
||||||
|
y: self.y * other.y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Div<f32> for Vec2<f32>{
|
||||||
|
type Output = Vec2<f32>;
|
||||||
|
fn div(self, other: f32) -> Vec2<f32> {
|
||||||
|
Vec2 {
|
||||||
|
x: 1.0/other * self.x,
|
||||||
|
y: 1.0/other * self.y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Div<i32> for Vec2<i32>{
|
||||||
|
type Output = Vec2<i32>;
|
||||||
|
fn div(self, other: i32) -> Vec2<i32> {
|
||||||
|
Vec2 {
|
||||||
|
x: self.x / other,
|
||||||
|
y: self.y / other
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <T> Div<Vec2<T>> for Vec2<T>
|
||||||
|
where T: std::ops::Div<Output = T>{
|
||||||
|
type Output = Vec2<T>;
|
||||||
|
fn div(self, other: Vec2<T>) -> Vec2<T> {
|
||||||
|
Vec2 {
|
||||||
|
x: self.x / other.x,
|
||||||
|
y: self.y / other.y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <T> Display for Vec2<T>
|
||||||
|
where T: Display { // nested type still needs to have Display
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
let str = format!("{} {}", self.x, self.y);
|
||||||
|
fmt.write_str(&str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
|
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
|
||||||
pub struct Vec3{
|
pub struct Vec3{
|
||||||
pub x: f32,
|
pub x: f32,
|
||||||
@@ -308,6 +398,22 @@ pub struct Rect {
|
|||||||
pub h: i32,
|
pub h: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Rect{
|
||||||
|
pub fn pos(&self) -> Vec2i {
|
||||||
|
Vec2i {
|
||||||
|
x: self.x,
|
||||||
|
y: self.y,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn size(&self) -> Vec2i {
|
||||||
|
Vec2i {
|
||||||
|
x: self.w - self.x,
|
||||||
|
y: self.h - self.y,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test{
|
mod test{
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
315
src/renderer.rs
315
src/renderer.rs
@@ -1,270 +1,121 @@
|
|||||||
|
|
||||||
use crate::primitives::{Vec3, Ray, Rect};
|
use crate::primitives::{
|
||||||
|
Vec2i,
|
||||||
|
Vec2f,
|
||||||
|
Vec3,
|
||||||
|
Ray,
|
||||||
|
Rect,
|
||||||
|
};
|
||||||
use crate::scene::{
|
use crate::scene::{
|
||||||
Camera,
|
|
||||||
Hittable,
|
Hittable,
|
||||||
|
Scene,
|
||||||
};
|
};
|
||||||
|
|
||||||
use core::cmp::Ordering;
|
|
||||||
use std::thread;
|
|
||||||
use std::sync::mpsc;
|
|
||||||
use std::ops;
|
|
||||||
use rand::Rng;
|
|
||||||
use rand::rngs::SmallRng;
|
use rand::rngs::SmallRng;
|
||||||
use rand::distributions::Uniform;
|
use rand::distributions::Uniform;
|
||||||
use itertools::Itertools;
|
|
||||||
|
|
||||||
// =================
|
const SKY_COLOR: Vec3 = Vec3 { x: 0.5, y: 0.7, z: 1.0};
|
||||||
// Description parts
|
|
||||||
// =================
|
|
||||||
|
|
||||||
#[derive (Clone)]
|
struct Distrs{
|
||||||
pub struct RenderContext{
|
zero_one: Uniform<f32>,
|
||||||
pub image: (i32, i32),
|
one_one: Uniform<f32>,
|
||||||
pub samples_per_pixel: u32,
|
|
||||||
pub max_depth: u32,
|
|
||||||
pub world: Hittable,
|
|
||||||
pub camera: Camera,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DistributionContianer {
|
struct RenderProperties {
|
||||||
pub distrib_zero_one: Uniform<f32>,
|
samples: u32, // samples are averaged results over a pixel
|
||||||
pub distrib_plusminus_one: Uniform<f32>,
|
bounces: u32, // bounces are how far the ray will travel (in hits not total distance)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DistributionContianer {
|
fn to_uv(coord: Vec2i, img_size: Vec2i) -> Vec2f {
|
||||||
fn new() -> Self {
|
let u = (coord.x as f32) / ((img_size.x - 1) as f32);
|
||||||
DistributionContianer {
|
let v = (coord.y as f32) / ((img_size.y - 1) as f32);
|
||||||
distrib_zero_one: Uniform::new(0.0, 1.0),
|
Vec2f::new(u, v)
|
||||||
distrib_plusminus_one: Uniform::new(-1.0, 1.0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// =============
|
fn ray_color(
|
||||||
// Drawing Parts
|
r: Ray, surface: &Hittable, depth: u32,
|
||||||
// =============
|
rng: &mut SmallRng,
|
||||||
|
distr: &Distrs,
|
||||||
fn render_line(y: i32, small_rng: &mut SmallRng, context: RenderContext, distr: &DistributionContianer) -> Vec<Vec3> {
|
) -> Vec3 {
|
||||||
//TODO: Ensure that the compiler hoists the distribution's out as constants
|
// recursion guard
|
||||||
// else, do so manually
|
|
||||||
(0..context.image.0).map(|x| {
|
|
||||||
sample_pixel(x, y, small_rng, &context, distr)
|
|
||||||
}).collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ray_color(r: Ray, world: &Hittable, depth: u32, srng: &mut SmallRng, distrib: Uniform<f32> ) -> Vec3 {
|
|
||||||
// recursion depth guard
|
|
||||||
if depth == 0 {
|
if depth == 0 {
|
||||||
return Vec3::zero();
|
return Vec3::zero();
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(rec) = world.hit(r, 0.001, f32::INFINITY){
|
// cast a ray, interrogate hit record
|
||||||
|
if let Some(record) = surface.hit(r, 0.001, f32::INFINITY){
|
||||||
let mut scattered = Ray {
|
let mut scattered = Ray {
|
||||||
orig: Vec3::zero(),
|
orig: Vec3::zero(),
|
||||||
dir: Vec3::zero()
|
dir: Vec3::zero(),
|
||||||
};
|
};
|
||||||
let mut attenuation = Vec3::zero();
|
let mut attenuation = Vec3::zero();
|
||||||
if rec.material.scatter(r, &rec, &mut attenuation, &mut scattered, srng) {
|
if record.material.scatter(
|
||||||
return attenuation * ray_color(scattered, world, depth-1, srng, distrib);
|
r,
|
||||||
};
|
&record,
|
||||||
}
|
&mut attenuation,
|
||||||
|
&mut scattered,
|
||||||
|
rng
|
||||||
|
) {
|
||||||
|
return attenuation * ray_color(
|
||||||
|
scattered, surface, depth-1, rng, distr
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} // TODO: explicit else block
|
||||||
|
// Rust gets angry about the inner if{} block because it evaluates to ()
|
||||||
|
// when the else path is taken. This is a problem for a function
|
||||||
|
// that returns Vec3 and not ().
|
||||||
|
|
||||||
let unitdir = Vec3::as_unit(r.dir);
|
{ // when nothing is struck, return sky color
|
||||||
let t = 0.5 * (unitdir.y + 1.0);
|
let unitdir = Vec3::as_unit(r.dir);
|
||||||
return Vec3::ones() * (1.0 - t) + Vec3::new(0.5, 0.7, 1.0) * t
|
let t = 0.5 * (unitdir.y + 1.0);
|
||||||
|
return Vec3::ones() * (1.0 - t) + SKY_COLOR * t
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sample_pixel(x: i32, y: i32, small_rng: &mut SmallRng, context: &RenderContext, distr: &DistributionContianer) -> Vec3{
|
fn sample_pixel(
|
||||||
(0..context.samples_per_pixel).into_iter().fold(
|
coord: Vec2i, // location in image/screen space
|
||||||
|
scene: &Scene, // scene we're drawing
|
||||||
|
render_props: &RenderProperties,
|
||||||
|
img_size: Vec2i,
|
||||||
|
// Supplied by the execution environment (the thread)
|
||||||
|
rng: &mut SmallRng,
|
||||||
|
dist: &Distrs,
|
||||||
|
) -> Vec3{
|
||||||
|
(0..render_props.samples)
|
||||||
|
.fold(
|
||||||
Vec3::zero(),
|
Vec3::zero(),
|
||||||
|color, _sample| {
|
|color, _sample| -> Vec3 {
|
||||||
let u = ((x as f32) + small_rng.sample(distr.distrib_zero_one)) / ((context.image.0 - 1) as f32);
|
let uv = to_uv(coord, img_size);
|
||||||
let v = ((y as f32) + small_rng.sample(distr.distrib_zero_one)) / ((context.image.1 - 1) as f32);
|
let ray = scene.camera.get_ray(uv.x, uv.y, rng);
|
||||||
let ray = context.camera.get_ray(u, v, small_rng);
|
color + ray_color(ray, &scene.world, render_props.bounces, rng, dist)
|
||||||
color + ray_color(ray, &context.world, context.max_depth, small_rng, distr.distrib_plusminus_one)
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===============
|
|
||||||
// Execution parts
|
|
||||||
// ===============
|
|
||||||
|
|
||||||
/* Iterable that produces pixels left-to-right, top-to-bottom.
|
|
||||||
* `Tile`s represent the render space, not the finished image.
|
|
||||||
* There is no internal pixel buffer
|
|
||||||
*/
|
|
||||||
|
|
||||||
type TileCursorIter = itertools::Product<ops::Range<i32>, ops::Range<i32>>;
|
|
||||||
|
|
||||||
struct Tile {
|
struct Tile {
|
||||||
bounds: Rect,
|
bounds: Rect,
|
||||||
context: RenderContext,
|
pixels: Vec<Vec3>,
|
||||||
small_rng: SmallRng,
|
|
||||||
rand_distr: DistributionContianer,
|
|
||||||
cursor: TileCursorIter,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tile{
|
impl Tile {
|
||||||
fn new(
|
pub fn render_line(
|
||||||
bounds: Rect,
|
bounds: Rect, y: i32, // bounding rect and line
|
||||||
context: RenderContext,
|
scene: &Scene,
|
||||||
small_rng: SmallRng,
|
properties: &RenderProperties,
|
||||||
rand_distr: DistributionContianer
|
rng: &mut SmallRng, distr: &Distrs // rng utils
|
||||||
) -> Self
|
) -> Self {
|
||||||
{
|
let pixels = (0..bounds.w)
|
||||||
Tile { bounds, context, small_rng, rand_distr,
|
.map ( |x| -> Vec3{
|
||||||
cursor: (bounds.x..(bounds.x + bounds.w))
|
sample_pixel(
|
||||||
.cartesian_product(bounds.y..(bounds.y + bounds.h)
|
Vec2i{x, y},
|
||||||
|
scene,
|
||||||
|
properties,
|
||||||
|
bounds.size(),
|
||||||
|
rng,
|
||||||
|
distr
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
|
.collect();
|
||||||
|
Self { bounds, pixels }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Iterator for Tile {
|
|
||||||
type Item = Vec3;
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
if let Some((x, y)) = self.cursor.next(){
|
|
||||||
Some(sample_pixel(
|
|
||||||
x, y,
|
|
||||||
&mut self.small_rng,
|
|
||||||
&self.context,
|
|
||||||
&self.rand_distr,
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive (Clone)]
|
|
||||||
pub enum RenderCommand{
|
|
||||||
Stop,
|
|
||||||
Line { line_num: i32, context: RenderContext },
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct RenderResult {
|
|
||||||
pub line_num: i32,
|
|
||||||
pub line: Vec<Vec3>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ord for RenderResult {
|
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
|
||||||
if self.line_num > other.line_num {
|
|
||||||
Ordering::Less
|
|
||||||
} else if self.line_num < other.line_num {
|
|
||||||
Ordering::Greater
|
|
||||||
} else {
|
|
||||||
Ordering::Equal
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for RenderResult {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
Some(self.cmp(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for RenderResult {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.line_num == other.line_num
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for RenderResult {}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* The dispatcher will hold a list of threads, and a list of command input channels to match.
|
|
||||||
* Helper functions exist to input jobs serially, and then dispatch them to an open thread.
|
|
||||||
*
|
|
||||||
* Since receivers can be matched to several senders, the input end of the result channel will
|
|
||||||
* be cloned and given to each of the threads.
|
|
||||||
* TODO: Consider holding a copy of the render_tx end in case threads exit early and need to
|
|
||||||
* be restored.
|
|
||||||
*/
|
|
||||||
pub struct Dispatcher{
|
|
||||||
handles: Vec<thread::JoinHandle<()>>,
|
|
||||||
command_transmitters: Vec<mpsc::SyncSender<RenderCommand>>,
|
|
||||||
next_to_feed: usize, // gonna do a round-robin style dispatch, ig.
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Dispatcher {
|
|
||||||
pub fn new(srng: &SmallRng, num_threads: usize) -> (Dispatcher, mpsc::Receiver<RenderResult> ) {
|
|
||||||
let mut handles = Vec::new();
|
|
||||||
let mut command_transmitters = Vec::<mpsc::SyncSender<RenderCommand>>::new();
|
|
||||||
|
|
||||||
let (render_tx, render_rx) = mpsc::sync_channel::<RenderResult>(1);
|
|
||||||
|
|
||||||
for _ in 0..num_threads {
|
|
||||||
// create new command tx/rx pairs. Store tx in the list, give rx to the thread.
|
|
||||||
let (command_tx, command_rx) = mpsc::sync_channel::<RenderCommand>(1);
|
|
||||||
// TODO: Pick appropriate command queue depth (or make it controllable, even)
|
|
||||||
|
|
||||||
|
|
||||||
let mut srng = srng.clone();
|
|
||||||
let threads_result_tx = render_tx.clone();
|
|
||||||
let distribs = DistributionContianer::new();
|
|
||||||
let thread_handle = thread::spawn(move || {
|
|
||||||
while let Ok(job) = command_rx.recv() {
|
|
||||||
match job {
|
|
||||||
RenderCommand::Stop => {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
RenderCommand::Line { line_num, context } => {
|
|
||||||
let line = render_line(line_num, &mut srng, context, &distribs);
|
|
||||||
let result = RenderResult { line_num, line };
|
|
||||||
threads_result_tx.send(result).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
handles.push(thread_handle);
|
|
||||||
command_transmitters.push(command_tx);
|
|
||||||
}
|
|
||||||
// finally, stash everything in the Dispatcher struct and return.
|
|
||||||
(
|
|
||||||
Dispatcher{
|
|
||||||
handles,
|
|
||||||
command_transmitters,
|
|
||||||
next_to_feed: 0,
|
|
||||||
},
|
|
||||||
render_rx
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: Reconsider round-robin dispatch
|
|
||||||
// When passing the message to threads which are still busy, this function
|
|
||||||
// will block (it's a sync_channel). While blocked, other threads could
|
|
||||||
// become available and left idle.
|
|
||||||
pub fn submit_job(&mut self, command: RenderCommand) -> Result<(), mpsc::SendError<RenderCommand>> {
|
|
||||||
// Stop command is special. We'll broadcast it to all threads.
|
|
||||||
if let RenderCommand::Stop = command {
|
|
||||||
for channel in &self.command_transmitters {
|
|
||||||
return channel.send(command.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check that `next_to_feed` is in-bounds, and then insert.
|
|
||||||
// index is post-incremented with this function call.
|
|
||||||
|
|
||||||
// wrap when at length (0-indexed so last valid index is len-1)
|
|
||||||
if self.next_to_feed == self.handles.len() {
|
|
||||||
self.next_to_feed = 0;
|
|
||||||
} else if self.next_to_feed > self.handles.len() {
|
|
||||||
panic!("How the hell did a +=1 skip past the maximum allowed size?");
|
|
||||||
}
|
|
||||||
|
|
||||||
match self.command_transmitters.get(self.next_to_feed){
|
|
||||||
Some(target) => target.send(command).unwrap(),
|
|
||||||
None => panic!("oh god oh fuck"),
|
|
||||||
}
|
|
||||||
self.next_to_feed += 1;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -169,7 +169,6 @@ pub fn degrees_to_radians(degrees: f32) -> f32 {
|
|||||||
degrees * std::f32::consts::PI / 180.0
|
degrees * std::f32::consts::PI / 180.0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Clone, Copy)]
|
|
||||||
pub struct Camera {
|
pub struct Camera {
|
||||||
origin: Vec3,
|
origin: Vec3,
|
||||||
lower_left_corner: Vec3,
|
lower_left_corner: Vec3,
|
||||||
|
|||||||
Reference in New Issue
Block a user