From 0acd86abcac0af783662819cfeca2c15f892d4c6 Mon Sep 17 00:00:00 2001 From: Sean Kim Date: Wed, 19 Feb 2025 22:07:34 +0100 Subject: [PATCH] Fix: Vertex Paint average brush produces incorrect results This commit moves the initialization of the node-sized accumulation array out of the parallel loop to avoid odd optimization errors when differing between debug and release builds as well as errors due to integer overflow in both builds. Additionally it only accumulates results from affected nodes. Pull Request: https://projects.blender.org/blender/blender/pulls/134720 --- source/blender/editors/sculpt_paint/paint_vertex.cc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/source/blender/editors/sculpt_paint/paint_vertex.cc b/source/blender/editors/sculpt_paint/paint_vertex.cc index eaeb4121fe9..de23d04df0a 100644 --- a/source/blender/editors/sculpt_paint/paint_vertex.cc +++ b/source/blender/editors/sculpt_paint/paint_vertex.cc @@ -1567,12 +1567,10 @@ static void calculate_average_color(VPaintData &vpd, using Blend = typename Traits::BlendType; const Span colors = attribute.typed().template cast(); - Array> accum(nodes.size()); + Array> accum(nodes.size(), {0, {0, 0, 0}}); node_mask.foreach_index(GrainSize(1), [&](const int i) { - LocalData &tls = all_tls.local(); VPaintAverageAccum &accum2 = accum[i]; - accum2.len = 0; - memset(accum2.value, 0, sizeof(accum2.value)); + LocalData &tls = all_tls.local(); const Span verts = nodes[i].verts(); tls.factors.resize(verts.size()); @@ -1620,12 +1618,12 @@ static void calculate_average_color(VPaintData &vpd, Blend accum_value[3] = {0}; Color blend(0, 0, 0, 0); - for (int i = 0; i < nodes.size(); i++) { + node_mask.foreach_index([&](const int i) { accum_len += accum[i].len; accum_value[0] += accum[i].value[0]; accum_value[1] += accum[i].value[1]; accum_value[2] += accum[i].value[2]; - } + }); if (accum_len != 0) { blend.r = Traits::round(sqrtf(Traits::divide_round(accum_value[0], accum_len))); blend.g = Traits::round(sqrtf(Traits::divide_round(accum_value[1], accum_len)));