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

AssetSaver and AssetTransformer split #11260

Merged
merged 6 commits into from
Jan 26, 2024

Conversation

thepackett
Copy link
Contributor

@thepackett thepackett commented Jan 8, 2024

Objective

One of a few Bevy Asset improvements I would like to make: #11216.

Currently asset processing and asset saving are handled by the same trait, AssetSaver. This makes it difficult to reuse saving implementations and impossible to have a single "universal" saver for a given asset type.

Solution

This PR splits off the processing portion of AssetSaver into AssetTransformer, which is responsible for transforming assets. This change involves adding the LoadTransformAndSave processor, which utilizes the new API. The LoadAndSave still exists since it remains useful in situations where no "transformation" of the asset is done, such as when compressing assets.

Notes:

As an aside, Bikeshedding is welcome on the names. I'm not entirely convinced by AssetTransformer, which was chosen mostly because AssetProcessor is taken. Additionally, LoadTransformSave may be sufficient instead of LoadTransformAndSave.


Changelog

Added

  • AssetTransformer which is responsible for transforming Assets.
  • LoadTransformAndSave, a Process implementation.

Changed

  • Changed AssetSaver's responsibilities from processing and saving to just saving.
  • Updated asset_processing example to use new API.
  • Old asset .meta files regenerated with new processor.

@alice-i-cecile alice-i-cecile added A-Assets Load files from disk to use for things like images, models, and sounds C-Code-Quality A section of code that is hard to understand or change C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Jan 8, 2024
@thepackett
Copy link
Contributor Author

thepackett commented Jan 9, 2024

In updating the implementation of CompressedImageSaver after my change, I realized that I overlooked one of the uses of AssetSaver: compressing assets. This means that LoadAndSave is still useful in situations where there is no asset manipulation, so I have re-added it.

@JMS55
Copy link
Contributor

JMS55 commented Jan 9, 2024

Why not Load -> Transform (Image -> Image, but compressed, or maybe a separate asset type) -> Save?

@thepackett
Copy link
Contributor Author

Why not Load -> Transform (Image -> Image, but compressed, or maybe a separate asset type) -> Save?

The AssetTransformer trait as I implemented it is about transforming from one asset type to another (or to itself, with changes). Nothing is stopping a user from implementing a compressed asset that way (as its own asset type that you transform to), however I suspect for most use cases it would be undesirable. Most things like a compressed image don't have any uses as an Asset type. Usually the only "useful" representation is an array of bytes which can be later decompressed into a useful asset.

So to go with that implementation it would mean that a user would have to add useless asset types to their app, which wouldn't be ergonomic. Additionally, AssetSaver's job it to output the bytes of an asset that needs to be saved, so stuff like compression fits pretty naturally there.

@JMS55
Copy link
Contributor

JMS55 commented Jan 9, 2024

Is this ready to review, or needs more time?

@thepackett
Copy link
Contributor Author

Is this ready to review, or needs more time?

I think at this point it's ready to review. I can't think of anything else that needs to be done or added.

@JMS55 JMS55 self-requested a review January 12, 2024 07:21
@JMS55 JMS55 added this to the 0.13 milestone Jan 12, 2024
/// This uses [`LoadTransformAndSaveSettings`] to configure the processor.
///
/// [`Asset`]: crate::Asset
pub struct LoadTransformAndSave<
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we not have to constrain the loader type, or the transformer output here? Huh.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not following. Does:

LoadTransformAndSave<L: AssetLoader, T: AssetTransformer<AssetInput = L::Asset>, S: AssetSaver<Asset = T::AssetOutput>>

need further constraints?
L can be any AssetLoader, T can be any AssetTransformer that accepts L's output as an input, and S can by any AssetSaver that accepts T's output as an input.

Copy link
Contributor

Choose a reason for hiding this comment

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

After a second look I guess it makes sense. I'm a bit surprised we don't have to specify e.g. the AssetTransformer output type, but it does make sense as is.

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jan 26, 2024
@alice-i-cecile
Copy link
Member

This split makes substantially more sense to me, and the code quality is high. It's even used in an example :D

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 26, 2024
Merged via the queue into bevyengine:main with commit 76682fd Jan 26, 2024
23 checks passed
tjamaan pushed a commit to tjamaan/bevy that referenced this pull request Feb 6, 2024
# Objective
One of a few Bevy Asset improvements I would like to make: bevyengine#11216.

Currently asset processing and asset saving are handled by the same
trait, `AssetSaver`. This makes it difficult to reuse saving
implementations and impossible to have a single "universal" saver for a
given asset type.

## Solution
This PR splits off the processing portion of `AssetSaver` into
`AssetTransformer`, which is responsible for transforming assets. This
change involves adding the `LoadTransformAndSave` processor, which
utilizes the new API. The `LoadAndSave` still exists since it remains
useful in situations where no "transformation" of the asset is done,
such as when compressing assets.

## Notes:
As an aside, Bikeshedding is welcome on the names. I'm not entirely
convinced by `AssetTransformer`, which was chosen mostly because
`AssetProcessor` is taken. Additionally, `LoadTransformSave` may be
sufficient instead of `LoadTransformAndSave`.


---

## Changelog
### Added 
- `AssetTransformer` which is responsible for transforming Assets.
- `LoadTransformAndSave`, a `Process` implementation.
### Changed
- Changed `AssetSaver`'s responsibilities from processing and saving to
just saving.
- Updated `asset_processing` example to use new API.
- Old asset .meta files regenerated with new processor.
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-Code-Quality A section of code that is hard to understand or change C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants