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