Skip to content

Commit

Permalink
Fix panic whilst loading UASTC encoded ktx2 textures (#9158)
Browse files Browse the repository at this point in the history
# Objective

Fixes #9121

Context:
- `ImageTextureLoader` depends on `RenderDevice` to work out which
compressed image formats it can support
- `RenderDevice` is initialised by `RenderPlugin`
- #8336 made `RenderPlugin`
initialisation async
- This caused `RenderDevice` to be missing at the time of
`ImageTextureLoader` initialisation, which in turn meant UASTC encoded
ktx2 textures were being converted to unsupported formats, and thus
caused panics

## Solution

- Delay `ImageTextureLoader` initialisation

---

## Changelog

- Moved `ImageTextureLoader` initialisation from `ImagePlugin::build()`
to `ImagePlugin::finish()`
- Default to `CompressedImageFormats::NONE` if `RenderDevice` resource
is missing

---------

Co-authored-by: 66OJ66 <hi0obxud@anonaddy.me>
  • Loading branch information
2 people authored and cart committed Aug 10, 2023
1 parent a634ce3 commit c6d7740
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
17 changes: 10 additions & 7 deletions crates/bevy_gltf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,23 @@ impl GltfPlugin {

impl Plugin for GltfPlugin {
fn build(&self, app: &mut App) {
app.register_type::<GltfExtras>()
.add_asset::<Gltf>()
.add_asset::<GltfNode>()
.add_asset::<GltfPrimitive>()
.add_asset::<GltfMesh>();
}

fn finish(&self, app: &mut App) {
let supported_compressed_formats = match app.world.get_resource::<RenderDevice>() {
Some(render_device) => CompressedImageFormats::from_features(render_device.features()),

None => CompressedImageFormats::all(),
None => CompressedImageFormats::NONE,
};
app.add_asset_loader::<GltfLoader>(GltfLoader {
supported_compressed_formats,
custom_vertex_attributes: self.custom_vertex_attributes.clone(),
})
.register_type::<GltfExtras>()
.add_asset::<Gltf>()
.add_asset::<GltfNode>()
.add_asset::<GltfPrimitive>()
.add_asset::<GltfMesh>();
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/texture/image_texture_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl FromWorld for ImageTextureLoader {
let supported_compressed_formats = match world.get_resource::<RenderDevice>() {
Some(render_device) => CompressedImageFormats::from_features(render_device.features()),

None => CompressedImageFormats::all(),
None => CompressedImageFormats::NONE,
};
Self {
supported_compressed_formats,
Expand Down
26 changes: 13 additions & 13 deletions crates/bevy_render/src/texture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,6 @@ impl ImagePlugin {

impl Plugin for ImagePlugin {
fn build(&self, app: &mut App) {
#[cfg(any(
feature = "png",
feature = "dds",
feature = "tga",
feature = "jpeg",
feature = "bmp",
feature = "basis-universal",
feature = "ktx2",
))]
{
app.init_asset_loader::<ImageTextureLoader>();
}

#[cfg(feature = "exr")]
{
app.init_asset_loader::<ExrTextureLoader>();
Expand Down Expand Up @@ -112,6 +99,19 @@ impl Plugin for ImagePlugin {
}

fn finish(&self, app: &mut App) {
#[cfg(any(
feature = "png",
feature = "dds",
feature = "tga",
feature = "jpeg",
feature = "bmp",
feature = "basis-universal",
feature = "ktx2",
))]
{
app.init_asset_loader::<ImageTextureLoader>();
}

if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
let default_sampler = {
let device = render_app.world.resource::<RenderDevice>();
Expand Down

0 comments on commit c6d7740

Please sign in to comment.