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