Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Change check_visibility to use thread-local queues instead of a channel #4663

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ bitflags = "1.2.1"
smallvec = { version = "1.6", features = ["union", "const_generics"] }
once_cell = "1.4.1" # TODO: replace once_cell with std equivalent if/when this lands: https://github.com/rust-lang/rfcs/pull/2788
downcast-rs = "1.2.0"
thread_local = "1.1"
thiserror = "1.0"
futures-lite = "1.4.0"
crossbeam-channel = "0.5.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was only needed for the parallel system. We don't need it anymore, so it's been removed.

anyhow = "1.0"
hex = "0.4.2"
hexasphere = "7.0.0"
Expand Down
46 changes: 30 additions & 16 deletions crates/bevy_render/src/view/visibility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;
use bevy_transform::components::GlobalTransform;
use bevy_transform::TransformSystem;
use std::cell::Cell;
use thread_local::ThreadLocal;

use crate::{
camera::{Camera, CameraProjection, OrthographicProjection, PerspectiveProjection, Projection},
Expand Down Expand Up @@ -148,22 +150,30 @@ pub fn update_frusta<T: Component + CameraProjection + Send + Sync + 'static>(
}

pub fn check_visibility(
mut thread_queues: Local<ThreadLocal<Cell<Vec<Entity>>>>,
mut view_query: Query<(&mut VisibleEntities, &Frustum, Option<&RenderLayers>), With<Camera>>,
mut visible_entity_query: Query<(
Entity,
&Visibility,
&mut ComputedVisibility,
Option<&RenderLayers>,
Option<&Aabb>,
Option<&NoFrustumCulling>,
Option<&GlobalTransform>,
mut visible_entity_query: ParamSet<(
Query<&mut ComputedVisibility>,
Query<(
Entity,
&Visibility,
&mut ComputedVisibility,
Option<&RenderLayers>,
Option<&Aabb>,
Option<&NoFrustumCulling>,
Option<&GlobalTransform>,
)>,
)>,
) {
// Reset the computed visibility to false
for mut computed_visibility in visible_entity_query.p0().iter_mut() {
computed_visibility.is_visible = false;
}

for (mut visible_entities, frustum, maybe_view_mask) in view_query.iter_mut() {
let view_mask = maybe_view_mask.copied().unwrap_or_default();
let (visible_entity_sender, visible_entity_receiver) = crossbeam_channel::unbounded();

visible_entity_query.par_for_each_mut(
visible_entities.entities.clear();
visible_entity_query.p1().par_for_each_mut(
1024,
|(
entity,
Expand All @@ -174,12 +184,10 @@ pub fn check_visibility(
maybe_no_frustum_culling,
maybe_transform,
)| {
// Reset visibility
computed_visibility.is_visible = false;

if !visibility.is_visible {
return;
}

let entity_mask = maybe_entity_mask.copied().unwrap_or_default();
if !view_mask.intersects(&entity_mask) {
return;
Expand All @@ -205,9 +213,15 @@ pub fn check_visibility(
}

computed_visibility.is_visible = true;
visible_entity_sender.send(entity).ok();
let cell = thread_queues.get_or_default();
let mut queue = cell.take();
queue.push(entity);
cell.set(queue);
},
);
visible_entities.entities = visible_entity_receiver.try_iter().collect();

for cell in thread_queues.iter_mut() {
visible_entities.entities.append(cell.get_mut());
}
}
}