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
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-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants