Fix panic whilst loading UASTC encoded ktx2 textures (#9158)
# Objective Fixes #9121 Context: - `ImageTextureLoader` depends on `RenderDevice` to work out which compressed image formats it can support - `RenderDevice` is initialised by `RenderPlugin` - https://github.com/bevyengine/bevy/pull/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>
This commit is contained in:
		
							parent
							
								
									3b1b60e7dc
								
							
						
					
					
						commit
						5b0e6a5321
					
				@ -40,20 +40,23 @@ impl GltfPlugin {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
impl Plugin for GltfPlugin {
 | 
					impl Plugin for GltfPlugin {
 | 
				
			||||||
    fn build(&self, app: &mut App) {
 | 
					    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>() {
 | 
					        let supported_compressed_formats = match app.world.get_resource::<RenderDevice>() {
 | 
				
			||||||
            Some(render_device) => CompressedImageFormats::from_features(render_device.features()),
 | 
					            Some(render_device) => CompressedImageFormats::from_features(render_device.features()),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            None => CompressedImageFormats::all(),
 | 
					            None => CompressedImageFormats::NONE,
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        app.add_asset_loader::<GltfLoader>(GltfLoader {
 | 
					        app.add_asset_loader::<GltfLoader>(GltfLoader {
 | 
				
			||||||
            supported_compressed_formats,
 | 
					            supported_compressed_formats,
 | 
				
			||||||
            custom_vertex_attributes: self.custom_vertex_attributes.clone(),
 | 
					            custom_vertex_attributes: self.custom_vertex_attributes.clone(),
 | 
				
			||||||
        })
 | 
					        });
 | 
				
			||||||
        .register_type::<GltfExtras>()
 | 
					 | 
				
			||||||
        .add_asset::<Gltf>()
 | 
					 | 
				
			||||||
        .add_asset::<GltfNode>()
 | 
					 | 
				
			||||||
        .add_asset::<GltfPrimitive>()
 | 
					 | 
				
			||||||
        .add_asset::<GltfMesh>();
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -82,7 +82,7 @@ impl FromWorld for ImageTextureLoader {
 | 
				
			|||||||
        let supported_compressed_formats = match world.get_resource::<RenderDevice>() {
 | 
					        let supported_compressed_formats = match world.get_resource::<RenderDevice>() {
 | 
				
			||||||
            Some(render_device) => CompressedImageFormats::from_features(render_device.features()),
 | 
					            Some(render_device) => CompressedImageFormats::from_features(render_device.features()),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            None => CompressedImageFormats::all(),
 | 
					            None => CompressedImageFormats::NONE,
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        Self {
 | 
					        Self {
 | 
				
			||||||
            supported_compressed_formats,
 | 
					            supported_compressed_formats,
 | 
				
			||||||
 | 
				
			|||||||
@ -70,19 +70,6 @@ impl ImagePlugin {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
impl Plugin for ImagePlugin {
 | 
					impl Plugin for ImagePlugin {
 | 
				
			||||||
    fn build(&self, app: &mut App) {
 | 
					    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")]
 | 
					        #[cfg(feature = "exr")]
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            app.init_asset_loader::<ExrTextureLoader>();
 | 
					            app.init_asset_loader::<ExrTextureLoader>();
 | 
				
			||||||
@ -112,6 +99,19 @@ impl Plugin for ImagePlugin {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn finish(&self, app: &mut App) {
 | 
					    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) {
 | 
					        if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
 | 
				
			||||||
            let default_sampler = {
 | 
					            let default_sampler = {
 | 
				
			||||||
                let device = render_app.world.resource::<RenderDevice>();
 | 
					                let device = render_app.world.resource::<RenderDevice>();
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user