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

Remove custom rounding #16097

Merged
merged 19 commits into from
Oct 28, 2024
Merged

Conversation

ickshonpe
Copy link
Contributor

@ickshonpe ickshonpe commented Oct 25, 2024

Objective

Taffy added layout rounding a while ago but it had a couple of bugs and caused some problems with the fussy ab_glyph text implementation. So I disabled Taffy's builtin rounding and added some hacks ad hoc that fixed (some) of those issues. Since then though Taffy's rounding algorithm has improved while we've changed layout a lot and migrated to cosmic-text so those hacks don't help any more and in some cases cause significant problems.

Also our rounding implementation only rounds to the nearest logical pixel, whereas Taffy rounds to the nearest physical pixel meaning it's much more accurate with high dpi displays.

fixes #15197

Some examples of layout rounding errors visible in the UI examples

These errors are much more obvious at high scale factor, you might not see any problems at a scale factor of 1.

cargo run --example text_wrap_debug

text_debug_gaps

The narrow horizontal and vertical lines are gaps in the layout caused by errors in the coordinate rounding.

cargo run --example text_debug

text_debug

The two text blocks here are aligned right to the same boundary but in this screen shot you can see that the lower block is one pixel off to the left. Because the size of this text node changes between frames with the reported framerate the rounding errors cause it to jump left and right.

Solution

Remove all our custom rounding hacks and reenable Taffy's layout rounding.

The gaps in the text_wrap_debug example are gone:
text_wrap_debug_fix

This doesn't fix some of the gaps that occur between borders and content but they seem appear to be a rendering problem as they disappear with UiAntiAlias::Off set.

Testing

Run the examples as described above in the Objective section. With this PR the problems mentioned shouldn't appear.

Also added an example in a separate PR #16096 layout_rounding_debug for identifying these issues.

Migration Guide

UiSurface::get_layout now also returns the final sizes before rounding. Call .0 on the Ok result to get the previously returned taffy::Layout value.

@ickshonpe ickshonpe requested a review from nicoburns October 25, 2024 09:12
Copy link
Contributor

@nicoburns nicoburns left a comment

Choose a reason for hiding this comment

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

Makes sense to me

…/n` to `1 - 1/n` and now it passes.

`ui_rounding_test` was failing after the layout rounding changes but I don't think this is a problem. It only fails at an extremely low scale factor (below ~0.14) but scale factors below 1 aren't common or particularly useful. Even if the node sizes are inaccurate, at that scale a lot of the detail is lost anyway.
@ickshonpe
Copy link
Contributor Author

ickshonpe commented Oct 25, 2024

ui_rounding_test was failing after the layout rounding changes but it only fails at extremely low scale factors (below ~0.14). At that scale factor with 7 logical pixels to 1 physical pixel not much is going to be visable anyway, so I don't think it's worth worrying about. I set the test to start at 1 - 1/n instead of 1/n scale factor and now it passes.

@ickshonpe ickshonpe added A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Oct 25, 2024
@alice-i-cecile
Copy link
Member

@ickshonpe CI is failing on a related test: can you take a look? I suspect the test itself is wrong.

@alice-i-cecile alice-i-cecile added this to the 0.15 milestone Oct 27, 2024
@ickshonpe
Copy link
Contributor Author

Yep I'll fix it now

@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 27, 2024
@@ -351,12 +351,10 @@ with UI components as a child of an entity without UI components, your UI layout

absolute_location += layout_location;

let rounded_size = approx_round_layout_coords(absolute_location + layout_size)
- approx_round_layout_coords(absolute_location);
let rounded_size = (absolute_location + layout_size) - (absolute_location);
Copy link
Contributor

Choose a reason for hiding this comment

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

Parentheses are redundant

@alice-i-cecile alice-i-cecile added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Oct 27, 2024
@alice-i-cecile
Copy link
Member

@ickshonpe CI failure is pointing out a real, if minor issue :) Ping me once it's resolved!

auto-merge was automatically disabled October 27, 2024 20:48

Head branch was pushed to by a user without write access

@ickshonpe
Copy link
Contributor Author

ickshonpe commented Oct 27, 2024

Removing the unrounded size is a breaking change, but I'm okay if the migration guide is just "please tell us why you wanted this".

Couldn't decide what to say in the migration guide, so went back and worked out a really wretched method to retrieve the unrounded size from Taffy. I guess it's better this way though, there don't seem to be any problems anymore but it feels safer to stick with using the unrounded size for text.

@ickshonpe
Copy link
Contributor Author

@ickshonpe CI failure is pointing out a real, if minor issue :) Ping me once it's resolved!

Fixed

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Oct 27, 2024
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Small docs and/or quality request, then this LGTM :)

@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-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Oct 28, 2024
@alice-i-cecile alice-i-cecile added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Oct 28, 2024
Copy link
Contributor

@BenjaminBrienen BenjaminBrienen left a comment

Choose a reason for hiding this comment

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

I noticed a small inconsistency with unwrap vs expect in the tests, but other than that, still good.


let layout = ui_surface
.get_layout(ui_node_entity)
.expect("failed to get layout");
.expect("failed to get layout")
Copy link
Contributor

Choose a reason for hiding this comment

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

expect() here

let ui_surface = world.resource::<UiSurface>();
let layout = ui_surface.get_layout(ui_entity).unwrap();
let mut ui_surface = world.resource_mut::<UiSurface>();
let layout = ui_surface.get_layout(ui_entity).unwrap().0;
Copy link
Contributor

Choose a reason for hiding this comment

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

unwrap() here

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Oct 28, 2024
Merged via the queue into bevyengine:main with commit 05d9068 Oct 28, 2024
29 checks passed
mockersf pushed a commit that referenced this pull request Nov 5, 2024
# Objective

Taffy added layout rounding a while ago but it had a couple of bugs and
caused some problems with the fussy `ab_glyph` text implementation. So I
disabled Taffy's builtin rounding and added some hacks ad hoc that fixed
(some) of those issues. Since then though Taffy's rounding algorithm has
improved while we've changed layout a lot and migrated to `cosmic-text`
so those hacks don't help any more and in some cases cause significant
problems.

Also our rounding implementation only rounds to the nearest logical
pixel, whereas Taffy rounds to the nearest physical pixel meaning it's
much more accurate with high dpi displays.

fixes #15197

## Some examples of layout rounding errors visible in the UI examples

These errors are much more obvious at high scale factor, you might not
see any problems at a scale factor of 1.

`cargo run --example text_wrap_debug`

<img width="1000" alt="text_debug_gaps"
src="https://github.com/user-attachments/assets/5a584016-b8e2-487b-8842-f0f359077391">

The narrow horizontal and vertical lines are gaps in the layout caused
by errors in the coordinate rounding.

`cargo run --example text_debug`

<img width="1000" alt="text_debug"
src="https://github.com/user-attachments/assets/a4b37c02-a2fd-441c-a7bd-cd7a1a72e7dd">

The two text blocks here are aligned right to the same boundary but in
this screen shot you can see that the lower block is one pixel off to
the left. Because the size of this text node changes between frames with
the reported framerate the rounding errors cause it to jump left and
right.

## Solution

Remove all our custom rounding hacks and reenable Taffy's layout
rounding.

The gaps in the `text_wrap_debug` example are gone:
<img width="1000" alt="text_wrap_debug_fix"
src="https://github.com/user-attachments/assets/92d2dd97-30c6-4ac8-99f1-6d65358995a7">

This doesn't fix some of the gaps that occur between borders and content
but they seem appear to be a rendering problem as they disappear with
`UiAntiAlias::Off` set.

## Testing

Run the examples as described above in the `Objective` section. With
this PR the problems mentioned shouldn't appear.

Also added an example in a separate PR #16096 `layout_rounding_debug`
for identifying these issues.

## Migration Guide

`UiSurface::get_layout` now also returns the final sizes before
rounding. Call `.0` on the `Ok` result to get the previously returned
`taffy::Layout` value.

---------

Co-authored-by: Rob Parrett <robparrett@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Delay in text-update vs layout-update, creating wiggling text
5 participants