Skip to content

Commit

Permalink
Merge branch 'main' into cubic-splines-overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnTheCoolingFan authored Jan 31, 2024
2 parents adf9faf + afa7b5c commit 52697c4
Show file tree
Hide file tree
Showing 142 changed files with 5,364 additions and 2,610 deletions.
10 changes: 8 additions & 2 deletions .cargo/config_fast_builds.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ linker = "clang"
rustflags = [
"-Clink-arg=-fuse-ld=lld", # Use LLD Linker
"-Zshare-generics=y", # (Nightly) Make the current crate share its generic instantiations
"-Zthreads=0", # (Nightly) Use improved multithreading with the recommended amount of threads.
]

# NOTE: you must install [Mach-O LLD Port](https://lld.llvm.org/MachO/index.html) on mac. you can easily do this by installing llvm which includes lld with the "brew" package manager:
Expand All @@ -16,17 +17,22 @@ rustflags = [
rustflags = [
"-Clink-arg=-fuse-ld=/usr/local/opt/llvm/bin/ld64.lld", # Use LLD Linker
"-Zshare-generics=y", # (Nightly) Make the current crate share its generic instantiations
"-Zthreads=0", # (Nightly) Use improved multithreading with the recommended amount of threads.
]

[target.aarch64-apple-darwin]
rustflags = [
"-Clink-arg=-fuse-ld=/opt/homebrew/opt/llvm/bin/ld64.lld", # Use LLD Linker
"-Zshare-generics=y", # (Nightly) Make the current crate share its generic instantiations
"-Zthreads=0", # (Nightly) Use improved multithreading with the recommended amount of threads.
]

[target.x86_64-pc-windows-msvc]
linker = "rust-lld.exe" # Use LLD Linker
rustflags = ["-Zshare-generics=n"]
linker = "rust-lld.exe" # Use LLD Linker
rustflags = [
"-Zshare-generics=n",
"-Zthreads=0", # (Nightly) Use improved multithreading with the recommended amount of threads.
]

# Optional: Uncommenting the following improves compile times, but reduces the amount of debug info to 'line number tables only'
# In most cases the gains are negligible, but if you are on macos and have slow compile times you should see significant gains.
Expand Down
16 changes: 15 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ match_same_arms = "warn"
semicolon_if_nothing_returned = "warn"
map_flatten = "warn"

[workspace.lints.rust]
unsafe_op_in_unsafe_fn = "warn"

[lints]
workspace = true

Expand Down Expand Up @@ -399,7 +402,7 @@ doc-scrape-examples = true

[package.metadata.example.2d_shapes]
name = "2D Shapes"
description = "Renders a rectangle, circle, and hexagon"
description = "Renders simple 2D primitive shapes like circles and polygons"
category = "2D Rendering"
wasm = true

Expand Down Expand Up @@ -1399,6 +1402,17 @@ description = "Illustrates event creation, activation, and reception"
category = "ECS (Entity Component System)"
wasm = false

[[example]]
name = "send_and_receive_events"
path = "examples/ecs/send_and_receive_events.rs"
doc-scrape-examples = true

[package.metadata.example.send_and_receive_events]
name = "Send and receive events"
description = "Demonstrates how to send and receive events of the same type in a single system"
category = "ECS (Entity Component System)"
wasm = false

[[example]]
name = "fixed_timestep"
path = "examples/ecs/fixed_timestep.rs"
Expand Down
3 changes: 3 additions & 0 deletions assets/data/asset_no_extension
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CustomAsset (
value: 13
)
38 changes: 22 additions & 16 deletions crates/bevy_animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![warn(missing_docs)]

use std::ops::Deref;
use std::ops::{Add, Deref, Mul};
use std::time::Duration;

use bevy_app::{App, Plugin, PostUpdate};
Expand Down Expand Up @@ -672,16 +672,22 @@ fn get_keyframe(target_count: usize, keyframes: &[f32], key_index: usize) -> &[f
&keyframes[start..end]
}

// Helper macro for cubic spline interpolation
// it needs to work on `f32`, `Vec3` and `Quat`
// TODO: replace by a function if the proper trait bounds can be figured out
macro_rules! cubic_spline_interpolation {
($value_start: expr, $tangent_out_start: expr, $tangent_in_end: expr, $value_end: expr, $lerp: expr, $step_duration: expr,) => {
$value_start * (2.0 * $lerp.powi(3) - 3.0 * $lerp.powi(2) + 1.0)
+ $tangent_out_start * ($step_duration) * ($lerp.powi(3) - 2.0 * $lerp.powi(2) + $lerp)
+ $value_end * (-2.0 * $lerp.powi(3) + 3.0 * $lerp.powi(2))
+ $tangent_in_end * ($step_duration) * ($lerp.powi(3) - $lerp.powi(2))
};
/// Helper function for cubic spline interpolation.
fn cubic_spline_interpolation<T>(
value_start: T,
tangent_out_start: T,
tangent_in_end: T,
value_end: T,
lerp: f32,
step_duration: f32,
) -> T
where
T: Mul<f32, Output = T> + Add<Output = T>,
{
value_start * (2.0 * lerp.powi(3) - 3.0 * lerp.powi(2) + 1.0)
+ tangent_out_start * (step_duration) * (lerp.powi(3) - 2.0 * lerp.powi(2) + lerp)
+ value_end * (-2.0 * lerp.powi(3) + 3.0 * lerp.powi(2))
+ tangent_in_end * step_duration * (lerp.powi(3) - lerp.powi(2))
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -828,7 +834,7 @@ fn apply_keyframe(
let tangent_out_start = keyframes[step_start * 3 + 2];
let tangent_in_end = keyframes[(step_start + 1) * 3];
let value_end = keyframes[(step_start + 1) * 3 + 1];
let result = cubic_spline_interpolation!(
let result = cubic_spline_interpolation(
value_start,
tangent_out_start,
tangent_in_end,
Expand All @@ -852,7 +858,7 @@ fn apply_keyframe(
let tangent_out_start = keyframes[step_start * 3 + 2];
let tangent_in_end = keyframes[(step_start + 1) * 3];
let value_end = keyframes[(step_start + 1) * 3 + 1];
let result = cubic_spline_interpolation!(
let result = cubic_spline_interpolation(
value_start,
tangent_out_start,
tangent_in_end,
Expand All @@ -876,7 +882,7 @@ fn apply_keyframe(
let tangent_out_start = keyframes[step_start * 3 + 2];
let tangent_in_end = keyframes[(step_start + 1) * 3];
let value_end = keyframes[(step_start + 1) * 3 + 1];
let result = cubic_spline_interpolation!(
let result = cubic_spline_interpolation(
value_start,
tangent_out_start,
tangent_in_end,
Expand Down Expand Up @@ -918,8 +924,8 @@ fn apply_keyframe(
.zip(tangents_in_end)
.zip(morph_end)
.map(
|(((value_start, tangent_out_start), tangent_in_end), value_end)| {
cubic_spline_interpolation!(
|(((&value_start, &tangent_out_start), &tangent_in_end), &value_end)| {
cubic_spline_interpolation(
value_start,
tangent_out_start,
tangent_in_end,
Expand Down
5 changes: 1 addition & 4 deletions crates/bevy_asset/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,7 @@ impl AsyncRead for VecReader {
/// Appends `.meta` to the given path.
pub(crate) fn get_meta_path(path: &Path) -> PathBuf {
let mut meta_path = path.to_path_buf();
let mut extension = path
.extension()
.unwrap_or_else(|| panic!("missing extension for asset path {path:?}"))
.to_os_string();
let mut extension = path.extension().unwrap_or_default().to_os_string();
extension.push(".meta");
meta_path.set_extension(extension);
meta_path
Expand Down
10 changes: 6 additions & 4 deletions crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ pub trait AssetLoader: Send + Sync + 'static {
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>>;

/// Returns a list of extensions supported by this asset loader, without the preceding dot.
fn extensions(&self) -> &[&str];
/// Returns a list of extensions supported by this [`AssetLoader`], without the preceding dot.
fn extensions(&self) -> &[&str] {
&[]
}
}

/// Provides type-erased access to an [`AssetLoader`].
Expand Down Expand Up @@ -396,7 +398,7 @@ impl<'a> LoadContext<'a> {
/// See [`AssetPath`] for more on labeled assets.
pub fn has_labeled_asset<'b>(&self, label: impl Into<CowArc<'b, str>>) -> bool {
let path = self.asset_path.clone().with_label(label.into());
self.asset_server.get_handle_untyped(&path).is_some()
!self.asset_server.get_handles_untyped(&path).is_empty()
}

/// "Finishes" this context by populating the final [`Asset`] value (and the erased [`AssetMeta`] value, if it exists).
Expand Down Expand Up @@ -546,7 +548,7 @@ impl<'a> LoadContext<'a> {
let loaded_asset = {
let (meta, loader, mut reader) = self
.asset_server
.get_meta_loader_and_reader(&path)
.get_meta_loader_and_reader(&path, None)
.await
.map_err(to_error)?;
self.asset_server
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl ReflectAsset {
handle: UntypedHandle,
) -> Option<&'w mut dyn Reflect> {
// SAFETY: requirements are deferred to the caller
(self.get_unchecked_mut)(world, handle)
unsafe { (self.get_unchecked_mut)(world, handle) }
}

/// Equivalent of [`Assets::add`]
Expand Down
36 changes: 24 additions & 12 deletions crates/bevy_asset/src/saver.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{io::Writer, meta::Settings, Asset, ErasedLoadedAsset};
use crate::{AssetLoader, LabeledAsset};
use crate::{AssetLoader, LabeledAsset, UntypedHandle};
use bevy_utils::{BoxedFuture, CowArc, HashMap};
use serde::{Deserialize, Serialize};
use std::ops::Deref;
use std::{borrow::Borrow, hash::Hash, ops::Deref};

/// Saves an [`Asset`] of a given [`AssetSaver::Asset`] type. [`AssetSaver::OutputLoader`] will then be used to load the saved asset
/// in the final deployed application. The saver should produce asset bytes in a format that [`AssetSaver::OutputLoader`] can read.
Expand Down Expand Up @@ -95,11 +95,12 @@ impl<'a, A: Asset> SavedAsset<'a, A> {
}

/// Returns the labeled asset, if it exists and matches this type.
pub fn get_labeled<B: Asset>(
&self,
label: impl Into<CowArc<'static, str>>,
) -> Option<SavedAsset<B>> {
let labeled = self.labeled_assets.get(&label.into())?;
pub fn get_labeled<B: Asset, Q>(&self, label: &Q) -> Option<SavedAsset<B>>
where
CowArc<'static, str>: Borrow<Q>,
Q: ?Sized + Hash + Eq,
{
let labeled = self.labeled_assets.get(label)?;
let value = labeled.asset.value.downcast_ref::<B>()?;
Some(SavedAsset {
value,
Expand All @@ -108,14 +109,25 @@ impl<'a, A: Asset> SavedAsset<'a, A> {
}

/// Returns the type-erased labeled asset, if it exists and matches this type.
pub fn get_erased_labeled(
&self,
label: impl Into<CowArc<'static, str>>,
) -> Option<&ErasedLoadedAsset> {
let labeled = self.labeled_assets.get(&label.into())?;
pub fn get_erased_labeled<Q>(&self, label: &Q) -> Option<&ErasedLoadedAsset>
where
CowArc<'static, str>: Borrow<Q>,
Q: ?Sized + Hash + Eq,
{
let labeled = self.labeled_assets.get(label)?;
Some(&labeled.asset)
}

/// Returns the [`UntypedHandle`] of the labeled asset with the provided 'label', if it exists.
pub fn get_untyped_handle<Q>(&self, label: &Q) -> Option<&UntypedHandle>
where
CowArc<'static, str>: Borrow<Q>,
Q: ?Sized + Hash + Eq,
{
let labeled = self.labeled_assets.get(label)?;
Some(&labeled.handle)
}

/// Iterate over all labels for "labeled assets" in the loaded asset
pub fn iter_labels(&self) -> impl Iterator<Item = &str> {
self.labeled_assets.keys().map(|s| &**s)
Expand Down
Loading

0 comments on commit 52697c4

Please sign in to comment.