Skip to content

Commit

Permalink
Add PodFaceMap for use by the light propagation chart.
Browse files Browse the repository at this point in the history
Motivation: I’m planning a new revision of the light propagation chart,
which will need more `FaceMap`s, so this makes it more convenient.
  • Loading branch information
kpreid committed Jan 15, 2025
1 parent 26ffc61 commit 21cc1dc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
9 changes: 1 addition & 8 deletions all-is-cubes/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,7 @@ mod chart_schema {
let face_cosines = face_cosines.map(|_, cosine| (cosine * 255.0).round() as u8);
Self {
direction: direction.map(TargetEndian::from).into(),
face_cosines: [
face_cosines.nx,
face_cosines.ny,
face_cosines.nz,
face_cosines.px,
face_cosines.py,
face_cosines.pz,
],
face_cosines: face_cosines.into(),
_padding: [0; 2],
}
}
Expand Down
11 changes: 1 addition & 10 deletions all-is-cubes/src/space/light/chart/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@ use crate::space::light::chart;

impl chart::OneRay {
pub fn face_cosines(&self) -> FaceMap<f32> {
let [nx, ny, nz, px, py, pz] = self.face_cosines;
FaceMap {
nx,
ny,
nz,
px,
py,
pz,
}
.map(|_, byte| f32::from(byte) / 255.0f32)
FaceMap::from(self.face_cosines).map(|_, byte| f32::from(byte) / 255.0f32)
}
}

Expand Down
31 changes: 29 additions & 2 deletions all-is-cubes/src/space/light/chart/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! This is the most efficient way I could think of to store and transfer the
//! pre-computed light ray chart data.
use all_is_cubes_base::math::Face7;
use all_is_cubes_base::math::{Face7, FaceMap};

// conditionally defined to be equal to f32 except in the build script
use super::TargetEndian;
Expand All @@ -19,7 +19,7 @@ pub(crate) struct OneRay {

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

/// Guaranteed zero padding to make up a multiple of 4 bytes.
pub _padding: [u8; 2],
Expand Down Expand Up @@ -106,6 +106,33 @@ impl From<Face7> for Face7Safe {
}
}

/// Variant of [`FaceMap`] that can implement `bytemuck::Pod` because it is `repr(packed)`.
/// (The regular `FaceMap` could too, *unsafely*, but I don't want to do that unsafe yet.)
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
#[repr(C, packed)]
pub(crate) struct PodFaceMap<T> {
nx: T,
ny: T,
nz: T,
pz: T,
py: T,
px: T,
}
impl<T> From<FaceMap<T>> for PodFaceMap<T> {
#[rustfmt::skip]
fn from(value: FaceMap<T>) -> Self {
let FaceMap { nx, ny, nz, px, py, pz } = value;
Self { nx, ny, nz, pz, py, px }
}
}
impl<T> From<PodFaceMap<T>> for FaceMap<T> {
#[rustfmt::skip]
fn from(value: PodFaceMap<T>) -> Self {
let PodFaceMap { nx, ny, nz, pz, py, px } = value;
FaceMap { nx, ny, nz, px, py, pz }
}
}

// Note: Most of the methods are either only used for reading or only used for writing,
// so they're defined in the respective crates to reduce complications like what they depend on,
// how `TargetEndian` is defined, and dead code warnings.

0 comments on commit 21cc1dc

Please sign in to comment.