From bfc13383e0a0800c1e9c57454ae5454f5891ba8a Mon Sep 17 00:00:00 2001 From: Bram Buurlage Date: Mon, 13 May 2024 01:24:58 +0200 Subject: [PATCH] Fix incorrect workgroupBarrier and OOB array access in auto_exposure (#13283) This commit fixes two issues in auto_exposure.wgsl: * A `storageBarrier()` was incorrectly used where a `workgroupBarrier()` should be used instead; * Resetting the `histogram_shared` array would write beyond the 64th index, which is out of bounds. ## Solution The first issue is fixed by using the appropriate workgroupBarrier instead; The second issue is fixed by adding a range check before setting `histogram_shared[local_invocation_index] = 0u`. ## Testing These changes were tested using the Xcode metal profiler, and I could not find any noticable change in compute shader performance. --- .../bevy_core_pipeline/src/auto_exposure/auto_exposure.wgsl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/bevy_core_pipeline/src/auto_exposure/auto_exposure.wgsl b/crates/bevy_core_pipeline/src/auto_exposure/auto_exposure.wgsl index c85c753d1a..a4eca41477 100644 --- a/crates/bevy_core_pipeline/src/auto_exposure/auto_exposure.wgsl +++ b/crates/bevy_core_pipeline/src/auto_exposure/auto_exposure.wgsl @@ -95,10 +95,12 @@ fn compute_histogram( @builtin(local_invocation_index) local_invocation_index: u32 ) { // Clear the workgroup shared histogram - histogram_shared[local_invocation_index] = 0u; + if local_invocation_index < 64 { + histogram_shared[local_invocation_index] = 0u; + } // Wait for all workgroup threads to clear the shared histogram - storageBarrier(); + workgroupBarrier(); let dim = vec2(textureDimensions(tex_color)); let uv = vec2(global_invocation_id.xy) / vec2(dim);