Skip to content

TrackedRenderPass Ergonomics #7227

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

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
125 changes: 47 additions & 78 deletions crates/bevy_core_pipeline/src/bloom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,106 +230,75 @@ impl Node for BloomNode {
});

let view = &bloom_texture.view(0);
let mut downsampling_first_pass =
render_context.begin_tracked_render_pass(RenderPassDescriptor {
label: Some("bloom_downsampling_first_pass"),
color_attachments: &[Some(RenderPassColorAttachment {
view,
resolve_target: None,
ops: Operations::default(),
})],
depth_stencil_attachment: None,
});
downsampling_first_pass.set_render_pipeline(downsampling_first_pipeline);
downsampling_first_pass.set_bind_group(
0,
&downsampling_first_bind_group,
&[uniform_index.index()],
);
downsampling_first_pass.draw(0..3, 0..1);

render_context
.render_pass(view_entity)
.set_label("bloom_downsampling_first_pass")
.add_color_attachment(view)
.begin()
.set_pipeline(downsampling_first_pipeline)
.set_bind_group(0, &downsampling_first_bind_group, &[uniform_index.index()])
.draw(0..3, 0..1);
}

// Other downsample passes
for mip in 1..bloom_texture.mip_count {
let down_sampling_bind_group = &bind_groups.downsampling_bind_groups[mip as usize - 1];
let view = &bloom_texture.view(mip);
let mut downsampling_pass =
render_context.begin_tracked_render_pass(RenderPassDescriptor {
label: Some("bloom_downsampling_pass"),
color_attachments: &[Some(RenderPassColorAttachment {
view,
resolve_target: None,
ops: Operations::default(),
})],
depth_stencil_attachment: None,
});
downsampling_pass.set_render_pipeline(downsampling_pipeline);
downsampling_pass.set_bind_group(
0,
&bind_groups.downsampling_bind_groups[mip as usize - 1],
&[uniform_index.index()],
);
downsampling_pass.draw(0..3, 0..1);

render_context
.render_pass(view_entity)
.set_label("bloom_downsampling_pass")
.add_color_attachment(view)
.begin()
.set_pipeline(downsampling_pipeline)
.set_bind_group(0, down_sampling_bind_group, &[uniform_index.index()])
.draw(0..3, 0..1);
}

// Upsample passes except the final one
for mip in (1..bloom_texture.mip_count).rev() {
let view = &bloom_texture.view(mip - 1);
let mut upsampling_pass =
render_context.begin_tracked_render_pass(RenderPassDescriptor {
label: Some("bloom_upsampling_pass"),
color_attachments: &[Some(RenderPassColorAttachment {
view,
resolve_target: None,
ops: Operations {
load: LoadOp::Load,
store: true,
},
})],
depth_stencil_attachment: None,
});
upsampling_pass.set_render_pipeline(upsampling_pipeline);
upsampling_pass.set_bind_group(
0,
&bind_groups.upsampling_bind_groups[(bloom_texture.mip_count - mip - 1) as usize],
&[uniform_index.index()],
);
let blend = compute_blend_factor(
bloom_settings,
mip as f32,
(bloom_texture.mip_count - 1) as f32,
);
upsampling_pass.set_blend_constant(Color::rgb_linear(blend, blend, blend));
upsampling_pass.draw(0..3, 0..1);
let upsampling_bind_group =
&bind_groups.upsampling_bind_groups[(bloom_texture.mip_count - mip - 1) as usize];
let view = &bloom_texture.view(mip - 1);

render_context
.render_pass(view_entity)
.set_label("bloom_upsampling_pass")
.add_color_attachment(view)
.set_color_ops(LoadOp::Load, true)
.begin()
.set_pipeline(upsampling_pipeline)
.set_bind_group(0, upsampling_bind_group, &[uniform_index.index()])
.set_blend_constant(Color::rgb_linear(blend, blend, blend))
.draw(0..3, 0..1);
}

// Final upsample pass
// This is very similar to the above upsampling passes with the only difference
// being the pipeline (which itself is barely different) and the color attachment
{
let mut upsampling_final_pass =
render_context.begin_tracked_render_pass(RenderPassDescriptor {
label: Some("bloom_upsampling_final_pass"),
color_attachments: &[Some(view_target.get_unsampled_color_attachment(
Operations {
load: LoadOp::Load,
store: true,
},
))],
depth_stencil_attachment: None,
});
upsampling_final_pass.set_render_pipeline(upsampling_final_pipeline);
upsampling_final_pass.set_bind_group(
0,
&bind_groups.upsampling_bind_groups[(bloom_texture.mip_count - 1) as usize],
&[uniform_index.index()],
);
if let Some(viewport) = camera.viewport.as_ref() {
upsampling_final_pass.set_camera_viewport(viewport);
}
let blend =
compute_blend_factor(bloom_settings, 0.0, (bloom_texture.mip_count - 1) as f32);
upsampling_final_pass.set_blend_constant(Color::rgb_linear(blend, blend, blend));
upsampling_final_pass.draw(0..3, 0..1);
let upsample_final_bind_group =
&bind_groups.upsampling_bind_groups[(bloom_texture.mip_count - 1) as usize];

render_context
.render_pass(view_entity)
.set_label("bloom_upsampling_final_pass")
.add_view_target_unsampled(view_target)
.set_color_ops(LoadOp::Load, true)
.begin()
.set_camera_viewport(camera)
.set_pipeline(upsampling_final_pipeline)
.set_bind_group(0, upsample_final_bind_group, &[uniform_index.index()])
.set_blend_constant(Color::rgb_linear(blend, blend, blend))
.draw(0..3, 0..1);
}

render_context.command_encoder().pop_debug_group();
Expand Down
12 changes: 12 additions & 0 deletions crates/bevy_core_pipeline/src/clear_color.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::prelude::*;
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
use bevy_render::render_resource::{LoadOp, RawColor};
use bevy_render::{color::Color, extract_resource::ExtractResource};
use serde::{Deserialize, Serialize};

Expand All @@ -13,6 +14,17 @@ pub enum ClearColorConfig {
None,
}

impl ClearColorConfig {
#[inline]
pub fn load_op(&self, world: &World) -> LoadOp<RawColor> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Todo: needs documentation

match self {
ClearColorConfig::Default => LoadOp::Clear(world.resource::<ClearColor>().0.into()),
ClearColorConfig::Custom(color) => LoadOp::Clear((*color).into()),
ClearColorConfig::None => LoadOp::Load,
}
}
}

/// A [`Resource`] that stores the color that is used to clear the screen between frames.
///
/// This color appears as the "background" color for simple apps,
Expand Down
51 changes: 16 additions & 35 deletions crates/bevy_core_pipeline/src/core_2d/main_pass_2d_node.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use crate::{
clear_color::{ClearColor, ClearColorConfig},
core_2d::{camera_2d::Camera2d, Transparent2d},
};
use crate::core_2d::{camera_2d::Camera2d, Transparent2d};
use bevy_ecs::prelude::*;
use bevy_render::{
camera::ExtractedCamera,
render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType},
render_phase::RenderPhase,
render_resource::{LoadOp, Operations, RenderPassDescriptor},
renderer::RenderContext,
view::{ExtractedView, ViewTarget},
};
Expand Down Expand Up @@ -63,46 +59,31 @@ impl Node for MainPass2dNode {
#[cfg(feature = "trace")]
let _main_pass_2d = info_span!("main_pass_2d").entered();

let mut render_pass = render_context.begin_tracked_render_pass(RenderPassDescriptor {
label: Some("main_pass_2d"),
color_attachments: &[Some(target.get_color_attachment(Operations {
load: match camera_2d.clear_color {
ClearColorConfig::Default => {
LoadOp::Clear(world.resource::<ClearColor>().0.into())
}
ClearColorConfig::Custom(color) => LoadOp::Clear(color.into()),
ClearColorConfig::None => LoadOp::Load,
},
store: true,
}))],
depth_stencil_attachment: None,
});

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

transparent_phase.render(&mut render_pass, world, view_entity);
render_context
.render_pass(view_entity)
.set_label("main_pass_2d")
.add_view_target(target)
.set_color_ops(camera_2d.clear_color.load_op(world), true)
.begin()
.set_camera_viewport(camera)
.render_phase(transparent_phase, world);
}

// WebGL2 quirk: if ending with a render pass with a custom viewport, the viewport isn't
// reset for the next render pass so add an empty render pass without a custom viewport
#[cfg(feature = "webgl")]
if camera.viewport.is_some() {
use bevy_render::render_resource::LoadOp;

#[cfg(feature = "trace")]
let _reset_viewport_pass_2d = info_span!("reset_viewport_pass_2d").entered();
let pass_descriptor = RenderPassDescriptor {
label: Some("reset_viewport_pass_2d"),
color_attachments: &[Some(target.get_color_attachment(Operations {
load: LoadOp::Load,
store: true,
}))],
depth_stencil_attachment: None,
};

render_context
.command_encoder()
.begin_render_pass(&pass_descriptor);
.render_pass(view_entity)
.set_label("reset_view_port_pass_2d")
.add_view_target(target)
.set_color_ops(LoadOp::Load, true)
.begin();
}

Ok(())
Expand Down
Loading