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::Outline;
|
||||||
use crate::{
|
use crate::{
|
||||||
prelude::UiCameraConfig, BackgroundColor, BorderColor, CalculatedClip, ContentSize, Node,
|
prelude::UiCameraConfig, BackgroundColor, BorderColor, CalculatedClip, ContentSize, Node,
|
||||||
Style, UiImage, UiScale, UiStack, UiTextureAtlasImage, Val,
|
Style, UiImage, UiScale, UiTextureAtlasImage, Val,
|
||||||
};
|
};
|
||||||
|
|
||||||
use bevy_app::prelude::*;
|
use bevy_app::prelude::*;
|
||||||
@ -152,7 +152,7 @@ fn get_ui_graph(render_app: &mut App) -> RenderGraph {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct ExtractedUiNode {
|
pub struct ExtractedUiNode {
|
||||||
pub stack_index: usize,
|
pub stack_index: u32,
|
||||||
pub transform: Mat4,
|
pub transform: Mat4,
|
||||||
pub color: Color,
|
pub color: Color,
|
||||||
pub rect: Rect,
|
pub rect: Rect,
|
||||||
@ -172,7 +172,6 @@ pub fn extract_atlas_uinodes(
|
|||||||
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
||||||
images: Extract<Res<Assets<Image>>>,
|
images: Extract<Res<Assets<Image>>>,
|
||||||
texture_atlases: Extract<Res<Assets<TextureAtlas>>>,
|
texture_atlases: Extract<Res<Assets<TextureAtlas>>>,
|
||||||
ui_stack: Extract<Res<UiStack>>,
|
|
||||||
uinode_query: Extract<
|
uinode_query: Extract<
|
||||||
Query<
|
Query<
|
||||||
(
|
(
|
||||||
@ -189,8 +188,7 @@ pub fn extract_atlas_uinodes(
|
|||||||
>,
|
>,
|
||||||
>,
|
>,
|
||||||
) {
|
) {
|
||||||
for (stack_index, entity) in ui_stack.uinodes.iter().enumerate() {
|
for (
|
||||||
if let Ok((
|
|
||||||
entity,
|
entity,
|
||||||
uinode,
|
uinode,
|
||||||
transform,
|
transform,
|
||||||
@ -199,7 +197,7 @@ pub fn extract_atlas_uinodes(
|
|||||||
clip,
|
clip,
|
||||||
texture_atlas_handle,
|
texture_atlas_handle,
|
||||||
atlas_image,
|
atlas_image,
|
||||||
)) = uinode_query.get(*entity)
|
) in uinode_query.iter()
|
||||||
{
|
{
|
||||||
// Skip invisible and completely transparent nodes
|
// Skip invisible and completely transparent nodes
|
||||||
if !view_visibility.get() || color.0.is_fully_transparent() {
|
if !view_visibility.get() || color.0.is_fully_transparent() {
|
||||||
@ -241,7 +239,7 @@ pub fn extract_atlas_uinodes(
|
|||||||
extracted_uinodes.uinodes.insert(
|
extracted_uinodes.uinodes.insert(
|
||||||
entity,
|
entity,
|
||||||
ExtractedUiNode {
|
ExtractedUiNode {
|
||||||
stack_index,
|
stack_index: uinode.stack_index,
|
||||||
transform: transform.compute_matrix(),
|
transform: transform.compute_matrix(),
|
||||||
color: color.0,
|
color: color.0,
|
||||||
rect: atlas_rect,
|
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 {
|
fn resolve_border_thickness(value: Val, parent_width: f32, viewport_size: Vec2) -> f32 {
|
||||||
match value {
|
match value {
|
||||||
@ -273,7 +270,6 @@ pub fn extract_uinode_borders(
|
|||||||
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
||||||
windows: Extract<Query<&Window, With<PrimaryWindow>>>,
|
windows: Extract<Query<&Window, With<PrimaryWindow>>>,
|
||||||
ui_scale: Extract<Res<UiScale>>,
|
ui_scale: Extract<Res<UiScale>>,
|
||||||
ui_stack: Extract<Res<UiStack>>,
|
|
||||||
uinode_query: Extract<
|
uinode_query: Extract<
|
||||||
Query<
|
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.
|
// so we have to divide by `UiScale` to get the size of the UI viewport.
|
||||||
/ ui_scale.0 as f32;
|
/ ui_scale.0 as f32;
|
||||||
|
|
||||||
for (stack_index, entity) in ui_stack.uinodes.iter().enumerate() {
|
for (node, global_transform, style, border_color, parent, view_visibility, clip) in
|
||||||
if let Ok((node, global_transform, style, border_color, parent, view_visibility, clip)) =
|
uinode_query.iter()
|
||||||
uinode_query.get(*entity)
|
|
||||||
{
|
{
|
||||||
// Skip invisible borders
|
// Skip invisible borders
|
||||||
if !view_visibility.get()
|
if !view_visibility.get()
|
||||||
@ -321,18 +316,12 @@ pub fn extract_uinode_borders(
|
|||||||
.unwrap_or(ui_logical_viewport_size.x);
|
.unwrap_or(ui_logical_viewport_size.x);
|
||||||
let left =
|
let left =
|
||||||
resolve_border_thickness(style.border.left, parent_width, ui_logical_viewport_size);
|
resolve_border_thickness(style.border.left, parent_width, ui_logical_viewport_size);
|
||||||
let right = resolve_border_thickness(
|
let right =
|
||||||
style.border.right,
|
resolve_border_thickness(style.border.right, parent_width, ui_logical_viewport_size);
|
||||||
parent_width,
|
|
||||||
ui_logical_viewport_size,
|
|
||||||
);
|
|
||||||
let top =
|
let top =
|
||||||
resolve_border_thickness(style.border.top, parent_width, ui_logical_viewport_size);
|
resolve_border_thickness(style.border.top, parent_width, ui_logical_viewport_size);
|
||||||
let bottom = resolve_border_thickness(
|
let bottom =
|
||||||
style.border.bottom,
|
resolve_border_thickness(style.border.bottom, parent_width, ui_logical_viewport_size);
|
||||||
parent_width,
|
|
||||||
ui_logical_viewport_size,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Calculate the border rects, ensuring no overlap.
|
// 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.
|
// 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(
|
extracted_uinodes.uinodes.insert(
|
||||||
commands.spawn_empty().id(),
|
commands.spawn_empty().id(),
|
||||||
ExtractedUiNode {
|
ExtractedUiNode {
|
||||||
stack_index,
|
stack_index: node.stack_index,
|
||||||
// This translates the uinode's transform to the center of the current border rectangle
|
// This translates the uinode's transform to the center of the current border rectangle
|
||||||
transform: transform * Mat4::from_translation(edge.center().extend(0.)),
|
transform: transform * Mat4::from_translation(edge.center().extend(0.)),
|
||||||
color: border_color.0,
|
color: border_color.0,
|
||||||
@ -389,12 +378,10 @@ pub fn extract_uinode_borders(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn extract_uinode_outlines(
|
pub fn extract_uinode_outlines(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
||||||
ui_stack: Extract<Res<UiStack>>,
|
|
||||||
uinode_query: Extract<
|
uinode_query: Extract<
|
||||||
Query<(
|
Query<(
|
||||||
&Node,
|
&Node,
|
||||||
@ -407,11 +394,7 @@ pub fn extract_uinode_outlines(
|
|||||||
clip_query: Query<&CalculatedClip>,
|
clip_query: Query<&CalculatedClip>,
|
||||||
) {
|
) {
|
||||||
let image = AssetId::<Image>::default();
|
let image = AssetId::<Image>::default();
|
||||||
|
for (node, global_transform, outline, view_visibility, maybe_parent) in uinode_query.iter() {
|
||||||
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)
|
|
||||||
{
|
|
||||||
// Skip invisible outlines
|
// Skip invisible outlines
|
||||||
if !view_visibility.get()
|
if !view_visibility.get()
|
||||||
|| outline.color.is_fully_transparent()
|
|| 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.
|
// 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
|
let clip =
|
||||||
.and_then(|parent| clip_query.get(parent.get()).ok().map(|clip| clip.clip));
|
maybe_parent.and_then(|parent| clip_query.get(parent.get()).ok().map(|clip| clip.clip));
|
||||||
|
|
||||||
// Calculate the outline rects.
|
// Calculate the outline rects.
|
||||||
let inner_rect =
|
let inner_rect = Rect::from_center_size(Vec2::ZERO, node.size() + 2. * node.outline_offset);
|
||||||
Rect::from_center_size(Vec2::ZERO, node.size() + 2. * node.outline_offset);
|
|
||||||
let outer_rect = inner_rect.inset(node.outline_width());
|
let outer_rect = inner_rect.inset(node.outline_width());
|
||||||
let outline_edges = [
|
let outline_edges = [
|
||||||
// Left edge
|
// Left edge
|
||||||
@ -466,7 +448,7 @@ pub fn extract_uinode_outlines(
|
|||||||
extracted_uinodes.uinodes.insert(
|
extracted_uinodes.uinodes.insert(
|
||||||
commands.spawn_empty().id(),
|
commands.spawn_empty().id(),
|
||||||
ExtractedUiNode {
|
ExtractedUiNode {
|
||||||
stack_index,
|
stack_index: node.stack_index,
|
||||||
// This translates the uinode's transform to the center of the current border rectangle
|
// This translates the uinode's transform to the center of the current border rectangle
|
||||||
transform: transform * Mat4::from_translation(edge.center().extend(0.)),
|
transform: transform * Mat4::from_translation(edge.center().extend(0.)),
|
||||||
color: outline.color,
|
color: outline.color,
|
||||||
@ -485,12 +467,10 @@ pub fn extract_uinode_outlines(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn extract_uinodes(
|
pub fn extract_uinodes(
|
||||||
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
||||||
images: Extract<Res<Assets<Image>>>,
|
images: Extract<Res<Assets<Image>>>,
|
||||||
ui_stack: Extract<Res<UiStack>>,
|
|
||||||
uinode_query: Extract<
|
uinode_query: Extract<
|
||||||
Query<
|
Query<
|
||||||
(
|
(
|
||||||
@ -506,9 +486,8 @@ pub fn extract_uinodes(
|
|||||||
>,
|
>,
|
||||||
>,
|
>,
|
||||||
) {
|
) {
|
||||||
for (stack_index, entity) in ui_stack.uinodes.iter().enumerate() {
|
for (entity, uinode, transform, color, maybe_image, view_visibility, clip) in
|
||||||
if let Ok((entity, uinode, transform, color, maybe_image, view_visibility, clip)) =
|
uinode_query.iter()
|
||||||
uinode_query.get(*entity)
|
|
||||||
{
|
{
|
||||||
// Skip invisible and completely transparent nodes
|
// Skip invisible and completely transparent nodes
|
||||||
if !view_visibility.get() || color.0.is_fully_transparent() {
|
if !view_visibility.get() || color.0.is_fully_transparent() {
|
||||||
@ -528,7 +507,7 @@ pub fn extract_uinodes(
|
|||||||
extracted_uinodes.uinodes.insert(
|
extracted_uinodes.uinodes.insert(
|
||||||
entity,
|
entity,
|
||||||
ExtractedUiNode {
|
ExtractedUiNode {
|
||||||
stack_index,
|
stack_index: uinode.stack_index,
|
||||||
transform: transform.compute_matrix(),
|
transform: transform.compute_matrix(),
|
||||||
color: color.0,
|
color: color.0,
|
||||||
rect: Rect {
|
rect: Rect {
|
||||||
@ -542,7 +521,6 @@ pub fn extract_uinodes(
|
|||||||
flip_y,
|
flip_y,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -625,7 +603,6 @@ pub fn extract_text_uinodes(
|
|||||||
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
|
||||||
texture_atlases: Extract<Res<Assets<TextureAtlas>>>,
|
texture_atlases: Extract<Res<Assets<TextureAtlas>>>,
|
||||||
windows: Extract<Query<&Window, With<PrimaryWindow>>>,
|
windows: Extract<Query<&Window, With<PrimaryWindow>>>,
|
||||||
ui_stack: Extract<Res<UiStack>>,
|
|
||||||
ui_scale: Extract<Res<UiScale>>,
|
ui_scale: Extract<Res<UiScale>>,
|
||||||
uinode_query: Extract<
|
uinode_query: Extract<
|
||||||
Query<(
|
Query<(
|
||||||
@ -647,9 +624,8 @@ pub fn extract_text_uinodes(
|
|||||||
|
|
||||||
let inverse_scale_factor = (scale_factor as f32).recip();
|
let inverse_scale_factor = (scale_factor as f32).recip();
|
||||||
|
|
||||||
for (stack_index, entity) in ui_stack.uinodes.iter().enumerate() {
|
for (uinode, global_transform, text, text_layout_info, view_visibility, clip) in
|
||||||
if let Ok((uinode, global_transform, text, text_layout_info, view_visibility, clip)) =
|
uinode_query.iter()
|
||||||
uinode_query.get(*entity)
|
|
||||||
{
|
{
|
||||||
// Skip if not visible or if size is set to zero (e.g. when a parent is set to `Display::None`)
|
// 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. {
|
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(
|
extracted_uinodes.uinodes.insert(
|
||||||
commands.spawn_empty().id(),
|
commands.spawn_empty().id(),
|
||||||
ExtractedUiNode {
|
ExtractedUiNode {
|
||||||
stack_index,
|
stack_index: uinode.stack_index,
|
||||||
transform: transform
|
transform: transform
|
||||||
* Mat4::from_translation(position.extend(0.) * inverse_scale_factor),
|
* Mat4::from_translation(position.extend(0.) * inverse_scale_factor),
|
||||||
color,
|
color,
|
||||||
@ -694,7 +670,6 @@ pub fn extract_text_uinodes(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Copy, Clone, Pod, Zeroable)]
|
#[derive(Copy, Clone, Pod, Zeroable)]
|
||||||
|
@ -35,6 +35,7 @@ pub fn ui_stack_system(
|
|||||||
root_node_query: Query<Entity, (With<Node>, Without<Parent>)>,
|
root_node_query: Query<Entity, (With<Node>, Without<Parent>)>,
|
||||||
zindex_query: Query<&ZIndex, With<Node>>,
|
zindex_query: Query<&ZIndex, With<Node>>,
|
||||||
children_query: Query<&Children>,
|
children_query: Query<&Children>,
|
||||||
|
mut update_query: Query<&mut Node>,
|
||||||
) {
|
) {
|
||||||
// Generate `StackingContext` tree
|
// Generate `StackingContext` tree
|
||||||
let mut global_context = StackingContext::default();
|
let mut global_context = StackingContext::default();
|
||||||
@ -55,6 +56,14 @@ pub fn ui_stack_system(
|
|||||||
ui_stack.uinodes.clear();
|
ui_stack.uinodes.clear();
|
||||||
ui_stack.uinodes.reserve(total_entry_count);
|
ui_stack.uinodes.reserve(total_entry_count);
|
||||||
fill_stack_recursively(&mut ui_stack.uinodes, &mut global_context);
|
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
|
/// Generate z-index based UI node tree
|
||||||
|
@ -14,9 +14,12 @@ use thiserror::Error;
|
|||||||
#[derive(Component, Debug, Copy, Clone, Reflect)]
|
#[derive(Component, Debug, Copy, Clone, Reflect)]
|
||||||
#[reflect(Component, Default)]
|
#[reflect(Component, Default)]
|
||||||
pub struct Node {
|
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,
|
pub(crate) calculated_size: Vec2,
|
||||||
/// The width of this node's outline.
|
/// The width of this node's outline.
|
||||||
/// If this value is `Auto`, negative or `0.` then no outline will be rendered.
|
/// If this value is `Auto`, negative or `0.` then no outline will be rendered.
|
||||||
@ -39,6 +42,12 @@ impl Node {
|
|||||||
self.calculated_size
|
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.
|
/// The calculated node size as width and height in logical pixels before rounding.
|
||||||
///
|
///
|
||||||
/// Automatically calculated by [`super::layout::ui_layout_system`].
|
/// Automatically calculated by [`super::layout::ui_layout_system`].
|
||||||
@ -92,6 +101,7 @@ impl Node {
|
|||||||
|
|
||||||
impl Node {
|
impl Node {
|
||||||
pub const DEFAULT: Self = Self {
|
pub const DEFAULT: Self = Self {
|
||||||
|
stack_index: 0,
|
||||||
calculated_size: Vec2::ZERO,
|
calculated_size: Vec2::ZERO,
|
||||||
outline_width: 0.,
|
outline_width: 0.,
|
||||||
outline_offset: 0.,
|
outline_offset: 0.,
|
||||||
|
Loading…
Reference in New Issue
Block a user