Merge branch 'blender2.7'
[blender.git] / source / blender / gpu / intern / gpu_select.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) 2014 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Antony Riakiotakis.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/gpu/intern/gpu_select.c
27  *  \ingroup gpu
28  *
29  * Interface for accessing gpu-related methods for selection. The semantics will be
30  * similar to glRenderMode(GL_SELECT) since the goal is to maintain compatibility.
31  */
32 #include <stdlib.h>
33
34 #include "GPU_select.h"
35 #include "GPU_extensions.h"
36 #include "GPU_glew.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_userdef_types.h"
41
42 #include "BLI_utildefines.h"
43
44 #include "gpu_select_private.h"
45
46 /* Internal algorithm used */
47 enum {
48         /** GL_SELECT, legacy OpenGL selection */
49         ALGO_GL_LEGACY = 1,
50         /** glBegin/EndQuery(GL_SAMPLES_PASSED... ), `gpu_select_query.c`
51          * Only sets 4th component (ID) correctly. */
52         ALGO_GL_QUERY = 2,
53         /** Read depth buffer for every drawing pass and extract depths, `gpu_select_pick.c`
54          * Only sets 4th component (ID) correctly. */
55         ALGO_GL_PICK = 3,
56 };
57
58 typedef struct GPUSelectState {
59         /* To ignore selection id calls when not initialized */
60         bool select_is_active;
61         /* flag to cache user preference for occlusion based selection */
62         bool use_gpu_select;
63         /* mode of operation */
64         char mode;
65         /* internal algorithm for selection */
66         char algorithm;
67         /* allow GPU_select_begin/end without drawing */
68         bool use_cache;
69 } GPUSelectState;
70
71 static GPUSelectState g_select_state = {0};
72
73 /**
74  * initialize and provide buffer for results
75  */
76 void GPU_select_begin(uint *buffer, uint bufsize, const rcti *input, char mode, int oldhits)
77 {
78         if (mode == GPU_SELECT_NEAREST_SECOND_PASS) {
79                 /* In the case hits was '-1', don't start the second pass since it's not going to give useful results.
80                  * As well as buffer overflow in 'gpu_select_query_load_id'. */
81                 BLI_assert(oldhits != -1);
82         }
83
84         g_select_state.select_is_active = true;
85         g_select_state.use_gpu_select = GPU_select_query_check_active();
86         g_select_state.mode = mode;
87
88         if (ELEM(g_select_state.mode, GPU_SELECT_PICK_ALL, GPU_SELECT_PICK_NEAREST)) {
89                 g_select_state.algorithm = ALGO_GL_PICK;
90         }
91         else if (!g_select_state.use_gpu_select) {
92                 g_select_state.algorithm = ALGO_GL_LEGACY;
93         }
94         else {
95                 g_select_state.algorithm = ALGO_GL_QUERY;
96         }
97
98         switch (g_select_state.algorithm) {
99                 case ALGO_GL_LEGACY:
100                 {
101                         g_select_state.use_cache = false;
102                         glSelectBuffer(bufsize, (GLuint *)buffer);
103                         glRenderMode(GL_SELECT);
104                         glInitNames();
105                         glPushName(-1);
106                         break;
107                 }
108                 case ALGO_GL_QUERY:
109                 {
110                         g_select_state.use_cache = false;
111                         gpu_select_query_begin((uint (*)[4])buffer, bufsize / 4, input, mode, oldhits);
112                         break;
113                 }
114                 default:  /* ALGO_GL_PICK */
115                 {
116                         gpu_select_pick_begin((uint (*)[4])buffer, bufsize / 4, input, mode);
117                         break;
118                 }
119         }
120 }
121
122 /**
123  * loads a new selection id and ends previous query, if any. In second pass of selection it also returns
124  * if id has been hit on the first pass already.
125  * Thus we can skip drawing un-hit objects.
126  *
127  * \warning We rely on the order of object rendering on passes to be the same for this to work.
128  */
129 bool GPU_select_load_id(uint id)
130 {
131         /* if no selection mode active, ignore */
132         if (!g_select_state.select_is_active)
133                 return true;
134
135         switch (g_select_state.algorithm) {
136                 case ALGO_GL_LEGACY:
137                 {
138                         glLoadName(id);
139                         return true;
140                 }
141                 case ALGO_GL_QUERY:
142                 {
143                         return gpu_select_query_load_id(id);
144                 }
145                 default:  /* ALGO_GL_PICK */
146                 {
147                         return gpu_select_pick_load_id(id);
148                 }
149         }
150 }
151
152 /**
153  * Cleanup and flush selection results to buffer.
154  * Return number of hits and hits in buffer.
155  * if \a dopass is true, we will do a second pass with occlusion queries to get the closest hit.
156  */
157 uint GPU_select_end(void)
158 {
159         uint hits = 0;
160
161         switch (g_select_state.algorithm) {
162                 case ALGO_GL_LEGACY:
163                 {
164                         glPopName();
165                         hits = glRenderMode(GL_RENDER);
166                         break;
167                 }
168                 case ALGO_GL_QUERY:
169                 {
170                         hits = gpu_select_query_end();
171                         break;
172                 }
173                 default:  /* ALGO_GL_PICK */
174                 {
175                         hits = gpu_select_pick_end();
176                         break;
177                 }
178         }
179
180         g_select_state.select_is_active = false;
181
182         return hits;
183 }
184
185 /**
186  * has user activated?
187  */
188 bool GPU_select_query_check_active(void)
189 {
190         return ELEM(U.gpu_select_method, USER_SELECT_USE_OCCLUSION_QUERY, USER_SELECT_AUTO);
191 }
192
193 /* ----------------------------------------------------------------------------
194  * Caching
195  *
196  * Support multiple begin/end's as long as they are within the initial region.
197  * Currently only used by ALGO_GL_PICK.
198  */
199
200 void GPU_select_cache_begin(void)
201 {
202         /* validate on GPU_select_begin, clear if not supported */
203         BLI_assert(g_select_state.use_cache == false);
204         g_select_state.use_cache = true;
205         if (g_select_state.algorithm == ALGO_GL_PICK) {
206                 gpu_select_pick_cache_begin();
207         }
208 }
209
210 void GPU_select_cache_load_id(void)
211 {
212         BLI_assert(g_select_state.use_cache == true);
213         if (g_select_state.algorithm == ALGO_GL_PICK) {
214                 gpu_select_pick_cache_load_id();
215         }
216 }
217
218 void GPU_select_cache_end(void)
219 {
220         if (g_select_state.algorithm == ALGO_GL_PICK) {
221                 gpu_select_pick_cache_end();
222         }
223         g_select_state.use_cache = false;
224 }
225
226 bool GPU_select_is_cached(void)
227 {
228         return g_select_state.use_cache && gpu_select_pick_is_cached();
229 }
230
231
232 /* ----------------------------------------------------------------------------
233  * Utilities
234  */
235
236 /**
237  * Helper function, nothing special but avoids doing inline since hit's aren't sorted by depth
238  * and purpose of 4x buffer indices isn't so clear.
239  *
240  * Note that comparing depth as uint is fine.
241  */
242 const uint *GPU_select_buffer_near(const uint *buffer, int hits)
243 {
244         const uint *buffer_near = NULL;
245         uint depth_min = (uint) - 1;
246         for (int i = 0; i < hits; i++) {
247                 if (buffer[1] < depth_min) {
248                         BLI_assert(buffer[3] != -1);
249                         depth_min = buffer[1];
250                         buffer_near = buffer;
251                 }
252                 buffer += 4;
253         }
254         return buffer_near;
255 }