text_pipeline/
text_pipeline.rs

1//! Text pipeline benchmark.
2//!
3//! Continuously recomputes a large block of text with 100 text spans.
4
5use bevy::{
6    color::palettes::basic::{BLUE, YELLOW},
7    diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
8    prelude::*,
9    text::{LineBreak, TextBounds},
10    window::{PresentMode, WindowResolution},
11    winit::{UpdateMode, WinitSettings},
12};
13
14fn main() {
15    App::new()
16        .add_plugins((
17            DefaultPlugins.set(WindowPlugin {
18                primary_window: Some(Window {
19                    present_mode: PresentMode::AutoNoVsync,
20                    resolution: WindowResolution::new(1920.0, 1080.0)
21                        .with_scale_factor_override(1.0),
22                    ..default()
23                }),
24                ..default()
25            }),
26            FrameTimeDiagnosticsPlugin::default(),
27            LogDiagnosticsPlugin::default(),
28        ))
29        .insert_resource(WinitSettings {
30            focused_mode: UpdateMode::Continuous,
31            unfocused_mode: UpdateMode::Continuous,
32        })
33        .add_systems(Startup, spawn)
34        .add_systems(Update, update_text_bounds)
35        .run();
36}
37
38fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
39    warn!(include_str!("warning_string.txt"));
40
41    commands.spawn(Camera2d);
42
43    let make_spans = |i| {
44        [
45            (
46                TextSpan("text".repeat(i)),
47                TextFont {
48                    font: asset_server.load("fonts/FiraMono-Medium.ttf"),
49                    font_size: (4 + i % 10) as f32,
50                    ..Default::default()
51                },
52                TextColor(BLUE.into()),
53            ),
54            (
55                TextSpan("pipeline".repeat(i)),
56                TextFont {
57                    font: asset_server.load("fonts/FiraSans-Bold.ttf"),
58                    font_size: (4 + i % 11) as f32,
59                    ..default()
60                },
61                TextColor(YELLOW.into()),
62            ),
63        ]
64    };
65
66    let spans = (1..50).flat_map(|i| make_spans(i).into_iter());
67
68    commands
69        .spawn((
70            Text2d::default(),
71            TextLayout {
72                justify: JustifyText::Center,
73                linebreak: LineBreak::AnyCharacter,
74            },
75            TextBounds::default(),
76        ))
77        .with_children(|p| {
78            for span in spans {
79                p.spawn(span);
80            }
81        });
82}
83
84// changing the bounds of the text will cause a recomputation
85fn update_text_bounds(time: Res<Time>, mut text_bounds_query: Query<&mut TextBounds>) {
86    let width = (1. + ops::sin(time.elapsed_secs())) * 600.0;
87    for mut text_bounds in text_bounds_query.iter_mut() {
88        text_bounds.width = Some(width);
89    }
90}