bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Retained `Gizmo`s

Open tim-blackbird opened this issue 1 year ago • 13 comments

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 LineGizmos.

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

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 Gizmos

tim-blackbird avatar Sep 27 '24 15:09 tim-blackbird

It's still a bit rough, but it's now in a working state! Performance feels good, I'm going to plug it into https://github.com/DGriffin91/line_racing/ and have a look at some real numbers!

Feel free to try it out, but I'd hold off on reviews for now :)

tim-blackbird avatar Sep 27 '24 16:09 tim-blackbird

I've fixed some issues with this PR.

  • I botched the port to the retained render world initially. I've re-done the port with bare minimum effort using TemporaryRenderEntity to keep things simple here.
  • I initially had used the whole GizmoConfig in the LineGizmo component, but I overlooked the render layers setting in there. RenderLayers is a component so it would be confusing to have it inside another component here. To solve that I've split the line specific settings out into a LineGizmoConfig struct and re-used that inside both GizmoConfig and the LineGizmo component.
  • Line joints were not working. Had to re-route some data to fix that.

I also decided to add support for the Transform component in this PR after all, since it was easier that I expected.

tim-blackbird avatar Oct 07 '24 13:10 tim-blackbird

My idea was to expand the Gizmos parameter with more than just lines, but it made sense to me to make the retained versions separate? I'm not 100% sure on it

tim-blackbird avatar Oct 08 '24 22:10 tim-blackbird

I think I'd prefer waiting until we actually need them to be separate to show that in the name. Right now it just looks a bit off that there's a Line prefix there.

IceSentry avatar Oct 09 '24 03:10 IceSentry

Any thoughts on alternative names for LineGizmo?

  • Gizmo
  • RetainedGizmo
  • RetainedGizmos

I don't really like having 'retained' in the name, but besides just Gizmo that is all I can think of.

tim-blackbird avatar Oct 10 '24 20:10 tim-blackbird

I kinda like GizmoAsset because it uses the asset system and it doesn't have the retained naming, but I'm honestly not sure either.

IceSentry avatar Oct 10 '24 21:10 IceSentry

I kinda like GizmoAsset because it uses the asset system and it doesn't have the retained naming, but I'm honestly not sure either.

We just don't use FooAsset anywhere else, do we?

tychedelia avatar Oct 10 '24 21:10 tychedelia

We just don't use FooAsset anywhere else, do we?

No, but we do in this PR. We need a name for both the component (currently LineGizmo) and the asset (currently LineGizmoAsset)

edit: My preference is still LineGizmo, but I'm fine with just Gizmo as well. We will need to restructure things in the future anyways for text, billboards/images (#14995), meshes, picking support, etc.

tim-blackbird avatar Oct 10 '24 21:10 tim-blackbird

Any thoughts on alternative names for LineGizmo?

None, but I think Gizmo is a good idea to follow.

pablo-lua avatar Oct 10 '24 21:10 pablo-lua

I've thought about it a bit more and decided to go through with removing the 'line' naming from this PR, see the Rename commit(dbdb9ad) for the changes to naming and docs

tim-blackbird avatar Oct 12 '24 09:10 tim-blackbird

Bumping to 0.16, sorry. Rendering's stability is too poor to merge this right now.

alice-i-cecile avatar Oct 15 '24 02:10 alice-i-cecile

This is really nice.

I realize this is a bikeshed, but I'd like to point out that the name Gizmo truly makes no sense any more. This is just polyline rendering with an immediate and retained API, and helpers to convert primitive shapes into polylines.

aevyrie avatar Oct 16 '24 08:10 aevyrie

@tim-blackbird I like this and it's well-reviewed. Can you resolve merge conflicts and ping me to merge please?

alice-i-cecile avatar Dec 03 '24 17:12 alice-i-cecile

Thank you to everyone involved with the authoring or reviewing of this PR! This work is relatively important and needs release notes! Head over to https://github.com/bevyengine/bevy-website/issues/1963 if you'd like to help out.

alice-i-cecile avatar Mar 25 '25 20:03 alice-i-cecile