Skip to content

Commit

Permalink
Parallelize meshlet bounds computation
Browse files Browse the repository at this point in the history
  • Loading branch information
JMS55 committed Nov 11, 2023
1 parent 1c38878 commit 90a6b49
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions crates/bevy_pbr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ naga_oil = "0.10"
smallvec = "1.6"
thread_local = "1.0"
serde = { version = "1", features = ["derive", "rc"] }
rayon = "1"
23 changes: 15 additions & 8 deletions crates/bevy_pbr/src/meshlet/from_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bevy_render::{
};
use bevy_utils::thiserror;
use meshopt::{build_meshlets, compute_meshlet_bounds_decoder, VertexDataAdapter};
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use std::{borrow::Cow, iter};

impl MeshletMesh {
Expand Down Expand Up @@ -38,21 +39,27 @@ impl MeshletMesh {

let mut meshopt_meshlets = build_meshlets(&indices, &vertices, 126, 64, 0.0);

// TODO: Parallelize
let meshlet_bounding_spheres = meshopt_meshlets
.iter()
.meshlets
.par_iter()
.map(|meshlet| meshopt::Meshlet {
vertices: &meshopt_meshlets.vertices[meshlet.vertex_offset as usize
..meshlet.vertex_offset as usize + meshlet.vertex_count as usize],
triangles: &meshopt_meshlets.triangles[meshlet.triangle_offset as usize
..meshlet.triangle_offset as usize + meshlet.triangle_count as usize * 3],
})
.map(|meshlet| {
let bounds = compute_meshlet_bounds_decoder(
compute_meshlet_bounds_decoder(
meshlet,
mesh.attribute(Mesh::ATTRIBUTE_POSITION)
.unwrap()
.as_float3()
.unwrap(),
);
MeshletBoundingSphere {
center: bounds.center.into(),
radius: bounds.radius,
}
)
})
.map(|bounds| MeshletBoundingSphere {
center: bounds.center.into(),
radius: bounds.radius,
})
.collect();

Expand Down

0 comments on commit 90a6b49

Please sign in to comment.