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> |
| 68 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 69 | #include "main/mtypes.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 70 | #include "glsl_symbol_table.h" |
| 71 | #include "glsl_parser_extras.h" |
| 72 | #include "ir.h" |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 73 | #include "ir_optimization.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 74 | #include "program.h" |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 75 | #include "hash_table.h" |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Visitor that determines whether or not a variable is ever written. |
| 79 | */ |
| 80 | class find_assignment_visitor : public ir_hierarchical_visitor { |
| 81 | public: |
| 82 | find_assignment_visitor(const char *name) |
| 83 | : name(name), found(false) |
| 84 | { |
| 85 | /* empty */ |
| 86 | } |
| 87 | |
| 88 | virtual ir_visitor_status visit_enter(ir_assignment *ir) |
| 89 | { |
| 90 | ir_variable *const var = ir->lhs->variable_referenced(); |
| 91 | |
| 92 | if (strcmp(name, var->name) == 0) { |
| 93 | found = true; |
| 94 | return visit_stop; |
| 95 | } |
| 96 | |
| 97 | return visit_continue_with_parent; |
| 98 | } |
| 99 | |
| 100 | bool variable_found() |
| 101 | { |
| 102 | return found; |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | const char *name; /**< Find writes to a variable with this name. */ |
| 107 | bool found; /**< Was a write to the variable found? */ |
| 108 | }; |
| 109 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 110 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 111 | void |
| 112 | invalidate_variable_locations(glsl_shader *sh, enum ir_variable_mode mode, |
| 113 | int generic_base) |
| 114 | { |
| 115 | foreach_list(node, &sh->ir) { |
| 116 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 117 | |
| 118 | if ((var == NULL) || (var->mode != (unsigned) mode)) |
| 119 | continue; |
| 120 | |
| 121 | /* Only assign locations for generic attributes / varyings / etc. |
| 122 | */ |
| 123 | if (var->location >= generic_base) |
| 124 | var->location = -1; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 129 | /** |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame^] | 130 | * Determine the number of attribute slots required for a particular type |
| 131 | * |
| 132 | * This code is here because it implements the language rules of a specific |
| 133 | * GLSL version. Since it's a property of the language and not a property of |
| 134 | * types in general, it doesn't really belong in glsl_type. |
| 135 | */ |
| 136 | unsigned |
| 137 | count_attribute_slots(const glsl_type *t) |
| 138 | { |
| 139 | /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: |
| 140 | * |
| 141 | * "A scalar input counts the same amount against this limit as a vec4, |
| 142 | * so applications may want to consider packing groups of four |
| 143 | * unrelated float inputs together into a vector to better utilize the |
| 144 | * capabilities of the underlying hardware. A matrix input will use up |
| 145 | * multiple locations. The number of locations used will equal the |
| 146 | * number of columns in the matrix." |
| 147 | * |
| 148 | * The spec does not explicitly say how arrays are counted. However, it |
| 149 | * should be safe to assume the total number of slots consumed by an array |
| 150 | * is the number of entries in the array multiplied by the number of slots |
| 151 | * consumed by a single element of the array. |
| 152 | */ |
| 153 | |
| 154 | if (t->is_array()) |
| 155 | return t->array_size() * count_attribute_slots(t->element_type()); |
| 156 | |
| 157 | if (t->is_matrix()) |
| 158 | return t->matrix_columns; |
| 159 | |
| 160 | return 1; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 165 | * Verify that a vertex shader executable meets all semantic requirements |
| 166 | * |
| 167 | * \param shader Vertex shader executable to be verified |
| 168 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 169 | bool |
| 170 | validate_vertex_shader_executable(struct glsl_shader *shader) |
| 171 | { |
| 172 | if (shader == NULL) |
| 173 | return true; |
| 174 | |
| 175 | if (!shader->symbols->get_function("main")) { |
| 176 | printf("error: vertex shader lacks `main'\n"); |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | find_assignment_visitor find("gl_Position"); |
| 181 | find.run(&shader->ir); |
| 182 | if (!find.variable_found()) { |
| 183 | printf("error: vertex shader does not write to `gl_Position'\n"); |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | |
Ian Romanick | c93b8f1 | 2010-06-17 15:20:22 -0700 | [diff] [blame] | 191 | /** |
| 192 | * Verify that a fragment shader executable meets all semantic requirements |
| 193 | * |
| 194 | * \param shader Fragment shader executable to be verified |
| 195 | */ |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 196 | bool |
| 197 | validate_fragment_shader_executable(struct glsl_shader *shader) |
| 198 | { |
| 199 | if (shader == NULL) |
| 200 | return true; |
| 201 | |
| 202 | if (!shader->symbols->get_function("main")) { |
| 203 | printf("error: fragment shader lacks `main'\n"); |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | find_assignment_visitor frag_color("gl_FragColor"); |
| 208 | find_assignment_visitor frag_data("gl_FragData"); |
| 209 | |
| 210 | frag_color.run(&shader->ir); |
| 211 | frag_data.run(&shader->ir); |
| 212 | |
| 213 | if (!frag_color.variable_found() && !frag_data.variable_found()) { |
| 214 | printf("error: fragment shader does not write to `gl_FragColor' or " |
| 215 | "`gl_FragData'\n"); |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | if (frag_color.variable_found() && frag_data.variable_found()) { |
| 220 | printf("error: fragment shader write to both `gl_FragColor' and " |
| 221 | "`gl_FragData'\n"); |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 229 | /** |
| 230 | * Perform validation of uniforms used across multiple shader stages |
| 231 | */ |
| 232 | bool |
| 233 | cross_validate_uniforms(struct glsl_shader **shaders, unsigned num_shaders) |
| 234 | { |
| 235 | /* Examine all of the uniforms in all of the shaders and cross validate |
| 236 | * them. |
| 237 | */ |
| 238 | glsl_symbol_table uniforms; |
| 239 | for (unsigned i = 0; i < num_shaders; i++) { |
| 240 | foreach_list(node, &shaders[i]->ir) { |
| 241 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 242 | |
| 243 | if ((var == NULL) || (var->mode != ir_var_uniform)) |
| 244 | continue; |
| 245 | |
| 246 | /* If a uniform with this name has already been seen, verify that the |
| 247 | * new instance has the same type. In addition, if the uniforms have |
| 248 | * initializers, the values of the initializers must be the same. |
| 249 | */ |
| 250 | ir_variable *const existing = uniforms.get_variable(var->name); |
| 251 | if (existing != NULL) { |
| 252 | if (var->type != existing->type) { |
| 253 | printf("error: uniform `%s' declared as type `%s' and " |
| 254 | "type `%s'\n", |
| 255 | var->name, var->type->name, existing->type->name); |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | if (var->constant_value != NULL) { |
| 260 | if (existing->constant_value != NULL) { |
| 261 | if (!var->constant_value->has_value(existing->constant_value)) { |
| 262 | printf("error: initializers for uniform `%s' have " |
| 263 | "differing values\n", |
| 264 | var->name); |
| 265 | return false; |
| 266 | } |
| 267 | } else |
| 268 | /* If the first-seen instance of a particular uniform did not |
| 269 | * have an initializer but a later instance does, copy the |
| 270 | * initializer to the version stored in the symbol table. |
| 271 | */ |
| 272 | existing->constant_value = var->constant_value->clone(); |
| 273 | } |
| 274 | } else |
| 275 | uniforms.add_variable(var->name, var); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 283 | /** |
| 284 | * Validate that outputs from one stage match inputs of another |
| 285 | */ |
| 286 | bool |
| 287 | cross_validate_outputs_to_inputs(glsl_shader *producer, glsl_shader *consumer) |
| 288 | { |
| 289 | glsl_symbol_table parameters; |
| 290 | /* FINISHME: Figure these out dynamically. */ |
| 291 | const char *const producer_stage = "vertex"; |
| 292 | const char *const consumer_stage = "fragment"; |
| 293 | |
| 294 | /* Find all shader outputs in the "producer" stage. |
| 295 | */ |
| 296 | foreach_list(node, &producer->ir) { |
| 297 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 298 | |
| 299 | /* FINISHME: For geometry shaders, this should also look for inout |
| 300 | * FINISHME: variables. |
| 301 | */ |
| 302 | if ((var == NULL) || (var->mode != ir_var_out)) |
| 303 | continue; |
| 304 | |
| 305 | parameters.add_variable(var->name, var); |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /* Find all shader inputs in the "consumer" stage. Any variables that have |
| 310 | * matching outputs already in the symbol table must have the same type and |
| 311 | * qualifiers. |
| 312 | */ |
| 313 | foreach_list(node, &consumer->ir) { |
| 314 | ir_variable *const input = ((ir_instruction *) node)->as_variable(); |
| 315 | |
| 316 | /* FINISHME: For geometry shaders, this should also look for inout |
| 317 | * FINISHME: variables. |
| 318 | */ |
| 319 | if ((input == NULL) || (input->mode != ir_var_in)) |
| 320 | continue; |
| 321 | |
| 322 | ir_variable *const output = parameters.get_variable(input->name); |
| 323 | if (output != NULL) { |
| 324 | /* Check that the types match between stages. |
| 325 | */ |
| 326 | if (input->type != output->type) { |
| 327 | printf("error: %s shader output `%s' delcared as type `%s', but " |
| 328 | "%s shader input declared as type `%s'\n", |
| 329 | producer_stage, output->name, output->type->name, |
| 330 | consumer_stage, input->type->name); |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | /* Check that all of the qualifiers match between stages. |
| 335 | */ |
| 336 | if (input->centroid != output->centroid) { |
| 337 | printf("error: %s shader output `%s' %s centroid qualifier, but " |
| 338 | "%s shader input %s centroid qualifier\n", |
| 339 | producer_stage, |
| 340 | output->name, |
| 341 | (output->centroid) ? "has" : "lacks", |
| 342 | consumer_stage, |
| 343 | (input->centroid) ? "has" : "lacks"); |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | if (input->invariant != output->invariant) { |
| 348 | printf("error: %s shader output `%s' %s invariant qualifier, but " |
| 349 | "%s shader input %s invariant qualifier\n", |
| 350 | producer_stage, |
| 351 | output->name, |
| 352 | (output->invariant) ? "has" : "lacks", |
| 353 | consumer_stage, |
| 354 | (input->invariant) ? "has" : "lacks"); |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | if (input->interpolation != output->interpolation) { |
| 359 | printf("error: %s shader output `%s' specifies %s interpolation " |
| 360 | "qualifier, " |
| 361 | "but %s shader input specifies %s interpolation " |
| 362 | "qualifier\n", |
| 363 | producer_stage, |
| 364 | output->name, |
| 365 | output->interpolation_string(), |
| 366 | consumer_stage, |
| 367 | input->interpolation_string()); |
| 368 | return false; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 377 | struct uniform_node { |
| 378 | exec_node link; |
| 379 | struct gl_uniform *u; |
| 380 | unsigned slots; |
| 381 | }; |
| 382 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 383 | void |
| 384 | assign_uniform_locations(struct glsl_program *prog) |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 385 | { |
| 386 | /* */ |
| 387 | exec_list uniforms; |
| 388 | unsigned total_uniforms = 0; |
| 389 | hash_table *ht = hash_table_ctor(32, hash_table_string_hash, |
| 390 | hash_table_string_compare); |
| 391 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 392 | for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) { |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 393 | unsigned next_position = 0; |
| 394 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 395 | foreach_list(node, &prog->_LinkedShaders[i]->ir) { |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 396 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 397 | |
| 398 | if ((var == NULL) || (var->mode != ir_var_uniform)) |
| 399 | continue; |
| 400 | |
| 401 | const unsigned vec4_slots = (var->component_slots() + 3) / 4; |
| 402 | assert(vec4_slots != 0); |
| 403 | |
| 404 | uniform_node *n = (uniform_node *) hash_table_find(ht, var->name); |
| 405 | if (n == NULL) { |
| 406 | n = (uniform_node *) calloc(1, sizeof(struct uniform_node)); |
| 407 | n->u = (gl_uniform *) calloc(vec4_slots, sizeof(struct gl_uniform)); |
| 408 | n->slots = vec4_slots; |
| 409 | |
| 410 | n->u[0].Name = strdup(var->name); |
| 411 | for (unsigned j = 1; j < vec4_slots; j++) |
| 412 | n->u[j].Name = n->u[0].Name; |
| 413 | |
| 414 | hash_table_insert(ht, n, n->u[0].Name); |
| 415 | uniforms.push_tail(& n->link); |
| 416 | total_uniforms += vec4_slots; |
| 417 | } |
| 418 | |
| 419 | if (var->constant_value != NULL) |
| 420 | for (unsigned j = 0; j < vec4_slots; j++) |
| 421 | n->u[j].Initialized = true; |
| 422 | |
| 423 | var->location = next_position; |
| 424 | |
| 425 | for (unsigned j = 0; j < vec4_slots; j++) { |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 426 | switch (prog->_LinkedShaders[i]->Type) { |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 427 | case GL_VERTEX_SHADER: |
| 428 | n->u[j].VertPos = next_position; |
| 429 | break; |
| 430 | case GL_FRAGMENT_SHADER: |
| 431 | n->u[j].FragPos = next_position; |
| 432 | break; |
| 433 | case GL_GEOMETRY_SHADER: |
| 434 | /* FINISHME: Support geometry shaders. */ |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 435 | assert(prog->_LinkedShaders[i]->Type != GL_GEOMETRY_SHADER); |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 436 | break; |
| 437 | } |
| 438 | |
| 439 | next_position++; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | gl_uniform_list *ul = (gl_uniform_list *) |
| 445 | calloc(1, sizeof(gl_uniform_list)); |
| 446 | |
| 447 | ul->Size = total_uniforms; |
| 448 | ul->NumUniforms = total_uniforms; |
| 449 | ul->Uniforms = (gl_uniform *) calloc(total_uniforms, sizeof(gl_uniform)); |
| 450 | |
| 451 | unsigned idx = 0; |
| 452 | uniform_node *next; |
| 453 | for (uniform_node *node = (uniform_node *) uniforms.head |
| 454 | ; node->link.next != NULL |
| 455 | ; node = next) { |
| 456 | next = (uniform_node *) node->link.next; |
| 457 | |
| 458 | node->link.remove(); |
| 459 | memcpy(&ul->Uniforms[idx], node->u, sizeof(gl_uniform) * node->slots); |
| 460 | idx += node->slots; |
| 461 | |
| 462 | free(node->u); |
| 463 | free(node); |
| 464 | } |
| 465 | |
| 466 | hash_table_dtor(ht); |
| 467 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 468 | prog->Uniforms = ul; |
Ian Romanick | 019a59b | 2010-06-21 16:10:42 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame^] | 472 | /** |
| 473 | * Find a contiguous set of available bits in a bitmask |
| 474 | * |
| 475 | * \param used_mask Bits representing used (1) and unused (0) locations |
| 476 | * \param needed_count Number of contiguous bits needed. |
| 477 | * |
| 478 | * \return |
| 479 | * Base location of the available bits on success or -1 on failure. |
| 480 | */ |
| 481 | int |
| 482 | find_available_slots(unsigned used_mask, unsigned needed_count) |
| 483 | { |
| 484 | unsigned needed_mask = (1 << needed_count) - 1; |
| 485 | const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count; |
| 486 | |
| 487 | /* The comparison to 32 is redundant, but without it GCC emits "warning: |
| 488 | * cannot optimize possibly infinite loops" for the loop below. |
| 489 | */ |
| 490 | if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32)) |
| 491 | return -1; |
| 492 | |
| 493 | for (int i = 0; i <= max_bit_to_test; i++) { |
| 494 | if ((needed_mask & ~used_mask) == needed_mask) |
| 495 | return i; |
| 496 | |
| 497 | needed_mask <<= 1; |
| 498 | } |
| 499 | |
| 500 | return -1; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | bool |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 505 | assign_attribute_locations(glsl_shader *sh, |
| 506 | struct gl_program_parameter_list *attrib) |
| 507 | { |
| 508 | unsigned used_locations = 0; |
| 509 | |
| 510 | assert(sh->Type == GL_VERTEX_SHADER); |
| 511 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame^] | 512 | /* Operate in a total of four passes. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 513 | * |
| 514 | * 1. Invalidate the location assignments for all vertex shader inputs. |
| 515 | * |
| 516 | * 2. Assign locations for inputs that have user-defined (via |
| 517 | * glBindVertexAttribLocation) locatoins. |
| 518 | * |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame^] | 519 | * 3. Sort the attributes without assigned locations by number of slots |
| 520 | * required in decreasing order. Fragmentation caused by attribute |
| 521 | * locations assigned by the application may prevent large attributes |
| 522 | * from having enough contiguous space. |
| 523 | * |
| 524 | * 4. Assign locations to any inputs without assigned locations. |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 525 | */ |
| 526 | |
| 527 | invalidate_variable_locations(sh, ir_var_in, VERT_ATTRIB_GENERIC0); |
| 528 | |
| 529 | if (attrib != NULL) { |
| 530 | for (unsigned i = 0; i < attrib->NumParameters; i++) { |
| 531 | ir_variable *const var = |
| 532 | sh->symbols->get_variable(attrib->Parameters[i].Name); |
| 533 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame^] | 534 | /* Note: attributes that occupy multiple slots, such as arrays or |
| 535 | * matrices, may appear in the attrib array multiple times. |
| 536 | */ |
| 537 | if ((var == NULL) || (var->location != -1)) |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 538 | continue; |
| 539 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame^] | 540 | /* From page 61 of the OpenGL 4.0 spec: |
| 541 | * |
| 542 | * "LinkProgram will fail if the attribute bindings assigned by |
| 543 | * BindAttribLocation do not leave not enough space to assign a |
| 544 | * location for an active matrix attribute or an active attribute |
| 545 | * array, both of which require multiple contiguous generic |
| 546 | * attributes." |
| 547 | * |
| 548 | * Previous versions of the spec contain similar language but omit the |
| 549 | * bit about attribute arrays. |
| 550 | * |
| 551 | * Page 61 of the OpenGL 4.0 spec also says: |
| 552 | * |
| 553 | * "It is possible for an application to bind more than one |
| 554 | * attribute name to the same location. This is referred to as |
| 555 | * aliasing. This will only work if only one of the aliased |
| 556 | * attributes is active in the executable program, or if no path |
| 557 | * through the shader consumes more than one attribute of a set |
| 558 | * of attributes aliased to the same location. A link error can |
| 559 | * occur if the linker determines that every path through the |
| 560 | * shader consumes multiple aliased attributes, but |
| 561 | * implementations are not required to generate an error in this |
| 562 | * case." |
| 563 | * |
| 564 | * These two paragraphs are either somewhat contradictory, or I don't |
| 565 | * fully understand one or both of them. |
| 566 | */ |
| 567 | /* FINISHME: The code as currently written does not support attribute |
| 568 | * FINISHME: location aliasing (see comment above). |
| 569 | */ |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 570 | const int attr = attrib->Parameters[i].StateIndexes[0]; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame^] | 571 | const unsigned slots = count_attribute_slots(var->type); |
| 572 | |
| 573 | /* Mask representing the contiguous slots that will be used by this |
| 574 | * attribute. |
| 575 | */ |
| 576 | const unsigned use_mask = (1 << slots) - 1; |
| 577 | |
| 578 | /* Generate a link error if the set of bits requested for this |
| 579 | * attribute overlaps any previously allocated bits. |
| 580 | */ |
| 581 | if ((~(use_mask << attr) & used_locations) != used_locations) { |
| 582 | printf("error: insufficient contiguous attribute locations " |
| 583 | "available for vertex shader input `%s'", |
| 584 | var->name); |
| 585 | return false; |
| 586 | } |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 587 | |
| 588 | var->location = VERT_ATTRIB_GENERIC0 + attr; |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame^] | 589 | used_locations |= (use_mask << attr); |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame^] | 593 | /* Temporary storage for the set of attributes that need locations assigned. |
| 594 | */ |
| 595 | struct temp_attr { |
| 596 | unsigned slots; |
| 597 | ir_variable *var; |
| 598 | |
| 599 | /* Used below in the call to qsort. */ |
| 600 | static int compare(const void *a, const void *b) |
| 601 | { |
| 602 | const temp_attr *const l = (const temp_attr *) a; |
| 603 | const temp_attr *const r = (const temp_attr *) b; |
| 604 | |
| 605 | /* Reversed because we want a descending order sort below. */ |
| 606 | return r->slots - l->slots; |
| 607 | } |
| 608 | } to_assign[16]; |
| 609 | |
| 610 | unsigned num_attr = 0; |
| 611 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 612 | foreach_list(node, &sh->ir) { |
| 613 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 614 | |
| 615 | if ((var == NULL) || (var->mode != ir_var_in)) |
| 616 | continue; |
| 617 | |
| 618 | /* The location was explicitly assigned, nothing to do here. |
| 619 | */ |
| 620 | if (var->location != -1) |
| 621 | continue; |
| 622 | |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame^] | 623 | to_assign[num_attr].slots = count_attribute_slots(var->type); |
| 624 | to_assign[num_attr].var = var; |
| 625 | num_attr++; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 626 | } |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame^] | 627 | |
| 628 | /* If all of the attributes were assigned locations by the application (or |
| 629 | * are built-in attributes with fixed locations), return early. This should |
| 630 | * be the common case. |
| 631 | */ |
| 632 | if (num_attr == 0) |
| 633 | return true; |
| 634 | |
| 635 | qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare); |
| 636 | |
| 637 | for (unsigned i = 0; i < num_attr; i++) { |
| 638 | /* Mask representing the contiguous slots that will be used by this |
| 639 | * attribute. |
| 640 | */ |
| 641 | const unsigned use_mask = (1 << to_assign[i].slots) - 1; |
| 642 | |
| 643 | int location = find_available_slots(used_locations, to_assign[i].slots); |
| 644 | |
| 645 | if (location < 0) { |
| 646 | printf("error: insufficient contiguous attribute locations " |
| 647 | "available for vertex shader input `%s'", |
| 648 | to_assign[i].var->name); |
| 649 | return false; |
| 650 | } |
| 651 | |
| 652 | to_assign[i].var->location = VERT_ATTRIB_GENERIC0 + location; |
| 653 | used_locations |= (use_mask << location); |
| 654 | } |
| 655 | |
| 656 | return true; |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | |
| 660 | void |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 661 | link_shaders(struct glsl_program *prog) |
| 662 | { |
| 663 | prog->LinkStatus = false; |
| 664 | prog->Validated = false; |
| 665 | prog->_Used = false; |
| 666 | |
| 667 | /* Separate the shaders into groups based on their type. |
| 668 | */ |
| 669 | struct glsl_shader **vert_shader_list; |
| 670 | unsigned num_vert_shaders = 0; |
| 671 | struct glsl_shader **frag_shader_list; |
| 672 | unsigned num_frag_shaders = 0; |
| 673 | |
| 674 | vert_shader_list = (struct glsl_shader **) |
Kenneth Graunke | c186b3f | 2010-06-17 15:37:26 -0700 | [diff] [blame] | 675 | calloc(2 * prog->NumShaders, sizeof(struct glsl_shader *)); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 676 | frag_shader_list = &vert_shader_list[prog->NumShaders]; |
| 677 | |
| 678 | for (unsigned i = 0; i < prog->NumShaders; i++) { |
| 679 | switch (prog->Shaders[i]->Type) { |
| 680 | case GL_VERTEX_SHADER: |
| 681 | vert_shader_list[num_vert_shaders] = prog->Shaders[i]; |
| 682 | num_vert_shaders++; |
| 683 | break; |
| 684 | case GL_FRAGMENT_SHADER: |
| 685 | frag_shader_list[num_frag_shaders] = prog->Shaders[i]; |
| 686 | num_frag_shaders++; |
| 687 | break; |
| 688 | case GL_GEOMETRY_SHADER: |
| 689 | /* FINISHME: Support geometry shaders. */ |
| 690 | assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER); |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /* FINISHME: Implement intra-stage linking. */ |
| 696 | assert(num_vert_shaders <= 1); |
| 697 | assert(num_frag_shaders <= 1); |
| 698 | |
| 699 | /* Verify that each of the per-target executables is valid. |
| 700 | */ |
| 701 | if (!validate_vertex_shader_executable(vert_shader_list[0]) |
| 702 | || !validate_fragment_shader_executable(frag_shader_list[0])) |
| 703 | goto done; |
| 704 | |
| 705 | |
| 706 | /* FINISHME: Perform inter-stage linking. */ |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 707 | prog->_LinkedShaders = (struct glsl_shader **) |
| 708 | calloc(2, sizeof(struct glsl_shader *)); |
| 709 | prog->_NumLinkedShaders = 0; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 710 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 711 | if (num_vert_shaders > 0) { |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 712 | prog->_LinkedShaders[prog->_NumLinkedShaders] = vert_shader_list[0]; |
| 713 | prog->_NumLinkedShaders++; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | if (num_frag_shaders > 0) { |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 717 | prog->_LinkedShaders[prog->_NumLinkedShaders] = frag_shader_list[0]; |
| 718 | prog->_NumLinkedShaders++; |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 721 | if (cross_validate_uniforms(prog->_LinkedShaders, prog->_NumLinkedShaders)) { |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 722 | /* Validate the inputs of each stage with the output of the preceeding |
| 723 | * stage. |
| 724 | */ |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 725 | for (unsigned i = 1; i < prog->_NumLinkedShaders; i++) { |
| 726 | if (!cross_validate_outputs_to_inputs(prog->_LinkedShaders[i - 1], |
| 727 | prog->_LinkedShaders[i])) |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 728 | goto done; |
| 729 | } |
| 730 | |
Ian Romanick | cc22c5a | 2010-06-18 17:13:42 -0700 | [diff] [blame] | 731 | prog->LinkStatus = true; |
Ian Romanick | 3710192 | 2010-06-18 19:02:10 -0700 | [diff] [blame] | 732 | } |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 733 | |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 734 | /* FINISHME: Perform whole-program optimization here. */ |
| 735 | |
Ian Romanick | abee16e | 2010-06-21 16:16:05 -0700 | [diff] [blame] | 736 | assign_uniform_locations(prog); |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 737 | |
Ian Romanick | 0ad22cd | 2010-06-21 17:18:31 -0700 | [diff] [blame] | 738 | if (prog->_LinkedShaders[0]->Type == GL_VERTEX_SHADER) |
Ian Romanick | 6984670 | 2010-06-22 17:29:19 -0700 | [diff] [blame^] | 739 | if (!assign_attribute_locations(prog->_LinkedShaders[0], |
| 740 | prog->Attributes)) |
| 741 | goto done; |
Ian Romanick | 13e10e4 | 2010-06-21 12:03:24 -0700 | [diff] [blame] | 742 | |
| 743 | /* FINISHME: Assign vertex shader output / fragment shader input |
| 744 | * FINISHME: locations. |
| 745 | */ |
| 746 | |
| 747 | /* FINISHME: Assign fragment shader output locations. */ |
| 748 | |
| 749 | /* FINISHME: Generate code here. */ |
| 750 | |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 751 | done: |
| 752 | free(vert_shader_list); |
| 753 | } |