Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text sections with the same font should be batched together #9278

Open
ickshonpe opened this issue Jul 26, 2023 · 1 comment · May be fixed by #9471
Open

Text sections with the same font should be batched together #9278

ickshonpe opened this issue Jul 26, 2023 · 1 comment · May be fixed by #9471
Labels
A-Text Rendering and layout for characters A-UI Graphical user interfaces, styles, layouts, and widgets C-Code-Quality A section of code that is hard to understand or change C-Performance A change motivated by improving speed, memory usage or compile times D-Trivial Nice and easy! A great choice to get started with Bevy

Comments

@ickshonpe
Copy link
Contributor

ickshonpe commented Jul 26, 2023

What problem does this solve or what need does it fill?

Consider the following example:

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn(Camera2dBundle::default());

    let sections = std::iter::repeat(
        [("One", 10.), ("Two", 30.), ("Three\n", 50.)]
            .into_iter()
            .map(|(message, font_size)| {
                TextSection::new(
                    message,
                    TextStyle {
                        font_size,
                        ..Default::default()
                    },
                )
            }),
    )
    .take(10)
    .flatten();
    
    commands.spawn(TextBundle::from_sections(sections));
}

Which has output:

text_batch_example

Text glyphs are stored in TextLayoutInfo in the field glyphs: Vec<PositionedGlyph>. These glyphs are stored in the natural reading order left to right, line after line from top to bottom.

In the example, the glyphs are all using the default font. But because it uses 3 different font sizes (10, 30 and 50) the text pipeline will generate three texture atlases, one for each font size. During the UI extraction schedule, the glyphs are added to the ExtractedUiNodes buffer in order "OneTwoThreeOneTwoThree" and so on. Then in prepare_ui_nodes glyphs that are adjacent in the buffer and from the same TextureAtlas are batched together. This generates thirty batches, ten with the glyphs for "One", ten for "Two" and ten for "Three".

Instead, we should be ordering the glyphs by TextureAtlas which would reduce the number of batches generated down to just three, one batch for the ten "One"s, one for the ten "Two"s and one for the ten "Three"s.

What solution would you like?

Remove the TextureAtlas handle and section information from PostionedGlyph. Store the TextureAtlas handle in a second list (maybe in a smallvec since most text blocks don't have many text sections) per section. Then sort this second list by atlas handle.

@ickshonpe ickshonpe added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Jul 26, 2023
@alice-i-cecile alice-i-cecile added A-UI Graphical user interfaces, styles, layouts, and widgets C-Performance A change motivated by improving speed, memory usage or compile times D-Trivial Nice and easy! A great choice to get started with Bevy C-Code-Quality A section of code that is hard to understand or change and removed C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Jul 27, 2023
@alice-i-cecile
Copy link
Member

Very helpful issue write-up, thank you :)

@viridia viridia added the A-Text Rendering and layout for characters label Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Text Rendering and layout for characters A-UI Graphical user interfaces, styles, layouts, and widgets C-Code-Quality A section of code that is hard to understand or change C-Performance A change motivated by improving speed, memory usage or compile times D-Trivial Nice and easy! A great choice to get started with Bevy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants