| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-12 02:40:04 +11:00
										 |  |  | /** \file blender/gpu/intern/gpu_select_sample_query.c
 | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |  *  \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 <stdlib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 20:33:23 +02:00
										 |  |  | #include "GPU_immediate.h"
 | 
					
						
							|  |  |  | #include "GPU_draw.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | #include "GPU_select.h"
 | 
					
						
							|  |  |  | #include "GPU_extensions.h"
 | 
					
						
							|  |  |  | #include "GPU_glew.h"
 | 
					
						
							| 
									
										
										
										
											2018-06-04 09:09:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_rect.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-08 18:31:21 +02:00
										 |  |  | #include "PIL_time.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | #include "gpu_select_private.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Ad hoc number of queries to allocate to skip doing many glGenQueries */ | 
					
						
							|  |  |  | #define ALLOC_QUERIES 200
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct GPUQueryState { | 
					
						
							|  |  |  | 	/* 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 */ | 
					
						
							| 
									
										
										
										
											2018-02-28 11:37:39 +11:00
										 |  |  | 	uint *queries; | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 	/* array holding the id corresponding to each query */ | 
					
						
							| 
									
										
										
										
											2018-02-28 11:37:39 +11:00
										 |  |  | 	uint *id; | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 	/* number of queries in *queries and *id */ | 
					
						
							| 
									
										
										
										
											2018-02-28 11:37:39 +11:00
										 |  |  | 	uint num_of_queries; | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 	/* index to the next query to start */ | 
					
						
							| 
									
										
										
										
											2018-02-28 11:37:39 +11:00
										 |  |  | 	uint active_query; | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 	/* cache on initialization */ | 
					
						
							| 
									
										
										
										
											2018-02-28 11:37:39 +11:00
										 |  |  | 	uint (*buffer)[4]; | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 	/* buffer size (stores number of integers, for actual size multiply by sizeof integer)*/ | 
					
						
							| 
									
										
										
										
											2018-02-28 11:37:39 +11:00
										 |  |  | 	uint bufsize; | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 	/* mode of operation */ | 
					
						
							|  |  |  | 	char mode; | 
					
						
							| 
									
										
										
										
											2018-02-28 11:37:39 +11:00
										 |  |  | 	uint index; | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 	int oldhits; | 
					
						
							|  |  |  | } GPUQueryState; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static GPUQueryState g_query_state = {0}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void gpu_select_query_begin( | 
					
						
							| 
									
										
										
										
											2018-02-28 11:37:39 +11:00
										 |  |  |         uint (*buffer)[4], uint bufsize, | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |         const rcti *input, char mode, | 
					
						
							|  |  |  |         int oldhits) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	float viewport[4]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	g_query_state.query_issued = false; | 
					
						
							|  |  |  | 	g_query_state.active_query = 0; | 
					
						
							|  |  |  | 	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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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"); | 
					
						
							|  |  |  | 	glGenQueries(g_query_state.num_of_queries, g_query_state.queries); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 07:46:25 +11:00
										 |  |  | 	gpuPushAttr(GPU_DEPTH_BUFFER_BIT | GPU_VIEWPORT_BIT | GPU_SCISSOR_BIT); | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 	/* 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.
 | 
					
						
							| 
									
										
										
										
											2018-05-30 19:32:16 +02:00
										 |  |  | 	 * We need to get the region of the viewport so that our geometry doesn't | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 	 * get rejected before the depth test. Should probably cull rect against | 
					
						
							| 
									
										
										
										
											2018-05-30 19:32:16 +02:00
										 |  |  | 	 * the viewport but this is a rare case I think */ | 
					
						
							|  |  |  | 	glGetFloatv(GL_VIEWPORT, viewport); | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 	glViewport(viewport[0], viewport[1], BLI_rcti_size_x(input), BLI_rcti_size_y(input)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* 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); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-28 11:37:39 +11:00
										 |  |  | bool gpu_select_query_load_id(uint id) | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (g_query_state.query_issued) { | 
					
						
							|  |  |  | 		glEndQuery(GL_SAMPLES_PASSED); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/* 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)); | 
					
						
							|  |  |  | 		glGenQueries(ALLOC_QUERIES, &g_query_state.queries[g_query_state.active_query]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glBeginQuery(GL_SAMPLES_PASSED, 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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-14 17:03:49 +10:00
										 |  |  | 	if (g_query_state.mode == GPU_SELECT_NEAREST_SECOND_PASS) { | 
					
						
							|  |  |  | 		/* Second pass should never run if first pass fails, can read past 'bufsize' in this case. */ | 
					
						
							|  |  |  | 		BLI_assert(g_query_state.oldhits != -1); | 
					
						
							|  |  |  | 		if (g_query_state.index < g_query_state.oldhits) { | 
					
						
							|  |  |  | 			if (g_query_state.buffer[g_query_state.index][3] == id) { | 
					
						
							|  |  |  | 				g_query_state.index++; | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							|  |  |  | 				return false; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-28 11:37:39 +11:00
										 |  |  | uint gpu_select_query_end(void) | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-28 11:37:39 +11:00
										 |  |  | 	uint hits = 0; | 
					
						
							|  |  |  | 	const uint maxhits = g_query_state.bufsize; | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (g_query_state.query_issued) { | 
					
						
							|  |  |  | 		glEndQuery(GL_SAMPLES_PASSED); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < g_query_state.active_query; i++) { | 
					
						
							| 
									
										
										
										
											2018-04-08 18:31:21 +02:00
										 |  |  | 		uint result = 0; | 
					
						
							| 
									
										
										
										
											2018-04-12 17:36:53 +02:00
										 |  |  | 		/* Wait until the result is available. */ | 
					
						
							| 
									
										
										
										
											2018-04-08 18:31:21 +02:00
										 |  |  | 		while (result == 0) { | 
					
						
							|  |  |  | 			glGetQueryObjectuiv(g_query_state.queries[i], GL_QUERY_RESULT_AVAILABLE, &result); | 
					
						
							|  |  |  | 			if (result == 0) { | 
					
						
							|  |  |  | 				/* (fclem) Not sure if this is better than calling
 | 
					
						
							|  |  |  | 				 * glGetQueryObjectuiv() indefinitely. */ | 
					
						
							|  |  |  | 				PIL_sleep_ms(1); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 		glGetQueryObjectuiv(g_query_state.queries[i], GL_QUERY_RESULT, &result); | 
					
						
							|  |  |  | 		if (result > 0) { | 
					
						
							|  |  |  | 			if (g_query_state.mode != GPU_SELECT_NEAREST_SECOND_PASS) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (hits < maxhits) { | 
					
						
							|  |  |  | 					g_query_state.buffer[hits][0] = 1; | 
					
						
							|  |  |  | 					g_query_state.buffer[hits][1] = 0xFFFF; | 
					
						
							|  |  |  | 					g_query_state.buffer[hits][2] = 0xFFFF; | 
					
						
							|  |  |  | 					g_query_state.buffer[hits][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][3] == g_query_state.id[i]) { | 
					
						
							|  |  |  | 						g_query_state.buffer[j][1] = 0; | 
					
						
							|  |  |  | 						g_query_state.buffer[j][2] = 0; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glDeleteQueries(g_query_state.num_of_queries, g_query_state.queries); | 
					
						
							|  |  |  | 	MEM_freeN(g_query_state.queries); | 
					
						
							|  |  |  | 	MEM_freeN(g_query_state.id); | 
					
						
							| 
									
										
										
										
											2019-01-29 07:46:25 +11:00
										 |  |  | 	gpuPopAttr(); | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return hits; | 
					
						
							|  |  |  | } |