Merge branch 'blender2.7'
[blender.git] / source / blender / gpu / intern / gpu_select_pick.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2017 Blender Foundation.
19  * All rights reserved.
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/gpu/intern/gpu_select_pick.c
25  *  \ingroup gpu
26  *
27  * Custom select code for picking small regions (not efficient for large regions).
28  * `gpu_select_pick_*` API.
29  */
30 #include <string.h>
31 #include <stdlib.h>
32 #include <float.h>
33
34 #include "GPU_immediate.h"
35 #include "GPU_draw.h"
36 #include "GPU_select.h"
37 #include "GPU_extensions.h"
38 #include "GPU_glew.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_rect.h"
43 #include "BLI_listbase.h"
44 #include "BLI_utildefines.h"
45
46 #include "gpu_select_private.h"
47
48 #include "BLI_strict_flags.h"
49
50 /* #define DEBUG_PRINT */
51
52 /* Alloc number for depths */
53 #define ALLOC_DEPTHS 200
54
55 /* Z-depth of cleared depth buffer */
56 #define DEPTH_MAX 0xffffffff
57
58 /* ----------------------------------------------------------------------------
59  * SubRectStride
60  */
61
62 /* For looping over a sub-region of a rect, could be moved into 'rct.c'*/
63 typedef struct SubRectStride {
64         uint start;     /* start here */
65         uint span;      /* read these */
66         uint span_len;  /* len times (read span 'len' times). */
67         uint skip;      /* skip those */
68 } SubRectStride;
69
70 /* we may want to change back to float if uint isn't well supported */
71 typedef uint depth_t;
72
73 /**
74  * Calculate values needed for looping over a sub-region (smaller buffer within a larger buffer).
75  *
76  * 'src' must be bigger than 'dst'.
77  */
78 static void rect_subregion_stride_calc(const rcti *src, const rcti *dst, SubRectStride *r_sub)
79 {
80         const int src_x = BLI_rcti_size_x(src);
81         // const int src_y = BLI_rcti_size_y(src);
82         const int dst_x = BLI_rcti_size_x(dst);
83         const int dst_y = BLI_rcti_size_y(dst);
84         const int x = dst->xmin - src->xmin;
85         const int y = dst->ymin - src->ymin;
86
87         BLI_assert(src->xmin <= dst->xmin && src->ymin <= dst->ymin &&
88                    src->xmax >= dst->xmax && src->ymax >= dst->ymax);
89         BLI_assert(x >= 0 && y >= 0);
90
91         r_sub->start    = (uint)((src_x * y) + x);
92         r_sub->span     = (uint)dst_x;
93         r_sub->span_len = (uint)dst_y;
94         r_sub->skip     = (uint)(src_x - dst_x);
95 }
96
97 /**
98  * Ignore depth clearing as a change,
99  * only check if its been changed _and_ filled in (ignore clearing since XRAY does this).
100  */
101 BLI_INLINE bool depth_is_filled(const depth_t *prev, const depth_t *curr)
102 {
103         return (*prev != *curr) && (*curr != DEPTH_MAX);
104 }
105
106 /* ----------------------------------------------------------------------------
107  * DepthBufCache
108  *
109  * Result of reading glReadPixels,
110  * use for both cache and non-cached storage.
111  */
112
113 /* store result of glReadPixels */
114 typedef struct DepthBufCache {
115         struct DepthBufCache *next, *prev;
116         uint id;
117         depth_t buf[0];
118 } DepthBufCache;
119
120 static DepthBufCache *depth_buf_malloc(uint rect_len)
121 {
122         DepthBufCache *rect = MEM_mallocN(sizeof(DepthBufCache) + sizeof(depth_t) * rect_len, __func__);
123         rect->id = SELECT_ID_NONE;
124         return rect;
125 }
126
127 static bool depth_buf_rect_depth_any(
128         const DepthBufCache *rect_depth,
129         uint rect_len)
130 {
131         const depth_t *curr = rect_depth->buf;
132         for (uint i = 0; i < rect_len; i++, curr++) {
133                 if (*curr != DEPTH_MAX) {
134                         return true;
135                 }
136         }
137         return false;
138 }
139
140 static bool depth_buf_subrect_depth_any(
141         const DepthBufCache *rect_depth,
142         const SubRectStride *sub_rect)
143 {
144         const depth_t *curr = rect_depth->buf + sub_rect->start;
145         for (uint i = 0; i < sub_rect->span_len; i++) {
146                 const depth_t *curr_end = curr + sub_rect->span;
147                 for (; curr < curr_end; curr++, curr++) {
148                         if (*curr != DEPTH_MAX) {
149                                 return true;
150                         }
151                 }
152                 curr += sub_rect->skip;
153         }
154         return false;
155 }
156
157 static bool depth_buf_rect_depth_any_filled(
158         const DepthBufCache *rect_prev, const DepthBufCache *rect_curr,
159         uint rect_len)
160 {
161 #if 0
162         return memcmp(rect_depth_a->buf, rect_depth_b->buf, rect_len * sizeof(depth_t)) != 0;
163 #else
164         const depth_t *prev = rect_prev->buf;
165         const depth_t *curr = rect_curr->buf;
166         for (uint i = 0; i < rect_len; i++, curr++, prev++) {
167                 if (depth_is_filled(prev, curr)) {
168                         return true;
169                 }
170         }
171         return false;
172 #endif
173 }
174
175 /**
176  * Both buffers are the same size, just check if the sub-rect contains any differences.
177  */
178 static bool depth_buf_subrect_depth_any_filled(
179         const DepthBufCache *rect_src, const DepthBufCache *rect_dst,
180         const SubRectStride *sub_rect)
181 {
182         /* same as above but different rect sizes */
183         const depth_t *prev = rect_src->buf + sub_rect->start;
184         const depth_t *curr = rect_dst->buf + sub_rect->start;
185         for (uint i = 0; i < sub_rect->span_len; i++) {
186                 const depth_t *curr_end = curr + sub_rect->span;
187                 for (; curr < curr_end; prev++, curr++) {
188                         if (depth_is_filled(prev, curr)) {
189                                 return true;
190                         }
191                 }
192                 prev += sub_rect->skip;
193                 curr += sub_rect->skip;
194         }
195         return false;
196 }
197
198 /* ----------------------------------------------------------------------------
199  * DepthID
200  *
201  * Internal structure for storing hits.
202  */
203
204 typedef struct DepthID {
205         uint id;
206         depth_t depth;
207 } DepthID;
208
209 static int depth_id_cmp(const void *v1, const void *v2)
210 {
211         const DepthID *d1 = v1, *d2 = v2;
212         if (d1->id < d2->id) {
213                 return -1;
214         }
215         else if (d1->id > d2->id) {
216                 return 1;
217         }
218         else {
219                 return 0;
220         }
221 }
222
223 static int depth_cmp(const void *v1, const void *v2)
224 {
225         const DepthID *d1 = v1, *d2 = v2;
226         if (d1->depth < d2->depth) {
227                 return -1;
228         }
229         else if (d1->depth > d2->depth) {
230                 return 1;
231         }
232         else {
233                 return 0;
234         }
235 }
236
237 /* depth sorting */
238 typedef struct GPUPickState {
239         /* cache on initialization */
240         uint (*buffer)[4];
241
242         /* buffer size (stores number of integers, for actual size multiply by sizeof integer)*/
243         uint bufsize;
244         /* mode of operation */
245         char mode;
246
247         /* OpenGL drawing, never use when (is_cached == true). */
248         struct {
249                 /* The current depth, accumulated as we draw */
250                 DepthBufCache *rect_depth;
251                 /* Scratch buffer, avoid allocs every time (when not caching) */
252                 DepthBufCache *rect_depth_test;
253
254                 /* Pass to glReadPixels (x, y, w, h) */
255                 int clip_readpixels[4];
256
257                 /* Set after first draw */
258                 bool is_init;
259                 uint prev_id;
260         } gl;
261
262         /* src: data stored in 'cache' and 'gl',
263          * dst: use when cached region is smaller (where src -> dst isn't 1:1) */
264         struct {
265                 rcti clip_rect;
266                 uint rect_len;
267         } src, dst;
268
269         /* Store cache between `GPU_select_cache_begin/end` */
270         bool use_cache;
271         bool is_cached;
272         struct {
273                 /* Cleanup used for iterating over both source and destination buffers:
274                  * src.clip_rect -> dst.clip_rect */
275                 SubRectStride sub_rect;
276
277                 /* List of DepthBufCache, sized of 'src.clip_rect' */
278                 ListBase bufs;
279         } cache;
280
281         /* Pickign methods */
282         union {
283                 /* GPU_SELECT_PICK_ALL */
284                 struct {
285                         DepthID *hits;
286                         uint hits_len;
287                         uint hits_len_alloc;
288                 } all;
289
290                 /* GPU_SELECT_PICK_NEAREST */
291                 struct {
292                         uint *rect_id;
293                 } nearest;
294         };
295 } GPUPickState;
296
297
298 static GPUPickState g_pick_state = {0};
299
300 void gpu_select_pick_begin(
301         uint (*buffer)[4], uint bufsize,
302         const rcti *input, char mode)
303 {
304         GPUPickState *ps = &g_pick_state;
305
306 #ifdef DEBUG_PRINT
307         printf("%s: mode=%d, use_cache=%d, is_cache=%d\n", __func__, mode, ps->use_cache, ps->is_cached);
308 #endif
309
310         ps->bufsize = bufsize;
311         ps->buffer = buffer;
312         ps->mode = mode;
313
314         const uint rect_len = (uint)(BLI_rcti_size_x(input) * BLI_rcti_size_y(input));
315         ps->dst.clip_rect = *input;
316         ps->dst.rect_len = rect_len;
317
318         /* Restrict OpenGL operations for when we don't have cache */
319         if (ps->is_cached == false) {
320                 gpuPushAttrib(GPU_DEPTH_BUFFER_BIT | GPU_VIEWPORT_BIT);
321
322                 /* disable writing to the framebuffer */
323                 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
324
325                 glEnable(GL_DEPTH_TEST);
326                 glDepthMask(GL_TRUE);
327
328                 if (mode == GPU_SELECT_PICK_ALL) {
329                         /* Note that other depth settings (such as #GL_LEQUAL) work too,
330                          * since the depth is always cleared.
331                          * Noting this for cases when depth picking is used where drawing calls change depth settings. */
332                         glDepthFunc(GL_ALWAYS);
333                 }
334                 else {
335                         glDepthFunc(GL_LEQUAL);
336                 }
337
338                 float viewport[4];
339                 glGetFloatv(GL_VIEWPORT, viewport);
340
341                 ps->src.clip_rect = *input;
342                 ps->src.rect_len = rect_len;
343
344                 ps->gl.clip_readpixels[0] = (int)viewport[0];
345                 ps->gl.clip_readpixels[1] = (int)viewport[1];
346                 ps->gl.clip_readpixels[2] = BLI_rcti_size_x(&ps->src.clip_rect);
347                 ps->gl.clip_readpixels[3] = BLI_rcti_size_y(&ps->src.clip_rect);
348
349                 glViewport(UNPACK4(ps->gl.clip_readpixels));
350
351                 /* It's possible we don't want to clear depth buffer,
352                  * so existing elements are masked by current z-buffer. */
353                 glClear(GL_DEPTH_BUFFER_BIT);
354
355                 /* scratch buffer (read new values here) */
356                 ps->gl.rect_depth_test = depth_buf_malloc(rect_len);
357                 ps->gl.rect_depth = depth_buf_malloc(rect_len);
358
359                 /* set initial 'far' value */
360 #if 0
361                 glReadPixels(UNPACK4(ps->gl.clip_readpixels), GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, ps->gl.rect_depth->buf);
362 #else
363                 for (uint i = 0; i < rect_len; i++) {
364                         ps->gl.rect_depth->buf[i] = DEPTH_MAX;
365                 }
366 #endif
367
368                 ps->gl.is_init = false;
369                 ps->gl.prev_id = 0;
370         }
371         else {
372                 /* Using cache (ps->is_cached == true) */
373                 /* src.clip_rect -> dst.clip_rect */
374                 rect_subregion_stride_calc(&ps->src.clip_rect, &ps->dst.clip_rect, &ps->cache.sub_rect);
375                 BLI_assert(ps->gl.rect_depth == NULL);
376                 BLI_assert(ps->gl.rect_depth_test == NULL);
377         }
378
379         if (mode == GPU_SELECT_PICK_ALL) {
380                 ps->all.hits = MEM_mallocN(sizeof(*ps->all.hits) * ALLOC_DEPTHS, __func__);
381                 ps->all.hits_len = 0;
382                 ps->all.hits_len_alloc = ALLOC_DEPTHS;
383         }
384         else {
385                 /* Set to 0xff for SELECT_ID_NONE */
386                 ps->nearest.rect_id = MEM_mallocN(sizeof(uint) * ps->dst.rect_len, __func__);
387                 memset(ps->nearest.rect_id, 0xff, sizeof(uint) * ps->dst.rect_len);
388         }
389 }
390
391 /**
392  * Given 2x depths, we know are different - update the depth information
393  * use for both cached/uncached depth buffers.
394  */
395 static void gpu_select_load_id_pass_all(const DepthBufCache *rect_curr)
396 {
397         GPUPickState *ps = &g_pick_state;
398         const uint id = rect_curr->id;
399         /* find the best depth for this pass and store in 'all.hits' */
400         depth_t depth_best = DEPTH_MAX;
401
402 #define EVAL_TEST() \
403         if (depth_best > *curr) { \
404                 depth_best = *curr; \
405         } ((void)0)
406
407         if (ps->is_cached == false) {
408                 const depth_t *curr = rect_curr->buf;
409                 BLI_assert(ps->src.rect_len == ps->dst.rect_len);
410                 const uint rect_len = ps->src.rect_len;
411                 for (uint i = 0; i < rect_len; i++, curr++) {
412                         EVAL_TEST();
413                 }
414         }
415         else {
416                 /* same as above but different rect sizes */
417                 const depth_t *curr = rect_curr->buf + ps->cache.sub_rect.start;
418                 for (uint i = 0; i < ps->cache.sub_rect.span_len; i++) {
419                         const depth_t *curr_end = curr + ps->cache.sub_rect.span;
420                         for (; curr < curr_end; curr++) {
421                                 EVAL_TEST();
422                         }
423                         curr += ps->cache.sub_rect.skip;
424                 }
425         }
426
427 #undef EVAL_TEST
428
429         /* ensure enough space */
430         if (UNLIKELY(ps->all.hits_len == ps->all.hits_len_alloc)) {
431                 ps->all.hits_len_alloc += ALLOC_DEPTHS;
432                 ps->all.hits = MEM_reallocN(ps->all.hits, ps->all.hits_len_alloc * sizeof(*ps->all.hits));
433         }
434         DepthID *d = &ps->all.hits[ps->all.hits_len++];
435         d->id = id;
436         d->depth = depth_best;
437 }
438
439 static void gpu_select_load_id_pass_nearest(const DepthBufCache *rect_prev, const DepthBufCache *rect_curr)
440 {
441         GPUPickState *ps = &g_pick_state;
442         const uint id = rect_curr->id;
443         /* keep track each pixels ID in 'nearest.rect_id' */
444         if (id != SELECT_ID_NONE) {
445                 uint *id_ptr = ps->nearest.rect_id;
446
447                 /* Check against DEPTH_MAX because XRAY will clear the buffer,
448                  * so previously set values will become unset.
449                  * In this case just leave those id's left as-is. */
450 #define EVAL_TEST() \
451                 if (depth_is_filled(prev, curr)) { \
452                         *id_ptr = id; \
453                 } ((void)0)
454
455                 if (ps->is_cached == false) {
456                         const depth_t *prev = rect_prev->buf;
457                         const depth_t *curr = rect_curr->buf;
458                         BLI_assert(ps->src.rect_len == ps->dst.rect_len);
459                         const uint rect_len = ps->src.rect_len;
460                         for (uint i = 0; i < rect_len; i++, curr++, prev++, id_ptr++) {
461                                 EVAL_TEST();
462                         }
463                 }
464                 else {
465                         /* same as above but different rect sizes */
466                         const depth_t *prev = rect_prev->buf + ps->cache.sub_rect.start;
467                         const depth_t *curr = rect_curr->buf + ps->cache.sub_rect.start;
468                         for (uint i = 0; i < ps->cache.sub_rect.span_len; i++) {
469                                 const depth_t *curr_end = curr + ps->cache.sub_rect.span;
470                                 for (; curr < curr_end; prev++, curr++, id_ptr++) {
471                                         EVAL_TEST();
472                                 }
473                                 prev += ps->cache.sub_rect.skip;
474                                 curr += ps->cache.sub_rect.skip;
475                         }
476                 }
477
478 #undef EVAL_TEST
479         }
480 }
481
482
483 bool gpu_select_pick_load_id(uint id)
484 {
485         GPUPickState *ps = &g_pick_state;
486         if (ps->gl.is_init) {
487                 const uint rect_len = ps->src.rect_len;
488                 glReadPixels(UNPACK4(ps->gl.clip_readpixels), GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, ps->gl.rect_depth_test->buf);
489                 /* perform initial check since most cases the array remains unchanged  */
490
491                 bool do_pass = false;
492                 if (g_pick_state.mode == GPU_SELECT_PICK_ALL) {
493                         if (depth_buf_rect_depth_any(ps->gl.rect_depth_test, rect_len)) {
494                                 ps->gl.rect_depth_test->id = ps->gl.prev_id;
495                                 gpu_select_load_id_pass_all(ps->gl.rect_depth_test);
496                                 do_pass = true;
497                         }
498                 }
499                 else {
500                         if (depth_buf_rect_depth_any_filled(ps->gl.rect_depth, ps->gl.rect_depth_test, rect_len)) {
501                                 ps->gl.rect_depth_test->id = ps->gl.prev_id;
502                                 gpu_select_load_id_pass_nearest(ps->gl.rect_depth, ps->gl.rect_depth_test);
503                                 do_pass = true;
504                         }
505                 }
506
507                 if (do_pass) {
508                         /* Store depth in cache */
509                         if (ps->use_cache) {
510                                 BLI_addtail(&ps->cache.bufs, ps->gl.rect_depth);
511                                 ps->gl.rect_depth = depth_buf_malloc(ps->src.rect_len);
512                         }
513
514                         SWAP(DepthBufCache *, ps->gl.rect_depth, ps->gl.rect_depth_test);
515
516                         if (g_pick_state.mode == GPU_SELECT_PICK_ALL) {
517                                 /* we want new depths every time */
518                                 glClear(GL_DEPTH_BUFFER_BIT);
519                         }
520                 }
521         }
522
523         ps->gl.is_init = true;
524         ps->gl.prev_id = id;
525
526         return true;
527 }
528
529 uint gpu_select_pick_end(void)
530 {
531         GPUPickState *ps = &g_pick_state;
532
533 #ifdef DEBUG_PRINT
534         printf("%s\n", __func__);
535 #endif
536
537         if (ps->is_cached == false) {
538                 if (ps->gl.is_init) {
539                         /* force finishing last pass */
540                         gpu_select_pick_load_id(ps->gl.prev_id);
541                 }
542                 gpuPopAttrib();
543                 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
544         }
545
546         /* assign but never free directly since it may be in cache */
547         DepthBufCache *rect_depth_final;
548
549         /* Store depth in cache */
550         if (ps->use_cache && !ps->is_cached) {
551                 BLI_addtail(&ps->cache.bufs, ps->gl.rect_depth);
552                 ps->gl.rect_depth = NULL;
553                 rect_depth_final = ps->cache.bufs.last;
554         }
555         else if (ps->is_cached) {
556                 rect_depth_final = ps->cache.bufs.last;
557         }
558         else {
559                 /* common case, no cache */
560                 rect_depth_final = ps->gl.rect_depth;
561         }
562
563         uint maxhits = g_pick_state.bufsize;
564         DepthID *depth_data;
565         uint depth_data_len = 0;
566
567         if (g_pick_state.mode == GPU_SELECT_PICK_ALL) {
568                 depth_data = ps->all.hits;
569                 depth_data_len = ps->all.hits_len;
570                 /* move ownership */
571                 ps->all.hits = NULL;
572                 ps->all.hits_len = 0;
573                 ps->all.hits_len_alloc = 0;
574         }
575         else {
576                 /* GPU_SELECT_PICK_NEAREST */
577
578                 /* Over alloc (unlikely we have as many depths as pixels) */
579                 uint depth_data_len_first_pass = 0;
580                 depth_data = MEM_mallocN(ps->dst.rect_len * sizeof(*depth_data), __func__);
581
582                 /* Partially de-duplicating copy,
583                  * when contiguous ID's are found - update their closest depth.
584                  * This isn't essential but means there is less data to sort. */
585
586 #define EVAL_TEST(i_src, i_dst) \
587                 { \
588                         const uint id = ps->nearest.rect_id[i_dst]; \
589                         if (id != SELECT_ID_NONE) { \
590                                 const depth_t depth = rect_depth_final->buf[i_src]; \
591                                 if (depth_last == NULL || depth_last->id != id) { \
592                                         DepthID *d = &depth_data[depth_data_len_first_pass++]; \
593                                         d->id = id; \
594                                         d->depth = depth; \
595                                 } \
596                                 else if (depth_last->depth > depth) { \
597                                         depth_last->depth = depth; \
598                                 } \
599                         } \
600                 } ((void)0)
601
602                 {
603                         DepthID *depth_last = NULL;
604                         if (ps->is_cached == false) {
605                                 for (uint i = 0; i < ps->src.rect_len; i++) {
606                                         EVAL_TEST(i, i);
607                                 }
608                         }
609                         else {
610                                 /* same as above but different rect sizes */
611                                 uint i_src = ps->cache.sub_rect.start, i_dst = 0;
612                                 for (uint j = 0; j < ps->cache.sub_rect.span_len; j++) {
613                                         const uint i_src_end = i_src + ps->cache.sub_rect.span;
614                                         for (; i_src < i_src_end; i_src++, i_dst++) {
615                                                 EVAL_TEST(i_src, i_dst);
616                                         }
617                                         i_src += ps->cache.sub_rect.skip;
618                                 }
619                         }
620                 }
621
622 #undef EVAL_TEST
623
624                 qsort(depth_data, depth_data_len_first_pass, sizeof(DepthID), depth_id_cmp);
625
626                 /* Sort by ID's then keep the best depth for each ID */
627                 depth_data_len = 0;
628                 {
629                         DepthID *depth_last = NULL;
630                         for (uint i = 0; i < depth_data_len_first_pass; i++) {
631                                 if (depth_last == NULL || depth_last->id != depth_data[i].id) {
632                                         depth_last = &depth_data[depth_data_len++];
633                                         *depth_last = depth_data[i];
634                                 }
635                                 else if (depth_last->depth > depth_data[i].depth) {
636                                         depth_last->depth = depth_data[i].depth;
637                                 }
638                         }
639                 }
640         }
641
642         /* Finally sort each unique (id, depth) pair by depth
643          * so the final hit-list is sorted by depth (nearest first) */
644         uint hits = 0;
645
646         if (depth_data_len > maxhits) {
647                 hits = (uint)-1;
648         }
649         else {
650                 /* leave sorting up to the caller */
651                 qsort(depth_data, depth_data_len, sizeof(DepthID), depth_cmp);
652
653                 for (uint i = 0; i < depth_data_len; i++) {
654 #ifdef DEBUG_PRINT
655                         printf("  hit: %u: depth %u\n", depth_data[i].id,  depth_data[i].depth);
656 #endif
657                         /* first 3 are dummy values */
658                         g_pick_state.buffer[hits][0] = 1;
659                         g_pick_state.buffer[hits][1] = 0x0;  /* depth_data[i].depth; */ /* unused */
660                         g_pick_state.buffer[hits][2] = 0x0;  /* z-far is currently never used. */
661                         g_pick_state.buffer[hits][3] = depth_data[i].id;
662                         hits++;
663                 }
664                 BLI_assert(hits < maxhits);
665         }
666
667         MEM_freeN(depth_data);
668
669         MEM_SAFE_FREE(ps->gl.rect_depth);
670         MEM_SAFE_FREE(ps->gl.rect_depth_test);
671
672         if (g_pick_state.mode == GPU_SELECT_PICK_ALL) {
673                 /* 'hits' already freed as 'depth_data' */
674         }
675         else {
676                 MEM_freeN(ps->nearest.rect_id);
677                 ps->nearest.rect_id = NULL;
678         }
679
680         if (ps->use_cache) {
681                 ps->is_cached = true;
682         }
683
684         return hits;
685 }
686
687 /* ----------------------------------------------------------------------------
688  * Caching
689  *
690  * Support multiple begin/end's reusing depth buffers.
691  */
692
693 void gpu_select_pick_cache_begin(void)
694 {
695         BLI_assert(g_pick_state.use_cache == false);
696 #ifdef DEBUG_PRINT
697         printf("%s\n", __func__);
698 #endif
699         g_pick_state.use_cache = true;
700         g_pick_state.is_cached = false;
701 }
702
703 void gpu_select_pick_cache_end(void)
704 {
705 #ifdef DEBUG_PRINT
706         printf("%s: with %d buffers\n", __func__, BLI_listbase_count(&g_pick_state.cache.bufs));
707 #endif
708         g_pick_state.use_cache = false;
709         g_pick_state.is_cached = false;
710
711         BLI_freelistN(&g_pick_state.cache.bufs);
712 }
713
714 /* is drawing needed? */
715 bool gpu_select_pick_is_cached(void)
716 {
717         return g_pick_state.is_cached;
718 }
719
720 void gpu_select_pick_cache_load_id(void)
721 {
722         BLI_assert(g_pick_state.is_cached == true);
723         GPUPickState *ps = &g_pick_state;
724 #ifdef DEBUG_PRINT
725         printf("%s (building depth from cache)\n", __func__);
726 #endif
727         for (DepthBufCache *rect_depth = ps->cache.bufs.first; rect_depth; rect_depth = rect_depth->next) {
728                 if (rect_depth->next != NULL) {
729                         /* we know the buffers differ, but this sub-region may not.
730                          * double check before adding an id-pass */
731                         if (g_pick_state.mode == GPU_SELECT_PICK_ALL) {
732                                 if (depth_buf_subrect_depth_any(rect_depth->next, &ps->cache.sub_rect)) {
733                                         gpu_select_load_id_pass_all(rect_depth->next);
734                                 }
735                         }
736                         else {
737                                 if (depth_buf_subrect_depth_any_filled(rect_depth, rect_depth->next, &ps->cache.sub_rect)) {
738                                         gpu_select_load_id_pass_nearest(rect_depth, rect_depth->next);
739                                 }
740                         }
741                 }
742         }
743 }