Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/partitioning/bvh/bvh_optimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,15 @@ impl Bvh {
}

workspace.rebuild_leaves.clear();
workspace.rebuild_frame_index = workspace.rebuild_frame_index.overflowing_add(1).0;
let config = self.optimization_config(workspace.rebuild_frame_index);
self.optimization.rebuild_frame_index = self.optimization.rebuild_frame_index.overflowing_add(1).0;
let config = self.optimization_config(self.optimization.rebuild_frame_index);

/*
* Subtree optimizations.
*/
// let t0 = core::time::Instant::now();
let num_leaves = self.nodes[0].leaf_count();
let mut start_index = workspace.rebuild_start_index;
let mut start_index = self.optimization.rebuild_start_index;

// println!("Max candidate leaf count = {}", max_candidate_leaf_count);
self.find_optimization_roots(
Expand All @@ -266,7 +266,7 @@ impl Bvh {
// to reach the target subtree count.
}

workspace.rebuild_start_index = start_index;
self.optimization.rebuild_start_index = start_index;

// println!(
// "Num refinement candidates: {}, list: {:?}",
Expand Down Expand Up @@ -522,6 +522,19 @@ impl Bvh {
}
}

/// The optimization state for used by `Bvh::optimize_incremental`.
/// This allows each call to `optimize_incremental` to continue from where the last one left off.
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
pub(super) struct BvhIncrementalOptimizationState {
pub(super) rebuild_frame_index: u32,
pub(super) rebuild_start_index: u32,
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
enum RootOptimizationMode {
PriorityQueue,
Expand Down
5 changes: 3 additions & 2 deletions src/partitioning/bvh/bvh_tree.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::BvhOptimizationHeapEntry;
use super::bvh_optimize::BvhIncrementalOptimizationState;
use crate::bounding_volume::{Aabb, BoundingVolume};
use crate::math::{Real, Vector};
use crate::query::{Ray, RayCast};
Expand Down Expand Up @@ -136,8 +137,6 @@ pub enum BvhBuildStrategy {
pub struct BvhWorkspace {
pub(super) refit_tmp: BvhNodeVec,
pub(super) rebuild_leaves: Vec<BvhNode>,
pub(super) rebuild_frame_index: u32,
pub(super) rebuild_start_index: u32,
pub(super) optimization_roots: Vec<u32>,
pub(super) queue: BinaryHeap<BvhOptimizationHeapEntry>,
pub(super) dequeue: VecDeque<u32>,
Expand Down Expand Up @@ -1752,6 +1751,7 @@ pub struct Bvh {
// We don’t store this in `Self::nodes` since it’s only useful for node removal.
pub(super) parents: Vec<BvhNodeIndex>,
pub(super) leaf_node_indices: VecMap<BvhNodeIndex>,
pub(super) optimization: BvhIncrementalOptimizationState,
}

impl Bvh {
Expand Down Expand Up @@ -2186,6 +2186,7 @@ impl Bvh {
nodes,
parents,
leaf_node_indices,
optimization: _,
} = self;
nodes.capacity() * size_of::<BvhNodeWide>()
+ parents.capacity() * size_of::<BvhNodeIndex>()
Expand Down