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.
This commit is contained in:
Germano Cavalcante 2021-04-17 13:51:33 -03:00
parent d9224f64a1
commit fa2c00ae91
1 changed files with 25 additions and 5 deletions

View File

@ -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];