Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * \file linker.cpp |
| 26 | * GLSL linker implementation |
| 27 | * |
| 28 | * Given a set of shaders that are to be linked to generate a final program, |
| 29 | * there are three distinct stages. |
| 30 | * |
| 31 | * In the first stage shaders are partitioned into groups based on the shader |
| 32 | * type. All shaders of a particular type (e.g., vertex shaders) are linked |
| 33 | * together. |
| 34 | * |
| 35 | * - Undefined references in each shader are resolve to definitions in |
| 36 | * another shader. |
| 37 | * - Types and qualifiers of uniforms, outputs, and global variables defined |
| 38 | * in multiple shaders with the same name are verified to be the same. |
| 39 | * - Initializers for uniforms and global variables defined |
| 40 | * in multiple shaders with the same name are verified to be the same. |
| 41 | * |
| 42 | * The result, in the terminology of the GLSL spec, is a set of shader |
| 43 | * executables for each processing unit. |
| 44 | * |
| 45 | * After the first stage is complete, a series of semantic checks are performed |
| 46 | * on each of the shader executables. |
| 47 | * |
| 48 | * - Each shader executable must define a \c main function. |
| 49 | * - Each vertex shader executable must write to \c gl_Position. |
| 50 | * - Each fragment shader executable must write to either \c gl_FragData or |
| 51 | * \c gl_FragColor. |
| 52 | * |
| 53 | * In the final stage individual shader executables are linked to create a |
| 54 | * complete exectuable. |
| 55 | * |
| 56 | * - Types of uniforms defined in multiple shader stages with the same name |
| 57 | * are verified to be the same. |
| 58 | * - Initializers for uniforms defined in multiple shader stages with the |
| 59 | * same name are verified to be the same. |
| 60 | * - Types and qualifiers of outputs defined in one stage are verified to |
| 61 | * be the same as the types and qualifiers of inputs defined with the same |
| 62 | * name in a later stage. |
| 63 | * |
| 64 | * \author Ian Romanick <ian.d.romanick@intel.com> |
| 65 | */ |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 66 | |
Chia-I Wu | bfd7c9a | 2010-08-23 17:51:42 +0800 | [diff] [blame] | 67 | #include "main/core.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 68 | #include "glsl_symbol_table.h" |
Eric Anholt | faf3dba | 2013-06-12 16:57:11 -0700 | [diff] [blame] | 69 | #include "glsl_parser_extras.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 70 | #include "ir.h" |
| 71 | #include "program.h" |
Aras Pranckevicius | 3174715 | 2010-07-29 12:40:49 +0300 | [diff] [blame] | 72 | #include "program/hash_table.h" |
Ian Romanick | 8fe8a81 | 2010-07-13 17:36:13 -0700 | [diff] [blame] | 73 | #include "linker.h" |
Paul Berry | 4b11b57 | 2012-12-17 14:20:35 -0800 | [diff] [blame] | 74 | #include "link_varyings.h" |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 75 | #include "ir_optimization.h" |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 76 | #include "ir_rvalue_visitor.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 77 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 78 | extern "C" { |
| 79 | #include "main/shaderobj.h" |
Eric Anholt | 6065a87 | 2013-06-12 18:12:40 -0700 | [diff] [blame] | 80 | #include "main/enums.h" |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 83 | void linker_error(gl_shader_program *, const char *, ...); |
| 84 | |
Eric Anholt | 10ef949 | 2013-09-20 11:03:44 -0700 | [diff] [blame] | 85 | namespace { |
| 86 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Visitor that determines whether or not a variable is ever written. |
| 89 | */ |
| 90 | class find_assignment_visitor : public ir_hierarchical_visitor { |
| 91 | public: |
| 92 | find_assignment_visitor(const char *name) |
| 93 | : name(name), found(false) |
| 94 | { |
| 95 | /* empty */ |
| 96 | } |
| 97 | |
| 98 | virtual ir_visitor_status visit_enter(ir_assignment *ir) |
| 99 | { |
| 100 | ir_variable *const var = ir->lhs->variable_referenced(); |
| 101 | |
| 102 | if (strcmp(name, var->name) == 0) { |
| 103 | found = true; |
| 104 | return visit_stop; |
| 105 | } |
| 106 | |
| 107 | return visit_continue_with_parent; |
| 108 | } |
| 109 | |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 110 | virtual ir_visitor_status visit_enter(ir_call *ir) |
| 111 | { |
Kenneth Graunke | 82065fa | 2011-09-20 18:08:11 -0700 | [diff] [blame] | 112 | exec_list_iterator sig_iter = ir->callee->parameters.iterator(); |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 113 | foreach_iter(exec_list_iterator, iter, *ir) { |
| 114 | ir_rvalue *param_rval = (ir_rvalue *)iter.get(); |
| 115 | ir_variable *sig_param = (ir_variable *)sig_iter.get(); |
| 116 | |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 117 | if (sig_param->mode == ir_var_function_out || |
| 118 | sig_param->mode == ir_var_function_inout) { |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 119 | ir_variable *var = param_rval->variable_referenced(); |
| 120 | if (var && strcmp(name, var->name) == 0) { |
| 121 | found = true; |
| 122 | return visit_stop; |
| 123 | } |
| 124 | } |
| 125 | sig_iter.next(); |
| 126 | } |
| 127 | |
Kenneth Graunke | d884f60 | 2012-03-20 15:56:37 -0700 | [diff] [blame] | 128 | if (ir->return_deref != NULL) { |
| 129 | ir_variable *const var = ir->return_deref->variable_referenced(); |
| 130 | |
| 131 | if (strcmp(name, var->name) == 0) { |
| 132 | found = true; |
| 133 | return visit_stop; |
| 134 | } |
| 135 | } |
| 136 | |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 137 | return visit_continue_with_parent; |
| 138 | } |
| 139 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 140 | bool variable_found() |
| 141 | { |
| 142 | return found; |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | const char *name; /**< Find writes to a variable with this name. */ |
| 147 | bool found; /**< Was a write to the variable found? */ |
| 148 | }; |
| 149 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 150 | |
Ian Romanick | c33e78f | 2010-08-13 12:30:41 -0700 | [diff] [blame] | 151 | /** |
| 152 | * Visitor that determines whether or not a variable is ever read. |
| 153 | */ |
| 154 | class find_deref_visitor : public ir_hierarchical_visitor { |
| 155 | public: |
| 156 | find_deref_visitor(const char *name) |
| 157 | : name(name), found(false) |
| 158 | { |
| 159 | /* empty */ |
| 160 | } |
| 161 | |
| 162 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 163 | { |
| 164 | if (strcmp(this->name, ir->var->name) == 0) { |
| 165 | this->found = true; |
| 166 | return visit_stop; |
| 167 | } |
| 168 | |
| 169 | return visit_continue; |
| 170 | } |
| 171 | |
| 172 | bool variable_found() const |
| 173 | { |
| 174 | return this->found; |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | const char *name; /**< Find writes to a variable with this name. */ |
| 179 | bool found; /**< Was a write to the variable found? */ |
| 180 | }; |
| 181 | |
| 182 | |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 183 | class geom_array_resize_visitor : public ir_hierarchical_visitor { |
| 184 | public: |
| 185 | unsigned num_vertices; |
| 186 | gl_shader_program *prog; |
| 187 | |
| 188 | geom_array_resize_visitor(unsigned num_vertices, gl_shader_program *prog) |
| 189 | { |
| 190 | this->num_vertices = num_vertices; |
| 191 | this->prog = prog; |
| 192 | } |
| 193 | |
| 194 | virtual ~geom_array_resize_visitor() |
| 195 | { |
| 196 | /* empty */ |
| 197 | } |
| 198 | |
| 199 | virtual ir_visitor_status visit(ir_variable *var) |
| 200 | { |
| 201 | if (!var->type->is_array() || var->mode != ir_var_shader_in) |
| 202 | return visit_continue; |
| 203 | |
| 204 | unsigned size = var->type->length; |
| 205 | |
| 206 | /* Generate a link error if the shader has declared this array with an |
| 207 | * incorrect size. |
| 208 | */ |
| 209 | if (size && size != this->num_vertices) { |
| 210 | linker_error(this->prog, "size of array %s declared as %u, " |
| 211 | "but number of input vertices is %u\n", |
| 212 | var->name, size, this->num_vertices); |
| 213 | return visit_continue; |
| 214 | } |
| 215 | |
| 216 | /* Generate a link error if the shader attempts to access an input |
| 217 | * array using an index too large for its actual size assigned at link |
| 218 | * time. |
| 219 | */ |
| 220 | if (var->max_array_access >= this->num_vertices) { |
| 221 | linker_error(this->prog, "geometry shader accesses element %i of " |
| 222 | "%s, but only %i input vertices\n", |
| 223 | var->max_array_access, var->name, this->num_vertices); |
| 224 | return visit_continue; |
| 225 | } |
| 226 | |
| 227 | var->type = glsl_type::get_array_instance(var->type->element_type(), |
| 228 | this->num_vertices); |
| 229 | var->max_array_access = this->num_vertices - 1; |
| 230 | |
| 231 | return visit_continue; |
| 232 | } |
| 233 | |
| 234 | /* Dereferences of input variables need to be updated so that their type |
| 235 | * matches the newly assigned type of the variable they are accessing. */ |
| 236 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 237 | { |
| 238 | ir->type = ir->var->type; |
| 239 | return visit_continue; |
| 240 | } |
| 241 | |
| 242 | /* Dereferences of 2D input arrays need to be updated so that their type |
| 243 | * matches the newly assigned type of the array they are accessing. */ |
| 244 | virtual ir_visitor_status visit_leave(ir_dereference_array *ir) |
| 245 | { |
| 246 | const glsl_type *const vt = ir->array->type; |
| 247 | if (vt->is_array()) |
| 248 | ir->type = vt->element_type(); |
| 249 | return visit_continue; |
| 250 | } |
| 251 | }; |
| 252 | |
| 253 | |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 254 | /** |
| 255 | * Visitor that determines whether or not a shader uses ir_end_primitive. |
| 256 | */ |
| 257 | class find_end_primitive_visitor : public ir_hierarchical_visitor { |
| 258 | public: |
| 259 | find_end_primitive_visitor() |
| 260 | : found(false) |
| 261 | { |
| 262 | /* empty */ |
| 263 | } |
| 264 | |
| 265 | virtual ir_visitor_status visit(ir_end_primitive *) |
| 266 | { |
| 267 | found = true; |
| 268 | return visit_stop; |
| 269 | } |
| 270 | |
| 271 | bool end_primitive_found() |
| 272 | { |
| 273 | return found; |
| 274 | } |
| 275 | |
| 276 | private: |
| 277 | bool found; |
| 278 | }; |
| 279 | |
Eric Anholt | 10ef949 | 2013-09-20 11:03:44 -0700 | [diff] [blame] | 280 | } /* anonymous namespace */ |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 281 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 282 | void |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 283 | linker_error(gl_shader_program *prog, const char *fmt, ...) |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 284 | { |
| 285 | va_list ap; |
| 286 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 287 | ralloc_strcat(&prog->InfoLog, "error: "); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 288 | va_start(ap, fmt); |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 289 | ralloc_vasprintf_append(&prog->InfoLog, fmt, ap); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 290 | va_end(ap); |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 291 | |
| 292 | prog->LinkStatus = false; |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
| 296 | void |
Ian Romanick | 379a32f | 2011-07-28 14:09:06 -0700 | [diff] [blame] | 297 | linker_warning(gl_shader_program *prog, const char *fmt, ...) |
| 298 | { |
| 299 | va_list ap; |
| 300 | |
| 301 | ralloc_strcat(&prog->InfoLog, "error: "); |
| 302 | va_start(ap, fmt); |
| 303 | ralloc_vasprintf_append(&prog->InfoLog, fmt, ap); |
| 304 | va_end(ap); |
| 305 | |
| 306 | } |
| 307 | |
| 308 | |
Paul Berry | b92900d | 2013-01-28 14:21:59 -0800 | [diff] [blame] | 309 | /** |
| 310 | * Given a string identifying a program resource, break it into a base name |
| 311 | * and an optional array index in square brackets. |
| 312 | * |
| 313 | * If an array index is present, \c out_base_name_end is set to point to the |
| 314 | * "[" that precedes the array index, and the array index itself is returned |
| 315 | * as a long. |
| 316 | * |
| 317 | * If no array index is present (or if the array index is negative or |
| 318 | * mal-formed), \c out_base_name_end, is set to point to the null terminator |
| 319 | * at the end of the input string, and -1 is returned. |
| 320 | * |
| 321 | * Only the final array index is parsed; if the string contains other array |
| 322 | * indices (or structure field accesses), they are left in the base name. |
| 323 | * |
| 324 | * No attempt is made to check that the base name is properly formed; |
| 325 | * typically the caller will look up the base name in a hash table, so |
| 326 | * ill-formed base names simply turn into hash table lookup failures. |
| 327 | */ |
| 328 | long |
| 329 | parse_program_resource_name(const GLchar *name, |
| 330 | const GLchar **out_base_name_end) |
| 331 | { |
| 332 | /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says: |
| 333 | * |
| 334 | * "When an integer array element or block instance number is part of |
| 335 | * the name string, it will be specified in decimal form without a "+" |
| 336 | * or "-" sign or any extra leading zeroes. Additionally, the name |
| 337 | * string will not include white space anywhere in the string." |
| 338 | */ |
| 339 | |
| 340 | const size_t len = strlen(name); |
| 341 | *out_base_name_end = name + len; |
| 342 | |
| 343 | if (len == 0 || name[len-1] != ']') |
| 344 | return -1; |
| 345 | |
| 346 | /* Walk backwards over the string looking for a non-digit character. This |
| 347 | * had better be the opening bracket for an array index. |
| 348 | * |
| 349 | * Initially, i specifies the location of the ']'. Since the string may |
| 350 | * contain only the ']' charcater, walk backwards very carefully. |
| 351 | */ |
| 352 | unsigned i; |
| 353 | for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i) |
| 354 | /* empty */ ; |
| 355 | |
| 356 | if ((i == 0) || name[i-1] != '[') |
| 357 | return -1; |
| 358 | |
| 359 | long array_index = strtol(&name[i], NULL, 10); |
| 360 | if (array_index < 0) |
| 361 | return -1; |
| 362 | |
| 363 | *out_base_name_end = name + (i - 1); |
| 364 | return array_index; |
| 365 | } |
| 366 | |
| 367 | |
Ian Romanick | 379a32f | 2011-07-28 14:09:06 -0700 | [diff] [blame] | 368 | void |
Ian Romanick | 63974c0 | 2013-10-04 10:46:29 -0700 | [diff] [blame] | 369 | link_invalidate_variable_locations(exec_list *ir) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 370 | { |
Ian Romanick | cf8b14c | 2013-10-22 15:07:00 -0700 | [diff] [blame] | 371 | foreach_list(node, ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 372 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 373 | |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 374 | if (var == NULL) |
| 375 | continue; |
| 376 | |
Ian Romanick | 63974c0 | 2013-10-04 10:46:29 -0700 | [diff] [blame] | 377 | /* Only assign locations for variables that lack an explicit location. |
| 378 | * Explicit locations are set for all built-in variables, generic vertex |
| 379 | * shader inputs (via layout(location=...)), and generic fragment shader |
| 380 | * outputs (also via layout(location=...)). |
| 381 | */ |
| 382 | if (!var->explicit_location) { |
| 383 | var->location = -1; |
| 384 | var->location_frac = 0; |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 385 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 386 | |
Ian Romanick | 63974c0 | 2013-10-04 10:46:29 -0700 | [diff] [blame] | 387 | /* ir_variable::is_unmatched_generic_inout is used by the linker while |
| 388 | * connecting outputs from one stage to inputs of the next stage. |
| 389 | * |
| 390 | * There are two implicit assumptions here. First, we assume that any |
| 391 | * built-in variable (i.e., non-generic in or out) will have |
| 392 | * explicit_location set. Second, we assume that any generic in or out |
| 393 | * will not have explicit_location set. |
| 394 | * |
| 395 | * This second assumption will only be valid until |
| 396 | * GL_ARB_separate_shader_objects is supported. When that extension is |
| 397 | * implemented, this function will need some modifications. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 398 | */ |
Ian Romanick | 63974c0 | 2013-10-04 10:46:29 -0700 | [diff] [blame] | 399 | if (!var->explicit_location) { |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 400 | var->is_unmatched_generic_inout = 1; |
Paul Berry | 3e81c66 | 2012-12-05 10:47:55 -0800 | [diff] [blame] | 401 | } else { |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 402 | var->is_unmatched_generic_inout = 0; |
Paul Berry | 3e81c66 | 2012-12-05 10:47:55 -0800 | [diff] [blame] | 403 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 408 | /** |
Paul Berry | 44e07de | 2013-06-11 14:11:05 -0700 | [diff] [blame] | 409 | * Set UsesClipDistance and ClipDistanceArraySize based on the given shader. |
| 410 | * |
| 411 | * Also check for errors based on incorrect usage of gl_ClipVertex and |
| 412 | * gl_ClipDistance. |
| 413 | * |
| 414 | * Return false if an error was reported. |
| 415 | */ |
| 416 | static void |
| 417 | analyze_clip_usage(const char *shader_type, struct gl_shader_program *prog, |
| 418 | struct gl_shader *shader, GLboolean *UsesClipDistance, |
| 419 | GLuint *ClipDistanceArraySize) |
| 420 | { |
| 421 | *ClipDistanceArraySize = 0; |
| 422 | |
| 423 | if (!prog->IsES && prog->Version >= 130) { |
| 424 | /* From section 7.1 (Vertex Shader Special Variables) of the |
| 425 | * GLSL 1.30 spec: |
| 426 | * |
| 427 | * "It is an error for a shader to statically write both |
| 428 | * gl_ClipVertex and gl_ClipDistance." |
| 429 | * |
| 430 | * This does not apply to GLSL ES shaders, since GLSL ES defines neither |
| 431 | * gl_ClipVertex nor gl_ClipDistance. |
| 432 | */ |
| 433 | find_assignment_visitor clip_vertex("gl_ClipVertex"); |
| 434 | find_assignment_visitor clip_distance("gl_ClipDistance"); |
| 435 | |
| 436 | clip_vertex.run(shader->ir); |
| 437 | clip_distance.run(shader->ir); |
| 438 | if (clip_vertex.variable_found() && clip_distance.variable_found()) { |
| 439 | linker_error(prog, "%s shader writes to both `gl_ClipVertex' " |
| 440 | "and `gl_ClipDistance'\n", shader_type); |
| 441 | return; |
| 442 | } |
| 443 | *UsesClipDistance = clip_distance.variable_found(); |
| 444 | ir_variable *clip_distance_var = |
| 445 | shader->symbols->get_variable("gl_ClipDistance"); |
| 446 | if (clip_distance_var) |
| 447 | *ClipDistanceArraySize = clip_distance_var->type->length; |
| 448 | } else { |
| 449 | *UsesClipDistance = false; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | |
| 454 | /** |
Paul Berry | 1ad54ae | 2011-09-17 09:42:02 -0700 | [diff] [blame] | 455 | * Verify that a vertex shader executable meets all semantic requirements. |
| 456 | * |
Paul Berry | 642e5b41 | 2012-01-04 13:57:52 -0800 | [diff] [blame] | 457 | * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize |
| 458 | * as a side effect. |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 459 | * |
| 460 | * \param shader Vertex shader executable to be verified |
| 461 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 462 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 463 | validate_vertex_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 464 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 465 | { |
| 466 | if (shader == NULL) |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 467 | return; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 468 | |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 469 | /* From the GLSL 1.10 spec, page 48: |
| 470 | * |
| 471 | * "The variable gl_Position is available only in the vertex |
| 472 | * language and is intended for writing the homogeneous vertex |
| 473 | * position. All executions of a well-formed vertex shader |
| 474 | * executable must write a value into this variable. [...] The |
| 475 | * variable gl_Position is available only in the vertex |
| 476 | * language and is intended for writing the homogeneous vertex |
| 477 | * position. All executions of a well-formed vertex shader |
| 478 | * executable must write a value into this variable." |
| 479 | * |
| 480 | * while in GLSL 1.40 this text is changed to: |
| 481 | * |
| 482 | * "The variable gl_Position is available only in the vertex |
| 483 | * language and is intended for writing the homogeneous vertex |
| 484 | * position. It can be written at any time during shader |
| 485 | * execution. It may also be read back by a vertex shader |
| 486 | * after being written. This value will be used by primitive |
| 487 | * assembly, clipping, culling, and other fixed functionality |
| 488 | * operations, if present, that operate on primitives after |
| 489 | * vertex processing has occurred. Its value is undefined if |
| 490 | * the vertex shader executable does not write gl_Position." |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 491 | * |
| 492 | * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is |
| 493 | * not an error. |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 494 | */ |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 495 | if (prog->Version < (prog->IsES ? 300 : 140)) { |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 496 | find_assignment_visitor find("gl_Position"); |
| 497 | find.run(shader->ir); |
| 498 | if (!find.variable_found()) { |
| 499 | linker_error(prog, "vertex shader does not write to `gl_Position'\n"); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 500 | return; |
Eric Anholt | f1c1c9e | 2012-03-19 22:43:27 -0700 | [diff] [blame] | 501 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Paul Berry | 44e07de | 2013-06-11 14:11:05 -0700 | [diff] [blame] | 504 | analyze_clip_usage("vertex", prog, shader, &prog->Vert.UsesClipDistance, |
| 505 | &prog->Vert.ClipDistanceArraySize); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 509 | /** |
| 510 | * Verify that a fragment shader executable meets all semantic requirements |
| 511 | * |
| 512 | * \param shader Fragment shader executable to be verified |
| 513 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 514 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 515 | validate_fragment_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 516 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 517 | { |
| 518 | if (shader == NULL) |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 519 | return; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 520 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 521 | find_assignment_visitor frag_color("gl_FragColor"); |
| 522 | find_assignment_visitor frag_data("gl_FragData"); |
| 523 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 524 | frag_color.run(shader->ir); |
| 525 | frag_data.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 526 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 527 | if (frag_color.variable_found() && frag_data.variable_found()) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 528 | linker_error(prog, "fragment shader writes to both " |
| 529 | "`gl_FragColor' and `gl_FragData'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 530 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 533 | /** |
| 534 | * Verify that a geometry shader executable meets all semantic requirements |
| 535 | * |
Paul Berry | 44e07de | 2013-06-11 14:11:05 -0700 | [diff] [blame] | 536 | * Also sets prog->Geom.VerticesIn, prog->Geom.UsesClipDistance, and |
| 537 | * prog->Geom.ClipDistanceArraySize as a side effect. |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 538 | * |
| 539 | * \param shader Geometry shader executable to be verified |
| 540 | */ |
| 541 | void |
| 542 | validate_geometry_shader_executable(struct gl_shader_program *prog, |
| 543 | struct gl_shader *shader) |
| 544 | { |
| 545 | if (shader == NULL) |
| 546 | return; |
| 547 | |
| 548 | unsigned num_vertices = vertices_per_prim(prog->Geom.InputType); |
| 549 | prog->Geom.VerticesIn = num_vertices; |
Paul Berry | 44e07de | 2013-06-11 14:11:05 -0700 | [diff] [blame] | 550 | |
| 551 | analyze_clip_usage("geometry", prog, shader, &prog->Geom.UsesClipDistance, |
| 552 | &prog->Geom.ClipDistanceArraySize); |
Paul Berry | 1a33e02 | 2013-08-18 20:59:37 -0700 | [diff] [blame] | 553 | |
| 554 | find_end_primitive_visitor end_primitive; |
| 555 | end_primitive.run(shader->ir); |
| 556 | prog->Geom.UsesEndPrimitive = end_primitive.end_primitive_found(); |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 557 | } |
| 558 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 559 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 560 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 561 | * Generate a string describing the mode of a variable |
| 562 | */ |
| 563 | static const char * |
| 564 | mode_string(const ir_variable *var) |
| 565 | { |
| 566 | switch (var->mode) { |
| 567 | case ir_var_auto: |
| 568 | return (var->read_only) ? "global constant" : "global variable"; |
| 569 | |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 570 | case ir_var_uniform: return "uniform"; |
| 571 | case ir_var_shader_in: return "shader input"; |
| 572 | case ir_var_shader_out: return "shader output"; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 573 | |
Kenneth Graunke | 819d57f | 2011-01-12 15:37:37 -0800 | [diff] [blame] | 574 | case ir_var_const_in: |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 575 | case ir_var_temporary: |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 576 | default: |
| 577 | assert(!"Should not get here."); |
| 578 | return "invalid variable"; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | |
| 583 | /** |
| 584 | * Perform validation of global variables used across multiple shaders |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 585 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 586 | void |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 587 | cross_validate_globals(struct gl_shader_program *prog, |
| 588 | struct gl_shader **shader_list, |
| 589 | unsigned num_shaders, |
| 590 | bool uniforms_only) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 591 | { |
| 592 | /* Examine all of the uniforms in all of the shaders and cross validate |
| 593 | * them. |
| 594 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 595 | glsl_symbol_table variables; |
| 596 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 597 | if (shader_list[i] == NULL) |
| 598 | continue; |
| 599 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 600 | foreach_list(node, shader_list[i]->ir) { |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 601 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 602 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 603 | if (var == NULL) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 604 | continue; |
| 605 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 606 | if (uniforms_only && (var->mode != ir_var_uniform)) |
| 607 | continue; |
| 608 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 609 | /* Don't cross validate temporaries that are at global scope. These |
| 610 | * will eventually get pulled into the shaders 'main'. |
| 611 | */ |
| 612 | if (var->mode == ir_var_temporary) |
| 613 | continue; |
| 614 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 615 | /* If a global with this name has already been seen, verify that the |
| 616 | * new instance has the same type. In addition, if the globals have |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 617 | * initializers, the values of the initializers must be the same. |
| 618 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 619 | ir_variable *const existing = variables.get_variable(var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 620 | if (existing != NULL) { |
| 621 | if (var->type != existing->type) { |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 622 | /* Consider the types to be "the same" if both types are arrays |
| 623 | * of the same type and one of the arrays is implicitly sized. |
| 624 | * In addition, set the type of the linked variable to the |
| 625 | * explicitly sized array. |
| 626 | */ |
| 627 | if (var->type->is_array() |
| 628 | && existing->type->is_array() |
| 629 | && (var->type->fields.array == existing->type->fields.array) |
| 630 | && ((var->type->length == 0) |
| 631 | || (existing->type->length == 0))) { |
Ian Romanick | 0f4b2a0 | 2011-01-25 12:06:18 -0800 | [diff] [blame] | 632 | if (var->type->length != 0) { |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 633 | existing->type = var->type; |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 634 | } |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 635 | } else { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 636 | linker_error(prog, "%s `%s' declared as type " |
| 637 | "`%s' and type `%s'\n", |
| 638 | mode_string(var), |
| 639 | var->name, var->type->name, |
| 640 | existing->type->name); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 641 | return; |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 642 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 645 | if (var->explicit_location) { |
| 646 | if (existing->explicit_location |
| 647 | && (var->location != existing->location)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 648 | linker_error(prog, "explicit locations for %s " |
| 649 | "`%s' have differing values\n", |
| 650 | mode_string(var), var->name); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 651 | return; |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | existing->location = var->location; |
| 655 | existing->explicit_location = true; |
| 656 | } |
| 657 | |
Kenneth Graunke | 9a9a830 | 2013-07-16 12:18:57 -0700 | [diff] [blame] | 658 | /* From the GLSL 4.20 specification: |
| 659 | * "A link error will result if two compilation units in a program |
| 660 | * specify different integer-constant bindings for the same |
| 661 | * opaque-uniform name. However, it is not an error to specify a |
| 662 | * binding on some but not all declarations for the same name" |
| 663 | */ |
| 664 | if (var->explicit_binding) { |
| 665 | if (existing->explicit_binding && |
| 666 | var->binding != existing->binding) { |
| 667 | linker_error(prog, "explicit bindings for %s " |
| 668 | "`%s' have differing values\n", |
| 669 | mode_string(var), var->name); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 670 | return; |
Kenneth Graunke | 9a9a830 | 2013-07-16 12:18:57 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | existing->binding = var->binding; |
| 674 | existing->explicit_binding = true; |
| 675 | } |
| 676 | |
Ian Romanick | 46173f9 | 2011-10-31 13:07:06 -0700 | [diff] [blame] | 677 | /* Validate layout qualifiers for gl_FragDepth. |
| 678 | * |
| 679 | * From the AMD/ARB_conservative_depth specs: |
| 680 | * |
| 681 | * "If gl_FragDepth is redeclared in any fragment shader in a |
| 682 | * program, it must be redeclared in all fragment shaders in |
| 683 | * that program that have static assignments to |
| 684 | * gl_FragDepth. All redeclarations of gl_FragDepth in all |
| 685 | * fragment shaders in a single program must have the same set |
| 686 | * of qualifiers." |
| 687 | */ |
| 688 | if (strcmp(var->name, "gl_FragDepth") == 0) { |
| 689 | bool layout_declared = var->depth_layout != ir_depth_layout_none; |
| 690 | bool layout_differs = |
| 691 | var->depth_layout != existing->depth_layout; |
| 692 | |
| 693 | if (layout_declared && layout_differs) { |
| 694 | linker_error(prog, |
| 695 | "All redeclarations of gl_FragDepth in all " |
| 696 | "fragment shaders in a single program must have " |
| 697 | "the same set of qualifiers."); |
| 698 | } |
| 699 | |
| 700 | if (var->used && layout_differs) { |
| 701 | linker_error(prog, |
| 702 | "If gl_FragDepth is redeclared with a layout " |
| 703 | "qualifier in any fragment shader, it must be " |
| 704 | "redeclared with the same layout qualifier in " |
| 705 | "all fragment shaders that have assignments to " |
| 706 | "gl_FragDepth"); |
| 707 | } |
| 708 | } |
Chad Versace | addae33 | 2011-01-27 01:40:31 -0800 | [diff] [blame] | 709 | |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 710 | /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says: |
| 711 | * |
| 712 | * "If a shared global has multiple initializers, the |
| 713 | * initializers must all be constant expressions, and they |
| 714 | * must all have the same value. Otherwise, a link error will |
| 715 | * result. (A shared global having only one initializer does |
| 716 | * not require that initializer to be a constant expression.)" |
| 717 | * |
| 718 | * Previous to 4.20 the GLSL spec simply said that initializers |
| 719 | * must have the same value. In this case of non-constant |
| 720 | * initializers, this was impossible to determine. As a result, |
| 721 | * no vendor actually implemented that behavior. The 4.20 |
| 722 | * behavior matches the implemented behavior of at least one other |
| 723 | * vendor, so we'll implement that for all GLSL versions. |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 724 | */ |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 725 | if (var->constant_initializer != NULL) { |
| 726 | if (existing->constant_initializer != NULL) { |
| 727 | if (!var->constant_initializer->has_value(existing->constant_initializer)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 728 | linker_error(prog, "initializers for %s " |
| 729 | "`%s' have differing values\n", |
| 730 | mode_string(var), var->name); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 731 | return; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 732 | } |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 733 | } else { |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 734 | /* If the first-seen instance of a particular uniform did not |
| 735 | * have an initializer but a later instance does, copy the |
| 736 | * initializer to the version stored in the symbol table. |
| 737 | */ |
Ian Romanick | de415b7 | 2010-07-14 13:22:12 -0700 | [diff] [blame] | 738 | /* FINISHME: This is wrong. The constant_value field should |
| 739 | * FINISHME: not be modified! Imagine a case where a shader |
| 740 | * FINISHME: without an initializer is linked in two different |
| 741 | * FINISHME: programs with shaders that have differing |
| 742 | * FINISHME: initializers. Linking with the first will |
| 743 | * FINISHME: modify the shader, and linking with the second |
| 744 | * FINISHME: will fail. |
| 745 | */ |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 746 | existing->constant_initializer = |
| 747 | var->constant_initializer->clone(ralloc_parent(existing), |
| 748 | NULL); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | if (var->has_initializer) { |
| 753 | if (existing->has_initializer |
| 754 | && (var->constant_initializer == NULL |
| 755 | || existing->constant_initializer == NULL)) { |
| 756 | linker_error(prog, |
| 757 | "shared global variable `%s' has multiple " |
| 758 | "non-constant initializers.\n", |
| 759 | var->name); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 760 | return; |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /* Some instance had an initializer, so keep track of that. In |
| 764 | * this location, all sorts of initializers (constant or |
| 765 | * otherwise) will propagate the existence to the variable |
| 766 | * stored in the symbol table. |
| 767 | */ |
| 768 | existing->has_initializer = true; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 769 | } |
Chad Versace | 7528f14 | 2010-11-17 14:34:38 -0800 | [diff] [blame] | 770 | |
| 771 | if (existing->invariant != var->invariant) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 772 | linker_error(prog, "declarations for %s `%s' have " |
| 773 | "mismatching invariant qualifiers\n", |
| 774 | mode_string(var), var->name); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 775 | return; |
Chad Versace | 7528f14 | 2010-11-17 14:34:38 -0800 | [diff] [blame] | 776 | } |
Chad Versace | 61428dd | 2011-01-10 15:29:30 -0800 | [diff] [blame] | 777 | if (existing->centroid != var->centroid) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 778 | linker_error(prog, "declarations for %s `%s' have " |
| 779 | "mismatching centroid qualifiers\n", |
| 780 | mode_string(var), var->name); |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 781 | return; |
Chad Versace | 61428dd | 2011-01-10 15:29:30 -0800 | [diff] [blame] | 782 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 783 | } else |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 784 | variables.add_variable(var); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 785 | } |
| 786 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 790 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 791 | * Perform validation of uniforms used across multiple shader stages |
| 792 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 793 | void |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 794 | cross_validate_uniforms(struct gl_shader_program *prog) |
| 795 | { |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 796 | cross_validate_globals(prog, prog->_LinkedShaders, |
| 797 | MESA_SHADER_TYPES, true); |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 800 | /** |
| 801 | * Accumulates the array of prog->UniformBlocks and checks that all |
| 802 | * definitons of blocks agree on their contents. |
| 803 | */ |
| 804 | static bool |
| 805 | interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog) |
| 806 | { |
| 807 | unsigned max_num_uniform_blocks = 0; |
| 808 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 809 | if (prog->_LinkedShaders[i]) |
| 810 | max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks; |
| 811 | } |
| 812 | |
| 813 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 814 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 815 | |
| 816 | prog->UniformBlockStageIndex[i] = ralloc_array(prog, int, |
| 817 | max_num_uniform_blocks); |
| 818 | for (unsigned int j = 0; j < max_num_uniform_blocks; j++) |
| 819 | prog->UniformBlockStageIndex[i][j] = -1; |
| 820 | |
| 821 | if (sh == NULL) |
| 822 | continue; |
| 823 | |
| 824 | for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) { |
| 825 | int index = link_cross_validate_uniform_block(prog, |
| 826 | &prog->UniformBlocks, |
| 827 | &prog->NumUniformBlocks, |
| 828 | &sh->UniformBlocks[j]); |
| 829 | |
| 830 | if (index == -1) { |
| 831 | linker_error(prog, "uniform block `%s' has mismatching definitions", |
| 832 | sh->UniformBlocks[j].Name); |
| 833 | return false; |
| 834 | } |
| 835 | |
| 836 | prog->UniformBlockStageIndex[i][index] = j; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | return true; |
| 841 | } |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 842 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 843 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 844 | /** |
| 845 | * Populates a shaders symbol table with all global declarations |
| 846 | */ |
| 847 | static void |
| 848 | populate_symbol_table(gl_shader *sh) |
| 849 | { |
| 850 | sh->symbols = new(sh) glsl_symbol_table; |
| 851 | |
| 852 | foreach_list(node, sh->ir) { |
| 853 | ir_instruction *const inst = (ir_instruction *) node; |
| 854 | ir_variable *var; |
| 855 | ir_function *func; |
| 856 | |
| 857 | if ((func = inst->as_function()) != NULL) { |
Eric Anholt | e8f5ebf | 2010-11-05 06:08:45 -0700 | [diff] [blame] | 858 | sh->symbols->add_function(func); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 859 | } else if ((var = inst->as_variable()) != NULL) { |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 860 | sh->symbols->add_variable(var); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | |
| 866 | /** |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 867 | * Remap variables referenced in an instruction tree |
| 868 | * |
| 869 | * This is used when instruction trees are cloned from one shader and placed in |
| 870 | * another. These trees will contain references to \c ir_variable nodes that |
| 871 | * do not exist in the target shader. This function finds these \c ir_variable |
| 872 | * references and replaces the references with matching variables in the target |
| 873 | * shader. |
| 874 | * |
| 875 | * If there is no matching variable in the target shader, a clone of the |
| 876 | * \c ir_variable is made and added to the target shader. The new variable is |
| 877 | * added to \b both the instruction stream and the symbol table. |
| 878 | * |
| 879 | * \param inst IR tree that is to be processed. |
| 880 | * \param symbols Symbol table containing global scope symbols in the |
| 881 | * linked shader. |
| 882 | * \param instructions Instruction stream where new variable declarations |
| 883 | * should be added. |
| 884 | */ |
| 885 | void |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 886 | remap_variables(ir_instruction *inst, struct gl_shader *target, |
| 887 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 888 | { |
| 889 | class remap_visitor : public ir_hierarchical_visitor { |
| 890 | public: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 891 | remap_visitor(struct gl_shader *target, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 892 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 893 | { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 894 | this->target = target; |
| 895 | this->symbols = target->symbols; |
| 896 | this->instructions = target->ir; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 897 | this->temps = temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 901 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 902 | if (ir->var->mode == ir_var_temporary) { |
| 903 | ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var); |
| 904 | |
| 905 | assert(var != NULL); |
| 906 | ir->var = var; |
| 907 | return visit_continue; |
| 908 | } |
| 909 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 910 | ir_variable *const existing = |
| 911 | this->symbols->get_variable(ir->var->name); |
| 912 | if (existing != NULL) |
| 913 | ir->var = existing; |
| 914 | else { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 915 | ir_variable *copy = ir->var->clone(this->target, NULL); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 916 | |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 917 | this->symbols->add_variable(copy); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 918 | this->instructions->push_head(copy); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 919 | ir->var = copy; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | return visit_continue; |
| 923 | } |
| 924 | |
| 925 | private: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 926 | struct gl_shader *target; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 927 | glsl_symbol_table *symbols; |
| 928 | exec_list *instructions; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 929 | hash_table *temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 930 | }; |
| 931 | |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 932 | remap_visitor v(target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 933 | |
| 934 | inst->accept(&v); |
| 935 | } |
| 936 | |
| 937 | |
| 938 | /** |
| 939 | * Move non-declarations from one instruction stream to another |
| 940 | * |
| 941 | * The intended usage pattern of this function is to pass the pointer to the |
Eric Anholt | 62c4763 | 2010-07-29 13:52:25 -0700 | [diff] [blame] | 942 | * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 943 | * pointer) for \c last and \c false for \c make_copies on the first |
| 944 | * call. Successive calls pass the return value of the previous call for |
| 945 | * \c last and \c true for \c make_copies. |
| 946 | * |
| 947 | * \param instructions Source instruction stream |
| 948 | * \param last Instruction after which new instructions should be |
| 949 | * inserted in the target instruction stream |
| 950 | * \param make_copies Flag selecting whether instructions in \c instructions |
| 951 | * should be copied (via \c ir_instruction::clone) into the |
| 952 | * target list or moved. |
| 953 | * |
| 954 | * \return |
| 955 | * The new "last" instruction in the target instruction stream. This pointer |
| 956 | * is suitable for use as the \c last parameter of a later call to this |
| 957 | * function. |
| 958 | */ |
| 959 | exec_node * |
| 960 | move_non_declarations(exec_list *instructions, exec_node *last, |
| 961 | bool make_copies, gl_shader *target) |
| 962 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 963 | hash_table *temps = NULL; |
| 964 | |
| 965 | if (make_copies) |
| 966 | temps = hash_table_ctor(0, hash_table_pointer_hash, |
| 967 | hash_table_pointer_compare); |
| 968 | |
Ian Romanick | 303c99f | 2010-07-19 12:34:56 -0700 | [diff] [blame] | 969 | foreach_list_safe(node, instructions) { |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 970 | ir_instruction *inst = (ir_instruction *) node; |
| 971 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 972 | if (inst->as_function()) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 973 | continue; |
| 974 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 975 | ir_variable *var = inst->as_variable(); |
| 976 | if ((var != NULL) && (var->mode != ir_var_temporary)) |
| 977 | continue; |
| 978 | |
| 979 | assert(inst->as_assignment() |
Kenneth Graunke | d884f60 | 2012-03-20 15:56:37 -0700 | [diff] [blame] | 980 | || inst->as_call() |
Kenneth Graunke | b45a68e | 2012-10-24 13:17:24 -0700 | [diff] [blame] | 981 | || inst->as_if() /* for initializers with the ?: operator */ |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 982 | || ((var != NULL) && (var->mode == ir_var_temporary))); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 983 | |
| 984 | if (make_copies) { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 985 | inst = inst->clone(target, NULL); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 986 | |
| 987 | if (var != NULL) |
| 988 | hash_table_insert(temps, inst, var); |
| 989 | else |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 990 | remap_variables(inst, target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 991 | } else { |
| 992 | inst->remove(); |
| 993 | } |
| 994 | |
| 995 | last->insert_after(inst); |
| 996 | last = inst; |
| 997 | } |
| 998 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 999 | if (make_copies) |
| 1000 | hash_table_dtor(temps); |
| 1001 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1002 | return last; |
| 1003 | } |
| 1004 | |
| 1005 | /** |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1006 | * Get the function signature for main from a shader |
| 1007 | */ |
| 1008 | static ir_function_signature * |
| 1009 | get_main_function_signature(gl_shader *sh) |
| 1010 | { |
| 1011 | ir_function *const f = sh->symbols->get_function("main"); |
| 1012 | if (f != NULL) { |
| 1013 | exec_list void_parameters; |
| 1014 | |
| 1015 | /* Look for the 'void main()' signature and ensure that it's defined. |
| 1016 | * This keeps the linker from accidentally pick a shader that just |
| 1017 | * contains a prototype for main. |
| 1018 | * |
| 1019 | * We don't have to check for multiple definitions of main (in multiple |
| 1020 | * shaders) because that would have already been caught above. |
| 1021 | */ |
Kenneth Graunke | 3e820e3 | 2013-08-30 23:11:55 -0700 | [diff] [blame] | 1022 | ir_function_signature *sig = f->matching_signature(NULL, &void_parameters); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1023 | if ((sig != NULL) && sig->is_defined) { |
| 1024 | return sig; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | return NULL; |
| 1029 | } |
| 1030 | |
| 1031 | |
| 1032 | /** |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 1033 | * This class is only used in link_intrastage_shaders() below but declaring |
| 1034 | * it inside that function leads to compiler warnings with some versions of |
| 1035 | * gcc. |
| 1036 | */ |
| 1037 | class array_sizing_visitor : public ir_hierarchical_visitor { |
| 1038 | public: |
Paul Berry | 15e05b9 | 2013-09-25 14:07:37 -0700 | [diff] [blame] | 1039 | array_sizing_visitor() |
| 1040 | : mem_ctx(ralloc_context(NULL)), |
| 1041 | unnamed_interfaces(hash_table_ctor(0, hash_table_pointer_hash, |
| 1042 | hash_table_pointer_compare)) |
| 1043 | { |
| 1044 | } |
| 1045 | |
| 1046 | ~array_sizing_visitor() |
| 1047 | { |
| 1048 | hash_table_dtor(this->unnamed_interfaces); |
| 1049 | ralloc_free(this->mem_ctx); |
| 1050 | } |
| 1051 | |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 1052 | virtual ir_visitor_status visit(ir_variable *var) |
| 1053 | { |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1054 | fixup_type(&var->type, var->max_array_access); |
| 1055 | if (var->type->is_interface()) { |
| 1056 | if (interface_contains_unsized_arrays(var->type)) { |
| 1057 | const glsl_type *new_type = |
| 1058 | resize_interface_members(var->type, var->max_ifc_array_access); |
| 1059 | var->type = new_type; |
| 1060 | var->change_interface_type(new_type); |
| 1061 | } |
| 1062 | } else if (var->type->is_array() && |
| 1063 | var->type->fields.array->is_interface()) { |
| 1064 | if (interface_contains_unsized_arrays(var->type->fields.array)) { |
| 1065 | const glsl_type *new_type = |
| 1066 | resize_interface_members(var->type->fields.array, |
| 1067 | var->max_ifc_array_access); |
| 1068 | var->change_interface_type(new_type); |
| 1069 | var->type = |
| 1070 | glsl_type::get_array_instance(new_type, var->type->length); |
| 1071 | } |
Paul Berry | 15e05b9 | 2013-09-25 14:07:37 -0700 | [diff] [blame] | 1072 | } else if (const glsl_type *ifc_type = var->get_interface_type()) { |
| 1073 | /* Store a pointer to the variable in the unnamed_interfaces |
| 1074 | * hashtable. |
| 1075 | */ |
| 1076 | ir_variable **interface_vars = (ir_variable **) |
| 1077 | hash_table_find(this->unnamed_interfaces, ifc_type); |
| 1078 | if (interface_vars == NULL) { |
| 1079 | interface_vars = rzalloc_array(mem_ctx, ir_variable *, |
| 1080 | ifc_type->length); |
| 1081 | hash_table_insert(this->unnamed_interfaces, interface_vars, |
| 1082 | ifc_type); |
| 1083 | } |
| 1084 | unsigned index = ifc_type->field_index(var->name); |
| 1085 | assert(index < ifc_type->length); |
| 1086 | assert(interface_vars[index] == NULL); |
| 1087 | interface_vars[index] = var; |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 1088 | } |
| 1089 | return visit_continue; |
| 1090 | } |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1091 | |
Paul Berry | 15e05b9 | 2013-09-25 14:07:37 -0700 | [diff] [blame] | 1092 | /** |
| 1093 | * For each unnamed interface block that was discovered while running the |
| 1094 | * visitor, adjust the interface type to reflect the newly assigned array |
| 1095 | * sizes, and fix up the ir_variable nodes to point to the new interface |
| 1096 | * type. |
| 1097 | */ |
| 1098 | void fixup_unnamed_interface_types() |
| 1099 | { |
| 1100 | hash_table_call_foreach(this->unnamed_interfaces, |
| 1101 | fixup_unnamed_interface_type, NULL); |
| 1102 | } |
| 1103 | |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1104 | private: |
| 1105 | /** |
| 1106 | * If the type pointed to by \c type represents an unsized array, replace |
| 1107 | * it with a sized array whose size is determined by max_array_access. |
| 1108 | */ |
| 1109 | static void fixup_type(const glsl_type **type, unsigned max_array_access) |
| 1110 | { |
Timothy Arceri | b59c592 | 2013-10-23 21:31:27 +1100 | [diff] [blame^] | 1111 | if ((*type)->is_unsized_array()) { |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1112 | *type = glsl_type::get_array_instance((*type)->fields.array, |
| 1113 | max_array_access + 1); |
| 1114 | assert(*type != NULL); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * Determine whether the given interface type contains unsized arrays (if |
| 1120 | * it doesn't, array_sizing_visitor doesn't need to process it). |
| 1121 | */ |
| 1122 | static bool interface_contains_unsized_arrays(const glsl_type *type) |
| 1123 | { |
| 1124 | for (unsigned i = 0; i < type->length; i++) { |
| 1125 | const glsl_type *elem_type = type->fields.structure[i].type; |
Timothy Arceri | b59c592 | 2013-10-23 21:31:27 +1100 | [diff] [blame^] | 1126 | if (elem_type->is_unsized_array()) |
Paul Berry | e226669 | 2013-09-23 10:44:19 -0700 | [diff] [blame] | 1127 | return true; |
| 1128 | } |
| 1129 | return false; |
| 1130 | } |
| 1131 | |
| 1132 | /** |
| 1133 | * Create a new interface type based on the given type, with unsized arrays |
| 1134 | * replaced by sized arrays whose size is determined by |
| 1135 | * max_ifc_array_access. |
| 1136 | */ |
| 1137 | static const glsl_type * |
| 1138 | resize_interface_members(const glsl_type *type, |
| 1139 | const unsigned *max_ifc_array_access) |
| 1140 | { |
| 1141 | unsigned num_fields = type->length; |
| 1142 | glsl_struct_field *fields = new glsl_struct_field[num_fields]; |
| 1143 | memcpy(fields, type->fields.structure, |
| 1144 | num_fields * sizeof(*fields)); |
| 1145 | for (unsigned i = 0; i < num_fields; i++) { |
| 1146 | fixup_type(&fields[i].type, max_ifc_array_access[i]); |
| 1147 | } |
| 1148 | glsl_interface_packing packing = |
| 1149 | (glsl_interface_packing) type->interface_packing; |
| 1150 | const glsl_type *new_ifc_type = |
| 1151 | glsl_type::get_interface_instance(fields, num_fields, |
| 1152 | packing, type->name); |
| 1153 | delete [] fields; |
| 1154 | return new_ifc_type; |
| 1155 | } |
Paul Berry | 15e05b9 | 2013-09-25 14:07:37 -0700 | [diff] [blame] | 1156 | |
| 1157 | static void fixup_unnamed_interface_type(const void *key, void *data, |
| 1158 | void *) |
| 1159 | { |
| 1160 | const glsl_type *ifc_type = (const glsl_type *) key; |
| 1161 | ir_variable **interface_vars = (ir_variable **) data; |
| 1162 | unsigned num_fields = ifc_type->length; |
| 1163 | glsl_struct_field *fields = new glsl_struct_field[num_fields]; |
| 1164 | memcpy(fields, ifc_type->fields.structure, |
| 1165 | num_fields * sizeof(*fields)); |
| 1166 | bool interface_type_changed = false; |
| 1167 | for (unsigned i = 0; i < num_fields; i++) { |
| 1168 | if (interface_vars[i] != NULL && |
| 1169 | fields[i].type != interface_vars[i]->type) { |
| 1170 | fields[i].type = interface_vars[i]->type; |
| 1171 | interface_type_changed = true; |
| 1172 | } |
| 1173 | } |
| 1174 | if (!interface_type_changed) { |
| 1175 | delete [] fields; |
| 1176 | return; |
| 1177 | } |
| 1178 | glsl_interface_packing packing = |
| 1179 | (glsl_interface_packing) ifc_type->interface_packing; |
| 1180 | const glsl_type *new_ifc_type = |
| 1181 | glsl_type::get_interface_instance(fields, num_fields, packing, |
| 1182 | ifc_type->name); |
| 1183 | delete [] fields; |
| 1184 | for (unsigned i = 0; i < num_fields; i++) { |
| 1185 | if (interface_vars[i] != NULL) |
| 1186 | interface_vars[i]->change_interface_type(new_ifc_type); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | /** |
| 1191 | * Memory context used to allocate the data in \c unnamed_interfaces. |
| 1192 | */ |
| 1193 | void *mem_ctx; |
| 1194 | |
| 1195 | /** |
| 1196 | * Hash table from const glsl_type * to an array of ir_variable *'s |
| 1197 | * pointing to the ir_variables constituting each unnamed interface block. |
| 1198 | */ |
| 1199 | hash_table *unnamed_interfaces; |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 1200 | }; |
| 1201 | |
Brian Paul | 84a1273 | 2012-02-02 20:10:40 -0700 | [diff] [blame] | 1202 | /** |
Eric Anholt | 6065a87 | 2013-06-12 18:12:40 -0700 | [diff] [blame] | 1203 | * Performs the cross-validation of geometry shader max_vertices and |
| 1204 | * primitive type layout qualifiers for the attached geometry shaders, |
| 1205 | * and propagates them to the linked GS and linked shader program. |
| 1206 | */ |
| 1207 | static void |
| 1208 | link_gs_inout_layout_qualifiers(struct gl_shader_program *prog, |
| 1209 | struct gl_shader *linked_shader, |
| 1210 | struct gl_shader **shader_list, |
| 1211 | unsigned num_shaders) |
| 1212 | { |
| 1213 | linked_shader->Geom.VerticesOut = 0; |
| 1214 | linked_shader->Geom.InputType = PRIM_UNKNOWN; |
| 1215 | linked_shader->Geom.OutputType = PRIM_UNKNOWN; |
| 1216 | |
| 1217 | /* No in/out qualifiers defined for anything but GLSL 1.50+ |
| 1218 | * geometry shaders so far. |
| 1219 | */ |
| 1220 | if (linked_shader->Type != GL_GEOMETRY_SHADER || prog->Version < 150) |
| 1221 | return; |
| 1222 | |
| 1223 | /* From the GLSL 1.50 spec, page 46: |
| 1224 | * |
| 1225 | * "All geometry shader output layout declarations in a program |
| 1226 | * must declare the same layout and same value for |
| 1227 | * max_vertices. There must be at least one geometry output |
| 1228 | * layout declaration somewhere in a program, but not all |
| 1229 | * geometry shaders (compilation units) are required to |
| 1230 | * declare it." |
| 1231 | */ |
| 1232 | |
| 1233 | for (unsigned i = 0; i < num_shaders; i++) { |
| 1234 | struct gl_shader *shader = shader_list[i]; |
| 1235 | |
| 1236 | if (shader->Geom.InputType != PRIM_UNKNOWN) { |
| 1237 | if (linked_shader->Geom.InputType != PRIM_UNKNOWN && |
| 1238 | linked_shader->Geom.InputType != shader->Geom.InputType) { |
| 1239 | linker_error(prog, "geometry shader defined with conflicting " |
| 1240 | "input types\n"); |
| 1241 | return; |
| 1242 | } |
| 1243 | linked_shader->Geom.InputType = shader->Geom.InputType; |
| 1244 | } |
| 1245 | |
| 1246 | if (shader->Geom.OutputType != PRIM_UNKNOWN) { |
| 1247 | if (linked_shader->Geom.OutputType != PRIM_UNKNOWN && |
| 1248 | linked_shader->Geom.OutputType != shader->Geom.OutputType) { |
| 1249 | linker_error(prog, "geometry shader defined with conflicting " |
| 1250 | "output types\n"); |
| 1251 | return; |
| 1252 | } |
| 1253 | linked_shader->Geom.OutputType = shader->Geom.OutputType; |
| 1254 | } |
| 1255 | |
| 1256 | if (shader->Geom.VerticesOut != 0) { |
| 1257 | if (linked_shader->Geom.VerticesOut != 0 && |
| 1258 | linked_shader->Geom.VerticesOut != shader->Geom.VerticesOut) { |
| 1259 | linker_error(prog, "geometry shader defined with conflicting " |
| 1260 | "output vertex count (%d and %d)\n", |
| 1261 | linked_shader->Geom.VerticesOut, |
| 1262 | shader->Geom.VerticesOut); |
| 1263 | return; |
| 1264 | } |
| 1265 | linked_shader->Geom.VerticesOut = shader->Geom.VerticesOut; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | /* Just do the intrastage -> interstage propagation right now, |
| 1270 | * since we already know we're in the right type of shader program |
| 1271 | * for doing it. |
| 1272 | */ |
| 1273 | if (linked_shader->Geom.InputType == PRIM_UNKNOWN) { |
| 1274 | linker_error(prog, |
| 1275 | "geometry shader didn't declare primitive input type\n"); |
| 1276 | return; |
| 1277 | } |
| 1278 | prog->Geom.InputType = linked_shader->Geom.InputType; |
| 1279 | |
| 1280 | if (linked_shader->Geom.OutputType == PRIM_UNKNOWN) { |
| 1281 | linker_error(prog, |
| 1282 | "geometry shader didn't declare primitive output type\n"); |
| 1283 | return; |
| 1284 | } |
| 1285 | prog->Geom.OutputType = linked_shader->Geom.OutputType; |
| 1286 | |
| 1287 | if (linked_shader->Geom.VerticesOut == 0) { |
| 1288 | linker_error(prog, |
| 1289 | "geometry shader didn't declare max_vertices\n"); |
| 1290 | return; |
| 1291 | } |
| 1292 | prog->Geom.VerticesOut = linked_shader->Geom.VerticesOut; |
| 1293 | } |
| 1294 | |
| 1295 | /** |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1296 | * Combine a group of shaders for a single stage to generate a linked shader |
| 1297 | * |
| 1298 | * \note |
| 1299 | * If this function is supplied a single shader, it is cloned, and the new |
| 1300 | * shader is returned. |
| 1301 | */ |
| 1302 | static struct gl_shader * |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1303 | link_intrastage_shaders(void *mem_ctx, |
| 1304 | struct gl_context *ctx, |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 1305 | struct gl_shader_program *prog, |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1306 | struct gl_shader **shader_list, |
| 1307 | unsigned num_shaders) |
| 1308 | { |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1309 | struct gl_uniform_block *uniform_blocks = NULL; |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1310 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1311 | /* Check that global variables defined in multiple shaders are consistent. |
| 1312 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 1313 | cross_validate_globals(prog, shader_list, num_shaders, false); |
| 1314 | if (!prog->LinkStatus) |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1315 | return NULL; |
| 1316 | |
Jordan Justen | 4a0bcd9 | 2013-05-20 23:42:49 -0700 | [diff] [blame] | 1317 | /* Check that interface blocks defined in multiple shaders are consistent. |
| 1318 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 1319 | validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list, |
| 1320 | num_shaders); |
| 1321 | if (!prog->LinkStatus) |
Jordan Justen | 4a0bcd9 | 2013-05-20 23:42:49 -0700 | [diff] [blame] | 1322 | return NULL; |
| 1323 | |
Paul Berry | 4682b9b | 2013-07-27 15:07:08 -0700 | [diff] [blame] | 1324 | /* Link up uniform blocks defined within this stage. */ |
| 1325 | const unsigned num_uniform_blocks = |
Ian Romanick | 514f8c7 | 2013-01-22 01:09:16 -0500 | [diff] [blame] | 1326 | link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders, |
| 1327 | &uniform_blocks); |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1328 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1329 | /* Check that there is only a single definition of each function signature |
| 1330 | * across all shaders. |
| 1331 | */ |
| 1332 | for (unsigned i = 0; i < (num_shaders - 1); i++) { |
| 1333 | foreach_list(node, shader_list[i]->ir) { |
| 1334 | ir_function *const f = ((ir_instruction *) node)->as_function(); |
| 1335 | |
| 1336 | if (f == NULL) |
| 1337 | continue; |
| 1338 | |
| 1339 | for (unsigned j = i + 1; j < num_shaders; j++) { |
| 1340 | ir_function *const other = |
| 1341 | shader_list[j]->symbols->get_function(f->name); |
| 1342 | |
| 1343 | /* If the other shader has no function (and therefore no function |
| 1344 | * signatures) with the same name, skip to the next shader. |
| 1345 | */ |
| 1346 | if (other == NULL) |
| 1347 | continue; |
| 1348 | |
| 1349 | foreach_iter (exec_list_iterator, iter, *f) { |
| 1350 | ir_function_signature *sig = |
| 1351 | (ir_function_signature *) iter.get(); |
| 1352 | |
Kenneth Graunke | 4b0bac0 | 2013-08-30 16:12:55 -0700 | [diff] [blame] | 1353 | if (!sig->is_defined || sig->is_builtin()) |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1354 | continue; |
| 1355 | |
| 1356 | ir_function_signature *other_sig = |
Kenneth Graunke | 3e820e3 | 2013-08-30 23:11:55 -0700 | [diff] [blame] | 1357 | other->exact_matching_signature(NULL, &sig->parameters); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1358 | |
| 1359 | if ((other_sig != NULL) && other_sig->is_defined |
Kenneth Graunke | 4b0bac0 | 2013-08-30 16:12:55 -0700 | [diff] [blame] | 1360 | && !other_sig->is_builtin()) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1361 | linker_error(prog, "function `%s' is multiply defined", |
| 1362 | f->name); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1363 | return NULL; |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | /* Find the shader that defines main, and make a clone of it. |
| 1371 | * |
| 1372 | * Starting with the clone, search for undefined references. If one is |
| 1373 | * found, find the shader that defines it. Clone the reference and add |
| 1374 | * it to the shader. Repeat until there are no undefined references or |
| 1375 | * until a reference cannot be resolved. |
| 1376 | */ |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1377 | gl_shader *main = NULL; |
| 1378 | for (unsigned i = 0; i < num_shaders; i++) { |
| 1379 | if (get_main_function_signature(shader_list[i]) != NULL) { |
| 1380 | main = shader_list[i]; |
| 1381 | break; |
| 1382 | } |
| 1383 | } |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1384 | |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1385 | if (main == NULL) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1386 | linker_error(prog, "%s shader lacks `main'\n", |
Eric Anholt | faf3dba | 2013-06-12 16:57:11 -0700 | [diff] [blame] | 1387 | _mesa_glsl_shader_target_name(shader_list[0]->Type)); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1388 | return NULL; |
| 1389 | } |
| 1390 | |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 1391 | gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1392 | linked->ir = new(linked) exec_list; |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1393 | clone_ir_list(mem_ctx, linked->ir, main->ir); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1394 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 1395 | linked->UniformBlocks = uniform_blocks; |
| 1396 | linked->NumUniformBlocks = num_uniform_blocks; |
| 1397 | ralloc_steal(linked, linked->UniformBlocks); |
| 1398 | |
Eric Anholt | 6065a87 | 2013-06-12 18:12:40 -0700 | [diff] [blame] | 1399 | link_gs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders); |
| 1400 | |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 1401 | populate_symbol_table(linked); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1402 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1403 | /* The a pointer to the main function in the final linked shader (i.e., the |
| 1404 | * copy of the original shader that contained the main function). |
| 1405 | */ |
| 1406 | ir_function_signature *const main_sig = get_main_function_signature(linked); |
| 1407 | |
| 1408 | /* Move any instructions other than variable declarations or function |
| 1409 | * declarations into main. |
| 1410 | */ |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 1411 | exec_node *insertion_point = |
| 1412 | move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false, |
| 1413 | linked); |
| 1414 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1415 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 1416 | if (shader_list[i] == main) |
| 1417 | continue; |
| 1418 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1419 | insertion_point = move_non_declarations(shader_list[i]->ir, |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 1420 | insertion_point, true, linked); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 1423 | /* Resolve initializers for global variables in the linked shader. |
| 1424 | */ |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 1425 | unsigned num_linking_shaders = num_shaders; |
| 1426 | for (unsigned i = 0; i < num_shaders; i++) |
| 1427 | num_linking_shaders += shader_list[i]->num_builtins_to_link; |
| 1428 | |
| 1429 | gl_shader **linking_shaders = |
| 1430 | (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *)); |
| 1431 | |
| 1432 | memcpy(linking_shaders, shader_list, |
| 1433 | sizeof(linking_shaders[0]) * num_shaders); |
| 1434 | |
| 1435 | unsigned idx = num_shaders; |
| 1436 | for (unsigned i = 0; i < num_shaders; i++) { |
| 1437 | memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link, |
| 1438 | sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link); |
| 1439 | idx += shader_list[i]->num_builtins_to_link; |
| 1440 | } |
| 1441 | |
| 1442 | assert(idx == num_linking_shaders); |
| 1443 | |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 1444 | if (!link_function_calls(prog, linked, linking_shaders, |
| 1445 | num_linking_shaders)) { |
| 1446 | ctx->Driver.DeleteShader(ctx, linked); |
Kenneth Graunke | 7d2423a | 2013-08-02 00:35:05 -0700 | [diff] [blame] | 1447 | free(linking_shaders); |
| 1448 | return NULL; |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 1449 | } |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 1450 | |
| 1451 | free(linking_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1452 | |
Paul Berry | c148ef6 | 2011-08-03 15:37:01 -0700 | [diff] [blame] | 1453 | /* At this point linked should contain all of the linked IR, so |
| 1454 | * validate it to make sure nothing went wrong. |
| 1455 | */ |
Kenneth Graunke | 7d2423a | 2013-08-02 00:35:05 -0700 | [diff] [blame] | 1456 | validate_ir_tree(linked->ir); |
Paul Berry | c148ef6 | 2011-08-03 15:37:01 -0700 | [diff] [blame] | 1457 | |
Paul Berry | 7cfefe6 | 2013-07-30 21:13:48 -0700 | [diff] [blame] | 1458 | /* Set the size of geometry shader input arrays */ |
| 1459 | if (linked->Type == GL_GEOMETRY_SHADER) { |
| 1460 | unsigned num_vertices = vertices_per_prim(prog->Geom.InputType); |
| 1461 | geom_array_resize_visitor input_resize_visitor(num_vertices, prog); |
| 1462 | foreach_iter(exec_list_iterator, iter, *linked->ir) { |
| 1463 | ir_instruction *ir = (ir_instruction *)iter.get(); |
| 1464 | ir->accept(&input_resize_visitor); |
| 1465 | } |
| 1466 | } |
| 1467 | |
Ian Romanick | c87e9ef | 2011-01-25 12:04:08 -0800 | [diff] [blame] | 1468 | /* Make a pass over all variable declarations to ensure that arrays with |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 1469 | * unspecified sizes have a size specified. The size is inferred from the |
| 1470 | * max_array_access field. |
| 1471 | */ |
Kenneth Graunke | 7d2423a | 2013-08-02 00:35:05 -0700 | [diff] [blame] | 1472 | array_sizing_visitor v; |
| 1473 | v.run(linked->ir); |
Paul Berry | 15e05b9 | 2013-09-25 14:07:37 -0700 | [diff] [blame] | 1474 | v.fixup_unnamed_interface_types(); |
Ian Romanick | 6f53921 | 2010-12-07 18:30:33 -0800 | [diff] [blame] | 1475 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1476 | return linked; |
| 1477 | } |
| 1478 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1479 | /** |
| 1480 | * Update the sizes of linked shader uniform arrays to the maximum |
| 1481 | * array index used. |
| 1482 | * |
| 1483 | * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec: |
| 1484 | * |
| 1485 | * If one or more elements of an array are active, |
| 1486 | * GetActiveUniform will return the name of the array in name, |
| 1487 | * subject to the restrictions listed above. The type of the array |
| 1488 | * is returned in type. The size parameter contains the highest |
| 1489 | * array element index used, plus one. The compiler or linker |
| 1490 | * determines the highest index used. There will be only one |
| 1491 | * active uniform reported by the GL per uniform array. |
| 1492 | |
| 1493 | */ |
| 1494 | static void |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 1495 | update_array_sizes(struct gl_shader_program *prog) |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1496 | { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1497 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1498 | if (prog->_LinkedShaders[i] == NULL) |
| 1499 | continue; |
| 1500 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1501 | foreach_list(node, prog->_LinkedShaders[i]->ir) { |
| 1502 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1503 | |
Paul Berry | 6a2baf3 | 2013-06-10 14:01:45 -0700 | [diff] [blame] | 1504 | if ((var == NULL) || (var->mode != ir_var_uniform) || |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1505 | !var->type->is_array()) |
| 1506 | continue; |
| 1507 | |
Eric Anholt | 9feb403 | 2012-05-01 14:43:31 -0700 | [diff] [blame] | 1508 | /* GL_ARB_uniform_buffer_object says that std140 uniforms |
| 1509 | * will not be eliminated. Since we always do std140, just |
| 1510 | * don't resize arrays in UBOs. |
| 1511 | */ |
Ian Romanick | 13be1f4 | 2012-12-14 12:00:14 -0800 | [diff] [blame] | 1512 | if (var->is_in_uniform_block()) |
Eric Anholt | 9feb403 | 2012-05-01 14:43:31 -0700 | [diff] [blame] | 1513 | continue; |
| 1514 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1515 | unsigned int size = var->max_array_access; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1516 | for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) { |
| 1517 | if (prog->_LinkedShaders[j] == NULL) |
| 1518 | continue; |
| 1519 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1520 | foreach_list(node2, prog->_LinkedShaders[j]->ir) { |
| 1521 | ir_variable *other_var = ((ir_instruction *) node2)->as_variable(); |
| 1522 | if (!other_var) |
| 1523 | continue; |
| 1524 | |
| 1525 | if (strcmp(var->name, other_var->name) == 0 && |
| 1526 | other_var->max_array_access > size) { |
| 1527 | size = other_var->max_array_access; |
| 1528 | } |
| 1529 | } |
| 1530 | } |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 1531 | |
Fabian Bieler | 6368478 | 2013-06-14 13:37:07 +0200 | [diff] [blame] | 1532 | if (size + 1 != var->type->length) { |
Ian Romanick | 89d81ab | 2011-01-25 10:41:20 -0800 | [diff] [blame] | 1533 | /* If this is a built-in uniform (i.e., it's backed by some |
| 1534 | * fixed-function state), adjust the number of state slots to |
| 1535 | * match the new array size. The number of slots per array entry |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 1536 | * is not known. It seems safe to assume that the total number of |
Ian Romanick | 89d81ab | 2011-01-25 10:41:20 -0800 | [diff] [blame] | 1537 | * slots is an integer multiple of the number of array elements. |
| 1538 | * Determine the number of slots per array element by dividing by |
| 1539 | * the old (total) size. |
| 1540 | */ |
| 1541 | if (var->num_state_slots > 0) { |
| 1542 | var->num_state_slots = (size + 1) |
| 1543 | * (var->num_state_slots / var->type->length); |
| 1544 | } |
| 1545 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1546 | var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 1547 | size + 1); |
| 1548 | /* FINISHME: We should update the types of array |
| 1549 | * dereferences of this variable now. |
| 1550 | */ |
| 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1556 | /** |
Bryan Cain | f18a086 | 2011-04-23 19:29:15 -0500 | [diff] [blame] | 1557 | * Find a contiguous set of available bits in a bitmask. |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1558 | * |
| 1559 | * \param used_mask Bits representing used (1) and unused (0) locations |
| 1560 | * \param needed_count Number of contiguous bits needed. |
| 1561 | * |
| 1562 | * \return |
| 1563 | * Base location of the available bits on success or -1 on failure. |
| 1564 | */ |
| 1565 | int |
| 1566 | find_available_slots(unsigned used_mask, unsigned needed_count) |
| 1567 | { |
| 1568 | unsigned needed_mask = (1 << needed_count) - 1; |
| 1569 | const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count; |
| 1570 | |
| 1571 | /* The comparison to 32 is redundant, but without it GCC emits "warning: |
| 1572 | * cannot optimize possibly infinite loops" for the loop below. |
| 1573 | */ |
| 1574 | if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32)) |
| 1575 | return -1; |
| 1576 | |
| 1577 | for (int i = 0; i <= max_bit_to_test; i++) { |
| 1578 | if ((needed_mask & ~used_mask) == needed_mask) |
| 1579 | return i; |
| 1580 | |
| 1581 | needed_mask <<= 1; |
| 1582 | } |
| 1583 | |
| 1584 | return -1; |
| 1585 | } |
| 1586 | |
| 1587 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1588 | /** |
| 1589 | * Assign locations for either VS inputs for FS outputs |
| 1590 | * |
| 1591 | * \param prog Shader program whose variables need locations assigned |
| 1592 | * \param target_index Selector for the program target to receive location |
| 1593 | * assignmnets. Must be either \c MESA_SHADER_VERTEX or |
| 1594 | * \c MESA_SHADER_FRAGMENT. |
| 1595 | * \param max_index Maximum number of generic locations. This corresponds |
| 1596 | * to either the maximum number of draw buffers or the |
| 1597 | * maximum number of generic attributes. |
| 1598 | * |
| 1599 | * \return |
| 1600 | * If locations are successfully assigned, true is returned. Otherwise an |
| 1601 | * error is emitted to the shader link log and false is returned. |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1602 | */ |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1603 | bool |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1604 | assign_attribute_or_color_locations(gl_shader_program *prog, |
| 1605 | unsigned target_index, |
| 1606 | unsigned max_index) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1607 | { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1608 | /* Mark invalid locations as being used. |
Ian Romanick | 9342d26 | 2010-06-22 17:41:37 -0700 | [diff] [blame] | 1609 | */ |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1610 | unsigned used_locations = (max_index >= 32) |
| 1611 | ? ~0 : ~((1 << max_index) - 1); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1612 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1613 | assert((target_index == MESA_SHADER_VERTEX) |
| 1614 | || (target_index == MESA_SHADER_FRAGMENT)); |
| 1615 | |
| 1616 | gl_shader *const sh = prog->_LinkedShaders[target_index]; |
| 1617 | if (sh == NULL) |
| 1618 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1619 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1620 | /* Operate in a total of four passes. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1621 | * |
| 1622 | * 1. Invalidate the location assignments for all vertex shader inputs. |
| 1623 | * |
| 1624 | * 2. Assign locations for inputs that have user-defined (via |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1625 | * glBindVertexAttribLocation) locations and outputs that have |
| 1626 | * user-defined locations (via glBindFragDataLocation). |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1627 | * |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1628 | * 3. Sort the attributes without assigned locations by number of slots |
| 1629 | * required in decreasing order. Fragmentation caused by attribute |
| 1630 | * locations assigned by the application may prevent large attributes |
| 1631 | * from having enough contiguous space. |
| 1632 | * |
| 1633 | * 4. Assign locations to any inputs without assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1634 | */ |
| 1635 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1636 | const int generic_base = (target_index == MESA_SHADER_VERTEX) |
Brian Paul | 7eb7d67 | 2011-07-07 16:47:59 -0600 | [diff] [blame] | 1637 | ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1638 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1639 | const enum ir_variable_mode direction = |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1640 | (target_index == MESA_SHADER_VERTEX) |
| 1641 | ? ir_var_shader_in : ir_var_shader_out; |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1642 | |
| 1643 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1644 | /* Temporary storage for the set of attributes that need locations assigned. |
| 1645 | */ |
| 1646 | struct temp_attr { |
| 1647 | unsigned slots; |
| 1648 | ir_variable *var; |
| 1649 | |
| 1650 | /* Used below in the call to qsort. */ |
| 1651 | static int compare(const void *a, const void *b) |
| 1652 | { |
| 1653 | const temp_attr *const l = (const temp_attr *) a; |
| 1654 | const temp_attr *const r = (const temp_attr *) b; |
| 1655 | |
| 1656 | /* Reversed because we want a descending order sort below. */ |
| 1657 | return r->slots - l->slots; |
| 1658 | } |
| 1659 | } to_assign[16]; |
| 1660 | |
| 1661 | unsigned num_attr = 0; |
| 1662 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1663 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1664 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1665 | |
Brian Paul | 4470ff2 | 2011-07-19 21:10:25 -0600 | [diff] [blame] | 1666 | if ((var == NULL) || (var->mode != (unsigned) direction)) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1667 | continue; |
| 1668 | |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1669 | if (var->explicit_location) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1670 | if ((var->location >= (int)(max_index + generic_base)) |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1671 | || (var->location < 0)) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1672 | linker_error(prog, |
| 1673 | "invalid explicit location %d specified for `%s'\n", |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1674 | (var->location < 0) |
| 1675 | ? var->location : var->location - generic_base, |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1676 | var->name); |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1677 | return false; |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1678 | } |
| 1679 | } else if (target_index == MESA_SHADER_VERTEX) { |
| 1680 | unsigned binding; |
| 1681 | |
| 1682 | if (prog->AttributeBindings->get(binding, var->name)) { |
| 1683 | assert(binding >= VERT_ATTRIB_GENERIC0); |
| 1684 | var->location = binding; |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 1685 | var->is_unmatched_generic_inout = 0; |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1686 | } |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1687 | } else if (target_index == MESA_SHADER_FRAGMENT) { |
| 1688 | unsigned binding; |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1689 | unsigned index; |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1690 | |
| 1691 | if (prog->FragDataBindings->get(binding, var->name)) { |
| 1692 | assert(binding >= FRAG_RESULT_DATA0); |
| 1693 | var->location = binding; |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 1694 | var->is_unmatched_generic_inout = 0; |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1695 | |
| 1696 | if (prog->FragDataIndexBindings->get(index, var->name)) { |
| 1697 | var->index = index; |
| 1698 | } |
Ian Romanick | b12b5d9 | 2011-11-04 16:08:52 -0700 | [diff] [blame] | 1699 | } |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
Ian Romanick | 9f0e98d | 2011-10-06 10:25:34 -0700 | [diff] [blame] | 1702 | /* If the variable is not a built-in and has a location statically |
| 1703 | * assigned in the shader (presumably via a layout qualifier), make sure |
| 1704 | * that it doesn't collide with other assigned locations. Otherwise, |
| 1705 | * add it to the list of variables that need linker-assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1706 | */ |
Paul Berry | 0026ad4 | 2013-07-31 08:15:08 -0700 | [diff] [blame] | 1707 | const unsigned slots = var->type->count_attribute_slots(); |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1708 | if (var->location != -1) { |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1709 | if (var->location >= generic_base && var->index < 1) { |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1710 | /* From page 61 of the OpenGL 4.0 spec: |
| 1711 | * |
| 1712 | * "LinkProgram will fail if the attribute bindings assigned |
| 1713 | * by BindAttribLocation do not leave not enough space to |
| 1714 | * assign a location for an active matrix attribute or an |
| 1715 | * active attribute array, both of which require multiple |
| 1716 | * contiguous generic attributes." |
| 1717 | * |
| 1718 | * Previous versions of the spec contain similar language but omit |
| 1719 | * the bit about attribute arrays. |
| 1720 | * |
| 1721 | * Page 61 of the OpenGL 4.0 spec also says: |
| 1722 | * |
| 1723 | * "It is possible for an application to bind more than one |
| 1724 | * attribute name to the same location. This is referred to as |
| 1725 | * aliasing. This will only work if only one of the aliased |
| 1726 | * attributes is active in the executable program, or if no |
| 1727 | * path through the shader consumes more than one attribute of |
| 1728 | * a set of attributes aliased to the same location. A link |
| 1729 | * error can occur if the linker determines that every path |
| 1730 | * through the shader consumes multiple aliased attributes, |
| 1731 | * but implementations are not required to generate an error |
| 1732 | * in this case." |
| 1733 | * |
| 1734 | * These two paragraphs are either somewhat contradictory, or I |
| 1735 | * don't fully understand one or both of them. |
| 1736 | */ |
| 1737 | /* FINISHME: The code as currently written does not support |
| 1738 | * FINISHME: attribute location aliasing (see comment above). |
| 1739 | */ |
| 1740 | /* Mask representing the contiguous slots that will be used by |
| 1741 | * this attribute. |
| 1742 | */ |
| 1743 | const unsigned attr = var->location - generic_base; |
| 1744 | const unsigned use_mask = (1 << slots) - 1; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1745 | |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1746 | /* Generate a link error if the set of bits requested for this |
| 1747 | * attribute overlaps any previously allocated bits. |
| 1748 | */ |
| 1749 | if ((~(use_mask << attr) & used_locations) != used_locations) { |
Dave Airlie | 7449ae4 | 2011-11-20 19:56:35 +0000 | [diff] [blame] | 1750 | const char *const string = (target_index == MESA_SHADER_VERTEX) |
| 1751 | ? "vertex shader input" : "fragment shader output"; |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1752 | linker_error(prog, |
Dave Airlie | 7449ae4 | 2011-11-20 19:56:35 +0000 | [diff] [blame] | 1753 | "insufficient contiguous locations " |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 1754 | "available for %s `%s' %d %d %d", string, |
| 1755 | var->name, used_locations, use_mask, attr); |
Ian Romanick | 523b611 | 2011-08-17 15:40:03 -0700 | [diff] [blame] | 1756 | return false; |
| 1757 | } |
| 1758 | |
| 1759 | used_locations |= (use_mask << attr); |
| 1760 | } |
| 1761 | |
| 1762 | continue; |
| 1763 | } |
| 1764 | |
| 1765 | to_assign[num_attr].slots = slots; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1766 | to_assign[num_attr].var = var; |
| 1767 | num_attr++; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1768 | } |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1769 | |
| 1770 | /* If all of the attributes were assigned locations by the application (or |
| 1771 | * are built-in attributes with fixed locations), return early. This should |
| 1772 | * be the common case. |
| 1773 | */ |
| 1774 | if (num_attr == 0) |
| 1775 | return true; |
| 1776 | |
| 1777 | qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare); |
| 1778 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1779 | if (target_index == MESA_SHADER_VERTEX) { |
| 1780 | /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can |
| 1781 | * only be explicitly assigned by via glBindAttribLocation. Mark it as |
| 1782 | * reserved to prevent it from being automatically allocated below. |
| 1783 | */ |
| 1784 | find_deref_visitor find("gl_Vertex"); |
| 1785 | find.run(sh->ir); |
| 1786 | if (find.variable_found()) |
| 1787 | used_locations |= (1 << 0); |
| 1788 | } |
Ian Romanick | 982e379 | 2010-06-29 18:58:20 -0700 | [diff] [blame] | 1789 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1790 | for (unsigned i = 0; i < num_attr; i++) { |
| 1791 | /* Mask representing the contiguous slots that will be used by this |
| 1792 | * attribute. |
| 1793 | */ |
| 1794 | const unsigned use_mask = (1 << to_assign[i].slots) - 1; |
| 1795 | |
| 1796 | int location = find_available_slots(used_locations, to_assign[i].slots); |
| 1797 | |
| 1798 | if (location < 0) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1799 | const char *const string = (target_index == MESA_SHADER_VERTEX) |
| 1800 | ? "vertex shader input" : "fragment shader output"; |
| 1801 | |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1802 | linker_error(prog, |
Dave Airlie | 7449ae4 | 2011-11-20 19:56:35 +0000 | [diff] [blame] | 1803 | "insufficient contiguous locations " |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 1804 | "available for %s `%s'", |
| 1805 | string, to_assign[i].var->name); |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1806 | return false; |
| 1807 | } |
| 1808 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 1809 | to_assign[i].var->location = generic_base + location; |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 1810 | to_assign[i].var->is_unmatched_generic_inout = 0; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1811 | used_locations |= (use_mask << location); |
| 1812 | } |
| 1813 | |
| 1814 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1818 | /** |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1819 | * Demote shader inputs and outputs that are not used in other stages |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1820 | */ |
| 1821 | void |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1822 | demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode) |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1823 | { |
| 1824 | foreach_list(node, sh->ir) { |
| 1825 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1826 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1827 | if ((var == NULL) || (var->mode != int(mode))) |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1828 | continue; |
| 1829 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1830 | /* A shader 'in' or 'out' variable is only really an input or output if |
| 1831 | * its value is used by other shader stages. This will cause the variable |
| 1832 | * to have a location assigned. |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1833 | */ |
Paul Berry | 3c9c17d | 2012-12-04 15:17:01 -0800 | [diff] [blame] | 1834 | if (var->is_unmatched_generic_inout) { |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1835 | var->mode = ir_var_auto; |
| 1836 | } |
| 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1841 | /** |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 1842 | * Store the gl_FragDepth layout in the gl_shader_program struct. |
| 1843 | */ |
| 1844 | static void |
| 1845 | store_fragdepth_layout(struct gl_shader_program *prog) |
| 1846 | { |
| 1847 | if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { |
| 1848 | return; |
| 1849 | } |
| 1850 | |
| 1851 | struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir; |
| 1852 | |
| 1853 | /* We don't look up the gl_FragDepth symbol directly because if |
| 1854 | * gl_FragDepth is not used in the shader, it's removed from the IR. |
| 1855 | * However, the symbol won't be removed from the symbol table. |
| 1856 | * |
| 1857 | * We're only interested in the cases where the variable is NOT removed |
| 1858 | * from the IR. |
| 1859 | */ |
| 1860 | foreach_list(node, ir) { |
| 1861 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1862 | |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1863 | if (var == NULL || var->mode != ir_var_shader_out) { |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 1864 | continue; |
| 1865 | } |
| 1866 | |
| 1867 | if (strcmp(var->name, "gl_FragDepth") == 0) { |
| 1868 | switch (var->depth_layout) { |
| 1869 | case ir_depth_layout_none: |
| 1870 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE; |
| 1871 | return; |
| 1872 | case ir_depth_layout_any: |
| 1873 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY; |
| 1874 | return; |
| 1875 | case ir_depth_layout_greater: |
| 1876 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER; |
| 1877 | return; |
| 1878 | case ir_depth_layout_less: |
| 1879 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS; |
| 1880 | return; |
| 1881 | case ir_depth_layout_unchanged: |
| 1882 | prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED; |
| 1883 | return; |
| 1884 | default: |
| 1885 | assert(0); |
| 1886 | return; |
| 1887 | } |
| 1888 | } |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | /** |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1893 | * Validate the resources used by a program versus the implementation limits |
| 1894 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 1895 | static void |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1896 | check_resources(struct gl_context *ctx, struct gl_shader_program *prog) |
| 1897 | { |
| 1898 | static const char *const shader_names[MESA_SHADER_TYPES] = { |
Marek Olšák | 030ca23 | 2013-06-12 17:15:46 +0200 | [diff] [blame] | 1899 | "vertex", "geometry", "fragment" |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1900 | }; |
| 1901 | |
| 1902 | const unsigned max_samplers[MESA_SHADER_TYPES] = { |
Marek Olšák | 5e78433 | 2013-05-02 02:30:44 +0200 | [diff] [blame] | 1903 | ctx->Const.VertexProgram.MaxTextureImageUnits, |
Marek Olšák | 030ca23 | 2013-06-12 17:15:46 +0200 | [diff] [blame] | 1904 | ctx->Const.GeometryProgram.MaxTextureImageUnits, |
| 1905 | ctx->Const.FragmentProgram.MaxTextureImageUnits |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1906 | }; |
| 1907 | |
Eric Anholt | 38e77e5 | 2013-05-23 11:10:15 -0700 | [diff] [blame] | 1908 | const unsigned max_default_uniform_components[MESA_SHADER_TYPES] = { |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1909 | ctx->Const.VertexProgram.MaxUniformComponents, |
Marek Olšák | 030ca23 | 2013-06-12 17:15:46 +0200 | [diff] [blame] | 1910 | ctx->Const.GeometryProgram.MaxUniformComponents, |
| 1911 | ctx->Const.FragmentProgram.MaxUniformComponents |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1912 | }; |
| 1913 | |
Eric Anholt | 38e77e5 | 2013-05-23 11:10:15 -0700 | [diff] [blame] | 1914 | const unsigned max_combined_uniform_components[MESA_SHADER_TYPES] = { |
| 1915 | ctx->Const.VertexProgram.MaxCombinedUniformComponents, |
Marek Olšák | 030ca23 | 2013-06-12 17:15:46 +0200 | [diff] [blame] | 1916 | ctx->Const.GeometryProgram.MaxCombinedUniformComponents, |
| 1917 | ctx->Const.FragmentProgram.MaxCombinedUniformComponents |
Eric Anholt | 38e77e5 | 2013-05-23 11:10:15 -0700 | [diff] [blame] | 1918 | }; |
| 1919 | |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 1920 | const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = { |
| 1921 | ctx->Const.VertexProgram.MaxUniformBlocks, |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 1922 | ctx->Const.GeometryProgram.MaxUniformBlocks, |
Marek Olšák | 030ca23 | 2013-06-12 17:15:46 +0200 | [diff] [blame] | 1923 | ctx->Const.FragmentProgram.MaxUniformBlocks |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 1924 | }; |
| 1925 | |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1926 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1927 | struct gl_shader *sh = prog->_LinkedShaders[i]; |
| 1928 | |
| 1929 | if (sh == NULL) |
| 1930 | continue; |
| 1931 | |
| 1932 | if (sh->num_samplers > max_samplers[i]) { |
| 1933 | linker_error(prog, "Too many %s shader texture samplers", |
| 1934 | shader_names[i]); |
| 1935 | } |
| 1936 | |
Eric Anholt | 38e77e5 | 2013-05-23 11:10:15 -0700 | [diff] [blame] | 1937 | if (sh->num_uniform_components > max_default_uniform_components[i]) { |
| 1938 | if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) { |
| 1939 | linker_warning(prog, "Too many %s shader default uniform block " |
| 1940 | "components, but the driver will try to optimize " |
| 1941 | "them out; this is non-portable out-of-spec " |
| 1942 | "behavior\n", |
| 1943 | shader_names[i]); |
| 1944 | } else { |
| 1945 | linker_error(prog, "Too many %s shader default uniform block " |
| 1946 | "components", |
| 1947 | shader_names[i]); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | if (sh->num_combined_uniform_components > |
| 1952 | max_combined_uniform_components[i]) { |
Marek Olšák | df809ae | 2011-12-10 04:14:46 +0100 | [diff] [blame] | 1953 | if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) { |
| 1954 | linker_warning(prog, "Too many %s shader uniform components, " |
| 1955 | "but the driver will try to optimize them out; " |
| 1956 | "this is non-portable out-of-spec behavior\n", |
| 1957 | shader_names[i]); |
| 1958 | } else { |
| 1959 | linker_error(prog, "Too many %s shader uniform components", |
| 1960 | shader_names[i]); |
| 1961 | } |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1962 | } |
| 1963 | } |
| 1964 | |
Eric Anholt | 877a897 | 2012-06-25 12:47:01 -0700 | [diff] [blame] | 1965 | unsigned blocks[MESA_SHADER_TYPES] = {0}; |
| 1966 | unsigned total_uniform_blocks = 0; |
| 1967 | |
| 1968 | for (unsigned i = 0; i < prog->NumUniformBlocks; i++) { |
| 1969 | for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) { |
| 1970 | if (prog->UniformBlockStageIndex[j][i] != -1) { |
| 1971 | blocks[j]++; |
| 1972 | total_uniform_blocks++; |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) { |
| 1977 | linker_error(prog, "Too many combined uniform blocks (%d/%d)", |
| 1978 | prog->NumUniformBlocks, |
| 1979 | ctx->Const.MaxCombinedUniformBlocks); |
| 1980 | } else { |
| 1981 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1982 | if (blocks[i] > max_uniform_blocks[i]) { |
| 1983 | linker_error(prog, "Too many %s uniform blocks (%d/%d)", |
| 1984 | shader_names[i], |
| 1985 | blocks[i], |
| 1986 | max_uniform_blocks[i]); |
| 1987 | break; |
| 1988 | } |
| 1989 | } |
| 1990 | } |
| 1991 | } |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 1992 | } |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1993 | |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1994 | void |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 1995 | link_shaders(struct gl_context *ctx, struct gl_shader_program *prog) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1996 | { |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 1997 | tfeedback_decl *tfeedback_decls = NULL; |
| 1998 | unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying; |
| 1999 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 2000 | void *mem_ctx = ralloc_context(NULL); // temporary linker context |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 2001 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2002 | prog->LinkStatus = true; /* All error paths will set this to false */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2003 | prog->Validated = false; |
| 2004 | prog->_Used = false; |
| 2005 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 2006 | ralloc_free(prog->InfoLog); |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 2007 | prog->InfoLog = ralloc_strdup(NULL, ""); |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 2008 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 2009 | ralloc_free(prog->UniformBlocks); |
| 2010 | prog->UniformBlocks = NULL; |
| 2011 | prog->NumUniformBlocks = 0; |
| 2012 | for (int i = 0; i < MESA_SHADER_TYPES; i++) { |
| 2013 | ralloc_free(prog->UniformBlockStageIndex[i]); |
| 2014 | prog->UniformBlockStageIndex[i] = NULL; |
| 2015 | } |
| 2016 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2017 | /* Separate the shaders into groups based on their type. |
| 2018 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 2019 | struct gl_shader **vert_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2020 | unsigned num_vert_shaders = 0; |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 2021 | struct gl_shader **frag_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2022 | unsigned num_frag_shaders = 0; |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 2023 | struct gl_shader **geom_shader_list; |
| 2024 | unsigned num_geom_shaders = 0; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2025 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 2026 | vert_shader_list = (struct gl_shader **) |
Paul Berry | 844bd71 | 2013-07-30 22:38:43 -0700 | [diff] [blame] | 2027 | calloc(prog->NumShaders, sizeof(struct gl_shader *)); |
| 2028 | frag_shader_list = (struct gl_shader **) |
| 2029 | calloc(prog->NumShaders, sizeof(struct gl_shader *)); |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 2030 | geom_shader_list = (struct gl_shader **) |
| 2031 | calloc(prog->NumShaders, sizeof(struct gl_shader *)); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2032 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 2033 | unsigned min_version = UINT_MAX; |
| 2034 | unsigned max_version = 0; |
Paul Berry | a9f34dc | 2012-08-02 17:49:44 -0700 | [diff] [blame] | 2035 | const bool is_es_prog = |
| 2036 | (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2037 | for (unsigned i = 0; i < prog->NumShaders; i++) { |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 2038 | min_version = MIN2(min_version, prog->Shaders[i]->Version); |
| 2039 | max_version = MAX2(max_version, prog->Shaders[i]->Version); |
| 2040 | |
Paul Berry | a9f34dc | 2012-08-02 17:49:44 -0700 | [diff] [blame] | 2041 | if (prog->Shaders[i]->IsES != is_es_prog) { |
| 2042 | linker_error(prog, "all shaders must use same shading " |
| 2043 | "language version\n"); |
| 2044 | goto done; |
| 2045 | } |
| 2046 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2047 | switch (prog->Shaders[i]->Type) { |
| 2048 | case GL_VERTEX_SHADER: |
| 2049 | vert_shader_list[num_vert_shaders] = prog->Shaders[i]; |
| 2050 | num_vert_shaders++; |
| 2051 | break; |
| 2052 | case GL_FRAGMENT_SHADER: |
| 2053 | frag_shader_list[num_frag_shaders] = prog->Shaders[i]; |
| 2054 | num_frag_shaders++; |
| 2055 | break; |
| 2056 | case GL_GEOMETRY_SHADER: |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 2057 | geom_shader_list[num_geom_shaders] = prog->Shaders[i]; |
| 2058 | num_geom_shaders++; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2059 | break; |
| 2060 | } |
| 2061 | } |
| 2062 | |
Paul Berry | 672fab0 | 2013-10-13 18:01:11 -0700 | [diff] [blame] | 2063 | /* In desktop GLSL, different shader versions may be linked together. In |
| 2064 | * GLSL ES, all shader versions must be the same. |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 2065 | */ |
Paul Berry | 672fab0 | 2013-10-13 18:01:11 -0700 | [diff] [blame] | 2066 | if (is_es_prog && min_version != max_version) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 2067 | linker_error(prog, "all shaders must use same shading " |
| 2068 | "language version\n"); |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 2069 | goto done; |
| 2070 | } |
| 2071 | |
| 2072 | prog->Version = max_version; |
Paul Berry | 91c92bb | 2012-08-02 17:50:43 -0700 | [diff] [blame] | 2073 | prog->IsES = is_es_prog; |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 2074 | |
Fabian Bieler | bd85ba0 | 2013-05-24 23:26:54 +0200 | [diff] [blame] | 2075 | /* Geometry shaders have to be linked with vertex shaders. |
| 2076 | */ |
| 2077 | if (num_geom_shaders > 0 && num_vert_shaders == 0) { |
| 2078 | linker_error(prog, "Geometry shader must be linked with " |
| 2079 | "vertex shader\n"); |
| 2080 | goto done; |
| 2081 | } |
| 2082 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2083 | for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) { |
| 2084 | if (prog->_LinkedShaders[i] != NULL) |
| 2085 | ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]); |
| 2086 | |
| 2087 | prog->_LinkedShaders[i] = NULL; |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 2088 | } |
| 2089 | |
Ian Romanick | cd6764e | 2010-07-16 16:00:07 -0700 | [diff] [blame] | 2090 | /* Link all shaders for a particular stage and validate the result. |
| 2091 | */ |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 2092 | if (num_vert_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2093 | gl_shader *const sh = |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 2094 | link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list, |
| 2095 | num_vert_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2096 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2097 | if (!prog->LinkStatus) |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2098 | goto done; |
| 2099 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2100 | validate_vertex_shader_executable(prog, sh); |
| 2101 | if (!prog->LinkStatus) |
Ian Romanick | f29ff6e | 2010-10-14 17:55:17 -0700 | [diff] [blame] | 2102 | goto done; |
Paul Berry | 44b7ebe | 2013-10-23 12:55:24 -0700 | [diff] [blame] | 2103 | prog->LastClipDistanceArraySize = prog->Vert.ClipDistanceArraySize; |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2104 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2105 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX], |
| 2106 | sh); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | if (num_frag_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2110 | gl_shader *const sh = |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 2111 | link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list, |
| 2112 | num_frag_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2113 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2114 | if (!prog->LinkStatus) |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2115 | goto done; |
| 2116 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2117 | validate_fragment_shader_executable(prog, sh); |
| 2118 | if (!prog->LinkStatus) |
Ian Romanick | f29ff6e | 2010-10-14 17:55:17 -0700 | [diff] [blame] | 2119 | goto done; |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 2120 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2121 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT], |
| 2122 | sh); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 2123 | } |
| 2124 | |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 2125 | if (num_geom_shaders > 0) { |
| 2126 | gl_shader *const sh = |
| 2127 | link_intrastage_shaders(mem_ctx, ctx, prog, geom_shader_list, |
| 2128 | num_geom_shaders); |
| 2129 | |
| 2130 | if (!prog->LinkStatus) |
| 2131 | goto done; |
| 2132 | |
| 2133 | validate_geometry_shader_executable(prog, sh); |
| 2134 | if (!prog->LinkStatus) |
| 2135 | goto done; |
Paul Berry | 44b7ebe | 2013-10-23 12:55:24 -0700 | [diff] [blame] | 2136 | prog->LastClipDistanceArraySize = prog->Geom.ClipDistanceArraySize; |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 2137 | |
| 2138 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_GEOMETRY], |
| 2139 | sh); |
| 2140 | } |
| 2141 | |
Ian Romanick | 3ed850e | 2010-06-23 12:18:21 -0700 | [diff] [blame] | 2142 | /* Here begins the inter-stage linking phase. Some initial validation is |
| 2143 | * performed, then locations are assigned for uniforms, attributes, and |
| 2144 | * varyings. |
| 2145 | */ |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2146 | cross_validate_uniforms(prog); |
| 2147 | if (!prog->LinkStatus) |
| 2148 | goto done; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2149 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2150 | unsigned prev; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2151 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2152 | for (prev = 0; prev < MESA_SHADER_TYPES; prev++) { |
| 2153 | if (prog->_LinkedShaders[prev] != NULL) |
| 2154 | break; |
| 2155 | } |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2156 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2157 | /* Validate the inputs of each stage with the output of the preceding |
| 2158 | * stage. |
| 2159 | */ |
| 2160 | for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) { |
| 2161 | if (prog->_LinkedShaders[i] == NULL) |
| 2162 | continue; |
Kenneth Graunke | 3ddfccb | 2013-05-20 23:46:16 -0700 | [diff] [blame] | 2163 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2164 | validate_interstage_interface_blocks(prog, prog->_LinkedShaders[prev], |
| 2165 | prog->_LinkedShaders[i]); |
| 2166 | if (!prog->LinkStatus) |
| 2167 | goto done; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2168 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2169 | cross_validate_outputs_to_inputs(prog, |
| 2170 | prog->_LinkedShaders[prev], |
| 2171 | prog->_LinkedShaders[i]); |
| 2172 | if (!prog->LinkStatus) |
| 2173 | goto done; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 2174 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2175 | prev = i; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 2176 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2177 | |
Jordan Justen | 5ebf547 | 2013-03-10 03:20:03 -0700 | [diff] [blame] | 2178 | |
| 2179 | for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) { |
| 2180 | if (prog->_LinkedShaders[i] != NULL) |
| 2181 | lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]); |
| 2182 | } |
| 2183 | |
Eric Anholt | 3de1395 | 2012-05-04 13:08:46 -0700 | [diff] [blame] | 2184 | /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do |
| 2185 | * it before optimization because we want most of the checks to get |
| 2186 | * dropped thanks to constant propagation. |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 2187 | * |
| 2188 | * This rule also applies to GLSL ES 3.00. |
Eric Anholt | 3de1395 | 2012-05-04 13:08:46 -0700 | [diff] [blame] | 2189 | */ |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 2190 | if (max_version >= (is_es_prog ? 300 : 130)) { |
Eric Anholt | 3de1395 | 2012-05-04 13:08:46 -0700 | [diff] [blame] | 2191 | struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]; |
| 2192 | if (sh) { |
| 2193 | lower_discard_flow(sh->ir); |
| 2194 | } |
| 2195 | } |
| 2196 | |
Eric Anholt | f609cf7 | 2012-04-27 13:52:56 -0700 | [diff] [blame] | 2197 | if (!interstage_cross_validate_uniform_blocks(prog)) |
| 2198 | goto done; |
| 2199 | |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 2200 | /* Do common optimization before assigning storage for attributes, |
| 2201 | * uniforms, and varyings. Later optimization could possibly make |
| 2202 | * some of that unused. |
| 2203 | */ |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2204 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 2205 | if (prog->_LinkedShaders[i] == NULL) |
| 2206 | continue; |
| 2207 | |
Ian Romanick | 02c5ae1 | 2011-07-11 10:46:01 -0700 | [diff] [blame] | 2208 | detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir); |
| 2209 | if (!prog->LinkStatus) |
| 2210 | goto done; |
| 2211 | |
Paul Berry | 1839244 | 2012-12-04 11:11:02 -0800 | [diff] [blame] | 2212 | if (ctx->ShaderCompilerOptions[i].LowerClipDistance) { |
| 2213 | lower_clip_distance(prog->_LinkedShaders[i]); |
| 2214 | } |
Paul Berry | c06e325 | 2011-08-11 20:58:21 -0700 | [diff] [blame] | 2215 | |
Brian Paul | 7feabfe | 2012-03-20 17:43:12 -0600 | [diff] [blame] | 2216 | unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations; |
| 2217 | |
Kenneth Graunke | b765740 | 2013-04-17 17:30:22 -0700 | [diff] [blame] | 2218 | while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll, &ctx->ShaderCompilerOptions[i])) |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 2219 | ; |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 2220 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 2221 | |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 2222 | /* Mark all generic shader inputs and outputs as unpaired. */ |
| 2223 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) { |
| 2224 | link_invalidate_variable_locations( |
Ian Romanick | 63974c0 | 2013-10-04 10:46:29 -0700 | [diff] [blame] | 2225 | prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir); |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 2226 | } |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 2227 | if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) { |
| 2228 | link_invalidate_variable_locations( |
Ian Romanick | 63974c0 | 2013-10-04 10:46:29 -0700 | [diff] [blame] | 2229 | prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir); |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 2230 | } |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 2231 | if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) { |
| 2232 | link_invalidate_variable_locations( |
Ian Romanick | 63974c0 | 2013-10-04 10:46:29 -0700 | [diff] [blame] | 2233 | prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir); |
Paul Berry | 50895d4 | 2012-12-05 07:17:07 -0800 | [diff] [blame] | 2234 | } |
| 2235 | |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2236 | /* FINISHME: The value of the max_attribute_index parameter is |
| 2237 | * FINISHME: implementation dependent based on the value of |
| 2238 | * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be |
| 2239 | * FINISHME: at least 16, so hardcode 16 for now. |
| 2240 | */ |
| 2241 | if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2242 | goto done; |
| 2243 | } |
| 2244 | |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 2245 | if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) { |
Ian Romanick | d32d4f7 | 2011-06-27 17:59:58 -0700 | [diff] [blame] | 2246 | goto done; |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 2247 | } |
| 2248 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 2249 | unsigned first; |
| 2250 | for (first = 0; first < MESA_SHADER_TYPES; first++) { |
| 2251 | if (prog->_LinkedShaders[first] != NULL) |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2252 | break; |
| 2253 | } |
| 2254 | |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2255 | if (num_tfeedback_decls != 0) { |
| 2256 | /* From GL_EXT_transform_feedback: |
| 2257 | * A program will fail to link if: |
| 2258 | * |
| 2259 | * * the <count> specified by TransformFeedbackVaryingsEXT is |
| 2260 | * non-zero, but the program object has no vertex or geometry |
| 2261 | * shader; |
| 2262 | */ |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 2263 | if (first == MESA_SHADER_FRAGMENT) { |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2264 | linker_error(prog, "Transform feedback varyings specified, but " |
| 2265 | "no vertex or geometry shader is present."); |
| 2266 | goto done; |
| 2267 | } |
| 2268 | |
| 2269 | tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl, |
| 2270 | prog->TransformFeedback.NumVarying); |
Paul Berry | 456279b | 2011-12-26 19:39:25 -0800 | [diff] [blame] | 2271 | if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls, |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2272 | prog->TransformFeedback.VaryingNames, |
| 2273 | tfeedback_decls)) |
| 2274 | goto done; |
| 2275 | } |
| 2276 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 2277 | /* Linking the stages in the opposite order (from fragment to vertex) |
| 2278 | * ensures that inter-shader outputs written to in an earlier stage are |
| 2279 | * eliminated if they are (transitively) not used in a later stage. |
| 2280 | */ |
| 2281 | int last, next; |
| 2282 | for (last = MESA_SHADER_TYPES-1; last >= 0; last--) { |
| 2283 | if (prog->_LinkedShaders[last] != NULL) |
| 2284 | break; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 2285 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 2286 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 2287 | if (last >= 0 && last < MESA_SHADER_FRAGMENT) { |
| 2288 | gl_shader *const sh = prog->_LinkedShaders[last]; |
| 2289 | |
| 2290 | if (num_tfeedback_decls != 0) { |
| 2291 | /* There was no fragment shader, but we still have to assign varying |
| 2292 | * locations for use by transform feedback. |
| 2293 | */ |
| 2294 | if (!assign_varying_locations(ctx, mem_ctx, prog, |
| 2295 | sh, NULL, |
Paul Berry | 3b0cf70 | 2013-04-10 06:48:42 -0700 | [diff] [blame] | 2296 | num_tfeedback_decls, tfeedback_decls, |
| 2297 | 0)) |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 2298 | goto done; |
| 2299 | } |
| 2300 | |
Marek Olšák | d13003f | 2013-08-09 22:34:45 +0200 | [diff] [blame] | 2301 | do_dead_builtin_varyings(ctx, sh, NULL, |
Marek Olšák | b3d8b4c | 2013-06-12 13:23:48 +0200 | [diff] [blame] | 2302 | num_tfeedback_decls, tfeedback_decls); |
| 2303 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 2304 | demote_shader_inputs_and_outputs(sh, ir_var_shader_out); |
| 2305 | |
| 2306 | /* Eliminate code that is now dead due to unused outputs being demoted. |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2307 | */ |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 2308 | while (do_dead_code(sh->ir, false)) |
| 2309 | ; |
| 2310 | } |
| 2311 | else if (first == MESA_SHADER_FRAGMENT) { |
Marek Olšák | b3d8b4c | 2013-06-12 13:23:48 +0200 | [diff] [blame] | 2312 | /* If the program only contains a fragment shader... |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 2313 | */ |
| 2314 | gl_shader *const sh = prog->_LinkedShaders[first]; |
| 2315 | |
Marek Olšák | d13003f | 2013-08-09 22:34:45 +0200 | [diff] [blame] | 2316 | do_dead_builtin_varyings(ctx, NULL, sh, |
Marek Olšák | b3d8b4c | 2013-06-12 13:23:48 +0200 | [diff] [blame] | 2317 | num_tfeedback_decls, tfeedback_decls); |
| 2318 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 2319 | demote_shader_inputs_and_outputs(sh, ir_var_shader_in); |
| 2320 | |
| 2321 | while (do_dead_code(sh->ir, false)) |
| 2322 | ; |
| 2323 | } |
| 2324 | |
| 2325 | next = last; |
| 2326 | for (int i = next - 1; i >= 0; i--) { |
| 2327 | if (prog->_LinkedShaders[i] == NULL) |
| 2328 | continue; |
| 2329 | |
| 2330 | gl_shader *const sh_i = prog->_LinkedShaders[i]; |
| 2331 | gl_shader *const sh_next = prog->_LinkedShaders[next]; |
Paul Berry | 3b0cf70 | 2013-04-10 06:48:42 -0700 | [diff] [blame] | 2332 | unsigned gs_input_vertices = |
| 2333 | next == MESA_SHADER_GEOMETRY ? prog->Geom.VerticesIn : 0; |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 2334 | |
| 2335 | if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next, |
| 2336 | next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0, |
Paul Berry | 3b0cf70 | 2013-04-10 06:48:42 -0700 | [diff] [blame] | 2337 | tfeedback_decls, gs_input_vertices)) |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2338 | goto done; |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 2339 | |
Marek Olšák | d13003f | 2013-08-09 22:34:45 +0200 | [diff] [blame] | 2340 | do_dead_builtin_varyings(ctx, sh_i, sh_next, |
Marek Olšák | b3d8b4c | 2013-06-12 13:23:48 +0200 | [diff] [blame] | 2341 | next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0, |
| 2342 | tfeedback_decls); |
| 2343 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 2344 | demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out); |
| 2345 | demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in); |
| 2346 | |
| 2347 | /* Eliminate code that is now dead due to unused outputs being demoted. |
| 2348 | */ |
| 2349 | while (do_dead_code(sh_i->ir, false)) |
| 2350 | ; |
| 2351 | while (do_dead_code(sh_next->ir, false)) |
| 2352 | ; |
| 2353 | |
Marek Olšák | 3c55582 | 2013-06-13 03:17:22 +0200 | [diff] [blame] | 2354 | /* This must be done after all dead varyings are eliminated. */ |
Ian Romanick | 42305fb | 2013-09-10 12:00:34 -0500 | [diff] [blame] | 2355 | if (!check_against_output_limit(ctx, prog, sh_i)) |
| 2356 | goto done; |
| 2357 | if (!check_against_input_limit(ctx, prog, sh_next)) |
Marek Olšák | 3c55582 | 2013-06-13 03:17:22 +0200 | [diff] [blame] | 2358 | goto done; |
| 2359 | |
Marek Olšák | 284d954 | 2013-06-12 02:18:09 +0200 | [diff] [blame] | 2360 | next = i; |
Paul Berry | 871ddb9 | 2011-11-05 11:17:32 -0700 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls)) |
| 2364 | goto done; |
| 2365 | |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 2366 | update_array_sizes(prog); |
Ian Romanick | 7199096 | 2011-10-18 16:01:49 -0700 | [diff] [blame] | 2367 | link_assign_uniform_locations(prog); |
Marek Olšák | ec174a4 | 2011-11-18 15:00:10 +0100 | [diff] [blame] | 2368 | store_fragdepth_layout(prog); |
Ian Romanick | 960d722 | 2011-10-21 11:21:02 -0700 | [diff] [blame] | 2369 | |
Paul Berry | b95d237 | 2013-07-27 11:08:31 -0700 | [diff] [blame] | 2370 | check_resources(ctx, prog); |
| 2371 | if (!prog->LinkStatus) |
Ian Romanick | 92f8159 | 2011-11-08 12:37:19 -0800 | [diff] [blame] | 2372 | goto done; |
| 2373 | |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 2374 | /* OpenGL ES requires that a vertex shader and a fragment shader both be |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 2375 | * present in a linked program. By checking prog->IsES, we also |
| 2376 | * catch the GL_ARB_ES2_compatibility case. |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 2377 | */ |
Eric Anholt | 57f7978 | 2011-07-22 12:57:47 -0700 | [diff] [blame] | 2378 | if (!prog->InternalSeparateShader && |
Paul Berry | 15ba2a5 | 2012-08-02 17:51:02 -0700 | [diff] [blame] | 2379 | (ctx->API == API_OPENGLES2 || prog->IsES)) { |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 2380 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 2381 | linker_error(prog, "program lacks a vertex shader\n"); |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 2382 | } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { |
Ian Romanick | 586e741 | 2011-07-28 14:04:09 -0700 | [diff] [blame] | 2383 | linker_error(prog, "program lacks a fragment shader\n"); |
Ian Romanick | ce9171f | 2011-02-03 17:10:14 -0800 | [diff] [blame] | 2384 | } |
| 2385 | } |
| 2386 | |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 2387 | /* FINISHME: Assign fragment shader output locations. */ |
| 2388 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2389 | done: |
| 2390 | free(vert_shader_list); |
Paul Berry | 844bd71 | 2013-07-30 22:38:43 -0700 | [diff] [blame] | 2391 | free(frag_shader_list); |
Bryan Cain | 2548092 | 2013-02-15 09:46:50 -0600 | [diff] [blame] | 2392 | free(geom_shader_list); |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 2393 | |
| 2394 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 2395 | if (prog->_LinkedShaders[i] == NULL) |
| 2396 | continue; |
| 2397 | |
| 2398 | /* Retain any live IR, but trash the rest. */ |
| 2399 | reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir); |
Ian Romanick | 7bbcc0b | 2011-09-30 14:21:10 -0700 | [diff] [blame] | 2400 | |
| 2401 | /* The symbol table in the linked shaders may contain references to |
| 2402 | * variables that were removed (e.g., unused uniforms). Since it may |
| 2403 | * contain junk, there is no possible valid use. Delete it and set the |
| 2404 | * pointer to NULL. |
| 2405 | */ |
| 2406 | delete prog->_LinkedShaders[i]->symbols; |
| 2407 | prog->_LinkedShaders[i]->symbols = NULL; |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 2408 | } |
| 2409 | |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 2410 | ralloc_free(mem_ctx); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 2411 | } |