| 
									
										
										
										
											2018-07-17 14:46:44 +02: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) 2016 by Mike Erwin. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup gpu | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  |  * GPU shader interface (C --> GLSL) | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-03-02 21:27:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-31 16:54:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  | #include "BLI_span.hh"
 | 
					
						
							|  |  |  | #include "BLI_vector.hh"
 | 
					
						
							| 
									
										
										
										
											2020-07-30 23:49:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  | #include "gpu_shader_interface.hh"
 | 
					
						
							| 
									
										
										
										
											2017-03-02 21:27:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  | namespace blender::gpu { | 
					
						
							| 
									
										
										
										
											2020-06-04 13:43:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  | ShaderInterface::ShaderInterface() | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-19 14:32:41 +10:00
										 |  |  |   /* TODO(fclem): add unique ID for debugging. */ | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-15 19:19:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  | ShaderInterface::~ShaderInterface() | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   /* Free memory used by name_buffer. */ | 
					
						
							|  |  |  |   MEM_freeN(name_buffer_); | 
					
						
							|  |  |  |   MEM_freeN(inputs_); | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-06-01 12:26:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  | static void sort_input_list(MutableSpan<ShaderInput> dst) | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   if (dst.size() == 0) { | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2020-06-02 12:11:39 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-06-01 12:26:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   Vector<ShaderInput> inputs_vec = Vector<ShaderInput>(dst.size()); | 
					
						
							|  |  |  |   MutableSpan<ShaderInput> src = inputs_vec.as_mutable_span(); | 
					
						
							|  |  |  |   src.copy_from(dst); | 
					
						
							| 
									
										
										
										
											2017-10-04 17:36:52 +05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   /* Simple sorting by going through the array and selecting the biggest element each time. */ | 
					
						
							|  |  |  |   for (uint i = 0; i < dst.size(); i++) { | 
					
						
							|  |  |  |     ShaderInput *input_src = &src[0]; | 
					
						
							|  |  |  |     for (uint j = 1; j < src.size(); j++) { | 
					
						
							| 
									
										
										
										
											2020-06-02 12:11:39 +02:00
										 |  |  |       if (src[j].name_hash > input_src->name_hash) { | 
					
						
							|  |  |  |         input_src = &src[j]; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-10-06 01:50:16 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-02 12:11:39 +02:00
										 |  |  |     dst[i] = *input_src; | 
					
						
							|  |  |  |     input_src->name_hash = 0; | 
					
						
							| 
									
										
										
										
											2017-10-06 01:50:16 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-06 01:50:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  | /* Sorts all inputs inside their respective array.
 | 
					
						
							|  |  |  |  * This is to allow fast hash collision detection. | 
					
						
							|  |  |  |  * See ShaderInterface::input_lookup for more details. */ | 
					
						
							|  |  |  | void ShaderInterface::sort_inputs() | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   sort_input_list(MutableSpan<ShaderInput>(inputs_, attr_len_)); | 
					
						
							|  |  |  |   sort_input_list(MutableSpan<ShaderInput>(inputs_ + attr_len_, ubo_len_)); | 
					
						
							|  |  |  |   sort_input_list(MutableSpan<ShaderInput>(inputs_ + attr_len_ + ubo_len_, uniform_len_)); | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-02 21:27:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  | void ShaderInterface::debug_print() | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   Span<ShaderInput> attrs = Span<ShaderInput>(inputs_, attr_len_); | 
					
						
							|  |  |  |   Span<ShaderInput> ubos = Span<ShaderInput>(inputs_ + attr_len_, ubo_len_); | 
					
						
							|  |  |  |   Span<ShaderInput> uniforms = Span<ShaderInput>(inputs_ + attr_len_ + ubo_len_, uniform_len_); | 
					
						
							|  |  |  |   char *name_buf = name_buffer_; | 
					
						
							|  |  |  |   const char format[] = "      | %.8x : %4d : %s\n"; | 
					
						
							| 
									
										
										
										
											2017-10-05 18:26:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   if (attrs.size() > 0) { | 
					
						
							|  |  |  |     printf("\n    Attributes :\n"); | 
					
						
							| 
									
										
										
										
											2019-04-06 14:23:25 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   for (const ShaderInput &attr : attrs) { | 
					
						
							|  |  |  |     printf(format, attr.name_hash, attr.location, name_buf + attr.name_offset); | 
					
						
							| 
									
										
										
										
											2020-06-02 12:11:39 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   if (uniforms.size() > 0) { | 
					
						
							|  |  |  |     printf("\n    Uniforms :\n"); | 
					
						
							| 
									
										
										
										
											2020-06-04 15:28:35 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   for (const ShaderInput &uni : uniforms) { | 
					
						
							|  |  |  |     /* Bypass samplers. */ | 
					
						
							|  |  |  |     if (uni.binding == -1) { | 
					
						
							|  |  |  |       printf(format, uni.name_hash, uni.location, name_buf + uni.name_offset); | 
					
						
							| 
									
										
										
										
											2020-06-02 12:11:39 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   if (ubos.size() > 0) { | 
					
						
							|  |  |  |     printf("\n    Uniform Buffer Objects :\n"); | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   for (const ShaderInput &ubo : ubos) { | 
					
						
							|  |  |  |     printf(format, ubo.name_hash, ubo.binding, name_buf + ubo.name_offset); | 
					
						
							| 
									
										
										
										
											2020-06-02 12:11:39 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-10-06 14:57:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   if (enabled_tex_mask_ > 0) { | 
					
						
							|  |  |  |     printf("\n    Samplers :\n"); | 
					
						
							| 
									
										
										
										
											2020-06-02 12:11:39 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   for (const ShaderInput &samp : uniforms) { | 
					
						
							|  |  |  |     /* Bypass uniforms. */ | 
					
						
							|  |  |  |     if (samp.binding != -1) { | 
					
						
							|  |  |  |       printf(format, samp.name_hash, samp.binding, name_buf + samp.name_offset); | 
					
						
							| 
									
										
										
										
											2020-06-02 12:11:39 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  |   printf("\n"); | 
					
						
							| 
									
										
										
										
											2018-07-17 14:46:44 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-12 17:56:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 13:05:22 +02:00
										 |  |  | }  // namespace blender::gpu
 |