-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't keep around additional cpu copy of loaded mesh files (#7824)
### What This is mainly a refactor of `re_renderer`'s model loading pipeline, but as the title (== changelog entry) points out, a nice side-effect that arose culling unnecessary memory usage which may be important for large meshes. @EtaLoop's attempt to add a color option to `Asset3D` (see #7458) made it clear that the output of the mesh loaders is really hard to work with: Prior to this PR, they would eagerly create gpu-sided meshes and then store them alongside an optional (but always filled-out) "cpu mesh" (in essence the unpacked model file). Now instead all model loading goes to a intermediate `CpuModel` which can be rather easily post processed. Gpu resources are then created as a separate step, consuming the `CpuModel` (it should be trivial to create a variant that doesn't consume if we need this in the future) ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) * [x] I have tested the web demo (if applicable): * Using examples from latest `main` build: [rerun.io/viewer](https://rerun.io/viewer/pr/7824?manifest_url=https://app.rerun.io/version/main/examples_manifest.json) * Using full set of examples from `nightly` build: [rerun.io/viewer](https://rerun.io/viewer/pr/7824?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json) * [x] The PR title and labels are set such as to maximize their usefulness for the next release's CHANGELOG * [x] If applicable, add a new check to the [release checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)! * [x] If have noted any breaking changes to the log API in `CHANGELOG.md` and the migration guide - [PR Build Summary](https://build.rerun.io/pr/7824) - [Recent benchmark results](https://build.rerun.io/graphs/crates.html) - [Wasm size tracking](https://build.rerun.io/graphs/sizes.html) To run all checks from `main`, comment on the PR with `@rerun-bot full-check`.
- Loading branch information
Showing
18 changed files
with
252 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use std::sync::Arc; | ||
|
||
use slotmap::{SecondaryMap, SlotMap}; | ||
|
||
use crate::{ | ||
mesh::{CpuMesh, GpuMesh, MeshError}, | ||
renderer::GpuMeshInstance, | ||
RenderContext, | ||
}; | ||
|
||
slotmap::new_key_type! { | ||
/// Key for identifying a cpu mesh in a model. | ||
pub struct CpuModelMeshKey; | ||
} | ||
|
||
/// Like [`GpuMeshInstance`], but for CPU sided usage in a [`CpuModel`] only. | ||
pub struct CpuMeshInstance { | ||
pub mesh: CpuModelMeshKey, | ||
pub world_from_mesh: glam::Affine3A, | ||
// TODO(andreas): Expose other properties we have on [`GpuMeshInstance`]. | ||
} | ||
|
||
/// A collection of meshes & mesh instances on the CPU. | ||
/// | ||
/// Note that there is currently no `GpuModel` equivalent, since | ||
/// [`GpuMeshInstance`]es use shared ownership of [`GpuMesh`]es. | ||
/// | ||
/// This is the output of a model loader and is ready to be converted into | ||
/// a series of [`GpuMeshInstance`]s that can be rendered. | ||
/// | ||
/// This is meant as a useful intermediate structure for doing post-processing steps on the model prior to gpu upload. | ||
#[derive(Default)] | ||
pub struct CpuModel { | ||
pub meshes: SlotMap<CpuModelMeshKey, CpuMesh>, | ||
pub instances: Vec<CpuMeshInstance>, | ||
} | ||
|
||
impl CpuModel { | ||
/// Creates a new [`CpuModel`] from a single [`CpuMesh`], creating a single instance with identity transform. | ||
pub fn from_single_mesh(mesh: CpuMesh) -> Self { | ||
let mut model = Self::default(); | ||
model.add_single_instance_mesh(mesh); | ||
model | ||
} | ||
|
||
/// Adds a new [`CpuMesh`] to the model, creating a single instance with identity transform. | ||
pub fn add_single_instance_mesh(&mut self, mesh: CpuMesh) { | ||
let mesh_key = self.meshes.insert(mesh); | ||
self.instances.push(CpuMeshInstance { | ||
mesh: mesh_key, | ||
world_from_mesh: glam::Affine3A::IDENTITY, | ||
}); | ||
} | ||
|
||
pub fn calculate_bounding_box(&self) -> re_math::BoundingBox { | ||
re_math::BoundingBox::from_points( | ||
self.instances | ||
.iter() | ||
.filter_map(|mesh_instance| { | ||
self.meshes.get(mesh_instance.mesh).map(|mesh| { | ||
mesh.vertex_positions | ||
.iter() | ||
.map(|p| mesh_instance.world_from_mesh.transform_point3(*p)) | ||
}) | ||
}) | ||
.flatten(), | ||
) | ||
} | ||
|
||
/// Converts the entire model into a serious of mesh instances that can be rendered. | ||
/// | ||
/// Silently ignores: | ||
/// * instances with invalid mesh keys | ||
/// * unreferenced meshes | ||
pub fn into_gpu_meshes(self, ctx: &RenderContext) -> Result<Vec<GpuMeshInstance>, MeshError> { | ||
let mut gpu_meshes = SecondaryMap::with_capacity(self.meshes.len()); | ||
for (mesh_key, mesh) in &self.meshes { | ||
gpu_meshes.insert(mesh_key, Arc::new(GpuMesh::new(ctx, mesh)?)); | ||
} | ||
|
||
Ok(self | ||
.instances | ||
.into_iter() | ||
.filter_map(|instance| { | ||
Some(GpuMeshInstance { | ||
gpu_mesh: gpu_meshes.get(instance.mesh)?.clone(), | ||
world_from_mesh: instance.world_from_mesh, | ||
additive_tint: Default::default(), | ||
outline_mask_ids: Default::default(), | ||
picking_layer_id: Default::default(), | ||
}) | ||
}) | ||
.collect()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.