Skip to content

Commit

Permalink
2d
Browse files Browse the repository at this point in the history
  • Loading branch information
IceSentry committed Feb 24, 2023
1 parent 60aa2b2 commit 192c329
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 33 deletions.
41 changes: 22 additions & 19 deletions crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ use bevy_sprite::{Mesh2dHandle, Mesh2dUniform};

pub mod gizmos;

mod node;
mod node_2d;
mod node_3d;

#[cfg(feature = "bevy_sprite")]
mod pipeline_2d;
#[cfg(feature = "bevy_pbr")]
mod pipeline_3d;

use crate::{gizmos::GizmoStorage, node::GizmoNode};
use crate::{gizmos::GizmoStorage, node_2d::GizmoNode2d, node_3d::GizmoNode3d};

/// The `bevy_gizmos` prelude.
pub mod prelude {
Expand Down Expand Up @@ -61,31 +62,33 @@ impl Plugin for GizmoPlugin {

#[cfg(feature = "bevy_sprite")]
{
use bevy_core_pipeline::core_2d::Transparent2d;
use pipeline_2d::*;

render_app
.add_render_command::<Transparent2d, DrawGizmoLines>()
.init_resource::<GizmoPipeline2d>()
.init_resource::<SpecializedMeshPipelines<GizmoPipeline2d>>()
.init_resource::<DrawFunctions<GizmoLine2d>>()
.add_render_command::<GizmoLine2d, DrawGizmoLines>()
.add_system(sort_phase_system::<GizmoLine2d>)
.add_system_to_schedule(ExtractSchedule, extract_gizmo_line_2d_camera_phase)
.add_system(queue_gizmos_2d.in_set(RenderSet::Queue));

let gizmo_node = GizmoNode::new(&mut render_app.world);
let gizmo_node = GizmoNode2d::new(&mut render_app.world);
let mut binding = render_app.world.resource_mut::<RenderGraph>();
let graph = binding.get_sub_graph_mut(core_2d::graph::NAME).unwrap();

graph.add_node(GizmoNode::NAME, gizmo_node);
graph.add_node(GizmoNode2d::NAME, gizmo_node);
graph.add_slot_edge(
graph.input_node().id,
core_2d::graph::input::VIEW_ENTITY,
GizmoNode::NAME,
GizmoNode::IN_VIEW,
GizmoNode2d::NAME,
GizmoNode2d::IN_VIEW,
);
graph.add_node_edge(
core_2d::graph::node::END_MAIN_PASS_POST_PROCESSING,
GizmoNode::NAME,
);
graph.add_node_edge(GizmoNode::NAME, core_2d::graph::node::UPSCALING);
// graph.add_node_edge(
// core_2d::graph::node::END_MAIN_PASS_POST_PROCESSING,
// GizmoNode2d::NAME,
// );
graph.add_node_edge(GizmoNode2d::NAME, core_2d::graph::node::UPSCALING);
}

#[cfg(feature = "bevy_pbr")]
Expand All @@ -101,22 +104,22 @@ impl Plugin for GizmoPlugin {
.add_system_to_schedule(ExtractSchedule, extract_gizmo_line_3d_camera_phase)
.add_system(queue_gizmos_3d.in_set(RenderSet::Queue));

let gizmo_node = GizmoNode::new(&mut render_app.world);
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(GizmoNode::NAME, gizmo_node);
graph.add_node(GizmoNode3d::NAME, gizmo_node);
graph.add_slot_edge(
graph.input_node().id,
core_3d::graph::input::VIEW_ENTITY,
GizmoNode::NAME,
GizmoNode::IN_VIEW,
GizmoNode3d::NAME,
GizmoNode3d::IN_VIEW,
);
graph.add_node_edge(
core_3d::graph::node::END_MAIN_PASS_POST_PROCESSING,
GizmoNode::NAME,
GizmoNode3d::NAME,
);
graph.add_node_edge(GizmoNode::NAME, core_3d::graph::node::UPSCALING);
graph.add_node_edge(GizmoNode3d::NAME, core_3d::graph::node::UPSCALING);
}
}
}
Expand Down
67 changes: 67 additions & 0 deletions crates/bevy_gizmos/src/node_2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use bevy_ecs::prelude::*;
use bevy_render::{
render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType},
render_phase::RenderPhase,
render_resource::{LoadOp, Operations, RenderPassDescriptor},
renderer::RenderContext,
view::{ExtractedView, ViewTarget},
};

use crate::pipeline_2d::GizmoLine2d;

pub struct GizmoNode2d {
view_query:
QueryState<(&'static ViewTarget, &'static RenderPhase<GizmoLine2d>), With<ExtractedView>>,
}

impl GizmoNode2d {
pub const IN_VIEW: &'static str = "view";
pub const NAME: &'static str = "gizmo_node_2d";

pub fn new(world: &mut World) -> Self {
Self {
view_query: QueryState::new(world),
}
}
}

impl Node for GizmoNode2d {
fn input(&self) -> Vec<SlotInfo> {
vec![SlotInfo::new(Self::IN_VIEW, SlotType::Entity)]
}

fn update(&mut self, world: &mut World) {
self.view_query.update_archetypes(world);
}

fn run(
&self,
graph: &mut RenderGraphContext,
render_context: &mut RenderContext,
world: &World,
) -> Result<(), NodeRunError> {
let view_entity = graph.get_input_entity(Self::IN_VIEW)?;
let Ok((
view_target,
gizmo_phase,
)) = self.view_query.get_manual(world, view_entity) else {
return Ok(());
};
{
#[cfg(feature = "trace")]
let _gizmo_line_2d_pass = info_span!("gizmo_line_2d_pass").entered();

let mut render_pass = render_context.begin_tracked_render_pass(RenderPassDescriptor {
label: Some("gizmo_line_2d"),
color_attachments: &[Some(view_target.get_color_attachment(Operations {
load: LoadOp::Load,
store: true,
}))],
depth_stencil_attachment: None,
});

gizmo_phase.render(&mut render_pass, world, view_entity);
}
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy_render::{

use crate::pipeline_3d::GizmoLine3d;

pub struct GizmoNode {
pub struct GizmoNode3d {
view_query: QueryState<
(
&'static ViewTarget,
Expand All @@ -21,9 +21,9 @@ pub struct GizmoNode {
>,
}

impl GizmoNode {
impl GizmoNode3d {
pub const IN_VIEW: &'static str = "view";
pub const NAME: &'static str = "gizmo_node";
pub const NAME: &'static str = "gizmo_node_2d";

pub fn new(world: &mut World) -> Self {
Self {
Expand All @@ -32,7 +32,7 @@ impl GizmoNode {
}
}

impl Node for GizmoNode {
impl Node for GizmoNode3d {
fn input(&self) -> Vec<SlotInfo> {
vec![SlotInfo::new(Self::IN_VIEW, SlotType::Entity)]
}
Expand All @@ -58,7 +58,7 @@ impl Node for GizmoNode {

{
#[cfg(feature = "trace")]
let _main_opaque_pass_3d_span = info_span!("gizmo_line_3d_pass").entered();
let _gizmo_line_3d_pass = info_span!("gizmo_line_3d_pass").entered();

let mut render_pass = render_context.begin_tracked_render_pass(RenderPassDescriptor {
label: Some("gizmo_line_3d"),
Expand Down
71 changes: 63 additions & 8 deletions crates/bevy_gizmos/src/pipeline_2d.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
use bevy_asset::Handle;
use bevy_core_pipeline::core_2d::Transparent2d;
use bevy_core_pipeline::prelude::Camera2d;
use bevy_ecs::{
prelude::Entity,
query::With,
system::{Query, Res, ResMut, Resource},
system::{Commands, Query, Res, ResMut, Resource},
world::{FromWorld, World},
};
use bevy_render::{
mesh::{Mesh, MeshVertexBufferLayout},
prelude::Camera,
render_asset::RenderAssets,
render_phase::{DrawFunctions, RenderPhase, SetItemPipeline},
render_phase::{
CachedRenderPipelinePhaseItem, DrawFunctionId, DrawFunctions, PhaseItem, RenderPhase,
SetItemPipeline,
},
render_resource::*,
texture::BevyDefault,
view::Msaa,
Extract,
};
use bevy_sprite::*;
use bevy_utils::FloatOrd;
Expand Down Expand Up @@ -92,18 +97,69 @@ pub(crate) type DrawGizmoLines = (
DrawMesh2d,
);

pub struct GizmoLine2d {
pub sort_key: FloatOrd,
pub pipeline: CachedRenderPipelineId,
pub entity: Entity,
pub draw_function: DrawFunctionId,
}

impl PhaseItem for GizmoLine2d {
type SortKey = FloatOrd;

#[inline]
fn entity(&self) -> Entity {
self.entity
}

#[inline]
fn sort_key(&self) -> Self::SortKey {
self.sort_key
}

#[inline]
fn draw_function(&self) -> DrawFunctionId {
self.draw_function
}

#[inline]
fn sort(items: &mut [Self]) {
items.sort_by_key(|item| item.sort_key());
}
}

impl CachedRenderPipelinePhaseItem for GizmoLine2d {
#[inline]
fn cached_pipeline(&self) -> CachedRenderPipelineId {
self.pipeline
}
}

pub fn extract_gizmo_line_2d_camera_phase(
mut commands: Commands,
cameras_2d: Extract<Query<(Entity, &Camera), With<Camera2d>>>,
) {
for (entity, camera) in &cameras_2d {
if camera.is_active {
commands
.get_or_spawn(entity)
.insert(RenderPhase::<GizmoLine2d>::default());
}
}
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn queue_gizmos_2d(
draw_functions: Res<DrawFunctions<Transparent2d>>,
draw_functions: Res<DrawFunctions<GizmoLine2d>>,
pipeline: Res<GizmoPipeline2d>,
pipeline_cache: Res<PipelineCache>,
mut specialized_pipelines: ResMut<SpecializedMeshPipelines<GizmoPipeline2d>>,
gpu_meshes: Res<RenderAssets<Mesh>>,
msaa: Res<Msaa>,
mesh_handles: Query<(Entity, &Mesh2dHandle), With<GizmoMesh>>,
mut views: Query<&mut RenderPhase<Transparent2d>>,
mut views: Query<&mut RenderPhase<GizmoLine2d>>,
) {
let draw_function = draw_functions.read().get_id::<DrawGizmoLines>().unwrap();
let draw_function = draw_functions.read().id::<DrawGizmoLines>();
let key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples());
for mut phase in &mut views {
for (entity, mesh_handle) in &mesh_handles {
Expand All @@ -113,12 +169,11 @@ pub(crate) fn queue_gizmos_2d(
let pipeline = specialized_pipelines
.specialize(&pipeline_cache, &pipeline, key, &mesh.layout)
.unwrap();
phase.add(Transparent2d {
phase.add(GizmoLine2d {
entity,
draw_function,
pipeline,
sort_key: FloatOrd(f32::MAX),
batch_range: None,
});
}
}
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_gizmos/src/pipeline_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ pub(crate) fn queue_gizmos_3d(
mut views: Query<(&ExtractedView, &mut RenderPhase<GizmoLine3d>)>,
) {
let draw_function = draw_functions.read().id::<DrawGizmoLines>();

let key = MeshPipelineKey::from_msaa_samples(msaa.samples());
for (view, mut phase) in &mut views {
let key = key | MeshPipelineKey::from_hdr(view.hdr);
Expand Down

0 comments on commit 192c329

Please sign in to comment.