Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Extract common RenderPhase code into render method #7013

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions crates/bevy_core_pipeline/src/core_2d/main_pass_2d_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_ecs::prelude::*;
use bevy_render::{
camera::ExtractedCamera,
render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType},
render_phase::{DrawFunctions, RenderPhase, TrackedRenderPass},
render_phase::RenderPhase,
render_resource::{LoadOp, Operations, RenderPassDescriptor},
renderer::RenderContext,
view::{ExtractedView, ViewTarget},
Expand Down Expand Up @@ -77,21 +77,13 @@ impl Node for MainPass2dNode {
depth_stencil_attachment: None,
};

let draw_functions = world.resource::<DrawFunctions<Transparent2d>>();

let render_pass = render_context
.command_encoder
.begin_render_pass(&pass_descriptor);

let mut draw_functions = draw_functions.write();
let mut tracked_pass = TrackedRenderPass::new(render_pass);
if let Some(viewport) = camera.viewport.as_ref() {
tracked_pass.set_camera_viewport(viewport);
}
for item in &transparent_phase.items {
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
draw_function.draw(world, &mut tracked_pass, view_entity, item);
}
transparent_phase.render(
world,
render_context,
view_entity,
camera.viewport.as_ref(),
pass_descriptor,
);
}

// WebGL2 quirk: if ending with a render pass with a custom viewport, the viewport isn't
Expand Down
65 changes: 22 additions & 43 deletions crates/bevy_core_pipeline/src/core_3d/main_pass_3d_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_ecs::prelude::*;
use bevy_render::{
camera::ExtractedCamera,
render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType},
render_phase::{DrawFunctions, RenderPhase, TrackedRenderPass},
render_phase::RenderPhase,
render_resource::{LoadOp, Operations, RenderPassDepthStencilAttachment, RenderPassDescriptor},
renderer::RenderContext,
view::{ExtractedView, ViewDepthTexture, ViewTarget},
Expand Down Expand Up @@ -95,20 +95,13 @@ impl Node for MainPass3dNode {
}),
};

let draw_functions = world.resource::<DrawFunctions<Opaque3d>>();

let render_pass = render_context
.command_encoder
.begin_render_pass(&pass_descriptor);
let mut draw_functions = draw_functions.write();
let mut tracked_pass = TrackedRenderPass::new(render_pass);
if let Some(viewport) = camera.viewport.as_ref() {
tracked_pass.set_camera_viewport(viewport);
}
for item in &opaque_phase.items {
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
draw_function.draw(world, &mut tracked_pass, view_entity, item);
}
opaque_phase.render(
world,
render_context,
view_entity,
camera.viewport.as_ref(),
pass_descriptor,
);
}

if !alpha_mask_phase.items.is_empty() {
Expand All @@ -134,20 +127,13 @@ impl Node for MainPass3dNode {
}),
};

let draw_functions = world.resource::<DrawFunctions<AlphaMask3d>>();

let render_pass = render_context
.command_encoder
.begin_render_pass(&pass_descriptor);
let mut draw_functions = draw_functions.write();
let mut tracked_pass = TrackedRenderPass::new(render_pass);
if let Some(viewport) = camera.viewport.as_ref() {
tracked_pass.set_camera_viewport(viewport);
}
for item in &alpha_mask_phase.items {
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
draw_function.draw(world, &mut tracked_pass, view_entity, item);
}
alpha_mask_phase.render(
world,
render_context,
view_entity,
camera.viewport.as_ref(),
pass_descriptor,
);
}

if !transparent_phase.items.is_empty() {
Expand Down Expand Up @@ -178,20 +164,13 @@ impl Node for MainPass3dNode {
}),
};

let draw_functions = world.resource::<DrawFunctions<Transparent3d>>();

let render_pass = render_context
.command_encoder
.begin_render_pass(&pass_descriptor);
let mut draw_functions = draw_functions.write();
let mut tracked_pass = TrackedRenderPass::new(render_pass);
if let Some(viewport) = camera.viewport.as_ref() {
tracked_pass.set_camera_viewport(viewport);
}
for item in &transparent_phase.items {
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
draw_function.draw(world, &mut tracked_pass, view_entity, item);
}
transparent_phase.render(
world,
render_context,
view_entity,
camera.viewport.as_ref(),
pass_descriptor,
);
}

// WebGL2 quirk: if ending with a render pass with a custom viewport, the viewport isn't
Expand Down
11 changes: 1 addition & 10 deletions crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1785,16 +1785,7 @@ impl Node for ShadowPassNode {
}),
};

let draw_functions = world.resource::<DrawFunctions<Shadow>>();
let render_pass = render_context
.command_encoder
.begin_render_pass(&pass_descriptor);
let mut draw_functions = draw_functions.write();
let mut tracked_pass = TrackedRenderPass::new(render_pass);
for item in &shadow_phase.items {
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
draw_function.draw(world, &mut tracked_pass, view_light_entity, item);
}
shadow_phase.render(world, render_context, view_entity, None, pass_descriptor);
}
}

Expand Down
31 changes: 31 additions & 0 deletions crates/bevy_render/src/render_phase/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
mod draw;
mod draw_state;

use bevy_ecs::entity::Entity;
pub use draw::*;
pub use draw_state::*;
use wgpu::RenderPassDescriptor;

use crate::camera::Viewport;
use crate::renderer::RenderContext;
use bevy_ecs::prelude::{Component, Query};
use bevy_ecs::world::World;

/// A resource to collect and sort draw requests for specific [`PhaseItems`](PhaseItem).
#[derive(Component)]
Expand All @@ -29,6 +34,32 @@ impl<I: PhaseItem> RenderPhase<I> {
pub fn sort(&mut self) {
I::sort(&mut self.items);
}

pub fn render(
&self,
world: &World,
render_context: &mut RenderContext,
view: Entity,
viewport: Option<&Viewport>,
pass_descriptor: RenderPassDescriptor,
) {
let render_pass = render_context
.command_encoder
.begin_render_pass(&pass_descriptor);
let mut render_pass = TrackedRenderPass::new(render_pass);

if let Some(viewport) = viewport {
render_pass.set_camera_viewport(viewport);
}

let draw_functions = world.resource::<DrawFunctions<I>>();
let mut draw_functions = draw_functions.write();

for item in &self.items {
let draw_function = draw_functions.get_mut(item.draw_function()).unwrap();
draw_function.draw(world, &mut render_pass, view, item);
}
}
}

impl<I: BatchedPhaseItem> RenderPhase<I> {
Expand Down
13 changes: 1 addition & 12 deletions crates/bevy_ui/src/render/render_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,7 @@ impl Node for UiPassNode {
depth_stencil_attachment: None,
};

let draw_functions = world.resource::<DrawFunctions<TransparentUi>>();

let render_pass = render_context
.command_encoder
.begin_render_pass(&pass_descriptor);

let mut draw_functions = draw_functions.write();
let mut tracked_pass = TrackedRenderPass::new(render_pass);
for item in &transparent_phase.items {
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
draw_function.draw(world, &mut tracked_pass, view_entity, item);
}
transparent_phase.render(world, render_context, view_entity, None, pass_descriptor);
Ok(())
}
}
Expand Down