Skip to content

Commit

Permalink
Cleanup: Use Parallel in extract_meshes (#12084)
Browse files Browse the repository at this point in the history
# Objective
#7348 added `bevy_utils::Parallel` and replaced the usage of the
`ThreadLocal<Cell<Vec<...>>>` in `check_visibility`, but we were also
using it in `extract_meshes`.

## Solution
Refactor the system to use `Parallel` instead.
  • Loading branch information
james7132 authored Feb 25, 2024
1 parent a7be8a2 commit fd91c61
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
1 change: 0 additions & 1 deletion crates/bevy_pbr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ fixedbitset = "0.4"
bytemuck = { version = "1", features = ["derive"] }
radsort = "0.1"
smallvec = "1.6"
thread_local = "1.0"

[lints]
workspace = true
33 changes: 15 additions & 18 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ use bevy_render::{
Extract,
};
use bevy_transform::components::GlobalTransform;
use bevy_utils::{tracing::error, Entry, HashMap, Hashed};
use std::cell::Cell;
use thread_local::ThreadLocal;
use bevy_utils::{tracing::error, Entry, HashMap, Hashed, Parallel};

#[cfg(debug_assertions)]
use bevy_utils::warn_once;
Expand Down Expand Up @@ -258,7 +256,7 @@ pub struct RenderMeshInstances(EntityHashMap<RenderMeshInstance>);

pub fn extract_meshes(
mut render_mesh_instances: ResMut<RenderMeshInstances>,
mut thread_local_queues: Local<ThreadLocal<Cell<Vec<(Entity, RenderMeshInstance)>>>>,
mut thread_local_queues: Local<Parallel<Vec<(Entity, RenderMeshInstance)>>>,
meshes_query: Extract<
Query<(
Entity,
Expand Down Expand Up @@ -306,25 +304,24 @@ pub fn extract_meshes(
previous_transform: (&previous_transform).into(),
flags: flags.bits(),
};
let tls = thread_local_queues.get_or_default();
let mut queue = tls.take();
queue.push((
entity,
RenderMeshInstance {
mesh_asset_id: handle.id(),
transforms,
shadow_caster: !not_shadow_caster,
material_bind_group_id: AtomicMaterialBindGroupId::default(),
automatic_batching: !no_automatic_batching,
},
));
tls.set(queue);
thread_local_queues.scope(|queue| {
queue.push((
entity,
RenderMeshInstance {
mesh_asset_id: handle.id(),
transforms,
shadow_caster: !not_shadow_caster,
material_bind_group_id: AtomicMaterialBindGroupId::default(),
automatic_batching: !no_automatic_batching,
},
));
});
},
);

render_mesh_instances.clear();
for queue in thread_local_queues.iter_mut() {
render_mesh_instances.extend(queue.get_mut().drain(..));
render_mesh_instances.extend(queue.drain(..));
}
}

Expand Down

0 comments on commit fd91c61

Please sign in to comment.