mirror of
https://github.com/bevyengine/bevy
synced 2024-11-30 00:20:20 +00:00
01aedc8431
# Objective Now that we can consolidate Bundles and Components under a single insert (thanks to #2975 and #6039), almost 100% of world spawns now look like `world.spawn().insert((Some, Tuple, Here))`. Spawning an entity without any components is an extremely uncommon pattern, so it makes sense to give spawn the "first class" ergonomic api. This consolidated api should be made consistent across all spawn apis (such as World and Commands). ## Solution All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input: ```rust // before: commands .spawn() .insert((A, B, C)); world .spawn() .insert((A, B, C); // after commands.spawn((A, B, C)); world.spawn((A, B, C)); ``` All existing instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api. A new `spawn_empty` has been added, replacing the old `spawn` api. By allowing `world.spawn(some_bundle)` to replace `world.spawn().insert(some_bundle)`, this opened the door to removing the initial entity allocation in the "empty" archetype / table done in `spawn()` (and subsequent move to the actual archetype in `.insert(some_bundle)`). This improves spawn performance by over 10%: ![image](https://user-images.githubusercontent.com/2694663/191627587-4ab2f949-4ccd-4231-80eb-80dd4d9ad6b9.png) To take this measurement, I added a new `world_spawn` benchmark. Unfortunately, optimizing `Commands::spawn` is slightly less trivial, as Commands expose the Entity id of spawned entities prior to actually spawning. Doing the optimization would (naively) require assurances that the `spawn(some_bundle)` command is applied before all other commands involving the entity (which would not necessarily be true, if memory serves). Optimizing `Commands::spawn` this way does feel possible, but it will require careful thought (and maybe some additional checks), which deserves its own PR. For now, it has the same performance characteristics of the current `Commands::spawn_bundle` on main. **Note that 99% of this PR is simple renames and refactors. The only code that needs careful scrutiny is the new `World::spawn()` impl, which is relatively straightforward, but it has some new unsafe code (which re-uses battle tested BundlerSpawner code path).** --- ## Changelog - All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input - All instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api - World and Commands now have `spawn_empty()`, which is equivalent to the old `spawn()` behavior. ## Migration Guide ```rust // Old (0.8): commands .spawn() .insert_bundle((A, B, C)); // New (0.9) commands.spawn((A, B, C)); // Old (0.8): commands.spawn_bundle((A, B, C)); // New (0.9) commands.spawn((A, B, C)); // Old (0.8): let entity = commands.spawn().id(); // New (0.9) let entity = commands.spawn_empty().id(); // Old (0.8) let entity = world.spawn().id(); // New (0.9) let entity = world.spawn_empty(); ```
93 lines
3.1 KiB
Rust
93 lines
3.1 KiB
Rust
//! Implements a custom asset io loader.
|
|
//! An [`AssetIo`] is what the asset server uses to read the raw bytes of assets.
|
|
//! It does not know anything about the asset formats, only how to talk to the underlying storage.
|
|
|
|
use bevy::{
|
|
asset::{AssetIo, AssetIoError, Metadata},
|
|
prelude::*,
|
|
utils::BoxedFuture,
|
|
};
|
|
use std::path::{Path, PathBuf};
|
|
|
|
/// A custom asset io implementation that simply defers to the platform default
|
|
/// implementation.
|
|
///
|
|
/// This can be used as a starting point for developing a useful implementation
|
|
/// that can defer to the default when needed.
|
|
struct CustomAssetIo(Box<dyn AssetIo>);
|
|
|
|
impl AssetIo for CustomAssetIo {
|
|
fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<Vec<u8>, AssetIoError>> {
|
|
info!("load_path({:?})", path);
|
|
self.0.load_path(path)
|
|
}
|
|
|
|
fn read_directory(
|
|
&self,
|
|
path: &Path,
|
|
) -> Result<Box<dyn Iterator<Item = PathBuf>>, AssetIoError> {
|
|
info!("read_directory({:?})", path);
|
|
self.0.read_directory(path)
|
|
}
|
|
|
|
fn watch_path_for_changes(&self, path: &Path) -> Result<(), AssetIoError> {
|
|
info!("watch_path_for_changes({:?})", path);
|
|
self.0.watch_path_for_changes(path)
|
|
}
|
|
|
|
fn watch_for_changes(&self) -> Result<(), AssetIoError> {
|
|
info!("watch_for_changes()");
|
|
self.0.watch_for_changes()
|
|
}
|
|
|
|
fn get_metadata(&self, path: &Path) -> Result<Metadata, AssetIoError> {
|
|
info!("get_metadata({:?})", path);
|
|
self.0.get_metadata(path)
|
|
}
|
|
}
|
|
|
|
/// A plugin used to execute the override of the asset io
|
|
struct CustomAssetIoPlugin;
|
|
|
|
impl Plugin for CustomAssetIoPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
let asset_io = {
|
|
// the platform default asset io requires a reference to the app
|
|
// builder to find its configuration
|
|
|
|
let default_io = bevy::asset::create_platform_default_asset_io(app);
|
|
|
|
// create the custom asset io instance
|
|
|
|
CustomAssetIo(default_io)
|
|
};
|
|
|
|
// the asset server is constructed and added the resource manager
|
|
|
|
app.insert_resource(AssetServer::new(asset_io));
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins_with(DefaultPlugins, |group| {
|
|
// the custom asset io plugin must be inserted in-between the
|
|
// `CorePlugin' and `AssetPlugin`. It needs to be after the
|
|
// CorePlugin, so that the IO task pool has already been constructed.
|
|
// And it must be before the `AssetPlugin` so that the asset plugin
|
|
// doesn't create another instance of an asset server. In general,
|
|
// the AssetPlugin should still run so that other aspects of the
|
|
// asset system are initialized correctly.
|
|
group.add_before::<bevy::asset::AssetPlugin, _>(CustomAssetIoPlugin)
|
|
})
|
|
.add_startup_system(setup)
|
|
.run();
|
|
}
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
commands.spawn(Camera2dBundle::default());
|
|
commands.spawn(SpriteBundle {
|
|
texture: asset_server.load("branding/icon.png"),
|
|
..default()
|
|
});
|
|
}
|