Skip to content

Commit

Permalink
Store only f32 direction vectors instead of full rays in the light …
Browse files Browse the repository at this point in the history
…chart.
  • Loading branch information
kpreid committed Aug 5, 2024
1 parent 14a4456 commit d4f02b4
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 21 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 12 additions & 7 deletions all-is-cubes/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extern crate alloc;
use std::path::{Path, PathBuf};
use std::{env, fs};

use euclid::Vector3D;

use all_is_cubes_base::math::{self, Face6, FaceMap, FreePoint, FreeVector};
use all_is_cubes_base::raycast::Ray;

Expand All @@ -24,8 +26,6 @@ const RAY_DIRECTION_STEP: isize = 5;
// TODO: Use more rays once we have a more efficient chart format that
// deduplicates work of near-parallel rays.
fn generate_light_ray_pattern() -> Vec<OneRay> {
let origin = FreePoint::new(0.5, 0.5, 0.5);

let mut rays = Vec::new();

// TODO: octahedron instead of cube
Expand All @@ -36,7 +36,7 @@ fn generate_light_ray_pattern() -> Vec<OneRay> {
|| y.abs() == RAY_DIRECTION_STEP
|| z.abs() == RAY_DIRECTION_STEP
{
let direction = FreeVector::new(x as f64, y as f64, z as f64).normalize();
let direction = Vector3D::new(x as f32, y as f32, z as f32).normalize();

let mut cosines = FaceMap::repeat(0.0f32);
for face in Face6::ALL {
Expand All @@ -45,7 +45,7 @@ fn generate_light_ray_pattern() -> Vec<OneRay> {
cosines[face] = cosine;
}

rays.push(OneRay::new(Ray::new(origin, direction), cosines))
rays.push(OneRay::new(direction, cosines))
}
}
}
Expand All @@ -59,7 +59,10 @@ fn generate_light_propagation_chart(rays: &[OneRay]) -> Vec<chart_schema::Steps>
let maximum_distance = 127.0;
rays.iter()
.map(|&info| {
let ray: Ray = info.ray.into();
let ray: Ray = Ray::new(
FreePoint::splat(0.5),
Vector3D::from(info.direction).map(|c| f64::from(f32::from(c))),
);
chart_schema::Steps {
info,
relative_cube_sequence: ray
Expand Down Expand Up @@ -128,8 +131,10 @@ use chart_schema::{OneRay, TargetEndian};
#[path = "src/space/light/chart/"]
mod chart_schema {
use crate::math::FaceMap;
use all_is_cubes_base::math::Cube;
use all_is_cubes_base::raycast::Ray;
use core::fmt;
use euclid::Vector3D;
use num_traits::{FromBytes, ToBytes};
use std::env;

Expand All @@ -138,10 +143,10 @@ mod chart_schema {
pub(crate) use shared::{IndirectSteps, OneRay, Step, Steps};

impl OneRay {
pub fn new(ray: Ray, face_cosines: FaceMap<f32>) -> Self {
pub fn new(direction: Vector3D<f32, Cube>, face_cosines: FaceMap<f32>) -> Self {
let face_cosines = face_cosines.map(|_, cosine| (cosine * 255.0).round() as u8);
Self {
ray: ray.into(),
direction: direction.map(TargetEndian::from).into(),
face_cosines: [
face_cosines.nx,
face_cosines.ny,
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes/src/space/light/chart/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl chart::OneRay {
py,
pz,
}
.map(|_, byte| byte as f32 / 255.0f32)
.map(|_, byte| f32::from(byte) / 255.0f32)
}
}

Expand Down
8 changes: 5 additions & 3 deletions all-is-cubes/src/space/light/chart/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ use super::TargetEndian;
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
#[repr(C)]
pub(crate) struct OneRay {
/// Ray whose origin is within the [0,0,0]..[1,1,1] cube and direction
/// is a unit vector in the direction of this light ray.
pub ray: Ray,
/// Unit direction vector specifying the ray direction.
pub direction: [TargetEndian<f32>; 3],

/// `FaceMap` data which stores the cosine (rescaled to 0-255)
/// between each face normal and this ray.
pub face_cosines: [u8; 6],

/// Guaranteed zero padding to make up a multiple of 4 bytes.
pub _padding: [u8; 2],
}

Expand Down
15 changes: 5 additions & 10 deletions all-is-cubes/src/space/light/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use alloc::boxed::Box;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::{fmt, mem};
use euclid::Vector3D;

use manyfmt::Fmt;

Expand All @@ -14,8 +15,7 @@ use rayon::iter::{IntoParallelRefMutIterator as _, ParallelIterator as _};
use super::debug::LightComputeOutput;
use crate::block::{self, EvaluatedBlock};
use crate::math::{
Cube, CubeFace, Face6, Face7, FaceMap, FreeCoordinate, Geometry, NotNan, OpacityCategory, Rgb,
Rgba, Vol,
Cube, CubeFace, Face6, Face7, FaceMap, Geometry, NotNan, OpacityCategory, Rgb, Rgba, Vol,
};
use crate::raycast::Ray;
use crate::space::light::{
Expand Down Expand Up @@ -400,7 +400,7 @@ impl LightStorage {
continue;
}
let mut ray_state =
LightRayState::new(cube, ray_info.ray.into(), ray_weight_by_faces);
LightRayState::new(cube, ray_info.direction.into(), ray_weight_by_faces);

// Stores the light value that might have been fetched, if it was, from the previous
// step's cube_ahead, which is the current step's cube_behind.
Expand Down Expand Up @@ -644,13 +644,8 @@ impl LightRayState {
/// * `abstract_ray`: ray as if we were lighting the [0, 0, 0] cube
/// * `ray_weight_by_faces`: how much influence this ray should have on the
/// total illumination
fn new(origin_cube: Cube, abstract_ray: Ray, ray_weight_by_faces: f32) -> Self {
let translated_ray = abstract_ray.translate(
origin_cube
.lower_bounds()
.map(FreeCoordinate::from)
.to_vector(),
);
fn new(origin_cube: Cube, direction: Vector3D<f32, Cube>, ray_weight_by_faces: f32) -> Self {
let translated_ray = Ray::new(origin_cube.midpoint(), direction.map(f64::from));
LightRayState {
alpha: 1.0,
ray_weight_by_faces,
Expand Down
Binary file modified test-renderers/expected/renderers/fog-None-ray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-renderers/expected/renderers/light-Flat-all.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-renderers/expected/renderers/light-Smooth-all.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d4f02b4

Please sign in to comment.