Skip to content

Commit

Permalink
Include the material bind group in the shadow batch set key.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pcwalton committed Feb 8, 2025
1 parent 6ed3c32 commit 6592c94
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,8 @@ pub fn specialize_shadows<M: Material>(
pub fn queue_shadows<M: Material>(
shadow_draw_functions: Res<DrawFunctions<Shadow>>,
render_mesh_instances: Res<RenderMeshInstances>,
render_materials: Res<RenderAssets<PreparedMaterial<M>>>,
render_material_instances: Res<RenderMaterialInstances<M>>,
mut shadow_render_phases: ResMut<ViewBinnedRenderPhases<Shadow>>,
gpu_preprocessing_support: Res<GpuPreprocessingSupport>,
mesh_allocator: Res<MeshAllocator>,
Expand Down Expand Up @@ -1900,12 +1902,20 @@ pub fn queue_shadows<M: Material>(
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,
};
Expand Down Expand Up @@ -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<u32>,

/// 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
Expand Down

0 comments on commit 6592c94

Please sign in to comment.