Cleanup: Use Parallel in extract_meshes (#12084)

# 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.
This commit is contained in:
James Liu 2024-02-25 11:06:54 -08:00 committed by GitHub
parent a7be8a2655
commit fd91c61d72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 19 deletions

View File

@ -39,7 +39,6 @@ fixedbitset = "0.4"
bytemuck = { version = "1", features = ["derive"] } bytemuck = { version = "1", features = ["derive"] }
radsort = "0.1" radsort = "0.1"
smallvec = "1.6" smallvec = "1.6"
thread_local = "1.0"
[lints] [lints]
workspace = true workspace = true

View File

@ -26,9 +26,7 @@ use bevy_render::{
Extract, Extract,
}; };
use bevy_transform::components::GlobalTransform; use bevy_transform::components::GlobalTransform;
use bevy_utils::{tracing::error, Entry, HashMap, Hashed}; use bevy_utils::{tracing::error, Entry, HashMap, Hashed, Parallel};
use std::cell::Cell;
use thread_local::ThreadLocal;
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
use bevy_utils::warn_once; use bevy_utils::warn_once;
@ -258,7 +256,7 @@ pub struct RenderMeshInstances(EntityHashMap<RenderMeshInstance>);
pub fn extract_meshes( pub fn extract_meshes(
mut render_mesh_instances: ResMut<RenderMeshInstances>, 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< meshes_query: Extract<
Query<( Query<(
Entity, Entity,
@ -306,25 +304,24 @@ pub fn extract_meshes(
previous_transform: (&previous_transform).into(), previous_transform: (&previous_transform).into(),
flags: flags.bits(), flags: flags.bits(),
}; };
let tls = thread_local_queues.get_or_default(); thread_local_queues.scope(|queue| {
let mut queue = tls.take(); queue.push((
queue.push(( entity,
entity, RenderMeshInstance {
RenderMeshInstance { mesh_asset_id: handle.id(),
mesh_asset_id: handle.id(), transforms,
transforms, shadow_caster: !not_shadow_caster,
shadow_caster: !not_shadow_caster, material_bind_group_id: AtomicMaterialBindGroupId::default(),
material_bind_group_id: AtomicMaterialBindGroupId::default(), automatic_batching: !no_automatic_batching,
automatic_batching: !no_automatic_batching, },
}, ));
)); });
tls.set(queue);
}, },
); );
render_mesh_instances.clear(); render_mesh_instances.clear();
for queue in thread_local_queues.iter_mut() { for queue in thread_local_queues.iter_mut() {
render_mesh_instances.extend(queue.get_mut().drain(..)); render_mesh_instances.extend(queue.drain(..));
} }
} }