formatting: Using canonical formatting on whole project
This commit is contained in:
+54
-110
@@ -10,14 +10,12 @@ use glam::Vec3;
|
||||
/// [`Aabb::union`] and [`Aabb::union_point`].
|
||||
#[derive(Clone, Copy, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
#[repr(C)]
|
||||
pub struct Aabb
|
||||
{
|
||||
pub struct Aabb {
|
||||
min: Vec3,
|
||||
max: Vec3,
|
||||
}
|
||||
|
||||
impl Aabb
|
||||
{
|
||||
impl Aabb {
|
||||
/// The empty box: it contains no point and is the identity element for
|
||||
/// union operations.
|
||||
pub const EMPTY: Self = Self {
|
||||
@@ -36,14 +34,12 @@ impl Aabb
|
||||
///
|
||||
/// No validation is performed so that [`Aabb::EMPTY`] can be built; use
|
||||
/// [`Aabb::is_empty`] to check the result.
|
||||
pub const fn new(min: Vec3, max: Vec3) -> Self
|
||||
{
|
||||
pub const fn new(min: Vec3, max: Vec3) -> Self {
|
||||
Self { min, max }
|
||||
}
|
||||
|
||||
/// Builds a box from a center and the half extents along each axis.
|
||||
pub fn from_center_half_extents(center: Vec3, half_extents: Vec3) -> Self
|
||||
{
|
||||
pub fn from_center_half_extents(center: Vec3, half_extents: Vec3) -> Self {
|
||||
Self {
|
||||
min: center - half_extents,
|
||||
max: center + half_extents,
|
||||
@@ -53,106 +49,86 @@ impl Aabb
|
||||
/// Builds the tightest box containing every point.
|
||||
///
|
||||
/// An empty iterator produces [`Aabb::EMPTY`].
|
||||
pub fn from_points(points: impl IntoIterator<Item = Vec3>) -> Self
|
||||
{
|
||||
pub fn from_points(points: impl IntoIterator<Item = Vec3>) -> Self {
|
||||
let mut aabb = Self::EMPTY;
|
||||
for point in points
|
||||
{
|
||||
for point in points {
|
||||
aabb.union_point(point);
|
||||
}
|
||||
aabb
|
||||
}
|
||||
|
||||
pub const fn min(&self) -> Vec3
|
||||
{
|
||||
pub const fn min(&self) -> Vec3 {
|
||||
self.min
|
||||
}
|
||||
|
||||
pub const fn max(&self) -> Vec3
|
||||
{
|
||||
pub const fn max(&self) -> Vec3 {
|
||||
self.max
|
||||
}
|
||||
|
||||
/// Center of the box. Not meaningful for an empty box.
|
||||
pub fn center(&self) -> Vec3
|
||||
{
|
||||
pub fn center(&self) -> Vec3 {
|
||||
(self.min + self.max) * 0.5
|
||||
}
|
||||
|
||||
/// Full extent of the box along each axis (`max - min`).
|
||||
pub fn size(&self) -> Vec3
|
||||
{
|
||||
pub fn size(&self) -> Vec3 {
|
||||
self.max - self.min
|
||||
}
|
||||
|
||||
/// Half extent of the box along each axis.
|
||||
pub fn half_size(&self) -> Vec3
|
||||
{
|
||||
pub fn half_size(&self) -> Vec3 {
|
||||
self.size() * 0.5
|
||||
}
|
||||
|
||||
/// Surface area of the box, used by SAH heuristics.
|
||||
pub fn surface_area(&self) -> f32
|
||||
{
|
||||
pub fn surface_area(&self) -> f32 {
|
||||
let size = self.size();
|
||||
2.0 * (size.x * size.y + size.y * size.z + size.z * size.x)
|
||||
}
|
||||
|
||||
pub fn volume(&self) -> f32
|
||||
{
|
||||
pub fn volume(&self) -> f32 {
|
||||
self.size().element_product()
|
||||
}
|
||||
|
||||
/// Index of the axis with the largest extent (0 = x, 1 = y, 2 = z).
|
||||
///
|
||||
/// Ties are broken in favor of the lower axis.
|
||||
pub fn longest_axis(&self) -> usize
|
||||
{
|
||||
pub fn longest_axis(&self) -> usize {
|
||||
let size = self.size();
|
||||
if size.x >= size.y && size.x >= size.z
|
||||
{
|
||||
if size.x >= size.y && size.x >= size.z {
|
||||
0
|
||||
}
|
||||
else if size.y >= size.z
|
||||
{
|
||||
} else if size.y >= size.z {
|
||||
1
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
2
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` when the box contains no point.
|
||||
pub fn is_empty(&self) -> bool
|
||||
{
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.min.x > self.max.x || self.min.y > self.max.y || self.min.z > self.max.z
|
||||
}
|
||||
|
||||
/// Returns `true` when both corners are finite.
|
||||
pub fn is_finite(&self) -> bool
|
||||
{
|
||||
pub fn is_finite(&self) -> bool {
|
||||
self.min.is_finite() && self.max.is_finite()
|
||||
}
|
||||
|
||||
pub fn contains_point(&self, point: Vec3) -> bool
|
||||
{
|
||||
pub fn contains_point(&self, point: Vec3) -> bool {
|
||||
point.cmpge(self.min).all() && point.cmple(self.max).all()
|
||||
}
|
||||
|
||||
/// Returns `true` when `other` is fully inside `self`.
|
||||
///
|
||||
/// An empty `other` is contained in every box.
|
||||
pub fn contains(&self, other: &Self) -> bool
|
||||
{
|
||||
pub fn contains(&self, other: &Self) -> bool {
|
||||
other.is_empty() || (self.min.cmple(other.min).all() && self.max.cmpge(other.max).all())
|
||||
}
|
||||
|
||||
/// Returns `true` when both boxes share at least one point.
|
||||
///
|
||||
/// Boxes touching exactly on a face, edge or corner do intersect.
|
||||
pub fn intersects(&self, other: &Self) -> bool
|
||||
{
|
||||
pub fn intersects(&self, other: &Self) -> bool {
|
||||
!self.is_empty()
|
||||
&& !other.is_empty()
|
||||
&& self.min.cmple(other.max).all()
|
||||
@@ -162,8 +138,7 @@ impl Aabb
|
||||
/// Overlap of both boxes.
|
||||
///
|
||||
/// The result is empty when the boxes do not intersect.
|
||||
pub fn intersection(&self, other: &Self) -> Self
|
||||
{
|
||||
pub fn intersection(&self, other: &Self) -> Self {
|
||||
Self {
|
||||
min: self.min.max(other.min),
|
||||
max: self.max.min(other.max),
|
||||
@@ -171,22 +146,19 @@ impl Aabb
|
||||
}
|
||||
|
||||
/// Grows the box to contain `point`.
|
||||
pub fn union_point(&mut self, point: Vec3)
|
||||
{
|
||||
pub fn union_point(&mut self, point: Vec3) {
|
||||
self.min = self.min.min(point);
|
||||
self.max = self.max.max(point);
|
||||
}
|
||||
|
||||
/// Grows the box to contain `other`.
|
||||
pub fn union(&mut self, other: &Self)
|
||||
{
|
||||
pub fn union(&mut self, other: &Self) {
|
||||
self.min = self.min.min(other.min);
|
||||
self.max = self.max.max(other.max);
|
||||
}
|
||||
|
||||
/// Returns a copy of the box grown by `margin` on every side.
|
||||
pub fn expanded(&self, margin: f32) -> Self
|
||||
{
|
||||
pub fn expanded(&self, margin: f32) -> Self {
|
||||
let margin = Vec3::splat(margin);
|
||||
Self {
|
||||
min: self.min - margin,
|
||||
@@ -199,10 +171,8 @@ impl Aabb
|
||||
///
|
||||
/// This avoids degenerate (flat) boxes, which are problematic for SAH
|
||||
/// splitting. An empty box is returned unchanged.
|
||||
pub fn padded_to_min_size(&self, min_size: f32) -> Self
|
||||
{
|
||||
if self.is_empty()
|
||||
{
|
||||
pub fn padded_to_min_size(&self, min_size: f32) -> Self {
|
||||
if self.is_empty() {
|
||||
return Self::EMPTY;
|
||||
}
|
||||
|
||||
@@ -211,8 +181,7 @@ impl Aabb
|
||||
}
|
||||
|
||||
/// Returns an iterator over all of the corner points of the box
|
||||
pub fn corner_points(&self) -> impl Iterator<Item = Vec3>
|
||||
{
|
||||
pub fn corner_points(&self) -> impl Iterator<Item = Vec3> {
|
||||
(0..=7).map(|i| {
|
||||
let a = &self.min;
|
||||
let b = &self.max;
|
||||
@@ -226,8 +195,7 @@ impl Aabb
|
||||
}
|
||||
|
||||
/// Returns the Aabb bounding box of the box transformed by `transformed`
|
||||
pub fn as_transformed_by(&self, transform: &Mat4) -> Self
|
||||
{
|
||||
pub fn as_transformed_by(&self, transform: &Mat4) -> Self {
|
||||
Self::from_points(
|
||||
self.corner_points()
|
||||
.map(|point| transform.transform_point3(point)),
|
||||
@@ -235,23 +203,19 @@ impl Aabb
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Aabb
|
||||
{
|
||||
fn default() -> Self
|
||||
{
|
||||
impl Default for Aabb {
|
||||
fn default() -> Self {
|
||||
Self::EMPTY
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests
|
||||
{
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const EPSILON: f32 = 1e-6;
|
||||
|
||||
fn assert_vec3_eq(a: Vec3, b: Vec3)
|
||||
{
|
||||
fn assert_vec3_eq(a: Vec3, b: Vec3) {
|
||||
assert!(
|
||||
(a - b).abs().max_element() <= EPSILON,
|
||||
"expected {b:?}, got {a:?}"
|
||||
@@ -259,8 +223,7 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_box_contains_nothing()
|
||||
{
|
||||
fn empty_box_contains_nothing() {
|
||||
let aabb = Aabb::EMPTY;
|
||||
assert!(aabb.is_empty());
|
||||
assert!(!aabb.contains_point(Vec3::ZERO));
|
||||
@@ -268,8 +231,7 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_is_identity_for_union()
|
||||
{
|
||||
fn empty_is_identity_for_union() {
|
||||
let aabb = Aabb::new(Vec3::ZERO, Vec3::ONE);
|
||||
|
||||
let mut grown = Aabb::EMPTY;
|
||||
@@ -282,14 +244,12 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_points_of_empty_iterator_is_empty()
|
||||
{
|
||||
fn from_points_of_empty_iterator_is_empty() {
|
||||
assert!(Aabb::from_points(std::iter::empty()).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_points_bounds_all_points()
|
||||
{
|
||||
fn from_points_bounds_all_points() {
|
||||
let points = [
|
||||
Vec3::new(1.0, -2.0, 3.0),
|
||||
Vec3::new(-4.0, 5.0, -6.0),
|
||||
@@ -299,15 +259,13 @@ mod tests
|
||||
let aabb = Aabb::from_points(points);
|
||||
assert_eq!(aabb.min(), Vec3::new(-4.0, -2.0, -6.0));
|
||||
assert_eq!(aabb.max(), Vec3::new(1.0, 5.0, 3.0));
|
||||
for point in points
|
||||
{
|
||||
for point in points {
|
||||
assert!(aabb.contains_point(point));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn center_and_size()
|
||||
{
|
||||
fn center_and_size() {
|
||||
let aabb = Aabb::new(Vec3::new(-1.0, 0.0, 2.0), Vec3::new(3.0, 4.0, 6.0));
|
||||
|
||||
assert_vec3_eq(aabb.center(), Vec3::new(1.0, 2.0, 4.0));
|
||||
@@ -316,8 +274,7 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_center_half_extents_round_trips()
|
||||
{
|
||||
fn from_center_half_extents_round_trips() {
|
||||
let aabb = Aabb::from_center_half_extents(Vec3::new(1.0, 2.0, 3.0), Vec3::splat(2.0));
|
||||
|
||||
assert_eq!(aabb.min(), Vec3::new(-1.0, 0.0, 1.0));
|
||||
@@ -325,8 +282,7 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn union_grows_to_contain_both_boxes()
|
||||
{
|
||||
fn union_grows_to_contain_both_boxes() {
|
||||
let a = Aabb::new(Vec3::ZERO, Vec3::ONE);
|
||||
let b = Aabb::new(Vec3::splat(2.0), Vec3::splat(4.0));
|
||||
|
||||
@@ -340,8 +296,7 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contains_point_respects_faces()
|
||||
{
|
||||
fn contains_point_respects_faces() {
|
||||
let aabb = Aabb::new(Vec3::ZERO, Vec3::ONE);
|
||||
|
||||
assert!(aabb.contains_point(Vec3::ZERO));
|
||||
@@ -352,8 +307,7 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contains_box()
|
||||
{
|
||||
fn contains_box() {
|
||||
let outer = Aabb::new(Vec3::ZERO, Vec3::splat(4.0));
|
||||
let inner = Aabb::new(Vec3::ONE, Vec3::splat(3.0));
|
||||
|
||||
@@ -363,8 +317,7 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn separated_boxes_do_not_intersect()
|
||||
{
|
||||
fn separated_boxes_do_not_intersect() {
|
||||
let a = Aabb::new(Vec3::ZERO, Vec3::ONE);
|
||||
let b = Aabb::new(Vec3::new(2.0, 0.0, 0.0), Vec3::new(3.0, 1.0, 1.0));
|
||||
|
||||
@@ -374,8 +327,7 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn touching_boxes_intersect()
|
||||
{
|
||||
fn touching_boxes_intersect() {
|
||||
let a = Aabb::new(Vec3::ZERO, Vec3::ONE);
|
||||
let b = Aabb::new(Vec3::new(1.0, 0.0, 0.0), Vec3::new(2.0, 1.0, 1.0));
|
||||
|
||||
@@ -384,8 +336,7 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlapping_boxes_intersect()
|
||||
{
|
||||
fn overlapping_boxes_intersect() {
|
||||
let a = Aabb::new(Vec3::ZERO, Vec3::splat(2.0));
|
||||
let b = Aabb::new(Vec3::ONE, Vec3::splat(3.0));
|
||||
|
||||
@@ -397,8 +348,7 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn surface_area_and_volume()
|
||||
{
|
||||
fn surface_area_and_volume() {
|
||||
let aabb = Aabb::new(Vec3::ZERO, Vec3::new(2.0, 3.0, 4.0));
|
||||
|
||||
assert!((aabb.volume() - 24.0).abs() <= EPSILON);
|
||||
@@ -406,8 +356,7 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn longest_axis_picks_largest_extent()
|
||||
{
|
||||
fn longest_axis_picks_largest_extent() {
|
||||
assert_eq!(
|
||||
Aabb::new(Vec3::ZERO, Vec3::new(3.0, 1.0, 2.0)).longest_axis(),
|
||||
0
|
||||
@@ -423,8 +372,7 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expanded_adds_margin_on_every_side()
|
||||
{
|
||||
fn expanded_adds_margin_on_every_side() {
|
||||
let aabb = Aabb::new(Vec3::ZERO, Vec3::ONE).expanded(0.5);
|
||||
|
||||
assert_eq!(aabb.min(), Vec3::splat(-0.5));
|
||||
@@ -433,8 +381,7 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn padded_to_min_size_keeps_flat_box_usable()
|
||||
{
|
||||
fn padded_to_min_size_keeps_flat_box_usable() {
|
||||
let flat = Aabb::new(Vec3::new(0.0, 0.0, 5.0), Vec3::new(2.0, 2.0, 5.0));
|
||||
let padded = flat.padded_to_min_size(1.0);
|
||||
|
||||
@@ -447,21 +394,18 @@ mod tests
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn padded_empty_box_stays_empty()
|
||||
{
|
||||
fn padded_empty_box_stays_empty() {
|
||||
assert!(Aabb::EMPTY.padded_to_min_size(1.0).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_is_empty()
|
||||
{
|
||||
fn default_is_empty() {
|
||||
assert_eq!(Aabb::default(), Aabb::EMPTY);
|
||||
assert!(Aabb::default().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layout_is_two_vec3s()
|
||||
{
|
||||
fn layout_is_two_vec3s() {
|
||||
assert_eq!(std::mem::size_of::<Aabb>(), 24);
|
||||
assert_eq!(bytemuck::bytes_of(&Aabb::EMPTY).len(), 24);
|
||||
}
|
||||
|
||||
@@ -11,8 +11,7 @@ use super::aabb::Aabb;
|
||||
|
||||
macro_rules! get_dim {
|
||||
($v:expr, $d:expr) => {
|
||||
match $d
|
||||
{
|
||||
match $d {
|
||||
0 => $v.x,
|
||||
1 => $v.y,
|
||||
_ => $v.z,
|
||||
@@ -29,8 +28,7 @@ const SAH_INTERSECT: f32 = 1.;
|
||||
const SAH_BIN_COUNT: usize = 16;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct BvhNode
|
||||
{
|
||||
pub struct BvhNode {
|
||||
/// The bounding box of this bvh node
|
||||
aabb: Aabb,
|
||||
|
||||
@@ -43,10 +41,8 @@ pub struct BvhNode
|
||||
first_or_left: u32,
|
||||
}
|
||||
|
||||
impl From<GpuBvhNode> for BvhNode
|
||||
{
|
||||
fn from(value: GpuBvhNode) -> Self
|
||||
{
|
||||
impl From<GpuBvhNode> for BvhNode {
|
||||
fn from(value: GpuBvhNode) -> Self {
|
||||
Self {
|
||||
aabb: value.aabb.into(),
|
||||
count: value.count,
|
||||
@@ -55,10 +51,8 @@ impl From<GpuBvhNode> for BvhNode
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BvhNode> for GpuBvhNode
|
||||
{
|
||||
fn from(value: BvhNode) -> Self
|
||||
{
|
||||
impl From<BvhNode> for GpuBvhNode {
|
||||
fn from(value: BvhNode) -> Self {
|
||||
GpuBvhNode {
|
||||
aabb: value.aabb.into(),
|
||||
count: value.count,
|
||||
@@ -68,10 +62,8 @@ impl From<BvhNode> for GpuBvhNode
|
||||
}
|
||||
}
|
||||
|
||||
impl BvhNode
|
||||
{
|
||||
pub fn internal(aabb: Aabb, left: u32) -> BvhNode
|
||||
{
|
||||
impl BvhNode {
|
||||
pub fn internal(aabb: Aabb, left: u32) -> BvhNode {
|
||||
Self {
|
||||
aabb,
|
||||
count: 0,
|
||||
@@ -79,8 +71,7 @@ impl BvhNode
|
||||
}
|
||||
}
|
||||
|
||||
pub fn leaf(aabb: Aabb, count: NonZero<u32>, first_index: u32) -> BvhNode
|
||||
{
|
||||
pub fn leaf(aabb: Aabb, count: NonZero<u32>, first_index: u32) -> BvhNode {
|
||||
Self {
|
||||
aabb,
|
||||
count: count.into(),
|
||||
@@ -88,78 +79,59 @@ impl BvhNode
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_internal(&self) -> bool
|
||||
{
|
||||
pub fn is_internal(&self) -> bool {
|
||||
self.count == 0
|
||||
}
|
||||
|
||||
pub fn is_leaf(&self) -> bool
|
||||
{
|
||||
pub fn is_leaf(&self) -> bool {
|
||||
self.count != 0
|
||||
}
|
||||
|
||||
pub fn first(&self) -> Option<u32>
|
||||
{
|
||||
if !self.is_leaf()
|
||||
{
|
||||
pub fn first(&self) -> Option<u32> {
|
||||
if !self.is_leaf() {
|
||||
None
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Some(self.first_or_left)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn count(&self) -> Option<u32>
|
||||
{
|
||||
if !self.is_leaf()
|
||||
{
|
||||
pub fn count(&self) -> Option<u32> {
|
||||
if !self.is_leaf() {
|
||||
None
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Some(self.count)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn left(&self) -> Option<u32>
|
||||
{
|
||||
if !self.is_internal()
|
||||
{
|
||||
pub fn left(&self) -> Option<u32> {
|
||||
if !self.is_internal() {
|
||||
None
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Some(self.first_or_left)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn right(&self) -> Option<u32>
|
||||
{
|
||||
pub fn right(&self) -> Option<u32> {
|
||||
self.left().map(|x| x + 1)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BvhPrimitive<T>
|
||||
{
|
||||
pub struct BvhPrimitive<T> {
|
||||
pub aabb: Aabb,
|
||||
pub handle: T,
|
||||
}
|
||||
|
||||
pub struct Bvh
|
||||
{
|
||||
pub struct Bvh {
|
||||
/// List of the bvh's nodes
|
||||
pub nodes: Vec<BvhNode>,
|
||||
pub depth: usize,
|
||||
}
|
||||
|
||||
pub fn build_bvh<T>(primitives: &mut [BvhPrimitive<T>], leaf_max: usize) -> Bvh
|
||||
{
|
||||
pub fn build_bvh<T>(primitives: &mut [BvhPrimitive<T>], leaf_max: usize) -> Bvh {
|
||||
let mut nodes = Vec::new();
|
||||
//let mut prim_indices = Vec::new();
|
||||
|
||||
if primitives.is_empty()
|
||||
{
|
||||
if primitives.is_empty() {
|
||||
return Bvh {
|
||||
nodes,
|
||||
depth: 1,
|
||||
@@ -194,8 +166,7 @@ pub fn build_node<T>(
|
||||
leaf_max: usize,
|
||||
depth: usize,
|
||||
max_depth: &mut usize,
|
||||
)
|
||||
{
|
||||
) {
|
||||
// We only extend a leaf if necessary
|
||||
assert!(nodes[node_index].is_leaf());
|
||||
let offset = nodes[node_index].first().expect("Node is leaf");
|
||||
@@ -207,10 +178,8 @@ pub fn build_node<T>(
|
||||
// We must now decide to keep the node as a leaf, or split using the heuristic:
|
||||
let split = sah_split(node_primitives, leaf_max);
|
||||
|
||||
match split
|
||||
{
|
||||
Some((split_dimension, split_plane)) =>
|
||||
{
|
||||
match split {
|
||||
Some((split_dimension, split_plane)) => {
|
||||
// Sah says split, do the split
|
||||
// We sort along the split dimension
|
||||
node_primitives.sort_unstable_by_key(|primitive| {
|
||||
@@ -222,15 +191,11 @@ pub fn build_node<T>(
|
||||
let mut left_aabb = Aabb::EMPTY;
|
||||
let mut right_aabb = Aabb::EMPTY;
|
||||
|
||||
for primitive in node_primitives.iter()
|
||||
{
|
||||
if get_dim!(primitive.aabb.center(), split_dimension) < split_plane
|
||||
{
|
||||
for primitive in node_primitives.iter() {
|
||||
if get_dim!(primitive.aabb.center(), split_dimension) < split_plane {
|
||||
left_count += 1;
|
||||
left_aabb.union(&primitive.aabb);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
right_aabb.union(&primitive.aabb);
|
||||
}
|
||||
}
|
||||
@@ -275,11 +240,9 @@ pub fn build_node<T>(
|
||||
max_depth,
|
||||
);
|
||||
}
|
||||
None =>
|
||||
{
|
||||
None => {
|
||||
// Sah says keep as leaf, do nothing
|
||||
if depth > *max_depth
|
||||
{
|
||||
if depth > *max_depth {
|
||||
*max_depth = depth;
|
||||
}
|
||||
}
|
||||
@@ -287,11 +250,9 @@ pub fn build_node<T>(
|
||||
}
|
||||
|
||||
/// Returns Some if SAH says split, none if it wants a leaf
|
||||
pub fn sah_split<T>(primitives: &[BvhPrimitive<T>], leaf_max: usize) -> Option<(usize, f32)>
|
||||
{
|
||||
pub fn sah_split<T>(primitives: &[BvhPrimitive<T>], leaf_max: usize) -> Option<(usize, f32)> {
|
||||
// Don't even bother, spliting it does nothing
|
||||
if primitives.len() <= 2
|
||||
{
|
||||
if primitives.len() <= 2 {
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -308,8 +269,7 @@ pub fn sah_split<T>(primitives: &[BvhPrimitive<T>], leaf_max: usize) -> Option<(
|
||||
let mut best_dimension = 0;
|
||||
let mut minimum_cost = f32::INFINITY;
|
||||
let mut minimum_split_plane = 0.;
|
||||
for dimension in 0..3usize
|
||||
{
|
||||
for dimension in 0..3usize {
|
||||
let cetroid_min = get_dim!(centroid_bounds.min(), dimension);
|
||||
|
||||
// The size of a bin in this dimension
|
||||
@@ -320,8 +280,7 @@ pub fn sah_split<T>(primitives: &[BvhPrimitive<T>], leaf_max: usize) -> Option<(
|
||||
let mut bin_counts: [usize; SAH_BIN_COUNT] = Default::default();
|
||||
|
||||
// Bin elements
|
||||
for primitive in primitives.iter()
|
||||
{
|
||||
for primitive in primitives.iter() {
|
||||
let centroid = get_dim!(primitive.aabb.center(), dimension);
|
||||
let bin_index =
|
||||
(((centroid - cetroid_min) / bin_width) as usize).clamp(0, SAH_BIN_COUNT - 1);
|
||||
@@ -338,8 +297,7 @@ pub fn sah_split<T>(primitives: &[BvhPrimitive<T>], leaf_max: usize) -> Option<(
|
||||
let mut suffix_counts: [usize; SAH_BIN_COUNT] = bin_counts;
|
||||
|
||||
// We build prefix and suffix simultaneously
|
||||
for i in 1..SAH_BIN_COUNT
|
||||
{
|
||||
for i in 1..SAH_BIN_COUNT {
|
||||
let prefix_previous = prefix[i - 1];
|
||||
prefix[i].union(&prefix_previous);
|
||||
prefix_counts[i] += prefix_counts[i - 1];
|
||||
@@ -352,14 +310,12 @@ pub fn sah_split<T>(primitives: &[BvhPrimitive<T>], leaf_max: usize) -> Option<(
|
||||
// Chose a split
|
||||
// We iterate only up to SAH_BIN_COUNT - 1
|
||||
// as we dont want to put all primitves on the left or right
|
||||
for i in 0..(SAH_BIN_COUNT - 1)
|
||||
{
|
||||
for i in 0..(SAH_BIN_COUNT - 1) {
|
||||
let left_sah = prefix[i].surface_area() * prefix_counts[i] as f32;
|
||||
let right_sah = suffix[i + 1].surface_area() * suffix_counts[i + 1] as f32;
|
||||
|
||||
let split_sah = left_sah + right_sah;
|
||||
if split_sah < minimum_cost
|
||||
{
|
||||
if split_sah < minimum_cost {
|
||||
best_dimension = dimension;
|
||||
minimum_cost = split_sah;
|
||||
|
||||
@@ -369,13 +325,10 @@ pub fn sah_split<T>(primitives: &[BvhPrimitive<T>], leaf_max: usize) -> Option<(
|
||||
}
|
||||
}
|
||||
|
||||
if leaf_sah < minimum_cost && primitives.len() <= leaf_max
|
||||
{
|
||||
if leaf_sah < minimum_cost && primitives.len() <= leaf_max {
|
||||
// SAH requires leaf
|
||||
None
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// SAH requires split
|
||||
Some((best_dimension, minimum_split_plane))
|
||||
}
|
||||
|
||||
@@ -18,24 +18,21 @@ const _: () = {
|
||||
///
|
||||
/// One instance is one `GpuBvhNode`; the shader generates all vertices, so no
|
||||
/// vertex or index buffer is needed.
|
||||
pub struct BvhDebugPipeline
|
||||
{
|
||||
pub struct BvhDebugPipeline {
|
||||
solid_pipeline: wgpu::RenderPipeline,
|
||||
line_pipeline: wgpu::RenderPipeline,
|
||||
bind_group_layout: wgpu::BindGroupLayout,
|
||||
view_proj_buffer: wgpu::Buffer,
|
||||
}
|
||||
|
||||
impl BvhDebugPipeline
|
||||
{
|
||||
impl BvhDebugPipeline {
|
||||
/// `color_format` must match the render pass attachment, including its sRGB
|
||||
/// suffix. `depth_format` must match `depth_view` in [`Self::render`].
|
||||
pub fn new(
|
||||
device: &wgpu::Device,
|
||||
color_format: wgpu::TextureFormat,
|
||||
depth_format: wgpu::TextureFormat,
|
||||
) -> Self
|
||||
{
|
||||
) -> Self {
|
||||
let shader = device.create_shader_module(wgpu::include_wgsl!("debug.wgsl"));
|
||||
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
@@ -182,10 +179,8 @@ impl BvhDebugPipeline
|
||||
object_buffer: &wgpu::Buffer,
|
||||
node_count: u32,
|
||||
max_objects_per_leaf: u32,
|
||||
)
|
||||
{
|
||||
if node_count == 0
|
||||
{
|
||||
) {
|
||||
if node_count == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -238,8 +233,7 @@ impl BvhDebugPipeline
|
||||
|
||||
// Filled object boxes first so the wireframes can depth-test against
|
||||
// them.
|
||||
if max_objects_per_leaf > 0
|
||||
{
|
||||
if max_objects_per_leaf > 0 {
|
||||
pass.set_pipeline(&self.solid_pipeline);
|
||||
pass.draw(0..36 * max_objects_per_leaf, 0..node_count);
|
||||
}
|
||||
|
||||
+11
-22
@@ -7,8 +7,7 @@ use crate::aabb::Aabb;
|
||||
|
||||
#[derive(Debug, Clone, Copy, bytemuck::Zeroable, bytemuck::Pod)]
|
||||
#[repr(C)]
|
||||
pub struct GpuAabb
|
||||
{
|
||||
pub struct GpuAabb {
|
||||
pub min: Vec3,
|
||||
_pad0: u32, // On the GPU vec3 are aligned to 4 u32
|
||||
pub max: Vec3,
|
||||
@@ -22,8 +21,7 @@ pub struct GpuAabb
|
||||
/// of data up to a 48-byte stride.
|
||||
#[derive(Debug, Clone, Copy, bytemuck::Zeroable, bytemuck::Pod)]
|
||||
#[repr(C)]
|
||||
pub struct GpuBvhNode
|
||||
{
|
||||
pub struct GpuBvhNode {
|
||||
pub aabb: GpuAabb,
|
||||
pub count: u32,
|
||||
pub first_or_left: u32,
|
||||
@@ -33,34 +31,27 @@ pub struct GpuBvhNode
|
||||
/// To be a leaf of a GPU BVH
|
||||
#[derive(Debug, Clone, Copy, bytemuck::Zeroable, bytemuck::Pod)]
|
||||
#[repr(C)]
|
||||
pub struct GpuObject
|
||||
{
|
||||
pub struct GpuObject {
|
||||
local_to_world: Mat4, // Tranform
|
||||
world_to_local: Mat4, // Ray to chunk
|
||||
table_slot: u32,
|
||||
_pad0: [u32; 3],
|
||||
}
|
||||
|
||||
impl From<Aabb> for GpuAabb
|
||||
{
|
||||
fn from(value: Aabb) -> Self
|
||||
{
|
||||
impl From<Aabb> for GpuAabb {
|
||||
fn from(value: Aabb) -> Self {
|
||||
Self::new(value.min(), value.max())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<GpuAabb> for Aabb
|
||||
{
|
||||
fn from(value: GpuAabb) -> Self
|
||||
{
|
||||
impl From<GpuAabb> for Aabb {
|
||||
fn from(value: GpuAabb) -> Self {
|
||||
Aabb::new(value.min, value.max)
|
||||
}
|
||||
}
|
||||
|
||||
impl GpuObject
|
||||
{
|
||||
pub fn new(slot: u32, transform: Mat4) -> Self
|
||||
{
|
||||
impl GpuObject {
|
||||
pub fn new(slot: u32, transform: Mat4) -> Self {
|
||||
GpuObject {
|
||||
local_to_world: transform,
|
||||
world_to_local: transform.inverse(),
|
||||
@@ -70,10 +61,8 @@ impl GpuObject
|
||||
}
|
||||
}
|
||||
|
||||
impl GpuAabb
|
||||
{
|
||||
pub fn new(min: Vec3, max: Vec3) -> Self
|
||||
{
|
||||
impl GpuAabb {
|
||||
pub fn new(min: Vec3, max: Vec3) -> Self {
|
||||
Self {
|
||||
min,
|
||||
_pad0: Default::default(),
|
||||
|
||||
+37
-74
@@ -31,8 +31,7 @@ pub mod gpu;
|
||||
|
||||
/// To handle an object stored in the bvh
|
||||
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
|
||||
pub struct BvhHandle
|
||||
{
|
||||
pub struct BvhHandle {
|
||||
pub anchor: StructureAnchor,
|
||||
pub producer_id: ProducerId,
|
||||
|
||||
@@ -42,8 +41,7 @@ pub struct BvhHandle
|
||||
}
|
||||
|
||||
/// An object that is stored by the BVH structure
|
||||
pub struct BvhObject
|
||||
{
|
||||
pub struct BvhObject {
|
||||
/// Identity of the voxel data
|
||||
pub anchor: StructureAnchor,
|
||||
pub producer_id: ProducerId,
|
||||
@@ -56,15 +54,12 @@ pub struct BvhObject
|
||||
pub bounding_box: Aabb,
|
||||
}
|
||||
|
||||
impl BvhObject
|
||||
{
|
||||
pub fn world_bounding_box(&self) -> Aabb
|
||||
{
|
||||
impl BvhObject {
|
||||
pub fn world_bounding_box(&self) -> Aabb {
|
||||
self.bounding_box.as_transformed_by(&self.transform)
|
||||
}
|
||||
|
||||
pub fn structure_anchor(&self) -> StructureAnchor
|
||||
{
|
||||
pub fn structure_anchor(&self) -> StructureAnchor {
|
||||
self.anchor
|
||||
}
|
||||
}
|
||||
@@ -79,8 +74,7 @@ impl BvhObject
|
||||
/// Raytrace bindings:
|
||||
/// 0 = `GpuBvhNode[]` (read-only), 1 = `GpuObject[]` (read-only),
|
||||
/// 2 = inner structure-table pointers, 3 = inner structure-table requests.
|
||||
pub struct BvhStructureTable
|
||||
{
|
||||
pub struct BvhStructureTable {
|
||||
/// Handle to object in the bvh
|
||||
objects: HashMap<BvhHandle, BvhObject>,
|
||||
|
||||
@@ -111,10 +105,8 @@ pub struct BvhStructureTable
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl BvhStructureTable
|
||||
{
|
||||
pub fn new(device: &wgpu::Device, initial_capacity: usize, max_requests: usize) -> Self
|
||||
{
|
||||
impl BvhStructureTable {
|
||||
pub fn new(device: &wgpu::Device, initial_capacity: usize, max_requests: usize) -> Self {
|
||||
let structure_table = DynamicStructureTable::new(device, initial_capacity, max_requests, 0);
|
||||
|
||||
let raytrace_bind_group_layout = create_bvh_raytrace_bind_group_layout(device);
|
||||
@@ -151,8 +143,7 @@ impl BvhStructureTable
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
object: BvhObject,
|
||||
) -> BvhHandle
|
||||
{
|
||||
) -> BvhHandle {
|
||||
let key = self.current_key;
|
||||
self.current_key += 1;
|
||||
let handle = BvhHandle {
|
||||
@@ -170,8 +161,7 @@ impl BvhStructureTable
|
||||
handle
|
||||
}
|
||||
|
||||
pub fn build(&self) -> (Vec<GpuBvhNode>, Vec<GpuObject>)
|
||||
{
|
||||
pub fn build(&self) -> (Vec<GpuBvhNode>, Vec<GpuObject>) {
|
||||
// Collect primitives
|
||||
let mut primitives = self
|
||||
.objects
|
||||
@@ -207,29 +197,24 @@ impl BvhStructureTable
|
||||
|
||||
/// Marks the CPU BVH as changed. The GPU buffers are rebuilt on the next
|
||||
/// [`Self::rebuild_if_dirty`] call.
|
||||
pub fn mark_dirty(&mut self)
|
||||
{
|
||||
pub fn mark_dirty(&mut self) {
|
||||
self.dirty = true;
|
||||
}
|
||||
|
||||
/// Returns true if the GPU buffers no longer match the CPU BVH.
|
||||
pub fn is_dirty(&self) -> bool
|
||||
{
|
||||
pub fn is_dirty(&self) -> bool {
|
||||
self.dirty
|
||||
}
|
||||
|
||||
/// Rebuilds the CPU BVH and uploads it to the GPU.
|
||||
pub fn rebuild(&mut self, device: &wgpu::Device, queue: &wgpu::Queue)
|
||||
{
|
||||
pub fn rebuild(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
|
||||
let (nodes, objects) = self.build();
|
||||
self.upload(device, queue, &nodes, &objects);
|
||||
}
|
||||
|
||||
/// Rebuilds only if the BVH changed since the last upload.
|
||||
pub fn rebuild_if_dirty(&mut self, device: &wgpu::Device, queue: &wgpu::Queue)
|
||||
{
|
||||
if self.dirty
|
||||
{
|
||||
pub fn rebuild_if_dirty(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
|
||||
if self.dirty {
|
||||
self.rebuild(device, queue);
|
||||
}
|
||||
}
|
||||
@@ -240,29 +225,24 @@ impl BvhStructureTable
|
||||
queue: &wgpu::Queue,
|
||||
nodes: &[GpuBvhNode],
|
||||
objects: &[GpuObject],
|
||||
)
|
||||
{
|
||||
) {
|
||||
let node_capacity = grown_capacity(self.node_capacity, nodes.len());
|
||||
if node_capacity > self.node_capacity
|
||||
{
|
||||
if node_capacity > self.node_capacity {
|
||||
self.node_capacity = node_capacity;
|
||||
self.bvh_nodes = create_bvh_buffer::<GpuBvhNode>(device, node_capacity, "bvh_nodes");
|
||||
}
|
||||
|
||||
let object_capacity = grown_capacity(self.object_capacity, objects.len());
|
||||
if object_capacity > self.object_capacity
|
||||
{
|
||||
if object_capacity > self.object_capacity {
|
||||
self.object_capacity = object_capacity;
|
||||
self.bvh_objects =
|
||||
create_bvh_buffer::<GpuObject>(device, object_capacity, "bvh_objects");
|
||||
}
|
||||
|
||||
if !nodes.is_empty()
|
||||
{
|
||||
if !nodes.is_empty() {
|
||||
queue.write_buffer(&self.bvh_nodes, 0, bytemuck::cast_slice(nodes));
|
||||
}
|
||||
if !objects.is_empty()
|
||||
{
|
||||
if !objects.is_empty() {
|
||||
queue.write_buffer(&self.bvh_objects, 0, bytemuck::cast_slice(objects));
|
||||
}
|
||||
|
||||
@@ -284,33 +264,27 @@ impl BvhStructureTable
|
||||
self.dirty = false;
|
||||
}
|
||||
|
||||
pub fn bvh_nodes(&self) -> &wgpu::Buffer
|
||||
{
|
||||
pub fn bvh_nodes(&self) -> &wgpu::Buffer {
|
||||
&self.bvh_nodes
|
||||
}
|
||||
|
||||
pub fn bvh_objects(&self) -> &wgpu::Buffer
|
||||
{
|
||||
pub fn bvh_objects(&self) -> &wgpu::Buffer {
|
||||
&self.bvh_objects
|
||||
}
|
||||
|
||||
pub fn node_count(&self) -> u32
|
||||
{
|
||||
pub fn node_count(&self) -> u32 {
|
||||
self.node_count
|
||||
}
|
||||
|
||||
pub fn object_count(&self) -> u32
|
||||
{
|
||||
pub fn object_count(&self) -> u32 {
|
||||
self.object_count
|
||||
}
|
||||
|
||||
pub fn raytrace_bind_group_layout(&self) -> &wgpu::BindGroupLayout
|
||||
{
|
||||
pub fn raytrace_bind_group_layout(&self) -> &wgpu::BindGroupLayout {
|
||||
&self.raytrace_bind_group_layout
|
||||
}
|
||||
|
||||
pub fn raytrace_bind_group(&self) -> &wgpu::BindGroup
|
||||
{
|
||||
pub fn raytrace_bind_group(&self) -> &wgpu::BindGroup {
|
||||
&self.raytrace_bind_group
|
||||
}
|
||||
|
||||
@@ -321,8 +295,7 @@ impl BvhStructureTable
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
|
||||
request_buffer: &CacheRequestBuffer,
|
||||
)
|
||||
{
|
||||
) {
|
||||
self.structure_table
|
||||
.gather_requests(device, encoder, request_buffer);
|
||||
}
|
||||
@@ -340,13 +313,11 @@ impl BvhStructureTable
|
||||
self.structure_table.insert(device, encoder, insertion);
|
||||
}
|
||||
|
||||
pub fn get(&self, handle: &BvhHandle) -> Option<&BvhObject>
|
||||
{
|
||||
pub fn get(&self, handle: &BvhHandle) -> Option<&BvhObject> {
|
||||
self.objects.get(handle)
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, handle: &BvhHandle) -> Option<&mut BvhObject>
|
||||
{
|
||||
pub fn get_mut(&mut self, handle: &BvhHandle) -> Option<&mut BvhObject> {
|
||||
self.objects.get_mut(handle)
|
||||
}
|
||||
}
|
||||
@@ -360,8 +331,7 @@ where
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
|
||||
insertion: &CacheInsertionData<N>,
|
||||
)
|
||||
{
|
||||
) {
|
||||
self.structure_table
|
||||
.validate_insertion(device, encoder, insertion);
|
||||
}
|
||||
@@ -371,15 +341,13 @@ where
|
||||
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
|
||||
cache_evictor: &CacheEvictor,
|
||||
invalid_timestamp: u32,
|
||||
)
|
||||
{
|
||||
) {
|
||||
self.structure_table
|
||||
.invalidate(encoder, cache_evictor, invalid_timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
fn create_bvh_buffer<T>(device: &wgpu::Device, capacity: usize, label: &str) -> wgpu::Buffer
|
||||
{
|
||||
fn create_bvh_buffer<T>(device: &wgpu::Device, capacity: usize, label: &str) -> wgpu::Buffer {
|
||||
let capacity = capacity.max(1);
|
||||
device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some(label),
|
||||
@@ -389,23 +357,19 @@ fn create_bvh_buffer<T>(device: &wgpu::Device, capacity: usize, label: &str) ->
|
||||
})
|
||||
}
|
||||
|
||||
fn grown_capacity(current: usize, required: usize) -> usize
|
||||
{
|
||||
if required <= current
|
||||
{
|
||||
fn grown_capacity(current: usize, required: usize) -> usize {
|
||||
if required <= current {
|
||||
return current;
|
||||
}
|
||||
|
||||
let mut capacity = current.max(1);
|
||||
while capacity < required
|
||||
{
|
||||
while capacity < required {
|
||||
capacity = capacity.saturating_mul(2);
|
||||
}
|
||||
capacity
|
||||
}
|
||||
|
||||
fn create_bvh_raytrace_bind_group_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout
|
||||
{
|
||||
fn create_bvh_raytrace_bind_group_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout {
|
||||
let storage = |read_only: bool| wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only },
|
||||
has_dynamic_offset: false,
|
||||
@@ -450,8 +414,7 @@ fn create_bvh_raytrace_bind_group(
|
||||
objects: &wgpu::Buffer,
|
||||
structure_pointers: &wgpu::Buffer,
|
||||
structure_requests: &wgpu::Buffer,
|
||||
) -> wgpu::BindGroup
|
||||
{
|
||||
) -> wgpu::BindGroup {
|
||||
device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("BvhStructureTable::raytrace_bind_group"),
|
||||
layout,
|
||||
|
||||
Reference in New Issue
Block a user