mirror of
https://github.com/bevyengine/bevy
synced 2024-12-21 02:23:08 +00:00
d2a07f9f72
# Objective Add a way to use the gizmo API in a retained manner, for increased performance. ## Solution - Move gizmo API from `Gizmos` to `GizmoBuffer`, ~ab~using `Deref` to keep usage the same as before. - Merge non-strip and strip variant of `LineGizmo` into one, storing the data in a `GizmoBuffer` to have the same API for retained `LineGizmo`s. ### Review guide - The meat of the changes are in `lib.rs`, `retained.rs`, `gizmos.rs`, `pipeline_3d.rs` and `pipeline_2d.rs` - The other files contain almost exclusively the churn from moving the gizmo API from `Gizmos` to `GizmoBuffer` ## Testing ### Performance Performance compared to the immediate mode API is from 65 to 80 times better for static lines. ``` 7900 XTX, 3700X 1707.9k lines/ms: gizmos_retained (21.3ms) 3488.5k lines/ms: gizmos_retained_continuous_polyline (31.3ms) 0.5k lines/ms: gizmos_retained_separate (97.7ms) 3054.9k lines/ms: bevy_polyline_retained_nan (16.8ms) 3596.3k lines/ms: bevy_polyline_retained_continuous_polyline (14.2ms) 0.6k lines/ms: bevy_polyline_retained_separate (78.9ms) 26.9k lines/ms: gizmos_immediate (14.9ms) 43.8k lines/ms: gizmos_immediate_continuous_polyline (18.3ms) ``` Looks like performance is good enough, being close to par with `bevy_polyline`. Benchmarks can be found here: This branch: https://github.com/tim-blackbird/line_racing/tree/retained-gizmos Bevy 0.14: https://github.com/DGriffin91/line_racing ## Showcase ```rust fn setup( mut commands: Commands, mut gizmo_assets: ResMut<Assets<GizmoAsset>> ) { let mut gizmo = GizmoAsset::default(); // A sphere made out of one million lines! gizmo .sphere(default(), 1., CRIMSON) .resolution(1_000_000 / 3); commands.spawn(Gizmo { handle: gizmo_assets.add(gizmo), ..default() }); } ``` ## Follow-up work - Port over to the retained rendering world proper - Calculate visibility and cull `Gizmo`s
138 lines
4.4 KiB
Rust
138 lines
4.4 KiB
Rust
//! This module is for 'retained' alternatives to the 'immediate mode' [`Gizmos`](crate::gizmos::Gizmos) system parameter.
|
|
|
|
use core::ops::{Deref, DerefMut};
|
|
|
|
use bevy_asset::Handle;
|
|
use bevy_ecs::component::{require, Component};
|
|
use bevy_reflect::Reflect;
|
|
use bevy_transform::components::Transform;
|
|
|
|
#[cfg(feature = "bevy_render")]
|
|
use {
|
|
crate::{config::GizmoLineJoint, LineGizmoUniform},
|
|
bevy_ecs::{
|
|
entity::Entity,
|
|
system::{Commands, Local, Query},
|
|
},
|
|
bevy_render::{view::RenderLayers, Extract},
|
|
bevy_transform::components::GlobalTransform,
|
|
};
|
|
|
|
use crate::{
|
|
config::{ErasedGizmoConfigGroup, GizmoLineConfig},
|
|
gizmos::GizmoBuffer,
|
|
GizmoAsset,
|
|
};
|
|
|
|
impl Deref for GizmoAsset {
|
|
type Target = GizmoBuffer<ErasedGizmoConfigGroup, ()>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.buffer
|
|
}
|
|
}
|
|
|
|
impl DerefMut for GizmoAsset {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
&mut self.buffer
|
|
}
|
|
}
|
|
|
|
/// A component that draws the gizmos of a [`GizmoAsset`].
|
|
///
|
|
/// When drawing a greater number of static lines a [`Gizmo`] component can
|
|
/// have far better performance than the [`Gizmos`] system parameter,
|
|
/// but the system parameter will perform better for smaller lines that update often.
|
|
///
|
|
/// ## Example
|
|
/// ```
|
|
/// # use bevy_ecs::prelude::*;
|
|
/// # use bevy_gizmos::prelude::*;
|
|
/// # use bevy_asset::prelude::*;
|
|
/// # use bevy_color::palettes::css::*;
|
|
/// # use bevy_utils::default;
|
|
/// # use bevy_math::prelude::*;
|
|
/// fn system(
|
|
/// mut commands: Commands,
|
|
/// mut gizmo_assets: ResMut<Assets<GizmoAsset>>,
|
|
/// ) {
|
|
/// let mut gizmo = GizmoAsset::default();
|
|
///
|
|
/// gizmo.sphere(Vec3::ZERO, 1., RED);
|
|
///
|
|
/// commands.spawn(Gizmo {
|
|
/// handle: gizmo_assets.add(gizmo),
|
|
/// line_config: GizmoLineConfig {
|
|
/// width: 4.,
|
|
/// ..default()
|
|
/// },
|
|
/// ..default()
|
|
/// });
|
|
/// }
|
|
/// ```
|
|
///
|
|
/// [`Gizmos`]: crate::gizmos::Gizmos
|
|
#[derive(Component, Clone, Debug, Default, Reflect)]
|
|
#[require(Transform)]
|
|
pub struct Gizmo {
|
|
/// The handle to the gizmo to draw.
|
|
pub handle: Handle<GizmoAsset>,
|
|
/// The line specific configuration for this gizmo.
|
|
pub line_config: GizmoLineConfig,
|
|
/// How closer to the camera than real geometry the gizmo should be.
|
|
///
|
|
/// In 2D this setting has no effect and is effectively always -1.
|
|
///
|
|
/// Value between -1 and 1 (inclusive).
|
|
/// * 0 means that there is no change to the gizmo position when rendering
|
|
/// * 1 means it is furthest away from camera as possible
|
|
/// * -1 means that it will always render in front of other things.
|
|
///
|
|
/// This is typically useful if you are drawing wireframes on top of polygons
|
|
/// and your wireframe is z-fighting (flickering on/off) with your main model.
|
|
/// You would set this value to a negative number close to 0.
|
|
pub depth_bias: f32,
|
|
}
|
|
|
|
#[cfg(feature = "bevy_render")]
|
|
pub(crate) fn extract_linegizmos(
|
|
mut commands: Commands,
|
|
mut previous_len: Local<usize>,
|
|
query: Extract<Query<(Entity, &Gizmo, &GlobalTransform, Option<&RenderLayers>)>>,
|
|
) {
|
|
use bevy_math::Affine3;
|
|
use bevy_render::sync_world::{MainEntity, TemporaryRenderEntity};
|
|
|
|
let mut values = Vec::with_capacity(*previous_len);
|
|
for (entity, gizmo, transform, render_layers) in &query {
|
|
let joints_resolution = if let GizmoLineJoint::Round(resolution) = gizmo.line_config.joints
|
|
{
|
|
resolution
|
|
} else {
|
|
0
|
|
};
|
|
|
|
values.push((
|
|
LineGizmoUniform {
|
|
world_from_local: Affine3::from(&transform.affine()).to_transpose(),
|
|
line_width: gizmo.line_config.width,
|
|
depth_bias: gizmo.depth_bias,
|
|
joints_resolution,
|
|
#[cfg(feature = "webgl")]
|
|
_padding: Default::default(),
|
|
},
|
|
#[cfg(any(feature = "bevy_pbr", feature = "bevy_sprite"))]
|
|
crate::config::GizmoMeshConfig {
|
|
line_perspective: gizmo.line_config.perspective,
|
|
line_style: gizmo.line_config.style,
|
|
line_joints: gizmo.line_config.joints,
|
|
render_layers: render_layers.cloned().unwrap_or_default(),
|
|
handle: gizmo.handle.clone_weak(),
|
|
},
|
|
MainEntity::from(entity),
|
|
TemporaryRenderEntity,
|
|
));
|
|
}
|
|
*previous_len = values.len();
|
|
commands.spawn_batch(values);
|
|
}
|