From a016fc19de41a65351da720e955bada98bf39022 Mon Sep 17 00:00:00 2001 From: Arman Uguray Date: Wed, 14 Jun 2023 15:53:19 -0700 Subject: [PATCH] [draw_leaf] Don't write past the end of the draw_monoids buffer The number of global invocations for draw_leaf can exceed the size of the draw_monoids buffer which gets conservatively set to the number of draw objects. Added an explicit bounds check to prevent the invalid write. This is not an issue when targeting wgpu as the WGSL compiler emits implicit bounds checking. When targeting Metal, we disable implicit bounds checks as that requires an extra buffer binding containing buffer sizes. This was caught by Xcode's Metal shader validation and resulted in visual artifacts in native rendering. --- shader/draw_leaf.wgsl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/shader/draw_leaf.wgsl b/shader/draw_leaf.wgsl index 59e9e97..3879afc 100644 --- a/shader/draw_leaf.wgsl +++ b/shader/draw_leaf.wgsl @@ -90,7 +90,9 @@ fn main( m = combine_draw_monoid(m, sh_scratch[local_id.x - 1u]); } // m now contains exclusive prefix sum of draw monoid - draw_monoid[ix] = m; + if ix < config.n_drawobj { + draw_monoid[ix] = m; + } let dd = config.drawdata_base + m.scene_offset; let di = m.info_offset; if tag_word == DRAWTAG_FILL_COLOR || tag_word == DRAWTAG_FILL_LIN_GRADIENT ||