1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use crate::{BvhNode, Bvh};
use crate::utils::UnsafeSliceWrapper;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
pub mod binned_sah;
pub mod locb;
pub mod spatial_sah;
pub trait BuildAlgorithm {
fn build(self) -> Bvh;
}
#[derive(Debug)]
pub enum BvhType {
LocallyOrderedClustered = 0,
BinnedSAH = 1,
}
impl From<u32> for BvhType {
fn from(i: u32) -> Self {
match i {
0 => BvhType::LocallyOrderedClustered,
1 => BvhType::BinnedSAH,
_ => BvhType::BinnedSAH,
}
}
}
#[derive(Debug, Clone)]
struct AtomicNodeStack<'a> {
counter: &'a AtomicUsize,
nodes: UnsafeSliceWrapper<'a, BvhNode>,
}
#[derive(Debug)]
struct AllocatedNodes<'a> {
pub left: usize,
pub right: usize,
pub left_node: &'a mut BvhNode,
pub right_node: &'a mut BvhNode,
}
#[allow(dead_code)]
impl<'a> AtomicNodeStack<'a> {
pub fn new(counter: &'a AtomicUsize, nodes: &'a mut [BvhNode]) -> (Self, &'a mut BvhNode) {
counter.store(1, SeqCst);
let nodes = UnsafeSliceWrapper::new(nodes);
let first_node = nodes.get_mut(0).unwrap();
(Self { counter, nodes }, first_node)
}
pub fn allocate(&self) -> Option<AllocatedNodes<'a>> {
let left = self.counter.fetch_add(2, SeqCst);
let right = left + 1;
let left_node = self.nodes.get_mut(left)?;
let right_node = self.nodes.get_mut(right)?;
Some(AllocatedNodes {
left,
right,
left_node,
right_node,
})
}
pub fn get(&self, idx: usize) -> Option<&'a BvhNode> {
self.nodes.get(idx)
}
pub fn get_mut(&self, idx: usize) -> Option<&'a mut BvhNode> {
self.nodes.get_mut(idx)
}
}