From 482e4f8a4746cd2f404b4a15b7835dc66d92f3b2 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Sun, 18 Jun 2023 22:35:09 -0400 Subject: [PATCH 1/5] fix normal prepass --- crates/bevy_pbr/src/render/pbr.wgsl | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/crates/bevy_pbr/src/render/pbr.wgsl b/crates/bevy_pbr/src/render/pbr.wgsl index e342823b8f7c4..12e0855ab6b44 100644 --- a/crates/bevy_pbr/src/render/pbr.wgsl +++ b/crates/bevy_pbr/src/render/pbr.wgsl @@ -109,18 +109,33 @@ fn fragment(in: FragmentInput) -> @location(0) vec4 { pbr_input.frag_coord = in.frag_coord; pbr_input.world_position = in.world_position; -#ifdef LOAD_PREPASS_NORMALS - pbr_input.world_normal = prepass_normal(in.frag_coord, 0u); -#else // LOAD_PREPASS_NORMALS pbr_input.world_normal = prepare_world_normal( in.world_normal, (material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u, in.is_front, ); -#endif // LOAD_PREPASS_NORMALS pbr_input.is_orthographic = is_orthographic; +#ifdef LOAD_PREPASS_NORMALS + let alpha_mode = material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS; + if alpha_mode == STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE || alpha_mode == STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK { + pbr_input.N = prepass_normal(in.frag_coord, 0u); + } else { + pbr_input.N = apply_normal_mapping( + material.flags, + pbr_input.world_normal, +#ifdef VERTEX_TANGENTS +#ifdef STANDARDMATERIAL_NORMAL_MAP + in.world_tangent, +#endif +#endif +#ifdef VERTEX_UVS + uv, +#endif + ); + } +#else LOAD_PREPASS_NORMALS pbr_input.N = apply_normal_mapping( material.flags, pbr_input.world_normal, @@ -133,12 +148,16 @@ fn fragment(in: FragmentInput) -> @location(0) vec4 { uv, #endif ); +#endif // LOAD_PREPASS_NORMALS + + pbr_input.V = V; pbr_input.occlusion = occlusion; pbr_input.flags = mesh.flags; output_color = pbr(pbr_input); + // return vec4(pbr_input.N, 1.0); } else { output_color = alpha_discard(material, output_color); } From 5c0f35ada1b03b5dee82daa7dfe5ffb6fd1af509 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Sun, 18 Jun 2023 22:45:16 -0400 Subject: [PATCH 2/5] cleaner ifdef --- crates/bevy_pbr/src/render/pbr.wgsl | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/crates/bevy_pbr/src/render/pbr.wgsl b/crates/bevy_pbr/src/render/pbr.wgsl index 12e0855ab6b44..494fa77c7fa64 100644 --- a/crates/bevy_pbr/src/render/pbr.wgsl +++ b/crates/bevy_pbr/src/render/pbr.wgsl @@ -122,6 +122,7 @@ fn fragment(in: FragmentInput) -> @location(0) vec4 { if alpha_mode == STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE || alpha_mode == STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK { pbr_input.N = prepass_normal(in.frag_coord, 0u); } else { +#endif pbr_input.N = apply_normal_mapping( material.flags, pbr_input.world_normal, @@ -134,22 +135,9 @@ fn fragment(in: FragmentInput) -> @location(0) vec4 { uv, #endif ); +#ifdef LOAD_PREPASS_NORMALS } -#else LOAD_PREPASS_NORMALS - pbr_input.N = apply_normal_mapping( - material.flags, - pbr_input.world_normal, -#ifdef VERTEX_TANGENTS -#ifdef STANDARDMATERIAL_NORMAL_MAP - in.world_tangent, -#endif #endif -#ifdef VERTEX_UVS - uv, -#endif - ); -#endif // LOAD_PREPASS_NORMALS - pbr_input.V = V; pbr_input.occlusion = occlusion; From 8bbd2fdb72051e952e8f5eea7a271366792ac8f3 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Sun, 18 Jun 2023 23:00:17 -0400 Subject: [PATCH 3/5] only load when msaa disabled --- crates/bevy_pbr/src/render/mesh.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index 89f95932ebc47..f3759f1645764 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -682,7 +682,7 @@ impl SpecializedMeshPipeline for MeshPipeline { let mut shader_defs = Vec::new(); let mut vertex_attributes = Vec::new(); - if key.contains(MeshPipelineKey::NORMAL_PREPASS) { + if key.contains(MeshPipelineKey::NORMAL_PREPASS) && key.msaa_samples() == 1 { shader_defs.push("LOAD_PREPASS_NORMALS".into()); } From 907dece6c0ccaf3ad06635f997a50d34f41a667a Mon Sep 17 00:00:00 2001 From: IceSentry Date: Sun, 18 Jun 2023 23:06:32 -0400 Subject: [PATCH 4/5] move check to specialize --- crates/bevy_pbr/src/render/pbr.wgsl | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/crates/bevy_pbr/src/render/pbr.wgsl b/crates/bevy_pbr/src/render/pbr.wgsl index 494fa77c7fa64..eb84895aa6fd3 100644 --- a/crates/bevy_pbr/src/render/pbr.wgsl +++ b/crates/bevy_pbr/src/render/pbr.wgsl @@ -118,11 +118,8 @@ fn fragment(in: FragmentInput) -> @location(0) vec4 { pbr_input.is_orthographic = is_orthographic; #ifdef LOAD_PREPASS_NORMALS - let alpha_mode = material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS; - if alpha_mode == STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE || alpha_mode == STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK { pbr_input.N = prepass_normal(in.frag_coord, 0u); - } else { -#endif +#else pbr_input.N = apply_normal_mapping( material.flags, pbr_input.world_normal, @@ -135,8 +132,6 @@ fn fragment(in: FragmentInput) -> @location(0) vec4 { uv, #endif ); -#ifdef LOAD_PREPASS_NORMALS - } #endif pbr_input.V = V; @@ -145,7 +140,6 @@ fn fragment(in: FragmentInput) -> @location(0) vec4 { pbr_input.flags = mesh.flags; output_color = pbr(pbr_input); - // return vec4(pbr_input.N, 1.0); } else { output_color = alpha_discard(material, output_color); } From 31017314401645f9d79bd73d4b655cbed7e6bc8b Mon Sep 17 00:00:00 2001 From: IceSentry Date: Sun, 18 Jun 2023 23:06:49 -0400 Subject: [PATCH 5/5] oops --- crates/bevy_pbr/src/render/mesh.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index f3759f1645764..20cb2a198666e 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -682,10 +682,6 @@ impl SpecializedMeshPipeline for MeshPipeline { let mut shader_defs = Vec::new(); let mut vertex_attributes = Vec::new(); - if key.contains(MeshPipelineKey::NORMAL_PREPASS) && key.msaa_samples() == 1 { - shader_defs.push("LOAD_PREPASS_NORMALS".into()); - } - if layout.contains(Mesh::ATTRIBUTE_POSITION) { shader_defs.push("VERTEX_POSITIONS".into()); vertex_attributes.push(Mesh::ATTRIBUTE_POSITION.at_shader_location(0)); @@ -747,6 +743,7 @@ impl SpecializedMeshPipeline for MeshPipeline { let (label, blend, depth_write_enabled); let pass = key.intersection(MeshPipelineKey::BLEND_RESERVED_BITS); + let mut is_opaque = false; if pass == MeshPipelineKey::BLEND_ALPHA { label = "alpha_blend_mesh_pipeline".into(); blend = Some(BlendState::ALPHA_BLENDING); @@ -783,6 +780,11 @@ impl SpecializedMeshPipeline for MeshPipeline { // the current fragment value in the output and the depth is written to the // depth buffer depth_write_enabled = true; + is_opaque = true; + } + + if key.contains(MeshPipelineKey::NORMAL_PREPASS) && key.msaa_samples() == 1 && is_opaque { + shader_defs.push("LOAD_PREPASS_NORMALS".into()); } if key.contains(MeshPipelineKey::TONEMAP_IN_SHADER) {