# Objective
The `AssetReader` trait allows customizing the behavior of fetching
bytes for an `AssetPath`, and expects implementors to return `dyn
AsyncRead + AsyncSeek`. This gives implementors of `AssetLoader` great
flexibility to tightly integrate their asset loading behavior with the
asynchronous task system.
However, almost all implementors of `AssetLoader` don't use the async
functionality at all, and just call `AsyncReadExt::read_to_end(&mut
Vec<u8>)`. This is incredibly inefficient, as this method repeatedly
calls `poll_read` on the trait object, filling the vector 32 bytes at a
time. At my work we have assets that are hundreds of megabytes which
makes this a meaningful overhead.
## Solution
Turn the `Reader` type alias into an actual trait, with a provided
method `read_to_end`. This provided method should be more efficient than
the existing extension method, as the compiler will know the underlying
type of `Reader` when generating this function, which removes the
repeated dynamic dispatches and allows the compiler to make further
optimizations after inlining. Individual implementors are able to
override the provided implementation -- for simple asset readers that
just copy bytes from one buffer to another, this allows removing a large
amount of overhead from the provided implementation.
Now that `Reader` is an actual trait, I also improved the ergonomics for
implementing `AssetReader`. Currently, implementors are expected to box
their reader and return it as a trait object, which adds unnecessary
boilerplate to implementations. This PR changes that trait method to
return a pseudo trait alias, which allows implementors to return `impl
Reader` instead of `Box<dyn Reader>`. Now, the boilerplate for boxing
occurs in `ErasedAssetReader`.
## Testing
I made identical changes to my company's fork of bevy. Our app, which
makes heavy use of `read_to_end` for asset loading, still worked
properly after this. I am not aware if we have a more systematic way of
testing asset loading for correctness.
---
## Migration Guide
The trait method `bevy_asset::io::AssetReader::read` (and `read_meta`)
now return an opaque type instead of a boxed trait object. Implementors
of these methods should change the type signatures appropriately
```rust
impl AssetReader for MyReader {
// Before
async fn read<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
let reader = // construct a reader
Box::new(reader) as Box<Reader<'a>>
}
// After
async fn read<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> {
// create a reader
}
}
```
`bevy::asset::io::Reader` is now a trait, rather than a type alias for a
trait object. Implementors of `AssetLoader::load` will need to adjust
the method signature accordingly
```rust
impl AssetLoader for MyLoader {
async fn load<'a>(
&'a self,
// Before:
reader: &'a mut bevy::asset::io::Reader,
// After:
reader: &'a mut dyn bevy::asset::io::Reader,
_: &'a Self::Settings,
load_context: &'a mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
}
```
Additionally, implementors of `AssetReader` that return a type
implementing `futures_io::AsyncRead` and `AsyncSeek` might need to
explicitly implement `bevy::asset::io::Reader` for that type.
```rust
impl bevy::asset::io::Reader for MyAsyncReadAndSeek {}
```
166 lines
4.7 KiB
Rust
166 lines
4.7 KiB
Rust
use bevy_asset::{io::Reader, AssetLoader, LoadContext};
|
|
use bevy_ecs::prelude::{FromWorld, World};
|
|
use thiserror::Error;
|
|
|
|
use crate::{
|
|
render_asset::RenderAssetUsages,
|
|
renderer::RenderDevice,
|
|
texture::{Image, ImageFormat, ImageType, TextureError},
|
|
};
|
|
|
|
use super::{CompressedImageFormats, ImageSampler};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
/// Loader for images that can be read by the `image` crate.
|
|
#[derive(Clone)]
|
|
pub struct ImageLoader {
|
|
supported_compressed_formats: CompressedImageFormats,
|
|
}
|
|
|
|
pub(crate) const IMG_FILE_EXTENSIONS: &[&str] = &[
|
|
#[cfg(feature = "basis-universal")]
|
|
"basis",
|
|
#[cfg(feature = "bmp")]
|
|
"bmp",
|
|
#[cfg(feature = "png")]
|
|
"png",
|
|
#[cfg(feature = "dds")]
|
|
"dds",
|
|
#[cfg(feature = "tga")]
|
|
"tga",
|
|
#[cfg(feature = "jpeg")]
|
|
"jpg",
|
|
#[cfg(feature = "jpeg")]
|
|
"jpeg",
|
|
#[cfg(feature = "ktx2")]
|
|
"ktx2",
|
|
#[cfg(feature = "webp")]
|
|
"webp",
|
|
#[cfg(feature = "pnm")]
|
|
"pam",
|
|
#[cfg(feature = "pnm")]
|
|
"pbm",
|
|
#[cfg(feature = "pnm")]
|
|
"pgm",
|
|
#[cfg(feature = "pnm")]
|
|
"ppm",
|
|
];
|
|
|
|
#[derive(Serialize, Deserialize, Default, Debug)]
|
|
pub enum ImageFormatSetting {
|
|
#[default]
|
|
FromExtension,
|
|
Format(ImageFormat),
|
|
Guess,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct ImageLoaderSettings {
|
|
pub format: ImageFormatSetting,
|
|
pub is_srgb: bool,
|
|
pub sampler: ImageSampler,
|
|
pub asset_usage: RenderAssetUsages,
|
|
}
|
|
|
|
impl Default for ImageLoaderSettings {
|
|
fn default() -> Self {
|
|
Self {
|
|
format: ImageFormatSetting::default(),
|
|
is_srgb: true,
|
|
sampler: ImageSampler::Default,
|
|
asset_usage: RenderAssetUsages::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[non_exhaustive]
|
|
#[derive(Debug, Error)]
|
|
pub enum ImageLoaderError {
|
|
#[error("Could load shader: {0}")]
|
|
Io(#[from] std::io::Error),
|
|
#[error("Could not load texture file: {0}")]
|
|
FileTexture(#[from] FileTextureError),
|
|
}
|
|
|
|
impl AssetLoader for ImageLoader {
|
|
type Asset = Image;
|
|
type Settings = ImageLoaderSettings;
|
|
type Error = ImageLoaderError;
|
|
async fn load<'a>(
|
|
&'a self,
|
|
reader: &'a mut dyn Reader,
|
|
settings: &'a ImageLoaderSettings,
|
|
load_context: &'a mut LoadContext<'_>,
|
|
) -> Result<Image, Self::Error> {
|
|
let mut bytes = Vec::new();
|
|
reader.read_to_end(&mut bytes).await?;
|
|
let image_type = match settings.format {
|
|
ImageFormatSetting::FromExtension => {
|
|
// use the file extension for the image type
|
|
let ext = load_context.path().extension().unwrap().to_str().unwrap();
|
|
ImageType::Extension(ext)
|
|
}
|
|
ImageFormatSetting::Format(format) => ImageType::Format(format),
|
|
ImageFormatSetting::Guess => {
|
|
let format = image::guess_format(&bytes).map_err(|err| FileTextureError {
|
|
error: err.into(),
|
|
path: format!("{}", load_context.path().display()),
|
|
})?;
|
|
ImageType::Format(ImageFormat::from_image_crate_format(format).ok_or_else(
|
|
|| FileTextureError {
|
|
error: TextureError::UnsupportedTextureFormat(format!("{format:?}")),
|
|
path: format!("{}", load_context.path().display()),
|
|
},
|
|
)?)
|
|
}
|
|
};
|
|
Ok(Image::from_buffer(
|
|
#[cfg(all(debug_assertions, feature = "dds"))]
|
|
load_context.path().display().to_string(),
|
|
&bytes,
|
|
image_type,
|
|
self.supported_compressed_formats,
|
|
settings.is_srgb,
|
|
settings.sampler.clone(),
|
|
settings.asset_usage,
|
|
)
|
|
.map_err(|err| FileTextureError {
|
|
error: err,
|
|
path: format!("{}", load_context.path().display()),
|
|
})?)
|
|
}
|
|
|
|
fn extensions(&self) -> &[&str] {
|
|
IMG_FILE_EXTENSIONS
|
|
}
|
|
}
|
|
|
|
impl FromWorld for ImageLoader {
|
|
fn from_world(world: &mut World) -> Self {
|
|
let supported_compressed_formats = match world.get_resource::<RenderDevice>() {
|
|
Some(render_device) => CompressedImageFormats::from_features(render_device.features()),
|
|
|
|
None => CompressedImageFormats::NONE,
|
|
};
|
|
Self {
|
|
supported_compressed_formats,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// An error that occurs when loading a texture from a file.
|
|
#[derive(Error, Debug)]
|
|
pub struct FileTextureError {
|
|
error: TextureError,
|
|
path: String,
|
|
}
|
|
impl std::fmt::Display for FileTextureError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
write!(
|
|
f,
|
|
"Error reading image file {}: {}, this is an error in `bevy_render`.",
|
|
self.path, self.error
|
|
)
|
|
}
|
|
}
|