From fa2c00ae91f04fad82da7a0593978031e3ec94cb Mon Sep 17 00:00:00 2001 From: Germano Cavalcante Date: Sat, 17 Apr 2021 13:51:33 -0300 Subject: Fix wrong logic used in 'ED_view3d_depth_read_cached' It is important to limit the pixels read on `BLI_array_iter_spiral_square`. Fortunately `ED_view3d_depth_read_cached` was not being called with a `margin` parameter. Wrong logic introduced in rB44c76e4ce310. --- source/blender/editors/space_view3d/view3d_utils.c | 30 ++++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'source/blender/editors/space_view3d') diff --git a/source/blender/editors/space_view3d/view3d_utils.c b/source/blender/editors/space_view3d/view3d_utils.c index fde98c18803..f96c17d7cff 100644 --- a/source/blender/editors/space_view3d/view3d_utils.c +++ b/source/blender/editors/space_view3d/view3d_utils.c @@ -1630,12 +1630,23 @@ bool ED_view3d_camera_to_view_selected(struct Main *bmain, /** \name Depth Buffer Utilities * \{ */ +struct ReadData { + int count; + int count_max; + float r_depth; +}; + static bool depth_read_test_fn(const void *value, void *userdata) { - float *r_depth = userdata; + struct ReadData *data = userdata; float depth = *(float *)value; - if (depth < *r_depth) { - *r_depth = depth; + if (depth < data->r_depth) { + data->r_depth = depth; + } + + if ((++data->count) >= data->count_max) { + /* Outside the margin. */ + return true; } return false; } @@ -1657,9 +1668,18 @@ bool ED_view3d_depth_read_cached(const ViewDepths *vd, float depth = 1.0f; if (margin) { - /* TODO: No need to go spiral. */ int shape[2] = {vd->w, vd->h}; - BLI_array_iter_spiral_square(vd->depths, shape, mval, depth_read_test_fn, &depth); + int pixel_count = (min_ii(x + margin + 1, shape[1]) - max_ii(x - margin, 0)) * + (min_ii(y + margin + 1, shape[0]) - max_ii(y - margin, 0)); + + struct ReadData data; + data.count = 0; + data.count_max = pixel_count; + data.r_depth = 1.0f; + + /* TODO: No need to go spiral. */ + BLI_array_iter_spiral_square(vd->depths, shape, mval, depth_read_test_fn, &data); + depth = data.r_depth; } else { depth = vd->depths[y * vd->w + x]; -- cgit v1.2.3