From bcfc086f3d7d67271380a216b8bdd8e0b0f57a0f Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 7 Feb 2025 23:43:45 -0800 Subject: [PATCH] Include the material bind group in the shadow batch set key. (#17738) Right now, meshes aren't grouped together based on the bindless texture slab when drawing shadows. This manifests itself as flickering in Bistro. I believe that there are two causes of this: 1. Alpha masked shadows may try to sample from the wrong texture, causing the alpha mask to appear and disappear. 2. Objects may try to sample from the blank textures that we pad out the bindless slabs with, causing them to vanish intermittently. This commit fixes the issue by including the material bind group ID as part of the shadow batch set key, just as we do for the prepass and main pass. --- crates/bevy_pbr/src/render/light.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index 5cdea0627b7b7..380eb87bbfde0 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -1830,6 +1830,8 @@ pub fn specialize_shadows( pub fn queue_shadows( shadow_draw_functions: Res>, render_mesh_instances: Res, + render_materials: Res>>, + render_material_instances: Res>, mut shadow_render_phases: ResMut>, gpu_preprocessing_support: Res, mesh_allocator: Res, @@ -1900,12 +1902,20 @@ pub fn queue_shadows( continue; } + let Some(material_asset_id) = render_material_instances.get(&main_entity) else { + continue; + }; + let Some(material) = render_materials.get(*material_asset_id) else { + continue; + }; + let (vertex_slab, index_slab) = mesh_allocator.mesh_slabs(&mesh_instance.mesh_asset_id); let batch_set_key = ShadowBatchSetKey { pipeline: *pipeline_id, draw_function: draw_shadow_mesh, + material_bind_group_index: Some(material.binding.group.0), vertex_slab: vertex_slab.unwrap_or_default(), index_slab, }; @@ -1952,6 +1962,11 @@ pub struct ShadowBatchSetKey { /// The function used to draw. pub draw_function: DrawFunctionId, + /// The ID of a bind group specific to the material. + /// + /// In the case of PBR, this is the `MaterialBindGroupIndex`. + pub material_bind_group_index: Option, + /// The ID of the slab of GPU memory that contains vertex data. /// /// For non-mesh items, you can fill this with 0 if your items can be