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