From 049305f5faa2ef08bb7d89ca85ceb6170acdc609 Mon Sep 17 00:00:00 2001 From: JMS55 <47158642+JMS55@users.noreply.github.com> Date: Sat, 1 Aug 2026 13:48:46 -0400 Subject: [PATCH] Fix shader out of bounds accesses --- crates/bevy_pbr/src/cluster/cluster_allocate.wgsl | 4 +++- crates/bevy_pbr/src/render/allocate_uniforms.wgsl | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/bevy_pbr/src/cluster/cluster_allocate.wgsl b/crates/bevy_pbr/src/cluster/cluster_allocate.wgsl index 433e32ab91df0..a98cdfac9ec4f 100644 --- a/crates/bevy_pbr/src/cluster/cluster_allocate.wgsl +++ b/crates/bevy_pbr/src/cluster/cluster_allocate.wgsl @@ -98,7 +98,9 @@ fn allocate_global_main(@builtin(local_invocation_id) local_id: vec3) { // March along the chunks sequentially, accumulating as we go. var current_offset = 0u; for (var i = 0u; i < cluster_count; i += 256u) { - offsets_and_counts.data[i + local_id.x][0].x += current_offset; + if (i + local_id.x < cluster_count) { + offsets_and_counts.data[i + local_id.x][0].x += current_offset; + } storageBarrier(); if (i + 255u < cluster_count) { diff --git a/crates/bevy_pbr/src/render/allocate_uniforms.wgsl b/crates/bevy_pbr/src/render/allocate_uniforms.wgsl index 89b73ad11b246..3301756d71e34 100644 --- a/crates/bevy_pbr/src/render/allocate_uniforms.wgsl +++ b/crates/bevy_pbr/src/render/allocate_uniforms.wgsl @@ -136,8 +136,11 @@ fn allocate_local_scan( // instances (plus the first output mesh uniform index if we're the first // workgroup) in the fan buffer in preparation for the next phase. if (local_id.x == WORKGROUP_SIZE - 1u) { - fan_buffer[group_id.x] = output_offsets[WORKGROUP_SIZE - 1u] + - bin_metadata[global_id.x].instance_count; + var chunk_total = output_offsets[WORKGROUP_SIZE - 1u]; + if (global_id.x < block_end) { + chunk_total += bin_metadata[global_id.x].instance_count; + } + fan_buffer[group_id.x] = chunk_total; } }