Skip to content

Commit 03e299b

Browse files
brianreavismockersf
authored andcommitted
Fix NonMesh draw command item queries (#17893)
# Objective This fixes `NonMesh` draw commands not receiving render-world entities since - #17698 This unbreaks item queries for queued non-mesh entities: ```rust struct MyDrawCommand { type ItemQuery = Read<DynamicUniformIndex<SomeUniform>>; // ... } ``` ### Solution Pass render entity to `NonMesh` draw commands instead of `Entity::PLACEHOLDER`. This PR also introduces sorting of the `NonMesh` bin keys like other types, which I assume is the intended behavior. @pcwalton ## Testing - Tested on a local project that extensively uses `NonMesh` items.
1 parent 94eeebb commit 03e299b

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

crates/bevy_render/src/batching/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ where
187187
phase.multidrawable_meshes.sort_unstable_keys();
188188
phase.batchable_meshes.sort_unstable_keys();
189189
phase.unbatchable_meshes.sort_unstable_keys();
190+
phase.non_mesh_items.sort_unstable_keys();
190191
}
191192
}
192193

crates/bevy_render/src/render_phase/mod.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ where
129129
/// entity are simply called in order at rendering time.
130130
///
131131
/// See the `custom_phase_item` example for an example of how to use this.
132-
pub non_mesh_items: IndexMap<(BPI::BatchSetKey, BPI::BinKey), RenderBin>,
132+
pub non_mesh_items: IndexMap<(BPI::BatchSetKey, BPI::BinKey), NonMeshEntities>,
133133

134134
/// Information on each batch set.
135135
///
@@ -322,6 +322,12 @@ pub struct UnbatchableBinnedEntities {
322322
pub(crate) buffer_indices: UnbatchableBinnedEntityIndexSet,
323323
}
324324

325+
/// Information about [`BinnedRenderPhaseType::NonMesh`] entities.
326+
pub struct NonMeshEntities {
327+
/// The entities.
328+
pub entities: MainEntityHashMap<Entity>,
329+
}
330+
325331
/// Stores instance indices and dynamic offsets for unbatchable entities in a
326332
/// binned render phase.
327333
///
@@ -381,8 +387,6 @@ pub enum BinnedRenderPhaseType {
381387

382388
/// The item is a mesh that's eligible for indirect rendering, but can't be
383389
/// batched with other meshes of the same type.
384-
///
385-
/// At the moment, this is used for skinned meshes.
386390
UnbatchableMesh,
387391

388392
/// The item isn't a mesh at all.
@@ -526,10 +530,12 @@ where
526530
.entry((batch_set_key.clone(), bin_key.clone()).clone())
527531
{
528532
indexmap::map::Entry::Occupied(mut entry) => {
529-
entry.get_mut().insert(main_entity, input_uniform_index);
533+
entry.get_mut().entities.insert(main_entity, entity);
530534
}
531535
indexmap::map::Entry::Vacant(entry) => {
532-
entry.insert(RenderBin::from_entity(main_entity, input_uniform_index));
536+
let mut entities = MainEntityHashMap::default();
537+
entities.insert(main_entity, entity);
538+
entry.insert(NonMeshEntities { entities });
533539
}
534540
}
535541
}
@@ -795,14 +801,14 @@ where
795801
let draw_functions = world.resource::<DrawFunctions<BPI>>();
796802
let mut draw_functions = draw_functions.write();
797803

798-
for ((batch_set_key, bin_key), bin) in &self.non_mesh_items {
799-
for &entity in bin.entities.keys() {
804+
for ((batch_set_key, bin_key), non_mesh_entities) in &self.non_mesh_items {
805+
for (main_entity, entity) in non_mesh_entities.entities.iter() {
800806
// Come up with a fake batch range and extra index. The draw
801807
// function is expected to manage any sort of batching logic itself.
802808
let binned_phase_item = BPI::new(
803809
batch_set_key.clone(),
804810
bin_key.clone(),
805-
(Entity::PLACEHOLDER, entity),
811+
(*entity, *main_entity),
806812
0..1,
807813
PhaseItemExtraIndex::None,
808814
);
@@ -921,7 +927,7 @@ fn remove_entity_from_bin<BPI>(
921927
multidrawable_meshes: &mut IndexMap<BPI::BatchSetKey, IndexMap<BPI::BinKey, RenderBin>>,
922928
batchable_meshes: &mut IndexMap<(BPI::BatchSetKey, BPI::BinKey), RenderBin>,
923929
unbatchable_meshes: &mut IndexMap<(BPI::BatchSetKey, BPI::BinKey), UnbatchableBinnedEntities>,
924-
non_mesh_items: &mut IndexMap<(BPI::BatchSetKey, BPI::BinKey), RenderBin>,
930+
non_mesh_items: &mut IndexMap<(BPI::BatchSetKey, BPI::BinKey), NonMeshEntities>,
925931
) where
926932
BPI: BinnedPhaseItem,
927933
{
@@ -984,10 +990,10 @@ fn remove_entity_from_bin<BPI>(
984990
entity_bin_key.batch_set_key.clone(),
985991
entity_bin_key.bin_key.clone(),
986992
)) {
987-
bin_entry.get_mut().remove(entity);
993+
bin_entry.get_mut().entities.remove(&entity);
988994

989995
// If the bin is now empty, remove the bin.
990-
if bin_entry.get_mut().is_empty() {
996+
if bin_entry.get_mut().entities.is_empty() {
991997
bin_entry.swap_remove();
992998
}
993999
}

0 commit comments

Comments
 (0)