Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gpu_select.c « intern « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1d448231ddaba0bfb912d85c9739c9e792f85a6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2014 Blender Foundation.
 * All rights reserved.
 *
 * Contributor(s): Antony Riakiotakis.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/gpu/intern/gpu_select.c
 *  \ingroup gpu
 *
 * Interface for accessing gpu-related methods for selection. The semantics will be
 * similar to glRenderMode(GL_SELECT) since the goal is to maintain compatibility.
 */
#include "GPU_select.h"
#include "GPU_extensions.h"

#include "BLI_utildefines.h"

#include "MEM_guardedalloc.h"

#include "DNA_userdef_types.h"

#include <GL/glew.h>

/* Ad hoc number of queries to allocate to skip doing many glGenQueries */
#define ALLOC_QUERIES 200

typedef struct GPUQueryState {
	/* To ignore selection id calls when not initialized */
	bool select_is_active;
	/* Tracks whether a query has been issued so that gpu_load_id can end the previous one */
	bool query_issued;
	/* array holding the OpenGL query identifiers */
	unsigned int *queries;
	/* array holding the id corresponding to each query */
	unsigned int *id;
	/* number of queries in *queries and *id */
	unsigned int num_of_queries;
	/* index to the next query to start */
	unsigned int active_query;
	/* flag to cache user preference for occlusion based selection */
	bool use_gpu_select;
	/* cache on initialization */
	unsigned int *buffer;
	unsigned int bufsize;
	/* mode of operation */
	char mode;
	unsigned int index;
	int oldhits;
} GPUQueryState;

static GPUQueryState g_query_state = {0};

void GPU_select_begin(unsigned int *buffer, unsigned int bufsize, rctf *input, char mode, int oldhits)
{
	g_query_state.select_is_active = true;
	g_query_state.query_issued = false;
	g_query_state.active_query = 0;
	g_query_state.use_gpu_select = GPU_select_query_check_active();
	g_query_state.num_of_queries = 0;
	g_query_state.bufsize = bufsize;
	g_query_state.buffer = buffer;
	g_query_state.mode = mode;
	g_query_state.index = 0;
	g_query_state.oldhits = oldhits;

	if (!g_query_state.use_gpu_select) {
		glSelectBuffer( bufsize, (GLuint *)buffer);
		glRenderMode(GL_SELECT);
		glInitNames();
		glPushName(-1);
	}
	else {
		float viewport[4];

		g_query_state.num_of_queries = ALLOC_QUERIES;

		g_query_state.queries = MEM_mallocN(g_query_state.num_of_queries * sizeof(*g_query_state.queries) , "gpu selection queries");
		g_query_state.id = MEM_mallocN(g_query_state.num_of_queries * sizeof(*g_query_state.id) , "gpu selection ids");
		glGenQueriesARB(g_query_state.num_of_queries, g_query_state.queries);

		glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_VIEWPORT_BIT);
		/* disable writing to the framebuffer */
		glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

		/* In order to save some fill rate we minimize the viewport using rect.
		 * We need to get the region of the scissor so that our geometry doesn't
		 * get rejected before the depth test. Should probably cull rect against
		 * scissor for viewport but this is a rare case I think */
		glGetFloatv(GL_SCISSOR_BOX, viewport);
		if (!input || input->xmin == input->xmax) {
			glViewport(viewport[0], viewport[1], 24, 24);
		}
		else {
			glViewport(viewport[0], viewport[1], (int)(input->xmax - input->xmin), (int)(input->ymax - input->ymin));
		}

		/* occlusion queries operates on fragments that pass tests and since we are interested on all
		 * objects in the view frustum independently of their order, we need to disable the depth test */
		if (mode == GPU_SELECT_ALL) {
			glDisable(GL_DEPTH_TEST);
			glDepthMask(GL_FALSE);
		}
		else if (mode == GPU_SELECT_NEAREST_FIRST_PASS) {
			glClear(GL_DEPTH_BUFFER_BIT);
			glEnable(GL_DEPTH_TEST);
			glDepthMask(GL_TRUE);
			glDepthFunc(GL_LEQUAL);
		}
		else if (mode == GPU_SELECT_NEAREST_SECOND_PASS) {
			glEnable(GL_DEPTH_TEST);
			glDepthMask(GL_FALSE);
			glDepthFunc(GL_EQUAL);
		}
	}
}

bool GPU_select_load_id(unsigned int id)
{
	/* if no selection mode active, ignore */
	if(!g_query_state.select_is_active)
		return true;

	if (!g_query_state.use_gpu_select) {
		glLoadName(id);
	}
	else {
		if (g_query_state.query_issued) {
			glEndQueryARB(GL_SAMPLES_PASSED_ARB);
		}
		/* if required, allocate extra queries */
		if (g_query_state.active_query == g_query_state.num_of_queries) {
			g_query_state.num_of_queries += ALLOC_QUERIES;
			g_query_state.queries = MEM_reallocN(g_query_state.queries, g_query_state.num_of_queries * sizeof(*g_query_state.queries));
			g_query_state.id = MEM_reallocN(g_query_state.id, g_query_state.num_of_queries * sizeof(*g_query_state.id));
			glGenQueriesARB(ALLOC_QUERIES, &g_query_state.queries[g_query_state.active_query]);
		}

		glBeginQueryARB(GL_SAMPLES_PASSED_ARB, g_query_state.queries[g_query_state.active_query]);
		g_query_state.id[g_query_state.active_query] = id;
		g_query_state.active_query++;
		g_query_state.query_issued = true;

		if (g_query_state.mode == GPU_SELECT_NEAREST_SECOND_PASS) {
			if (g_query_state.buffer[g_query_state.index * 4 + 3] == id) {
				g_query_state.index++;
				return true;
			}
			else {
				return false;
			}
		}
	}

	return true;
}

unsigned int GPU_select_end(void)
{
	unsigned int hits = 0;
	if (!g_query_state.use_gpu_select) {
		glPopName();
		hits = glRenderMode(GL_RENDER);
	}
	else {
		int i;

		if (g_query_state.query_issued) {
			glEndQueryARB(GL_SAMPLES_PASSED_ARB);
		}

		for (i = 0; i < g_query_state.active_query; i++) {
			unsigned int result;
			glGetQueryObjectuivARB(g_query_state.queries[i], GL_QUERY_RESULT_ARB, &result);
			if (result > 0) {
				if (g_query_state.mode != GPU_SELECT_NEAREST_SECOND_PASS) {
					if(hits < g_query_state.bufsize) {
						g_query_state.buffer[hits * 4] = 1;
						g_query_state.buffer[hits * 4 + 1] = 0xFFFF;
						g_query_state.buffer[hits * 4 + 2] = 0xFFFF;
						g_query_state.buffer[hits * 4 + 3] = g_query_state.id[i];

						hits++;
					}
					else {
						hits = -1;
						break;
					}
				}
				else {
					int j;
					/* search in buffer and make selected object first */
					for (j = 0; j < g_query_state.oldhits; j++) {
						if (g_query_state.buffer[j * 4 + 3] == g_query_state.id[i]) {
							g_query_state.buffer[j * 4 + 1] = 0;
							g_query_state.buffer[j * 4 + 2] = 0;
						}
					}
					break;
				}
			}
		}

		glDeleteQueriesARB(g_query_state.num_of_queries, g_query_state.queries);
		MEM_freeN(g_query_state.queries);
		MEM_freeN(g_query_state.id);
		glPopAttrib();
		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
	}

	g_query_state.select_is_active = false;

	return hits;
}


bool GPU_select_query_check_support(void)
{
	return GLEW_ARB_occlusion_query;
}


bool GPU_select_query_check_active(void)
{
	return GLEW_ARB_occlusion_query &&
	       ((U.gpu_select_method == USER_SELECT_USE_OCCLUSION_QUERY) ||
	        ((U.gpu_select_method == USER_SELECT_AUTO) && GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_ANY, GPU_DRIVER_ANY)));
}