Skip to content

Commit

Permalink
extract node 3d to core pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
IceSentry committed Feb 25, 2023
1 parent 1230d2b commit ef546ce
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 93 deletions.
1 change: 1 addition & 0 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
109 changes: 109 additions & 0 deletions crates/bevy_core_pipeline/src/gizmo_3d/mod.rs
Original file line number Diff line number Diff line change
@@ -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::<DrawFunctions<GizmoLine3d>>()
.add_system(sort_phase_system::<GizmoLine3d>)
.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::<RenderGraph>();
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<FloatOrd>;

#[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<Query<(Entity, &Camera), With<Camera3d>>>,
) {
for (entity, camera) in &cameras_3d {
if camera.is_active {
commands
.get_or_spawn(entity)
.insert(RenderPhase::<GizmoLine3d>::default());
}
}
}
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -32,7 +34,7 @@ impl GizmoNode3d {
}
}

impl Node for GizmoNode3d {
impl Node for Gizmo3dNode {
fn input(&self) -> Vec<SlotInfo> {
vec![SlotInfo::new(Self::IN_VIEW, SlotType::Entity)]
}
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_core_pipeline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 6 additions & 26 deletions crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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,
};
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -83,28 +83,8 @@ impl Plugin for GizmoPlugin {
render_app
.init_resource::<GizmoPipeline3d>()
.init_resource::<SpecializedMeshPipelines<GizmoPipeline3d>>()
.init_resource::<DrawFunctions<GizmoLine3d>>()
.add_render_command::<GizmoLine3d, DrawGizmoLines>()
.add_system(sort_phase_system::<GizmoLine3d>)
.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::<RenderGraph>();
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);
}
}
}
Expand Down
63 changes: 2 additions & 61 deletions crates/bevy_gizmos/src/pipeline_3d.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -25,7 +20,6 @@ use bevy_render::{
texture::BevyDefault,
view::Msaa,
};
use bevy_utils::FloatOrd;

use crate::{GizmoConfig, GizmoMesh, LINE_SHADER_HANDLE};

Expand Down Expand Up @@ -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<FloatOrd>;

#[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<Query<(Entity, &Camera), With<Camera3d>>>,
) {
for (entity, camera) in &cameras_3d {
if camera.is_active {
commands
.get_or_spawn(entity)
.insert(RenderPhase::<GizmoLine3d>::default());
}
}
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn queue_gizmos_3d(
draw_functions: Res<DrawFunctions<GizmoLine3d>>,
Expand Down

0 comments on commit ef546ce

Please sign in to comment.