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