use bevy default texture format if the surface is not yet available (#6233)

# Objective

- Fix #6231

## Solution

- In case no supported format is found, try to use Bevy default instead of panicking
This commit is contained in:
François 2022-10-11 12:32:03 +00:00
parent 7673db731e
commit 13dcdba05f
2 changed files with 11 additions and 11 deletions

View File

@ -39,6 +39,7 @@ pub mod prelude {
use globals::GlobalsPlugin; use globals::GlobalsPlugin;
pub use once_cell; pub use once_cell;
use prelude::ComputedVisibility; use prelude::ComputedVisibility;
use wgpu::TextureFormat;
use crate::{ use crate::{
camera::CameraPlugin, camera::CameraPlugin,
@ -48,7 +49,7 @@ use crate::{
render_graph::RenderGraph, render_graph::RenderGraph,
render_resource::{PipelineCache, Shader, ShaderLoader}, render_resource::{PipelineCache, Shader, ShaderLoader},
renderer::{render_system, RenderInstance, RenderTextureFormat}, renderer::{render_system, RenderInstance, RenderTextureFormat},
texture::ImagePlugin, texture::{BevyDefault, ImagePlugin},
view::{ViewPlugin, WindowRenderPlugin}, view::{ViewPlugin, WindowRenderPlugin},
}; };
use bevy_app::{App, AppLabel, Plugin}; use bevy_app::{App, AppLabel, Plugin};
@ -163,9 +164,12 @@ impl Plugin for RenderPlugin {
&options, &options,
&request_adapter_options, &request_adapter_options,
)); ));
// `available_texture_formats` won't be empty, or else will panick in the former let texture_format = RenderTextureFormat(
// `initialize_renderer` call. available_texture_formats
let first_available_texture_format = RenderTextureFormat(available_texture_formats[0]); .get(0)
.cloned()
.unwrap_or_else(TextureFormat::bevy_default),
);
debug!("Configured wgpu adapter Limits: {:#?}", device.limits()); debug!("Configured wgpu adapter Limits: {:#?}", device.limits());
debug!("Configured wgpu adapter Features: {:#?}", device.features()); debug!("Configured wgpu adapter Features: {:#?}", device.features());
app.insert_resource(device.clone()) app.insert_resource(device.clone())
@ -173,7 +177,7 @@ impl Plugin for RenderPlugin {
.insert_resource(adapter_info.clone()) .insert_resource(adapter_info.clone())
.insert_resource(render_adapter.clone()) .insert_resource(render_adapter.clone())
.insert_resource(available_texture_formats.clone()) .insert_resource(available_texture_formats.clone())
.insert_resource(first_available_texture_format.clone()) .insert_resource(texture_format.clone())
.init_resource::<ScratchMainWorld>() .init_resource::<ScratchMainWorld>()
.register_type::<Frustum>() .register_type::<Frustum>()
.register_type::<CubemapFrusta>(); .register_type::<CubemapFrusta>();
@ -217,7 +221,7 @@ impl Plugin for RenderPlugin {
.insert_resource(queue) .insert_resource(queue)
.insert_resource(render_adapter) .insert_resource(render_adapter)
.insert_resource(available_texture_formats) .insert_resource(available_texture_formats)
.insert_resource(first_available_texture_format) .insert_resource(texture_format)
.insert_resource(adapter_info) .insert_resource(adapter_info)
.insert_resource(pipeline_cache) .insert_resource(pipeline_cache)
.insert_resource(asset_server); .insert_resource(asset_server);

View File

@ -103,7 +103,7 @@ pub struct RenderInstance(pub Instance);
pub struct RenderAdapterInfo(pub AdapterInfo); pub struct RenderAdapterInfo(pub AdapterInfo);
/// The [`TextureFormat`](wgpu::TextureFormat) used for rendering. /// The [`TextureFormat`](wgpu::TextureFormat) used for rendering.
/// Initially it's the first element in `AvailableTextureFormats`. /// Initially it's the first element in `AvailableTextureFormats`, or Bevy default format.
#[derive(Resource, Clone, Deref, DerefMut)] #[derive(Resource, Clone, Deref, DerefMut)]
pub struct RenderTextureFormat(pub wgpu::TextureFormat); pub struct RenderTextureFormat(pub wgpu::TextureFormat);
@ -278,10 +278,6 @@ pub async fn initialize_renderer(
let mut available_texture_formats = Vec::new(); let mut available_texture_formats = Vec::new();
if let Some(s) = request_adapter_options.compatible_surface { if let Some(s) = request_adapter_options.compatible_surface {
available_texture_formats = s.get_supported_formats(&adapter); available_texture_formats = s.get_supported_formats(&adapter);
if available_texture_formats.is_empty() {
info!("{:?}", adapter_info);
panic!("No supported texture formats found!");
}
}; };
let available_texture_formats = Arc::new(available_texture_formats); let available_texture_formats = Arc::new(available_texture_formats);
( (