Cleanup: use 'uint' for GPU_select
[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  * Needed when GL context of #GPU_select_end
154  * can't be used to finalize selection operations
155  * (because of context changes).
156  */
157 void GPU_select_finalize(void)
158 {
159         if (!g_select_state.select_is_active)
160                 return;
161
162         switch (g_select_state.algorithm) {
163                 case ALGO_GL_LEGACY:
164                 case ALGO_GL_QUERY:
165                 {
166                         break;
167                 }
168                 default:  /* ALGO_GL_PICK */
169                 {
170                         gpu_select_pick_finalize();
171                 }
172         }
173 }
174
175 /**
176  * Cleanup and flush selection results to buffer.
177  * Return number of hits and hits in buffer.
178  * if \a dopass is true, we will do a second pass with occlusion queries to get the closest hit.
179  */
180 uint GPU_select_end(void)
181 {
182         uint hits = 0;
183
184         switch (g_select_state.algorithm) {
185                 case ALGO_GL_LEGACY:
186                 {
187                         glPopName();
188                         hits = glRenderMode(GL_RENDER);
189                         break;
190                 }
191                 case ALGO_GL_QUERY:
192                 {
193                         hits = gpu_select_query_end();
194                         break;
195                 }
196                 default:  /* ALGO_GL_PICK */
197                 {
198                         hits = gpu_select_pick_end();
199                         break;
200                 }
201         }
202
203         g_select_state.select_is_active = false;
204
205         return hits;
206 }
207
208 /**
209  * has user activated?
210  */
211 bool GPU_select_query_check_active(void)
212 {
213         return ((U.gpu_select_method == USER_SELECT_USE_OCCLUSION_QUERY) ||
214                 ((U.gpu_select_method == USER_SELECT_AUTO) &&
215                  (GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_ANY, GPU_DRIVER_ANY) ||
216                   /* unsupported by nouveau, gallium 0.4, see: T47940 */
217                   GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_UNIX, GPU_DRIVER_OPENSOURCE))));
218
219 }
220
221 /* ----------------------------------------------------------------------------
222  * Caching
223  *
224  * Support multiple begin/end's as long as they are within the initial region.
225  * Currently only used by ALGO_GL_PICK.
226  */
227
228 void GPU_select_cache_begin(void)
229 {
230         /* validate on GPU_select_begin, clear if not supported */
231         BLI_assert(g_select_state.use_cache == false);
232         g_select_state.use_cache = true;
233         if (g_select_state.algorithm == ALGO_GL_PICK) {
234                 gpu_select_pick_cache_begin();
235         }
236 }
237
238 void GPU_select_cache_load_id(void)
239 {
240         BLI_assert(g_select_state.use_cache == true);
241         if (g_select_state.algorithm == ALGO_GL_PICK) {
242                 gpu_select_pick_cache_load_id();
243         }
244 }
245
246 void GPU_select_cache_end(void)
247 {
248         if (g_select_state.algorithm == ALGO_GL_PICK) {
249                 gpu_select_pick_cache_end();
250         }
251         g_select_state.use_cache = false;
252 }
253
254 bool GPU_select_is_cached(void)
255 {
256         return g_select_state.use_cache && gpu_select_pick_is_cached();
257 }