mirror of
https://github.com/bevyengine/bevy
synced 2025-01-12 21:29:03 +00:00
b47217bfab
This upstreams the code changes used by the new renderer to enable cross-app Entity reuse: * Spawning at specific entities * get_or_spawn: spawns an entity if it doesn't already exist and returns an EntityMut * insert_or_spawn_batch: the batched equivalent to `world.get_or_spawn(entity).insert_bundle(bundle)` * Clearing entities and storages * Allocating Entities with "invalid" archetypes. These entities cannot be queried / are treated as "non existent". They serve as "reserved" entities that won't show up when calling `spawn()`. They must be "specifically spawned at" using apis like `get_or_spawn(entity)`. In combination, these changes enable the "render world" to clear entities / storages each frame and reserve all "app world entities". These can then be spawned during the "render extract step". This refactors "spawn" and "insert" code in a way that I think is a massive improvement to legibility and re-usability. It also yields marginal performance wins by reducing some duplicate lookups (less than a percentage point improvement on insertion benchmarks). There is also some potential for future unsafe reduction (by making BatchSpawner and BatchInserter generic). But for now I want to cut down generic usage to a minimum to encourage smaller binaries and faster compiles. This is currently a draft because it needs more tests (although this code has already had some real-world testing on my custom-shaders branch). I also fixed the benchmarks (which currently don't compile!) / added new ones to illustrate batching wins. After these changes, Bevy ECS is basically ready to accommodate the new renderer. I think the biggest missing piece at this point is "sub apps".
84 lines
1.8 KiB
Rust
84 lines
1.8 KiB
Rust
use crate::{
|
|
bundle::{Bundle, BundleSpawner},
|
|
entity::Entity,
|
|
world::World,
|
|
};
|
|
|
|
pub struct SpawnBatchIter<'w, I>
|
|
where
|
|
I: Iterator,
|
|
I::Item: Bundle,
|
|
{
|
|
inner: I,
|
|
spawner: BundleSpawner<'w, 'w>,
|
|
}
|
|
|
|
impl<'w, I> SpawnBatchIter<'w, I>
|
|
where
|
|
I: Iterator,
|
|
I::Item: Bundle,
|
|
{
|
|
#[inline]
|
|
pub(crate) fn new(world: &'w mut World, iter: I) -> Self {
|
|
// Ensure all entity allocations are accounted for so `self.entities` can realloc if
|
|
// necessary
|
|
world.flush();
|
|
|
|
let (lower, upper) = iter.size_hint();
|
|
let length = upper.unwrap_or(lower);
|
|
|
|
let bundle_info = world.bundles.init_info::<I::Item>(&mut world.components);
|
|
world.entities.reserve(length as u32);
|
|
let mut spawner = bundle_info.get_bundle_spawner(
|
|
&mut world.entities,
|
|
&mut world.archetypes,
|
|
&mut world.components,
|
|
&mut world.storages,
|
|
*world.change_tick.get_mut(),
|
|
);
|
|
spawner.reserve_storage(length);
|
|
|
|
Self {
|
|
inner: iter,
|
|
spawner,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<I> Drop for SpawnBatchIter<'_, I>
|
|
where
|
|
I: Iterator,
|
|
I::Item: Bundle,
|
|
{
|
|
fn drop(&mut self) {
|
|
for _ in self {}
|
|
}
|
|
}
|
|
|
|
impl<I> Iterator for SpawnBatchIter<'_, I>
|
|
where
|
|
I: Iterator,
|
|
I::Item: Bundle,
|
|
{
|
|
type Item = Entity;
|
|
|
|
fn next(&mut self) -> Option<Entity> {
|
|
let bundle = self.inner.next()?;
|
|
// SAFE: bundle matches spawner type
|
|
unsafe { Some(self.spawner.spawn(bundle)) }
|
|
}
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
self.inner.size_hint()
|
|
}
|
|
}
|
|
|
|
impl<I, T> ExactSizeIterator for SpawnBatchIter<'_, I>
|
|
where
|
|
I: ExactSizeIterator<Item = T>,
|
|
T: Bundle,
|
|
{
|
|
fn len(&self) -> usize {
|
|
self.inner.len()
|
|
}
|
|
}
|