Skip to content

Commit

Permalink
Merge branch 'bevyengine:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Litttlefish authored May 24, 2024
2 parents 8ed5353 + ec01c2d commit 93b089f
Show file tree
Hide file tree
Showing 16 changed files with 2,163 additions and 66 deletions.
24 changes: 24 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ futures-lite = "2.0.1"
crossbeam-channel = "0.5.0"
argh = "0.1.12"
thiserror = "1.0"
event-listener = "5.3.0"

[[example]]
name = "hello_world"
Expand Down Expand Up @@ -429,6 +430,17 @@ description = "Renders a 2d mesh"
category = "2D Rendering"
wasm = true

[[example]]
name = "mesh2d_arcs"
path = "examples/2d/mesh2d_arcs.rs"
doc-scrape-examples = true

[package.metadata.example.mesh2d_arcs]
name = "Arc 2D Meshes"
description = "Demonstrates UV-mapping of the circular segment and sector primitives"
category = "2D Rendering"
wasm = true

[[example]]
name = "mesh2d_manual"
path = "examples/2d/mesh2d_manual.rs"
Expand Down Expand Up @@ -1420,6 +1432,18 @@ description = "How to configure the texture to repeat instead of the default cla
category = "Assets"
wasm = true

# Assets
[[example]]
name = "multi_asset_sync"
path = "examples/asset/multi_asset_sync.rs"
doc-scrape-examples = true

[package.metadata.example.multi_asset_sync]
name = "Mult-asset synchronization"
description = "Demonstrates how to wait for multiple assets to be loaded."
category = "Assets"
wasm = true

# Async Tasks
[[example]]
name = "async_compute"
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/loader_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<'ctx, 'builder> NestedLoader<'ctx, 'builder> {
let handle = if self.load_context.should_load_dependencies {
self.load_context
.asset_server
.load_with_meta_transform(path, self.meta_transform)
.load_with_meta_transform(path, self.meta_transform, ())
} else {
self.load_context
.asset_server
Expand Down
51 changes: 48 additions & 3 deletions crates/bevy_asset/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,31 @@ impl AssetServer {
/// The asset load will fail and an error will be printed to the logs if the asset stored at `path` is not of type `A`.
#[must_use = "not using the returned strong handle may result in the unexpected release of the asset"]
pub fn load<'a, A: Asset>(&self, path: impl Into<AssetPath<'a>>) -> Handle<A> {
self.load_with_meta_transform(path, None)
self.load_with_meta_transform(path, None, ())
}

/// Begins loading an [`Asset`] of type `A` stored at `path` while holding a guard item.
/// The guard item is dropped when either the asset is loaded or loading has failed.
///
/// This function returns a "strong" [`Handle`]. When the [`Asset`] is loaded (and enters [`LoadState::Loaded`]), it will be added to the
/// associated [`Assets`] resource.
///
/// The guard item should notify the caller in its [`Drop`] implementation. See example `multi_asset_sync`.
/// Synchronously this can be a [`Arc<AtomicU32>`] that decrements its counter, asynchronously this can be a `Barrier`.
/// This function only guarantees the asset referenced by the [`Handle`] is loaded. If your asset is separated into
/// multiple files, sub-assets referenced by the main asset might still be loading, depend on the implementation of the [`AssetLoader`].
///
/// Additionally, you can check the asset's load state by reading [`AssetEvent`] events, calling [`AssetServer::load_state`], or checking
/// the [`Assets`] storage to see if the [`Asset`] exists yet.
///
/// The asset load will fail and an error will be printed to the logs if the asset stored at `path` is not of type `A`.
#[must_use = "not using the returned strong handle may result in the unexpected release of the asset"]
pub fn load_acquire<'a, A: Asset, G: Send + Sync + 'static>(
&self,
path: impl Into<AssetPath<'a>>,
guard: G,
) -> Handle<A> {
self.load_with_meta_transform(path, None, guard)
}

/// Begins loading an [`Asset`] of type `A` stored at `path`. The given `settings` function will override the asset's
Expand All @@ -282,13 +306,33 @@ impl AssetServer {
path: impl Into<AssetPath<'a>>,
settings: impl Fn(&mut S) + Send + Sync + 'static,
) -> Handle<A> {
self.load_with_meta_transform(path, Some(loader_settings_meta_transform(settings)))
self.load_with_meta_transform(path, Some(loader_settings_meta_transform(settings)), ())
}

/// Begins loading an [`Asset`] of type `A` stored at `path` while holding a guard item.
/// The guard item is dropped when either the asset is loaded or loading has failed.
///
/// This function only guarantees the asset referenced by the [`Handle`] is loaded. If your asset is separated into
/// multiple files, sub-assets referenced by the main asset might still be loading, depend on the implementation of the [`AssetLoader`].
///
/// The given `settings` function will override the asset's
/// [`AssetLoader`] settings. The type `S` _must_ match the configured [`AssetLoader::Settings`] or `settings` changes
/// will be ignored and an error will be printed to the log.
#[must_use = "not using the returned strong handle may result in the unexpected release of the asset"]
pub fn load_acquire_with_settings<'a, A: Asset, S: Settings, G: Send + Sync + 'static>(
&self,
path: impl Into<AssetPath<'a>>,
settings: impl Fn(&mut S) + Send + Sync + 'static,
guard: G,
) -> Handle<A> {
self.load_with_meta_transform(path, Some(loader_settings_meta_transform(settings)), guard)
}

pub(crate) fn load_with_meta_transform<'a, A: Asset>(
pub(crate) fn load_with_meta_transform<'a, A: Asset, G: Send + Sync + 'static>(
&self,
path: impl Into<AssetPath<'a>>,
meta_transform: Option<MetaTransform>,
guard: G,
) -> Handle<A> {
let path = path.into().into_owned();
let (handle, should_load) = self.data.infos.write().get_or_create_path_handle::<A>(
Expand All @@ -305,6 +349,7 @@ impl AssetServer {
if let Err(err) = server.load_internal(owned_handle, path, false, None).await {
error!("{}", err);
}
drop(guard);
})
.detach();
}
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ approx = { version = "0.5", optional = true }
rand = { version = "0.8", features = [
"alloc",
], default-features = false, optional = true }
smallvec = { version = "1.11" }

[dev-dependencies]
approx = "0.5"
Expand All @@ -26,6 +27,8 @@ rand = "0.8"
rand_chacha = "0.3"
# Enable the approx feature when testing.
bevy_math = { path = ".", version = "0.14.0-dev", features = ["approx"] }
glam = { version = "0.27", features = ["approx"] }


[features]
default = ["rand"]
Expand Down
Loading

0 comments on commit 93b089f

Please sign in to comment.