Skip to content

Asset Value Templates via asset_value() and HandleTemplate::Value#23839

Merged
alice-i-cecile merged 3 commits intobevyengine:mainfrom
cart:asset_value_template
Apr 20, 2026
Merged

Asset Value Templates via asset_value() and HandleTemplate::Value#23839
alice-i-cecile merged 3 commits intobevyengine:mainfrom
cart:asset_value_template

Conversation

@cart
Copy link
Copy Markdown
Member

@cart cart commented Apr 17, 2026

Objective

Temporary simple / suboptimal solution to #23822

It would be very nice to be able to define assets inline in BSN.

Solution

  • Add a new HandleTemplate::Value variant, which contains an Arc<Mutex<AssetOrHandle<T>>>
  • This template, when first applied, will lock the mutex, insert the asset value as a new asset, cache the handle in the Arc<Mutex<HandleOrTemplate> and return the handle. Future calls will lock the mutex and return the cached handle.
  • Add asset_value(SOME_ASSET) function to improve ergonomics.
  • Port 3d_scene to illustrate

Doing a lock on every spawn is obviously suboptimal. The long term plan for "inline assets in BSN" is defined in #23822 and will not use this locking approach.

@cart cart added A-Assets Load files from disk to use for things like images, models, and sounds C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Scenes Composing and serializing ECS objects labels Apr 17, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in Assets Apr 17, 2026
@cart cart marked this pull request as draft April 17, 2026 01:08
@alice-i-cecile alice-i-cecile added the S-Needs-Benchmarking This set of changes needs performance benchmarking to double-check that they help label Apr 17, 2026
@cart cart force-pushed the asset_value_template branch from 1f09306 to 15cdea8 Compare April 17, 2026 01:09
Comment thread crates/bevy_asset/src/handle.rs
Comment thread crates/bevy_asset/src/handle.rs
Comment thread crates/bevy_asset/src/handle.rs
@andriyDev
Copy link
Copy Markdown
Contributor

Keep in mind our AssetServer is also a Mutex, so this would be equivalent to a load I think? Which is like.... Not a big deal IMO? Or if it is we have bigger problems lol.

In fact, this should be even faster since the asset server is likely under more contention than each asset template.

@cart
Copy link
Copy Markdown
Member Author

cart commented Apr 17, 2026

In fact, this should be even faster since the asset server is likely under more contention than each asset template.

The difference here is that in an ideal world, we hit the asset server once to retrieve the handle, then clone that directly rather than asking the asset server again. A non-BSN equivalent would be much faster, as it could just cache the handle somewhere. These are only on par when you spawn something exactly once.

@cart
Copy link
Copy Markdown
Member Author

cart commented Apr 17, 2026

Just benchmarked spawning 100 scene instances each containing 10 asset handles (using scene inheritance to reuse the ResolvedScene):

image

The difference is present, but not to a degree that we shouldn't land this. I'll push the benchmark and do the doc work!

@cart cart force-pushed the asset_value_template branch from 15cdea8 to 06a5fba Compare April 17, 2026 23:45
@cart cart marked this pull request as ready for review April 17, 2026 23:51
@cart cart added this to the 0.19 milestone Apr 17, 2026
@cart cart removed the S-Needs-Benchmarking This set of changes needs performance benchmarking to double-check that they help label Apr 17, 2026
@alice-i-cecile alice-i-cecile added X-Blessed Has a large architectural impact or tradeoffs, but the design has been endorsed by decision makers D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 18, 2026
app.world_mut().spawn_scene(ui_loaded_asset()).unwrap();
});

drop(handle);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this drop does nothing.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 20, 2026
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Apr 20, 2026
Merged via the queue into bevyengine:main with commit 6e9522c Apr 20, 2026
46 checks passed
@github-project-automation github-project-automation Bot moved this from Needs SME Triage to Done in Assets Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Assets Load files from disk to use for things like images, models, and sounds A-Scenes Composing and serializing ECS objects C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Blessed Has a large architectural impact or tradeoffs, but the design has been endorsed by decision makers

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants