Add stack index to Node
(#9853)
# Objective If we add the stack index to `Node` then we don't need to walk the `UiStack` repeatedly during extraction. ## Solution Add a field `stack_index` to `Node`. Update it in `ui_stack_system`. Iterate queries directly in the UI's extraction systems. ### Benchmarks ``` cargo run --profile stress-test --features trace_tracy --example many_buttons -- --no-text --no-borders ``` frames (yellow this PR, red main): <img width="447" alt="frames-per-second" src="https://github.com/bevyengine/bevy/assets/27962798/385c0ccf-c257-42a2-b736-117542d56eff"> `ui_stack_system`: <img width="585" alt="ui-stack-system" src="https://github.com/bevyengine/bevy/assets/27962798/2916cc44-2887-4c3b-a144-13250d84f7d5"> extract schedule: <img width="469" alt="extract-schedule" src="https://github.com/bevyengine/bevy/assets/27962798/858d4ab4-d99f-48e8-b153-1c92f51e0743"> --- ## Changelog * Added the field `stack_index` to `Node`. * `ui_stack_system` updates `Node::stack_index` after a new `UiStack` is generated. * The UI's extraction functions iterate a query directly rather than walking the `UiStack` and doing lookups.
This commit is contained in:
parent
44928e0df4
commit
563d6e36bb
@ -13,7 +13,7 @@ pub use render_pass::*;
|
||||
use crate::Outline;
|
||||
use crate::{
|
||||
prelude::UiCameraConfig, BackgroundColor, BorderColor, CalculatedClip, ContentSize, Node,
|
||||
Style, UiImage, UiScale, UiStack, UiTextureAtlasImage, Val,
|
||||
Style, UiImage, UiScale, UiTextureAtlasImage, Val,
|
||||
};
|
||||
|
||||
use bevy_app::prelude::*;
|
||||
@ -152,7 +152,7 @@ fn get_ui_graph(render_app: &mut App) -> RenderGraph {
|
||||
}
|
||||
|
||||
pub struct ExtractedUiNode {
|
||||
pub stack_index: usize,
|
||||
pub stack_index: u32,
|
||||
pub transform: Mat4,
|
||||
pub color: Color,
|
||||
pub rect: Rect,
|
||||
@ -172,7 +172,6 @@ pub fn extract_atlas_uinodes(
|
||||
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
||||
images: Extract<Res<Assets<Image>>>,
|
||||
texture_atlases: Extract<Res<Assets<TextureAtlas>>>,
|
||||
ui_stack: Extract<Res<UiStack>>,
|
||||
uinode_query: Extract<
|
||||
Query<
|
||||
(
|
||||
@ -189,8 +188,7 @@ pub fn extract_atlas_uinodes(
|
||||
>,
|
||||
>,
|
||||
) {
|
||||
for (stack_index, entity) in ui_stack.uinodes.iter().enumerate() {
|
||||
if let Ok((
|
||||
for (
|
||||
entity,
|
||||
uinode,
|
||||
transform,
|
||||
@ -199,7 +197,7 @@ pub fn extract_atlas_uinodes(
|
||||
clip,
|
||||
texture_atlas_handle,
|
||||
atlas_image,
|
||||
)) = uinode_query.get(*entity)
|
||||
) in uinode_query.iter()
|
||||
{
|
||||
// Skip invisible and completely transparent nodes
|
||||
if !view_visibility.get() || color.0.is_fully_transparent() {
|
||||
@ -241,7 +239,7 @@ pub fn extract_atlas_uinodes(
|
||||
extracted_uinodes.uinodes.insert(
|
||||
entity,
|
||||
ExtractedUiNode {
|
||||
stack_index,
|
||||
stack_index: uinode.stack_index,
|
||||
transform: transform.compute_matrix(),
|
||||
color: color.0,
|
||||
rect: atlas_rect,
|
||||
@ -254,7 +252,6 @@ pub fn extract_atlas_uinodes(
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_border_thickness(value: Val, parent_width: f32, viewport_size: Vec2) -> f32 {
|
||||
match value {
|
||||
@ -273,7 +270,6 @@ pub fn extract_uinode_borders(
|
||||
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
||||
windows: Extract<Query<&Window, With<PrimaryWindow>>>,
|
||||
ui_scale: Extract<Res<UiScale>>,
|
||||
ui_stack: Extract<Res<UiStack>>,
|
||||
uinode_query: Extract<
|
||||
Query<
|
||||
(
|
||||
@ -300,9 +296,8 @@ pub fn extract_uinode_borders(
|
||||
// so we have to divide by `UiScale` to get the size of the UI viewport.
|
||||
/ ui_scale.0 as f32;
|
||||
|
||||
for (stack_index, entity) in ui_stack.uinodes.iter().enumerate() {
|
||||
if let Ok((node, global_transform, style, border_color, parent, view_visibility, clip)) =
|
||||
uinode_query.get(*entity)
|
||||
for (node, global_transform, style, border_color, parent, view_visibility, clip) in
|
||||
uinode_query.iter()
|
||||
{
|
||||
// Skip invisible borders
|
||||
if !view_visibility.get()
|
||||
@ -321,18 +316,12 @@ pub fn extract_uinode_borders(
|
||||
.unwrap_or(ui_logical_viewport_size.x);
|
||||
let left =
|
||||
resolve_border_thickness(style.border.left, parent_width, ui_logical_viewport_size);
|
||||
let right = resolve_border_thickness(
|
||||
style.border.right,
|
||||
parent_width,
|
||||
ui_logical_viewport_size,
|
||||
);
|
||||
let right =
|
||||
resolve_border_thickness(style.border.right, parent_width, ui_logical_viewport_size);
|
||||
let top =
|
||||
resolve_border_thickness(style.border.top, parent_width, ui_logical_viewport_size);
|
||||
let bottom = resolve_border_thickness(
|
||||
style.border.bottom,
|
||||
parent_width,
|
||||
ui_logical_viewport_size,
|
||||
);
|
||||
let bottom =
|
||||
resolve_border_thickness(style.border.bottom, parent_width, ui_logical_viewport_size);
|
||||
|
||||
// Calculate the border rects, ensuring no overlap.
|
||||
// The border occupies the space between the node's bounding rect and the node's bounding rect inset in each direction by the node's corresponding border value.
|
||||
@ -370,7 +359,7 @@ pub fn extract_uinode_borders(
|
||||
extracted_uinodes.uinodes.insert(
|
||||
commands.spawn_empty().id(),
|
||||
ExtractedUiNode {
|
||||
stack_index,
|
||||
stack_index: node.stack_index,
|
||||
// This translates the uinode's transform to the center of the current border rectangle
|
||||
transform: transform * Mat4::from_translation(edge.center().extend(0.)),
|
||||
color: border_color.0,
|
||||
@ -389,12 +378,10 @@ pub fn extract_uinode_borders(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extract_uinode_outlines(
|
||||
mut commands: Commands,
|
||||
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
||||
ui_stack: Extract<Res<UiStack>>,
|
||||
uinode_query: Extract<
|
||||
Query<(
|
||||
&Node,
|
||||
@ -407,11 +394,7 @@ pub fn extract_uinode_outlines(
|
||||
clip_query: Query<&CalculatedClip>,
|
||||
) {
|
||||
let image = AssetId::<Image>::default();
|
||||
|
||||
for (stack_index, entity) in ui_stack.uinodes.iter().enumerate() {
|
||||
if let Ok((node, global_transform, outline, view_visibility, maybe_parent)) =
|
||||
uinode_query.get(*entity)
|
||||
{
|
||||
for (node, global_transform, outline, view_visibility, maybe_parent) in uinode_query.iter() {
|
||||
// Skip invisible outlines
|
||||
if !view_visibility.get()
|
||||
|| outline.color.is_fully_transparent()
|
||||
@ -421,12 +404,11 @@ pub fn extract_uinode_outlines(
|
||||
}
|
||||
|
||||
// Outline's are drawn outside of a node's borders, so they are clipped using the clipping Rect of their UI node entity's parent.
|
||||
let clip = maybe_parent
|
||||
.and_then(|parent| clip_query.get(parent.get()).ok().map(|clip| clip.clip));
|
||||
let clip =
|
||||
maybe_parent.and_then(|parent| clip_query.get(parent.get()).ok().map(|clip| clip.clip));
|
||||
|
||||
// Calculate the outline rects.
|
||||
let inner_rect =
|
||||
Rect::from_center_size(Vec2::ZERO, node.size() + 2. * node.outline_offset);
|
||||
let inner_rect = Rect::from_center_size(Vec2::ZERO, node.size() + 2. * node.outline_offset);
|
||||
let outer_rect = inner_rect.inset(node.outline_width());
|
||||
let outline_edges = [
|
||||
// Left edge
|
||||
@ -466,7 +448,7 @@ pub fn extract_uinode_outlines(
|
||||
extracted_uinodes.uinodes.insert(
|
||||
commands.spawn_empty().id(),
|
||||
ExtractedUiNode {
|
||||
stack_index,
|
||||
stack_index: node.stack_index,
|
||||
// This translates the uinode's transform to the center of the current border rectangle
|
||||
transform: transform * Mat4::from_translation(edge.center().extend(0.)),
|
||||
color: outline.color,
|
||||
@ -485,12 +467,10 @@ pub fn extract_uinode_outlines(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extract_uinodes(
|
||||
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
||||
images: Extract<Res<Assets<Image>>>,
|
||||
ui_stack: Extract<Res<UiStack>>,
|
||||
uinode_query: Extract<
|
||||
Query<
|
||||
(
|
||||
@ -506,9 +486,8 @@ pub fn extract_uinodes(
|
||||
>,
|
||||
>,
|
||||
) {
|
||||
for (stack_index, entity) in ui_stack.uinodes.iter().enumerate() {
|
||||
if let Ok((entity, uinode, transform, color, maybe_image, view_visibility, clip)) =
|
||||
uinode_query.get(*entity)
|
||||
for (entity, uinode, transform, color, maybe_image, view_visibility, clip) in
|
||||
uinode_query.iter()
|
||||
{
|
||||
// Skip invisible and completely transparent nodes
|
||||
if !view_visibility.get() || color.0.is_fully_transparent() {
|
||||
@ -528,7 +507,7 @@ pub fn extract_uinodes(
|
||||
extracted_uinodes.uinodes.insert(
|
||||
entity,
|
||||
ExtractedUiNode {
|
||||
stack_index,
|
||||
stack_index: uinode.stack_index,
|
||||
transform: transform.compute_matrix(),
|
||||
color: color.0,
|
||||
rect: Rect {
|
||||
@ -542,7 +521,6 @@ pub fn extract_uinodes(
|
||||
flip_y,
|
||||
},
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -625,7 +603,6 @@ pub fn extract_text_uinodes(
|
||||
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
||||
texture_atlases: Extract<Res<Assets<TextureAtlas>>>,
|
||||
windows: Extract<Query<&Window, With<PrimaryWindow>>>,
|
||||
ui_stack: Extract<Res<UiStack>>,
|
||||
ui_scale: Extract<Res<UiScale>>,
|
||||
uinode_query: Extract<
|
||||
Query<(
|
||||
@ -647,9 +624,8 @@ pub fn extract_text_uinodes(
|
||||
|
||||
let inverse_scale_factor = (scale_factor as f32).recip();
|
||||
|
||||
for (stack_index, entity) in ui_stack.uinodes.iter().enumerate() {
|
||||
if let Ok((uinode, global_transform, text, text_layout_info, view_visibility, clip)) =
|
||||
uinode_query.get(*entity)
|
||||
for (uinode, global_transform, text, text_layout_info, view_visibility, clip) in
|
||||
uinode_query.iter()
|
||||
{
|
||||
// Skip if not visible or if size is set to zero (e.g. when a parent is set to `Display::None`)
|
||||
if !view_visibility.get() || uinode.size().x == 0. || uinode.size().y == 0. {
|
||||
@ -679,7 +655,7 @@ pub fn extract_text_uinodes(
|
||||
extracted_uinodes.uinodes.insert(
|
||||
commands.spawn_empty().id(),
|
||||
ExtractedUiNode {
|
||||
stack_index,
|
||||
stack_index: uinode.stack_index,
|
||||
transform: transform
|
||||
* Mat4::from_translation(position.extend(0.) * inverse_scale_factor),
|
||||
color,
|
||||
@ -694,7 +670,6 @@ pub fn extract_text_uinodes(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Pod, Zeroable)]
|
||||
|
@ -35,6 +35,7 @@ pub fn ui_stack_system(
|
||||
root_node_query: Query<Entity, (With<Node>, Without<Parent>)>,
|
||||
zindex_query: Query<&ZIndex, With<Node>>,
|
||||
children_query: Query<&Children>,
|
||||
mut update_query: Query<&mut Node>,
|
||||
) {
|
||||
// Generate `StackingContext` tree
|
||||
let mut global_context = StackingContext::default();
|
||||
@ -55,6 +56,14 @@ pub fn ui_stack_system(
|
||||
ui_stack.uinodes.clear();
|
||||
ui_stack.uinodes.reserve(total_entry_count);
|
||||
fill_stack_recursively(&mut ui_stack.uinodes, &mut global_context);
|
||||
|
||||
for (i, entity) in ui_stack.uinodes.iter().enumerate() {
|
||||
update_query
|
||||
.get_mut(*entity)
|
||||
.unwrap()
|
||||
.bypass_change_detection()
|
||||
.stack_index = i as u32;
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate z-index based UI node tree
|
||||
|
@ -14,9 +14,12 @@ use thiserror::Error;
|
||||
#[derive(Component, Debug, Copy, Clone, Reflect)]
|
||||
#[reflect(Component, Default)]
|
||||
pub struct Node {
|
||||
/// The size of the node as width and height in logical pixels.
|
||||
/// The order of the node in the UI layout.
|
||||
/// Nodes with a higher stack index are drawn on top of and recieve interactions before nodes with lower stack indices.
|
||||
pub(crate) stack_index: u32,
|
||||
/// The size of the node as width and height in logical pixels
|
||||
///
|
||||
/// Automatically calculated by [`super::layout::ui_layout_system`].
|
||||
/// automatically calculated by [`super::layout::ui_layout_system`]
|
||||
pub(crate) calculated_size: Vec2,
|
||||
/// The width of this node's outline.
|
||||
/// If this value is `Auto`, negative or `0.` then no outline will be rendered.
|
||||
@ -39,6 +42,12 @@ impl Node {
|
||||
self.calculated_size
|
||||
}
|
||||
|
||||
/// The order of the node in the UI layout.
|
||||
/// Nodes with a higher stack index are drawn on top of and recieve interactions before nodes with lower stack indices.
|
||||
pub const fn stack_index(&self) -> u32 {
|
||||
self.stack_index
|
||||
}
|
||||
|
||||
/// The calculated node size as width and height in logical pixels before rounding.
|
||||
///
|
||||
/// Automatically calculated by [`super::layout::ui_layout_system`].
|
||||
@ -92,6 +101,7 @@ impl Node {
|
||||
|
||||
impl Node {
|
||||
pub const DEFAULT: Self = Self {
|
||||
stack_index: 0,
|
||||
calculated_size: Vec2::ZERO,
|
||||
outline_width: 0.,
|
||||
outline_offset: 0.,
|
||||
|
Loading…
Reference in New Issue
Block a user