many_glyphs/
many_glyphs.rs

1//! Simple text rendering benchmark.
2//!
3//! Creates a text block with a single span containing `100_000` glyphs,
4//! and renders it with the UI in a white color and with Text2d in a red color.
5//!
6//! To recompute all text each frame run
7//! `cargo run --example many_glyphs --release recompute-text`
8use argh::FromArgs;
9use bevy::{
10    color::palettes::basic::RED,
11    diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
12    prelude::*,
13    text::{LineBreak, TextBounds},
14    window::{PresentMode, WindowResolution},
15    winit::WinitSettings,
16};
17
18#[derive(FromArgs, Resource)]
19/// `many_glyphs` stress test
20struct Args {
21    /// don't draw the UI text.
22    #[argh(switch)]
23    no_ui: bool,
24
25    /// don't draw the Text2d text.
26    #[argh(switch)]
27    no_text2d: bool,
28
29    /// whether to force the text to recompute every frame by triggering change detection.
30    #[argh(switch)]
31    recompute_text: bool,
32}
33
34fn main() {
35    // `from_env` panics on the web
36    #[cfg(not(target_arch = "wasm32"))]
37    let args: Args = argh::from_env();
38    #[cfg(target_arch = "wasm32")]
39    let args = Args::from_args(&[], &[]).unwrap();
40
41    let mut app = App::new();
42    app.add_plugins((
43        DefaultPlugins.set(WindowPlugin {
44            primary_window: Some(Window {
45                present_mode: PresentMode::AutoNoVsync,
46                resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),
47                ..default()
48            }),
49            ..default()
50        }),
51        FrameTimeDiagnosticsPlugin::default(),
52        LogDiagnosticsPlugin::default(),
53    ))
54    .insert_resource(WinitSettings::continuous())
55    .add_systems(Startup, setup);
56
57    if args.recompute_text {
58        app.add_systems(Update, force_text_recomputation);
59    }
60
61    app.insert_resource(args).run();
62}
63
64fn setup(mut commands: Commands, args: Res<Args>) {
65    warn!(include_str!("warning_string.txt"));
66
67    commands.spawn(Camera2d);
68    let text_string = "0123456789".repeat(10_000);
69    let text_font = TextFont {
70        font_size: 4.,
71        ..Default::default()
72    };
73    let text_block = TextLayout {
74        justify: Justify::Left,
75        linebreak: LineBreak::AnyCharacter,
76    };
77
78    if !args.no_ui {
79        commands
80            .spawn(Node {
81                width: percent(100),
82                align_items: AlignItems::Center,
83                justify_content: JustifyContent::Center,
84                ..default()
85            })
86            .with_children(|commands| {
87                commands
88                    .spawn(Node {
89                        width: px(1000),
90                        ..Default::default()
91                    })
92                    .with_child((Text(text_string.clone()), text_font.clone(), text_block));
93            });
94    }
95
96    if !args.no_text2d {
97        commands.spawn((
98            Text2d::new(text_string),
99            text_font.clone(),
100            TextColor(RED.into()),
101            bevy::sprite::Anchor::CENTER,
102            TextBounds::new_horizontal(1000.),
103            text_block,
104        ));
105    }
106}
107
108fn force_text_recomputation(mut text_query: Query<&mut TextLayout>) {
109    for mut block in &mut text_query {
110        block.set_changed();
111    }
112}