Blender V5.3
gpu_select.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11#include <cstdlib>
12#include <cstring>
13
14#include "GPU_select.hh"
15
16#include "BLI_rect.hh"
17
18#include "BLI_utildefines.hh"
19
20#include "gpu_select_private.hh"
21
22namespace blender {
23
24/* -------------------------------------------------------------------- */
27
28/* Internal algorithm used */
43
45 /* To ignore selection id calls when not initialized */
47 /* mode of operation */
49 /* internal algorithm for selection */
51 /* allow GPU_select_begin/end without drawing */
62};
63
65
67
68/* -------------------------------------------------------------------- */
71
73 const rcti *input,
74 const int radius,
75 GPUSelectMode mode,
76 int oldhits,
77 bool use_select_next)
78{
80 /* In the case hits was '-1',
81 * don't start the second pass since it's not going to give useful results.
82 * As well as buffer overflow in 'gpu_select_query_load_id'. */
83 BLI_assert(oldhits != -1);
84 }
85
87 g_select_state.mode = mode;
88
89 if (use_select_next) {
91 }
94 }
95 else {
97 }
98
99 /* This function is called when cache has already been initialized,
100 * so only manipulate cache values when cache is pending. */
103
104 switch (g_select_state.algorithm) {
105 case ALGO_SELECT_NEXT:
106 case ALGO_SAMPLE_QUERY: {
108 break;
109 }
110 default: {
113 break;
114 }
115 }
116 }
117
118 switch (g_select_state.algorithm) {
119 case ALGO_SELECT_NEXT: {
120 gpu_select_next_begin(buffer, input, radius, mode);
121 break;
122 }
123 case ALGO_SAMPLE_QUERY: {
124 gpu_select_query_begin(buffer, input, mode, oldhits);
125 break;
126 }
127 default: /* ALGO_DEPTH_PICK */
128 {
129 gpu_select_pick_begin(buffer, input, mode);
130 break;
131 }
132 }
133}
134
136 GPUSelectBuffer *buffer, const rcti *input, const int radius, GPUSelectMode mode, int oldhits)
137{
138 gpu_select_begin_ex(buffer, input, radius, mode, oldhits, true);
139}
140
141void GPU_select_begin(GPUSelectBuffer *buffer, const rcti *input, GPUSelectMode mode, int oldhits)
142{
143 gpu_select_begin_ex(buffer, input, 0, mode, oldhits, false);
144}
145
147{
148 /* if no selection mode active, ignore */
150 return true;
151 }
152
153 switch (g_select_state.algorithm) {
154 case ALGO_SELECT_NEXT:
155 /* This shouldn't use this pipeline. */
157 return false;
158
159 case ALGO_SAMPLE_QUERY: {
160 return gpu_select_query_load_id(id);
161 }
162 default: /* ALGO_DEPTH_PICK */
163 {
164 return gpu_select_pick_load_id(id, false);
165 }
166 }
167}
168
170{
171 uint hits = 0;
172
173 switch (g_select_state.algorithm) {
174 case ALGO_SELECT_NEXT: {
175 hits = gpu_select_next_end();
176 break;
177 }
178 case ALGO_SAMPLE_QUERY: {
179 hits = gpu_select_query_end();
180 break;
181 }
182 default: /* ALGO_DEPTH_PICK */
183 {
184 hits = gpu_select_pick_end();
185 break;
186 }
187 }
188
190
191 return hits;
192}
193
195
196/* -------------------------------------------------------------------- */
202
204{
206 /* Ensure #GPU_select_cache_end is always called. */
208
209 /* Signal that cache should be used, instead of calling the algorithms cache-begin function.
210 * This is more convenient as the exact method of selection may not be known by the caller. */
212}
213
221
223{
227 }
229 /* Paranoid assignment, should already be false. */
231}
232
237
239
240/* -------------------------------------------------------------------- */
243int gpu_select_buffer_depth_id_cmp(const void *sel_a_p, const void *sel_b_p)
244{
245 GPUSelectResult *a = static_cast<GPUSelectResult *>(const_cast<void *>(sel_a_p));
246 GPUSelectResult *b = static_cast<GPUSelectResult *>(const_cast<void *>(sel_b_p));
247
248 if (a->depth < b->depth) {
249 return -1;
250 }
251 if (a->depth > b->depth) {
252 return 1;
253 }
254
255 /* Depths match, sort by id. */
256 /* NOTE: this is endianness-sensitive.
257 * GPUSelectResult values are always expected to be little-endian. */
258 uint sel_a = a->id;
259 uint sel_b = b->id;
260
261 if (sel_a < sel_b) {
262 return -1;
263 }
264 if (sel_a > sel_b) {
265 return 1;
266 }
267 return 0;
268}
269
271{
272 const GPUSelectResult *hit_result_near = nullptr;
273 uint depth_min = uint(-1);
274 for (const GPUSelectResult &hit_result : hit_results) {
275 if (hit_result.depth < depth_min) {
276 BLI_assert(hit_result.id != -1);
277 depth_min = hit_result.depth;
278 hit_result_near = &hit_result;
279 }
280 }
281 return hit_result_near;
282}
283
285{
286 uint index_src = 0;
287 uint index_dst = 0;
288 uint hits_final = 0;
289 for (const GPUSelectResult &hit_result : hit_results) {
290 if (hit_result.id != select_id) {
291 if (index_dst != index_src) {
292 hit_results[index_dst] = hit_result;
293 }
294 index_dst++;
295 hits_final++;
296 }
297 index_src++;
298 }
299 return hits_final;
300}
301
302void GPU_select_buffer_stride_realign(const rcti *src, const rcti *dst, uint *r_buf)
303{
304 const int x = dst->xmin - src->xmin;
305 const int y = dst->ymin - src->ymin;
306
307 BLI_assert(src->xmin <= dst->xmin && src->ymin <= dst->ymin && src->xmax >= dst->xmax &&
308 src->ymax >= dst->ymax);
309 BLI_assert(x >= 0 && y >= 0);
310
311 const int src_x = BLI_rcti_size_x(src);
312 const int src_y = BLI_rcti_size_y(src);
313 const int dst_x = BLI_rcti_size_x(dst);
314 const int dst_y = BLI_rcti_size_y(dst);
315
316 BLI_assert(dst_x > 0 && dst_y > 0);
317
318 int last_px_id = src_x * (y + dst_y - 1) + (x + dst_x - 1);
319 memset(&r_buf[last_px_id + 1], 0, (src_x * src_y - (last_px_id + 1)) * sizeof(*r_buf));
320
321 if (last_px_id < 0) {
322 /* Nothing to write. */
323 BLI_assert(last_px_id == -1);
324 return;
325 }
326
327 int last_px_written = dst_x * dst_y - 1;
328 const int skip = src_x - dst_x;
329
330 while (true) {
331 for (int i = dst_x; i--;) {
332 r_buf[last_px_id--] = r_buf[last_px_written--];
333 }
334 if (last_px_written < 0) {
335 break;
336 }
337 last_px_id -= skip;
338 memset(&r_buf[last_px_id + 1], 0, skip * sizeof(*r_buf));
339 }
340 memset(r_buf, 0, (last_px_id + 1) * sizeof(*r_buf));
341}
342
344
345} // namespace blender
#define BLI_assert_unreachable()
#define ELEM(...)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void gpu_select_pick_begin(GPUSelectBuffer *buffer, const rcti *input, GPUSelectMode mode)
unsigned int uint
Definition BKE_text.h:55
bool GPU_select_load_id(unsigned int id)
@ GPU_SELECT_PICK_ALL
Definition GPU_select.hh:27
@ GPU_SELECT_PICK_NEAREST
Definition GPU_select.hh:28
@ GPU_SELECT_NEAREST_SECOND_PASS
Definition GPU_select.hh:25
bool gpu_select_pick_load_id(uint id, bool end)
void gpu_select_pick_cache_begin()
void GPU_select_cache_load_id()
bool gpu_select_query_load_id(uint id)
void GPU_select_begin_next(GPUSelectBuffer *buffer, const rcti *input, int radius, GPUSelectMode mode, int oldhits)
void gpu_select_pick_cache_end()
void GPU_select_buffer_stride_realign(const rcti *src, const rcti *dst, uint *r_buf)
unsigned int GPU_select_end()
int gpu_select_buffer_depth_id_cmp(const void *sel_a_p, const void *sel_b_p)
uint GPU_select_buffer_remove_by_id(MutableSpan< GPUSelectResult > hit_results, uint select_id)
void GPU_select_cache_end()
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
@ ALGO_SAMPLE_QUERY
Definition gpu_select.cc:34
@ ALGO_SELECT_NEXT
Definition gpu_select.cc:41
@ ALGO_DEPTH_PICK
Definition gpu_select.cc:39
bool GPU_select_is_cached()
uint gpu_select_pick_end()
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
bool gpu_select_pick_is_cached()
static GPUSelectState g_select_state
Definition gpu_select.cc:64
void gpu_select_query_begin(GPUSelectBuffer *buffer, const rcti *input, GPUSelectMode mode, int oldhits)
const GPUSelectResult * GPU_select_buffer_near(const Span< GPUSelectResult > hit_results)
void GPU_select_begin(GPUSelectBuffer *buffer, const rcti *input, GPUSelectMode mode, int oldhits)
void gpu_select_pick_cache_load_id()
static void gpu_select_begin_ex(GPUSelectBuffer *buffer, const rcti *input, const int radius, GPUSelectMode mode, int oldhits, bool use_select_next)
Definition gpu_select.cc:72
void GPU_select_cache_begin()
uint gpu_select_next_end()
void gpu_select_next_begin(GPUSelectBuffer *buffer, const rcti *input, const int radius, GPUSelectMode mode)
GPUSelectAlgo algorithm
Definition gpu_select.cc:50