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

Improved text batching #14848

Merged
merged 30 commits into from
Oct 8, 2024
Merged

Conversation

ickshonpe
Copy link
Contributor

@ickshonpe ickshonpe commented Aug 21, 2024

Objective

The UI text rendering is really slow because it extracts each glyph as a separate ui node even though all the glyphs in a text section have the same texture, color and clipping rects.

Solution

Store the glyphs in a seperate contiguous array, queue one transparent ui item per text section which has indices into the glyph array.

Testing

cargo run --example many_glyphs --release

Runs at about 22fps on main and 95fps with this PR on my computer.

I'll do some proper comparisons once I work out why tracy 11 is refusing to run.

@ickshonpe ickshonpe added A-UI Graphical user interfaces, styles, layouts, and widgets A-Text Rendering and layout for characters A-Rendering Drawing game state to the screen C-Performance A change motivated by improving speed, memory usage or compile times labels Aug 21, 2024
@alice-i-cecile alice-i-cecile added this to the 0.15 milestone Aug 21, 2024
@alice-i-cecile alice-i-cecile added M-Needs-Release-Note Work that should be called out in the blog due to impact S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 21, 2024
section_index,
..
},
) in text_layout_info.glyphs.iter().enumerate()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this is a bit hard to read, moving the PositionedGlyph destructuring into the for loop body could make it easier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. I prefer the more concise deconstruction,it uses less vertical space and the inner code block isn't that large . It's not that important though if consensus favours the other way I don't mind changing it.

@TrialDragon TrialDragon added the D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes label Aug 22, 2024
Copy link
Contributor

@kristoff3r kristoff3r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me, and I tested some text examples on Linux

Comment on lines +1301 to +1369
let size = glyph.rect.size();

let rect_size = glyph_rect.size().extend(1.0);

// Specify the corners of the glyph
let positions = QUAD_VERTEX_POSITIONS.map(|pos| {
(glyph.transform * (pos * rect_size).extend(1.)).xyz()
});

let positions_diff = if let Some(clip) = extracted_uinode.clip {
[
Vec2::new(
f32::max(clip.min.x - positions[0].x, 0.),
f32::max(clip.min.y - positions[0].y, 0.),
),
Vec2::new(
f32::min(clip.max.x - positions[1].x, 0.),
f32::max(clip.min.y - positions[1].y, 0.),
),
Vec2::new(
f32::min(clip.max.x - positions[2].x, 0.),
f32::min(clip.max.y - positions[2].y, 0.),
),
Vec2::new(
f32::max(clip.min.x - positions[3].x, 0.),
f32::min(clip.max.y - positions[3].y, 0.),
),
]
} else {
[Vec2::ZERO; 4]
};

let positions_clipped = [
positions[0] + positions_diff[0].extend(0.),
positions[1] + positions_diff[1].extend(0.),
positions[2] + positions_diff[2].extend(0.),
positions[3] + positions_diff[3].extend(0.),
];

// cull nodes that are completely clipped
let transformed_rect_size =
glyph.transform.transform_vector3(rect_size);
if positions_diff[0].x - positions_diff[1].x
>= transformed_rect_size.x
|| positions_diff[1].y - positions_diff[2].y
>= transformed_rect_size.y
{
continue;
}

let uvs = [
Vec2::new(
glyph.rect.min.x + positions_diff[0].x,
glyph.rect.min.y + positions_diff[0].y,
),
Vec2::new(
glyph.rect.max.x + positions_diff[1].x,
glyph.rect.min.y + positions_diff[1].y,
),
Vec2::new(
glyph.rect.max.x + positions_diff[2].x,
glyph.rect.max.y + positions_diff[2].y,
),
Vec2::new(
glyph.rect.min.x + positions_diff[3].x,
glyph.rect.max.y + positions_diff[3].y,
),
]
.map(|pos| pos / atlas_extent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these calculations are duplicated in the Node / Glyph cases. It's not a big issue but maybe they could be moved to a small helper function or closure? This would make the code less verbose and ensure that they are equivalent if they change later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely ugly I agree, I don't want to tackle it here though as it's very fragile and there are some bugs with clipping that need to be fixed.

crates/bevy_ui/src/render/mod.rs Outdated Show resolved Hide resolved
Co-authored-by: Kristoffer Søholm <k.soeholm@gmail.com>
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Oct 8, 2024
@alice-i-cecile
Copy link
Member

@ickshonpe I'm comfortable merging this: let me know when CI is green and merge conflicts are resolved?

@ickshonpe
Copy link
Contributor Author

ickshonpe commented Oct 8, 2024

Running into some conflicts with the retained render world changes I think, not sure where the problem is.

@ickshonpe
Copy link
Contributor Author

@ickshonpe I'm comfortable merging this: let me know when CI is green and merge conflicts are resolved?

Okay panic over, somehow I managed to accidently replace all the continues in the prepare function with returns 😂, should be ready to merge now

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Oct 8, 2024
Merged via the queue into bevyengine:main with commit 675f8ad Oct 8, 2024
26 checks passed
@ickshonpe ickshonpe mentioned this pull request Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen A-Text Rendering and layout for characters A-UI Graphical user interfaces, styles, layouts, and widgets C-Performance A change motivated by improving speed, memory usage or compile times D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes M-Needs-Release-Note Work that should be called out in the blog due to impact S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants