-
Notifications
You must be signed in to change notification settings - Fork 250
/
mesh_cache.rs
78 lines (67 loc) · 2.42 KB
/
mesh_cache.rs
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
78
use super::scene::MeshSourceData;
use crate::mesh_loader::{CpuMesh, GpuMesh};
use re_log_types::MeshFormat;
use std::sync::Arc;
// ----------------------------------------------------------------------------
#[derive(Default)]
pub struct CpuMeshCache(nohash_hasher::IntMap<u64, Option<Arc<CpuMesh>>>);
impl CpuMeshCache {
pub fn load(
&mut self,
mesh_id: u64,
name: &str,
mesh_data: &MeshSourceData,
) -> Option<Arc<CpuMesh>> {
crate::profile_function!();
self.0
.entry(mesh_id)
.or_insert_with(|| {
re_log::debug!("Loading CPU mesh {name:?}…");
let result = match mesh_data {
MeshSourceData::Mesh3D(mesh3d) => CpuMesh::load(name.to_owned(), mesh3d),
MeshSourceData::StaticGlb(glb_bytes) => {
CpuMesh::load_raw(name.to_owned(), MeshFormat::Glb, glb_bytes)
}
};
match result {
Ok(cpu_mesh) => Some(Arc::new(cpu_mesh)),
Err(err) => {
re_log::warn!("Failed to load mesh {name:?}: {}", re_error::format(&err));
None
}
}
})
.clone()
}
}
// ----------------------------------------------------------------------------
#[derive(Default)]
pub struct GpuMeshCache(nohash_hasher::IntMap<u64, Option<GpuMesh>>);
impl GpuMeshCache {
pub fn load(&mut self, three_d: &three_d::Context, mesh_id: u64, cpu_mesh: &CpuMesh) {
crate::profile_function!();
self.0
.entry(mesh_id)
.or_insert_with(|| match cpu_mesh.to_gpu(three_d) {
Ok(gpu_mesh) => Some(gpu_mesh),
Err(err) => {
re_log::warn!(
"Failed to load mesh {:?}: {}",
cpu_mesh.name(),
re_error::format(&err)
);
None
}
});
}
pub fn set_instances(&mut self, mesh_id: u64, instances: &three_d::Instances) {
if let Some(Some(gpu_mesh)) = self.0.get_mut(&mesh_id) {
for model in &mut gpu_mesh.meshes {
model.set_instances(instances);
}
}
}
pub fn get(&self, mesh_id: u64) -> Option<&GpuMesh> {
self.0.get(&mesh_id)?.as_ref()
}
}