Select utils refactor: remove lagacy `ED_view3d_select_id_read_rect`
authormano-wii <germano.costa@ig.com.br>
Mon, 5 Aug 2019 21:02:43 +0000 (18:02 -0300)
committermano-wii <germano.costa@ig.com.br>
Wed, 7 Aug 2019 15:43:31 +0000 (12:43 -0300)
`ED_view3d_select_id_read_rect` serves only as a bridge to `DRW_framebuffer_select_id_read`.
Keeping these codes similar only increases the complexity of some functions.

Reviewers: campbellbarton

Differential Revision: https://developer.blender.org/D5415

source/blender/draw/DRW_engine.h
source/blender/draw/engines/select/select_engine.c
source/blender/editors/include/ED_select_buffer_utils.h
source/blender/editors/include/ED_view3d.h
source/blender/editors/space_view3d/view3d_draw_legacy.c
source/blender/editors/space_view3d/view3d_select.c
source/blender/editors/util/CMakeLists.txt
source/blender/editors/util/select_buffer_utils.c

index be04452f07938236944d6bbd5b8b8aaceeb643af..87a96740c561b26e2307a5b013d2ef151221d06c 100644 (file)
@@ -181,7 +181,7 @@ void DRW_select_context_create(struct Base **bases, const uint bases_len, short
 bool DRW_select_elem_get(const uint sel_id, uint *r_elem, uint *r_base_index, char *r_elem_type);
 uint DRW_select_context_offset_for_object_elem(const uint base_index, char elem_type);
 uint DRW_select_context_elem_len(void);
-void DRW_framebuffer_select_id_read(const struct rcti *rect, uint *r_buf);
+uint *DRW_framebuffer_select_id_read(const struct rcti *rect, uint *r_buf_len);
 void DRW_draw_select_id_object(struct Depsgraph *depsgraph,
                                struct ViewLayer *view_layer,
                                struct ARegion *ar,
index 1f00a116499bc5de1a5ef7ab2f418a974670ca81..8bce61b3031fc090f3974910a0518c8f7c23374f 100644 (file)
@@ -344,7 +344,7 @@ uint DRW_select_context_elem_len(void)
 }
 
 /* Read a block of pixels from the select frame buffer. */
-void DRW_framebuffer_select_id_read(const rcti *rect, uint *r_buf)
+uint *DRW_framebuffer_select_id_read(const rcti *rect, uint *r_buf_len)
 {
   /* clamp rect by texture */
   rcti r = {
@@ -356,6 +356,9 @@ void DRW_framebuffer_select_id_read(const rcti *rect, uint *r_buf)
 
   rcti rect_clamp = *rect;
   if (BLI_rcti_isect(&r, &rect_clamp, &rect_clamp)) {
+    size_t buf_len = BLI_rcti_size_x(rect) * BLI_rcti_size_y(rect);
+    uint *r_buf = MEM_mallocN(buf_len * sizeof(*r_buf), __func__);
+
     DRW_opengl_context_enable();
     GPU_framebuffer_bind(e_data.framebuffer_select_id);
     glReadBuffer(GL_COLOR_ATTACHMENT0);
@@ -373,12 +376,14 @@ void DRW_framebuffer_select_id_read(const rcti *rect, uint *r_buf)
     if (!BLI_rcti_compare(rect, &rect_clamp)) {
       GPU_select_buffer_stride_realign(rect, &rect_clamp, r_buf);
     }
-  }
-  else {
-    size_t buf_size = BLI_rcti_size_x(rect) * BLI_rcti_size_y(rect) * sizeof(*r_buf);
 
-    memset(r_buf, 0, buf_size);
+    if (r_buf_len) {
+      *r_buf_len = buf_len;
+    }
+
+    return r_buf;
   }
+  return NULL;
 }
 
 void DRW_select_context_create(Base **UNUSED(bases), const uint bases_len, short select_mode)
index af745cee676e3555242b4928a4d5c4bf1b26aa7c..1b55de30d96ee8b09f0aa9b483d80851ef7dbb29 100644 (file)
 struct rcti;
 
 /* Boolean array from selection ID's. */
-uint *ED_select_buffer_bitmap_from_rect(const uint bitmap_len, const struct rcti *rect);
-uint *ED_select_buffer_bitmap_from_circle(const uint bitmap_len,
-                                          const int center[2],
-                                          const int radius);
-uint *ED_select_buffer_bitmap_from_poly(const uint bitmap_len,
-                                        const int poly[][2],
+uint *ED_select_buffer_bitmap_from_rect(const struct rcti *rect, uint *r_bitmap_len);
+uint *ED_select_buffer_bitmap_from_circle(const int center[2],
+                                          const int radius,
+                                          uint *r_bitmap_len);
+uint *ED_select_buffer_bitmap_from_poly(const int poly[][2],
                                         const int poly_len,
-                                        const rcti *rect);
+                                        const rcti *rect,
+                                        uint *r_bitmap_len);
 
 /* Single result from selection ID's. */
 uint ED_select_buffer_sample_point(const int center[2]);
index 1b8d65bbca4566e96ae20a3f790798d15b0b28ee..67dfb184d197f617796785e22f1c0003bddfcf69 100644 (file)
@@ -465,8 +465,6 @@ int ED_view3d_backbuf_sample_size_clamp(struct ARegion *ar, const float dist);
 
 void ED_view3d_select_id_validate(struct ViewContext *vc);
 
-uint *ED_view3d_select_id_read_rect(const struct rcti *rect, uint *r_buf_len);
-
 bool ED_view3d_autodist(struct Depsgraph *depsgraph,
                         struct ARegion *ar,
                         struct View3D *v3d,
index d16d90fae01e32fb60af2d9b2f4c6dbe190e0500..307d2a1a41bb1fa2b95efebb41e8054fd582365b 100644 (file)
@@ -238,22 +238,6 @@ void ED_view3d_backbuf_depth_validate(ViewContext *vc)
   }
 }
 
-uint *ED_view3d_select_id_read_rect(const rcti *clip, uint *r_buf_len)
-{
-  uint width = BLI_rcti_size_x(clip);
-  uint height = BLI_rcti_size_y(clip);
-  uint buf_len = width * height;
-  uint *buf = MEM_mallocN(buf_len * sizeof(*buf), __func__);
-
-  DRW_framebuffer_select_id_read(clip, buf);
-
-  if (r_buf_len) {
-    *r_buf_len = buf_len;
-  }
-
-  return buf;
-}
-
 /**
  * allow for small values [0.5 - 2.5],
  * and large values, FLT_MAX by clamping by the area size
index b4bc2748de51b00f3a36909253a86238daaa5139..39684cb698685226c8d578c243a57153ebee4558 100644 (file)
@@ -828,8 +828,7 @@ static bool do_lasso_select_mesh(ViewContext *vc,
     if (wm_userdata->data == NULL) {
       editselect_buf_cache_init_with_generic_userdata(wm_userdata, vc, ts->selectmode);
       esel = wm_userdata->data;
-      const uint buffer_len = DRW_select_context_elem_len();
-      esel->select_bitmap = ED_select_buffer_bitmap_from_poly(buffer_len, mcords, moves, &rect);
+      esel->select_bitmap = ED_select_buffer_bitmap_from_poly(mcords, moves, &rect, NULL);
     }
   }
 
@@ -1140,7 +1139,7 @@ static bool do_lasso_select_paintvert(ViewContext *vc,
       editselect_buf_cache_init_with_generic_userdata(wm_userdata, vc, SCE_SELECT_VERTEX);
       esel = wm_userdata->data;
       const uint buffer_len = DRW_select_context_elem_len();
-      esel->select_bitmap = ED_select_buffer_bitmap_from_poly(buffer_len, mcords, moves, &rect);
+      esel->select_bitmap = ED_select_buffer_bitmap_from_poly(mcords, moves, &rect, NULL);
     }
   }
 
@@ -1199,7 +1198,7 @@ static bool do_lasso_select_paintface(ViewContext *vc,
     editselect_buf_cache_init_with_generic_userdata(wm_userdata, vc, SCE_SELECT_FACE);
     esel = wm_userdata->data;
     const uint buffer_len = DRW_select_context_elem_len();
-    esel->select_bitmap = ED_select_buffer_bitmap_from_poly(buffer_len, mcords, moves, &rect);
+    esel->select_bitmap = ED_select_buffer_bitmap_from_poly(mcords, moves, &rect, NULL);
   }
 
   if (esel->select_bitmap) {
@@ -2554,8 +2553,7 @@ static bool do_paintvert_box_select(ViewContext *vc,
     if (wm_userdata->data == NULL) {
       editselect_buf_cache_init_with_generic_userdata(wm_userdata, vc, SCE_SELECT_VERTEX);
       esel = wm_userdata->data;
-      const uint buffer_len = DRW_select_context_elem_len();
-      esel->select_bitmap = ED_select_buffer_bitmap_from_rect(buffer_len, rect);
+      esel->select_bitmap = ED_select_buffer_bitmap_from_rect(rect, NULL);
     }
     if (esel->select_bitmap != NULL) {
       changed |= edbm_backbuf_check_and_select_verts_obmode(me, esel, sel_op);
@@ -2609,8 +2607,7 @@ static bool do_paintface_box_select(ViewContext *vc,
     if (wm_userdata->data == NULL) {
       editselect_buf_cache_init_with_generic_userdata(wm_userdata, vc, SCE_SELECT_FACE);
       esel = wm_userdata->data;
-      const uint buffer_len = DRW_select_context_elem_len();
-      esel->select_bitmap = ED_select_buffer_bitmap_from_rect(buffer_len, rect);
+      esel->select_bitmap = ED_select_buffer_bitmap_from_rect(rect, NULL);
     }
     if (esel->select_bitmap != NULL) {
       changed |= edbm_backbuf_check_and_select_faces_obmode(me, esel, sel_op);
@@ -2807,8 +2804,7 @@ static bool do_mesh_box_select(ViewContext *vc,
     if (wm_userdata->data == NULL) {
       editselect_buf_cache_init_with_generic_userdata(wm_userdata, vc, ts->selectmode);
       esel = wm_userdata->data;
-      const uint buffer_len = DRW_select_context_elem_len();
-      esel->select_bitmap = ED_select_buffer_bitmap_from_rect(buffer_len, rect);
+      esel->select_bitmap = ED_select_buffer_bitmap_from_rect(rect, NULL);
     }
   }
 
@@ -3395,8 +3391,7 @@ static bool mesh_circle_select(ViewContext *vc,
   struct EditSelectBuf_Cache *esel = wm_userdata->data;
 
   if (use_zbuf) {
-    const uint buffer_len = DRW_select_context_elem_len();
-    esel->select_bitmap = ED_select_buffer_bitmap_from_circle(buffer_len, mval, (int)(rad + 1.0f));
+    esel->select_bitmap = ED_select_buffer_bitmap_from_circle(mval, (int)(rad + 1.0f), NULL);
   }
 
   if (ts->selectmode & SCE_SELECT_VERTEX) {
@@ -3473,8 +3468,7 @@ static bool paint_facesel_circle_select(ViewContext *vc,
 
   {
     struct EditSelectBuf_Cache *esel = wm_userdata->data;
-    const uint buffer_len = DRW_select_context_elem_len();
-    esel->select_bitmap = ED_select_buffer_bitmap_from_circle(buffer_len, mval, (int)(rad + 1.0f));
+    esel->select_bitmap = ED_select_buffer_bitmap_from_circle(mval, (int)(rad + 1.0f), NULL);
     if (esel->select_bitmap != NULL) {
       changed |= edbm_backbuf_check_and_select_faces_obmode(me, esel, sel_op);
       MEM_freeN(esel->select_bitmap);
@@ -3528,8 +3522,7 @@ static bool paint_vertsel_circle_select(ViewContext *vc,
 
   if (use_zbuf) {
     struct EditSelectBuf_Cache *esel = wm_userdata->data;
-    const uint buffer_len = DRW_select_context_elem_len();
-    esel->select_bitmap = ED_select_buffer_bitmap_from_circle(buffer_len, mval, (int)(rad + 1.0f));
+    esel->select_bitmap = ED_select_buffer_bitmap_from_circle(mval, (int)(rad + 1.0f), NULL);
     if (esel->select_bitmap != NULL) {
       changed |= edbm_backbuf_check_and_select_verts_obmode(me, esel, sel_op);
       MEM_freeN(esel->select_bitmap);
index c09237d825d8d3dd3a565da80d2b012a2671917d..23464e9985adb9da89a4518c0bba74532a50f90d 100644 (file)
@@ -22,6 +22,7 @@ set(INC
   ../../blentranslation
   ../../bmesh
   ../../depsgraph
+  ../../draw
   ../../gpu
   ../../imbuf
   ../../makesdna
index fa03f8d15146894dd734d2e79a47312508edeb15..df5864d3dd198a64cc9b54285ae7d184349ed55e 100644 (file)
 #include "BLI_rect.h"
 #include "BLI_utildefines.h"
 
-#include "ED_select_buffer_utils.h"
+#include "DRW_engine.h"
 
-/* Only for #ED_view3d_select_id_read,
- * note that this file shouldn't have 3D view specific logic in it, we could have a more general
- * way to read from selection buffers that doesn't depend on the view3d API. */
-#include "ED_view3d.h"
+#include "ED_select_buffer_utils.h"
 
 /* -------------------------------------------------------------------- */
 /** \name Select Bitmap from ID's
  * \param rect: The rectangle to sample indices from (min/max inclusive).
  * \returns a #BLI_bitmap the length of \a bitmap_len or NULL on failure.
  */
-uint *ED_select_buffer_bitmap_from_rect(const uint bitmap_len, const rcti *rect)
+uint *ED_select_buffer_bitmap_from_rect(const rcti *rect, uint *r_bitmap_len)
 {
+  const uint bitmap_len = DRW_select_context_elem_len();
+  if (bitmap_len == 0) {
+    return NULL;
+  }
+
   rcti rect_px = *rect;
   rect_px.xmax += 1;
   rect_px.ymax += 1;
 
   uint buf_len;
-  const uint *buf = ED_view3d_select_id_read_rect(&rect_px, &buf_len);
+  const uint *buf = DRW_framebuffer_select_id_read(&rect_px, &buf_len);
+
   if (buf == NULL) {
     return NULL;
   }
@@ -77,6 +80,11 @@ uint *ED_select_buffer_bitmap_from_rect(const uint bitmap_len, const rcti *rect)
     buf_iter++;
   }
   MEM_freeN((void *)buf);
+
+  if (r_bitmap_len) {
+    *r_bitmap_len = bitmap_len;
+  }
+
   return bitmap_buf;
 }
 
@@ -86,10 +94,11 @@ uint *ED_select_buffer_bitmap_from_rect(const uint bitmap_len, const rcti *rect)
  * \param radius: Circle radius.
  * \returns a #BLI_bitmap the length of \a bitmap_len or NULL on failure.
  */
-uint *ED_select_buffer_bitmap_from_circle(const uint bitmap_len,
-                                          const int center[2],
-                                          const int radius)
+uint *ED_select_buffer_bitmap_from_circle(const int center[2],
+                                          const int radius,
+                                          uint *r_bitmap_len)
 {
+  const uint bitmap_len = DRW_select_context_elem_len();
   if (bitmap_len == 0) {
     return NULL;
   }
@@ -101,7 +110,8 @@ uint *ED_select_buffer_bitmap_from_circle(const uint bitmap_len,
       .ymax = center[1] + radius + 1,
   };
 
-  const uint *buf = ED_view3d_select_id_read_rect(&rect, NULL);
+  const uint *buf = DRW_framebuffer_select_id_read(&rect, NULL);
+
   if (buf == NULL) {
     return NULL;
   }
@@ -122,6 +132,11 @@ uint *ED_select_buffer_bitmap_from_circle(const uint bitmap_len,
     }
   }
   MEM_freeN((void *)buf);
+
+  if (r_bitmap_len) {
+    *r_bitmap_len = bitmap_len;
+  }
+
   return bitmap_buf;
 }
 
@@ -147,12 +162,13 @@ static void ed_select_buffer_mask_px_cb(int x, int x_end, int y, void *user_data
  * \param radius: Circle radius.
  * \returns a #BLI_bitmap the length of \a bitmap_len or NULL on failure.
  */
-uint *ED_select_buffer_bitmap_from_poly(const uint bitmap_len,
-                                        const int poly[][2],
+uint *ED_select_buffer_bitmap_from_poly(const int poly[][2],
                                         const int poly_len,
-                                        const rcti *rect)
+                                        const rcti *rect,
+                                        uint *r_bitmap_len)
 
 {
+  const uint bitmap_len = DRW_select_context_elem_len();
   if (bitmap_len == 0) {
     return NULL;
   }
@@ -163,7 +179,8 @@ uint *ED_select_buffer_bitmap_from_poly(const uint bitmap_len,
 
   struct PolyMaskData poly_mask_data;
   uint buf_len;
-  const uint *buf = ED_view3d_select_id_read_rect(&rect_px, &buf_len);
+  const uint *buf = DRW_framebuffer_select_id_read(&rect_px, &buf_len);
+
   if (buf == NULL) {
     return NULL;
   }
@@ -196,6 +213,10 @@ uint *ED_select_buffer_bitmap_from_poly(const uint bitmap_len,
   MEM_freeN((void *)buf);
   MEM_freeN(buf_mask);
 
+  if (r_bitmap_len) {
+    *r_bitmap_len = bitmap_len;
+  }
+
   return bitmap_buf;
 }
 
@@ -221,7 +242,7 @@ uint ED_select_buffer_sample_point(const int center[2])
   };
 
   uint buf_len;
-  uint *buf = ED_view3d_select_id_read_rect(&rect, &buf_len);
+  uint *buf = DRW_framebuffer_select_id_read(&rect, &buf_len);
   BLI_assert(0 != buf_len);
   uint ret = buf[0];
   MEM_freeN(buf);
@@ -243,6 +264,9 @@ uint ED_select_buffer_find_nearest_to_point(const int center[2],
   /* Create region around center (typically the mouse cursor).
    * This must be square and have an odd width,
    * the spiraling algorithm does not work with arbitrary rectangles. */
+
+  uint index = 0;
+
   rcti rect;
   BLI_rcti_init_pt_radius(&rect, center, *dist);
   rect.xmax += 1;
@@ -255,15 +279,18 @@ uint ED_select_buffer_find_nearest_to_point(const int center[2],
   /* Read from selection framebuffer. */
 
   uint buf_len;
-  const uint *buf = ED_view3d_select_id_read_rect(&rect, &buf_len);
+  const uint *buf = DRW_framebuffer_select_id_read(&rect, &buf_len);
+
+  if (buf == NULL) {
+    return index;
+  }
+
   BLI_assert(width * height == buf_len);
 
   /* Spiral, starting from center of buffer. */
   int spiral_offset = height * (int)(width / 2) + (height / 2);
   int spiral_direction = 0;
 
-  uint index = 0;
-
   for (int nr = 1; nr <= height; nr++) {
     for (int a = 0; a < 2; a++) {
       for (int b = 0; b < nr; b++) {