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 | */ |
| 66 | #include <cstdlib> |
| 67 | #include <cstdio> |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 68 | #include <cstdarg> |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 69 | #include <climits> |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 70 | |
| 71 | extern "C" { |
| 72 | #include <talloc.h> |
| 73 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 74 | |
Chia-I Wu | bfd7c9a | 2010-08-23 17:51:42 +0800 | [diff] [blame] | 75 | #include "main/core.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 76 | #include "glsl_symbol_table.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 77 | #include "ir.h" |
| 78 | #include "program.h" |
Aras Pranckevicius | 3174715 | 2010-07-29 12:40:49 +0300 | [diff] [blame] | 79 | #include "program/hash_table.h" |
Ian Romanick | 8fe8a81 | 2010-07-13 17:36:13 -0700 | [diff] [blame] | 80 | #include "linker.h" |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 81 | #include "ir_optimization.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 82 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 83 | extern "C" { |
| 84 | #include "main/shaderobj.h" |
| 85 | } |
| 86 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Visitor that determines whether or not a variable is ever written. |
| 89 | */ |
| 90 | class find_assignment_visitor : public ir_hierarchical_visitor { |
| 91 | public: |
| 92 | find_assignment_visitor(const char *name) |
| 93 | : name(name), found(false) |
| 94 | { |
| 95 | /* empty */ |
| 96 | } |
| 97 | |
| 98 | virtual ir_visitor_status visit_enter(ir_assignment *ir) |
| 99 | { |
| 100 | ir_variable *const var = ir->lhs->variable_referenced(); |
| 101 | |
| 102 | if (strcmp(name, var->name) == 0) { |
| 103 | found = true; |
| 104 | return visit_stop; |
| 105 | } |
| 106 | |
| 107 | return visit_continue_with_parent; |
| 108 | } |
| 109 | |
Eric Anholt | 18a6023 | 2010-08-23 11:29:25 -0700 | [diff] [blame] | 110 | virtual ir_visitor_status visit_enter(ir_call *ir) |
| 111 | { |
| 112 | exec_list_iterator sig_iter = ir->get_callee()->parameters.iterator(); |
| 113 | foreach_iter(exec_list_iterator, iter, *ir) { |
| 114 | ir_rvalue *param_rval = (ir_rvalue *)iter.get(); |
| 115 | ir_variable *sig_param = (ir_variable *)sig_iter.get(); |
| 116 | |
| 117 | if (sig_param->mode == ir_var_out || |
| 118 | sig_param->mode == ir_var_inout) { |
| 119 | ir_variable *var = param_rval->variable_referenced(); |
| 120 | if (var && strcmp(name, var->name) == 0) { |
| 121 | found = true; |
| 122 | return visit_stop; |
| 123 | } |
| 124 | } |
| 125 | sig_iter.next(); |
| 126 | } |
| 127 | |
| 128 | return visit_continue_with_parent; |
| 129 | } |
| 130 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 131 | bool variable_found() |
| 132 | { |
| 133 | return found; |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | const char *name; /**< Find writes to a variable with this name. */ |
| 138 | bool found; /**< Was a write to the variable found? */ |
| 139 | }; |
| 140 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 141 | |
Ian Romanick | c33e78f | 2010-08-13 12:30:41 -0700 | [diff] [blame] | 142 | /** |
| 143 | * Visitor that determines whether or not a variable is ever read. |
| 144 | */ |
| 145 | class find_deref_visitor : public ir_hierarchical_visitor { |
| 146 | public: |
| 147 | find_deref_visitor(const char *name) |
| 148 | : name(name), found(false) |
| 149 | { |
| 150 | /* empty */ |
| 151 | } |
| 152 | |
| 153 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 154 | { |
| 155 | if (strcmp(this->name, ir->var->name) == 0) { |
| 156 | this->found = true; |
| 157 | return visit_stop; |
| 158 | } |
| 159 | |
| 160 | return visit_continue; |
| 161 | } |
| 162 | |
| 163 | bool variable_found() const |
| 164 | { |
| 165 | return this->found; |
| 166 | } |
| 167 | |
| 168 | private: |
| 169 | const char *name; /**< Find writes to a variable with this name. */ |
| 170 | bool found; /**< Was a write to the variable found? */ |
| 171 | }; |
| 172 | |
| 173 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 174 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 175 | linker_error_printf(gl_shader_program *prog, const char *fmt, ...) |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 176 | { |
| 177 | va_list ap; |
| 178 | |
| 179 | prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: "); |
| 180 | va_start(ap, fmt); |
| 181 | prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap); |
| 182 | va_end(ap); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | void |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 187 | invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode, |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 188 | int generic_base) |
| 189 | { |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 190 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 191 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 192 | |
| 193 | if ((var == NULL) || (var->mode != (unsigned) mode)) |
| 194 | continue; |
| 195 | |
| 196 | /* Only assign locations for generic attributes / varyings / etc. |
| 197 | */ |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 198 | if ((var->location >= generic_base) && !var->explicit_location) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 199 | var->location = -1; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 204 | /** |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 205 | * Determine the number of attribute slots required for a particular type |
| 206 | * |
| 207 | * This code is here because it implements the language rules of a specific |
| 208 | * GLSL version. Since it's a property of the language and not a property of |
| 209 | * types in general, it doesn't really belong in glsl_type. |
| 210 | */ |
| 211 | unsigned |
| 212 | count_attribute_slots(const glsl_type *t) |
| 213 | { |
| 214 | /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: |
| 215 | * |
| 216 | * "A scalar input counts the same amount against this limit as a vec4, |
| 217 | * so applications may want to consider packing groups of four |
| 218 | * unrelated float inputs together into a vector to better utilize the |
| 219 | * capabilities of the underlying hardware. A matrix input will use up |
| 220 | * multiple locations. The number of locations used will equal the |
| 221 | * number of columns in the matrix." |
| 222 | * |
| 223 | * The spec does not explicitly say how arrays are counted. However, it |
| 224 | * should be safe to assume the total number of slots consumed by an array |
| 225 | * is the number of entries in the array multiplied by the number of slots |
| 226 | * consumed by a single element of the array. |
| 227 | */ |
| 228 | |
| 229 | if (t->is_array()) |
| 230 | return t->array_size() * count_attribute_slots(t->element_type()); |
| 231 | |
| 232 | if (t->is_matrix()) |
| 233 | return t->matrix_columns; |
| 234 | |
| 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /** |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 240 | * Verify that a vertex shader executable meets all semantic requirements |
| 241 | * |
| 242 | * \param shader Vertex shader executable to be verified |
| 243 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 244 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 245 | validate_vertex_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 246 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 247 | { |
| 248 | if (shader == NULL) |
| 249 | return true; |
| 250 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 251 | find_assignment_visitor find("gl_Position"); |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 252 | find.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 253 | if (!find.variable_found()) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 254 | linker_error_printf(prog, |
| 255 | "vertex shader does not write to `gl_Position'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 256 | return false; |
| 257 | } |
| 258 | |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 263 | /** |
| 264 | * Verify that a fragment shader executable meets all semantic requirements |
| 265 | * |
| 266 | * \param shader Fragment shader executable to be verified |
| 267 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 268 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 269 | validate_fragment_shader_executable(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 270 | struct gl_shader *shader) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 271 | { |
| 272 | if (shader == NULL) |
| 273 | return true; |
| 274 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 275 | find_assignment_visitor frag_color("gl_FragColor"); |
| 276 | find_assignment_visitor frag_data("gl_FragData"); |
| 277 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 278 | frag_color.run(shader->ir); |
| 279 | frag_data.run(shader->ir); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 280 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 281 | if (frag_color.variable_found() && frag_data.variable_found()) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 282 | linker_error_printf(prog, "fragment shader writes to both " |
| 283 | "`gl_FragColor' and `gl_FragData'\n"); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 291 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 292 | * Generate a string describing the mode of a variable |
| 293 | */ |
| 294 | static const char * |
| 295 | mode_string(const ir_variable *var) |
| 296 | { |
| 297 | switch (var->mode) { |
| 298 | case ir_var_auto: |
| 299 | return (var->read_only) ? "global constant" : "global variable"; |
| 300 | |
| 301 | case ir_var_uniform: return "uniform"; |
| 302 | case ir_var_in: return "shader input"; |
| 303 | case ir_var_out: return "shader output"; |
| 304 | case ir_var_inout: return "shader inout"; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 305 | |
| 306 | case ir_var_temporary: |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 307 | default: |
| 308 | assert(!"Should not get here."); |
| 309 | return "invalid variable"; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /** |
| 315 | * Perform validation of global variables used across multiple shaders |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 316 | */ |
| 317 | bool |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 318 | cross_validate_globals(struct gl_shader_program *prog, |
| 319 | struct gl_shader **shader_list, |
| 320 | unsigned num_shaders, |
| 321 | bool uniforms_only) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 322 | { |
| 323 | /* Examine all of the uniforms in all of the shaders and cross validate |
| 324 | * them. |
| 325 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 326 | glsl_symbol_table variables; |
| 327 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 328 | if (shader_list[i] == NULL) |
| 329 | continue; |
| 330 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 331 | foreach_list(node, shader_list[i]->ir) { |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 332 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 333 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 334 | if (var == NULL) |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 335 | continue; |
| 336 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 337 | if (uniforms_only && (var->mode != ir_var_uniform)) |
| 338 | continue; |
| 339 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 340 | /* Don't cross validate temporaries that are at global scope. These |
| 341 | * will eventually get pulled into the shaders 'main'. |
| 342 | */ |
| 343 | if (var->mode == ir_var_temporary) |
| 344 | continue; |
| 345 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 346 | /* If a global with this name has already been seen, verify that the |
| 347 | * new instance has the same type. In addition, if the globals have |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 348 | * initializers, the values of the initializers must be the same. |
| 349 | */ |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 350 | ir_variable *const existing = variables.get_variable(var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 351 | if (existing != NULL) { |
| 352 | if (var->type != existing->type) { |
Ian Romanick | a2711d6 | 2010-08-29 22:07:49 -0700 | [diff] [blame] | 353 | /* Consider the types to be "the same" if both types are arrays |
| 354 | * of the same type and one of the arrays is implicitly sized. |
| 355 | * In addition, set the type of the linked variable to the |
| 356 | * explicitly sized array. |
| 357 | */ |
| 358 | if (var->type->is_array() |
| 359 | && existing->type->is_array() |
| 360 | && (var->type->fields.array == existing->type->fields.array) |
| 361 | && ((var->type->length == 0) |
| 362 | || (existing->type->length == 0))) { |
| 363 | if (existing->type->length == 0) |
| 364 | existing->type = var->type; |
| 365 | } else { |
| 366 | linker_error_printf(prog, "%s `%s' declared as type " |
| 367 | "`%s' and type `%s'\n", |
| 368 | mode_string(var), |
| 369 | var->name, var->type->name, |
| 370 | existing->type->name); |
| 371 | return false; |
| 372 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 375 | if (var->explicit_location) { |
| 376 | if (existing->explicit_location |
| 377 | && (var->location != existing->location)) { |
| 378 | linker_error_printf(prog, "explicit locations for %s " |
| 379 | "`%s' have differing values\n", |
| 380 | mode_string(var), var->name); |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | existing->location = var->location; |
| 385 | existing->explicit_location = true; |
| 386 | } |
| 387 | |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 388 | /* FINISHME: Handle non-constant initializers. |
| 389 | */ |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 390 | if (var->constant_value != NULL) { |
| 391 | if (existing->constant_value != NULL) { |
| 392 | if (!var->constant_value->has_value(existing->constant_value)) { |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 393 | linker_error_printf(prog, "initializers for %s " |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 394 | "`%s' have differing values\n", |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 395 | mode_string(var), var->name); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 396 | return false; |
| 397 | } |
| 398 | } else |
| 399 | /* If the first-seen instance of a particular uniform did not |
| 400 | * have an initializer but a later instance does, copy the |
| 401 | * initializer to the version stored in the symbol table. |
| 402 | */ |
Ian Romanick | de415b7 | 2010-07-14 13:22:12 -0700 | [diff] [blame] | 403 | /* FINISHME: This is wrong. The constant_value field should |
| 404 | * FINISHME: not be modified! Imagine a case where a shader |
| 405 | * FINISHME: without an initializer is linked in two different |
| 406 | * FINISHME: programs with shaders that have differing |
| 407 | * FINISHME: initializers. Linking with the first will |
| 408 | * FINISHME: modify the shader, and linking with the second |
| 409 | * FINISHME: will fail. |
| 410 | */ |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 411 | existing->constant_value = |
| 412 | var->constant_value->clone(talloc_parent(existing), NULL); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 413 | } |
Chad Versace | 7528f14 | 2010-11-17 14:34:38 -0800 | [diff] [blame^] | 414 | |
| 415 | if (existing->invariant != var->invariant) { |
| 416 | linker_error_printf(prog, "declarations for %s `%s' have " |
| 417 | "mismatching invariant qualifiers\n", |
| 418 | mode_string(var), var->name); |
| 419 | return false; |
| 420 | } |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 421 | } else |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 422 | variables.add_variable(var); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 430 | /** |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 431 | * Perform validation of uniforms used across multiple shader stages |
| 432 | */ |
| 433 | bool |
| 434 | cross_validate_uniforms(struct gl_shader_program *prog) |
| 435 | { |
| 436 | return cross_validate_globals(prog, prog->_LinkedShaders, |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 437 | MESA_SHADER_TYPES, true); |
Ian Romanick | e2e5d0d | 2010-06-29 18:47:11 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | |
| 441 | /** |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 442 | * Validate that outputs from one stage match inputs of another |
| 443 | */ |
| 444 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 445 | cross_validate_outputs_to_inputs(struct gl_shader_program *prog, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 446 | gl_shader *producer, gl_shader *consumer) |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 447 | { |
| 448 | glsl_symbol_table parameters; |
| 449 | /* FINISHME: Figure these out dynamically. */ |
| 450 | const char *const producer_stage = "vertex"; |
| 451 | const char *const consumer_stage = "fragment"; |
| 452 | |
| 453 | /* Find all shader outputs in the "producer" stage. |
| 454 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 455 | foreach_list(node, producer->ir) { |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 456 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 457 | |
| 458 | /* FINISHME: For geometry shaders, this should also look for inout |
| 459 | * FINISHME: variables. |
| 460 | */ |
| 461 | if ((var == NULL) || (var->mode != ir_var_out)) |
| 462 | continue; |
| 463 | |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 464 | parameters.add_variable(var); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | |
| 468 | /* Find all shader inputs in the "consumer" stage. Any variables that have |
| 469 | * matching outputs already in the symbol table must have the same type and |
| 470 | * qualifiers. |
| 471 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 472 | foreach_list(node, consumer->ir) { |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 473 | ir_variable *const input = ((ir_instruction *) node)->as_variable(); |
| 474 | |
| 475 | /* FINISHME: For geometry shaders, this should also look for inout |
| 476 | * FINISHME: variables. |
| 477 | */ |
| 478 | if ((input == NULL) || (input->mode != ir_var_in)) |
| 479 | continue; |
| 480 | |
| 481 | ir_variable *const output = parameters.get_variable(input->name); |
| 482 | if (output != NULL) { |
| 483 | /* Check that the types match between stages. |
| 484 | */ |
| 485 | if (input->type != output->type) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 486 | linker_error_printf(prog, |
Brian Paul | 2b95525 | 2010-09-21 14:56:58 -0600 | [diff] [blame] | 487 | "%s shader output `%s' declared as " |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 488 | "type `%s', but %s shader input declared " |
| 489 | "as type `%s'\n", |
| 490 | producer_stage, output->name, |
| 491 | output->type->name, |
| 492 | consumer_stage, input->type->name); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 493 | return false; |
| 494 | } |
| 495 | |
| 496 | /* Check that all of the qualifiers match between stages. |
| 497 | */ |
| 498 | if (input->centroid != output->centroid) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 499 | linker_error_printf(prog, |
| 500 | "%s shader output `%s' %s centroid qualifier, " |
| 501 | "but %s shader input %s centroid qualifier\n", |
| 502 | producer_stage, |
| 503 | output->name, |
| 504 | (output->centroid) ? "has" : "lacks", |
| 505 | consumer_stage, |
| 506 | (input->centroid) ? "has" : "lacks"); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 507 | return false; |
| 508 | } |
| 509 | |
| 510 | if (input->invariant != output->invariant) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 511 | linker_error_printf(prog, |
| 512 | "%s shader output `%s' %s invariant qualifier, " |
| 513 | "but %s shader input %s invariant qualifier\n", |
| 514 | producer_stage, |
| 515 | output->name, |
| 516 | (output->invariant) ? "has" : "lacks", |
| 517 | consumer_stage, |
| 518 | (input->invariant) ? "has" : "lacks"); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 519 | return false; |
| 520 | } |
| 521 | |
| 522 | if (input->interpolation != output->interpolation) { |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 523 | linker_error_printf(prog, |
| 524 | "%s shader output `%s' specifies %s " |
| 525 | "interpolation qualifier, " |
| 526 | "but %s shader input specifies %s " |
| 527 | "interpolation qualifier\n", |
| 528 | producer_stage, |
| 529 | output->name, |
| 530 | output->interpolation_string(), |
| 531 | consumer_stage, |
| 532 | input->interpolation_string()); |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 533 | return false; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | return true; |
| 539 | } |
| 540 | |
| 541 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 542 | /** |
| 543 | * Populates a shaders symbol table with all global declarations |
| 544 | */ |
| 545 | static void |
| 546 | populate_symbol_table(gl_shader *sh) |
| 547 | { |
| 548 | sh->symbols = new(sh) glsl_symbol_table; |
| 549 | |
| 550 | foreach_list(node, sh->ir) { |
| 551 | ir_instruction *const inst = (ir_instruction *) node; |
| 552 | ir_variable *var; |
| 553 | ir_function *func; |
| 554 | |
| 555 | if ((func = inst->as_function()) != NULL) { |
Eric Anholt | e8f5ebf | 2010-11-05 06:08:45 -0700 | [diff] [blame] | 556 | sh->symbols->add_function(func); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 557 | } else if ((var = inst->as_variable()) != NULL) { |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 558 | sh->symbols->add_variable(var); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | |
| 564 | /** |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 565 | * Remap variables referenced in an instruction tree |
| 566 | * |
| 567 | * This is used when instruction trees are cloned from one shader and placed in |
| 568 | * another. These trees will contain references to \c ir_variable nodes that |
| 569 | * do not exist in the target shader. This function finds these \c ir_variable |
| 570 | * references and replaces the references with matching variables in the target |
| 571 | * shader. |
| 572 | * |
| 573 | * If there is no matching variable in the target shader, a clone of the |
| 574 | * \c ir_variable is made and added to the target shader. The new variable is |
| 575 | * added to \b both the instruction stream and the symbol table. |
| 576 | * |
| 577 | * \param inst IR tree that is to be processed. |
| 578 | * \param symbols Symbol table containing global scope symbols in the |
| 579 | * linked shader. |
| 580 | * \param instructions Instruction stream where new variable declarations |
| 581 | * should be added. |
| 582 | */ |
| 583 | void |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 584 | remap_variables(ir_instruction *inst, struct gl_shader *target, |
| 585 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 586 | { |
| 587 | class remap_visitor : public ir_hierarchical_visitor { |
| 588 | public: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 589 | remap_visitor(struct gl_shader *target, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 590 | hash_table *temps) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 591 | { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 592 | this->target = target; |
| 593 | this->symbols = target->symbols; |
| 594 | this->instructions = target->ir; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 595 | this->temps = temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 599 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 600 | if (ir->var->mode == ir_var_temporary) { |
| 601 | ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var); |
| 602 | |
| 603 | assert(var != NULL); |
| 604 | ir->var = var; |
| 605 | return visit_continue; |
| 606 | } |
| 607 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 608 | ir_variable *const existing = |
| 609 | this->symbols->get_variable(ir->var->name); |
| 610 | if (existing != NULL) |
| 611 | ir->var = existing; |
| 612 | else { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 613 | ir_variable *copy = ir->var->clone(this->target, NULL); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 614 | |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 615 | this->symbols->add_variable(copy); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 616 | this->instructions->push_head(copy); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 617 | ir->var = copy; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | return visit_continue; |
| 621 | } |
| 622 | |
| 623 | private: |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 624 | struct gl_shader *target; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 625 | glsl_symbol_table *symbols; |
| 626 | exec_list *instructions; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 627 | hash_table *temps; |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 628 | }; |
| 629 | |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 630 | remap_visitor v(target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 631 | |
| 632 | inst->accept(&v); |
| 633 | } |
| 634 | |
| 635 | |
| 636 | /** |
| 637 | * Move non-declarations from one instruction stream to another |
| 638 | * |
| 639 | * 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] | 640 | * 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] | 641 | * pointer) for \c last and \c false for \c make_copies on the first |
| 642 | * call. Successive calls pass the return value of the previous call for |
| 643 | * \c last and \c true for \c make_copies. |
| 644 | * |
| 645 | * \param instructions Source instruction stream |
| 646 | * \param last Instruction after which new instructions should be |
| 647 | * inserted in the target instruction stream |
| 648 | * \param make_copies Flag selecting whether instructions in \c instructions |
| 649 | * should be copied (via \c ir_instruction::clone) into the |
| 650 | * target list or moved. |
| 651 | * |
| 652 | * \return |
| 653 | * The new "last" instruction in the target instruction stream. This pointer |
| 654 | * is suitable for use as the \c last parameter of a later call to this |
| 655 | * function. |
| 656 | */ |
| 657 | exec_node * |
| 658 | move_non_declarations(exec_list *instructions, exec_node *last, |
| 659 | bool make_copies, gl_shader *target) |
| 660 | { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 661 | hash_table *temps = NULL; |
| 662 | |
| 663 | if (make_copies) |
| 664 | temps = hash_table_ctor(0, hash_table_pointer_hash, |
| 665 | hash_table_pointer_compare); |
| 666 | |
Ian Romanick | 303c99f | 2010-07-19 12:34:56 -0700 | [diff] [blame] | 667 | foreach_list_safe(node, instructions) { |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 668 | ir_instruction *inst = (ir_instruction *) node; |
| 669 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 670 | if (inst->as_function()) |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 671 | continue; |
| 672 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 673 | ir_variable *var = inst->as_variable(); |
| 674 | if ((var != NULL) && (var->mode != ir_var_temporary)) |
| 675 | continue; |
| 676 | |
| 677 | assert(inst->as_assignment() |
| 678 | || ((var != NULL) && (var->mode == ir_var_temporary))); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 679 | |
| 680 | if (make_copies) { |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 681 | inst = inst->clone(target, NULL); |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 682 | |
| 683 | if (var != NULL) |
| 684 | hash_table_insert(temps, inst, var); |
| 685 | else |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 686 | remap_variables(inst, target, temps); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 687 | } else { |
| 688 | inst->remove(); |
| 689 | } |
| 690 | |
| 691 | last->insert_after(inst); |
| 692 | last = inst; |
| 693 | } |
| 694 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 695 | if (make_copies) |
| 696 | hash_table_dtor(temps); |
| 697 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 698 | return last; |
| 699 | } |
| 700 | |
| 701 | /** |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 702 | * Get the function signature for main from a shader |
| 703 | */ |
| 704 | static ir_function_signature * |
| 705 | get_main_function_signature(gl_shader *sh) |
| 706 | { |
| 707 | ir_function *const f = sh->symbols->get_function("main"); |
| 708 | if (f != NULL) { |
| 709 | exec_list void_parameters; |
| 710 | |
| 711 | /* Look for the 'void main()' signature and ensure that it's defined. |
| 712 | * This keeps the linker from accidentally pick a shader that just |
| 713 | * contains a prototype for main. |
| 714 | * |
| 715 | * We don't have to check for multiple definitions of main (in multiple |
| 716 | * shaders) because that would have already been caught above. |
| 717 | */ |
| 718 | ir_function_signature *sig = f->matching_signature(&void_parameters); |
| 719 | if ((sig != NULL) && sig->is_defined) { |
| 720 | return sig; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | return NULL; |
| 725 | } |
| 726 | |
| 727 | |
| 728 | /** |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 729 | * Combine a group of shaders for a single stage to generate a linked shader |
| 730 | * |
| 731 | * \note |
| 732 | * If this function is supplied a single shader, it is cloned, and the new |
| 733 | * shader is returned. |
| 734 | */ |
| 735 | static struct gl_shader * |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 736 | link_intrastage_shaders(void *mem_ctx, |
| 737 | struct gl_context *ctx, |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 738 | struct gl_shader_program *prog, |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 739 | struct gl_shader **shader_list, |
| 740 | unsigned num_shaders) |
| 741 | { |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 742 | /* Check that global variables defined in multiple shaders are consistent. |
| 743 | */ |
| 744 | if (!cross_validate_globals(prog, shader_list, num_shaders, false)) |
| 745 | return NULL; |
| 746 | |
| 747 | /* Check that there is only a single definition of each function signature |
| 748 | * across all shaders. |
| 749 | */ |
| 750 | for (unsigned i = 0; i < (num_shaders - 1); i++) { |
| 751 | foreach_list(node, shader_list[i]->ir) { |
| 752 | ir_function *const f = ((ir_instruction *) node)->as_function(); |
| 753 | |
| 754 | if (f == NULL) |
| 755 | continue; |
| 756 | |
| 757 | for (unsigned j = i + 1; j < num_shaders; j++) { |
| 758 | ir_function *const other = |
| 759 | shader_list[j]->symbols->get_function(f->name); |
| 760 | |
| 761 | /* If the other shader has no function (and therefore no function |
| 762 | * signatures) with the same name, skip to the next shader. |
| 763 | */ |
| 764 | if (other == NULL) |
| 765 | continue; |
| 766 | |
| 767 | foreach_iter (exec_list_iterator, iter, *f) { |
| 768 | ir_function_signature *sig = |
| 769 | (ir_function_signature *) iter.get(); |
| 770 | |
Kenneth Graunke | f412fac | 2010-09-05 01:48:11 -0700 | [diff] [blame] | 771 | if (!sig->is_defined || sig->is_builtin) |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 772 | continue; |
| 773 | |
| 774 | ir_function_signature *other_sig = |
| 775 | other->exact_matching_signature(& sig->parameters); |
| 776 | |
| 777 | if ((other_sig != NULL) && other_sig->is_defined |
Kenneth Graunke | f412fac | 2010-09-05 01:48:11 -0700 | [diff] [blame] | 778 | && !other_sig->is_builtin) { |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 779 | linker_error_printf(prog, |
| 780 | "function `%s' is multiply defined", |
| 781 | f->name); |
| 782 | return NULL; |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /* Find the shader that defines main, and make a clone of it. |
| 790 | * |
| 791 | * Starting with the clone, search for undefined references. If one is |
| 792 | * found, find the shader that defines it. Clone the reference and add |
| 793 | * it to the shader. Repeat until there are no undefined references or |
| 794 | * until a reference cannot be resolved. |
| 795 | */ |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 796 | gl_shader *main = NULL; |
| 797 | for (unsigned i = 0; i < num_shaders; i++) { |
| 798 | if (get_main_function_signature(shader_list[i]) != NULL) { |
| 799 | main = shader_list[i]; |
| 800 | break; |
| 801 | } |
| 802 | } |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 803 | |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 804 | if (main == NULL) { |
| 805 | linker_error_printf(prog, "%s shader lacks `main'\n", |
| 806 | (shader_list[0]->Type == GL_VERTEX_SHADER) |
| 807 | ? "vertex" : "fragment"); |
| 808 | return NULL; |
| 809 | } |
| 810 | |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 811 | gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 812 | linked->ir = new(linked) exec_list; |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 813 | clone_ir_list(mem_ctx, linked->ir, main->ir); |
Ian Romanick | 15ce87e | 2010-07-09 15:28:22 -0700 | [diff] [blame] | 814 | |
| 815 | populate_symbol_table(linked); |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 816 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 817 | /* The a pointer to the main function in the final linked shader (i.e., the |
| 818 | * copy of the original shader that contained the main function). |
| 819 | */ |
| 820 | ir_function_signature *const main_sig = get_main_function_signature(linked); |
| 821 | |
| 822 | /* Move any instructions other than variable declarations or function |
| 823 | * declarations into main. |
| 824 | */ |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 825 | exec_node *insertion_point = |
| 826 | move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false, |
| 827 | linked); |
| 828 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 829 | for (unsigned i = 0; i < num_shaders; i++) { |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 830 | if (shader_list[i] == main) |
| 831 | continue; |
| 832 | |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 833 | insertion_point = move_non_declarations(shader_list[i]->ir, |
Ian Romanick | 9303e35 | 2010-07-19 12:33:54 -0700 | [diff] [blame] | 834 | insertion_point, true, linked); |
Ian Romanick | 31a9786 | 2010-07-12 18:48:50 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Ian Romanick | 13f782c | 2010-06-29 18:53:38 -0700 | [diff] [blame] | 837 | /* Resolve initializers for global variables in the linked shader. |
| 838 | */ |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 839 | unsigned num_linking_shaders = num_shaders; |
| 840 | for (unsigned i = 0; i < num_shaders; i++) |
| 841 | num_linking_shaders += shader_list[i]->num_builtins_to_link; |
| 842 | |
| 843 | gl_shader **linking_shaders = |
| 844 | (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *)); |
| 845 | |
| 846 | memcpy(linking_shaders, shader_list, |
| 847 | sizeof(linking_shaders[0]) * num_shaders); |
| 848 | |
| 849 | unsigned idx = num_shaders; |
| 850 | for (unsigned i = 0; i < num_shaders; i++) { |
| 851 | memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link, |
| 852 | sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link); |
| 853 | idx += shader_list[i]->num_builtins_to_link; |
| 854 | } |
| 855 | |
| 856 | assert(idx == num_linking_shaders); |
| 857 | |
Ian Romanick | 4a45595 | 2010-10-13 15:13:02 -0700 | [diff] [blame] | 858 | if (!link_function_calls(prog, linked, linking_shaders, |
| 859 | num_linking_shaders)) { |
| 860 | ctx->Driver.DeleteShader(ctx, linked); |
| 861 | linked = NULL; |
| 862 | } |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 863 | |
| 864 | free(linking_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 865 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 866 | return linked; |
| 867 | } |
| 868 | |
| 869 | |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 870 | struct uniform_node { |
| 871 | exec_node link; |
| 872 | struct gl_uniform *u; |
| 873 | unsigned slots; |
| 874 | }; |
| 875 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 876 | /** |
| 877 | * Update the sizes of linked shader uniform arrays to the maximum |
| 878 | * array index used. |
| 879 | * |
| 880 | * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec: |
| 881 | * |
| 882 | * If one or more elements of an array are active, |
| 883 | * GetActiveUniform will return the name of the array in name, |
| 884 | * subject to the restrictions listed above. The type of the array |
| 885 | * is returned in type. The size parameter contains the highest |
| 886 | * array element index used, plus one. The compiler or linker |
| 887 | * determines the highest index used. There will be only one |
| 888 | * active uniform reported by the GL per uniform array. |
| 889 | |
| 890 | */ |
| 891 | static void |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 892 | update_array_sizes(struct gl_shader_program *prog) |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 893 | { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 894 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 895 | if (prog->_LinkedShaders[i] == NULL) |
| 896 | continue; |
| 897 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 898 | foreach_list(node, prog->_LinkedShaders[i]->ir) { |
| 899 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 900 | |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 901 | if ((var == NULL) || (var->mode != ir_var_uniform && |
| 902 | var->mode != ir_var_in && |
| 903 | var->mode != ir_var_out) || |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 904 | !var->type->is_array()) |
| 905 | continue; |
| 906 | |
| 907 | unsigned int size = var->max_array_access; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 908 | for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) { |
| 909 | if (prog->_LinkedShaders[j] == NULL) |
| 910 | continue; |
| 911 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 912 | foreach_list(node2, prog->_LinkedShaders[j]->ir) { |
| 913 | ir_variable *other_var = ((ir_instruction *) node2)->as_variable(); |
| 914 | if (!other_var) |
| 915 | continue; |
| 916 | |
| 917 | if (strcmp(var->name, other_var->name) == 0 && |
| 918 | other_var->max_array_access > size) { |
| 919 | size = other_var->max_array_access; |
| 920 | } |
| 921 | } |
| 922 | } |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 923 | |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 924 | if (size + 1 != var->type->fields.array->length) { |
| 925 | var->type = glsl_type::get_array_instance(var->type->fields.array, |
| 926 | size + 1); |
| 927 | /* FINISHME: We should update the types of array |
| 928 | * dereferences of this variable now. |
| 929 | */ |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 935 | static void |
| 936 | add_uniform(void *mem_ctx, exec_list *uniforms, struct hash_table *ht, |
| 937 | const char *name, const glsl_type *type, GLenum shader_type, |
| 938 | unsigned *next_shader_pos, unsigned *total_uniforms) |
| 939 | { |
| 940 | if (type->is_record()) { |
| 941 | for (unsigned int i = 0; i < type->length; i++) { |
| 942 | const glsl_type *field_type = type->fields.structure[i].type; |
| 943 | char *field_name = talloc_asprintf(mem_ctx, "%s.%s", name, |
| 944 | type->fields.structure[i].name); |
| 945 | |
| 946 | add_uniform(mem_ctx, uniforms, ht, field_name, field_type, |
| 947 | shader_type, next_shader_pos, total_uniforms); |
| 948 | } |
| 949 | } else { |
| 950 | uniform_node *n = (uniform_node *) hash_table_find(ht, name); |
| 951 | unsigned int vec4_slots; |
| 952 | const glsl_type *array_elem_type = NULL; |
| 953 | |
| 954 | if (type->is_array()) { |
| 955 | array_elem_type = type->fields.array; |
| 956 | /* Array of structures. */ |
| 957 | if (array_elem_type->is_record()) { |
| 958 | for (unsigned int i = 0; i < type->length; i++) { |
| 959 | char *elem_name = talloc_asprintf(mem_ctx, "%s[%d]", name, i); |
| 960 | add_uniform(mem_ctx, uniforms, ht, elem_name, array_elem_type, |
| 961 | shader_type, next_shader_pos, total_uniforms); |
| 962 | } |
| 963 | return; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | /* Fix the storage size of samplers at 1 vec4 each. Be sure to pad out |
| 968 | * vectors to vec4 slots. |
| 969 | */ |
| 970 | if (type->is_array()) { |
| 971 | if (array_elem_type->is_sampler()) |
| 972 | vec4_slots = type->length; |
| 973 | else |
| 974 | vec4_slots = type->length * array_elem_type->matrix_columns; |
| 975 | } else if (type->is_sampler()) { |
| 976 | vec4_slots = 1; |
| 977 | } else { |
| 978 | vec4_slots = type->matrix_columns; |
| 979 | } |
| 980 | |
| 981 | if (n == NULL) { |
| 982 | n = (uniform_node *) calloc(1, sizeof(struct uniform_node)); |
| 983 | n->u = (gl_uniform *) calloc(1, sizeof(struct gl_uniform)); |
| 984 | n->slots = vec4_slots; |
| 985 | |
| 986 | n->u->Name = strdup(name); |
Eric Anholt | 0924ba0 | 2010-08-24 14:27:27 -0700 | [diff] [blame] | 987 | n->u->Type = type; |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 988 | n->u->VertPos = -1; |
| 989 | n->u->FragPos = -1; |
| 990 | n->u->GeomPos = -1; |
| 991 | (*total_uniforms)++; |
| 992 | |
| 993 | hash_table_insert(ht, n, name); |
| 994 | uniforms->push_tail(& n->link); |
| 995 | } |
| 996 | |
| 997 | switch (shader_type) { |
| 998 | case GL_VERTEX_SHADER: |
| 999 | n->u->VertPos = *next_shader_pos; |
| 1000 | break; |
| 1001 | case GL_FRAGMENT_SHADER: |
| 1002 | n->u->FragPos = *next_shader_pos; |
| 1003 | break; |
| 1004 | case GL_GEOMETRY_SHADER: |
| 1005 | n->u->GeomPos = *next_shader_pos; |
| 1006 | break; |
| 1007 | } |
| 1008 | |
| 1009 | (*next_shader_pos) += vec4_slots; |
| 1010 | } |
| 1011 | } |
| 1012 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1013 | void |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 1014 | assign_uniform_locations(struct gl_shader_program *prog) |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1015 | { |
| 1016 | /* */ |
| 1017 | exec_list uniforms; |
| 1018 | unsigned total_uniforms = 0; |
| 1019 | hash_table *ht = hash_table_ctor(32, hash_table_string_hash, |
| 1020 | hash_table_string_compare); |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 1021 | void *mem_ctx = talloc_new(NULL); |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1022 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1023 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1024 | if (prog->_LinkedShaders[i] == NULL) |
| 1025 | continue; |
| 1026 | |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1027 | unsigned next_position = 0; |
| 1028 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1029 | foreach_list(node, prog->_LinkedShaders[i]->ir) { |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1030 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1031 | |
| 1032 | if ((var == NULL) || (var->mode != ir_var_uniform)) |
| 1033 | continue; |
| 1034 | |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 1035 | if (strncmp(var->name, "gl_", 3) == 0) { |
| 1036 | /* At the moment, we don't allocate uniform locations for |
| 1037 | * builtin uniforms. It's permitted by spec, and we'll |
| 1038 | * likely switch to doing that at some point, but not yet. |
Eric Anholt | 8d61a23 | 2010-08-05 16:00:46 -0700 | [diff] [blame] | 1039 | */ |
| 1040 | continue; |
| 1041 | } |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1042 | |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1043 | var->location = next_position; |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 1044 | add_uniform(mem_ctx, &uniforms, ht, var->name, var->type, |
| 1045 | prog->_LinkedShaders[i]->Type, |
| 1046 | &next_position, &total_uniforms); |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1047 | } |
| 1048 | } |
| 1049 | |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 1050 | talloc_free(mem_ctx); |
| 1051 | |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1052 | gl_uniform_list *ul = (gl_uniform_list *) |
| 1053 | calloc(1, sizeof(gl_uniform_list)); |
| 1054 | |
| 1055 | ul->Size = total_uniforms; |
| 1056 | ul->NumUniforms = total_uniforms; |
| 1057 | ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform)); |
| 1058 | |
| 1059 | unsigned idx = 0; |
| 1060 | uniform_node *next; |
| 1061 | for (uniform_node *node = (uniform_node *) uniforms.head |
| 1062 | ; node->link.next != NULL |
| 1063 | ; node = next) { |
| 1064 | next = (uniform_node *) node->link.next; |
| 1065 | |
| 1066 | node->link.remove(); |
Eric Anholt | 45388b5 | 2010-08-24 13:51:35 -0700 | [diff] [blame] | 1067 | memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform)); |
| 1068 | idx++; |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1069 | |
| 1070 | free(node->u); |
| 1071 | free(node); |
| 1072 | } |
| 1073 | |
| 1074 | hash_table_dtor(ht); |
| 1075 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1076 | prog->Uniforms = ul; |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1080 | /** |
| 1081 | * Find a contiguous set of available bits in a bitmask |
| 1082 | * |
| 1083 | * \param used_mask Bits representing used (1) and unused (0) locations |
| 1084 | * \param needed_count Number of contiguous bits needed. |
| 1085 | * |
| 1086 | * \return |
| 1087 | * Base location of the available bits on success or -1 on failure. |
| 1088 | */ |
| 1089 | int |
| 1090 | find_available_slots(unsigned used_mask, unsigned needed_count) |
| 1091 | { |
| 1092 | unsigned needed_mask = (1 << needed_count) - 1; |
| 1093 | const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count; |
| 1094 | |
| 1095 | /* The comparison to 32 is redundant, but without it GCC emits "warning: |
| 1096 | * cannot optimize possibly infinite loops" for the loop below. |
| 1097 | */ |
| 1098 | if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32)) |
| 1099 | return -1; |
| 1100 | |
| 1101 | for (int i = 0; i <= max_bit_to_test; i++) { |
| 1102 | if ((needed_mask & ~used_mask) == needed_mask) |
| 1103 | return i; |
| 1104 | |
| 1105 | needed_mask <<= 1; |
| 1106 | } |
| 1107 | |
| 1108 | return -1; |
| 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | bool |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 1113 | assign_attribute_locations(gl_shader_program *prog, unsigned max_attribute_index) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1114 | { |
Ian Romanick | 9342d26 | 2010-06-22 17:41:37 -0700 | [diff] [blame] | 1115 | /* Mark invalid attribute locations as being used. |
| 1116 | */ |
| 1117 | unsigned used_locations = (max_attribute_index >= 32) |
| 1118 | ? ~0 : ~((1 << max_attribute_index) - 1); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1119 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1120 | gl_shader *const sh = prog->_LinkedShaders[0]; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1121 | assert(sh->Type == GL_VERTEX_SHADER); |
| 1122 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1123 | /* Operate in a total of four passes. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1124 | * |
| 1125 | * 1. Invalidate the location assignments for all vertex shader inputs. |
| 1126 | * |
| 1127 | * 2. Assign locations for inputs that have user-defined (via |
| 1128 | * glBindVertexAttribLocation) locatoins. |
| 1129 | * |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1130 | * 3. Sort the attributes without assigned locations by number of slots |
| 1131 | * required in decreasing order. Fragmentation caused by attribute |
| 1132 | * locations assigned by the application may prevent large attributes |
| 1133 | * from having enough contiguous space. |
| 1134 | * |
| 1135 | * 4. Assign locations to any inputs without assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1136 | */ |
| 1137 | |
| 1138 | invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0); |
| 1139 | |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1140 | if (prog->Attributes != NULL) { |
| 1141 | for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1142 | ir_variable *const var = |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1143 | sh->symbols->get_variable(prog->Attributes->Parameters[i].Name); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1144 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1145 | /* Note: attributes that occupy multiple slots, such as arrays or |
| 1146 | * matrices, may appear in the attrib array multiple times. |
| 1147 | */ |
| 1148 | if ((var == NULL) || (var->location != -1)) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1149 | continue; |
| 1150 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1151 | /* From page 61 of the OpenGL 4.0 spec: |
| 1152 | * |
| 1153 | * "LinkProgram will fail if the attribute bindings assigned by |
| 1154 | * BindAttribLocation do not leave not enough space to assign a |
| 1155 | * location for an active matrix attribute or an active attribute |
| 1156 | * array, both of which require multiple contiguous generic |
| 1157 | * attributes." |
| 1158 | * |
| 1159 | * Previous versions of the spec contain similar language but omit the |
| 1160 | * bit about attribute arrays. |
| 1161 | * |
| 1162 | * Page 61 of the OpenGL 4.0 spec also says: |
| 1163 | * |
| 1164 | * "It is possible for an application to bind more than one |
| 1165 | * attribute name to the same location. This is referred to as |
| 1166 | * aliasing. This will only work if only one of the aliased |
| 1167 | * attributes is active in the executable program, or if no path |
| 1168 | * through the shader consumes more than one attribute of a set |
| 1169 | * of attributes aliased to the same location. A link error can |
| 1170 | * occur if the linker determines that every path through the |
| 1171 | * shader consumes multiple aliased attributes, but |
| 1172 | * implementations are not required to generate an error in this |
| 1173 | * case." |
| 1174 | * |
| 1175 | * These two paragraphs are either somewhat contradictory, or I don't |
| 1176 | * fully understand one or both of them. |
| 1177 | */ |
| 1178 | /* FINISHME: The code as currently written does not support attribute |
| 1179 | * FINISHME: location aliasing (see comment above). |
| 1180 | */ |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1181 | const int attr = prog->Attributes->Parameters[i].StateIndexes[0]; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1182 | const unsigned slots = count_attribute_slots(var->type); |
| 1183 | |
| 1184 | /* Mask representing the contiguous slots that will be used by this |
| 1185 | * attribute. |
| 1186 | */ |
| 1187 | const unsigned use_mask = (1 << slots) - 1; |
| 1188 | |
| 1189 | /* Generate a link error if the set of bits requested for this |
| 1190 | * attribute overlaps any previously allocated bits. |
| 1191 | */ |
| 1192 | if ((~(use_mask << attr) & used_locations) != used_locations) { |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1193 | linker_error_printf(prog, |
| 1194 | "insufficient contiguous attribute locations " |
| 1195 | "available for vertex shader input `%s'", |
| 1196 | var->name); |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1197 | return false; |
| 1198 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1199 | |
| 1200 | var->location = VERT_ATTRIB_GENERIC0 + attr; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1201 | used_locations |= (use_mask << attr); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1205 | /* Temporary storage for the set of attributes that need locations assigned. |
| 1206 | */ |
| 1207 | struct temp_attr { |
| 1208 | unsigned slots; |
| 1209 | ir_variable *var; |
| 1210 | |
| 1211 | /* Used below in the call to qsort. */ |
| 1212 | static int compare(const void *a, const void *b) |
| 1213 | { |
| 1214 | const temp_attr *const l = (const temp_attr *) a; |
| 1215 | const temp_attr *const r = (const temp_attr *) b; |
| 1216 | |
| 1217 | /* Reversed because we want a descending order sort below. */ |
| 1218 | return r->slots - l->slots; |
| 1219 | } |
| 1220 | } to_assign[16]; |
| 1221 | |
| 1222 | unsigned num_attr = 0; |
| 1223 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1224 | foreach_list(node, sh->ir) { |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1225 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1226 | |
| 1227 | if ((var == NULL) || (var->mode != ir_var_in)) |
| 1228 | continue; |
| 1229 | |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 1230 | if (var->explicit_location) { |
| 1231 | const unsigned slots = count_attribute_slots(var->type); |
| 1232 | const unsigned use_mask = (1 << slots) - 1; |
| 1233 | const int attr = var->location - VERT_ATTRIB_GENERIC0; |
| 1234 | |
| 1235 | if ((var->location >= (int)(max_attribute_index + VERT_ATTRIB_GENERIC0)) |
| 1236 | || (var->location < 0)) { |
| 1237 | linker_error_printf(prog, |
| 1238 | "invalid explicit location %d specified for " |
| 1239 | "`%s'\n", |
| 1240 | (var->location < 0) ? var->location : attr, |
| 1241 | var->name); |
| 1242 | return false; |
| 1243 | } else if (var->location >= VERT_ATTRIB_GENERIC0) { |
| 1244 | used_locations |= (use_mask << attr); |
| 1245 | } |
| 1246 | } |
| 1247 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1248 | /* The location was explicitly assigned, nothing to do here. |
| 1249 | */ |
| 1250 | if (var->location != -1) |
| 1251 | continue; |
| 1252 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1253 | to_assign[num_attr].slots = count_attribute_slots(var->type); |
| 1254 | to_assign[num_attr].var = var; |
| 1255 | num_attr++; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1256 | } |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1257 | |
| 1258 | /* If all of the attributes were assigned locations by the application (or |
| 1259 | * are built-in attributes with fixed locations), return early. This should |
| 1260 | * be the common case. |
| 1261 | */ |
| 1262 | if (num_attr == 0) |
| 1263 | return true; |
| 1264 | |
| 1265 | qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare); |
| 1266 | |
Ian Romanick | 982e379 | 2010-06-29 18:58:20 -0700 | [diff] [blame] | 1267 | /* VERT_ATTRIB_GENERIC0 is a psdueo-alias for VERT_ATTRIB_POS. It can only |
| 1268 | * be explicitly assigned by via glBindAttribLocation. Mark it as reserved |
| 1269 | * to prevent it from being automatically allocated below. |
| 1270 | */ |
Ian Romanick | c33e78f | 2010-08-13 12:30:41 -0700 | [diff] [blame] | 1271 | find_deref_visitor find("gl_Vertex"); |
| 1272 | find.run(sh->ir); |
| 1273 | if (find.variable_found()) |
| 1274 | used_locations |= (1 << 0); |
Ian Romanick | 982e379 | 2010-06-29 18:58:20 -0700 | [diff] [blame] | 1275 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1276 | for (unsigned i = 0; i < num_attr; i++) { |
| 1277 | /* Mask representing the contiguous slots that will be used by this |
| 1278 | * attribute. |
| 1279 | */ |
| 1280 | const unsigned use_mask = (1 << to_assign[i].slots) - 1; |
| 1281 | |
| 1282 | int location = find_available_slots(used_locations, to_assign[i].slots); |
| 1283 | |
| 1284 | if (location < 0) { |
Ian Romanick | 553dcdc | 2010-06-23 12:14:02 -0700 | [diff] [blame] | 1285 | linker_error_printf(prog, |
| 1286 | "insufficient contiguous attribute locations " |
| 1287 | "available for vertex shader input `%s'", |
| 1288 | to_assign[i].var->name); |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1289 | return false; |
| 1290 | } |
| 1291 | |
| 1292 | to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location; |
| 1293 | used_locations |= (use_mask << location); |
| 1294 | } |
| 1295 | |
| 1296 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1300 | /** |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1301 | * Demote shader inputs and outputs that are not used in other stages |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1302 | */ |
| 1303 | void |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1304 | 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] | 1305 | { |
| 1306 | foreach_list(node, sh->ir) { |
| 1307 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1308 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1309 | if ((var == NULL) || (var->mode != int(mode))) |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1310 | continue; |
| 1311 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1312 | /* A shader 'in' or 'out' variable is only really an input or output if |
| 1313 | * its value is used by other shader stages. This will cause the variable |
| 1314 | * to have a location assigned. |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1315 | */ |
| 1316 | if (var->location == -1) { |
| 1317 | var->mode = ir_var_auto; |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 1323 | void |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1324 | assign_varying_locations(struct gl_shader_program *prog, |
| 1325 | gl_shader *producer, gl_shader *consumer) |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1326 | { |
| 1327 | /* FINISHME: Set dynamically when geometry shader support is added. */ |
| 1328 | unsigned output_index = VERT_RESULT_VAR0; |
| 1329 | unsigned input_index = FRAG_ATTRIB_VAR0; |
| 1330 | |
| 1331 | /* Operate in a total of three passes. |
| 1332 | * |
| 1333 | * 1. Assign locations for any matching inputs and outputs. |
| 1334 | * |
| 1335 | * 2. Mark output variables in the producer that do not have locations as |
| 1336 | * not being outputs. This lets the optimizer eliminate them. |
| 1337 | * |
| 1338 | * 3. Mark input variables in the consumer that do not have locations as |
| 1339 | * not being inputs. This lets the optimizer eliminate them. |
| 1340 | */ |
| 1341 | |
| 1342 | invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0); |
| 1343 | invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0); |
| 1344 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1345 | foreach_list(node, producer->ir) { |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1346 | ir_variable *const output_var = ((ir_instruction *) node)->as_variable(); |
| 1347 | |
| 1348 | if ((output_var == NULL) || (output_var->mode != ir_var_out) |
| 1349 | || (output_var->location != -1)) |
| 1350 | continue; |
| 1351 | |
| 1352 | ir_variable *const input_var = |
| 1353 | consumer->symbols->get_variable(output_var->name); |
| 1354 | |
| 1355 | if ((input_var == NULL) || (input_var->mode != ir_var_in)) |
| 1356 | continue; |
| 1357 | |
| 1358 | assert(input_var->location == -1); |
| 1359 | |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1360 | output_var->location = output_index; |
| 1361 | input_var->location = input_index; |
| 1362 | |
Ian Romanick | df869d9 | 2010-08-30 15:37:44 -0700 | [diff] [blame] | 1363 | /* FINISHME: Support for "varying" records in GLSL 1.50. */ |
| 1364 | assert(!output_var->type->is_record()); |
| 1365 | |
| 1366 | if (output_var->type->is_array()) { |
| 1367 | const unsigned slots = output_var->type->length |
| 1368 | * output_var->type->fields.array->matrix_columns; |
| 1369 | |
| 1370 | output_index += slots; |
| 1371 | input_index += slots; |
| 1372 | } else { |
| 1373 | const unsigned slots = output_var->type->matrix_columns; |
| 1374 | |
| 1375 | output_index += slots; |
| 1376 | input_index += slots; |
| 1377 | } |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1380 | foreach_list(node, consumer->ir) { |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1381 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 1382 | |
| 1383 | if ((var == NULL) || (var->mode != ir_var_in)) |
| 1384 | continue; |
| 1385 | |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1386 | if (var->location == -1) { |
| 1387 | if (prog->Version <= 120) { |
| 1388 | /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec: |
| 1389 | * |
| 1390 | * Only those varying variables used (i.e. read) in |
| 1391 | * the fragment shader executable must be written to |
| 1392 | * by the vertex shader executable; declaring |
| 1393 | * superfluous varying variables in a vertex shader is |
| 1394 | * permissible. |
| 1395 | * |
| 1396 | * We interpret this text as meaning that the VS must |
| 1397 | * write the variable for the FS to read it. See |
| 1398 | * "glsl1-varying read but not written" in piglit. |
| 1399 | */ |
| 1400 | |
| 1401 | linker_error_printf(prog, "fragment shader varying %s not written " |
| 1402 | "by vertex shader\n.", var->name); |
| 1403 | prog->LinkStatus = false; |
| 1404 | } |
| 1405 | |
| 1406 | /* An 'in' variable is only really a shader input if its |
| 1407 | * value is written by the previous stage. |
| 1408 | */ |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1409 | var->mode = ir_var_auto; |
| 1410 | } |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | |
| 1415 | void |
Kristian Høgsberg | f9995b3 | 2010-10-12 12:26:10 -0400 | [diff] [blame] | 1416 | link_shaders(struct gl_context *ctx, struct gl_shader_program *prog) |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1417 | { |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1418 | void *mem_ctx = talloc_init("temporary linker context"); |
| 1419 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1420 | prog->LinkStatus = false; |
| 1421 | prog->Validated = false; |
| 1422 | prog->_Used = false; |
| 1423 | |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 1424 | if (prog->InfoLog != NULL) |
| 1425 | talloc_free(prog->InfoLog); |
| 1426 | |
| 1427 | prog->InfoLog = talloc_strdup(NULL, ""); |
| 1428 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1429 | /* Separate the shaders into groups based on their type. |
| 1430 | */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1431 | struct gl_shader **vert_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1432 | unsigned num_vert_shaders = 0; |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1433 | struct gl_shader **frag_shader_list; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1434 | unsigned num_frag_shaders = 0; |
| 1435 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 1436 | vert_shader_list = (struct gl_shader **) |
| 1437 | calloc(2 * prog->NumShaders, sizeof(struct gl_shader *)); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1438 | frag_shader_list = &vert_shader_list[prog->NumShaders]; |
| 1439 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1440 | unsigned min_version = UINT_MAX; |
| 1441 | unsigned max_version = 0; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1442 | for (unsigned i = 0; i < prog->NumShaders; i++) { |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1443 | min_version = MIN2(min_version, prog->Shaders[i]->Version); |
| 1444 | max_version = MAX2(max_version, prog->Shaders[i]->Version); |
| 1445 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1446 | switch (prog->Shaders[i]->Type) { |
| 1447 | case GL_VERTEX_SHADER: |
| 1448 | vert_shader_list[num_vert_shaders] = prog->Shaders[i]; |
| 1449 | num_vert_shaders++; |
| 1450 | break; |
| 1451 | case GL_FRAGMENT_SHADER: |
| 1452 | frag_shader_list[num_frag_shaders] = prog->Shaders[i]; |
| 1453 | num_frag_shaders++; |
| 1454 | break; |
| 1455 | case GL_GEOMETRY_SHADER: |
| 1456 | /* FINISHME: Support geometry shaders. */ |
| 1457 | assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER); |
| 1458 | break; |
| 1459 | } |
| 1460 | } |
| 1461 | |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1462 | /* Previous to GLSL version 1.30, different compilation units could mix and |
| 1463 | * match shading language versions. With GLSL 1.30 and later, the versions |
| 1464 | * of all shaders must match. |
| 1465 | */ |
Kenneth Graunke | 5a81d05 | 2010-08-31 09:33:58 -0700 | [diff] [blame] | 1466 | assert(min_version >= 100); |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1467 | assert(max_version <= 130); |
Kenneth Graunke | 5a81d05 | 2010-08-31 09:33:58 -0700 | [diff] [blame] | 1468 | if ((max_version >= 130 || min_version == 100) |
| 1469 | && min_version != max_version) { |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 1470 | linker_error_printf(prog, "all shaders must use same shading " |
| 1471 | "language version\n"); |
| 1472 | goto done; |
| 1473 | } |
| 1474 | |
| 1475 | prog->Version = max_version; |
| 1476 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1477 | for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1478 | if (prog->_LinkedShaders[i] != NULL) |
| 1479 | ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]); |
| 1480 | |
| 1481 | prog->_LinkedShaders[i] = NULL; |
Eric Anholt | 5d0f430 | 2010-08-18 12:02:35 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
Ian Romanick | cd6764e | 2010-07-16 16:00:07 -0700 | [diff] [blame] | 1484 | /* Link all shaders for a particular stage and validate the result. |
| 1485 | */ |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1486 | if (num_vert_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1487 | gl_shader *const sh = |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1488 | link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list, |
| 1489 | num_vert_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1490 | |
| 1491 | if (sh == NULL) |
| 1492 | goto done; |
| 1493 | |
| 1494 | if (!validate_vertex_shader_executable(prog, sh)) |
Ian Romanick | f29ff6e | 2010-10-14 17:55:17 -0700 | [diff] [blame] | 1495 | goto done; |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1496 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1497 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX], |
| 1498 | sh); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | if (num_frag_shaders > 0) { |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1502 | gl_shader *const sh = |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1503 | link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list, |
| 1504 | num_frag_shaders); |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1505 | |
| 1506 | if (sh == NULL) |
| 1507 | goto done; |
| 1508 | |
| 1509 | if (!validate_fragment_shader_executable(prog, sh)) |
Ian Romanick | f29ff6e | 2010-10-14 17:55:17 -0700 | [diff] [blame] | 1510 | goto done; |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 1511 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1512 | _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT], |
| 1513 | sh); |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
Ian Romanick | 3ed850e | 2010-06-23 12:18:21 -0700 | [diff] [blame] | 1516 | /* Here begins the inter-stage linking phase. Some initial validation is |
| 1517 | * performed, then locations are assigned for uniforms, attributes, and |
| 1518 | * varyings. |
| 1519 | */ |
Ian Romanick | ed1fe3d | 2010-06-23 12:09:14 -0700 | [diff] [blame] | 1520 | if (cross_validate_uniforms(prog)) { |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1521 | unsigned prev; |
| 1522 | |
| 1523 | for (prev = 0; prev < MESA_SHADER_TYPES; prev++) { |
| 1524 | if (prog->_LinkedShaders[prev] != NULL) |
| 1525 | break; |
| 1526 | } |
| 1527 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1528 | /* Validate the inputs of each stage with the output of the preceeding |
| 1529 | * stage. |
| 1530 | */ |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1531 | for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) { |
| 1532 | if (prog->_LinkedShaders[i] == NULL) |
| 1533 | continue; |
| 1534 | |
Ian Romanick | f36460e | 2010-06-23 12:07:22 -0700 | [diff] [blame] | 1535 | if (!cross_validate_outputs_to_inputs(prog, |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1536 | prog->_LinkedShaders[prev], |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1537 | prog->_LinkedShaders[i])) |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1538 | goto done; |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1539 | |
| 1540 | prev = i; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 1543 | prog->LinkStatus = true; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 1544 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1545 | |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 1546 | /* Do common optimization before assigning storage for attributes, |
| 1547 | * uniforms, and varyings. Later optimization could possibly make |
| 1548 | * some of that unused. |
| 1549 | */ |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1550 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1551 | if (prog->_LinkedShaders[i] == NULL) |
| 1552 | continue; |
| 1553 | |
Luca Barbieri | e591c46 | 2010-09-05 22:29:58 +0200 | [diff] [blame] | 1554 | while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, 32)) |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 1555 | ; |
Ian Romanick | a7ba9a7 | 2010-07-20 13:36:32 -0700 | [diff] [blame] | 1556 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1557 | |
Eric Anholt | 586b4b5 | 2010-09-28 14:32:16 -0700 | [diff] [blame] | 1558 | update_array_sizes(prog); |
| 1559 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 1560 | assign_uniform_locations(prog); |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1561 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1562 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) { |
Ian Romanick | 9342d26 | 2010-06-22 17:41:37 -0700 | [diff] [blame] | 1563 | /* FINISHME: The value of the max_attribute_index parameter is |
| 1564 | * FINISHME: implementation dependent based on the value of |
| 1565 | * FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be |
| 1566 | * FINISHME: at least 16, so hardcode 16 for now. |
| 1567 | */ |
Ian Romanick | 4b5489d | 2010-10-07 17:20:15 -0700 | [diff] [blame] | 1568 | if (!assign_attribute_locations(prog, 16)) { |
| 1569 | prog->LinkStatus = false; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame] | 1570 | goto done; |
Ian Romanick | 4b5489d | 2010-10-07 17:20:15 -0700 | [diff] [blame] | 1571 | } |
Ian Romanick | 40e114b | 2010-08-17 14:55:50 -0700 | [diff] [blame] | 1572 | } |
| 1573 | |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1574 | unsigned prev; |
| 1575 | for (prev = 0; prev < MESA_SHADER_TYPES; prev++) { |
| 1576 | if (prog->_LinkedShaders[prev] != NULL) |
| 1577 | break; |
| 1578 | } |
| 1579 | |
| 1580 | for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) { |
| 1581 | if (prog->_LinkedShaders[i] == NULL) |
| 1582 | continue; |
| 1583 | |
Eric Anholt | b706283 | 2010-07-28 13:52:23 -0700 | [diff] [blame] | 1584 | assign_varying_locations(prog, |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1585 | prog->_LinkedShaders[prev], |
Ian Romanick | 0e59b26 | 2010-06-23 11:23:01 -0700 | [diff] [blame] | 1586 | prog->_LinkedShaders[i]); |
Ian Romanick | 3322fba | 2010-10-14 13:28:42 -0700 | [diff] [blame] | 1587 | prev = i; |
| 1588 | } |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1589 | |
Ian Romanick | cc90e62 | 2010-10-19 17:59:10 -0700 | [diff] [blame] | 1590 | if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) { |
| 1591 | demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX], |
| 1592 | ir_var_out); |
| 1593 | } |
| 1594 | |
| 1595 | if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) { |
| 1596 | gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY]; |
| 1597 | |
| 1598 | demote_shader_inputs_and_outputs(sh, ir_var_in); |
| 1599 | demote_shader_inputs_and_outputs(sh, ir_var_inout); |
| 1600 | demote_shader_inputs_and_outputs(sh, ir_var_out); |
| 1601 | } |
| 1602 | |
| 1603 | if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) { |
| 1604 | gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]; |
| 1605 | |
| 1606 | demote_shader_inputs_and_outputs(sh, ir_var_in); |
| 1607 | } |
| 1608 | |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 1609 | /* FINISHME: Assign fragment shader output locations. */ |
| 1610 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1611 | done: |
| 1612 | free(vert_shader_list); |
Kenneth Graunke | 2da02e7 | 2010-11-17 11:03:57 -0800 | [diff] [blame] | 1613 | |
| 1614 | for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) { |
| 1615 | if (prog->_LinkedShaders[i] == NULL) |
| 1616 | continue; |
| 1617 | |
| 1618 | /* Retain any live IR, but trash the rest. */ |
| 1619 | reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir); |
| 1620 | } |
| 1621 | |
| 1622 | talloc_free(mem_ctx); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 1623 | } |