Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need some way to nest labeled assets #18010

Open
pcwalton opened this issue Feb 24, 2025 · 4 comments · May be fixed by #18213
Open

Need some way to nest labeled assets #18010

pcwalton opened this issue Feb 24, 2025 · 4 comments · May be fixed by #18213
Assignees
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior P-Regression Functionality that used to work but no longer does. Add a test for this! S-Needs-Design This issue requires design work to think about how it would best be accomplished

Comments

@pcwalton
Copy link
Contributor

Bevy version

0.16-dev after #15481

Overview

If you have an asset that loads nested glTFs, for example my bevy-glxf-loader, then if you have more than one glTF in that asset the names like Scene0, Mesh0, etc. will collide. What I'd like is to be able to namespace those loaded assets with a prefix of some kind.

@pcwalton pcwalton added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Feb 24, 2025
@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Feb 24, 2025
@andriyDev andriyDev self-assigned this Feb 24, 2025
@andriyDev
Copy link
Contributor

I took a stab at fixing this but looks like it's not so trivial. Or rather, there's a deeper bug lurking. Turns out add_loaded_labeled_asset is just kinda broken. When you do an immediate load of an asset, it assigns the path of the asset you are nested-loading. So if you are loading asset "a.txt", and within you load "b.ron", subassets of the second asset will have a path of "b.ron#subasset1", rather than "a.txt#subasset1". This means the path "a.txt#subasset1" perpetually is in the NotLoaded state.

I think we need to propagate the path into these subassets. We treat the "root" subasset of "b.ron" as a regular subasset of "a.txt", so we need to somehow set the path of its subassets to be "a.txt".

@alice-i-cecile alice-i-cecile added A-Assets Load files from disk to use for things like images, models, and sounds S-Needs-Design This issue requires design work to think about how it would best be accomplished P-Regression Functionality that used to work but no longer does. Add a test for this! and removed S-Needs-Triage This issue needs to be labelled labels Feb 24, 2025
github-merge-queue bot pushed a commit that referenced this issue Feb 24, 2025
…ate label. (#18013)

# Objective

- Makes #18010 more easily debuggable. This doesn't solve that issue,
but protects us from it in the future.

## Solution

- Make `LoadContext::add_labeled_asset` and friends return an error if
it finds a duplicate asset.

## Testing

- Added a test - it fails before the fix.

---

## Migration Guide

- `AssetLoader`s must now handle the case of a duplicate subasset label
when using `LoadContext::add_labeled_asset` and its variants. If you
know your subasset labels are unique by construction (e.g., they include
an index number), you can simply unwrap this result.
@andriyDev
Copy link
Contributor

Ok I've figured out more of the problem. Here's a diagram I came up with:

root loader tries to load "root#nested/subasset"
v
root -> nested -> subasset
          ^
nested loader finds handle for "nested#subasset" (or generates it) and stores it in nested

Once root calls add_loaded_labeled_asset, it registers all "nested" subassets as subassets of root - but now with the path "root#nested/subasset" (which is not what is stored in nested)

In essence, the handles we store in the "nested" asset (in the nested loader) correspond to a different path than "root". That means when we look up the handles of assets in "nested", those assets will never get loaded, since they were loaded as subassets of "root".

I don't understand yet how #15481 broke this. It seems to me like this problem should have been just the same in that situation...

@andriyDev
Copy link
Contributor

Now I think I understand the full scope of the issue. Nested asset loading is weird...

(Also to be clear, this is all addressing that "deeper bug" I mentioned above).

The problem is that before #15481, LoadedAsset had two functions:

  1. It was the type you get from LoadContext::finish for a subasset in the current loader.
  2. It was the type you get from NestedLoader<StaticTyped, Immediate>::load.

This means LoadedAsset::labeled_assets could store two kinds of assets:

  1. Subassets of subassets.
  2. Subassets of a dependency that you did a nested load for.

#15481 (my PR) assumed we were only holding the first kind of asset. However this is a problem for the second kind of asset. The nested asset loaded the asset assuming the path was for the nested asset. That means the handles are all related to the path "nested#subasset_name". After #15481, when we merge the labeled_assets of a CompleteLoadedAsset coming from a nested load, we don't give them a new handle related to the root asset. So even though we treat it as a subasset (requiring uniqueness), we can't access it like a subasset ("root#subasset_name").

We could prevent registering nested-loaded assets as subassets, but then any handles you get from that nested load are useless. Unless we register these nested-loaded assets as such in LoadContext. This would make these nested-loaded assets no longer subassets, but correspond to their "real" counterparts. I think this is the path I am going to explore.

Nested asset loading is very weird. It's not just parsing bytes - it can load another actual asset. I think we need to take a more general look and redesign nested asset loading. I have a feeling we can consolidate a lot of the loading stuff. But I digress.

@andriyDev
Copy link
Contributor

There's another issue creeping up that I'm not sure how to handle. AFAICT, nested-loading an asset path with a label is broken. Unless I'm missing something, it will give you the root asset when you requested the labeled asset. Unfortunately, we're in a bind to fix this. If we properly return the labeled asset, we won't know which subassets are relevant to that subasset (since we remove that information when we merge subassets of subassets). So any handles in that subasset can't be found, so we can't package them up into CompleteLoadedAsset, and we can't register them in the root loader. Before #15481, this was more likely to work correctly (since we could register subassets to their "parent" subasset), though it still wasn't guaranteed.

One immediate solution: we could just panic if you pass a label when doing a nested asset load. At least this would prevent erroneous requests.

@alice-i-cecile alice-i-cecile removed this from the 0.16 milestone Mar 9, 2025
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 C-Bug An unexpected or incorrect behavior P-Regression Functionality that used to work but no longer does. Add a test for this! S-Needs-Design This issue requires design work to think about how it would best be accomplished
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants