Merge branch 'blender2.7'
[blender.git] / source / blender / gpu / intern / gpu_select_sample_query.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_sample_query.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
33 #include <stdlib.h>
34
35 #include "GPU_immediate.h"
36 #include "GPU_draw.h"
37 #include "GPU_select.h"
38 #include "GPU_extensions.h"
39 #include "GPU_glew.h"
40
41 #include "MEM_guardedalloc.h"
42
43 #include "BLI_rect.h"
44
45 #include "BLI_utildefines.h"
46
47 #include "PIL_time.h"
48
49 #include "gpu_select_private.h"
50
51
52 /* Ad hoc number of queries to allocate to skip doing many glGenQueries */
53 #define ALLOC_QUERIES 200
54
55 typedef struct GPUQueryState {
56         /* Tracks whether a query has been issued so that gpu_load_id can end the previous one */
57         bool query_issued;
58         /* array holding the OpenGL query identifiers */
59         uint *queries;
60         /* array holding the id corresponding to each query */
61         uint *id;
62         /* number of queries in *queries and *id */
63         uint num_of_queries;
64         /* index to the next query to start */
65         uint active_query;
66         /* cache on initialization */
67         uint (*buffer)[4];
68         /* buffer size (stores number of integers, for actual size multiply by sizeof integer)*/
69         uint bufsize;
70         /* mode of operation */
71         char mode;
72         uint index;
73         int oldhits;
74 } GPUQueryState;
75
76 static GPUQueryState g_query_state = {0};
77
78
79 void gpu_select_query_begin(
80         uint (*buffer)[4], uint bufsize,
81         const rcti *input, char mode,
82         int oldhits)
83 {
84         float viewport[4];
85
86         g_query_state.query_issued = false;
87         g_query_state.active_query = 0;
88         g_query_state.num_of_queries = 0;
89         g_query_state.bufsize = bufsize;
90         g_query_state.buffer = buffer;
91         g_query_state.mode = mode;
92         g_query_state.index = 0;
93         g_query_state.oldhits = oldhits;
94
95         g_query_state.num_of_queries = ALLOC_QUERIES;
96
97         g_query_state.queries = MEM_mallocN(g_query_state.num_of_queries * sizeof(*g_query_state.queries), "gpu selection queries");
98         g_query_state.id = MEM_mallocN(g_query_state.num_of_queries * sizeof(*g_query_state.id), "gpu selection ids");
99         glGenQueries(g_query_state.num_of_queries, g_query_state.queries);
100
101         gpuPushAttrib(GPU_DEPTH_BUFFER_BIT | GPU_VIEWPORT_BIT | GPU_SCISSOR_BIT);
102         /* disable writing to the framebuffer */
103         glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
104
105         /* In order to save some fill rate we minimize the viewport using rect.
106          * We need to get the region of the viewport so that our geometry doesn't
107          * get rejected before the depth test. Should probably cull rect against
108          * the viewport but this is a rare case I think */
109         glGetFloatv(GL_VIEWPORT, viewport);
110         glViewport(viewport[0], viewport[1], BLI_rcti_size_x(input), BLI_rcti_size_y(input));
111
112         /* occlusion queries operates on fragments that pass tests and since we are interested on all
113          * objects in the view frustum independently of their order, we need to disable the depth test */
114         if (mode == GPU_SELECT_ALL) {
115                 glDisable(GL_DEPTH_TEST);
116                 glDepthMask(GL_FALSE);
117         }
118         else if (mode == GPU_SELECT_NEAREST_FIRST_PASS) {
119                 glClear(GL_DEPTH_BUFFER_BIT);
120                 glEnable(GL_DEPTH_TEST);
121                 glDepthMask(GL_TRUE);
122                 glDepthFunc(GL_LEQUAL);
123         }
124         else if (mode == GPU_SELECT_NEAREST_SECOND_PASS) {
125                 glEnable(GL_DEPTH_TEST);
126                 glDepthMask(GL_FALSE);
127                 glDepthFunc(GL_EQUAL);
128         }
129 }
130
131 bool gpu_select_query_load_id(uint id)
132 {
133         if (g_query_state.query_issued) {
134                 glEndQuery(GL_SAMPLES_PASSED);
135         }
136         /* if required, allocate extra queries */
137         if (g_query_state.active_query == g_query_state.num_of_queries) {
138                 g_query_state.num_of_queries += ALLOC_QUERIES;
139                 g_query_state.queries = MEM_reallocN(g_query_state.queries, g_query_state.num_of_queries * sizeof(*g_query_state.queries));
140                 g_query_state.id = MEM_reallocN(g_query_state.id, g_query_state.num_of_queries * sizeof(*g_query_state.id));
141                 glGenQueries(ALLOC_QUERIES, &g_query_state.queries[g_query_state.active_query]);
142         }
143
144         glBeginQuery(GL_SAMPLES_PASSED, g_query_state.queries[g_query_state.active_query]);
145         g_query_state.id[g_query_state.active_query] = id;
146         g_query_state.active_query++;
147         g_query_state.query_issued = true;
148
149         if (g_query_state.mode == GPU_SELECT_NEAREST_SECOND_PASS) {
150                 /* Second pass should never run if first pass fails, can read past 'bufsize' in this case. */
151                 BLI_assert(g_query_state.oldhits != -1);
152                 if (g_query_state.index < g_query_state.oldhits) {
153                         if (g_query_state.buffer[g_query_state.index][3] == id) {
154                                 g_query_state.index++;
155                                 return true;
156                         }
157                         else {
158                                 return false;
159                         }
160                 }
161         }
162
163         return true;
164 }
165
166 uint gpu_select_query_end(void)
167 {
168         int i;
169
170         uint hits = 0;
171         const uint maxhits = g_query_state.bufsize;
172
173         if (g_query_state.query_issued) {
174                 glEndQuery(GL_SAMPLES_PASSED);
175         }
176
177         for (i = 0; i < g_query_state.active_query; i++) {
178                 uint result = 0;
179                 /* Wait until the result is available. */
180                 while (result == 0) {
181                         glGetQueryObjectuiv(g_query_state.queries[i], GL_QUERY_RESULT_AVAILABLE, &result);
182                         if (result == 0) {
183                                 /* (fclem) Not sure if this is better than calling
184                                  * glGetQueryObjectuiv() indefinitely. */
185                                 PIL_sleep_ms(1);
186                         }
187                 }
188                 glGetQueryObjectuiv(g_query_state.queries[i], GL_QUERY_RESULT, &result);
189                 if (result > 0) {
190                         if (g_query_state.mode != GPU_SELECT_NEAREST_SECOND_PASS) {
191
192                                 if (hits < maxhits) {
193                                         g_query_state.buffer[hits][0] = 1;
194                                         g_query_state.buffer[hits][1] = 0xFFFF;
195                                         g_query_state.buffer[hits][2] = 0xFFFF;
196                                         g_query_state.buffer[hits][3] = g_query_state.id[i];
197
198                                         hits++;
199                                 }
200                                 else {
201                                         hits = -1;
202                                         break;
203                                 }
204                         }
205                         else {
206                                 int j;
207                                 /* search in buffer and make selected object first */
208                                 for (j = 0; j < g_query_state.oldhits; j++) {
209                                         if (g_query_state.buffer[j][3] == g_query_state.id[i]) {
210                                                 g_query_state.buffer[j][1] = 0;
211                                                 g_query_state.buffer[j][2] = 0;
212                                         }
213                                 }
214                                 break;
215                         }
216                 }
217         }
218
219         glDeleteQueries(g_query_state.num_of_queries, g_query_state.queries);
220         MEM_freeN(g_query_state.queries);
221         MEM_freeN(g_query_state.id);
222         gpuPopAttrib();
223         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
224
225         return hits;
226 }