Skip to content

Commit 3f4938f

Browse files
committed
Add prepass_reads_material flag in material that sets PREPASS_READS_MATERIAL
1 parent 4b0579b commit 3f4938f

5 files changed

Lines changed: 32 additions & 2 deletions

File tree

crates/bevy_material/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ pub struct MaterialProperties {
9494
pub shadows_enabled: bool,
9595
/// Whether prepass is enabled for this material
9696
pub prepass_enabled: bool,
97+
/// Wether depth only prepass needs to read the material layout
98+
pub prepass_reads_material: bool,
9799
}
98100

99101
impl MaterialProperties {

crates/bevy_pbr/src/extended_material.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ pub trait MaterialExtension: Asset + AsBindGroup + Clone + Sized {
6161
true
6262
}
6363

64+
#[inline]
65+
fn prepass_reads_material() -> bool {
66+
false
67+
}
68+
6469
/// Returns this material's prepass vertex shader. If [`ShaderRef::Default`] is returned, the base material prepass vertex shader
6570
/// will be used.
6671
fn prepass_vertex_shader() -> ShaderRef {
@@ -355,6 +360,10 @@ impl<B: Material, E: MaterialExtension> Material for ExtendedMaterial<B, E> {
355360
E::enable_shadows()
356361
}
357362

363+
fn prepass_reads_material() -> bool {
364+
E::prepass_reads_material()
365+
}
366+
358367
fn prepass_vertex_shader() -> ShaderRef {
359368
match E::prepass_vertex_shader() {
360369
ShaderRef::Default => B::prepass_vertex_shader(),

crates/bevy_pbr/src/material.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ pub trait Material: Asset + AsBindGroup + Clone + Sized {
200200
true
201201
}
202202

203+
#[inline]
204+
fn prepass_reads_material() -> bool {
205+
false
206+
}
207+
203208
/// Returns this material's prepass vertex shader. If [`ShaderRef::Default`] is returned, the default prepass vertex shader
204209
/// will be used.
205210
///
@@ -1690,6 +1695,7 @@ where
16901695

16911696
let shadows_enabled = M::enable_shadows();
16921697
let prepass_enabled = M::enable_prepass();
1698+
let prepass_reads_material = M::prepass_reads_material();
16931699

16941700
let draw_opaque_pbr = opaque_draw_functions.read().id::<DrawMaterial>();
16951701
let draw_alpha_mask_pbr = alpha_mask_draw_functions.read().id::<DrawMaterial>();
@@ -1790,6 +1796,7 @@ where
17901796
material_key,
17911797
shadows_enabled,
17921798
prepass_enabled,
1799+
prepass_reads_material,
17931800
}),
17941801
})
17951802
}

crates/bevy_pbr/src/prepass/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ impl SpecializedMeshPipeline for PrepassPipelineSpecializer {
386386
}
387387

388388
fn is_depth_only_opaque_prepass(mesh_key: MeshPipelineKey) -> bool {
389-
mesh_key.intersection(MeshPipelineKey::ALL_PREPASS_BITS) == MeshPipelineKey::DEPTH_PREPASS
389+
!mesh_key.contains(MeshPipelineKey::PREPASS_READS_MATERIAL)
390+
&& mesh_key.intersection(MeshPipelineKey::ALL_PREPASS_BITS) == MeshPipelineKey::DEPTH_PREPASS
390391
}
391392

392393
impl PrepassPipeline {
@@ -421,7 +422,10 @@ impl PrepassPipeline {
421422
// or emulated by setting depth in the fragment shader for GPUs that don't support it natively.
422423
let emulate_unclipped_depth = mesh_key.contains(MeshPipelineKey::UNCLIPPED_DEPTH_ORTHO)
423424
&& !self.depth_clip_control_supported;
424-
if is_depth_only_opaque_prepass(mesh_key) && !emulate_unclipped_depth {
425+
if is_depth_only_opaque_prepass(mesh_key)
426+
&& !emulate_unclipped_depth
427+
&& !material_properties.prepass_reads_material
428+
{
425429
bind_group_layouts.push(self.empty_layout.clone());
426430
} else {
427431
bind_group_layouts.push(
@@ -1114,6 +1118,10 @@ pub(crate) fn specialize_prepass_material_meshes(
11141118
mesh_key |= MeshPipelineKey::VISIBILITY_RANGE_DITHER;
11151119
}
11161120

1121+
if material.properties.prepass_reads_material {
1122+
mesh_key |= MeshPipelineKey::PREPASS_READS_MATERIAL;
1123+
}
1124+
11171125
// If the previous frame has skins or morph targets, note that.
11181126
if motion_vector_prepass.is_some() {
11191127
if mesh_instance

crates/bevy_pbr/src/render/light.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,6 +2448,10 @@ pub(crate) fn specialize_shadows(
24482448
_ => MeshPipelineKey::NONE,
24492449
};
24502450

2451+
if material.properties.prepass_reads_material {
2452+
mesh_key |= MeshPipelineKey::PREPASS_READS_MATERIAL;
2453+
}
2454+
24512455
work_items.push(ShadowSpecializationWorkItem {
24522456
visible_entity: *visible_entity,
24532457
retained_view_entity: extracted_view_light.retained_view_entity,

0 commit comments

Comments
 (0)