bevy/crates/bevy_pbr/src/render
Máté Homolya 9e11e96a59
Fix false positive GPU frustum culling (#17939)
# Objective

Fix incorrect mesh culling where objects (particularly directional
shadows) were being incorrectly culled during the early preprocessing
phase. The issue manifested specifically on Apple M1 GPUs but not on
newer devices like the M4. The bug was in the
`view_frustum_intersects_obb` function, where including the w component
(plane distance) in the dot product calculations led to false positive
culling results. This caused objects to be incorrectly culled before
shadow casting could begin.

## Issue Details
The problem of missing shadows is reproducible on Apple M1 GPUs as of
this commit (bisected):

```
00722b8d0 Make indirect drawing opt-out instead of opt-in, enabling multidraw by default. (#16757)
```

and as recent as this commit:

```
c818c9214 Add option to animate materials in many_cubes (#17927)
```

- The frustum culling calculation incorrectly included the w component
(plane distance) when transforming basis vectors
- The relative radius calculation should only consider directional
transformation (xyz), not positional information (w)
- This caused false positive culling specifically on M1 devices likely
due to different device-specific floating-point behavior
- When objects were incorrectly culled, `early_instance_count` never
incremented, leading to missing geometry in the shadow pass

## Testing

- Tested on M1 and M4 devices to verify the fix
- Verified shadows and geometry render correctly on both platforms
- Confirmed the solution matches the existing Rust implementation's
behavior for calculating the relative radius:
c818c92143/crates/bevy_render/src/primitives/mod.rs (L77-L87)
- The fix resolves a mathematical error in the frustum culling
calculation while maintaining correct culling behavior across all
platforms.

---

## Showcase

`c818c9214`
<img width="1284" alt="c818c9214"
src="https://github.com/user-attachments/assets/fe1c7ea9-b13d-422e-b12d-f1cd74475213"
/>

`mate-h/frustum-cull-fix`
<img width="1283" alt="frustum-cull-fix"
src="https://github.com/user-attachments/assets/8a9ccb2a-64b6-4d5e-a17d-ac4798da5b51"
/>
2025-02-20 05:35:21 +00:00
..
build_indirect_params.wgsl Split out the IndirectParametersMetadata into CPU-populated and GPU-populated buffers. (#17863) 2025-02-18 00:53:44 +00:00
clustered_forward.wgsl Implement basic clustered decal projectors. (#17315) 2025-01-26 20:13:39 +00:00
fog.rs Weak handle migration (#17695) 2025-02-05 22:44:20 +00:00
fog.wgsl update shader imports (#10180) 2023-10-21 11:51:58 +00:00
forward_io.wgsl Add UV channel selection to StandardMaterial (#13200) 2024-05-13 18:23:09 +00:00
gpu_preprocess.rs Fix motion vector computation after #17688. (#17717) 2025-02-18 09:34:19 +00:00
light.rs Make the specialized pipeline cache two-level. (#17915) 2025-02-18 07:23:33 +00:00
mesh_bindings.rs Allow clippy::too_many_arguments to lint without warnings (#17249) 2025-01-09 07:26:15 +00:00
mesh_bindings.wgsl Fix meshlet shaders for bindless mode. (#16825) 2024-12-24 02:39:18 +00:00
mesh_functions.wgsl gate get_tag behind ndef MESHLET_MESH_MATERIAL_PASS (#17809) 2025-02-11 22:36:17 +00:00
mesh_preprocess.wgsl Fix false positive GPU frustum culling (#17939) 2025-02-20 05:35:21 +00:00
mesh_types.wgsl Retain skins from frame to frame. (#17818) 2025-02-18 00:56:04 +00:00
mesh_view_bindings.rs Use target_abi = "sim" instead of ios_simulator feature (#17702) 2025-02-11 23:01:26 +00:00
mesh_view_bindings.wgsl Implement basic clustered decal projectors. (#17315) 2025-01-26 20:13:39 +00:00
mesh_view_types.wgsl Implement basic clustered decal projectors. (#17315) 2025-01-26 20:13:39 +00:00
mesh.rs Fix motion vector computation after #17688. (#17717) 2025-02-18 09:34:19 +00:00
mesh.wgsl Batch skinned meshes on platforms where storage buffers are available. (#16599) 2024-12-10 17:50:03 +00:00
mod.rs Retain skins from frame to frame. (#17818) 2025-02-18 00:56:04 +00:00
morph.rs Type safe retained render world (#15756) 2024-10-10 18:47:04 +00:00
morph.wgsl Implement motion vectors and TAA for skinned meshes and meshes with morph targets. (#13572) 2024-05-31 17:02:28 +00:00
occlusion_culling.wgsl Implement experimental GPU two-phase occlusion culling for the standard 3D mesh pipeline. (#17413) 2025-01-27 05:02:46 +00:00
parallax_mapping.wgsl Fix meshlet shaders for bindless mode. (#16825) 2024-12-24 02:39:18 +00:00
pbr_ambient.wgsl Use “specular occlusion” term to consistently extinguish fresnel on Ambient and Environment Map lights (#10182) 2023-10-23 03:26:20 +00:00
pbr_bindings.wgsl Add support for specular tints and maps per the KHR_materials_specular glTF extension. (#14069) 2025-01-26 20:38:46 +00:00
pbr_fragment.wgsl Fix meshlets when bindless disabled. (#17770) 2025-02-10 09:46:10 +00:00
pbr_functions.wgsl Update typos to 1.29.6 (#17850) 2025-02-13 19:44:47 +00:00
pbr_lighting.wgsl Introduce support for mixed lighting by allowing lights to opt out of contributing diffuse light to lightmapped objects. (#16761) 2024-12-16 23:48:33 +00:00
pbr_prepass_functions.wgsl Implement bindless lightmaps. (#16653) 2024-12-16 23:37:06 +00:00
pbr_prepass.wgsl Update the prepass for the bindless lightmap changes. (#16855) 2024-12-17 19:21:12 +00:00
pbr_transmission.wgsl Update typos (#17126) 2025-01-03 17:44:26 +00:00
pbr_types.wgsl Add support for specular tints and maps per the KHR_materials_specular glTF extension. (#14069) 2025-01-26 20:38:46 +00:00
pbr.wgsl Implement basic clustered decal projectors. (#17315) 2025-01-26 20:13:39 +00:00
reset_indirect_batch_sets.wgsl Implement experimental GPU two-phase occlusion culling for the standard 3D mesh pipeline. (#17413) 2025-01-27 05:02:46 +00:00
rgb9e5.wgsl Deferred Renderer (#9258) 2023-10-12 22:10:38 +00:00
shadow_sampling.wgsl Upgrade to wgpu v24 (#17542) 2025-02-09 19:40:53 +00:00
shadows.wgsl Implement percentage-closer soft shadows (PCSS). (#13497) 2024-09-18 18:07:17 +00:00
skin.rs Retain skins from frame to frame. (#17818) 2025-02-18 00:56:04 +00:00
skinning.wgsl Fix motion vector computation after #17688. (#17717) 2025-02-18 09:34:19 +00:00
utils.wgsl Per-meshlet compressed vertex data (#15643) 2024-10-08 18:42:55 +00:00
view_transformations.wgsl Implement experimental GPU two-phase occlusion culling for the standard 3D mesh pipeline. (#17413) 2025-01-27 05:02:46 +00:00
wireframe.wgsl Swap material and mesh bind groups (#10485) 2023-11-28 22:26:22 +00:00