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

Fix meshlet interactions with regular shading passes #13816

Merged
merged 4 commits into from
Jun 21, 2024
Merged
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
12 changes: 6 additions & 6 deletions crates/bevy_core_pipeline/src/deferred/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,17 @@ impl ViewNode for DeferredGBufferPrepassNode {
let view_entity = graph.view_entity();
render_context.add_command_buffer_generation_task(move |render_device| {
#[cfg(feature = "trace")]
let _deferred_span = info_span!("deferred").entered();
let _deferred_span = info_span!("deferred_prepass").entered();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the rename to add _prepass everywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was a bit confusing seeing "deferred" in the profiler. This made it a bit clearer, and matched the node name.


// Command encoder setup
let mut command_encoder =
render_device.create_command_encoder(&CommandEncoderDescriptor {
label: Some("deferred_command_encoder"),
label: Some("deferred_prepass_command_encoder"),
});

// Render pass setup
let render_pass = command_encoder.begin_render_pass(&RenderPassDescriptor {
label: Some("deferred"),
label: Some("deferred_prepass"),
color_attachments: &color_attachments,
depth_stencil_attachment,
timestamp_writes: None,
Expand All @@ -148,20 +148,20 @@ impl ViewNode for DeferredGBufferPrepassNode {
|| !opaque_deferred_phase.unbatchable_keys.is_empty()
{
#[cfg(feature = "trace")]
let _opaque_prepass_span = info_span!("opaque_deferred").entered();
let _opaque_prepass_span = info_span!("opaque_deferred_prepass").entered();
opaque_deferred_phase.render(&mut render_pass, world, view_entity);
}

// Alpha masked draws
if !alpha_mask_deferred_phase.is_empty() {
#[cfg(feature = "trace")]
let _alpha_mask_deferred_span = info_span!("alpha_mask_deferred").entered();
let _alpha_mask_deferred_span = info_span!("alpha_mask_deferred_prepass").entered();
alpha_mask_deferred_phase.render(&mut render_pass, world, view_entity);
}

drop(render_pass);

// Copy prepass depth to the main depth texture
// After rendering to the view depth texture, copy it to the prepass depth texture
if let Some(prepass_depth_texture) = &view_prepass_textures.depth {
command_encoder.copy_texture_to_texture(
view_depth_texture.texture.as_image_copy(),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/prepass/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl ViewNode for PrepassNode {
pass_span.end(&mut render_pass);
drop(render_pass);

// Copy prepass depth to the main depth texture if deferred isn't going to
// After rendering to the view depth texture, copy it to the prepass depth texture if deferred isn't going to
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment makes sense for what the code does, but I'm not sure I understand when that would ever happen. If you use deferred + prepass, then you're using deferred so it will do the copy. If you just use the prepass without deferred, it should run before the main pass so I don't get why we would want to copy the view depth to the prepass depth. Shouldn't it be the other way around?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, is it because the meshlet prepass writes directly to the view depth?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think meshlets should be mentioned in that comment if that's the case.

Copy link
Contributor Author

@JMS55 JMS55 Jun 14, 2024

Choose a reason for hiding this comment

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

This is existing code, for the regular (non meshlet) prepass. We don't actually render directly to the prepass texture, we render to the view depth, and then copy the result into the prepass depth texture.

I was just updating the comment as it was inaccurate before.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, right, I remember now.

if deferred_prepass.is_none() {
if let Some(prepass_depth_texture) = &view_prepass_textures.depth {
command_encoder.copy_texture_to_texture(
Expand Down
21 changes: 15 additions & 6 deletions crates/bevy_pbr/src/meshlet/material_draw_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ use crate::{
MeshViewBindGroup, PrepassViewBindGroup, ViewFogUniformOffset, ViewLightProbesUniformOffset,
ViewLightsUniformOffset, ViewScreenSpaceReflectionsUniformOffset,
};
use bevy_core_pipeline::prepass::{PreviousViewUniformOffset, ViewPrepassTextures};
use bevy_ecs::{query::QueryItem, world::World};
use bevy_core_pipeline::prepass::{
MotionVectorPrepass, PreviousViewUniformOffset, ViewPrepassTextures,
};
use bevy_ecs::{
query::{Has, QueryItem},
world::World,
};
use bevy_render::{
camera::ExtractedCamera,
render_graph::{NodeRunError, RenderGraphContext, ViewNode},
Expand Down Expand Up @@ -138,7 +143,8 @@ impl ViewNode for MeshletPrepassNode {
&'static ExtractedCamera,
&'static ViewPrepassTextures,
&'static ViewUniformOffset,
Option<&'static PreviousViewUniformOffset>,
&'static PreviousViewUniformOffset,
Has<MotionVectorPrepass>,
&'static MeshletViewMaterialsPrepass,
&'static MeshletViewBindGroups,
&'static MeshletViewResources,
Expand All @@ -153,6 +159,7 @@ impl ViewNode for MeshletPrepassNode {
view_prepass_textures,
view_uniform_offset,
previous_view_uniform_offset,
view_has_motion_vector_prepass,
meshlet_view_materials,
meshlet_view_bind_groups,
meshlet_view_resources,
Expand Down Expand Up @@ -212,7 +219,7 @@ impl ViewNode for MeshletPrepassNode {
render_pass.set_camera_viewport(viewport);
}

if let Some(previous_view_uniform_offset) = previous_view_uniform_offset {
if view_has_motion_vector_prepass {
render_pass.set_bind_group(
0,
prepass_view_bind_group.motion_vectors.as_ref().unwrap(),
Expand Down Expand Up @@ -259,7 +266,8 @@ impl ViewNode for MeshletDeferredGBufferPrepassNode {
&'static ExtractedCamera,
&'static ViewPrepassTextures,
&'static ViewUniformOffset,
Option<&'static PreviousViewUniformOffset>,
&'static PreviousViewUniformOffset,
Has<MotionVectorPrepass>,
&'static MeshletViewMaterialsDeferredGBufferPrepass,
&'static MeshletViewBindGroups,
&'static MeshletViewResources,
Expand All @@ -274,6 +282,7 @@ impl ViewNode for MeshletDeferredGBufferPrepassNode {
view_prepass_textures,
view_uniform_offset,
previous_view_uniform_offset,
view_has_motion_vector_prepass,
meshlet_view_materials,
meshlet_view_bind_groups,
meshlet_view_resources,
Expand Down Expand Up @@ -338,7 +347,7 @@ impl ViewNode for MeshletDeferredGBufferPrepassNode {
render_pass.set_camera_viewport(viewport);
}

if let Some(previous_view_uniform_offset) = previous_view_uniform_offset {
if view_has_motion_vector_prepass {
render_pass.set_bind_group(
0,
prepass_view_bind_group.motion_vectors.as_ref().unwrap(),
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_pbr/src/meshlet/material_draw_prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ pub fn prepare_material_meshlet_meshes_main_opaque_pass<M: Material>(
continue;
};

if material.properties.alpha_mode != AlphaMode::Opaque
if material.properties.render_method != OpaqueRendererMethod::Forward
|| material.properties.alpha_mode != AlphaMode::Opaque
|| material.properties.reads_view_transmission_texture
{
continue;
Expand Down
12 changes: 8 additions & 4 deletions crates/bevy_pbr/src/meshlet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ const MESHLET_MESH_MATERIAL_SHADER_HANDLE: Handle<Shader> =
///
/// This plugin does not work on WASM.
///
/// Mixing forward+prepass and deferred rendering for opaque materials is not currently supported when using this plugin.
/// You must use one or the other by setting [`crate::DefaultOpaqueRendererMethod`].
/// Do not override [`crate::Material::opaque_render_method`] for any material when using this plugin.
///
/// ![A render of the Stanford dragon as a `MeshletMesh`](https://raw.githubusercontent.com/bevyengine/bevy/main/crates/bevy_pbr/src/meshlet/meshlet_preview.png)
pub struct MeshletPlugin;

Expand Down Expand Up @@ -206,18 +210,18 @@ impl Plugin for MeshletPlugin {
.add_render_graph_edges(
Core3d,
(
// TODO: Meshlet VisibilityBufferRaster should be after main pass when not using depth prepass
// Non-meshlet shading passes _must_ come before meshlet shading passes
NodePbr::ShadowPass,
Node3d::Prepass,
Node3d::DeferredPrepass,
NodeMeshlet::VisibilityBufferRasterPass,
NodeMeshlet::Prepass,
Node3d::Prepass,
NodeMeshlet::DeferredPrepass,
Node3d::DeferredPrepass,
Node3d::CopyDeferredLightingId,
Node3d::EndPrepasses,
Node3d::StartMainPass,
Node3d::MainOpaquePass,
NodeMeshlet::MainOpaquePass,
Node3d::MainOpaquePass,
Node3d::EndMainPass,
),
)
Expand Down