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