refactor: cleaner request path + raytracing code
This commit is contained in:
+81
-299
@@ -2,6 +2,7 @@
|
||||
#![feature(generic_const_exprs)]
|
||||
#![feature(float_algebraic)]
|
||||
|
||||
use std::fs::File;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::AtomicU32;
|
||||
@@ -23,6 +24,7 @@ use itertools::Itertools;
|
||||
use rand::random_range;
|
||||
use rayon::iter::IntoParallelRefIterator;
|
||||
use rayon::iter::ParallelIterator;
|
||||
use vxls_production::io::read_tree;
|
||||
use vxls_production::sparse_tree::Color;
|
||||
use vxls_production::sparse_tree::NTree;
|
||||
use vxls_production::sparse_tree::load_ply;
|
||||
@@ -56,32 +58,31 @@ use winit::window::WindowId;
|
||||
use crate::camera::Camera;
|
||||
use crate::egui_renderer::EguiRenderer;
|
||||
use crate::producers::SdfGenerator;
|
||||
use crate::producers::TreeProducer;
|
||||
use crate::producers::VoxelAttributes;
|
||||
use vxls_bvh::BvhHandle;
|
||||
use vxls_bvh::BvhObject;
|
||||
use vxls_bvh::BvhStructureTable;
|
||||
use vxls_bvh::aabb::Aabb;
|
||||
use vxls_bvh::debug::BvhDebugPipeline;
|
||||
use vxls_production::Attributes;
|
||||
use vxls_production::ProducedNode;
|
||||
use vxls_production::Producer;
|
||||
use vxls_production::ProducerId;
|
||||
use vxls_production::StructureAnchor;
|
||||
use vxls_production::StructureLocator;
|
||||
use vxls_production::request_queue::FrameId;
|
||||
use vxls_production::request_queue::IncomingRequest;
|
||||
use vxls_production::request_queue::QueueConfig;
|
||||
use vxls_production::request_queue::RequestKey;
|
||||
use vxls_production::request_queue::ReadyItem;
|
||||
use vxls_production::request_queue::RequestQueue;
|
||||
use vxls_production::request_queue::RequestTarget;
|
||||
use vxls_production::tree_locator::TreeLocator;
|
||||
use vxls_profiler::Frame;
|
||||
use vxls_profiler::Profile;
|
||||
use vxls_profiler::ProfilingEncoder;
|
||||
use vxls_voxel_cache::VoxelCache;
|
||||
use vxls_voxel_cache::VoxelCacheInsertion;
|
||||
use vxls_voxel_cache::insertion::CacheInsertionData;
|
||||
use vxls_voxel_cache::insertion::InsertionUploadError;
|
||||
use vxls_voxel_cache::requests::CacheRequestBuffer;
|
||||
use vxls_voxel_cache::schema::ChildInformation;
|
||||
use vxls_voxel_cache::schema::InsertionInformation;
|
||||
use vxls_voxel_cache::schema::LocationPage;
|
||||
|
||||
mod camera;
|
||||
mod egui_renderer;
|
||||
@@ -94,16 +95,6 @@ enum DisplayTexture {
|
||||
Albedo,
|
||||
}
|
||||
|
||||
struct VoxelPayload<const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
color: [u32; N * N * N],
|
||||
normal: [[f32; 3]; N * N * N],
|
||||
insertion_information: InsertionInformation<N>,
|
||||
location_page: LocationPage,
|
||||
}
|
||||
|
||||
struct State {
|
||||
instance: wgpu::Instance,
|
||||
window: Arc<Window>,
|
||||
@@ -128,9 +119,10 @@ struct State {
|
||||
gi_bg_layout: BindGroupLayout,
|
||||
|
||||
voxel_cache: Arc<Mutex<VoxelCache<4>>>,
|
||||
request_queue: Arc<Mutex<RequestQueue<VoxelPayload<4>>>>,
|
||||
request_queue: Arc<Mutex<RequestQueue<ProducedNode<4, VoxelAttributes>>>>,
|
||||
request_buffer: Arc<CacheRequestBuffer>,
|
||||
cache_insertion: Arc<CacheInsertionData<4>>,
|
||||
frame_id: FrameId,
|
||||
|
||||
structure_table: Arc<Mutex<BvhStructureTable>>,
|
||||
//structure_table: Arc<Mutex<FixedStructureTable>>,
|
||||
@@ -358,7 +350,9 @@ impl State {
|
||||
*/
|
||||
|
||||
// Load dragon ply
|
||||
let tree: NTree<4, Color> = load_ply("../../dragon.txt");
|
||||
//let tree: NTree<4, Color> = load_ply("../../dragon.txt");
|
||||
let mut file = File::open("../../dragon.vxls").unwrap();
|
||||
let tree: NTree<4, VoxelAttributes> = read_tree(&mut file, None).unwrap().tree;
|
||||
// let mut arrays = vec![Color(0., 0., 0., 0.); 256 * 256 * 256];
|
||||
// for ((x, y), z) in (0..256).cartesian_product(0..256).cartesian_product(0..256)
|
||||
// {
|
||||
@@ -420,12 +414,13 @@ impl State {
|
||||
let mut bvh_objs = Vec::new();
|
||||
for _ in 0..1000 {
|
||||
let transform = Mat4::from_rotation_translation(
|
||||
Quat::from_euler(
|
||||
glam::EulerRot::XYZ,
|
||||
random_range(-3.1..3.1),
|
||||
random_range(-3.1..3.1),
|
||||
random_range(-3.1..3.1),
|
||||
),
|
||||
// Quat::from_euler(
|
||||
// glam::EulerRot::XYZ,
|
||||
// random_range(-3.1..3.1),
|
||||
// random_range(-3.1..3.1),
|
||||
// random_range(-3.1..3.1),
|
||||
// ),
|
||||
Quat::IDENTITY,
|
||||
Vec3::new(
|
||||
random_range(extent.min().x..extent.max().x),
|
||||
random_range(extent.min().y..extent.max().y),
|
||||
@@ -729,10 +724,10 @@ impl State {
|
||||
});
|
||||
|
||||
let structure_table = Arc::new(Mutex::new(structure_table));
|
||||
let structure_table_1 = structure_table.clone();
|
||||
let request_queue = Arc::new(Mutex::new(RequestQueue::new(QueueConfig::default())));
|
||||
let request_queue_1 = request_queue.clone();
|
||||
let (wakeup_tx, wakeup_rx) = std::sync::mpsc::channel();
|
||||
let producer = TreeProducer::new(tree);
|
||||
std::thread::spawn(move || {
|
||||
loop {
|
||||
if !wakeup_rx.recv().is_ok() {
|
||||
@@ -755,30 +750,18 @@ impl State {
|
||||
break;
|
||||
}
|
||||
|
||||
let finshed = work
|
||||
let finished = work
|
||||
.par_iter()
|
||||
.map(|work_item| {
|
||||
// let d = fullfill_request(
|
||||
// &terrain_generator,
|
||||
// &work_item.key,
|
||||
// &structure_table_1,
|
||||
// );
|
||||
let d = fullfill_request_tree(&tree, &work_item.key);
|
||||
(
|
||||
work_item,
|
||||
VoxelPayload {
|
||||
color: d.0,
|
||||
normal: d.1,
|
||||
insertion_information: d.2,
|
||||
location_page: d.3,
|
||||
},
|
||||
)
|
||||
let payload = producer
|
||||
.produce(work_item.key.anchor, work_item.key.node_locator::<4>());
|
||||
(work_item, payload)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
{
|
||||
let mut locked = request_queue_1.lock();
|
||||
finshed.into_iter().for_each(|(work_item, payload)| {
|
||||
finished.into_iter().for_each(|(work_item, payload)| {
|
||||
locked.complete(*work_item, payload);
|
||||
});
|
||||
}
|
||||
@@ -827,6 +810,7 @@ impl State {
|
||||
request_queue,
|
||||
request_buffer: Arc::new(request_buffer),
|
||||
cache_insertion: Arc::new(insertion_data),
|
||||
frame_id: 0,
|
||||
|
||||
structure_table,
|
||||
structure_table_cache_insertion: Arc::new(structure_table_insertion_data),
|
||||
@@ -1034,6 +1018,7 @@ impl State {
|
||||
fn render(&mut self) {
|
||||
self.camera.update();
|
||||
|
||||
/*
|
||||
let mut locked_table = self.structure_table.lock();
|
||||
for (hndl, dir) in self.bvh_objs.iter_mut() {
|
||||
let obj = locked_table.get_mut(hndl);
|
||||
@@ -1053,6 +1038,7 @@ impl State {
|
||||
locked_table.mark_dirty();
|
||||
locked_table.rebuild_if_dirty(&self.device, &self.queue);
|
||||
drop(locked_table);
|
||||
*/
|
||||
|
||||
// Create texture view.
|
||||
// NOTE: We must handle Timeout because the surface may be unavailable
|
||||
@@ -1422,25 +1408,22 @@ impl State {
|
||||
.contains(&winit::keyboard::KeyCode::KeyF)
|
||||
{
|
||||
self.insertion_debounce = true;
|
||||
let frame_id: FrameId = self.voxel_cache.lock().current_timestamp().into();
|
||||
let cloned_queue = self.queue.clone();
|
||||
self.frame_id = self.frame_id.wrapping_add(1);
|
||||
let frame_id = self.frame_id;
|
||||
let finished = Arc::new(AtomicU32::new(0));
|
||||
|
||||
// Cache requests
|
||||
let cloned_insertion_data = self.cache_insertion.clone();
|
||||
// Cache requests. The callback only ingests; production and
|
||||
// uploads happen after both downloads have landed, so the main
|
||||
// thread can never encode an insertion from a half-updated batch.
|
||||
let request_buffer_done = Arc::new(AtomicBool::new(false));
|
||||
let request_buffer_done_1 = request_buffer_done.clone();
|
||||
let finshed_1 = finished.clone();
|
||||
let finished_1 = finished.clone();
|
||||
let request_queue_1 = self.request_queue.clone();
|
||||
self.request_buffer.host_download_requests(
|
||||
&self.device,
|
||||
&self.queue,
|
||||
move |request_informations, request_metas| {
|
||||
let done = request_buffer_done_1.load(std::sync::atomic::Ordering::Relaxed);
|
||||
if !done {
|
||||
request_buffer_done_1.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
finshed_1.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
} else {
|
||||
if request_buffer_done_1.swap(true, std::sync::atomic::Ordering::Relaxed) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1451,99 +1434,23 @@ impl State {
|
||||
.zip(request_metas.iter())
|
||||
.map(|(info, meta)| IncomingRequest::from_wire(info, meta)),
|
||||
);
|
||||
//println!("Structure table: {} requests", request_informations.len());
|
||||
|
||||
let drained =
|
||||
request_queue_1
|
||||
.lock()
|
||||
.drain_ready_matching(frame_id, 1024, |key| !key.is_root());
|
||||
|
||||
let mut insertion_infos = vec![];
|
||||
let mut color_attributes = vec![];
|
||||
let mut normal_attributes: Vec<[[f32; 3]; 64]> = vec![];
|
||||
let mut location_pages = vec![];
|
||||
let mut insertion_parents = vec![];
|
||||
|
||||
drained.into_iter().for_each(|payload| {
|
||||
insertion_infos.push(payload.payload.insertion_information);
|
||||
color_attributes.push(payload.payload.color);
|
||||
normal_attributes.push(payload.payload.normal);
|
||||
location_pages.push(payload.payload.location_page);
|
||||
insertion_parents.push(payload.insertion_parent());
|
||||
});
|
||||
|
||||
let request_count = request_informations.len() as u32;
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
cloned_insertion_data.insertion_information_buffer(),
|
||||
0,
|
||||
bytemuck::cast_slice(&insertion_infos),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
cloned_insertion_data.location_pages_buffer(),
|
||||
0,
|
||||
bytemuck::cast_slice(&location_pages),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
cloned_insertion_data.insertion_parents_buffer(),
|
||||
0,
|
||||
bytemuck::cast_slice(&insertion_parents),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
&cloned_insertion_data.attributes_buffers()[0],
|
||||
0,
|
||||
bytemuck::cast_slice(&color_attributes),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
&cloned_insertion_data.attributes_buffers()[1],
|
||||
0,
|
||||
bytemuck::cast_slice(&normal_attributes),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
cloned_insertion_data.destination_indices_buffer(),
|
||||
0,
|
||||
bytemuck::cast_slice(&vec![u32::MAX; request_count as usize]),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
cloned_insertion_data.valid_count_buffer(),
|
||||
0,
|
||||
bytemuck::bytes_of(&0u32),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
cloned_insertion_data.page_count_buffer(),
|
||||
0,
|
||||
bytemuck::bytes_of(&request_count),
|
||||
);
|
||||
finished_1.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
},
|
||||
);
|
||||
|
||||
let cloned_queue = self.queue.clone();
|
||||
|
||||
// Structure table requests
|
||||
let cloned_st_insertion_data = self.structure_table_cache_insertion.clone();
|
||||
// Structure table requests.
|
||||
let structure_table_request_buffer_done = Arc::new(AtomicBool::new(false));
|
||||
let structure_table_request_buffer_done_1 = structure_table_request_buffer_done.clone();
|
||||
let finshed_2 = finished.clone();
|
||||
let cloned_table_2 = self.structure_table.clone();
|
||||
let finished_2 = finished.clone();
|
||||
let request_queue_2 = self.request_queue.clone();
|
||||
self.structure_table_request_buffer.host_download_requests(
|
||||
&self.device,
|
||||
&self.queue,
|
||||
move |request_informations, request_metas| {
|
||||
let done = structure_table_request_buffer_done_1
|
||||
.load(std::sync::atomic::Ordering::Relaxed);
|
||||
if !done {
|
||||
structure_table_request_buffer_done_1
|
||||
.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
finshed_2.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
} else {
|
||||
if structure_table_request_buffer_done_1
|
||||
.swap(true, std::sync::atomic::Ordering::Relaxed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1555,76 +1462,7 @@ impl State {
|
||||
.map(|(info, meta)| IncomingRequest::from_wire(info, meta)),
|
||||
);
|
||||
|
||||
let drained =
|
||||
request_queue_2
|
||||
.lock()
|
||||
.drain_ready_matching(frame_id, 128, |key| key.is_root());
|
||||
|
||||
//println!("Structure table: {} requests", request_informations.len());
|
||||
|
||||
let mut insertion_infos = vec![];
|
||||
let mut color_attributes = vec![];
|
||||
let mut normal_attributes: Vec<[[f32; 3]; 64]> = vec![];
|
||||
let mut location_pages = vec![];
|
||||
let mut insertion_parents = vec![];
|
||||
|
||||
drained.into_iter().for_each(|payload| {
|
||||
insertion_infos.push(payload.payload.insertion_information);
|
||||
color_attributes.push(payload.payload.color);
|
||||
normal_attributes.push(payload.payload.normal);
|
||||
location_pages.push(payload.payload.location_page);
|
||||
insertion_parents.push(payload.insertion_parent());
|
||||
});
|
||||
|
||||
let request_count = request_informations.len() as u32;
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
cloned_st_insertion_data.insertion_information_buffer(),
|
||||
0,
|
||||
bytemuck::cast_slice(&insertion_infos),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
cloned_st_insertion_data.location_pages_buffer(),
|
||||
0,
|
||||
bytemuck::cast_slice(&location_pages),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
cloned_st_insertion_data.insertion_parents_buffer(),
|
||||
0,
|
||||
bytemuck::cast_slice(&insertion_parents),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
&cloned_st_insertion_data.attributes_buffers()[0],
|
||||
0,
|
||||
bytemuck::cast_slice(&color_attributes),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
&cloned_st_insertion_data.attributes_buffers()[1],
|
||||
0,
|
||||
bytemuck::cast_slice(&normal_attributes),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
cloned_st_insertion_data.destination_indices_buffer(),
|
||||
0,
|
||||
bytemuck::cast_slice(&vec![u32::MAX; request_count as usize]),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
cloned_st_insertion_data.valid_count_buffer(),
|
||||
0,
|
||||
bytemuck::bytes_of(&0u32),
|
||||
);
|
||||
|
||||
cloned_queue.write_buffer(
|
||||
cloned_st_insertion_data.page_count_buffer(),
|
||||
0,
|
||||
bytemuck::bytes_of(&request_count),
|
||||
);
|
||||
finished_2.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1637,6 +1475,26 @@ impl State {
|
||||
let _ = self.device.poll(wgpu::wgt::PollType::Poll);
|
||||
}
|
||||
|
||||
let children = self.request_queue.lock().drain_ready_matching(
|
||||
frame_id,
|
||||
self.cache_insertion.capacity(),
|
||||
|key| !key.is_root(),
|
||||
);
|
||||
let roots = self.request_queue.lock().drain_ready_matching(
|
||||
frame_id,
|
||||
self.structure_table_cache_insertion.capacity(),
|
||||
|key| key.is_root(),
|
||||
);
|
||||
|
||||
if let Err(error) = upload_ready(&self.queue, &self.cache_insertion, children) {
|
||||
eprintln!("cache insertion upload failed: {error}");
|
||||
}
|
||||
if let Err(error) =
|
||||
upload_ready(&self.queue, &self.structure_table_cache_insertion, roots)
|
||||
{
|
||||
eprintln!("structure table insertion upload failed: {error}");
|
||||
}
|
||||
|
||||
let mut encoder = self.device.create_command_encoder(&Default::default());
|
||||
|
||||
let mut scoped_encoder = ProfilingEncoder::new(&self.device, &mut encoder);
|
||||
@@ -1780,100 +1638,6 @@ pub struct ChunkImmediate {
|
||||
frame_timestamp: u32,
|
||||
}
|
||||
|
||||
fn fullfill_request_tree<const N: usize>(
|
||||
tree: &NTree<N, Color>,
|
||||
request: &RequestKey,
|
||||
) -> (
|
||||
[u32; N * N * N],
|
||||
[[f32; 3]; N * N * N],
|
||||
InsertionInformation<N>,
|
||||
LocationPage,
|
||||
) {
|
||||
match request.target {
|
||||
RequestTarget::Root => {
|
||||
let root = tree
|
||||
.get_by_loc(TreeLocator::root())
|
||||
.expect("Root must exist");
|
||||
|
||||
if !root.subdivided {
|
||||
// Produce little node of constant
|
||||
(
|
||||
[root.attribute.as_packed_u32(); N * N * N],
|
||||
//[sparse_tree::Color(1., 1., 1., 1.).as_packed_u32(); N * N * N],
|
||||
[Default::default(); N * N * N],
|
||||
InsertionInformation {
|
||||
children: [ChildInformation::new(false, root.attribute.3 != 0.); N * N * N],
|
||||
},
|
||||
LocationPage {
|
||||
node_id: StructureLocator::from(TreeLocator::<N>::root()),
|
||||
producer_id: ProducerId(0),
|
||||
anchor: StructureAnchor::zeroed(),
|
||||
},
|
||||
)
|
||||
} else {
|
||||
let mut colors = vec![];
|
||||
let mut children = vec![];
|
||||
TreeLocator::root().children().for_each(|loc| {
|
||||
let node = tree.get_by_loc(loc).unwrap();
|
||||
colors.push(node.attribute.as_packed_u32());
|
||||
children.push(ChildInformation::new(
|
||||
node.subdivided,
|
||||
node.subdivided || node.attribute.3 != 0.,
|
||||
));
|
||||
});
|
||||
|
||||
(
|
||||
std::array::from_fn(|i| colors[i]),
|
||||
[Default::default(); N * N * N],
|
||||
InsertionInformation {
|
||||
children: std::array::from_fn(|i| children[i]),
|
||||
},
|
||||
LocationPage {
|
||||
node_id: StructureLocator::from(TreeLocator::<N>::root()),
|
||||
producer_id: ProducerId(0),
|
||||
anchor: StructureAnchor::zeroed(),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
RequestTarget::Child {
|
||||
parent,
|
||||
children_index,
|
||||
} => {
|
||||
let child_z = children_index / (4 * 4);
|
||||
let child_y = (children_index % (4 * 4)) / 4;
|
||||
let child_x = children_index % 4;
|
||||
|
||||
let loc: TreeLocator<N> = parent.into();
|
||||
let loc = loc.child(child_x, child_y, child_z);
|
||||
|
||||
let mut colors = vec![];
|
||||
let mut children = vec![];
|
||||
loc.children().for_each(|child_loc| {
|
||||
let node = tree.get_by_loc(child_loc).unwrap();
|
||||
colors.push(node.attribute.as_packed_u32());
|
||||
children.push(ChildInformation::new(
|
||||
node.subdivided,
|
||||
node.subdivided || node.attribute.3 != 0.,
|
||||
));
|
||||
});
|
||||
|
||||
(
|
||||
std::array::from_fn(|i| colors[i]),
|
||||
[Default::default(); N * N * N],
|
||||
InsertionInformation {
|
||||
children: std::array::from_fn(|i| children[i]),
|
||||
},
|
||||
LocationPage {
|
||||
node_id: StructureLocator::from(loc),
|
||||
producer_id: ProducerId(0),
|
||||
anchor: StructureAnchor::zeroed(),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
fn fullfill_request<const N: usize>(
|
||||
generator: &dyn ChunkedProducer<N>,
|
||||
@@ -1973,3 +1737,21 @@ where
|
||||
)
|
||||
}
|
||||
*/
|
||||
|
||||
/// Stages produced nodes into one insertion batch and uploads it.
|
||||
fn upload_ready<const N: usize>(
|
||||
queue: &wgpu::Queue,
|
||||
insertion: &CacheInsertionData<N>,
|
||||
ready: Vec<ReadyItem<ProducedNode<N, VoxelAttributes>>>,
|
||||
) -> Result<usize, InsertionUploadError>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
let entries: Vec<_> = ready.iter().map(|item| item.insertion_entry()).collect();
|
||||
let voxels: Vec<VoxelAttributes> = ready
|
||||
.iter()
|
||||
.flat_map(|item| item.payload.attributes.iter().copied())
|
||||
.collect();
|
||||
|
||||
insertion.upload(queue, &entries, &voxels)
|
||||
}
|
||||
|
||||
@@ -11,8 +11,16 @@ use noise::NoiseFn;
|
||||
use noise::Simplex;
|
||||
use rand::random;
|
||||
|
||||
use vxls_production::ProducedNode;
|
||||
use vxls_production::Producer as VoxelProducer;
|
||||
use vxls_production::StructureAnchor;
|
||||
use vxls_production::StructureLocator;
|
||||
use vxls_production::sparse_tree::Color;
|
||||
use vxls_production::sparse_tree::ColorByte;
|
||||
use vxls_production::sparse_tree::NTree;
|
||||
use vxls_production::tree_locator::TreeLocator;
|
||||
use vxls_voxel_cache::schema::ChildInformation;
|
||||
use vxls_voxel_cache::schema::InsertionInformation;
|
||||
|
||||
/// The voxel attributes used by the demo cache.
|
||||
///
|
||||
@@ -646,3 +654,75 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Produces cache pages from an in-memory sparse tree.
|
||||
///
|
||||
/// The node to produce is named by the request's `node_locator`, so the
|
||||
/// producer never needs to know whether the cache asked for a structure root
|
||||
/// or an internal child.
|
||||
pub struct TreeProducer<const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
tree: NTree<N, VoxelAttributes>,
|
||||
}
|
||||
|
||||
impl<const N: usize> TreeProducer<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pub fn new(tree: NTree<N, VoxelAttributes>) -> Self {
|
||||
Self { tree }
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> VoxelProducer<N, VoxelAttributes> for TreeProducer<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
fn produce(
|
||||
&self,
|
||||
_anchor: StructureAnchor,
|
||||
locator: StructureLocator,
|
||||
) -> ProducedNode<N, VoxelAttributes> {
|
||||
let locator = TreeLocator::<N>::from(locator);
|
||||
let node = self
|
||||
.tree
|
||||
.get_by_loc(locator)
|
||||
.expect("requested node is not in the source tree");
|
||||
|
||||
let mut children = [ChildInformation::NONE; N * N * N];
|
||||
let mut attributes = [VoxelAttributes::default(); N * N * N];
|
||||
|
||||
if node.subdivided {
|
||||
for (index, child_locator) in locator.children().enumerate() {
|
||||
let child = self
|
||||
.tree
|
||||
.get_by_loc(child_locator)
|
||||
.expect("subdivided node is missing a child");
|
||||
|
||||
children[index] = ChildInformation::new(
|
||||
child.subdivided,
|
||||
child.subdivided || ColorByte(child.attribute.color).as_color().3 != 0.,
|
||||
);
|
||||
attributes[index] = VoxelAttributes {
|
||||
color: child.attribute.color,
|
||||
normal: child.attribute.normal,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
let color = node.attribute.color;
|
||||
let occupied = ColorByte(node.attribute.color).as_color().3 != 0.;
|
||||
|
||||
for index in 0..N * N * N {
|
||||
children[index] = ChildInformation::new(false, occupied);
|
||||
attributes[index] = VoxelAttributes {
|
||||
color,
|
||||
normal: node.attribute.normal,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
ProducedNode::new(InsertionInformation { children }, attributes)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user