Skip to content

[Merged by Bors] - Rework extract_meshes #4240

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

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
102 changes: 33 additions & 69 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,79 +116,43 @@ bitflags::bitflags! {

pub fn extract_meshes(
mut commands: Commands,
mut previous_caster_len: Local<usize>,
mut previous_not_caster_len: Local<usize>,
caster_query: Query<
(
Entity,
&ComputedVisibility,
&GlobalTransform,
&Handle<Mesh>,
Option<&NotShadowReceiver>,
),
Without<NotShadowCaster>,
>,
not_caster_query: Query<
(
Entity,
&ComputedVisibility,
&GlobalTransform,
&Handle<Mesh>,
Option<&NotShadowReceiver>,
),
With<NotShadowCaster>,
>,
mut prev_len_shadow_caster: Local<usize>,
mut prev_len_not_shadow_caster: Local<usize>,
meshes_query: Query<(
Entity,
&ComputedVisibility,
&GlobalTransform,
&Handle<Mesh>,
Option<Without<NotShadowReceiver>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Woah, what is this query syntax? We can do optional filters within the query parameters? What does that even mean?

Copy link
Member

Choose a reason for hiding this comment

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

If the component exists, it returns Some(component). Otherwise it returns None.

The particularly tricky bit here is that Without is being used in the fetch parameter of the query, and so you're getting a bool returned there.

Copy link
Contributor

Choose a reason for hiding this comment

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

So if it matches Without<NotShadowCaster> then it returns Some(true) and otherwise None?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just Option<&NotShadowCaster> and invert the logic?

Copy link
Member

Choose a reason for hiding this comment

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

I agree with that suggestion; I think this is probably clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A few things:

  1. Theoretically, Option<&NotShadowCaster> requires building a reference based on an offset and stuff, while Option<With<NotShadowCaster>> does not, it might have different performance characteristic.
  2. Minor, but the item type for Option<With<NotShadowCaster>> is Option<()> not Option<bool>
  3. I preferred Without<Not…> over With<Not…> because in the first implementation I got very confused by the negative on the variables.

Option<Without<NotShadowCaster>>,
)>,
) {
let mut caster_values = Vec::with_capacity(*previous_caster_len);
for (entity, computed_visibility, transform, handle, not_receiver) in caster_query.iter() {
if !computed_visibility.is_visible {
continue;
}
let transform = transform.compute_matrix();
caster_values.push((
entity,
(
handle.clone_weak(),
MeshUniform {
flags: if not_receiver.is_some() {
MeshFlags::empty().bits
} else {
MeshFlags::SHADOW_RECEIVER.bits
},
transform,
inverse_transpose_model: transform.inverse().transpose(),
},
),
));
}
*previous_caster_len = caster_values.len();
commands.insert_or_spawn_batch(caster_values);
let mut caster_commands = Vec::with_capacity(*prev_len_shadow_caster);
let mut not_caster_commands = Vec::with_capacity(*prev_len_not_shadow_caster);
let visible_meshes = meshes_query.iter().filter(|(_, vis, ..)| vis.is_visible);

let mut not_caster_values = Vec::with_capacity(*previous_not_caster_len);
for (entity, computed_visibility, transform, mesh, not_receiver) in not_caster_query.iter() {
if !computed_visibility.is_visible {
continue;
}
for (entity, _, transform, handle, is_receiver, is_caster) in visible_meshes {
let transform = transform.compute_matrix();
not_caster_values.push((
entity,
(
mesh.clone_weak(),
MeshUniform {
flags: if not_receiver.is_some() {
MeshFlags::empty().bits
} else {
MeshFlags::SHADOW_RECEIVER.bits
},
transform,
inverse_transpose_model: transform.inverse().transpose(),
},
NotShadowCaster,
),
));
let shadow_receiver_flags = if is_receiver.is_some() {
MeshFlags::SHADOW_RECEIVER.bits
} else {
MeshFlags::empty().bits
};
let uniform = MeshUniform {
flags: shadow_receiver_flags,
transform,
inverse_transpose_model: transform.inverse().transpose(),
};
if is_caster.is_some() {
caster_commands.push((entity, (handle.clone_weak(), uniform)));
} else {
not_caster_commands.push((entity, (handle.clone_weak(), uniform, NotShadowCaster)));
}
}
*previous_not_caster_len = not_caster_values.len();
commands.insert_or_spawn_batch(not_caster_values);
*prev_len_shadow_caster = caster_commands.len();
*prev_len_not_shadow_caster = not_caster_commands.len();
commands.insert_or_spawn_batch(caster_commands);
commands.insert_or_spawn_batch(not_caster_commands);
}

#[derive(Debug, Default)]
Expand Down