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