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