Blender V5.0
draw_select_buffer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cfloat>
12
13#include "BLI_math_matrix.hh"
14#include "MEM_guardedalloc.h"
15
16#include "BLI_array_utils.h"
17#include "BLI_bitmap.h"
18#include "BLI_bitmap_draw_2d.h"
19#include "BLI_math_matrix.h"
20#include "BLI_rect.h"
21
22#include "DNA_scene_types.h"
23#include "DNA_screen_types.h"
24#include "DNA_view3d_types.h"
25
26#include "GPU_framebuffer.hh"
27#include "GPU_select.hh"
28
29#include "DEG_depsgraph.hh"
31
32#include "DRW_engine.hh"
33#include "DRW_render.hh"
34#include "DRW_select_buffer.hh"
35
37
38using blender::int2;
39using blender::Span;
40
42{
43 uint64_t last_update = this->depsgraph_last_update;
45
46 /* Check if the viewport has changed.
47 * This can happen when triggering the selection operator *while* playing back animation and
48 * looking through an animated camera. */
49 if (!blender::math::is_equal(this->persmat, blender::float4x4(rv3d->persmat), FLT_EPSILON)) {
50 return true;
51 }
52 /* Check if any of the drawn objects have been transformed.
53 * This can happen when triggering the selection operator *while* playing back animation on an
54 * edited mesh. */
55 for (Object *obj_eval : this->objects) {
56 if (obj_eval->runtime->last_update_transform > last_update) {
57 return true;
58 }
59 }
60 return false;
61}
62
63/* -------------------------------------------------------------------- */
66
68 Depsgraph *depsgraph, ARegion *region, View3D *v3d, const rcti *rect, uint *r_buf_len)
69{
70 uint *buf = nullptr;
71 uint buf_len = 0;
72
73 /* Clamp rect. */
74 rcti r{};
75 r.xmin = 0;
76 r.xmax = region->winx;
77 r.ymin = 0;
78 r.ymax = region->winy;
79
80 /* Make sure that the rect is within the bounds of the viewport.
81 * Some GPUs have problems reading pixels off limits. */
82 rcti rect_clamp = *rect;
83 if (BLI_rcti_isect(&r, &rect_clamp, &rect_clamp) && !BLI_rcti_is_empty(&rect_clamp)) {
85 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
86
88
89 if (select_ctx->is_dirty(depsgraph, rv3d)) {
90 /* Update drawing. */
91 DRW_draw_select_id(depsgraph, region, v3d);
92 }
93
94 if (select_ctx->max_index_drawn_len > 1) {
97
98 /* Read the UI32 pixels. */
99 buf_len = BLI_rcti_size_x(rect) * BLI_rcti_size_y(rect);
100 buf = MEM_malloc_arrayN<uint>(buf_len, __func__);
101
103 GPU_framebuffer_bind(select_id_fb);
104 GPU_framebuffer_read_color(select_id_fb,
105 rect_clamp.xmin,
106 rect_clamp.ymin,
107 BLI_rcti_size_x(&rect_clamp),
108 BLI_rcti_size_y(&rect_clamp),
109 1,
110 0,
112 buf);
113
114 if (!BLI_rcti_compare(rect, &rect_clamp)) {
115 /* The rect has been clamped so we need to realign the buffer and fill in the blanks */
116 GPU_select_buffer_stride_realign(rect, &rect_clamp, buf);
117 }
118 }
119
122 }
123
124 if (r_buf_len) {
125 *r_buf_len = buf_len;
126 }
127
128 return buf;
129}
130
132
133/* -------------------------------------------------------------------- */
140
142 Depsgraph *depsgraph, ARegion *region, View3D *v3d, const rcti *rect, uint *r_bitmap_len)
143{
145
146 rcti rect_px = *rect;
147 rect_px.xmax += 1;
148 rect_px.ymax += 1;
149
150 uint buf_len;
151 uint *buf = DRW_select_buffer_read(depsgraph, region, v3d, &rect_px, &buf_len);
152 if (buf == nullptr) {
153 return nullptr;
154 }
155
156 BLI_assert(select_ctx->max_index_drawn_len > 0);
157 const uint bitmap_len = select_ctx->max_index_drawn_len - 1;
158
159 BLI_bitmap *bitmap_buf = BLI_BITMAP_NEW(bitmap_len, __func__);
160 const uint *buf_iter = buf;
161 while (buf_len--) {
162 const uint index = *buf_iter - 1;
163 if (index < bitmap_len) {
164 BLI_BITMAP_ENABLE(bitmap_buf, index);
165 }
166 buf_iter++;
167 }
168 MEM_freeN(buf);
169
170 if (r_bitmap_len) {
171 *r_bitmap_len = bitmap_len;
172 }
173
174 return bitmap_buf;
175}
176
178 ARegion *region,
179 View3D *v3d,
180 const int center[2],
181 const int radius,
182 uint *r_bitmap_len)
183{
185
186 rcti rect{};
187 rect.xmin = center[0] - radius;
188 rect.xmax = center[0] + radius + 1;
189 rect.ymin = center[1] - radius;
190 rect.ymax = center[1] + radius + 1;
191
192 const uint *buf = DRW_select_buffer_read(depsgraph, region, v3d, &rect, nullptr);
193
194 if (buf == nullptr) {
195 return nullptr;
196 }
197
198 BLI_assert(select_ctx->max_index_drawn_len > 0);
199 const uint bitmap_len = select_ctx->max_index_drawn_len - 1;
200
201 BLI_bitmap *bitmap_buf = BLI_BITMAP_NEW(bitmap_len, __func__);
202 const uint *buf_iter = buf;
203 const int radius_sq = radius * radius;
204 for (int yc = -radius; yc <= radius; yc++) {
205 for (int xc = -radius; xc <= radius; xc++, buf_iter++) {
206 if (xc * xc + yc * yc < radius_sq) {
207 /* Intentionally wrap to max value if this is zero. */
208 const uint index = *buf_iter - 1;
209 if (index < bitmap_len) {
210 BLI_BITMAP_ENABLE(bitmap_buf, index);
211 }
212 }
213 }
214 }
215 MEM_freeN(buf);
216
217 if (r_bitmap_len) {
218 *r_bitmap_len = bitmap_len;
219 }
220
221 return bitmap_buf;
222}
223
228
229static void drw_select_mask_px_cb(int x, int x_end, int y, void *user_data)
230{
231 PolyMaskData *data = static_cast<PolyMaskData *>(user_data);
232 BLI_bitmap *px = data->px;
233 int i = (y * data->width) + x;
234 do {
235 BLI_BITMAP_ENABLE(px, i);
236 i++;
237 } while (++x != x_end);
238}
239
241 ARegion *region,
242 View3D *v3d,
243 const Span<int2> poly,
244 const rcti *rect,
245 uint *r_bitmap_len)
246{
248
249 rcti rect_px = *rect;
250 rect_px.xmax += 1;
251 rect_px.ymax += 1;
252
253 uint buf_len;
254 uint *buf = DRW_select_buffer_read(depsgraph, region, v3d, &rect_px, &buf_len);
255 if (buf == nullptr) {
256 return nullptr;
257 }
258
259 BLI_bitmap *buf_mask = BLI_BITMAP_NEW(buf_len, __func__);
260
261 PolyMaskData poly_mask_data;
262 poly_mask_data.px = buf_mask;
263 poly_mask_data.width = (rect->xmax - rect->xmin) + 1;
264
266 rect_px.ymin,
267 rect_px.xmax,
268 rect_px.ymax,
269 poly,
271 &poly_mask_data);
272
273 BLI_assert(select_ctx->max_index_drawn_len > 0);
274 const uint bitmap_len = select_ctx->max_index_drawn_len - 1;
275
276 BLI_bitmap *bitmap_buf = BLI_BITMAP_NEW(bitmap_len, __func__);
277 const uint *buf_iter = buf;
278 int i = 0;
279 while (buf_len--) {
280 const uint index = *buf_iter - 1;
281 if (index < bitmap_len && BLI_BITMAP_TEST(buf_mask, i)) {
282 BLI_BITMAP_ENABLE(bitmap_buf, index);
283 }
284 buf_iter++;
285 i++;
286 }
287 MEM_freeN(buf);
288 MEM_freeN(buf_mask);
289
290 if (r_bitmap_len) {
291 *r_bitmap_len = bitmap_len;
292 }
293
294 return bitmap_buf;
295}
296
298
299/* -------------------------------------------------------------------- */
305
307 ARegion *region,
308 View3D *v3d,
309 const int center[2])
310{
311 uint ret = 0;
312
313 rcti rect{};
314 rect.xmin = center[0];
315 rect.xmax = center[0] + 1;
316 rect.ymin = center[1];
317 rect.ymax = center[1] + 1;
318
319 uint buf_len;
320 uint *buf = DRW_select_buffer_read(depsgraph, region, v3d, &rect, &buf_len);
321 if (buf) {
322 BLI_assert(0 != buf_len);
323 ret = buf[0];
324 MEM_freeN(buf);
325 }
326
327 return ret;
328}
329
336
337static bool select_buffer_test_fn(const void *__restrict value, void *__restrict userdata)
338{
339 SelectReadData *data = static_cast<SelectReadData *>(userdata);
340 uint hit_id = *(uint *)value;
341 if (hit_id && hit_id >= data->id_min && hit_id < data->id_max) {
342 /* Start at 1 to confirm. */
343 data->val_ptr = value;
344 data->r_index = (hit_id - data->id_min) + 1;
345 return true;
346 }
347 return false;
348}
349
351 ARegion *region,
352 View3D *v3d,
353 const int center[2],
354 const uint id_min,
355 const uint id_max,
356 uint *dist)
357{
358 /* Create region around center (typically the mouse cursor).
359 * This must be square and have an odd width. */
360
361 rcti rect;
362 BLI_rcti_init_pt_radius(&rect, center, *dist);
363 rect.xmax += 1;
364 rect.ymax += 1;
365
366 int width = BLI_rcti_size_x(&rect);
367 int height = width;
368
369 /* Read from selection framebuffer. */
370
371 uint buf_len;
372 const uint *buf = DRW_select_buffer_read(depsgraph, region, v3d, &rect, &buf_len);
373
374 if (buf == nullptr) {
375 return 0;
376 }
377
378 const int shape[2] = {height, width};
379 const int center_yx[2] = {(height - 1) / 2, (width - 1) / 2};
380 SelectReadData data = {nullptr, id_min, id_max, 0};
382
383 if (data.val_ptr) {
384 size_t offset = (size_t(data.val_ptr) - size_t(buf)) / sizeof(*buf);
385 int hit_x = offset % width;
386 int hit_y = offset / width;
387 *dist = uint(abs(hit_y - center_yx[0]) + abs(hit_x - center_yx[1]));
388 }
389
390 MEM_freeN(buf);
391 return data.r_index;
392}
393
395
396/* -------------------------------------------------------------------- */
399
401 uint &r_elem,
402 uint &r_base_index,
403 char &r_elem_type)
404{
406
407 for (const auto &item : select_ctx->elem_ranges.items()) {
408 const ElemIndexRanges &ranges = item.value;
409 Object *ob = item.key;
410 if (!ranges.total.contains(sel_id)) {
411 continue;
412 }
413 if (ranges.face.contains(sel_id)) {
414 r_elem = sel_id - ranges.face.start();
415 r_elem_type = SCE_SELECT_FACE;
416 r_base_index = select_ctx->objects.first_index_of_try(ob);
417 return r_base_index != -1;
418 }
419 if (ranges.edge.contains(sel_id)) {
420 r_elem = sel_id - ranges.edge.start();
421 r_elem_type = SCE_SELECT_EDGE;
422 r_base_index = select_ctx->objects.first_index_of_try(ob);
423 return r_base_index != -1;
424 }
425 if (ranges.vert.contains(sel_id)) {
426 r_elem = sel_id - ranges.vert.start();
427 r_elem_type = SCE_SELECT_VERTEX;
428 r_base_index = select_ctx->objects.first_index_of_try(ob);
429 return r_base_index != -1;
430 }
431 }
432 return false;
433}
434
436 Object *object,
437 char elem_type)
438{
440
441 Object *ob_eval = DEG_get_evaluated(depsgraph, object);
442
443 const ElemIndexRanges base_ofs = select_ctx->elem_ranges.lookup_default(ob_eval,
445
446 if (elem_type == SCE_SELECT_VERTEX) {
447 return base_ofs.vert.start();
448 }
449 if (elem_type == SCE_SELECT_EDGE) {
450 return base_ofs.edge.start();
451 }
452 if (elem_type == SCE_SELECT_FACE) {
453 return base_ofs.face.start();
454 }
455 BLI_assert(0);
456 return 0;
457}
458
460
461/* -------------------------------------------------------------------- */
464
466 const blender::Span<Base *> bases,
467 short select_mode)
468{
470
471 select_ctx->objects.reinitialize(bases.size());
472
473 for (const int i : bases.index_range()) {
474 Object *obj = bases[i]->object;
475 select_ctx->objects[i] = DEG_get_evaluated(depsgraph, obj);
476 }
477
478 select_ctx->select_mode = select_mode;
479 select_ctx->persmat = blender::float4x4::zero();
480}
481
Generic array manipulation API.
#define BLI_array_iter_spiral_square(arr, arr_shape, center, test_fn, user_data)
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition BLI_bitmap.h:37
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition BLI_bitmap.h:61
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition BLI_bitmap.h:78
unsigned int BLI_bitmap
Definition BLI_bitmap.h:13
void BLI_bitmap_draw_2d_poly_v2i_n(int xmin, int ymin, int xmax, int ymax, blender::Span< blender::int2 > verts, void(*callback)(int x, int x_end, int y, void *), void *user_data)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.h:198
bool BLI_rcti_compare(const struct rcti *rect_a, const struct rcti *rect_b)
void BLI_rcti_init_pt_radius(struct rcti *rect, const int xy[2], int size)
Definition rct.cc:466
bool BLI_rcti_isect(const struct rcti *src1, const struct rcti *src2, struct rcti *dest)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.h:194
bool BLI_rcti_is_empty(const struct rcti *rect)
unsigned int uint
uint64_t DEG_get_update_count(const Depsgraph *depsgraph)
Definition depsgraph.cc:355
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
@ SCE_SELECT_FACE
@ SCE_SELECT_VERTEX
@ SCE_SELECT_EDGE
void DRW_gpu_context_disable()
void DRW_gpu_context_enable()
void DRW_draw_select_id(Depsgraph *depsgraph, ARegion *region, View3D *v3d)
void GPU_framebuffer_restore()
void GPU_framebuffer_read_color(blender::gpu::FrameBuffer *fb, int x, int y, int width, int height, int channels, int slot, eGPUDataFormat data_format, void *r_data)
void GPU_framebuffer_bind(blender::gpu::FrameBuffer *fb)
void GPU_select_buffer_stride_realign(const rcti *src, const rcti *dst, uint *r_buf)
int GPU_texture_height(const blender::gpu::Texture *texture)
int GPU_texture_width(const blender::gpu::Texture *texture)
@ GPU_DATA_UINT
Read Guarded memory(de)allocation.
BMesh const char void * data
BPy_StructRNA * depsgraph
unsigned long long int uint64_t
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
ItemIterator items() const &
Definition BLI_map.hh:902
void reinitialize(const int64_t new_size)
int64_t first_index_of_try(const T &value) const
constexpr int64_t start() const
constexpr bool contains(int64_t value) const
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
uint * DRW_select_buffer_bitmap_from_rect(Depsgraph *depsgraph, ARegion *region, View3D *v3d, const rcti *rect, uint *r_bitmap_len)
static bool select_buffer_test_fn(const void *__restrict value, void *__restrict userdata)
uint * DRW_select_buffer_bitmap_from_circle(Depsgraph *depsgraph, ARegion *region, View3D *v3d, const int center[2], const int radius, uint *r_bitmap_len)
void DRW_select_buffer_context_create(Depsgraph *depsgraph, const blender::Span< Base * > bases, short select_mode)
uint * DRW_select_buffer_bitmap_from_poly(Depsgraph *depsgraph, ARegion *region, View3D *v3d, const Span< int2 > poly, const rcti *rect, uint *r_bitmap_len)
bool DRW_select_buffer_elem_get(const uint sel_id, uint &r_elem, uint &r_base_index, char &r_elem_type)
uint DRW_select_buffer_sample_point(Depsgraph *depsgraph, ARegion *region, View3D *v3d, const int center[2])
uint DRW_select_buffer_context_offset_for_object_elem(Depsgraph *depsgraph, Object *object, char elem_type)
uint DRW_select_buffer_find_nearest_to_point(Depsgraph *depsgraph, ARegion *region, View3D *v3d, const int center[2], const uint id_min, const uint id_max, uint *dist)
uint * DRW_select_buffer_read(Depsgraph *depsgraph, ARegion *region, View3D *v3d, const rcti *rect, uint *r_buf_len)
static void drw_select_mask_px_cb(int x, int x_end, int y, void *user_data)
#define abs
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
bool is_equal(const MatBase< T, NumCol, NumRow > &a, const MatBase< T, NumCol, NumRow > &b, const T epsilon=T(0))
MatBase< float, 4, 4 > float4x4
VecBase< int32_t, 2 > int2
return ret
blender::gpu::Texture * DRW_engine_select_texture_get()
blender::gpu::FrameBuffer * DRW_engine_select_framebuffer_get()
SELECTID_Context * DRW_select_engine_context_get()
void * regiondata
blender::IndexRange face
blender::IndexRange total
blender::IndexRange vert
blender::IndexRange edge
float persmat[4][4]
blender::Map< Object *, ElemIndexRanges > elem_ranges
bool is_dirty(Depsgraph *depsgraph, RegionView3D *rv3d)
blender::Vector< Object * > objects
blender::float4x4 persmat
int ymin
int ymax
int xmin
int xmax
i
Definition text_draw.cc:230