From 04a02c305e2783398dff22923e89afb9149ebb1d Mon Sep 17 00:00:00 2001 From: Jan Hohenheim Date: Tue, 2 Jul 2024 22:01:38 +0200 Subject: [PATCH] Fix white frames at startup --- src/bevy_config.rs | 47 ++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/bevy_config.rs b/src/bevy_config.rs index 30d908ed..c58125cb 100644 --- a/src/bevy_config.rs +++ b/src/bevy_config.rs @@ -1,6 +1,5 @@ use crate::util::error; -use bevy::render::settings::{WgpuFeatures, WgpuSettings}; -use bevy::render::RenderPlugin; +use bevy::core::FrameCount; use bevy::{prelude::*, window::PrimaryWindow, winit::WinitWindows}; use std::io::Cursor; use winit::window::Icon; @@ -8,30 +7,23 @@ use winit::window::Icon; /// Overrides the default Bevy plugins and configures things like the screen settings. pub(super) fn plugin(app: &mut App) { app.add_plugins( - DefaultPlugins - .set(WindowPlugin { - primary_window: Some(Window { - title: "Foxtrot".to_string(), - ..default() - }), + DefaultPlugins.set(WindowPlugin { + primary_window: Window { + title: "Foxtrot".to_string(), + // This will spawn an invisible window + // The window will be made visible in the make_visible() system after 3 frames. + // This is useful when you want to avoid the white window that shows up before the GPU is ready to render the app. + visible: false, ..default() - }) - .set(RenderPlugin { - render_creation: create_wgpu_settings().into(), - synchronous_pipeline_compilation: false, - }), + } + .into(), + ..default() + }), ) .insert_resource(Msaa::Sample4) .insert_resource(ClearColor(Color::rgb(0.4, 0.4, 0.4))) - .add_systems(Startup, set_window_icon.pipe(error)); -} - -fn create_wgpu_settings() -> WgpuSettings { - let mut wgpu_settings = WgpuSettings::default(); - wgpu_settings - .features - .set(WgpuFeatures::VERTEX_WRITABLE_STORAGE, true); - wgpu_settings + .add_systems(Startup, set_window_icon.pipe(error)) + .add_systems(Update, make_visible); } // Sets the icon on Windows and X11 @@ -55,3 +47,14 @@ fn set_window_icon( }; Ok(()) } + +/// Source: +fn make_visible(mut window: Query<&mut Window>, frames: Res) { + // The delay may be different for your app or system. + if frames.0 == 3 { + // At this point the gpu is ready to show the app so we can make the window visible. + // Alternatively, you could toggle the visibility in Startup. + // It will work, but it will have one white frame before it starts rendering + window.single_mut().visible = true; + } +}