linker.cpp revision 895a5873d141c726e2e81ba53d5b757e4708fb29
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->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      if (ir->return_deref != NULL) {
121	 ir_variable *const var = ir->return_deref->variable_referenced();
122
123	 if (strcmp(name, var->name) == 0) {
124	    found = true;
125	    return visit_stop;
126	 }
127      }
128
129      return visit_continue_with_parent;
130   }
131
132   bool variable_found()
133   {
134      return found;
135   }
136
137private:
138   const char *name;       /**< Find writes to a variable with this name. */
139   bool found;             /**< Was a write to the variable found? */
140};
141
142
143/**
144 * Visitor that determines whether or not a variable is ever read.
145 */
146class find_deref_visitor : public ir_hierarchical_visitor {
147public:
148   find_deref_visitor(const char *name)
149      : name(name), found(false)
150   {
151      /* empty */
152   }
153
154   virtual ir_visitor_status visit(ir_dereference_variable *ir)
155   {
156      if (strcmp(this->name, ir->var->name) == 0) {
157	 this->found = true;
158	 return visit_stop;
159      }
160
161      return visit_continue;
162   }
163
164   bool variable_found() const
165   {
166      return this->found;
167   }
168
169private:
170   const char *name;       /**< Find writes to a variable with this name. */
171   bool found;             /**< Was a write to the variable found? */
172};
173
174
175void
176linker_error(gl_shader_program *prog, const char *fmt, ...)
177{
178   va_list ap;
179
180   ralloc_strcat(&prog->InfoLog, "error: ");
181   va_start(ap, fmt);
182   ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
183   va_end(ap);
184
185   prog->LinkStatus = false;
186}
187
188
189void
190linker_warning(gl_shader_program *prog, const char *fmt, ...)
191{
192   va_list ap;
193
194   ralloc_strcat(&prog->InfoLog, "error: ");
195   va_start(ap, fmt);
196   ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
197   va_end(ap);
198
199}
200
201
202void
203link_invalidate_variable_locations(gl_shader *sh, enum ir_variable_mode mode,
204				   int generic_base)
205{
206   foreach_list(node, sh->ir) {
207      ir_variable *const var = ((ir_instruction *) node)->as_variable();
208
209      if ((var == NULL) || (var->mode != (unsigned) mode))
210	 continue;
211
212      /* Only assign locations for generic attributes / varyings / etc.
213       */
214      if ((var->location >= generic_base) && !var->explicit_location)
215	  var->location = -1;
216   }
217}
218
219
220/**
221 * Determine the number of attribute slots required for a particular type
222 *
223 * This code is here because it implements the language rules of a specific
224 * GLSL version.  Since it's a property of the language and not a property of
225 * types in general, it doesn't really belong in glsl_type.
226 */
227unsigned
228count_attribute_slots(const glsl_type *t)
229{
230   /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
231    *
232    *     "A scalar input counts the same amount against this limit as a vec4,
233    *     so applications may want to consider packing groups of four
234    *     unrelated float inputs together into a vector to better utilize the
235    *     capabilities of the underlying hardware. A matrix input will use up
236    *     multiple locations.  The number of locations used will equal the
237    *     number of columns in the matrix."
238    *
239    * The spec does not explicitly say how arrays are counted.  However, it
240    * should be safe to assume the total number of slots consumed by an array
241    * is the number of entries in the array multiplied by the number of slots
242    * consumed by a single element of the array.
243    */
244
245   if (t->is_array())
246      return t->array_size() * count_attribute_slots(t->element_type());
247
248   if (t->is_matrix())
249      return t->matrix_columns;
250
251   return 1;
252}
253
254
255/**
256 * Verify that a vertex shader executable meets all semantic requirements.
257 *
258 * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
259 * as a side effect.
260 *
261 * \param shader  Vertex shader executable to be verified
262 */
263bool
264validate_vertex_shader_executable(struct gl_shader_program *prog,
265				  struct gl_shader *shader)
266{
267   if (shader == NULL)
268      return true;
269
270   /* From the GLSL 1.10 spec, page 48:
271    *
272    *     "The variable gl_Position is available only in the vertex
273    *      language and is intended for writing the homogeneous vertex
274    *      position. All executions of a well-formed vertex shader
275    *      executable must write a value into this variable. [...] The
276    *      variable gl_Position is available only in the vertex
277    *      language and is intended for writing the homogeneous vertex
278    *      position. All executions of a well-formed vertex shader
279    *      executable must write a value into this variable."
280    *
281    * while in GLSL 1.40 this text is changed to:
282    *
283    *     "The variable gl_Position is available only in the vertex
284    *      language and is intended for writing the homogeneous vertex
285    *      position. It can be written at any time during shader
286    *      execution. It may also be read back by a vertex shader
287    *      after being written. This value will be used by primitive
288    *      assembly, clipping, culling, and other fixed functionality
289    *      operations, if present, that operate on primitives after
290    *      vertex processing has occurred. Its value is undefined if
291    *      the vertex shader executable does not write gl_Position."
292    */
293   if (prog->Version < 140) {
294      find_assignment_visitor find("gl_Position");
295      find.run(shader->ir);
296      if (!find.variable_found()) {
297	 linker_error(prog, "vertex shader does not write to `gl_Position'\n");
298	 return false;
299      }
300   }
301
302   prog->Vert.ClipDistanceArraySize = 0;
303
304   if (prog->Version >= 130) {
305      /* From section 7.1 (Vertex Shader Special Variables) of the
306       * GLSL 1.30 spec:
307       *
308       *   "It is an error for a shader to statically write both
309       *   gl_ClipVertex and gl_ClipDistance."
310       */
311      find_assignment_visitor clip_vertex("gl_ClipVertex");
312      find_assignment_visitor clip_distance("gl_ClipDistance");
313
314      clip_vertex.run(shader->ir);
315      clip_distance.run(shader->ir);
316      if (clip_vertex.variable_found() && clip_distance.variable_found()) {
317         linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
318                      "and `gl_ClipDistance'\n");
319         return false;
320      }
321      prog->Vert.UsesClipDistance = clip_distance.variable_found();
322      ir_variable *clip_distance_var =
323         shader->symbols->get_variable("gl_ClipDistance");
324      if (clip_distance_var)
325         prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
326   }
327
328   return true;
329}
330
331
332/**
333 * Verify that a fragment shader executable meets all semantic requirements
334 *
335 * \param shader  Fragment shader executable to be verified
336 */
337bool
338validate_fragment_shader_executable(struct gl_shader_program *prog,
339				    struct gl_shader *shader)
340{
341   if (shader == NULL)
342      return true;
343
344   find_assignment_visitor frag_color("gl_FragColor");
345   find_assignment_visitor frag_data("gl_FragData");
346
347   frag_color.run(shader->ir);
348   frag_data.run(shader->ir);
349
350   if (frag_color.variable_found() && frag_data.variable_found()) {
351      linker_error(prog,  "fragment shader writes to both "
352		   "`gl_FragColor' and `gl_FragData'\n");
353      return false;
354   }
355
356   return true;
357}
358
359
360/**
361 * Generate a string describing the mode of a variable
362 */
363static const char *
364mode_string(const ir_variable *var)
365{
366   switch (var->mode) {
367   case ir_var_auto:
368      return (var->read_only) ? "global constant" : "global variable";
369
370   case ir_var_uniform: return "uniform";
371   case ir_var_in:      return "shader input";
372   case ir_var_out:     return "shader output";
373   case ir_var_inout:   return "shader inout";
374
375   case ir_var_const_in:
376   case ir_var_temporary:
377   default:
378      assert(!"Should not get here.");
379      return "invalid variable";
380   }
381}
382
383
384/**
385 * Perform validation of global variables used across multiple shaders
386 */
387bool
388cross_validate_globals(struct gl_shader_program *prog,
389		       struct gl_shader **shader_list,
390		       unsigned num_shaders,
391		       bool uniforms_only)
392{
393   /* Examine all of the uniforms in all of the shaders and cross validate
394    * them.
395    */
396   glsl_symbol_table variables;
397   for (unsigned i = 0; i < num_shaders; i++) {
398      if (shader_list[i] == NULL)
399	 continue;
400
401      foreach_list(node, shader_list[i]->ir) {
402	 ir_variable *const var = ((ir_instruction *) node)->as_variable();
403
404	 if (var == NULL)
405	    continue;
406
407	 if (uniforms_only && (var->mode != ir_var_uniform))
408	    continue;
409
410	 /* Don't cross validate temporaries that are at global scope.  These
411	  * will eventually get pulled into the shaders 'main'.
412	  */
413	 if (var->mode == ir_var_temporary)
414	    continue;
415
416	 /* If a global with this name has already been seen, verify that the
417	  * new instance has the same type.  In addition, if the globals have
418	  * initializers, the values of the initializers must be the same.
419	  */
420	 ir_variable *const existing = variables.get_variable(var->name);
421	 if (existing != NULL) {
422	    if (var->type != existing->type) {
423	       /* Consider the types to be "the same" if both types are arrays
424		* of the same type and one of the arrays is implicitly sized.
425		* In addition, set the type of the linked variable to the
426		* explicitly sized array.
427		*/
428	       if (var->type->is_array()
429		   && existing->type->is_array()
430		   && (var->type->fields.array == existing->type->fields.array)
431		   && ((var->type->length == 0)
432		       || (existing->type->length == 0))) {
433		  if (var->type->length != 0) {
434		     existing->type = var->type;
435		  }
436	       } else {
437		  linker_error(prog, "%s `%s' declared as type "
438			       "`%s' and type `%s'\n",
439			       mode_string(var),
440			       var->name, var->type->name,
441			       existing->type->name);
442		  return false;
443	       }
444	    }
445
446	    if (var->explicit_location) {
447	       if (existing->explicit_location
448		   && (var->location != existing->location)) {
449		     linker_error(prog, "explicit locations for %s "
450				  "`%s' have differing values\n",
451				  mode_string(var), var->name);
452		     return false;
453	       }
454
455	       existing->location = var->location;
456	       existing->explicit_location = true;
457	    }
458
459	    /* Validate layout qualifiers for gl_FragDepth.
460	     *
461	     * From the AMD/ARB_conservative_depth specs:
462	     *
463	     *    "If gl_FragDepth is redeclared in any fragment shader in a
464	     *    program, it must be redeclared in all fragment shaders in
465	     *    that program that have static assignments to
466	     *    gl_FragDepth. All redeclarations of gl_FragDepth in all
467	     *    fragment shaders in a single program must have the same set
468	     *    of qualifiers."
469	     */
470	    if (strcmp(var->name, "gl_FragDepth") == 0) {
471	       bool layout_declared = var->depth_layout != ir_depth_layout_none;
472	       bool layout_differs =
473		  var->depth_layout != existing->depth_layout;
474
475	       if (layout_declared && layout_differs) {
476		  linker_error(prog,
477			       "All redeclarations of gl_FragDepth in all "
478			       "fragment shaders in a single program must have "
479			       "the same set of qualifiers.");
480	       }
481
482	       if (var->used && layout_differs) {
483		  linker_error(prog,
484			       "If gl_FragDepth is redeclared with a layout "
485			       "qualifier in any fragment shader, it must be "
486			       "redeclared with the same layout qualifier in "
487			       "all fragment shaders that have assignments to "
488			       "gl_FragDepth");
489	       }
490	    }
491
492	    /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
493	     *
494	     *     "If a shared global has multiple initializers, the
495	     *     initializers must all be constant expressions, and they
496	     *     must all have the same value. Otherwise, a link error will
497	     *     result. (A shared global having only one initializer does
498	     *     not require that initializer to be a constant expression.)"
499	     *
500	     * Previous to 4.20 the GLSL spec simply said that initializers
501	     * must have the same value.  In this case of non-constant
502	     * initializers, this was impossible to determine.  As a result,
503	     * no vendor actually implemented that behavior.  The 4.20
504	     * behavior matches the implemented behavior of at least one other
505	     * vendor, so we'll implement that for all GLSL versions.
506	     */
507	    if (var->constant_initializer != NULL) {
508	       if (existing->constant_initializer != NULL) {
509		  if (!var->constant_initializer->has_value(existing->constant_initializer)) {
510		     linker_error(prog, "initializers for %s "
511				  "`%s' have differing values\n",
512				  mode_string(var), var->name);
513		     return false;
514		  }
515	       } else {
516		  /* If the first-seen instance of a particular uniform did not
517		   * have an initializer but a later instance does, copy the
518		   * initializer to the version stored in the symbol table.
519		   */
520		  /* FINISHME: This is wrong.  The constant_value field should
521		   * FINISHME: not be modified!  Imagine a case where a shader
522		   * FINISHME: without an initializer is linked in two different
523		   * FINISHME: programs with shaders that have differing
524		   * FINISHME: initializers.  Linking with the first will
525		   * FINISHME: modify the shader, and linking with the second
526		   * FINISHME: will fail.
527		   */
528		  existing->constant_initializer =
529		     var->constant_initializer->clone(ralloc_parent(existing),
530						      NULL);
531	       }
532	    }
533
534	    if (var->has_initializer) {
535	       if (existing->has_initializer
536		   && (var->constant_initializer == NULL
537		       || existing->constant_initializer == NULL)) {
538		  linker_error(prog,
539			       "shared global variable `%s' has multiple "
540			       "non-constant initializers.\n",
541			       var->name);
542		  return false;
543	       }
544
545	       /* Some instance had an initializer, so keep track of that.  In
546		* this location, all sorts of initializers (constant or
547		* otherwise) will propagate the existence to the variable
548		* stored in the symbol table.
549		*/
550	       existing->has_initializer = true;
551	    }
552
553	    if (existing->invariant != var->invariant) {
554	       linker_error(prog, "declarations for %s `%s' have "
555			    "mismatching invariant qualifiers\n",
556			    mode_string(var), var->name);
557	       return false;
558	    }
559            if (existing->centroid != var->centroid) {
560               linker_error(prog, "declarations for %s `%s' have "
561			    "mismatching centroid qualifiers\n",
562			    mode_string(var), var->name);
563               return false;
564            }
565	 } else
566	    variables.add_variable(var);
567      }
568   }
569
570   return true;
571}
572
573
574/**
575 * Perform validation of uniforms used across multiple shader stages
576 */
577bool
578cross_validate_uniforms(struct gl_shader_program *prog)
579{
580   return cross_validate_globals(prog, prog->_LinkedShaders,
581				 MESA_SHADER_TYPES, true);
582}
583
584/**
585 * Accumulates the array of prog->UniformBlocks and checks that all
586 * definitons of blocks agree on their contents.
587 */
588static bool
589interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
590{
591   unsigned max_num_uniform_blocks = 0;
592   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
593      if (prog->_LinkedShaders[i])
594	 max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
595   }
596
597   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
598      struct gl_shader *sh = prog->_LinkedShaders[i];
599
600      prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
601						     max_num_uniform_blocks);
602      for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
603	 prog->UniformBlockStageIndex[i][j] = -1;
604
605      if (sh == NULL)
606	 continue;
607
608      for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
609	 int index = link_cross_validate_uniform_block(prog,
610						       &prog->UniformBlocks,
611						       &prog->NumUniformBlocks,
612						       &sh->UniformBlocks[j]);
613
614	 if (index == -1) {
615	    linker_error(prog, "uniform block `%s' has mismatching definitions",
616			 sh->UniformBlocks[j].Name);
617	    return false;
618	 }
619
620	 prog->UniformBlockStageIndex[i][index] = j;
621      }
622   }
623
624   return true;
625}
626
627/**
628 * Validate that outputs from one stage match inputs of another
629 */
630bool
631cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
632				 gl_shader *producer, gl_shader *consumer)
633{
634   glsl_symbol_table parameters;
635   /* FINISHME: Figure these out dynamically. */
636   const char *const producer_stage = "vertex";
637   const char *const consumer_stage = "fragment";
638
639   /* Find all shader outputs in the "producer" stage.
640    */
641   foreach_list(node, producer->ir) {
642      ir_variable *const var = ((ir_instruction *) node)->as_variable();
643
644      /* FINISHME: For geometry shaders, this should also look for inout
645       * FINISHME: variables.
646       */
647      if ((var == NULL) || (var->mode != ir_var_out))
648	 continue;
649
650      parameters.add_variable(var);
651   }
652
653
654   /* Find all shader inputs in the "consumer" stage.  Any variables that have
655    * matching outputs already in the symbol table must have the same type and
656    * qualifiers.
657    */
658   foreach_list(node, consumer->ir) {
659      ir_variable *const input = ((ir_instruction *) node)->as_variable();
660
661      /* FINISHME: For geometry shaders, this should also look for inout
662       * FINISHME: variables.
663       */
664      if ((input == NULL) || (input->mode != ir_var_in))
665	 continue;
666
667      ir_variable *const output = parameters.get_variable(input->name);
668      if (output != NULL) {
669	 /* Check that the types match between stages.
670	  */
671	 if (input->type != output->type) {
672	    /* There is a bit of a special case for gl_TexCoord.  This
673	     * built-in is unsized by default.  Applications that variable
674	     * access it must redeclare it with a size.  There is some
675	     * language in the GLSL spec that implies the fragment shader
676	     * and vertex shader do not have to agree on this size.  Other
677	     * driver behave this way, and one or two applications seem to
678	     * rely on it.
679	     *
680	     * Neither declaration needs to be modified here because the array
681	     * sizes are fixed later when update_array_sizes is called.
682	     *
683	     * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
684	     *
685	     *     "Unlike user-defined varying variables, the built-in
686	     *     varying variables don't have a strict one-to-one
687	     *     correspondence between the vertex language and the
688	     *     fragment language."
689	     */
690	    if (!output->type->is_array()
691		|| (strncmp("gl_", output->name, 3) != 0)) {
692	       linker_error(prog,
693			    "%s shader output `%s' declared as type `%s', "
694			    "but %s shader input declared as type `%s'\n",
695			    producer_stage, output->name,
696			    output->type->name,
697			    consumer_stage, input->type->name);
698	       return false;
699	    }
700	 }
701
702	 /* Check that all of the qualifiers match between stages.
703	  */
704	 if (input->centroid != output->centroid) {
705	    linker_error(prog,
706			 "%s shader output `%s' %s centroid qualifier, "
707			 "but %s shader input %s centroid qualifier\n",
708			 producer_stage,
709			 output->name,
710			 (output->centroid) ? "has" : "lacks",
711			 consumer_stage,
712			 (input->centroid) ? "has" : "lacks");
713	    return false;
714	 }
715
716	 if (input->invariant != output->invariant) {
717	    linker_error(prog,
718			 "%s shader output `%s' %s invariant qualifier, "
719			 "but %s shader input %s invariant qualifier\n",
720			 producer_stage,
721			 output->name,
722			 (output->invariant) ? "has" : "lacks",
723			 consumer_stage,
724			 (input->invariant) ? "has" : "lacks");
725	    return false;
726	 }
727
728	 if (input->interpolation != output->interpolation) {
729	    linker_error(prog,
730			 "%s shader output `%s' specifies %s "
731			 "interpolation qualifier, "
732			 "but %s shader input specifies %s "
733			 "interpolation qualifier\n",
734			 producer_stage,
735			 output->name,
736			 output->interpolation_string(),
737			 consumer_stage,
738			 input->interpolation_string());
739	    return false;
740	 }
741      }
742   }
743
744   return true;
745}
746
747
748/**
749 * Populates a shaders symbol table with all global declarations
750 */
751static void
752populate_symbol_table(gl_shader *sh)
753{
754   sh->symbols = new(sh) glsl_symbol_table;
755
756   foreach_list(node, sh->ir) {
757      ir_instruction *const inst = (ir_instruction *) node;
758      ir_variable *var;
759      ir_function *func;
760
761      if ((func = inst->as_function()) != NULL) {
762	 sh->symbols->add_function(func);
763      } else if ((var = inst->as_variable()) != NULL) {
764	 sh->symbols->add_variable(var);
765      }
766   }
767}
768
769
770/**
771 * Remap variables referenced in an instruction tree
772 *
773 * This is used when instruction trees are cloned from one shader and placed in
774 * another.  These trees will contain references to \c ir_variable nodes that
775 * do not exist in the target shader.  This function finds these \c ir_variable
776 * references and replaces the references with matching variables in the target
777 * shader.
778 *
779 * If there is no matching variable in the target shader, a clone of the
780 * \c ir_variable is made and added to the target shader.  The new variable is
781 * added to \b both the instruction stream and the symbol table.
782 *
783 * \param inst         IR tree that is to be processed.
784 * \param symbols      Symbol table containing global scope symbols in the
785 *                     linked shader.
786 * \param instructions Instruction stream where new variable declarations
787 *                     should be added.
788 */
789void
790remap_variables(ir_instruction *inst, struct gl_shader *target,
791		hash_table *temps)
792{
793   class remap_visitor : public ir_hierarchical_visitor {
794   public:
795	 remap_visitor(struct gl_shader *target,
796		    hash_table *temps)
797      {
798	 this->target = target;
799	 this->symbols = target->symbols;
800	 this->instructions = target->ir;
801	 this->temps = temps;
802      }
803
804      virtual ir_visitor_status visit(ir_dereference_variable *ir)
805      {
806	 if (ir->var->mode == ir_var_temporary) {
807	    ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
808
809	    assert(var != NULL);
810	    ir->var = var;
811	    return visit_continue;
812	 }
813
814	 ir_variable *const existing =
815	    this->symbols->get_variable(ir->var->name);
816	 if (existing != NULL)
817	    ir->var = existing;
818	 else {
819	    ir_variable *copy = ir->var->clone(this->target, NULL);
820
821	    this->symbols->add_variable(copy);
822	    this->instructions->push_head(copy);
823	    ir->var = copy;
824	 }
825
826	 return visit_continue;
827      }
828
829   private:
830      struct gl_shader *target;
831      glsl_symbol_table *symbols;
832      exec_list *instructions;
833      hash_table *temps;
834   };
835
836   remap_visitor v(target, temps);
837
838   inst->accept(&v);
839}
840
841
842/**
843 * Move non-declarations from one instruction stream to another
844 *
845 * The intended usage pattern of this function is to pass the pointer to the
846 * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
847 * pointer) for \c last and \c false for \c make_copies on the first
848 * call.  Successive calls pass the return value of the previous call for
849 * \c last and \c true for \c make_copies.
850 *
851 * \param instructions Source instruction stream
852 * \param last         Instruction after which new instructions should be
853 *                     inserted in the target instruction stream
854 * \param make_copies  Flag selecting whether instructions in \c instructions
855 *                     should be copied (via \c ir_instruction::clone) into the
856 *                     target list or moved.
857 *
858 * \return
859 * The new "last" instruction in the target instruction stream.  This pointer
860 * is suitable for use as the \c last parameter of a later call to this
861 * function.
862 */
863exec_node *
864move_non_declarations(exec_list *instructions, exec_node *last,
865		      bool make_copies, gl_shader *target)
866{
867   hash_table *temps = NULL;
868
869   if (make_copies)
870      temps = hash_table_ctor(0, hash_table_pointer_hash,
871			      hash_table_pointer_compare);
872
873   foreach_list_safe(node, instructions) {
874      ir_instruction *inst = (ir_instruction *) node;
875
876      if (inst->as_function())
877	 continue;
878
879      ir_variable *var = inst->as_variable();
880      if ((var != NULL) && (var->mode != ir_var_temporary))
881	 continue;
882
883      assert(inst->as_assignment()
884             || inst->as_call()
885             || inst->as_if() /* for initializers with the ?: operator */
886	     || ((var != NULL) && (var->mode == ir_var_temporary)));
887
888      if (make_copies) {
889	 inst = inst->clone(target, NULL);
890
891	 if (var != NULL)
892	    hash_table_insert(temps, inst, var);
893	 else
894	    remap_variables(inst, target, temps);
895      } else {
896	 inst->remove();
897      }
898
899      last->insert_after(inst);
900      last = inst;
901   }
902
903   if (make_copies)
904      hash_table_dtor(temps);
905
906   return last;
907}
908
909/**
910 * Get the function signature for main from a shader
911 */
912static ir_function_signature *
913get_main_function_signature(gl_shader *sh)
914{
915   ir_function *const f = sh->symbols->get_function("main");
916   if (f != NULL) {
917      exec_list void_parameters;
918
919      /* Look for the 'void main()' signature and ensure that it's defined.
920       * This keeps the linker from accidentally pick a shader that just
921       * contains a prototype for main.
922       *
923       * We don't have to check for multiple definitions of main (in multiple
924       * shaders) because that would have already been caught above.
925       */
926      ir_function_signature *sig = f->matching_signature(&void_parameters);
927      if ((sig != NULL) && sig->is_defined) {
928	 return sig;
929      }
930   }
931
932   return NULL;
933}
934
935
936/**
937 * This class is only used in link_intrastage_shaders() below but declaring
938 * it inside that function leads to compiler warnings with some versions of
939 * gcc.
940 */
941class array_sizing_visitor : public ir_hierarchical_visitor {
942public:
943   virtual ir_visitor_status visit(ir_variable *var)
944   {
945      if (var->type->is_array() && (var->type->length == 0)) {
946         const glsl_type *type =
947            glsl_type::get_array_instance(var->type->fields.array,
948                                          var->max_array_access + 1);
949         assert(type != NULL);
950         var->type = type;
951      }
952      return visit_continue;
953   }
954};
955
956/**
957 * Combine a group of shaders for a single stage to generate a linked shader
958 *
959 * \note
960 * If this function is supplied a single shader, it is cloned, and the new
961 * shader is returned.
962 */
963static struct gl_shader *
964link_intrastage_shaders(void *mem_ctx,
965			struct gl_context *ctx,
966			struct gl_shader_program *prog,
967			struct gl_shader **shader_list,
968			unsigned num_shaders)
969{
970   struct gl_uniform_block *uniform_blocks = NULL;
971   unsigned num_uniform_blocks = 0;
972
973   /* Check that global variables defined in multiple shaders are consistent.
974    */
975   if (!cross_validate_globals(prog, shader_list, num_shaders, false))
976      return NULL;
977
978   /* Check that uniform blocks between shaders for a stage agree. */
979   for (unsigned i = 0; i < num_shaders; i++) {
980      struct gl_shader *sh = shader_list[i];
981
982      for (unsigned j = 0; j < shader_list[i]->NumUniformBlocks; j++) {
983	 link_assign_uniform_block_offsets(shader_list[i]);
984
985	 int index = link_cross_validate_uniform_block(mem_ctx,
986						       &uniform_blocks,
987						       &num_uniform_blocks,
988						       &sh->UniformBlocks[j]);
989	 if (index == -1) {
990	    linker_error(prog, "uniform block `%s' has mismatching definitions",
991			 sh->UniformBlocks[j].Name);
992	    return NULL;
993	 }
994      }
995   }
996
997   /* Check that there is only a single definition of each function signature
998    * across all shaders.
999    */
1000   for (unsigned i = 0; i < (num_shaders - 1); i++) {
1001      foreach_list(node, shader_list[i]->ir) {
1002	 ir_function *const f = ((ir_instruction *) node)->as_function();
1003
1004	 if (f == NULL)
1005	    continue;
1006
1007	 for (unsigned j = i + 1; j < num_shaders; j++) {
1008	    ir_function *const other =
1009	       shader_list[j]->symbols->get_function(f->name);
1010
1011	    /* If the other shader has no function (and therefore no function
1012	     * signatures) with the same name, skip to the next shader.
1013	     */
1014	    if (other == NULL)
1015	       continue;
1016
1017	    foreach_iter (exec_list_iterator, iter, *f) {
1018	       ir_function_signature *sig =
1019		  (ir_function_signature *) iter.get();
1020
1021	       if (!sig->is_defined || sig->is_builtin)
1022		  continue;
1023
1024	       ir_function_signature *other_sig =
1025		  other->exact_matching_signature(& sig->parameters);
1026
1027	       if ((other_sig != NULL) && other_sig->is_defined
1028		   && !other_sig->is_builtin) {
1029		  linker_error(prog, "function `%s' is multiply defined",
1030			       f->name);
1031		  return NULL;
1032	       }
1033	    }
1034	 }
1035      }
1036   }
1037
1038   /* Find the shader that defines main, and make a clone of it.
1039    *
1040    * Starting with the clone, search for undefined references.  If one is
1041    * found, find the shader that defines it.  Clone the reference and add
1042    * it to the shader.  Repeat until there are no undefined references or
1043    * until a reference cannot be resolved.
1044    */
1045   gl_shader *main = NULL;
1046   for (unsigned i = 0; i < num_shaders; i++) {
1047      if (get_main_function_signature(shader_list[i]) != NULL) {
1048	 main = shader_list[i];
1049	 break;
1050      }
1051   }
1052
1053   if (main == NULL) {
1054      linker_error(prog, "%s shader lacks `main'\n",
1055		   (shader_list[0]->Type == GL_VERTEX_SHADER)
1056		   ? "vertex" : "fragment");
1057      return NULL;
1058   }
1059
1060   gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
1061   linked->ir = new(linked) exec_list;
1062   clone_ir_list(mem_ctx, linked->ir, main->ir);
1063
1064   linked->UniformBlocks = uniform_blocks;
1065   linked->NumUniformBlocks = num_uniform_blocks;
1066   ralloc_steal(linked, linked->UniformBlocks);
1067
1068   populate_symbol_table(linked);
1069
1070   /* The a pointer to the main function in the final linked shader (i.e., the
1071    * copy of the original shader that contained the main function).
1072    */
1073   ir_function_signature *const main_sig = get_main_function_signature(linked);
1074
1075   /* Move any instructions other than variable declarations or function
1076    * declarations into main.
1077    */
1078   exec_node *insertion_point =
1079      move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
1080			    linked);
1081
1082   for (unsigned i = 0; i < num_shaders; i++) {
1083      if (shader_list[i] == main)
1084	 continue;
1085
1086      insertion_point = move_non_declarations(shader_list[i]->ir,
1087					      insertion_point, true, linked);
1088   }
1089
1090   /* Resolve initializers for global variables in the linked shader.
1091    */
1092   unsigned num_linking_shaders = num_shaders;
1093   for (unsigned i = 0; i < num_shaders; i++)
1094      num_linking_shaders += shader_list[i]->num_builtins_to_link;
1095
1096   gl_shader **linking_shaders =
1097      (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
1098
1099   memcpy(linking_shaders, shader_list,
1100	  sizeof(linking_shaders[0]) * num_shaders);
1101
1102   unsigned idx = num_shaders;
1103   for (unsigned i = 0; i < num_shaders; i++) {
1104      memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
1105	     sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
1106      idx += shader_list[i]->num_builtins_to_link;
1107   }
1108
1109   assert(idx == num_linking_shaders);
1110
1111   if (!link_function_calls(prog, linked, linking_shaders,
1112			    num_linking_shaders)) {
1113      ctx->Driver.DeleteShader(ctx, linked);
1114      linked = NULL;
1115   }
1116
1117   free(linking_shaders);
1118
1119#ifdef DEBUG
1120   /* At this point linked should contain all of the linked IR, so
1121    * validate it to make sure nothing went wrong.
1122    */
1123   if (linked)
1124      validate_ir_tree(linked->ir);
1125#endif
1126
1127   /* Make a pass over all variable declarations to ensure that arrays with
1128    * unspecified sizes have a size specified.  The size is inferred from the
1129    * max_array_access field.
1130    */
1131   if (linked != NULL) {
1132      array_sizing_visitor v;
1133
1134      v.run(linked->ir);
1135   }
1136
1137   return linked;
1138}
1139
1140/**
1141 * Update the sizes of linked shader uniform arrays to the maximum
1142 * array index used.
1143 *
1144 * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
1145 *
1146 *     If one or more elements of an array are active,
1147 *     GetActiveUniform will return the name of the array in name,
1148 *     subject to the restrictions listed above. The type of the array
1149 *     is returned in type. The size parameter contains the highest
1150 *     array element index used, plus one. The compiler or linker
1151 *     determines the highest index used.  There will be only one
1152 *     active uniform reported by the GL per uniform array.
1153
1154 */
1155static void
1156update_array_sizes(struct gl_shader_program *prog)
1157{
1158   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
1159	 if (prog->_LinkedShaders[i] == NULL)
1160	    continue;
1161
1162      foreach_list(node, prog->_LinkedShaders[i]->ir) {
1163	 ir_variable *const var = ((ir_instruction *) node)->as_variable();
1164
1165	 if ((var == NULL) || (var->mode != ir_var_uniform &&
1166			       var->mode != ir_var_in &&
1167			       var->mode != ir_var_out) ||
1168	     !var->type->is_array())
1169	    continue;
1170
1171	 /* GL_ARB_uniform_buffer_object says that std140 uniforms
1172	  * will not be eliminated.  Since we always do std140, just
1173	  * don't resize arrays in UBOs.
1174	  */
1175	 if (var->uniform_block != -1)
1176	    continue;
1177
1178	 unsigned int size = var->max_array_access;
1179	 for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
1180	       if (prog->_LinkedShaders[j] == NULL)
1181		  continue;
1182
1183	    foreach_list(node2, prog->_LinkedShaders[j]->ir) {
1184	       ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
1185	       if (!other_var)
1186		  continue;
1187
1188	       if (strcmp(var->name, other_var->name) == 0 &&
1189		   other_var->max_array_access > size) {
1190		  size = other_var->max_array_access;
1191	       }
1192	    }
1193	 }
1194
1195	 if (size + 1 != var->type->fields.array->length) {
1196	    /* If this is a built-in uniform (i.e., it's backed by some
1197	     * fixed-function state), adjust the number of state slots to
1198	     * match the new array size.  The number of slots per array entry
1199	     * is not known.  It seems safe to assume that the total number of
1200	     * slots is an integer multiple of the number of array elements.
1201	     * Determine the number of slots per array element by dividing by
1202	     * the old (total) size.
1203	     */
1204	    if (var->num_state_slots > 0) {
1205	       var->num_state_slots = (size + 1)
1206		  * (var->num_state_slots / var->type->length);
1207	    }
1208
1209	    var->type = glsl_type::get_array_instance(var->type->fields.array,
1210						      size + 1);
1211	    /* FINISHME: We should update the types of array
1212	     * dereferences of this variable now.
1213	     */
1214	 }
1215      }
1216   }
1217}
1218
1219/**
1220 * Find a contiguous set of available bits in a bitmask.
1221 *
1222 * \param used_mask     Bits representing used (1) and unused (0) locations
1223 * \param needed_count  Number of contiguous bits needed.
1224 *
1225 * \return
1226 * Base location of the available bits on success or -1 on failure.
1227 */
1228int
1229find_available_slots(unsigned used_mask, unsigned needed_count)
1230{
1231   unsigned needed_mask = (1 << needed_count) - 1;
1232   const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
1233
1234   /* The comparison to 32 is redundant, but without it GCC emits "warning:
1235    * cannot optimize possibly infinite loops" for the loop below.
1236    */
1237   if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
1238      return -1;
1239
1240   for (int i = 0; i <= max_bit_to_test; i++) {
1241      if ((needed_mask & ~used_mask) == needed_mask)
1242	 return i;
1243
1244      needed_mask <<= 1;
1245   }
1246
1247   return -1;
1248}
1249
1250
1251/**
1252 * Assign locations for either VS inputs for FS outputs
1253 *
1254 * \param prog          Shader program whose variables need locations assigned
1255 * \param target_index  Selector for the program target to receive location
1256 *                      assignmnets.  Must be either \c MESA_SHADER_VERTEX or
1257 *                      \c MESA_SHADER_FRAGMENT.
1258 * \param max_index     Maximum number of generic locations.  This corresponds
1259 *                      to either the maximum number of draw buffers or the
1260 *                      maximum number of generic attributes.
1261 *
1262 * \return
1263 * If locations are successfully assigned, true is returned.  Otherwise an
1264 * error is emitted to the shader link log and false is returned.
1265 */
1266bool
1267assign_attribute_or_color_locations(gl_shader_program *prog,
1268				    unsigned target_index,
1269				    unsigned max_index)
1270{
1271   /* Mark invalid locations as being used.
1272    */
1273   unsigned used_locations = (max_index >= 32)
1274      ? ~0 : ~((1 << max_index) - 1);
1275
1276   assert((target_index == MESA_SHADER_VERTEX)
1277	  || (target_index == MESA_SHADER_FRAGMENT));
1278
1279   gl_shader *const sh = prog->_LinkedShaders[target_index];
1280   if (sh == NULL)
1281      return true;
1282
1283   /* Operate in a total of four passes.
1284    *
1285    * 1. Invalidate the location assignments for all vertex shader inputs.
1286    *
1287    * 2. Assign locations for inputs that have user-defined (via
1288    *    glBindVertexAttribLocation) locations and outputs that have
1289    *    user-defined locations (via glBindFragDataLocation).
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   link_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      } else if (target_index == MESA_SHADER_FRAGMENT) {
1351	 unsigned binding;
1352	 unsigned index;
1353
1354	 if (prog->FragDataBindings->get(binding, var->name)) {
1355	    assert(binding >= FRAG_RESULT_DATA0);
1356	    var->location = binding;
1357
1358	    if (prog->FragDataIndexBindings->get(index, var->name)) {
1359	       var->index = index;
1360	    }
1361	 }
1362      }
1363
1364      /* If the variable is not a built-in and has a location statically
1365       * assigned in the shader (presumably via a layout qualifier), make sure
1366       * that it doesn't collide with other assigned locations.  Otherwise,
1367       * add it to the list of variables that need linker-assigned locations.
1368       */
1369      const unsigned slots = count_attribute_slots(var->type);
1370      if (var->location != -1) {
1371	 if (var->location >= generic_base && var->index < 1) {
1372	    /* From page 61 of the OpenGL 4.0 spec:
1373	     *
1374	     *     "LinkProgram will fail if the attribute bindings assigned
1375	     *     by BindAttribLocation do not leave not enough space to
1376	     *     assign a location for an active matrix attribute or an
1377	     *     active attribute array, both of which require multiple
1378	     *     contiguous generic attributes."
1379	     *
1380	     * Previous versions of the spec contain similar language but omit
1381	     * the bit about attribute arrays.
1382	     *
1383	     * Page 61 of the OpenGL 4.0 spec also says:
1384	     *
1385	     *     "It is possible for an application to bind more than one
1386	     *     attribute name to the same location. This is referred to as
1387	     *     aliasing. This will only work if only one of the aliased
1388	     *     attributes is active in the executable program, or if no
1389	     *     path through the shader consumes more than one attribute of
1390	     *     a set of attributes aliased to the same location. A link
1391	     *     error can occur if the linker determines that every path
1392	     *     through the shader consumes multiple aliased attributes,
1393	     *     but implementations are not required to generate an error
1394	     *     in this case."
1395	     *
1396	     * These two paragraphs are either somewhat contradictory, or I
1397	     * don't fully understand one or both of them.
1398	     */
1399	    /* FINISHME: The code as currently written does not support
1400	     * FINISHME: attribute location aliasing (see comment above).
1401	     */
1402	    /* Mask representing the contiguous slots that will be used by
1403	     * this attribute.
1404	     */
1405	    const unsigned attr = var->location - generic_base;
1406	    const unsigned use_mask = (1 << slots) - 1;
1407
1408	    /* Generate a link error if the set of bits requested for this
1409	     * attribute overlaps any previously allocated bits.
1410	     */
1411	    if ((~(use_mask << attr) & used_locations) != used_locations) {
1412	       const char *const string = (target_index == MESA_SHADER_VERTEX)
1413		  ? "vertex shader input" : "fragment shader output";
1414	       linker_error(prog,
1415			    "insufficient contiguous locations "
1416			    "available for %s `%s' %d %d %d", string,
1417			    var->name, used_locations, use_mask, attr);
1418	       return false;
1419	    }
1420
1421	    used_locations |= (use_mask << attr);
1422	 }
1423
1424	 continue;
1425      }
1426
1427      to_assign[num_attr].slots = slots;
1428      to_assign[num_attr].var = var;
1429      num_attr++;
1430   }
1431
1432   /* If all of the attributes were assigned locations by the application (or
1433    * are built-in attributes with fixed locations), return early.  This should
1434    * be the common case.
1435    */
1436   if (num_attr == 0)
1437      return true;
1438
1439   qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
1440
1441   if (target_index == MESA_SHADER_VERTEX) {
1442      /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS.  It can
1443       * only be explicitly assigned by via glBindAttribLocation.  Mark it as
1444       * reserved to prevent it from being automatically allocated below.
1445       */
1446      find_deref_visitor find("gl_Vertex");
1447      find.run(sh->ir);
1448      if (find.variable_found())
1449	 used_locations |= (1 << 0);
1450   }
1451
1452   for (unsigned i = 0; i < num_attr; i++) {
1453      /* Mask representing the contiguous slots that will be used by this
1454       * attribute.
1455       */
1456      const unsigned use_mask = (1 << to_assign[i].slots) - 1;
1457
1458      int location = find_available_slots(used_locations, to_assign[i].slots);
1459
1460      if (location < 0) {
1461	 const char *const string = (target_index == MESA_SHADER_VERTEX)
1462	    ? "vertex shader input" : "fragment shader output";
1463
1464	 linker_error(prog,
1465		      "insufficient contiguous locations "
1466		      "available for %s `%s'",
1467		      string, to_assign[i].var->name);
1468	 return false;
1469      }
1470
1471      to_assign[i].var->location = generic_base + location;
1472      used_locations |= (use_mask << location);
1473   }
1474
1475   return true;
1476}
1477
1478
1479/**
1480 * Demote shader inputs and outputs that are not used in other stages
1481 */
1482void
1483demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
1484{
1485   foreach_list(node, sh->ir) {
1486      ir_variable *const var = ((ir_instruction *) node)->as_variable();
1487
1488      if ((var == NULL) || (var->mode != int(mode)))
1489	 continue;
1490
1491      /* A shader 'in' or 'out' variable is only really an input or output if
1492       * its value is used by other shader stages.  This will cause the variable
1493       * to have a location assigned.
1494       */
1495      if (var->location == -1) {
1496	 var->mode = ir_var_auto;
1497      }
1498   }
1499}
1500
1501
1502/**
1503 * Data structure tracking information about a transform feedback declaration
1504 * during linking.
1505 */
1506class tfeedback_decl
1507{
1508public:
1509   bool init(struct gl_context *ctx, struct gl_shader_program *prog,
1510             const void *mem_ctx, const char *input);
1511   static bool is_same(const tfeedback_decl &x, const tfeedback_decl &y);
1512   bool assign_location(struct gl_context *ctx, struct gl_shader_program *prog,
1513                        ir_variable *output_var);
1514   bool accumulate_num_outputs(struct gl_shader_program *prog, unsigned *count);
1515   bool store(struct gl_context *ctx, struct gl_shader_program *prog,
1516              struct gl_transform_feedback_info *info, unsigned buffer,
1517              const unsigned max_outputs) const;
1518
1519   /**
1520    * True if assign_location() has been called for this object.
1521    */
1522   bool is_assigned() const
1523   {
1524      return this->location != -1;
1525   }
1526
1527   bool is_next_buffer_separator() const
1528   {
1529      return this->next_buffer_separator;
1530   }
1531
1532   bool is_varying() const
1533   {
1534      return !this->next_buffer_separator && !this->skip_components;
1535   }
1536
1537   /**
1538    * Determine whether this object refers to the variable var.
1539    */
1540   bool matches_var(ir_variable *var) const
1541   {
1542      if (this->is_clip_distance_mesa)
1543         return strcmp(var->name, "gl_ClipDistanceMESA") == 0;
1544      else
1545         return strcmp(var->name, this->var_name) == 0;
1546   }
1547
1548   /**
1549    * The total number of varying components taken up by this variable.  Only
1550    * valid if is_assigned() is true.
1551    */
1552   unsigned num_components() const
1553   {
1554      if (this->is_clip_distance_mesa)
1555         return this->size;
1556      else
1557         return this->vector_elements * this->matrix_columns * this->size;
1558   }
1559
1560private:
1561   /**
1562    * The name that was supplied to glTransformFeedbackVaryings.  Used for
1563    * error reporting and glGetTransformFeedbackVarying().
1564    */
1565   const char *orig_name;
1566
1567   /**
1568    * The name of the variable, parsed from orig_name.
1569    */
1570   const char *var_name;
1571
1572   /**
1573    * True if the declaration in orig_name represents an array.
1574    */
1575   bool is_subscripted;
1576
1577   /**
1578    * If is_subscripted is true, the subscript that was specified in orig_name.
1579    */
1580   unsigned array_subscript;
1581
1582   /**
1583    * True if the variable is gl_ClipDistance and the driver lowers
1584    * gl_ClipDistance to gl_ClipDistanceMESA.
1585    */
1586   bool is_clip_distance_mesa;
1587
1588   /**
1589    * The vertex shader output location that the linker assigned for this
1590    * variable.  -1 if a location hasn't been assigned yet.
1591    */
1592   int location;
1593
1594   /**
1595    * If location != -1, the number of vector elements in this variable, or 1
1596    * if this variable is a scalar.
1597    */
1598   unsigned vector_elements;
1599
1600   /**
1601    * If location != -1, the number of matrix columns in this variable, or 1
1602    * if this variable is not a matrix.
1603    */
1604   unsigned matrix_columns;
1605
1606   /** Type of the varying returned by glGetTransformFeedbackVarying() */
1607   GLenum type;
1608
1609   /**
1610    * If location != -1, the size that should be returned by
1611    * glGetTransformFeedbackVarying().
1612    */
1613   unsigned size;
1614
1615   /**
1616    * How many components to skip. If non-zero, this is
1617    * gl_SkipComponents{1,2,3,4} from ARB_transform_feedback3.
1618    */
1619   unsigned skip_components;
1620
1621   /**
1622    * Whether this is gl_NextBuffer from ARB_transform_feedback3.
1623    */
1624   bool next_buffer_separator;
1625};
1626
1627
1628/**
1629 * Initialize this object based on a string that was passed to
1630 * glTransformFeedbackVaryings.  If there is a parse error, the error is
1631 * reported using linker_error(), and false is returned.
1632 */
1633bool
1634tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
1635                     const void *mem_ctx, const char *input)
1636{
1637   /* We don't have to be pedantic about what is a valid GLSL variable name,
1638    * because any variable with an invalid name can't exist in the IR anyway.
1639    */
1640
1641   this->location = -1;
1642   this->orig_name = input;
1643   this->is_clip_distance_mesa = false;
1644   this->skip_components = 0;
1645   this->next_buffer_separator = false;
1646
1647   if (ctx->Extensions.ARB_transform_feedback3) {
1648      /* Parse gl_NextBuffer. */
1649      if (strcmp(input, "gl_NextBuffer") == 0) {
1650         this->next_buffer_separator = true;
1651         return true;
1652      }
1653
1654      /* Parse gl_SkipComponents. */
1655      if (strcmp(input, "gl_SkipComponents1") == 0)
1656         this->skip_components = 1;
1657      else if (strcmp(input, "gl_SkipComponents2") == 0)
1658         this->skip_components = 2;
1659      else if (strcmp(input, "gl_SkipComponents3") == 0)
1660         this->skip_components = 3;
1661      else if (strcmp(input, "gl_SkipComponents4") == 0)
1662         this->skip_components = 4;
1663
1664      if (this->skip_components)
1665         return true;
1666   }
1667
1668   /* Parse a declaration. */
1669   const char *bracket = strrchr(input, '[');
1670
1671   if (bracket) {
1672      this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);
1673      if (sscanf(bracket, "[%u]", &this->array_subscript) != 1) {
1674         linker_error(prog, "Cannot parse transform feedback varying %s", input);
1675         return false;
1676      }
1677      this->is_subscripted = true;
1678   } else {
1679      this->var_name = ralloc_strdup(mem_ctx, input);
1680      this->is_subscripted = false;
1681   }
1682
1683   /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
1684    * class must behave specially to account for the fact that gl_ClipDistance
1685    * is converted from a float[8] to a vec4[2].
1686    */
1687   if (ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance &&
1688       strcmp(this->var_name, "gl_ClipDistance") == 0) {
1689      this->is_clip_distance_mesa = true;
1690   }
1691
1692   return true;
1693}
1694
1695
1696/**
1697 * Determine whether two tfeedback_decl objects refer to the same variable and
1698 * array index (if applicable).
1699 */
1700bool
1701tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
1702{
1703   assert(x.is_varying() && y.is_varying());
1704
1705   if (strcmp(x.var_name, y.var_name) != 0)
1706      return false;
1707   if (x.is_subscripted != y.is_subscripted)
1708      return false;
1709   if (x.is_subscripted && x.array_subscript != y.array_subscript)
1710      return false;
1711   return true;
1712}
1713
1714
1715/**
1716 * Assign a location for this tfeedback_decl object based on the location
1717 * assignment in output_var.
1718 *
1719 * If an error occurs, the error is reported through linker_error() and false
1720 * is returned.
1721 */
1722bool
1723tfeedback_decl::assign_location(struct gl_context *ctx,
1724                                struct gl_shader_program *prog,
1725                                ir_variable *output_var)
1726{
1727   assert(this->is_varying());
1728
1729   if (output_var->type->is_array()) {
1730      /* Array variable */
1731      const unsigned matrix_cols =
1732         output_var->type->fields.array->matrix_columns;
1733      unsigned actual_array_size = this->is_clip_distance_mesa ?
1734         prog->Vert.ClipDistanceArraySize : output_var->type->array_size();
1735
1736      if (this->is_subscripted) {
1737         /* Check array bounds. */
1738         if (this->array_subscript >= actual_array_size) {
1739            linker_error(prog, "Transform feedback varying %s has index "
1740                         "%i, but the array size is %u.",
1741                         this->orig_name, this->array_subscript,
1742                         actual_array_size);
1743            return false;
1744         }
1745         if (this->is_clip_distance_mesa) {
1746            this->location =
1747               output_var->location + this->array_subscript / 4;
1748         } else {
1749            this->location =
1750               output_var->location + this->array_subscript * matrix_cols;
1751         }
1752         this->size = 1;
1753      } else {
1754         this->location = output_var->location;
1755         this->size = actual_array_size;
1756      }
1757      this->vector_elements = output_var->type->fields.array->vector_elements;
1758      this->matrix_columns = matrix_cols;
1759      if (this->is_clip_distance_mesa)
1760         this->type = GL_FLOAT;
1761      else
1762         this->type = output_var->type->fields.array->gl_type;
1763   } else {
1764      /* Regular variable (scalar, vector, or matrix) */
1765      if (this->is_subscripted) {
1766         linker_error(prog, "Transform feedback varying %s requested, "
1767                      "but %s is not an array.",
1768                      this->orig_name, this->var_name);
1769         return false;
1770      }
1771      this->location = output_var->location;
1772      this->size = 1;
1773      this->vector_elements = output_var->type->vector_elements;
1774      this->matrix_columns = output_var->type->matrix_columns;
1775      this->type = output_var->type->gl_type;
1776   }
1777
1778   /* From GL_EXT_transform_feedback:
1779    *   A program will fail to link if:
1780    *
1781    *   * the total number of components to capture in any varying
1782    *     variable in <varyings> is greater than the constant
1783    *     MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
1784    *     buffer mode is SEPARATE_ATTRIBS_EXT;
1785    */
1786   if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
1787       this->num_components() >
1788       ctx->Const.MaxTransformFeedbackSeparateComponents) {
1789      linker_error(prog, "Transform feedback varying %s exceeds "
1790                   "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
1791                   this->orig_name);
1792      return false;
1793   }
1794
1795   return true;
1796}
1797
1798
1799bool
1800tfeedback_decl::accumulate_num_outputs(struct gl_shader_program *prog,
1801                                       unsigned *count)
1802{
1803   if (!this->is_varying()) {
1804      return true;
1805   }
1806
1807   if (!this->is_assigned()) {
1808      /* From GL_EXT_transform_feedback:
1809       *   A program will fail to link if:
1810       *
1811       *   * any variable name specified in the <varyings> array is not
1812       *     declared as an output in the geometry shader (if present) or
1813       *     the vertex shader (if no geometry shader is present);
1814       */
1815      linker_error(prog, "Transform feedback varying %s undeclared.",
1816                   this->orig_name);
1817      return false;
1818   }
1819
1820   unsigned translated_size = this->size;
1821   if (this->is_clip_distance_mesa)
1822      translated_size = (translated_size + 3) / 4;
1823
1824   *count += translated_size * this->matrix_columns;
1825
1826   return true;
1827}
1828
1829
1830/**
1831 * Update gl_transform_feedback_info to reflect this tfeedback_decl.
1832 *
1833 * If an error occurs, the error is reported through linker_error() and false
1834 * is returned.
1835 */
1836bool
1837tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
1838                      struct gl_transform_feedback_info *info,
1839                      unsigned buffer, const unsigned max_outputs) const
1840{
1841   assert(!this->next_buffer_separator);
1842
1843   /* Handle gl_SkipComponents. */
1844   if (this->skip_components) {
1845      info->BufferStride[buffer] += this->skip_components;
1846      return true;
1847   }
1848
1849   /* From GL_EXT_transform_feedback:
1850    *   A program will fail to link if:
1851    *
1852    *     * the total number of components to capture is greater than
1853    *       the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
1854    *       and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
1855    */
1856   if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
1857       info->BufferStride[buffer] + this->num_components() >
1858       ctx->Const.MaxTransformFeedbackInterleavedComponents) {
1859      linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
1860                   "limit has been exceeded.");
1861      return false;
1862   }
1863
1864   unsigned translated_size = this->size;
1865   if (this->is_clip_distance_mesa)
1866      translated_size = (translated_size + 3) / 4;
1867   unsigned components_so_far = 0;
1868   for (unsigned index = 0; index < translated_size; ++index) {
1869      for (unsigned v = 0; v < this->matrix_columns; ++v) {
1870         unsigned num_components = this->vector_elements;
1871         assert(info->NumOutputs < max_outputs);
1872         info->Outputs[info->NumOutputs].ComponentOffset = 0;
1873         if (this->is_clip_distance_mesa) {
1874            if (this->is_subscripted) {
1875               num_components = 1;
1876               info->Outputs[info->NumOutputs].ComponentOffset =
1877                  this->array_subscript % 4;
1878            } else {
1879               num_components = MIN2(4, this->size - components_so_far);
1880            }
1881         }
1882         info->Outputs[info->NumOutputs].OutputRegister =
1883            this->location + v + index * this->matrix_columns;
1884         info->Outputs[info->NumOutputs].NumComponents = num_components;
1885         info->Outputs[info->NumOutputs].OutputBuffer = buffer;
1886         info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer];
1887         ++info->NumOutputs;
1888         info->BufferStride[buffer] += num_components;
1889         components_so_far += num_components;
1890      }
1891   }
1892   assert(components_so_far == this->num_components());
1893
1894   info->Varyings[info->NumVarying].Name = ralloc_strdup(prog, this->orig_name);
1895   info->Varyings[info->NumVarying].Type = this->type;
1896   info->Varyings[info->NumVarying].Size = this->size;
1897   info->NumVarying++;
1898
1899   return true;
1900}
1901
1902
1903/**
1904 * Parse all the transform feedback declarations that were passed to
1905 * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
1906 *
1907 * If an error occurs, the error is reported through linker_error() and false
1908 * is returned.
1909 */
1910static bool
1911parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
1912                      const void *mem_ctx, unsigned num_names,
1913                      char **varying_names, tfeedback_decl *decls)
1914{
1915   for (unsigned i = 0; i < num_names; ++i) {
1916      if (!decls[i].init(ctx, prog, mem_ctx, varying_names[i]))
1917         return false;
1918
1919      if (!decls[i].is_varying())
1920         continue;
1921
1922      /* From GL_EXT_transform_feedback:
1923       *   A program will fail to link if:
1924       *
1925       *   * any two entries in the <varyings> array specify the same varying
1926       *     variable;
1927       *
1928       * We interpret this to mean "any two entries in the <varyings> array
1929       * specify the same varying variable and array index", since transform
1930       * feedback of arrays would be useless otherwise.
1931       */
1932      for (unsigned j = 0; j < i; ++j) {
1933         if (!decls[j].is_varying())
1934            continue;
1935
1936         if (tfeedback_decl::is_same(decls[i], decls[j])) {
1937            linker_error(prog, "Transform feedback varying %s specified "
1938                         "more than once.", varying_names[i]);
1939            return false;
1940         }
1941      }
1942   }
1943   return true;
1944}
1945
1946
1947/**
1948 * Assign a location for a variable that is produced in one pipeline stage
1949 * (the "producer") and consumed in the next stage (the "consumer").
1950 *
1951 * \param input_var is the input variable declaration in the consumer.
1952 *
1953 * \param output_var is the output variable declaration in the producer.
1954 *
1955 * \param input_index is the counter that keeps track of assigned input
1956 *        locations in the consumer.
1957 *
1958 * \param output_index is the counter that keeps track of assigned output
1959 *        locations in the producer.
1960 *
1961 * It is permissible for \c input_var to be NULL (this happens if a variable
1962 * is output by the producer and consumed by transform feedback, but not
1963 * consumed by the consumer).
1964 *
1965 * If the variable has already been assigned a location, this function has no
1966 * effect.
1967 */
1968void
1969assign_varying_location(ir_variable *input_var, ir_variable *output_var,
1970                        unsigned *input_index, unsigned *output_index)
1971{
1972   if (output_var->location != -1) {
1973      /* Location already assigned. */
1974      return;
1975   }
1976
1977   if (input_var) {
1978      assert(input_var->location == -1);
1979      input_var->location = *input_index;
1980   }
1981
1982   output_var->location = *output_index;
1983
1984   /* FINISHME: Support for "varying" records in GLSL 1.50. */
1985   assert(!output_var->type->is_record());
1986
1987   if (output_var->type->is_array()) {
1988      const unsigned slots = output_var->type->length
1989         * output_var->type->fields.array->matrix_columns;
1990
1991      *output_index += slots;
1992      *input_index += slots;
1993   } else {
1994      const unsigned slots = output_var->type->matrix_columns;
1995
1996      *output_index += slots;
1997      *input_index += slots;
1998   }
1999}
2000
2001
2002/**
2003 * Is the given variable a varying variable to be counted against the
2004 * limit in ctx->Const.MaxVarying?
2005 * This includes variables such as texcoords, colors and generic
2006 * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
2007 */
2008static bool
2009is_varying_var(GLenum shaderType, const ir_variable *var)
2010{
2011   /* Only fragment shaders will take a varying variable as an input */
2012   if (shaderType == GL_FRAGMENT_SHADER &&
2013       var->mode == ir_var_in &&
2014       var->explicit_location) {
2015      switch (var->location) {
2016      case FRAG_ATTRIB_WPOS:
2017      case FRAG_ATTRIB_FACE:
2018      case FRAG_ATTRIB_PNTC:
2019         return false;
2020      default:
2021         return true;
2022      }
2023   }
2024   return false;
2025}
2026
2027
2028/**
2029 * Assign locations for all variables that are produced in one pipeline stage
2030 * (the "producer") and consumed in the next stage (the "consumer").
2031 *
2032 * Variables produced by the producer may also be consumed by transform
2033 * feedback.
2034 *
2035 * \param num_tfeedback_decls is the number of declarations indicating
2036 *        variables that may be consumed by transform feedback.
2037 *
2038 * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
2039 *        representing the result of parsing the strings passed to
2040 *        glTransformFeedbackVaryings().  assign_location() will be called for
2041 *        each of these objects that matches one of the outputs of the
2042 *        producer.
2043 *
2044 * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
2045 * be NULL.  In this case, varying locations are assigned solely based on the
2046 * requirements of transform feedback.
2047 */
2048bool
2049assign_varying_locations(struct gl_context *ctx,
2050			 struct gl_shader_program *prog,
2051			 gl_shader *producer, gl_shader *consumer,
2052                         unsigned num_tfeedback_decls,
2053                         tfeedback_decl *tfeedback_decls)
2054{
2055   /* FINISHME: Set dynamically when geometry shader support is added. */
2056   unsigned output_index = VERT_RESULT_VAR0;
2057   unsigned input_index = FRAG_ATTRIB_VAR0;
2058
2059   /* Operate in a total of three passes.
2060    *
2061    * 1. Assign locations for any matching inputs and outputs.
2062    *
2063    * 2. Mark output variables in the producer that do not have locations as
2064    *    not being outputs.  This lets the optimizer eliminate them.
2065    *
2066    * 3. Mark input variables in the consumer that do not have locations as
2067    *    not being inputs.  This lets the optimizer eliminate them.
2068    */
2069
2070   link_invalidate_variable_locations(producer, ir_var_out, VERT_RESULT_VAR0);
2071   if (consumer)
2072      link_invalidate_variable_locations(consumer, ir_var_in, FRAG_ATTRIB_VAR0);
2073
2074   foreach_list(node, producer->ir) {
2075      ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
2076
2077      if ((output_var == NULL) || (output_var->mode != ir_var_out))
2078	 continue;
2079
2080      ir_variable *input_var =
2081	 consumer ? consumer->symbols->get_variable(output_var->name) : NULL;
2082
2083      if (input_var && input_var->mode != ir_var_in)
2084         input_var = NULL;
2085
2086      if (input_var) {
2087         assign_varying_location(input_var, output_var, &input_index,
2088                                 &output_index);
2089      }
2090
2091      for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2092         if (!tfeedback_decls[i].is_varying())
2093            continue;
2094
2095         if (!tfeedback_decls[i].is_assigned() &&
2096             tfeedback_decls[i].matches_var(output_var)) {
2097            if (output_var->location == -1) {
2098               assign_varying_location(input_var, output_var, &input_index,
2099                                       &output_index);
2100            }
2101            if (!tfeedback_decls[i].assign_location(ctx, prog, output_var))
2102               return false;
2103         }
2104      }
2105   }
2106
2107   unsigned varying_vectors = 0;
2108
2109   if (consumer) {
2110      foreach_list(node, consumer->ir) {
2111         ir_variable *const var = ((ir_instruction *) node)->as_variable();
2112
2113         if ((var == NULL) || (var->mode != ir_var_in))
2114            continue;
2115
2116         if (var->location == -1) {
2117            if (prog->Version <= 120) {
2118               /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
2119                *
2120                *     Only those varying variables used (i.e. read) in
2121                *     the fragment shader executable must be written to
2122                *     by the vertex shader executable; declaring
2123                *     superfluous varying variables in a vertex shader is
2124                *     permissible.
2125                *
2126                * We interpret this text as meaning that the VS must
2127                * write the variable for the FS to read it.  See
2128                * "glsl1-varying read but not written" in piglit.
2129                */
2130
2131               linker_error(prog, "fragment shader varying %s not written "
2132                            "by vertex shader\n.", var->name);
2133            }
2134
2135            /* An 'in' variable is only really a shader input if its
2136             * value is written by the previous stage.
2137             */
2138            var->mode = ir_var_auto;
2139         } else if (is_varying_var(consumer->Type, var)) {
2140            /* The packing rules are used for vertex shader inputs are also
2141             * used for fragment shader inputs.
2142             */
2143            varying_vectors += count_attribute_slots(var->type);
2144         }
2145      }
2146   }
2147
2148   if (ctx->API == API_OPENGLES2 || prog->Version == 100) {
2149      if (varying_vectors > ctx->Const.MaxVarying) {
2150         if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2151            linker_warning(prog, "shader uses too many varying vectors "
2152                           "(%u > %u), but the driver will try to optimize "
2153                           "them out; this is non-portable out-of-spec "
2154                           "behavior\n",
2155                           varying_vectors, ctx->Const.MaxVarying);
2156         } else {
2157            linker_error(prog, "shader uses too many varying vectors "
2158                         "(%u > %u)\n",
2159                         varying_vectors, ctx->Const.MaxVarying);
2160            return false;
2161         }
2162      }
2163   } else {
2164      const unsigned float_components = varying_vectors * 4;
2165      if (float_components > ctx->Const.MaxVarying * 4) {
2166         if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
2167            linker_warning(prog, "shader uses too many varying components "
2168                           "(%u > %u), but the driver will try to optimize "
2169                           "them out; this is non-portable out-of-spec "
2170                           "behavior\n",
2171                           float_components, ctx->Const.MaxVarying * 4);
2172         } else {
2173            linker_error(prog, "shader uses too many varying components "
2174                         "(%u > %u)\n",
2175                         float_components, ctx->Const.MaxVarying * 4);
2176            return false;
2177         }
2178      }
2179   }
2180
2181   return true;
2182}
2183
2184
2185/**
2186 * Store transform feedback location assignments into
2187 * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
2188 *
2189 * If an error occurs, the error is reported through linker_error() and false
2190 * is returned.
2191 */
2192static bool
2193store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
2194                     unsigned num_tfeedback_decls,
2195                     tfeedback_decl *tfeedback_decls)
2196{
2197   bool separate_attribs_mode =
2198      prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS;
2199
2200   ralloc_free(prog->LinkedTransformFeedback.Varyings);
2201   ralloc_free(prog->LinkedTransformFeedback.Outputs);
2202
2203   memset(&prog->LinkedTransformFeedback, 0,
2204          sizeof(prog->LinkedTransformFeedback));
2205
2206   prog->LinkedTransformFeedback.Varyings =
2207      rzalloc_array(prog,
2208		    struct gl_transform_feedback_varying_info,
2209		    num_tfeedback_decls);
2210
2211   unsigned num_outputs = 0;
2212   for (unsigned i = 0; i < num_tfeedback_decls; ++i)
2213      if (!tfeedback_decls[i].accumulate_num_outputs(prog, &num_outputs))
2214         return false;
2215
2216   prog->LinkedTransformFeedback.Outputs =
2217      rzalloc_array(prog,
2218                    struct gl_transform_feedback_output,
2219                    num_outputs);
2220
2221   unsigned num_buffers = 0;
2222
2223   if (separate_attribs_mode) {
2224      /* GL_SEPARATE_ATTRIBS */
2225      for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2226         if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback,
2227                                       num_buffers, num_outputs))
2228            return false;
2229
2230         num_buffers++;
2231      }
2232   }
2233   else {
2234      /* GL_INVERLEAVED_ATTRIBS */
2235      for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
2236         if (tfeedback_decls[i].is_next_buffer_separator()) {
2237            num_buffers++;
2238            continue;
2239         }
2240
2241         if (!tfeedback_decls[i].store(ctx, prog,
2242                                       &prog->LinkedTransformFeedback,
2243                                       num_buffers, num_outputs))
2244            return false;
2245      }
2246      num_buffers++;
2247   }
2248
2249   assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs);
2250
2251   prog->LinkedTransformFeedback.NumBuffers = num_buffers;
2252   return true;
2253}
2254
2255/**
2256 * Store the gl_FragDepth layout in the gl_shader_program struct.
2257 */
2258static void
2259store_fragdepth_layout(struct gl_shader_program *prog)
2260{
2261   if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2262      return;
2263   }
2264
2265   struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
2266
2267   /* We don't look up the gl_FragDepth symbol directly because if
2268    * gl_FragDepth is not used in the shader, it's removed from the IR.
2269    * However, the symbol won't be removed from the symbol table.
2270    *
2271    * We're only interested in the cases where the variable is NOT removed
2272    * from the IR.
2273    */
2274   foreach_list(node, ir) {
2275      ir_variable *const var = ((ir_instruction *) node)->as_variable();
2276
2277      if (var == NULL || var->mode != ir_var_out) {
2278         continue;
2279      }
2280
2281      if (strcmp(var->name, "gl_FragDepth") == 0) {
2282         switch (var->depth_layout) {
2283         case ir_depth_layout_none:
2284            prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
2285            return;
2286         case ir_depth_layout_any:
2287            prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
2288            return;
2289         case ir_depth_layout_greater:
2290            prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
2291            return;
2292         case ir_depth_layout_less:
2293            prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
2294            return;
2295         case ir_depth_layout_unchanged:
2296            prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2297            return;
2298         default:
2299            assert(0);
2300            return;
2301         }
2302      }
2303   }
2304}
2305
2306/**
2307 * Validate the resources used by a program versus the implementation limits
2308 */
2309static bool
2310check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
2311{
2312   static const char *const shader_names[MESA_SHADER_TYPES] = {
2313      "vertex", "fragment", "geometry"
2314   };
2315
2316   const unsigned max_samplers[MESA_SHADER_TYPES] = {
2317      ctx->Const.MaxVertexTextureImageUnits,
2318      ctx->Const.MaxTextureImageUnits,
2319      ctx->Const.MaxGeometryTextureImageUnits
2320   };
2321
2322   const unsigned max_uniform_components[MESA_SHADER_TYPES] = {
2323      ctx->Const.VertexProgram.MaxUniformComponents,
2324      ctx->Const.FragmentProgram.MaxUniformComponents,
2325      0          /* FINISHME: Geometry shaders. */
2326   };
2327
2328   const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
2329      ctx->Const.VertexProgram.MaxUniformBlocks,
2330      ctx->Const.FragmentProgram.MaxUniformBlocks,
2331      ctx->Const.GeometryProgram.MaxUniformBlocks,
2332   };
2333
2334   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2335      struct gl_shader *sh = prog->_LinkedShaders[i];
2336
2337      if (sh == NULL)
2338	 continue;
2339
2340      if (sh->num_samplers > max_samplers[i]) {
2341	 linker_error(prog, "Too many %s shader texture samplers",
2342		      shader_names[i]);
2343      }
2344
2345      if (sh->num_uniform_components > max_uniform_components[i]) {
2346         if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
2347            linker_warning(prog, "Too many %s shader uniform components, "
2348                           "but the driver will try to optimize them out; "
2349                           "this is non-portable out-of-spec behavior\n",
2350                           shader_names[i]);
2351         } else {
2352            linker_error(prog, "Too many %s shader uniform components",
2353                         shader_names[i]);
2354         }
2355      }
2356   }
2357
2358   unsigned blocks[MESA_SHADER_TYPES] = {0};
2359   unsigned total_uniform_blocks = 0;
2360
2361   for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
2362      for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
2363	 if (prog->UniformBlockStageIndex[j][i] != -1) {
2364	    blocks[j]++;
2365	    total_uniform_blocks++;
2366	 }
2367      }
2368
2369      if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
2370	 linker_error(prog, "Too many combined uniform blocks (%d/%d)",
2371		      prog->NumUniformBlocks,
2372		      ctx->Const.MaxCombinedUniformBlocks);
2373      } else {
2374	 for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2375	    if (blocks[i] > max_uniform_blocks[i]) {
2376	       linker_error(prog, "Too many %s uniform blocks (%d/%d)",
2377			    shader_names[i],
2378			    blocks[i],
2379			    max_uniform_blocks[i]);
2380	       break;
2381	    }
2382	 }
2383      }
2384   }
2385
2386   return prog->LinkStatus;
2387}
2388
2389void
2390link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
2391{
2392   tfeedback_decl *tfeedback_decls = NULL;
2393   unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
2394
2395   void *mem_ctx = ralloc_context(NULL); // temporary linker context
2396
2397   prog->LinkStatus = false;
2398   prog->Validated = false;
2399   prog->_Used = false;
2400
2401   ralloc_free(prog->InfoLog);
2402   prog->InfoLog = ralloc_strdup(NULL, "");
2403
2404   ralloc_free(prog->UniformBlocks);
2405   prog->UniformBlocks = NULL;
2406   prog->NumUniformBlocks = 0;
2407   for (int i = 0; i < MESA_SHADER_TYPES; i++) {
2408      ralloc_free(prog->UniformBlockStageIndex[i]);
2409      prog->UniformBlockStageIndex[i] = NULL;
2410   }
2411
2412   /* Separate the shaders into groups based on their type.
2413    */
2414   struct gl_shader **vert_shader_list;
2415   unsigned num_vert_shaders = 0;
2416   struct gl_shader **frag_shader_list;
2417   unsigned num_frag_shaders = 0;
2418
2419   vert_shader_list = (struct gl_shader **)
2420      calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
2421   frag_shader_list =  &vert_shader_list[prog->NumShaders];
2422
2423   unsigned min_version = UINT_MAX;
2424   unsigned max_version = 0;
2425   for (unsigned i = 0; i < prog->NumShaders; i++) {
2426      min_version = MIN2(min_version, prog->Shaders[i]->Version);
2427      max_version = MAX2(max_version, prog->Shaders[i]->Version);
2428
2429      switch (prog->Shaders[i]->Type) {
2430      case GL_VERTEX_SHADER:
2431	 vert_shader_list[num_vert_shaders] = prog->Shaders[i];
2432	 num_vert_shaders++;
2433	 break;
2434      case GL_FRAGMENT_SHADER:
2435	 frag_shader_list[num_frag_shaders] = prog->Shaders[i];
2436	 num_frag_shaders++;
2437	 break;
2438      case GL_GEOMETRY_SHADER:
2439	 /* FINISHME: Support geometry shaders. */
2440	 assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
2441	 break;
2442      }
2443   }
2444
2445   /* Previous to GLSL version 1.30, different compilation units could mix and
2446    * match shading language versions.  With GLSL 1.30 and later, the versions
2447    * of all shaders must match.
2448    */
2449   assert(min_version >= 100);
2450   assert(max_version <= 140);
2451   if ((max_version >= 130 || min_version == 100)
2452       && min_version != max_version) {
2453      linker_error(prog, "all shaders must use same shading "
2454		   "language version\n");
2455      goto done;
2456   }
2457
2458   prog->Version = max_version;
2459
2460   for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
2461      if (prog->_LinkedShaders[i] != NULL)
2462	 ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
2463
2464      prog->_LinkedShaders[i] = NULL;
2465   }
2466
2467   /* Link all shaders for a particular stage and validate the result.
2468    */
2469   if (num_vert_shaders > 0) {
2470      gl_shader *const sh =
2471	 link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
2472				 num_vert_shaders);
2473
2474      if (sh == NULL)
2475	 goto done;
2476
2477      if (!validate_vertex_shader_executable(prog, sh))
2478	 goto done;
2479
2480      _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
2481			     sh);
2482   }
2483
2484   if (num_frag_shaders > 0) {
2485      gl_shader *const sh =
2486	 link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
2487				 num_frag_shaders);
2488
2489      if (sh == NULL)
2490	 goto done;
2491
2492      if (!validate_fragment_shader_executable(prog, sh))
2493	 goto done;
2494
2495      _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
2496			     sh);
2497   }
2498
2499   /* Here begins the inter-stage linking phase.  Some initial validation is
2500    * performed, then locations are assigned for uniforms, attributes, and
2501    * varyings.
2502    */
2503   if (cross_validate_uniforms(prog)) {
2504      unsigned prev;
2505
2506      for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2507	 if (prog->_LinkedShaders[prev] != NULL)
2508	    break;
2509      }
2510
2511      /* Validate the inputs of each stage with the output of the preceding
2512       * stage.
2513       */
2514      for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2515	 if (prog->_LinkedShaders[i] == NULL)
2516	    continue;
2517
2518	 if (!cross_validate_outputs_to_inputs(prog,
2519					       prog->_LinkedShaders[prev],
2520					       prog->_LinkedShaders[i]))
2521	    goto done;
2522
2523	 prev = i;
2524      }
2525
2526      prog->LinkStatus = true;
2527   }
2528
2529   /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
2530    * it before optimization because we want most of the checks to get
2531    * dropped thanks to constant propagation.
2532    */
2533   if (max_version >= 130) {
2534      struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2535      if (sh) {
2536	 lower_discard_flow(sh->ir);
2537      }
2538   }
2539
2540   if (!interstage_cross_validate_uniform_blocks(prog))
2541      goto done;
2542
2543   /* Do common optimization before assigning storage for attributes,
2544    * uniforms, and varyings.  Later optimization could possibly make
2545    * some of that unused.
2546    */
2547   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2548      if (prog->_LinkedShaders[i] == NULL)
2549	 continue;
2550
2551      detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
2552      if (!prog->LinkStatus)
2553	 goto done;
2554
2555      if (ctx->ShaderCompilerOptions[i].LowerClipDistance)
2556         lower_clip_distance(prog->_LinkedShaders[i]->ir);
2557
2558      unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
2559
2560      while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll))
2561	 ;
2562   }
2563
2564   /* FINISHME: The value of the max_attribute_index parameter is
2565    * FINISHME: implementation dependent based on the value of
2566    * FINISHME: GL_MAX_VERTEX_ATTRIBS.  GL_MAX_VERTEX_ATTRIBS must be
2567    * FINISHME: at least 16, so hardcode 16 for now.
2568    */
2569   if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
2570      goto done;
2571   }
2572
2573   if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
2574      goto done;
2575   }
2576
2577   unsigned prev;
2578   for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
2579      if (prog->_LinkedShaders[prev] != NULL)
2580	 break;
2581   }
2582
2583   if (num_tfeedback_decls != 0) {
2584      /* From GL_EXT_transform_feedback:
2585       *   A program will fail to link if:
2586       *
2587       *   * the <count> specified by TransformFeedbackVaryingsEXT is
2588       *     non-zero, but the program object has no vertex or geometry
2589       *     shader;
2590       */
2591      if (prev >= MESA_SHADER_FRAGMENT) {
2592         linker_error(prog, "Transform feedback varyings specified, but "
2593                      "no vertex or geometry shader is present.");
2594         goto done;
2595      }
2596
2597      tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
2598                                     prog->TransformFeedback.NumVarying);
2599      if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
2600                                 prog->TransformFeedback.VaryingNames,
2601                                 tfeedback_decls))
2602         goto done;
2603   }
2604
2605   for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
2606      if (prog->_LinkedShaders[i] == NULL)
2607	 continue;
2608
2609      if (!assign_varying_locations(
2610             ctx, prog, prog->_LinkedShaders[prev], prog->_LinkedShaders[i],
2611             i == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
2612             tfeedback_decls))
2613	 goto done;
2614
2615      prev = i;
2616   }
2617
2618   if (prev != MESA_SHADER_FRAGMENT && num_tfeedback_decls != 0) {
2619      /* There was no fragment shader, but we still have to assign varying
2620       * locations for use by transform feedback.
2621       */
2622      if (!assign_varying_locations(
2623             ctx, prog, prog->_LinkedShaders[prev], NULL, num_tfeedback_decls,
2624             tfeedback_decls))
2625         goto done;
2626   }
2627
2628   if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
2629      goto done;
2630
2631   if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
2632      demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
2633				       ir_var_out);
2634
2635      /* Eliminate code that is now dead due to unused vertex outputs being
2636       * demoted.
2637       */
2638      while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
2639	 ;
2640   }
2641
2642   if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
2643      gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
2644
2645      demote_shader_inputs_and_outputs(sh, ir_var_in);
2646      demote_shader_inputs_and_outputs(sh, ir_var_inout);
2647      demote_shader_inputs_and_outputs(sh, ir_var_out);
2648
2649      /* Eliminate code that is now dead due to unused geometry outputs being
2650       * demoted.
2651       */
2652      while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
2653	 ;
2654   }
2655
2656   if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
2657      gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2658
2659      demote_shader_inputs_and_outputs(sh, ir_var_in);
2660
2661      /* Eliminate code that is now dead due to unused fragment inputs being
2662       * demoted.  This shouldn't actually do anything other than remove
2663       * declarations of the (now unused) global variables.
2664       */
2665      while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
2666	 ;
2667   }
2668
2669   update_array_sizes(prog);
2670   link_assign_uniform_locations(prog);
2671   store_fragdepth_layout(prog);
2672
2673   if (!check_resources(ctx, prog))
2674      goto done;
2675
2676   /* OpenGL ES requires that a vertex shader and a fragment shader both be
2677    * present in a linked program.  By checking for use of shading language
2678    * version 1.00, we also catch the GL_ARB_ES2_compatibility case.
2679    */
2680   if (!prog->InternalSeparateShader &&
2681       (ctx->API == API_OPENGLES2 || prog->Version == 100)) {
2682      if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
2683	 linker_error(prog, "program lacks a vertex shader\n");
2684      } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
2685	 linker_error(prog, "program lacks a fragment shader\n");
2686      }
2687   }
2688
2689   /* FINISHME: Assign fragment shader output locations. */
2690
2691done:
2692   free(vert_shader_list);
2693
2694   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2695      if (prog->_LinkedShaders[i] == NULL)
2696	 continue;
2697
2698      /* Retain any live IR, but trash the rest. */
2699      reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
2700
2701      /* The symbol table in the linked shaders may contain references to
2702       * variables that were removed (e.g., unused uniforms).  Since it may
2703       * contain junk, there is no possible valid use.  Delete it and set the
2704       * pointer to NULL.
2705       */
2706      delete prog->_LinkedShaders[i]->symbols;
2707      prog->_LinkedShaders[i]->symbols = NULL;
2708   }
2709
2710   ralloc_free(mem_ctx);
2711}
2712