From 2ee69807b11630a2564aa4c05153958cb5dec7d4 Mon Sep 17 00:00:00 2001 From: UkoeHB <37489173+UkoeHB@users.noreply.github.com> Date: Mon, 8 Apr 2024 12:00:09 -0500 Subject: [PATCH] Fix potential out-of-bounds access in pbr_functions.wgsl (#12585) # Objective - Fix a potential out-of-bounds access in the `pbr_functions.wgsl` shader. ## Solution - Correctly compute the `GpuLights::directional_lights` array length. ## Comments I think this solves this comment in the code, but need someone to test it: ```rust //NOTE: When running bevy on Adreno GPU chipsets in WebGL, any value above 1 will result in a crash // when loading the wgsl "pbr_functions.wgsl" in the function apply_fog. ``` --- crates/bevy_pbr/src/render/light.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index e616ee250c..bb0c7853dd 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -991,7 +991,8 @@ pub fn prepare_lights( cluster_factors_zw.y, ), cluster_dimensions: clusters.dimensions.extend(n_clusters), - n_directional_lights: directional_lights.iter().len() as u32, + n_directional_lights: directional_lights.iter().len().min(MAX_DIRECTIONAL_LIGHTS) + as u32, // spotlight shadow maps are stored in the directional light array, starting at num_directional_cascades_enabled. // the spot lights themselves start in the light array at point_light_count. so to go from light // index to shadow map index, we need to subtract point light count and add directional shadowmap count.