| 
									
										
										
										
											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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup gpu | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Interface for accessing gpu-related methods for selection. The semantics will be | 
					
						
							|  |  |  |  * similar to glRenderMode(GL_SELECT) since the goal is to maintain compatibility. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 11:28:09 +01:00
										 |  |  | #include <cstdlib>
 | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 01:39:16 +02:00
										 |  |  | #include "GPU_debug.h"
 | 
					
						
							| 
									
										
										
										
											2020-08-20 16:57:38 +02:00
										 |  |  | #include "GPU_framebuffer.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | #include "GPU_select.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-11 14:52:57 +11:00
										 |  |  | #include "GPU_state.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"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 23:52:55 +02:00
										 |  |  | #include "BLI_bitmap.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | #include "BLI_utildefines.h"
 | 
					
						
							| 
									
										
										
										
											2020-09-07 23:52:55 +02:00
										 |  |  | #include "BLI_vector.hh"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "gpu_backend.hh"
 | 
					
						
							|  |  |  | #include "gpu_query.hh"
 | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "gpu_select_private.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 23:52:55 +02:00
										 |  |  | using namespace blender; | 
					
						
							|  |  |  | using namespace blender::gpu; | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 12:46:43 +01:00
										 |  |  | struct GPUSelectQueryState { | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |   /* Tracks whether a query has been issued so that gpu_load_id can end the previous one */ | 
					
						
							|  |  |  |   bool query_issued; | 
					
						
							| 
									
										
										
										
											2020-09-07 23:52:55 +02:00
										 |  |  |   /* GPU queries abstraction. Contains an array of queries. */ | 
					
						
							|  |  |  |   QueryPool *queries; | 
					
						
							|  |  |  |   /* Array holding the id corresponding id to each query. */ | 
					
						
							|  |  |  |   Vector<uint> *ids; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2020-08-16 23:20:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Previous state to restore after drawing. */ | 
					
						
							|  |  |  |   int viewport[4]; | 
					
						
							|  |  |  |   int scissor[4]; | 
					
						
							|  |  |  |   eGPUWriteMask write_mask; | 
					
						
							| 
									
										
										
										
											2020-08-20 16:38:34 +02:00
										 |  |  |   eGPUDepthTest depth_test; | 
					
						
							| 
									
										
										
										
											2020-12-04 12:46:43 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:25:30 +01:00
										 |  |  | static GPUSelectQueryState g_query_state = {false}; | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | void gpu_select_query_begin( | 
					
						
							| 
									
										
										
										
											2018-02-28 11:37:39 +11:00
										 |  |  |     uint (*buffer)[4], uint bufsize, const rcti *input, char mode, int oldhits) | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-16 01:39:16 +02:00
										 |  |  |   GPU_debug_group_begin("Selection Queries"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |   g_query_state.query_issued = false; | 
					
						
							|  |  |  |   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; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 23:52:55 +02:00
										 |  |  |   g_query_state.ids = new Vector<uint>(); | 
					
						
							|  |  |  |   g_query_state.queries = GPUBackend::get()->querypool_alloc(); | 
					
						
							|  |  |  |   g_query_state.queries->init(GPU_QUERY_OCCLUSION); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-16 23:20:59 +02:00
										 |  |  |   g_query_state.write_mask = GPU_write_mask_get(); | 
					
						
							| 
									
										
										
										
											2020-08-20 16:38:34 +02:00
										 |  |  |   g_query_state.depth_test = GPU_depth_test_get(); | 
					
						
							| 
									
										
										
										
											2020-08-16 23:20:59 +02:00
										 |  |  |   GPU_scissor_get(g_query_state.scissor); | 
					
						
							| 
									
										
										
										
											2020-08-22 01:41:43 +02:00
										 |  |  |   GPU_viewport_size_get_i(g_query_state.viewport); | 
					
						
							| 
									
										
										
										
											2020-08-16 23:20:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-22 01:41:43 +02:00
										 |  |  |   /* Write to color buffer. Seems to fix issues with selecting alpha blended geom (see T7997). */ | 
					
						
							|  |  |  |   GPU_color_mask(true, true, true, true); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |   /* 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 */ | 
					
						
							| 
									
										
										
										
											2020-08-22 01:41:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   int viewport[4] = { | 
					
						
							|  |  |  |       UNPACK2(g_query_state.viewport), BLI_rcti_size_x(input), BLI_rcti_size_y(input)}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   GPU_viewport(UNPACK4(viewport)); | 
					
						
							|  |  |  |   GPU_scissor(UNPACK4(viewport)); | 
					
						
							|  |  |  |   GPU_scissor_test(false); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |   /* 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) { | 
					
						
							| 
									
										
										
										
											2019-05-14 13:55:35 +02:00
										 |  |  |     /* glQueries on Windows+Intel drivers only works with depth testing turned on.
 | 
					
						
							|  |  |  |      * See T62947 for details */ | 
					
						
							| 
									
										
										
										
											2020-08-20 16:57:38 +02:00
										 |  |  |     GPU_depth_test(GPU_DEPTH_ALWAYS); | 
					
						
							|  |  |  |     GPU_depth_mask(true); | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |   } | 
					
						
							|  |  |  |   else if (mode == GPU_SELECT_NEAREST_FIRST_PASS) { | 
					
						
							| 
									
										
										
										
											2020-08-20 16:57:38 +02:00
										 |  |  |     GPU_depth_test(GPU_DEPTH_LESS_EQUAL); | 
					
						
							|  |  |  |     GPU_depth_mask(true); | 
					
						
							| 
									
										
										
										
											2020-08-23 11:11:27 +02:00
										 |  |  |     GPU_clear_depth(1.0f); | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |   } | 
					
						
							|  |  |  |   else if (mode == GPU_SELECT_NEAREST_SECOND_PASS) { | 
					
						
							| 
									
										
										
										
											2020-08-20 16:57:38 +02:00
										 |  |  |     GPU_depth_test(GPU_DEPTH_EQUAL); | 
					
						
							|  |  |  |     GPU_depth_mask(false); | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2020-09-07 23:52:55 +02:00
										 |  |  |     g_query_state.queries->end_query(); | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 23:52:55 +02:00
										 |  |  |   g_query_state.queries->begin_query(); | 
					
						
							|  |  |  |   g_query_state.ids->append(id); | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |   g_query_state.query_issued = true; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-08-07 12:39:35 +02:00
										 |  |  |       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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-02-28 11:37:39 +11:00
										 |  |  |   uint hits = 0; | 
					
						
							|  |  |  |   const uint maxhits = g_query_state.bufsize; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |   if (g_query_state.query_issued) { | 
					
						
							| 
									
										
										
										
											2020-09-07 23:52:55 +02:00
										 |  |  |     g_query_state.queries->end_query(); | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 23:52:55 +02:00
										 |  |  |   Span<uint> ids = *g_query_state.ids; | 
					
						
							|  |  |  |   Vector<uint32_t> result(ids.size()); | 
					
						
							|  |  |  |   g_query_state.queries->get_occlusion_result(result); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 23:52:55 +02:00
										 |  |  |   for (int i = 0; i < result.size(); i++) { | 
					
						
							|  |  |  |     if (result[i] != 0) { | 
					
						
							|  |  |  |       if (g_query_state.mode != GPU_SELECT_NEAREST_SECOND_PASS) { | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |         if (hits < maxhits) { | 
					
						
							|  |  |  |           g_query_state.buffer[hits][0] = 1; | 
					
						
							|  |  |  |           g_query_state.buffer[hits][1] = 0xFFFF; | 
					
						
							|  |  |  |           g_query_state.buffer[hits][2] = 0xFFFF; | 
					
						
							| 
									
										
										
										
											2020-09-07 23:52:55 +02:00
										 |  |  |           g_query_state.buffer[hits][3] = ids[i]; | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |           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++) { | 
					
						
							| 
									
										
										
										
											2020-09-07 23:52:55 +02:00
										 |  |  |           if (g_query_state.buffer[j][3] == ids[i]) { | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |             g_query_state.buffer[j][1] = 0; | 
					
						
							|  |  |  |             g_query_state.buffer[j][2] = 0; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-07 23:52:55 +02:00
										 |  |  |   delete g_query_state.queries; | 
					
						
							|  |  |  |   delete g_query_state.ids; | 
					
						
							| 
									
										
										
										
											2020-08-16 23:20:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   GPU_write_mask(g_query_state.write_mask); | 
					
						
							|  |  |  |   GPU_depth_test(g_query_state.depth_test); | 
					
						
							|  |  |  |   GPU_viewport(UNPACK4(g_query_state.viewport)); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 01:39:16 +02:00
										 |  |  |   GPU_debug_group_end(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 05:17:55 +11:00
										 |  |  |   return hits; | 
					
						
							|  |  |  | } |