Compare commits

..

No commits in common. "e0ee0d6dffcff3969ca9f1d5fad54dd4c97408f1" and "706902dc8e027b27f62214c6694aded339b8f838" have entirely different histories.

33 changed files with 418 additions and 377 deletions

16
Cargo.lock generated
View File

@ -11,9 +11,9 @@ dependencies = [
[[package]] [[package]]
name = "approx" name = "approx"
version = "0.4.0" version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f2a05fd1bd10b2527e20a2cd32d8873d115b8b39fe219ee25f42a8aca6ba278" checksum = "f0e60b75072ecd4168020818c0107f2857bb6c4e64252d8d3983f6263b40a5c3"
dependencies = [ dependencies = [
"num-traits", "num-traits",
] ]
@ -186,9 +186,11 @@ dependencies = [
[[package]] [[package]]
name = "glam" name = "glam"
version = "0.15.1" version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/bitshifter/glam-rs.git?rev=0f314f99#0f314f990710ff9357e5896de2b55ec82fe88e0d"
checksum = "411e0584defa447c328f25c756ba3d0685727ecc126b46c3c1176001141cd4b6" dependencies = [
"approx",
]
[[package]] [[package]]
name = "half" name = "half"
@ -607,9 +609,9 @@ checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8"
[[package]] [[package]]
name = "sobol_burley" name = "sobol_burley"
version = "0.3.0" version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26e3528b09b1f1b1e152342a4462d1e80d568dc5623a0772252a6e584a53d550" checksum = "92317634ef0e1ece7bd4031abc3186df3781d0bf2b05682796655200aef4c965"
[[package]] [[package]]
name = "spectral_upsampling" name = "spectral_upsampling"

View File

@ -30,12 +30,12 @@ nom = "5"
num_cpus = "1.8" num_cpus = "1.8"
openexr = "0.7" openexr = "0.7"
kioku = "0.3" kioku = "0.3"
sobol_burley = "0.3" sobol_burley = "0.1"
png_encode_mini = "0.1.2" png_encode_mini = "0.1.2"
rustc-serialize = "0.3" rustc-serialize = "0.3"
scoped_threadpool = "0.1" scoped_threadpool = "0.1"
time = "0.1" time = "0.1"
glam = "0.15" glam = {git="https://github.com/bitshifter/glam-rs.git", rev="0f314f99", default-features=false, features=["approx"]}
fastapprox = "0.3" fastapprox = "0.3"
# Local crate dependencies # Local crate dependencies

View File

@ -6,7 +6,7 @@
use std::mem::{transmute, MaybeUninit}; use std::mem::{transmute, MaybeUninit};
use glam::BVec4A; use glam::Vec4Mask;
use kioku::Arena; use kioku::Arena;
@ -123,12 +123,12 @@ impl<'a> BVH4<'a> {
traversal_code, traversal_code,
} => { } => {
node_tests += ray_stack.ray_count_in_next_task() as u64; node_tests += ray_stack.ray_count_in_next_task() as u64;
let mut all_hits = BVec4A::default(); let mut all_hits = Vec4Mask::default();
// Ray testing // Ray testing
ray_stack.pop_do_next_task_and_push_rays(children.len(), |ray_idx| { ray_stack.pop_do_next_task_and_push_rays(children.len(), |ray_idx| {
if rays.is_done(ray_idx) { if rays.is_done(ray_idx) {
BVec4A::default() Vec4Mask::default()
} else { } else {
let hits = if bounds.len() == 1 { let hits = if bounds.len() == 1 {
bounds[0].intersect_ray( bounds[0].intersect_ray(

View File

@ -7,7 +7,7 @@ use std::{
use crate::{ use crate::{
lerp::{lerp, lerp_slice, Lerp}, lerp::{lerp, lerp_slice, Lerp},
math::{fast_minf32, Point, Transform, Vector}, math::{fast_minf32, Matrix4x4, Point, Vector},
}; };
const BBOX_MAXT_ADJUST: f32 = 1.000_000_24; const BBOX_MAXT_ADJUST: f32 = 1.000_000_24;
@ -41,8 +41,8 @@ impl BBox {
// Returns whether the given ray intersects with the bbox. // Returns whether the given ray intersects with the bbox.
pub fn intersect_ray(&self, orig: Point, dir_inv: Vector, max_t: f32) -> bool { pub fn intersect_ray(&self, orig: Point, dir_inv: Vector, max_t: f32) -> bool {
// Calculate slab intersections // Calculate slab intersections
let t1 = (self.min.co - orig.co) * dir_inv.co; let t1 = (self.min.co - orig.co).truncate() * dir_inv.co;
let t2 = (self.max.co - orig.co) * dir_inv.co; let t2 = (self.max.co - orig.co).truncate() * dir_inv.co;
// Find the far and near intersection // Find the far and near intersection
let far_t = t1.max(t2).extend(std::f32::INFINITY); let far_t = t1.max(t2).extend(std::f32::INFINITY);
@ -55,7 +55,7 @@ impl BBox {
} }
// Creates a new BBox transformed into a different space. // Creates a new BBox transformed into a different space.
pub fn transformed(&self, xform: Transform) -> BBox { pub fn transformed(&self, xform: Matrix4x4) -> BBox {
// BBox corners // BBox corners
let vs = [ let vs = [
Point::new(self.min.x(), self.min.y(), self.min.z()), Point::new(self.min.x(), self.min.y(), self.min.z()),
@ -150,7 +150,7 @@ impl Lerp for BBox {
} }
} }
pub fn transform_bbox_slice_from(bbs_in: &[BBox], xforms: &[Transform], bbs_out: &mut Vec<BBox>) { pub fn transform_bbox_slice_from(bbs_in: &[BBox], xforms: &[Matrix4x4], bbs_out: &mut Vec<BBox>) {
bbs_out.clear(); bbs_out.clear();
// Transform the bounding boxes // Transform the bounding boxes

View File

@ -9,7 +9,7 @@ use crate::{
math::{Point, Vector}, math::{Point, Vector},
}; };
use glam::{BVec4A, Vec4}; use glam::{Vec4, Vec4Mask};
const BBOX_MAXT_ADJUST: f32 = 1.000_000_24; const BBOX_MAXT_ADJUST: f32 = 1.000_000_24;
@ -60,14 +60,14 @@ impl BBox4 {
} }
// Returns whether the given ray intersects with the bboxes. // Returns whether the given ray intersects with the bboxes.
pub fn intersect_ray(&self, orig: Point, dir_inv: Vector, max_t: f32) -> BVec4A { pub fn intersect_ray(&self, orig: Point, dir_inv: Vector, max_t: f32) -> Vec4Mask {
// Get the ray data into SIMD format. // Get the ray data into SIMD format.
let ro_x = Vec4::splat(orig.co[0]); let ro_x = Vec4::splat(orig.co.x());
let ro_y = Vec4::splat(orig.co[1]); let ro_y = Vec4::splat(orig.co.y());
let ro_z = Vec4::splat(orig.co[2]); let ro_z = Vec4::splat(orig.co.z());
let rdi_x = Vec4::splat(dir_inv.co[0]); let rdi_x = Vec4::splat(dir_inv.co.x());
let rdi_y = Vec4::splat(dir_inv.co[1]); let rdi_y = Vec4::splat(dir_inv.co.y());
let rdi_z = Vec4::splat(dir_inv.co[2]); let rdi_z = Vec4::splat(dir_inv.co.z());
let max_t = Vec4::splat(max_t); let max_t = Vec4::splat(max_t);
// Slab tests // Slab tests

View File

@ -4,14 +4,14 @@ use kioku::Arena;
use crate::{ use crate::{
lerp::lerp_slice, lerp::lerp_slice,
math::{Point, Transform, Vector}, math::{Matrix4x4, Point, Vector},
ray::Ray, ray::Ray,
sampling::square_to_circle, sampling::square_to_circle,
}; };
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct Camera<'a> { pub struct Camera<'a> {
transforms: &'a [Transform], transforms: &'a [Matrix4x4],
fovs: &'a [f32], fovs: &'a [f32],
tfovs: &'a [f32], tfovs: &'a [f32],
aperture_radii: &'a [f32], aperture_radii: &'a [f32],
@ -21,7 +21,7 @@ pub struct Camera<'a> {
impl<'a> Camera<'a> { impl<'a> Camera<'a> {
pub fn new( pub fn new(
arena: &'a Arena, arena: &'a Arena,
transforms: &[Transform], transforms: &[Matrix4x4],
fovs: &[f32], fovs: &[f32],
mut aperture_radii: &[f32], mut aperture_radii: &[f32],
mut focus_distances: &[f32], mut focus_distances: &[f32],

View File

@ -95,10 +95,10 @@ impl Color {
SpectralSample::from_parts( SpectralSample::from_parts(
// TODO: make this SIMD // TODO: make this SIMD
Vec4::new( Vec4::new(
plancks_law(temperature, wls[0]) * factor, plancks_law(temperature, wls.x()) * factor,
plancks_law(temperature, wls[1]) * factor, plancks_law(temperature, wls.y()) * factor,
plancks_law(temperature, wls[2]) * factor, plancks_law(temperature, wls.z()) * factor,
plancks_law(temperature, wls[3]) * factor, plancks_law(temperature, wls.w()) * factor,
), ),
hero_wavelength, hero_wavelength,
) )
@ -110,10 +110,10 @@ impl Color {
SpectralSample::from_parts( SpectralSample::from_parts(
// TODO: make this SIMD // TODO: make this SIMD
Vec4::new( Vec4::new(
plancks_law_normalized(temperature, wls[0]) * factor, plancks_law_normalized(temperature, wls.x()) * factor,
plancks_law_normalized(temperature, wls[1]) * factor, plancks_law_normalized(temperature, wls.y()) * factor,
plancks_law_normalized(temperature, wls[2]) * factor, plancks_law_normalized(temperature, wls.z()) * factor,
plancks_law_normalized(temperature, wls[3]) * factor, plancks_law_normalized(temperature, wls.w()) * factor,
), ),
hero_wavelength, hero_wavelength,
) )
@ -518,10 +518,10 @@ impl XYZ {
} }
pub fn from_spectral_sample(ss: &SpectralSample) -> XYZ { pub fn from_spectral_sample(ss: &SpectralSample) -> XYZ {
let xyz0 = XYZ::from_wavelength(ss.wl_n(0), ss.e[0]); let xyz0 = XYZ::from_wavelength(ss.wl_n(0), ss.e.x());
let xyz1 = XYZ::from_wavelength(ss.wl_n(1), ss.e[1]); let xyz1 = XYZ::from_wavelength(ss.wl_n(1), ss.e.y());
let xyz2 = XYZ::from_wavelength(ss.wl_n(2), ss.e[2]); let xyz2 = XYZ::from_wavelength(ss.wl_n(2), ss.e.z());
let xyz3 = XYZ::from_wavelength(ss.wl_n(3), ss.e[3]); let xyz3 = XYZ::from_wavelength(ss.wl_n(3), ss.e.w());
(xyz0 + xyz1 + xyz2 + xyz3) * 0.75 (xyz0 + xyz1 + xyz2 + xyz3) * 0.75
} }

View File

@ -1,6 +1,6 @@
#![allow(dead_code)] #![allow(dead_code)]
use math3d::{Normal, Point, Transform, Vector}; use math3d::{Matrix4x4, Normal, Point, Vector};
/// Trait for allowing a type to be linearly interpolated. /// Trait for allowing a type to be linearly interpolated.
pub trait Lerp: Copy { pub trait Lerp: Copy {
@ -106,8 +106,8 @@ impl Lerp for glam::Vec4 {
} }
} }
impl Lerp for Transform { impl Lerp for Matrix4x4 {
fn lerp(self, other: Transform, alpha: f32) -> Transform { fn lerp(self, other: Matrix4x4, alpha: f32) -> Matrix4x4 {
(self * (1.0 - alpha)) + (other * alpha) (self * (1.0 - alpha)) + (other * alpha)
} }
} }
@ -120,8 +120,8 @@ impl Lerp for Normal {
impl Lerp for Point { impl Lerp for Point {
fn lerp(self, other: Point, alpha: f32) -> Point { fn lerp(self, other: Point, alpha: f32) -> Point {
let s = self; let s = self.norm();
let o = other; let o = other.norm();
Point { Point {
co: (s.co * (1.0 - alpha)) + (o.co * alpha), co: (s.co * (1.0 - alpha)) + (o.co * alpha),
} }
@ -215,21 +215,23 @@ mod tests {
#[test] #[test]
fn lerp_matrix() { fn lerp_matrix() {
let a = Transform::new_from_values( let a = Matrix4x4::new_from_values(
0.0, 2.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 0.0, 2.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0,
); );
let b = Transform::new_from_values( let b = Matrix4x4::new_from_values(
-1.0, 1.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, -1.0, 1.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,
); );
let c1 = Transform::new_from_values( let c1 = Matrix4x4::new_from_values(
-0.25, 1.75, 2.25, 3.25, 4.25, 5.25, 6.25, 7.25, 8.25, 9.25, 10.25, 11.25, -0.25, 1.75, 2.25, 3.25, 4.25, 5.25, 6.25, 7.25, 8.25, 9.25, 10.25, 11.25, 12.25,
13.25, 14.25, 15.25,
); );
let c2 = Transform::new_from_values( let c2 = Matrix4x4::new_from_values(
-0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, -0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5,
); );
let c3 = Transform::new_from_values( let c3 = Matrix4x4::new_from_values(
-0.75, 1.25, 2.75, 3.75, 4.75, 5.75, 6.75, 7.75, 8.75, 9.75, 10.75, 11.75, -0.75, 1.25, 2.75, 3.75, 4.75, 5.75, 6.75, 7.75, 8.75, 9.75, 10.75, 11.75, 12.75,
13.75, 14.75, 15.75,
); );
assert_eq!(a.lerp(b, 0.0), a); assert_eq!(a.lerp(b, 0.0), a);

View File

@ -6,7 +6,7 @@ use std::fmt::Debug;
use crate::{ use crate::{
color::SpectralSample, color::SpectralSample,
math::{Normal, Point, Transform, Vector}, math::{Matrix4x4, Normal, Point, Vector},
surface::Surface, surface::Surface,
}; };
@ -34,7 +34,7 @@ pub trait SurfaceLight: Surface {
/// - The pdf of the sample. /// - The pdf of the sample.
fn sample_from_point( fn sample_from_point(
&self, &self,
space: &Transform, space: &Matrix4x4,
arr: Point, arr: Point,
u: f32, u: f32,
v: f32, v: f32,

View File

@ -5,7 +5,7 @@ use crate::{
boundable::Boundable, boundable::Boundable,
color::{Color, SpectralSample}, color::{Color, SpectralSample},
lerp::lerp_slice, lerp::lerp_slice,
math::{cross, dot, Normal, Point, Transform, Vector}, math::{cross, dot, Matrix4x4, Normal, Point, Vector},
ray::{RayBatch, RayStack}, ray::{RayBatch, RayStack},
sampling::{ sampling::{
spherical_triangle_solid_angle, triangle_surface_area, uniform_sample_spherical_triangle, spherical_triangle_solid_angle, triangle_surface_area, uniform_sample_spherical_triangle,
@ -51,7 +51,7 @@ impl<'a> RectangleLight<'a> {
// more efficiently by inlining it there. // more efficiently by inlining it there.
fn sample_pdf( fn sample_pdf(
&self, &self,
space: &Transform, space: &Matrix4x4,
arr: Point, arr: Point,
sample_dir: Vector, sample_dir: Vector,
hit_point: Point, hit_point: Point,
@ -97,7 +97,7 @@ impl<'a> RectangleLight<'a> {
// fn outgoing( // fn outgoing(
// &self, // &self,
// space: &Transform, // space: &Matrix4x4,
// dir: Vector, // dir: Vector,
// u: f32, // u: f32,
// v: f32, // v: f32,
@ -120,7 +120,7 @@ impl<'a> RectangleLight<'a> {
impl<'a> SurfaceLight for RectangleLight<'a> { impl<'a> SurfaceLight for RectangleLight<'a> {
fn sample_from_point( fn sample_from_point(
&self, &self,
space: &Transform, space: &Matrix4x4,
arr: Point, arr: Point,
u: f32, u: f32,
v: f32, v: f32,
@ -261,7 +261,7 @@ impl<'a> Surface for RectangleLight<'a> {
ray_stack: &mut RayStack, ray_stack: &mut RayStack,
isects: &mut [SurfaceIntersection], isects: &mut [SurfaceIntersection],
shader: &dyn SurfaceShader, shader: &dyn SurfaceShader,
space: &[Transform], space: &[Matrix4x4],
) { ) {
let _ = shader; // Silence 'unused' warning let _ = shader; // Silence 'unused' warning

View File

@ -7,7 +7,7 @@ use crate::{
boundable::Boundable, boundable::Boundable,
color::{Color, SpectralSample}, color::{Color, SpectralSample},
lerp::lerp_slice, lerp::lerp_slice,
math::{coordinate_system_from_vector, dot, Normal, Point, Transform, Vector}, math::{coordinate_system_from_vector, dot, Matrix4x4, Normal, Point, Vector},
ray::{RayBatch, RayStack}, ray::{RayBatch, RayStack},
sampling::{uniform_sample_cone, uniform_sample_cone_pdf, uniform_sample_sphere}, sampling::{uniform_sample_cone, uniform_sample_cone_pdf, uniform_sample_sphere},
shading::surface_closure::SurfaceClosure, shading::surface_closure::SurfaceClosure,
@ -50,7 +50,7 @@ impl<'a> SphereLight<'a> {
// more efficiently by inlining it there. // more efficiently by inlining it there.
fn sample_pdf( fn sample_pdf(
&self, &self,
space: &Transform, space: &Matrix4x4,
arr: Point, arr: Point,
sample_dir: Vector, sample_dir: Vector,
sample_u: f32, sample_u: f32,
@ -84,7 +84,7 @@ impl<'a> SphereLight<'a> {
impl<'a> SurfaceLight for SphereLight<'a> { impl<'a> SurfaceLight for SphereLight<'a> {
fn sample_from_point( fn sample_from_point(
&self, &self,
space: &Transform, space: &Matrix4x4,
arr: Point, arr: Point,
u: f32, u: f32,
v: f32, v: f32,
@ -210,7 +210,7 @@ impl<'a> Surface for SphereLight<'a> {
ray_stack: &mut RayStack, ray_stack: &mut RayStack,
isects: &mut [SurfaceIntersection], isects: &mut [SurfaceIntersection],
shader: &dyn SurfaceShader, shader: &dyn SurfaceShader,
space: &[Transform], space: &[Matrix4x4],
) { ) {
let _ = shader; // Silence 'unused' warning let _ = shader; // Silence 'unused' warning

View File

@ -2,7 +2,7 @@
use std::f32; use std::f32;
pub use math3d::{cross, dot, CrossProduct, DotProduct, Normal, Point, Transform, Vector}; pub use math3d::{cross, dot, CrossProduct, DotProduct, Matrix4x4, Normal, Point, Vector};
/// Clamps a value between a min and max. /// Clamps a value between a min and max.
pub fn clamp<T: PartialOrd>(v: T, lower: T, upper: T) -> T { pub fn clamp<T: PartialOrd>(v: T, lower: T, upper: T) -> T {

View File

@ -10,7 +10,7 @@ use crate::{
camera::Camera, camera::Camera,
color::{rec709_e_to_xyz, Color}, color::{rec709_e_to_xyz, Color},
light::WorldLightSource, light::WorldLightSource,
math::Transform, math::Matrix4x4,
renderer::Renderer, renderer::Renderer,
scene::Scene, scene::Scene,
scene::World, scene::World,
@ -553,17 +553,16 @@ fn parse_world<'a>(arena: &'a Arena, tree: &'a DataTree) -> Result<World<'a>, Ps
} }
} }
pub fn parse_matrix(contents: &str) -> Result<Transform, PsyParseError> { pub fn parse_matrix(contents: &str) -> Result<Matrix4x4, PsyParseError> {
if let IResult::Ok((leftover, ns)) = all_consuming(tuple(( if let IResult::Ok((leftover, ns)) = all_consuming(tuple((
ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32,
ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32, ws_f32,
)))(contents) )))(contents)
{ {
if leftover.is_empty() { if leftover.is_empty() {
return Ok(Transform::new_from_values( return Ok(Matrix4x4::new_from_values(
// We throw away the last row, since it's not necessarily affine. ns.0, ns.4, ns.8, ns.12, ns.1, ns.5, ns.9, ns.13, ns.2, ns.6, ns.10, ns.14, ns.3,
// TODO: is there a more correct way to handle this? ns.7, ns.11, ns.15,
ns.0, ns.4, ns.8, ns.12, ns.1, ns.5, ns.9, ns.13, ns.2, ns.6, ns.10, ns.14,
)); ));
} }
} }

View File

@ -1,8 +1,8 @@
#![allow(dead_code)] #![allow(dead_code)]
use glam::BVec4A; use glam::Vec4Mask;
use crate::math::{Point, Transform, Vector}; use crate::math::{Matrix4x4, Point, Vector};
type RayIndexType = u16; type RayIndexType = u16;
type FlagType = u8; type FlagType = u8;
@ -86,7 +86,7 @@ impl RayBatch {
pub fn set_from_ray(&mut self, ray: &Ray, is_occlusion: bool, idx: usize) { pub fn set_from_ray(&mut self, ray: &Ray, is_occlusion: bool, idx: usize) {
self.hot[idx].orig_local = ray.orig; self.hot[idx].orig_local = ray.orig;
self.hot[idx].dir_inv_local = Vector { self.hot[idx].dir_inv_local = Vector {
co: ray.dir.co.recip(), co: ray.dir.co.reciprocal(),
}; };
self.hot[idx].max_t = ray.max_t; self.hot[idx].max_t = ray.max_t;
self.hot[idx].time = ray.time; self.hot[idx].time = ray.time;
@ -119,10 +119,10 @@ impl RayBatch {
/// ///
/// This should be called when entering (and exiting) traversal of a /// This should be called when entering (and exiting) traversal of a
/// new transform space. /// new transform space.
pub fn update_local(&mut self, idx: usize, xform: &Transform) { pub fn update_local(&mut self, idx: usize, xform: &Matrix4x4) {
self.hot[idx].orig_local = self.cold[idx].orig * *xform; self.hot[idx].orig_local = self.cold[idx].orig * *xform;
self.hot[idx].dir_inv_local = Vector { self.hot[idx].dir_inv_local = Vector {
co: (self.cold[idx].dir * *xform).co.recip(), co: (self.cold[idx].dir * *xform).co.reciprocal(),
}; };
} }
@ -349,7 +349,7 @@ impl RayStack {
/// indicated lanes. /// indicated lanes.
pub fn pop_do_next_task_and_push_rays<F>(&mut self, output_lane_count: usize, mut handle_ray: F) pub fn pop_do_next_task_and_push_rays<F>(&mut self, output_lane_count: usize, mut handle_ray: F)
where where
F: FnMut(usize) -> BVec4A, F: FnMut(usize) -> Vec4Mask,
{ {
// Pop the task and do necessary bookkeeping. // Pop the task and do necessary bookkeeping.
let task = self.tasks.pop().unwrap(); let task = self.tasks.pop().unwrap();

View File

@ -706,7 +706,7 @@ fn get_sample_4d(
let seed = pixel_co.0 ^ (pixel_co.1 << 16) ^ seed.wrapping_mul(0x736caf6f); let seed = pixel_co.0 ^ (pixel_co.1 << 16) ^ seed.wrapping_mul(0x736caf6f);
match dimension_set { match dimension_set {
ds if ds < sobol_burley::NUM_DIMENSION_SETS_4D as u32 => { ds if ds < sobol_burley::MAX_DIMENSION_SET as u32 => {
// Sobol sampling. // Sobol sampling.
let n4 = sobol_burley::sample_4d(i, ds, seed); let n4 = sobol_burley::sample_4d(i, ds, seed);
(n4[0], n4[1], n4[2], n4[3]) (n4[0], n4[1], n4[2], n4[3])

View File

@ -10,7 +10,7 @@ use crate::{
color::SpectralSample, color::SpectralSample,
lerp::lerp_slice, lerp::lerp_slice,
light::SurfaceLight, light::SurfaceLight,
math::{Normal, Point, Transform}, math::{Matrix4x4, Normal, Point},
shading::SurfaceShader, shading::SurfaceShader,
surface::{Surface, SurfaceIntersection}, surface::{Surface, SurfaceIntersection},
transform_stack::TransformStack, transform_stack::TransformStack,
@ -21,7 +21,7 @@ pub struct Assembly<'a> {
// Instance list // Instance list
pub instances: &'a [Instance], pub instances: &'a [Instance],
pub light_instances: &'a [Instance], pub light_instances: &'a [Instance],
pub xforms: &'a [Transform], pub xforms: &'a [Matrix4x4],
// Surface shader list // Surface shader list
pub surface_shaders: &'a [&'a dyn SurfaceShader], pub surface_shaders: &'a [&'a dyn SurfaceShader],
@ -60,7 +60,7 @@ impl<'a> Assembly<'a> {
let sel_xform = if !xform_stack.top().is_empty() { let sel_xform = if !xform_stack.top().is_empty() {
lerp_slice(xform_stack.top(), time) lerp_slice(xform_stack.top(), time)
} else { } else {
Transform::new() Matrix4x4::new()
}; };
if let Some((light_i, sel_pdf, whittled_n)) = self.light_accel.select( if let Some((light_i, sel_pdf, whittled_n)) = self.light_accel.select(
idata.incoming * sel_xform, idata.incoming * sel_xform,
@ -90,7 +90,7 @@ impl<'a> Assembly<'a> {
if !pxforms.is_empty() { if !pxforms.is_empty() {
lerp_slice(pxforms, time) lerp_slice(pxforms, time)
} else { } else {
Transform::new() Matrix4x4::new()
} }
}; };
@ -152,7 +152,7 @@ pub struct AssemblyBuilder<'a> {
// Instance list // Instance list
instances: Vec<Instance>, instances: Vec<Instance>,
xforms: Vec<Transform>, xforms: Vec<Matrix4x4>,
// Shader list // Shader list
surface_shaders: Vec<&'a dyn SurfaceShader>, surface_shaders: Vec<&'a dyn SurfaceShader>,
@ -224,7 +224,7 @@ impl<'a> AssemblyBuilder<'a> {
&mut self, &mut self,
name: &str, name: &str,
surface_shader_name: Option<&str>, surface_shader_name: Option<&str>,
xforms: Option<&[Transform]>, xforms: Option<&[Matrix4x4]>,
) { ) {
// Make sure name exists // Make sure name exists
if !self.name_exists(name) { if !self.name_exists(name) {

View File

@ -492,23 +492,23 @@ mod ggx_closure {
let spectrum_sample = col.to_spectral_sample(wavelength); let spectrum_sample = col.to_spectral_sample(wavelength);
let rev_fresnel = 1.0 - fresnel; let rev_fresnel = 1.0 - fresnel;
let c0 = lerp( let c0 = lerp(
schlick_fresnel_from_fac(spectrum_sample.e[0], hb), schlick_fresnel_from_fac(spectrum_sample.e.x(), hb),
spectrum_sample.e[0], spectrum_sample.e.x(),
rev_fresnel, rev_fresnel,
); );
let c1 = lerp( let c1 = lerp(
schlick_fresnel_from_fac(spectrum_sample.e[1], hb), schlick_fresnel_from_fac(spectrum_sample.e.y(), hb),
spectrum_sample.e[1], spectrum_sample.e.y(),
rev_fresnel, rev_fresnel,
); );
let c2 = lerp( let c2 = lerp(
schlick_fresnel_from_fac(spectrum_sample.e[2], hb), schlick_fresnel_from_fac(spectrum_sample.e.z(), hb),
spectrum_sample.e[2], spectrum_sample.e.z(),
rev_fresnel, rev_fresnel,
); );
let c3 = lerp( let c3 = lerp(
schlick_fresnel_from_fac(spectrum_sample.e[3], hb), schlick_fresnel_from_fac(spectrum_sample.e.w(), hb),
spectrum_sample.e[3], spectrum_sample.e.w(),
rev_fresnel, rev_fresnel,
); );

View File

@ -9,7 +9,7 @@ use crate::{
bbox::BBox, bbox::BBox,
boundable::Boundable, boundable::Boundable,
lerp::lerp_slice, lerp::lerp_slice,
math::{cross, dot, Normal, Point, Transform}, math::{cross, dot, Matrix4x4, Normal, Point},
ray::{RayBatch, RayStack}, ray::{RayBatch, RayStack},
shading::SurfaceClosure, shading::SurfaceClosure,
}; };
@ -150,13 +150,13 @@ impl<'a> MicropolyBatch<'a> {
rays: &mut RayBatch, rays: &mut RayBatch,
ray_stack: &mut RayStack, ray_stack: &mut RayStack,
isects: &mut [SurfaceIntersection], isects: &mut [SurfaceIntersection],
space: &[Transform], space: &[Matrix4x4],
) { ) {
// Precalculate transform for non-motion blur cases // Precalculate transform for non-motion blur cases
let static_mat_space = if space.len() == 1 { let static_mat_space = if space.len() == 1 {
lerp_slice(space, 0.0).inverse() lerp_slice(space, 0.0).inverse()
} else { } else {
Transform::new() Matrix4x4::new()
}; };
self.accel self.accel

View File

@ -10,7 +10,7 @@ use std::fmt::Debug;
use crate::{ use crate::{
boundable::Boundable, boundable::Boundable,
math::{Normal, Point, Transform, Vector}, math::{Matrix4x4, Normal, Point, Vector},
ray::{RayBatch, RayStack}, ray::{RayBatch, RayStack},
shading::surface_closure::SurfaceClosure, shading::surface_closure::SurfaceClosure,
shading::SurfaceShader, shading::SurfaceShader,
@ -25,7 +25,7 @@ pub trait Surface: Boundable + Debug + Sync {
ray_stack: &mut RayStack, ray_stack: &mut RayStack,
isects: &mut [SurfaceIntersection], isects: &mut [SurfaceIntersection],
shader: &dyn SurfaceShader, shader: &dyn SurfaceShader,
space: &[Transform], space: &[Matrix4x4],
); );
} }
@ -86,7 +86,7 @@ pub struct SurfaceIntersectionData {
// a cube centered around `pos` with dimensions of `2 * pos_err`. // a cube centered around `pos` with dimensions of `2 * pos_err`.
pub nor: Normal, // Shading normal pub nor: Normal, // Shading normal
pub nor_g: Normal, // True geometric normal pub nor_g: Normal, // True geometric normal
pub local_space: Transform, // Matrix from global space to local space pub local_space: Matrix4x4, // Matrix from global space to local space
pub t: f32, // Ray t-value at the intersection point pub t: f32, // Ray t-value at the intersection point
pub sample_pdf: f32, // The PDF of getting this point by explicitly sampling the surface pub sample_pdf: f32, // The PDF of getting this point by explicitly sampling the surface
} }

View File

@ -7,7 +7,7 @@ use crate::{
bbox::BBox, bbox::BBox,
boundable::Boundable, boundable::Boundable,
lerp::lerp_slice, lerp::lerp_slice,
math::{cross, dot, Normal, Point, Transform}, math::{cross, dot, Matrix4x4, Normal, Point},
ray::{RayBatch, RayStack}, ray::{RayBatch, RayStack},
shading::SurfaceShader, shading::SurfaceShader,
}; };
@ -128,13 +128,13 @@ impl<'a> Surface for TriangleMesh<'a> {
ray_stack: &mut RayStack, ray_stack: &mut RayStack,
isects: &mut [SurfaceIntersection], isects: &mut [SurfaceIntersection],
shader: &dyn SurfaceShader, shader: &dyn SurfaceShader,
space: &[Transform], space: &[Matrix4x4],
) { ) {
// Precalculate transform for non-motion blur cases // Precalculate transform for non-motion blur cases
let static_mat_space = if space.len() == 1 { let static_mat_space = if space.len() == 1 {
lerp_slice(space, 0.0).inverse() lerp_slice(space, 0.0).inverse()
} else { } else {
Transform::new() Matrix4x4::new()
}; };
self.accel self.accel

View File

@ -4,7 +4,7 @@ use crate::{
accel::ray_code, accel::ray_code,
color::{rec709_to_xyz, Color}, color::{rec709_to_xyz, Color},
lerp::lerp_slice, lerp::lerp_slice,
math::Transform, math::Matrix4x4,
ray::{RayBatch, RayStack}, ray::{RayBatch, RayStack},
scene::{Assembly, InstanceType, Object}, scene::{Assembly, InstanceType, Object},
shading::{SimpleSurfaceShader, SurfaceShader}, shading::{SimpleSurfaceShader, SurfaceShader},
@ -63,7 +63,7 @@ impl<'a> TracerInner<'a> {
// Prep the accel part of the rays. // Prep the accel part of the rays.
{ {
let ident = Transform::new(); let ident = Matrix4x4::new();
for i in 0..rays.len() { for i in 0..rays.len() {
rays.update_local(i, &ident); rays.update_local(i, &ident);
} }
@ -140,7 +140,7 @@ impl<'a> TracerInner<'a> {
rays.update_local(ray_idx, &lerp_slice(xforms, t)); rays.update_local(ray_idx, &lerp_slice(xforms, t));
}); });
} else { } else {
let ident = Transform::new(); let ident = Matrix4x4::new();
ray_stack.pop_do_next_task(|ray_idx| { ray_stack.pop_do_next_task(|ray_idx| {
rays.update_local(ray_idx, &ident); rays.update_local(ray_idx, &ident);
}); });

View File

@ -3,10 +3,10 @@ use std::{
mem::{transmute, MaybeUninit}, mem::{transmute, MaybeUninit},
}; };
use crate::{algorithm::merge_slices_to, math::Transform}; use crate::{algorithm::merge_slices_to, math::Matrix4x4};
pub struct TransformStack { pub struct TransformStack {
stack: Vec<MaybeUninit<Transform>>, stack: Vec<MaybeUninit<Matrix4x4>>,
stack_indices: Vec<usize>, stack_indices: Vec<usize>,
} }
@ -30,11 +30,11 @@ impl TransformStack {
self.stack_indices.push(0); self.stack_indices.push(0);
} }
pub fn push(&mut self, xforms: &[Transform]) { pub fn push(&mut self, xforms: &[Matrix4x4]) {
assert!(!xforms.is_empty()); assert!(!xforms.is_empty());
if self.stack.is_empty() { if self.stack.is_empty() {
let xforms: &[MaybeUninit<Transform>] = unsafe { transmute(xforms) }; let xforms: &[MaybeUninit<Matrix4x4>] = unsafe { transmute(xforms) };
self.stack.extend(xforms); self.stack.extend(xforms);
} else { } else {
let sil = self.stack_indices.len(); let sil = self.stack_indices.len();
@ -73,7 +73,7 @@ impl TransformStack {
self.stack_indices.pop(); self.stack_indices.pop();
} }
pub fn top(&self) -> &[Transform] { pub fn top(&self) -> &[Matrix4x4] {
let sil = self.stack_indices.len(); let sil = self.stack_indices.len();
let i1 = self.stack_indices[sil - 2]; let i1 = self.stack_indices[sil - 2];
let i2 = self.stack_indices[sil - 1]; let i2 = self.stack_indices[sil - 1];

View File

@ -102,7 +102,7 @@ pub fn encode(xyz: (f32, f32, f32)) -> u32 {
let v = (((9.0 * V_SCALE) * xyz.1 / s) + 0.5).max(1.0).min(255.0); let v = (((9.0 * V_SCALE) * xyz.1 / s) + 0.5).max(1.0).min(255.0);
((u as u32) << 8) | (v as u32) ((u as u32) << 8) | (v as u32)
} };
let y_bits = xyz.1.to_bits() & 0x7fffffff; let y_bits = xyz.1.to_bits() & 0x7fffffff;

View File

@ -11,5 +11,5 @@ path = "src/lib.rs"
# Local crate dependencies # Local crate dependencies
[dependencies] [dependencies]
glam = "0.15" glam = {git="https://github.com/bitshifter/glam-rs.git", rev="0f314f99", default-features=false, features=["approx"]}
approx = "0.4" approx = "0.3"

View File

@ -1,11 +1,11 @@
#![allow(dead_code)] #![allow(dead_code)]
mod matrix;
mod normal; mod normal;
mod point; mod point;
mod transform;
mod vector; mod vector;
pub use self::{normal::Normal, point::Point, transform::Transform, vector::Vector}; pub use self::{matrix::Matrix4x4, normal::Normal, point::Point, vector::Vector};
/// Trait for calculating dot products. /// Trait for calculating dot products.
pub trait DotProduct { pub trait DotProduct {

View File

@ -0,0 +1,193 @@
#![allow(dead_code)]
use std::ops::{Add, Mul};
use approx::RelativeEq;
use glam::{Mat4, Vec4};
use super::Point;
/// A 4x4 matrix, used for transforms
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Matrix4x4(pub Mat4);
impl Matrix4x4 {
/// Creates a new identity matrix
#[inline]
pub fn new() -> Matrix4x4 {
Matrix4x4(Mat4::identity())
}
/// Creates a new matrix with the specified values:
/// a b c d
/// e f g h
/// i j k l
/// m n o p
#[inline]
#[allow(clippy::many_single_char_names)]
#[allow(clippy::too_many_arguments)]
pub fn new_from_values(
a: f32,
b: f32,
c: f32,
d: f32,
e: f32,
f: f32,
g: f32,
h: f32,
i: f32,
j: f32,
k: f32,
l: f32,
m: f32,
n: f32,
o: f32,
p: f32,
) -> Matrix4x4 {
Matrix4x4(Mat4::new(
Vec4::new(a, e, i, m),
Vec4::new(b, f, j, n),
Vec4::new(c, g, k, o),
Vec4::new(d, h, l, p),
))
}
#[inline]
pub fn from_location(loc: Point) -> Matrix4x4 {
Matrix4x4(Mat4::from_translation(loc.co.truncate()))
}
/// Returns whether the matrices are approximately equal to each other.
/// Each corresponding element in the matrices cannot have a relative
/// error exceeding epsilon.
#[inline]
pub fn aprx_eq(&self, other: Matrix4x4, epsilon: f32) -> bool {
self.0.relative_eq(&other.0, std::f32::EPSILON, epsilon)
}
/// Returns the transpose of the matrix
#[inline]
pub fn transposed(&self) -> Matrix4x4 {
Matrix4x4(self.0.transpose())
}
/// Returns the inverse of the Matrix
#[inline]
pub fn inverse(&self) -> Matrix4x4 {
Matrix4x4(self.0.inverse())
}
}
impl Default for Matrix4x4 {
fn default() -> Self {
Self::new()
}
}
/// Multiply two matrices together
impl Mul for Matrix4x4 {
type Output = Self;
#[inline]
fn mul(self, other: Self) -> Self {
Self(other.0.mul_mat4(&self.0))
}
}
/// Multiply a matrix by a f32
impl Mul<f32> for Matrix4x4 {
type Output = Self;
#[inline]
fn mul(self, other: f32) -> Self {
Self(self.0 * other)
}
}
/// Add two matrices together
impl Add for Matrix4x4 {
type Output = Self;
#[inline]
fn add(self, other: Self) -> Self {
Self(self.0 + other.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn equality_test() {
let a = Matrix4x4::new();
let b = Matrix4x4::new();
let c = Matrix4x4::new_from_values(
1.1, 0.0, 0.0, 0.0, 0.0, 1.1, 0.0, 0.0, 0.0, 0.0, 1.1, 0.0, 0.0, 0.0, 0.0, 1.1,
);
assert_eq!(a, b);
assert!(a != c);
}
#[test]
fn approximate_equality_test() {
let a = Matrix4x4::new();
let b = Matrix4x4::new_from_values(
1.000001, 0.0, 0.0, 0.0, 0.0, 1.000001, 0.0, 0.0, 0.0, 0.0, 1.000001, 0.0, 0.0, 0.0,
0.0, 1.000001,
);
let c = Matrix4x4::new_from_values(
1.000003, 0.0, 0.0, 0.0, 0.0, 1.000003, 0.0, 0.0, 0.0, 0.0, 1.000003, 0.0, 0.0, 0.0,
0.0, 1.000003,
);
let d = Matrix4x4::new_from_values(
-1.000001, 0.0, 0.0, 0.0, 0.0, -1.000001, 0.0, 0.0, 0.0, 0.0, -1.000001, 0.0, 0.0, 0.0,
0.0, -1.000001,
);
assert!(a.aprx_eq(b, 0.000001));
assert!(!a.aprx_eq(c, 0.000001));
assert!(!a.aprx_eq(d, 0.000001));
}
#[test]
fn multiply_test() {
let a = Matrix4x4::new_from_values(
1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, 13.0, 7.0, 15.0, 3.0,
);
let b = Matrix4x4::new_from_values(
1.0, 5.0, 9.0, 13.0, 2.0, 6.0, 10.0, 14.0, 3.0, 7.0, 11.0, 15.0, 4.0, 8.0, 12.0, 16.0,
);
let c = Matrix4x4::new_from_values(
266.0, 141.0, 331.0, 188.5, 292.0, 158.0, 366.0, 213.0, 318.0, 175.0, 401.0, 237.5,
344.0, 192.0, 436.0, 262.0,
);
assert_eq!(a * b, c);
}
#[test]
fn inverse_test() {
let a = Matrix4x4::new_from_values(
1.0, 0.33, 0.0, -2.0, 0.0, 1.0, 0.0, 0.0, 2.1, 0.7, 1.3, 0.0, 0.0, 0.0, 0.0, -1.0,
);
let b = a.inverse();
let c = Matrix4x4::new();
assert!((dbg!(a * b)).aprx_eq(dbg!(c), 0.0000001));
}
#[test]
fn transpose_test() {
let a = Matrix4x4::new_from_values(
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,
);
let b = Matrix4x4::new_from_values(
1.0, 5.0, 9.0, 13.0, 2.0, 6.0, 10.0, 14.0, 3.0, 7.0, 11.0, 15.0, 4.0, 8.0, 12.0, 16.0,
);
let c = a.transposed();
assert_eq!(b, c);
}
}

View File

@ -5,21 +5,21 @@ use std::{
ops::{Add, Div, Mul, Neg, Sub}, ops::{Add, Div, Mul, Neg, Sub},
}; };
use glam::Vec3A; use glam::Vec3;
use super::{CrossProduct, DotProduct, Transform, Vector}; use super::{CrossProduct, DotProduct, Matrix4x4, Vector};
/// A surface normal in 3d homogeneous space. /// A surface normal in 3d homogeneous space.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct Normal { pub struct Normal {
pub co: Vec3A, pub co: Vec3,
} }
impl Normal { impl Normal {
#[inline(always)] #[inline(always)]
pub fn new(x: f32, y: f32, z: f32) -> Normal { pub fn new(x: f32, y: f32, z: f32) -> Normal {
Normal { Normal {
co: Vec3A::new(x, y, z), co: Vec3::new(x, y, z),
} }
} }
@ -57,32 +57,32 @@ impl Normal {
#[inline(always)] #[inline(always)]
pub fn x(&self) -> f32 { pub fn x(&self) -> f32 {
self.co[0] self.co.x()
} }
#[inline(always)] #[inline(always)]
pub fn y(&self) -> f32 { pub fn y(&self) -> f32 {
self.co[1] self.co.y()
} }
#[inline(always)] #[inline(always)]
pub fn z(&self) -> f32 { pub fn z(&self) -> f32 {
self.co[2] self.co.z()
} }
#[inline(always)] #[inline(always)]
pub fn set_x(&mut self, x: f32) { pub fn set_x(&mut self, x: f32) {
self.co[0] = x; self.co.set_x(x);
} }
#[inline(always)] #[inline(always)]
pub fn set_y(&mut self, y: f32) { pub fn set_y(&mut self, y: f32) {
self.co[1] = y; self.co.set_y(y);
} }
#[inline(always)] #[inline(always)]
pub fn set_z(&mut self, z: f32) { pub fn set_z(&mut self, z: f32) {
self.co[2] = z; self.co.set_z(z);
} }
} }
@ -126,13 +126,14 @@ impl Mul<f32> for Normal {
} }
} }
impl Mul<Transform> for Normal { impl Mul<Matrix4x4> for Normal {
type Output = Normal; type Output = Normal;
#[inline] #[inline]
fn mul(self, other: Transform) -> Normal { fn mul(self, other: Matrix4x4) -> Normal {
let mat = other.0.inverse().transpose();
Normal { Normal {
co: other.0.matrix3.inverse().transpose().mul_vec3a(self.co), co: mat.transform_vector3(self.co),
} }
} }
} }
@ -175,9 +176,9 @@ impl CrossProduct for Normal {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::super::{CrossProduct, DotProduct, Transform}; use super::super::{CrossProduct, DotProduct, Matrix4x4};
use super::*; use super::*;
use approx::assert_ulps_eq; use approx::UlpsEq;
#[test] #[test]
fn add() { fn add() {
@ -209,14 +210,12 @@ mod tests {
#[test] #[test]
fn mul_matrix_1() { fn mul_matrix_1() {
let n = Normal::new(1.0, 2.5, 4.0); let n = Normal::new(1.0, 2.5, 4.0);
let m = Transform::new_from_values( let m = Matrix4x4::new_from_values(
1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, 1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, 13.0, 7.0, 15.0, 3.0,
); );
let nm = n * m; let nm = n * m;
let nm2 = Normal::new(-4.0625, 1.78125, -0.03125); let nm2 = Normal::new(-19.258825, 5.717648, -1.770588);
for i in 0..3 { assert!(nm.co.ulps_eq(&nm2.co, 0.0, 4));
assert_ulps_eq!(nm.co[i], nm2.co[i], max_ulps = 4);
}
} }
#[test] #[test]

View File

@ -5,28 +5,37 @@ use std::{
ops::{Add, Mul, Sub}, ops::{Add, Mul, Sub},
}; };
use glam::Vec3A; use glam::Vec4;
use super::{Transform, Vector}; use super::{Matrix4x4, Vector};
/// A position in 3d homogeneous space. /// A position in 3d homogeneous space.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct Point { pub struct Point {
pub co: Vec3A, pub co: Vec4,
} }
impl Point { impl Point {
#[inline(always)] #[inline(always)]
pub fn new(x: f32, y: f32, z: f32) -> Point { pub fn new(x: f32, y: f32, z: f32) -> Point {
Point { Point {
co: Vec3A::new(x, y, z), co: Vec4::new(x, y, z, 1.0),
}
}
/// Returns the point in standardized coordinates, where the
/// fourth homogeneous component has been normalized to 1.0.
#[inline(always)]
pub fn norm(&self) -> Point {
Point {
co: self.co / self.co.w(),
} }
} }
#[inline(always)] #[inline(always)]
pub fn min(&self, other: Point) -> Point { pub fn min(&self, other: Point) -> Point {
let n1 = self; let n1 = self.norm();
let n2 = other; let n2 = other.norm();
Point { Point {
co: n1.co.min(n2.co), co: n1.co.min(n2.co),
@ -35,8 +44,8 @@ impl Point {
#[inline(always)] #[inline(always)]
pub fn max(&self, other: Point) -> Point { pub fn max(&self, other: Point) -> Point {
let n1 = self; let n1 = self.norm();
let n2 = other; let n2 = other.norm();
Point { Point {
co: n1.co.max(n2.co), co: n1.co.max(n2.co),
@ -45,7 +54,9 @@ impl Point {
#[inline(always)] #[inline(always)]
pub fn into_vector(self) -> Vector { pub fn into_vector(self) -> Vector {
Vector { co: self.co } Vector {
co: self.co.truncate(),
}
} }
#[inline(always)] #[inline(always)]
@ -60,32 +71,32 @@ impl Point {
#[inline(always)] #[inline(always)]
pub fn x(&self) -> f32 { pub fn x(&self) -> f32 {
self.co[0] self.co.x()
} }
#[inline(always)] #[inline(always)]
pub fn y(&self) -> f32 { pub fn y(&self) -> f32 {
self.co[1] self.co.y()
} }
#[inline(always)] #[inline(always)]
pub fn z(&self) -> f32 { pub fn z(&self) -> f32 {
self.co[2] self.co.z()
} }
#[inline(always)] #[inline(always)]
pub fn set_x(&mut self, x: f32) { pub fn set_x(&mut self, x: f32) {
self.co[0] = x; self.co.set_x(x);
} }
#[inline(always)] #[inline(always)]
pub fn set_y(&mut self, y: f32) { pub fn set_y(&mut self, y: f32) {
self.co[1] = y; self.co.set_y(y);
} }
#[inline(always)] #[inline(always)]
pub fn set_z(&mut self, z: f32) { pub fn set_z(&mut self, z: f32) {
self.co[2] = z; self.co.set_z(z);
} }
} }
@ -102,7 +113,7 @@ impl Add<Vector> for Point {
#[inline(always)] #[inline(always)]
fn add(self, other: Vector) -> Point { fn add(self, other: Vector) -> Point {
Point { Point {
co: self.co + other.co, co: self.co + other.co.extend(0.0),
} }
} }
} }
@ -113,7 +124,7 @@ impl Sub for Point {
#[inline(always)] #[inline(always)]
fn sub(self, other: Point) -> Vector { fn sub(self, other: Point) -> Vector {
Vector { Vector {
co: self.co - other.co, co: (self.norm().co - other.norm().co).truncate(),
} }
} }
} }
@ -124,27 +135,36 @@ impl Sub<Vector> for Point {
#[inline(always)] #[inline(always)]
fn sub(self, other: Vector) -> Point { fn sub(self, other: Vector) -> Point {
Point { Point {
co: self.co - other.co, co: self.co - other.co.extend(0.0),
} }
} }
} }
impl Mul<Transform> for Point { impl Mul<Matrix4x4> for Point {
type Output = Point; type Output = Point;
#[inline] #[inline]
fn mul(self, other: Transform) -> Point { fn mul(self, other: Matrix4x4) -> Point {
Point { Point {
co: other.0.transform_point3a(self.co), co: other.0.mul_vec4(self.co),
} }
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::super::{Transform, Vector}; use super::super::{Matrix4x4, Vector};
use super::*; use super::*;
#[test]
fn norm() {
let mut p1 = Point::new(1.0, 2.0, 3.0);
let p2 = Point::new(2.0, 4.0, 6.0);
p1.co.set_w(0.5);
assert_eq!(p2, p1.norm());
}
#[test] #[test]
fn add() { fn add() {
let p1 = Point::new(1.0, 2.0, 3.0); let p1 = Point::new(1.0, 2.0, 3.0);
@ -166,8 +186,8 @@ mod tests {
#[test] #[test]
fn mul_matrix_1() { fn mul_matrix_1() {
let p = Point::new(1.0, 2.5, 4.0); let p = Point::new(1.0, 2.5, 4.0);
let m = Transform::new_from_values( let m = Matrix4x4::new_from_values(
1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, 1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, 0.0, 0.0, 0.0, 1.0,
); );
let pm = Point::new(15.5, 54.0, 70.0); let pm = Point::new(15.5, 54.0, 70.0);
assert_eq!(p * m, pm); assert_eq!(p * m, pm);
@ -176,10 +196,11 @@ mod tests {
#[test] #[test]
fn mul_matrix_2() { fn mul_matrix_2() {
let p = Point::new(1.0, 2.5, 4.0); let p = Point::new(1.0, 2.5, 4.0);
let m = Transform::new_from_values( let m = Matrix4x4::new_from_values(
1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, 1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, 2.0, 3.0, 1.0, 5.0,
); );
let pm = Point::new(15.5, 54.0, 70.0); let mut pm = Point::new(15.5, 54.0, 70.0);
pm.co.set_w(18.5);
assert_eq!(p * m, pm); assert_eq!(p * m, pm);
} }
@ -187,11 +208,12 @@ mod tests {
fn mul_matrix_3() { fn mul_matrix_3() {
// Make sure matrix multiplication composes the way one would expect // Make sure matrix multiplication composes the way one would expect
let p = Point::new(1.0, 2.5, 4.0); let p = Point::new(1.0, 2.5, 4.0);
let m1 = Transform::new_from_values( let m1 = Matrix4x4::new_from_values(
1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, 1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, 13.0, 7.0, 15.0, 3.0,
);
let m2 = Matrix4x4::new_from_values(
4.0, 1.0, 2.0, 3.5, 3.0, 6.0, 5.0, 2.0, 2.0, 2.0, 4.0, 12.0, 5.0, 7.0, 8.0, 11.0,
); );
let m2 =
Transform::new_from_values(4.0, 1.0, 2.0, 3.5, 3.0, 6.0, 5.0, 2.0, 2.0, 2.0, 4.0, 12.0);
println!("{:?}", m1 * m2); println!("{:?}", m1 * m2);
let pmm1 = p * (m1 * m2); let pmm1 = p * (m1 * m2);

View File

@ -1,178 +0,0 @@
#![allow(dead_code)]
use std::ops::{Add, Mul};
use approx::relative_eq;
use glam::{Affine3A, Mat3, Mat4, Vec3};
use super::Point;
/// A 4x3 affine transform matrix, used for transforms.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Transform(pub Affine3A);
impl Transform {
/// Creates a new identity matrix
#[inline]
pub fn new() -> Transform {
Transform(Affine3A::IDENTITY)
}
/// Creates a new matrix with the specified values:
/// a b c d
/// e f g h
/// i j k l
/// m n o p
#[inline]
#[allow(clippy::many_single_char_names)]
#[allow(clippy::too_many_arguments)]
pub fn new_from_values(
a: f32,
b: f32,
c: f32,
d: f32,
e: f32,
f: f32,
g: f32,
h: f32,
i: f32,
j: f32,
k: f32,
l: f32,
) -> Transform {
Transform(Affine3A::from_mat3_translation(
Mat3::from_cols(Vec3::new(a, e, i), Vec3::new(b, f, j), Vec3::new(c, g, k)),
Vec3::new(d, h, l),
))
}
#[inline]
pub fn from_location(loc: Point) -> Transform {
Transform(Affine3A::from_translation(loc.co.into()))
}
/// Returns whether the matrices are approximately equal to each other.
/// Each corresponding element in the matrices cannot have a relative
/// error exceeding epsilon.
#[inline]
pub fn aprx_eq(&self, other: Transform, epsilon: f32) -> bool {
let mut eq = true;
for c in 0..3 {
for r in 0..3 {
let a = self.0.matrix3.col(c)[r];
let b = other.0.matrix3.col(c)[r];
eq &= relative_eq!(a, b, epsilon = epsilon);
}
}
for i in 0..3 {
let a = self.0.translation[i];
let b = other.0.translation[i];
eq &= relative_eq!(a, b, epsilon = epsilon);
}
eq
}
/// Returns the inverse of the Matrix
#[inline]
pub fn inverse(&self) -> Transform {
Transform(self.0.inverse())
}
}
impl Default for Transform {
fn default() -> Self {
Self::new()
}
}
/// Multiply two matrices together
impl Mul for Transform {
type Output = Self;
#[inline]
fn mul(self, other: Self) -> Self {
Self(other.0 * self.0)
}
}
/// Multiply a matrix by a f32
impl Mul<f32> for Transform {
type Output = Self;
#[inline]
fn mul(self, other: f32) -> Self {
Self(Affine3A::from_mat4(Mat4::from(self.0) * other))
}
}
/// Add two matrices together
impl Add for Transform {
type Output = Self;
#[inline]
fn add(self, other: Self) -> Self {
Self(Affine3A::from_mat4(
Mat4::from(self.0) + Mat4::from(other.0),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn equality_test() {
let a = Transform::new();
let b = Transform::new();
let c =
Transform::new_from_values(1.1, 0.0, 0.0, 0.0, 0.0, 1.1, 0.0, 0.0, 0.0, 0.0, 1.1, 0.0);
assert_eq!(a, b);
assert!(a != c);
}
#[test]
fn approximate_equality_test() {
let a = Transform::new();
let b = Transform::new_from_values(
1.000001, 0.0, 0.0, 0.0, 0.0, 1.000001, 0.0, 0.0, 0.0, 0.0, 1.000001, 0.0,
);
let c = Transform::new_from_values(
1.000003, 0.0, 0.0, 0.0, 0.0, 1.000003, 0.0, 0.0, 0.0, 0.0, 1.000003, 0.0,
);
let d = Transform::new_from_values(
-1.000001, 0.0, 0.0, 0.0, 0.0, -1.000001, 0.0, 0.0, 0.0, 0.0, -1.000001, 0.0,
);
assert!(a.aprx_eq(b, 0.000001));
assert!(!a.aprx_eq(c, 0.000001));
assert!(!a.aprx_eq(d, 0.000001));
}
#[test]
fn multiply_test() {
let a = Transform::new_from_values(
1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0,
);
let b = Transform::new_from_values(
1.0, 5.0, 9.0, 13.0, 2.0, 6.0, 10.0, 14.0, 3.0, 7.0, 11.0, 15.0,
);
let c = Transform::new_from_values(
97.0, 50.0, 136.0, 162.5, 110.0, 60.0, 156.0, 185.0, 123.0, 70.0, 176.0, 207.5,
);
assert_eq!(a * b, c);
}
#[test]
fn inverse_test() {
let a = Transform::new_from_values(
1.0, 0.33, 0.0, -2.0, 0.0, 1.0, 0.0, 0.0, 2.1, 0.7, 1.3, 0.0,
);
let b = a.inverse();
let c = Transform::new();
assert!((dbg!(a * b)).aprx_eq(dbg!(c), 0.0000001));
}
}

View File

@ -5,21 +5,21 @@ use std::{
ops::{Add, Div, Mul, Neg, Sub}, ops::{Add, Div, Mul, Neg, Sub},
}; };
use glam::Vec3A; use glam::Vec3;
use super::{CrossProduct, DotProduct, Normal, Point, Transform}; use super::{CrossProduct, DotProduct, Matrix4x4, Normal, Point};
/// A direction vector in 3d homogeneous space. /// A direction vector in 3d homogeneous space.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct Vector { pub struct Vector {
pub co: Vec3A, pub co: Vec3,
} }
impl Vector { impl Vector {
#[inline(always)] #[inline(always)]
pub fn new(x: f32, y: f32, z: f32) -> Vector { pub fn new(x: f32, y: f32, z: f32) -> Vector {
Vector { Vector {
co: Vec3A::new(x, y, z), co: Vec3::new(x, y, z),
} }
} }
@ -43,13 +43,15 @@ impl Vector {
#[inline(always)] #[inline(always)]
pub fn abs(&self) -> Vector { pub fn abs(&self) -> Vector {
Vector { Vector {
co: self.co * self.co.signum(), co: self.co * self.co.sign(),
} }
} }
#[inline(always)] #[inline(always)]
pub fn into_point(self) -> Point { pub fn into_point(self) -> Point {
Point { co: self.co } Point {
co: self.co.extend(1.0),
}
} }
#[inline(always)] #[inline(always)]
@ -69,32 +71,32 @@ impl Vector {
#[inline(always)] #[inline(always)]
pub fn x(&self) -> f32 { pub fn x(&self) -> f32 {
self.co[0] self.co.x()
} }
#[inline(always)] #[inline(always)]
pub fn y(&self) -> f32 { pub fn y(&self) -> f32 {
self.co[1] self.co.y()
} }
#[inline(always)] #[inline(always)]
pub fn z(&self) -> f32 { pub fn z(&self) -> f32 {
self.co[2] self.co.z()
} }
#[inline(always)] #[inline(always)]
pub fn set_x(&mut self, x: f32) { pub fn set_x(&mut self, x: f32) {
self.co[0] = x; self.co.set_x(x);
} }
#[inline(always)] #[inline(always)]
pub fn set_y(&mut self, y: f32) { pub fn set_y(&mut self, y: f32) {
self.co[1] = y; self.co.set_y(y);
} }
#[inline(always)] #[inline(always)]
pub fn set_z(&mut self, z: f32) { pub fn set_z(&mut self, z: f32) {
self.co[2] = z; self.co.set_z(z);
} }
} }
@ -138,13 +140,13 @@ impl Mul<f32> for Vector {
} }
} }
impl Mul<Transform> for Vector { impl Mul<Matrix4x4> for Vector {
type Output = Vector; type Output = Vector;
#[inline] #[inline]
fn mul(self, other: Transform) -> Vector { fn mul(self, other: Matrix4x4) -> Vector {
Vector { Vector {
co: other.0.transform_vector3a(self.co), co: other.0.transform_vector3(self.co),
} }
} }
} }
@ -187,7 +189,7 @@ impl CrossProduct for Vector {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::super::{CrossProduct, DotProduct, Transform}; use super::super::{CrossProduct, DotProduct, Matrix4x4};
use super::*; use super::*;
#[test] #[test]
@ -220,8 +222,8 @@ mod tests {
#[test] #[test]
fn mul_matrix_1() { fn mul_matrix_1() {
let v = Vector::new(1.0, 2.5, 4.0); let v = Vector::new(1.0, 2.5, 4.0);
let m = Transform::new_from_values( let m = Matrix4x4::new_from_values(
1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, 1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, 13.0, 7.0, 15.0, 3.0,
); );
assert_eq!(v * m, Vector::new(14.0, 46.0, 58.0)); assert_eq!(v * m, Vector::new(14.0, 46.0, 58.0));
} }
@ -229,8 +231,8 @@ mod tests {
#[test] #[test]
fn mul_matrix_2() { fn mul_matrix_2() {
let v = Vector::new(1.0, 2.5, 4.0); let v = Vector::new(1.0, 2.5, 4.0);
let m = Transform::new_from_values( let m = Matrix4x4::new_from_values(
1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, 1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, 0.0, 0.0, 0.0, 1.0,
); );
assert_eq!(v * m, Vector::new(14.0, 46.0, 58.0)); assert_eq!(v * m, Vector::new(14.0, 46.0, 58.0));
} }

View File

@ -10,4 +10,4 @@ name = "spectral_upsampling"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
glam = "0.15" glam = {git="https://github.com/bitshifter/glam-rs.git", rev="0f314f99", default-features=false, features=["approx"]}

View File

@ -118,14 +118,14 @@ fn small_rgb_to_spectrum_p4(
// Evaluate the spectral function and return the result. // Evaluate the spectral function and return the result.
if max_val <= table_mid_value { if max_val <= table_mid_value {
rgb2spec_eval_4([c[0][0], c[0][1], c[0][2]], lambdas) * (1.0 / table_mid_value) * max_val rgb2spec_eval_4([c[0].x(), c[0].y(), c[0].z()], lambdas) * (1.0 / table_mid_value) * max_val
} else if max_val < 1.0 { } else if max_val < 1.0 {
let n = (max_val - table_mid_value) / (1.0 - table_mid_value); let n = (max_val - table_mid_value) / (1.0 - table_mid_value);
let s0 = rgb2spec_eval_4([c[0][0], c[0][1], c[0][2]], lambdas); let s0 = rgb2spec_eval_4([c[0].x(), c[0].y(), c[0].z()], lambdas);
let s1 = rgb2spec_eval_4([c[1][0], c[1][1], c[1][2]], lambdas); let s1 = rgb2spec_eval_4([c[1].x(), c[1].y(), c[1].z()], lambdas);
(s0 * (1.0 - n)) + (s1 * n) (s0 * (1.0 - n)) + (s1 * n)
} else { } else {
rgb2spec_eval_4([c[1][0], c[1][1], c[1][2]], lambdas) * max_val rgb2spec_eval_4([c[1].x(), c[1].y(), c[1].z()], lambdas) * max_val
} }
} }
@ -147,7 +147,7 @@ fn rgb2spec_eval_4(coeff: [f32; RGB2SPEC_N_COEFFS], lambda: Vec4) -> Vec4 {
let y = { let y = {
// TODO: replace this with a SIMD sqrt op. // TODO: replace this with a SIMD sqrt op.
let (x, y, z, w) = rgb2spec_fma_4(x, x, Vec4::splat(1.0)).into(); let (x, y, z, w) = rgb2spec_fma_4(x, x, Vec4::splat(1.0)).into();
Vec4::new(x.sqrt(), y.sqrt(), z.sqrt(), w.sqrt()).recip() Vec4::new(x.sqrt(), y.sqrt(), z.sqrt(), w.sqrt()).reciprocal()
}; };
rgb2spec_fma_4(Vec4::splat(0.5) * x, y, Vec4::splat(0.5)) rgb2spec_fma_4(Vec4::splat(0.5) * x, y, Vec4::splat(0.5))

View File

@ -227,13 +227,13 @@ pub fn spectrum_xyz_to_p_4(lambdas: Vec4, xyz: (f32, f32, f32)) -> Vec4 {
// Get the spectral values for the vertices of the grid cell. // Get the spectral values for the vertices of the grid cell.
// TODO: use integer SIMD intrinsics to make this part faster. // TODO: use integer SIMD intrinsics to make this part faster.
let mut p = [Vec4::splat(0.0); 6]; let mut p = [Vec4::splat(0.0); 6];
let sb0: [i32; 4] = [sb[0] as i32, sb[1] as i32, sb[2] as i32, sb[3] as i32]; let sb0: [i32; 4] = [sb.x() as i32, sb.y() as i32, sb.z() as i32, sb.w() as i32];
assert!(sb0[0].max(sb0[1]).max(sb0[2].max(sb0[3])) < SPECTRUM_NUM_SAMPLES); assert!(sb0[0].max(sb0[1]).max(sb0[2].max(sb0[3])) < SPECTRUM_NUM_SAMPLES);
let sb1: [i32; 4] = [ let sb1: [i32; 4] = [
(sb[0] as i32 + 1).min(SPECTRUM_NUM_SAMPLES - 1), (sb.x() as i32 + 1).min(SPECTRUM_NUM_SAMPLES - 1),
(sb[1] as i32 + 1).min(SPECTRUM_NUM_SAMPLES - 1), (sb.y() as i32 + 1).min(SPECTRUM_NUM_SAMPLES - 1),
(sb[2] as i32 + 1).min(SPECTRUM_NUM_SAMPLES - 1), (sb.z() as i32 + 1).min(SPECTRUM_NUM_SAMPLES - 1),
(sb[3] as i32 + 1).min(SPECTRUM_NUM_SAMPLES - 1), (sb.w() as i32 + 1).min(SPECTRUM_NUM_SAMPLES - 1),
]; ];
let sbf = sb - Vec4::new(sb0[0] as f32, sb0[1] as f32, sb0[2] as f32, sb0[3] as f32); let sbf = sb - Vec4::new(sb0[0] as f32, sb0[1] as f32, sb0[2] as f32, sb0[3] as f32);
for i in 0..(num as usize) { for i in 0..(num as usize) {