Skip to content

Conversation

ickshonpe
Copy link
Contributor

@ickshonpe ickshonpe commented Sep 4, 2025

Objective

In the UI picking and rendering systems, we first query for all the pickable or renderable UI nodes and then query per node for the details of the camera and its render target. But the typical application using Bevy UI will have hundreds of UI nodes and just one UI camera, so this is extremely inefficient.

Instead, we can partition the UI stack into disjoint slices where all the UI nodes in each slice have the same camera target. Then perform any camera and render target lookups per slice, instead of per node.

Solution

Partition the UI stack into disjoint layers of nodes sharing the same camera target.

  • Add a partition: Vec<Range<usize>> field to UiStack.
  • Update the partitions in ui_stack_system.
  • Query for cameras per slice in ui_focus_system and ui_picking.

Splitting the rendering changes off into their own PR.


Testing

Some basic checks have been added to the existing test_ui_stack_system and test_with_equal_global_zindex_zindex_decides_order tests.

Examples like ui_target_camera, viewport_node and ui_drag_and_drop can be used to test the changes.

Showcase

yellow this PR, red main:

cargo run --example many_buttons --release --features bevy/trace_tracy

ui_picking
ui_picking

ui_stack_system
ui_stack


@james7132 james7132 added C-Performance A change motivated by improving speed, memory usage or compile times A-UI Graphical user interfaces, styles, layouts, and widgets S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 4, 2025
@ickshonpe ickshonpe changed the title Layered UI stack UI stack layers Sep 4, 2025
@ickshonpe ickshonpe added D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes A-Rendering Drawing game state to the screen C-Code-Quality A section of code that is hard to understand or change A-Picking Pointing at and selecting objects of all sorts labels Sep 5, 2025
@ickshonpe ickshonpe changed the title UI stack layers UI stack partition Sep 5, 2025
@ickshonpe ickshonpe removed the A-Rendering Drawing game state to the screen label Sep 5, 2025
Copy link
Contributor

@Zeophlite Zeophlite left a comment

Choose a reason for hiding this comment

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

Very neat

continue;
}

// Nodes with Display::None have a (0., 0.) logical rect and can be ignored
Copy link
Contributor

Choose a reason for hiding this comment

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

I stumbled here for a moment... I was like: "What does Display::None have to do with this?" until I figured that I should read the sentence in another way:

"Nodes that have a size of 0 - logical rect of (0., 0.) - are actually "Display::None" and thus can be ignored

@Zeophlite Zeophlite 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 Sep 14, 2025
@alice-i-cecile alice-i-cecile added this to the 0.18 milestone Sep 14, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Sep 29, 2025
Merged via the queue into bevyengine:main with commit fac186f Sep 29, 2025
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Picking Pointing at and selecting objects of all sorts 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-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes 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.

5 participants