From ef546ce8b22bb3ff8518c1f4997ac67055879c97 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Fri, 24 Feb 2023 19:54:49 -0500 Subject: [PATCH] extract node 3d to core pipeline --- crates/bevy_core_pipeline/src/core_3d/mod.rs | 1 + crates/bevy_core_pipeline/src/gizmo_3d/mod.rs | 109 ++++++++++++++++++ .../src/gizmo_3d/node.rs} | 14 ++- crates/bevy_core_pipeline/src/lib.rs | 1 + crates/bevy_gizmos/src/lib.rs | 32 +---- crates/bevy_gizmos/src/pipeline_3d.rs | 63 +--------- 6 files changed, 127 insertions(+), 93 deletions(-) create mode 100644 crates/bevy_core_pipeline/src/gizmo_3d/mod.rs rename crates/{bevy_gizmos/src/node_3d.rs => bevy_core_pipeline/src/gizmo_3d/node.rs} (91%) diff --git a/crates/bevy_core_pipeline/src/core_3d/mod.rs b/crates/bevy_core_pipeline/src/core_3d/mod.rs index d69f6c17b6d78..9d633c3292471 100644 --- a/crates/bevy_core_pipeline/src/core_3d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_3d/mod.rs @@ -12,6 +12,7 @@ pub mod graph { pub const BLOOM: &str = "bloom"; pub const TONEMAPPING: &str = "tonemapping"; pub const FXAA: &str = "fxaa"; + pub const GIZMO: &str = "gizmo"; pub const UPSCALING: &str = "upscaling"; pub const END_MAIN_PASS_POST_PROCESSING: &str = "end_main_pass_post_processing"; } diff --git a/crates/bevy_core_pipeline/src/gizmo_3d/mod.rs b/crates/bevy_core_pipeline/src/gizmo_3d/mod.rs new file mode 100644 index 0000000000000..6166b76ad1f2a --- /dev/null +++ b/crates/bevy_core_pipeline/src/gizmo_3d/mod.rs @@ -0,0 +1,109 @@ +use std::cmp::Reverse; + +use bevy_app::Plugin; +use bevy_ecs::{ + prelude::Entity, + query::With, + system::{Commands, Query}, +}; +use bevy_render::{ + prelude::Camera, + render_graph::RenderGraph, + render_phase::{ + sort_phase_system, CachedRenderPipelinePhaseItem, DrawFunctionId, DrawFunctions, PhaseItem, + RenderPhase, + }, + render_resource::*, + Extract, ExtractSchedule, RenderApp, +}; +use bevy_utils::FloatOrd; +use core_3d::Camera3d; + +use crate::core_3d; + +use self::node::Gizmo3dNode; + +mod node; + +pub struct Gizmo3dPlugin; + +impl Plugin for Gizmo3dPlugin { + fn build(&self, app: &mut bevy_app::App) { + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { return; }; + + render_app + .init_resource::>() + .add_system(sort_phase_system::) + .add_system_to_schedule(ExtractSchedule, extract_gizmo_line_3d_camera_phase); + + let gizmo_node = Gizmo3dNode::new(&mut render_app.world); + let mut binding = render_app.world.resource_mut::(); + let graph = binding.get_sub_graph_mut(core_3d::graph::NAME).unwrap(); + + graph.add_node(core_3d::graph::node::GIZMO, gizmo_node); + graph.add_slot_edge( + graph.input_node().id, + core_3d::graph::input::VIEW_ENTITY, + core_3d::graph::node::GIZMO, + Gizmo3dNode::IN_VIEW, + ); + graph.add_node_edge( + core_3d::graph::node::END_MAIN_PASS_POST_PROCESSING, + core_3d::graph::node::GIZMO, + ); + graph.add_node_edge(core_3d::graph::node::GIZMO, core_3d::graph::node::UPSCALING); + } +} + +pub struct GizmoLine3d { + pub distance: f32, + pub pipeline: CachedRenderPipelineId, + pub entity: Entity, + pub draw_function: DrawFunctionId, +} + +impl PhaseItem for GizmoLine3d { + // NOTE: Values increase towards the camera. Front-to-back ordering for opaque means we need a descending sort. + type SortKey = Reverse; + + #[inline] + fn entity(&self) -> Entity { + self.entity + } + + #[inline] + fn sort_key(&self) -> Self::SortKey { + Reverse(FloatOrd(self.distance)) + } + + #[inline] + fn draw_function(&self) -> DrawFunctionId { + self.draw_function + } + + #[inline] + fn sort(items: &mut [Self]) { + // Key negated to match reversed SortKey ordering + radsort::sort_by_key(items, |item| -item.distance); + } +} + +impl CachedRenderPipelinePhaseItem for GizmoLine3d { + #[inline] + fn cached_pipeline(&self) -> CachedRenderPipelineId { + self.pipeline + } +} + +pub fn extract_gizmo_line_3d_camera_phase( + mut commands: Commands, + cameras_3d: Extract>>, +) { + for (entity, camera) in &cameras_3d { + if camera.is_active { + commands + .get_or_spawn(entity) + .insert(RenderPhase::::default()); + } + } +} diff --git a/crates/bevy_gizmos/src/node_3d.rs b/crates/bevy_core_pipeline/src/gizmo_3d/node.rs similarity index 91% rename from crates/bevy_gizmos/src/node_3d.rs rename to crates/bevy_core_pipeline/src/gizmo_3d/node.rs index 2a232d73fe8a5..a1c48d5e7d7c9 100644 --- a/crates/bevy_gizmos/src/node_3d.rs +++ b/crates/bevy_core_pipeline/src/gizmo_3d/node.rs @@ -1,4 +1,3 @@ -use bevy_core_pipeline::core_3d::Camera3dDepthLoadOp; use bevy_ecs::prelude::*; use bevy_render::{ render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType}, @@ -7,10 +6,14 @@ use bevy_render::{ renderer::RenderContext, view::{ExtractedView, ViewDepthTexture, ViewTarget}, }; +#[cfg(feature = "trace")] +use bevy_utils::tracing::info_span; -use crate::pipeline_3d::GizmoLine3d; +use crate::core_3d::Camera3dDepthLoadOp; -pub struct GizmoNode3d { +use super::GizmoLine3d; + +pub struct Gizmo3dNode { view_query: QueryState< ( &'static ViewTarget, @@ -21,9 +24,8 @@ pub struct GizmoNode3d { >, } -impl GizmoNode3d { +impl Gizmo3dNode { pub const IN_VIEW: &'static str = "view"; - pub const NAME: &'static str = "gizmo_node_2d"; pub fn new(world: &mut World) -> Self { Self { @@ -32,7 +34,7 @@ impl GizmoNode3d { } } -impl Node for GizmoNode3d { +impl Node for Gizmo3dNode { fn input(&self) -> Vec { vec![SlotInfo::new(Self::IN_VIEW, SlotType::Entity)] } diff --git a/crates/bevy_core_pipeline/src/lib.rs b/crates/bevy_core_pipeline/src/lib.rs index d459dae6ff7c0..f300573fddbcc 100644 --- a/crates/bevy_core_pipeline/src/lib.rs +++ b/crates/bevy_core_pipeline/src/lib.rs @@ -5,6 +5,7 @@ pub mod core_3d; pub mod fullscreen_vertex_shader; pub mod fxaa; pub mod gizmo_2d; +pub mod gizmo_3d; pub mod prepass; pub mod tonemapping; pub mod upscaling; diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index e053f99e53f6c..1975d49b37800 100644 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -3,8 +3,8 @@ use std::mem; use bevy_app::{CoreSet, Plugin}; use bevy_asset::{load_internal_asset, Assets, Handle, HandleUntyped}; use bevy_core_pipeline::{ - core_3d, gizmo_2d::{Gizmo2dPlugin, GizmoLine2d}, + gizmo_3d::{Gizmo3dPlugin, GizmoLine3d}, }; use bevy_ecs::{ prelude::{Component, DetectChanges}, @@ -16,8 +16,7 @@ use bevy_math::Mat4; use bevy_reflect::TypeUuid; use bevy_render::{ mesh::Mesh, - render_graph::RenderGraph, - render_phase::{sort_phase_system, AddRenderCommand, DrawFunctions}, + render_phase::AddRenderCommand, render_resource::{PrimitiveTopology, Shader, SpecializedMeshPipelines}, Extract, ExtractSchedule, RenderApp, RenderSet, }; @@ -29,14 +28,12 @@ use bevy_sprite::{Mesh2dHandle, Mesh2dUniform}; pub mod gizmos; -mod node_3d; - #[cfg(feature = "bevy_sprite")] mod pipeline_2d; #[cfg(feature = "bevy_pbr")] mod pipeline_3d; -use crate::{gizmos::GizmoStorage, node_3d::GizmoNode3d}; +use crate::gizmos::GizmoStorage; /// The `bevy_gizmos` prelude. pub mod prelude { @@ -61,6 +58,9 @@ impl Plugin for GizmoPlugin { #[cfg(feature = "bevy_sprite")] app.add_plugin(Gizmo2dPlugin); + #[cfg(feature = "bevy_pbr")] + app.add_plugin(Gizmo3dPlugin); + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { return; }; render_app.add_system_to_schedule(ExtractSchedule, extract_gizmo_data); @@ -83,28 +83,8 @@ impl Plugin for GizmoPlugin { render_app .init_resource::() .init_resource::>() - .init_resource::>() .add_render_command::() - .add_system(sort_phase_system::) - .add_system_to_schedule(ExtractSchedule, extract_gizmo_line_3d_camera_phase) .add_system(queue_gizmos_3d.in_set(RenderSet::Queue)); - - let gizmo_node = GizmoNode3d::new(&mut render_app.world); - let mut binding = render_app.world.resource_mut::(); - let graph = binding.get_sub_graph_mut(core_3d::graph::NAME).unwrap(); - - graph.add_node(GizmoNode3d::NAME, gizmo_node); - graph.add_slot_edge( - graph.input_node().id, - core_3d::graph::input::VIEW_ENTITY, - GizmoNode3d::NAME, - GizmoNode3d::IN_VIEW, - ); - graph.add_node_edge( - core_3d::graph::node::END_MAIN_PASS_POST_PROCESSING, - GizmoNode3d::NAME, - ); - graph.add_node_edge(GizmoNode3d::NAME, core_3d::graph::node::UPSCALING); } } } diff --git a/crates/bevy_gizmos/src/pipeline_3d.rs b/crates/bevy_gizmos/src/pipeline_3d.rs index 1e5e100180b80..1e364f3260de1 100644 --- a/crates/bevy_gizmos/src/pipeline_3d.rs +++ b/crates/bevy_gizmos/src/pipeline_3d.rs @@ -1,21 +1,16 @@ -use std::cmp::Reverse; - use bevy_asset::Handle; -use bevy_core_pipeline::prelude::Camera3d; +use bevy_core_pipeline::gizmo_3d::GizmoLine3d; use bevy_ecs::{ entity::Entity, query::With, - system::{Commands, Query, Res, ResMut, Resource}, + system::{Query, Res, ResMut, Resource}, world::{FromWorld, World}, }; use bevy_pbr::*; use bevy_render::{ mesh::Mesh, - prelude::Camera, - render_phase::{CachedRenderPipelinePhaseItem, DrawFunctionId, PhaseItem}, render_resource::Shader, view::{ExtractedView, ViewTarget}, - Extract, }; use bevy_render::{ mesh::MeshVertexBufferLayout, @@ -25,7 +20,6 @@ use bevy_render::{ texture::BevyDefault, view::Msaa, }; -use bevy_utils::FloatOrd; use crate::{GizmoConfig, GizmoMesh, LINE_SHADER_HANDLE}; @@ -159,59 +153,6 @@ pub(crate) type DrawGizmoLines = ( DrawMesh, ); -pub struct GizmoLine3d { - pub distance: f32, - pub pipeline: CachedRenderPipelineId, - pub entity: Entity, - pub draw_function: DrawFunctionId, -} - -impl PhaseItem for GizmoLine3d { - // NOTE: Values increase towards the camera. Front-to-back ordering for opaque means we need a descending sort. - type SortKey = Reverse; - - #[inline] - fn entity(&self) -> Entity { - self.entity - } - - #[inline] - fn sort_key(&self) -> Self::SortKey { - Reverse(FloatOrd(self.distance)) - } - - #[inline] - fn draw_function(&self) -> DrawFunctionId { - self.draw_function - } - - #[inline] - fn sort(items: &mut [Self]) { - // Key negated to match reversed SortKey ordering - radsort::sort_by_key(items, |item| -item.distance); - } -} - -impl CachedRenderPipelinePhaseItem for GizmoLine3d { - #[inline] - fn cached_pipeline(&self) -> CachedRenderPipelineId { - self.pipeline - } -} - -pub fn extract_gizmo_line_3d_camera_phase( - mut commands: Commands, - cameras_3d: Extract>>, -) { - for (entity, camera) in &cameras_3d { - if camera.is_active { - commands - .get_or_spawn(entity) - .insert(RenderPhase::::default()); - } - } -} - #[allow(clippy::too_many_arguments)] pub(crate) fn queue_gizmos_3d( draw_functions: Res>,