From cacf3929db5969736a6627c4c49abee2920fe857 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Tue, 10 Sep 2024 21:21:40 +0100 Subject: [PATCH 1/4] fix spelling mistake (#15146) # Objective Fix spelling mistake: `interned_root_notes` -> `interned_root_nodes` --- crates/bevy_ui/src/layout/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ui/src/layout/mod.rs b/crates/bevy_ui/src/layout/mod.rs index b529df6f30f40..269af520b1e8f 100644 --- a/crates/bevy_ui/src/layout/mod.rs +++ b/crates/bevy_ui/src/layout/mod.rs @@ -76,7 +76,7 @@ pub struct UiLayoutSystemRemovedComponentParam<'w, 's> { #[doc(hidden)] #[derive(Default)] pub struct UiLayoutSystemBuffers { - interned_root_notes: Vec>, + interned_root_nodes: Vec>, resized_windows: EntityHashSet, camera_layout_info: EntityHashMap, } @@ -122,7 +122,7 @@ pub fn ui_layout_system( #[cfg(feature = "bevy_text")] mut text_pipeline: ResMut, ) { let UiLayoutSystemBuffers { - interned_root_notes, + interned_root_nodes, resized_windows, camera_layout_info, } = &mut *buffers; @@ -147,7 +147,7 @@ pub fn ui_layout_system( size, resized, scale_factor: scale_factor * ui_scale.0, - root_nodes: interned_root_notes.pop().unwrap_or_default(), + root_nodes: interned_root_nodes.pop().unwrap_or_default(), } }; @@ -280,7 +280,7 @@ pub fn ui_layout_system( } camera.root_nodes.clear(); - interned_root_notes.push(camera.root_nodes); + interned_root_nodes.push(camera.root_nodes); } fn update_uinode_geometry_recursive( From fa51e260521a969577e5259ae9402ad1de45d6cd Mon Sep 17 00:00:00 2001 From: UkoeHB <37489173+UkoeHB@users.noreply.github.com> Date: Tue, 10 Sep 2024 18:28:05 -0500 Subject: [PATCH 2/4] Trim cosmic-text's shape run cache (#15037) # Objective - Fixes https://github.com/bevyengine/bevy/pull/14991. The `cosmic-text` shape run cache requires manual cleanup for old text that no longer needs to be cached. ## Solution - Add a system to trim the cache. - Add an `average fps` indicator to the `text_debug` example. ## Testing Tested with `cargo run --example text_debug`. - **No shape run cache**: 82fps with ~1fps variance. - **Shape run cache no trim**: 90-100fps with ~2-4fps variance - **Shape run cache trim age = 1**: 90-100fps with ~2-8fps variance - **Shape run cache trim age = 2**: 90-100fps with ~2-4fps variance - **Shape run cache trim age = 2000**: 80-120fps with ~2-6fps variance The shape run cache seems to increase average FPS but also increases frame time variance (when there is dynamic text). --- crates/bevy_text/src/lib.rs | 3 +- crates/bevy_text/src/pipeline.rs | 17 ++++++++- examples/ui/text_debug.rs | 65 +++++++++++++++++++++++++++++--- 3 files changed, 78 insertions(+), 7 deletions(-) diff --git a/crates/bevy_text/src/lib.rs b/crates/bevy_text/src/lib.rs index 4c379e61e5ae1..0b638cdb1d5e8 100644 --- a/crates/bevy_text/src/lib.rs +++ b/crates/bevy_text/src/lib.rs @@ -123,7 +123,8 @@ impl Plugin for TextPlugin { .ambiguous_with(CameraUpdateSystem), remove_dropped_font_atlas_sets, ), - ); + ) + .add_systems(Last, trim_cosmic_cache); if let Some(render_app) = app.get_sub_app_mut(RenderApp) { render_app.add_systems( diff --git a/crates/bevy_text/src/pipeline.rs b/crates/bevy_text/src/pipeline.rs index 7ac46096270e8..f5fbc64b3a4d8 100644 --- a/crates/bevy_text/src/pipeline.rs +++ b/crates/bevy_text/src/pipeline.rs @@ -1,7 +1,12 @@ use std::sync::Arc; use bevy_asset::{AssetId, Assets}; -use bevy_ecs::{component::Component, entity::Entity, reflect::ReflectComponent, system::Resource}; +use bevy_ecs::{ + component::Component, + entity::Entity, + reflect::ReflectComponent, + system::{ResMut, Resource}, +}; use bevy_math::{UVec2, Vec2}; use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_render::texture::Image; @@ -407,3 +412,13 @@ fn buffer_dimensions(buffer: &Buffer) -> Vec2 { Vec2::new(width.ceil(), height).ceil() } + +/// Discards stale data cached in `FontSystem`. +pub(crate) fn trim_cosmic_cache(mut pipeline: ResMut) { + // A trim age of 2 was found to reduce frame time variance vs age of 1 when tested with dynamic text. + // See https://github.com/bevyengine/bevy/pull/15037 + // + // We assume only text updated frequently benefits from the shape cache (e.g. animated text, or + // text that is dynamically measured for UI). + pipeline.font_system_mut().shape_run_cache.trim(2); +} diff --git a/examples/ui/text_debug.rs b/examples/ui/text_debug.rs index 0e4fbe1baca75..e2f596c35290f 100644 --- a/examples/ui/text_debug.rs +++ b/examples/ui/text_debug.rs @@ -1,5 +1,7 @@ //! Shows various text layout options. +use std::{collections::VecDeque, time::Duration}; + use bevy::{ color::palettes::css::*, diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}, @@ -154,7 +156,15 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { builder.spawn(( TextBundle::from_sections([ TextSection::new( - "This text changes in the bottom right", + "", + TextStyle { + font: font.clone(), + font_size: 25.0, + ..default() + }, + ), + TextSection::new( + "\nThis text changes in the bottom right", TextStyle { font: font.clone(), font_size: 25.0, @@ -223,10 +233,23 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { } fn change_text_system( + mut fps_history: Local>, + mut time_history: Local>, time: Res