1/*
2 * Copyright © 2012 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 link_varyings.cpp
26 *
27 * Linker functions related specifically to linking varyings between shader
28 * stages.
29 */
30
31
32#include "main/errors.h"
33#include "main/consts_exts.h"
34#include "main/shader_types.h"
35#include "glsl_symbol_table.h"
36#include "ir.h"
37#include "linker.h"
38#include "link_varyings.h"
39
40
41/**
42 * Get the varying type stripped of the outermost array if we're processing
43 * a stage whose varyings are arrays indexed by a vertex number (such as
44 * geometry shader inputs).
45 */
46static const glsl_type *
47get_varying_type(const ir_variable *var, gl_shader_stage stage)
48{
49   const glsl_type *type = var->type;
50
51   if (!var->data.patch &&
52       ((var->data.mode == ir_var_shader_out &&
53         stage == MESA_SHADER_TESS_CTRL) ||
54        (var->data.mode == ir_var_shader_in &&
55         (stage == MESA_SHADER_TESS_CTRL || stage == MESA_SHADER_TESS_EVAL ||
56          stage == MESA_SHADER_GEOMETRY)))) {
57      assert(type->is_array());
58      type = type->fields.array;
59   }
60
61   return type;
62}
63
64/**
65 * Validate the types and qualifiers of an output from one stage against the
66 * matching input to another stage.
67 */
68static void
69cross_validate_types_and_qualifiers(const struct gl_constants *consts,
70                                    struct gl_shader_program *prog,
71                                    const ir_variable *input,
72                                    const ir_variable *output,
73                                    gl_shader_stage consumer_stage,
74                                    gl_shader_stage producer_stage)
75{
76   /* Check that the types match between stages.
77    */
78   const glsl_type *type_to_match = input->type;
79
80   /* VS -> GS, VS -> TCS, VS -> TES, TES -> GS */
81   const bool extra_array_level = (producer_stage == MESA_SHADER_VERTEX &&
82                                   consumer_stage != MESA_SHADER_FRAGMENT) ||
83                                  consumer_stage == MESA_SHADER_GEOMETRY;
84   if (extra_array_level) {
85      assert(type_to_match->is_array());
86      type_to_match = type_to_match->fields.array;
87   }
88
89   if (type_to_match != output->type) {
90      if (output->type->is_struct()) {
91         /* Structures across shader stages can have different name
92          * and considered to match in type if and only if structure
93          * members match in name, type, qualification, and declaration
94          * order. The precision doesn’t need to match.
95          */
96         if (!output->type->record_compare(type_to_match,
97                                           false, /* match_name */
98                                           true, /* match_locations */
99                                           false /* match_precision */)) {
100            linker_error(prog,
101                  "%s shader output `%s' declared as struct `%s', "
102                  "doesn't match in type with %s shader input "
103                  "declared as struct `%s'\n",
104                  _mesa_shader_stage_to_string(producer_stage),
105                  output->name,
106                  output->type->name,
107                  _mesa_shader_stage_to_string(consumer_stage),
108                  input->type->name);
109         }
110      } else if (!output->type->is_array() || !is_gl_identifier(output->name)) {
111         /* There is a bit of a special case for gl_TexCoord.  This
112          * built-in is unsized by default.  Applications that variable
113          * access it must redeclare it with a size.  There is some
114          * language in the GLSL spec that implies the fragment shader
115          * and vertex shader do not have to agree on this size.  Other
116          * driver behave this way, and one or two applications seem to
117          * rely on it.
118          *
119          * Neither declaration needs to be modified here because the array
120          * sizes are fixed later when update_array_sizes is called.
121          *
122          * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
123          *
124          *     "Unlike user-defined varying variables, the built-in
125          *     varying variables don't have a strict one-to-one
126          *     correspondence between the vertex language and the
127          *     fragment language."
128          */
129         linker_error(prog,
130                      "%s shader output `%s' declared as type `%s', "
131                      "but %s shader input declared as type `%s'\n",
132                      _mesa_shader_stage_to_string(producer_stage),
133                      output->name,
134                      output->type->name,
135                      _mesa_shader_stage_to_string(consumer_stage),
136                      input->type->name);
137         return;
138      }
139   }
140
141   /* Check that all of the qualifiers match between stages.
142    */
143
144   /* According to the OpenGL and OpenGLES GLSL specs, the centroid qualifier
145    * should match until OpenGL 4.3 and OpenGLES 3.1. The OpenGLES 3.0
146    * conformance test suite does not verify that the qualifiers must match.
147    * The deqp test suite expects the opposite (OpenGLES 3.1) behavior for
148    * OpenGLES 3.0 drivers, so we relax the checking in all cases.
149    */
150   if (false /* always skip the centroid check */ &&
151       prog->data->Version < (prog->IsES ? 310 : 430) &&
152       input->data.centroid != output->data.centroid) {
153      linker_error(prog,
154                   "%s shader output `%s' %s centroid qualifier, "
155                   "but %s shader input %s centroid qualifier\n",
156                   _mesa_shader_stage_to_string(producer_stage),
157                   output->name,
158                   (output->data.centroid) ? "has" : "lacks",
159                   _mesa_shader_stage_to_string(consumer_stage),
160                   (input->data.centroid) ? "has" : "lacks");
161      return;
162   }
163
164   if (input->data.sample != output->data.sample) {
165      linker_error(prog,
166                   "%s shader output `%s' %s sample qualifier, "
167                   "but %s shader input %s sample qualifier\n",
168                   _mesa_shader_stage_to_string(producer_stage),
169                   output->name,
170                   (output->data.sample) ? "has" : "lacks",
171                   _mesa_shader_stage_to_string(consumer_stage),
172                   (input->data.sample) ? "has" : "lacks");
173      return;
174   }
175
176   if (input->data.patch != output->data.patch) {
177      linker_error(prog,
178                   "%s shader output `%s' %s patch qualifier, "
179                   "but %s shader input %s patch qualifier\n",
180                   _mesa_shader_stage_to_string(producer_stage),
181                   output->name,
182                   (output->data.patch) ? "has" : "lacks",
183                   _mesa_shader_stage_to_string(consumer_stage),
184                   (input->data.patch) ? "has" : "lacks");
185      return;
186   }
187
188   /* The GLSL 4.20 and GLSL ES 3.00 specifications say:
189    *
190    *    "As only outputs need be declared with invariant, an output from
191    *     one shader stage will still match an input of a subsequent stage
192    *     without the input being declared as invariant."
193    *
194    * while GLSL 4.10 says:
195    *
196    *    "For variables leaving one shader and coming into another shader,
197    *     the invariant keyword has to be used in both shaders, or a link
198    *     error will result."
199    *
200    * and GLSL ES 1.00 section 4.6.4 "Invariance and Linking" says:
201    *
202    *    "The invariance of varyings that are declared in both the vertex
203    *     and fragment shaders must match."
204    */
205   if (input->data.explicit_invariant != output->data.explicit_invariant &&
206       prog->data->Version < (prog->IsES ? 300 : 420)) {
207      linker_error(prog,
208                   "%s shader output `%s' %s invariant qualifier, "
209                   "but %s shader input %s invariant qualifier\n",
210                   _mesa_shader_stage_to_string(producer_stage),
211                   output->name,
212                   (output->data.explicit_invariant) ? "has" : "lacks",
213                   _mesa_shader_stage_to_string(consumer_stage),
214                   (input->data.explicit_invariant) ? "has" : "lacks");
215      return;
216   }
217
218   /* GLSL >= 4.40 removes text requiring interpolation qualifiers
219    * to match cross stage, they must only match within the same stage.
220    *
221    * From page 84 (page 90 of the PDF) of the GLSL 4.40 spec:
222    *
223    *     "It is a link-time error if, within the same stage, the interpolation
224    *     qualifiers of variables of the same name do not match.
225    *
226    * Section 4.3.9 (Interpolation) of the GLSL ES 3.00 spec says:
227    *
228    *    "When no interpolation qualifier is present, smooth interpolation
229    *    is used."
230    *
231    * So we match variables where one is smooth and the other has no explicit
232    * qualifier.
233    */
234   unsigned input_interpolation = input->data.interpolation;
235   unsigned output_interpolation = output->data.interpolation;
236   if (prog->IsES) {
237      if (input_interpolation == INTERP_MODE_NONE)
238         input_interpolation = INTERP_MODE_SMOOTH;
239      if (output_interpolation == INTERP_MODE_NONE)
240         output_interpolation = INTERP_MODE_SMOOTH;
241   }
242   if (input_interpolation != output_interpolation &&
243       prog->data->Version < 440) {
244      if (!consts->AllowGLSLCrossStageInterpolationMismatch) {
245         linker_error(prog,
246                      "%s shader output `%s' specifies %s "
247                      "interpolation qualifier, "
248                      "but %s shader input specifies %s "
249                      "interpolation qualifier\n",
250                      _mesa_shader_stage_to_string(producer_stage),
251                      output->name,
252                      interpolation_string(output->data.interpolation),
253                      _mesa_shader_stage_to_string(consumer_stage),
254                      interpolation_string(input->data.interpolation));
255         return;
256      } else {
257         linker_warning(prog,
258                        "%s shader output `%s' specifies %s "
259                        "interpolation qualifier, "
260                        "but %s shader input specifies %s "
261                        "interpolation qualifier\n",
262                        _mesa_shader_stage_to_string(producer_stage),
263                        output->name,
264                        interpolation_string(output->data.interpolation),
265                        _mesa_shader_stage_to_string(consumer_stage),
266                        interpolation_string(input->data.interpolation));
267      }
268   }
269}
270
271/**
272 * Validate front and back color outputs against single color input
273 */
274static void
275cross_validate_front_and_back_color(const struct gl_constants *consts,
276                                    struct gl_shader_program *prog,
277                                    const ir_variable *input,
278                                    const ir_variable *front_color,
279                                    const ir_variable *back_color,
280                                    gl_shader_stage consumer_stage,
281                                    gl_shader_stage producer_stage)
282{
283   if (front_color != NULL && front_color->data.assigned)
284      cross_validate_types_and_qualifiers(consts, prog, input, front_color,
285                                          consumer_stage, producer_stage);
286
287   if (back_color != NULL && back_color->data.assigned)
288      cross_validate_types_and_qualifiers(consts, prog, input, back_color,
289                                          consumer_stage, producer_stage);
290}
291
292static unsigned
293compute_variable_location_slot(ir_variable *var, gl_shader_stage stage)
294{
295   unsigned location_start = VARYING_SLOT_VAR0;
296
297   switch (stage) {
298      case MESA_SHADER_VERTEX:
299         if (var->data.mode == ir_var_shader_in)
300            location_start = VERT_ATTRIB_GENERIC0;
301         break;
302      case MESA_SHADER_TESS_CTRL:
303      case MESA_SHADER_TESS_EVAL:
304         if (var->data.patch)
305            location_start = VARYING_SLOT_PATCH0;
306         break;
307      case MESA_SHADER_FRAGMENT:
308         if (var->data.mode == ir_var_shader_out)
309            location_start = FRAG_RESULT_DATA0;
310         break;
311      default:
312         break;
313   }
314
315   return var->data.location - location_start;
316}
317
318struct explicit_location_info {
319   ir_variable *var;
320   bool base_type_is_integer;
321   unsigned base_type_bit_size;
322   unsigned interpolation;
323   bool centroid;
324   bool sample;
325   bool patch;
326};
327
328static bool
329check_location_aliasing(struct explicit_location_info explicit_locations[][4],
330                        ir_variable *var,
331                        unsigned location,
332                        unsigned component,
333                        unsigned location_limit,
334                        const glsl_type *type,
335                        unsigned interpolation,
336                        bool centroid,
337                        bool sample,
338                        bool patch,
339                        gl_shader_program *prog,
340                        gl_shader_stage stage)
341{
342   unsigned last_comp;
343   unsigned base_type_bit_size;
344   const glsl_type *type_without_array = type->without_array();
345   const bool base_type_is_integer =
346      glsl_base_type_is_integer(type_without_array->base_type);
347   const bool is_struct = type_without_array->is_struct();
348   if (is_struct) {
349      /* structs don't have a defined underlying base type so just treat all
350       * component slots as used and set the bit size to 0. If there is
351       * location aliasing, we'll fail anyway later.
352       */
353      last_comp = 4;
354      base_type_bit_size = 0;
355   } else {
356      unsigned dmul = type_without_array->is_64bit() ? 2 : 1;
357      last_comp = component + type_without_array->vector_elements * dmul;
358      base_type_bit_size =
359         glsl_base_type_get_bit_size(type_without_array->base_type);
360   }
361
362   while (location < location_limit) {
363      unsigned comp = 0;
364      while (comp < 4) {
365         struct explicit_location_info *info =
366            &explicit_locations[location][comp];
367
368         if (info->var) {
369            if (info->var->type->without_array()->is_struct() || is_struct) {
370               /* Structs cannot share location since they are incompatible
371                * with any other underlying numerical type.
372                */
373               linker_error(prog,
374                            "%s shader has multiple %sputs sharing the "
375                            "same location that don't have the same "
376                            "underlying numerical type. Struct variable '%s', "
377                            "location %u\n",
378                            _mesa_shader_stage_to_string(stage),
379                            var->data.mode == ir_var_shader_in ? "in" : "out",
380                            is_struct ? var->name : info->var->name,
381                            location);
382               return false;
383            } else if (comp >= component && comp < last_comp) {
384               /* Component aliasing is not allowed */
385               linker_error(prog,
386                            "%s shader has multiple %sputs explicitly "
387                            "assigned to location %d and component %d\n",
388                            _mesa_shader_stage_to_string(stage),
389                            var->data.mode == ir_var_shader_in ? "in" : "out",
390                            location, comp);
391               return false;
392            } else {
393               /* From the OpenGL 4.60.5 spec, section 4.4.1 Input Layout
394                * Qualifiers, Page 67, (Location aliasing):
395                *
396                *   " Further, when location aliasing, the aliases sharing the
397                *     location must have the same underlying numerical type
398                *     and bit width (floating-point or integer, 32-bit versus
399                *     64-bit, etc.) and the same auxiliary storage and
400                *     interpolation qualification."
401                */
402
403               /* If the underlying numerical type isn't integer, implicitly
404                * it will be float or else we would have failed by now.
405                */
406               if (info->base_type_is_integer != base_type_is_integer) {
407                  linker_error(prog,
408                               "%s shader has multiple %sputs sharing the "
409                               "same location that don't have the same "
410                               "underlying numerical type. Location %u "
411                               "component %u.\n",
412                               _mesa_shader_stage_to_string(stage),
413                               var->data.mode == ir_var_shader_in ?
414                               "in" : "out", location, comp);
415                  return false;
416               }
417
418               if (info->base_type_bit_size != base_type_bit_size) {
419                  linker_error(prog,
420                               "%s shader has multiple %sputs sharing the "
421                               "same location that don't have the same "
422                               "underlying numerical bit size. Location %u "
423                               "component %u.\n",
424                               _mesa_shader_stage_to_string(stage),
425                               var->data.mode == ir_var_shader_in ?
426                               "in" : "out", location, comp);
427                  return false;
428               }
429
430               if (info->interpolation != interpolation) {
431                  linker_error(prog,
432                               "%s shader has multiple %sputs sharing the "
433                               "same location that don't have the same "
434                               "interpolation qualification. Location %u "
435                               "component %u.\n",
436                               _mesa_shader_stage_to_string(stage),
437                               var->data.mode == ir_var_shader_in ?
438                               "in" : "out", location, comp);
439                  return false;
440               }
441
442               if (info->centroid != centroid ||
443                   info->sample != sample ||
444                   info->patch != patch) {
445                  linker_error(prog,
446                               "%s shader has multiple %sputs sharing the "
447                               "same location that don't have the same "
448                               "auxiliary storage qualification. Location %u "
449                               "component %u.\n",
450                               _mesa_shader_stage_to_string(stage),
451                               var->data.mode == ir_var_shader_in ?
452                               "in" : "out", location, comp);
453                  return false;
454               }
455            }
456         } else if (comp >= component && comp < last_comp) {
457            info->var = var;
458            info->base_type_is_integer = base_type_is_integer;
459            info->base_type_bit_size = base_type_bit_size;
460            info->interpolation = interpolation;
461            info->centroid = centroid;
462            info->sample = sample;
463            info->patch = patch;
464         }
465
466         comp++;
467
468         /* We need to do some special handling for doubles as dvec3 and
469          * dvec4 consume two consecutive locations. We don't need to
470          * worry about components beginning at anything other than 0 as
471          * the spec does not allow this for dvec3 and dvec4.
472          */
473         if (comp == 4 && last_comp > 4) {
474            last_comp = last_comp - 4;
475            /* Bump location index and reset the component index */
476            location++;
477            comp = 0;
478            component = 0;
479         }
480      }
481
482      location++;
483   }
484
485   return true;
486}
487
488static bool
489validate_explicit_variable_location(const struct gl_constants *consts,
490                                    struct explicit_location_info explicit_locations[][4],
491                                    ir_variable *var,
492                                    gl_shader_program *prog,
493                                    gl_linked_shader *sh)
494{
495   const glsl_type *type = get_varying_type(var, sh->Stage);
496   unsigned num_elements = type->count_attribute_slots(false);
497   unsigned idx = compute_variable_location_slot(var, sh->Stage);
498   unsigned slot_limit = idx + num_elements;
499
500   /* Vertex shader inputs and fragment shader outputs are validated in
501    * assign_attribute_or_color_locations() so we should not attempt to
502    * validate them again here.
503    */
504   unsigned slot_max;
505   if (var->data.mode == ir_var_shader_out) {
506      assert(sh->Stage != MESA_SHADER_FRAGMENT);
507      slot_max =
508         consts->Program[sh->Stage].MaxOutputComponents / 4;
509   } else {
510      assert(var->data.mode == ir_var_shader_in);
511      assert(sh->Stage != MESA_SHADER_VERTEX);
512      slot_max =
513         consts->Program[sh->Stage].MaxInputComponents / 4;
514   }
515
516   if (slot_limit > slot_max) {
517      linker_error(prog,
518                   "Invalid location %u in %s shader\n",
519                   idx, _mesa_shader_stage_to_string(sh->Stage));
520      return false;
521   }
522
523   const glsl_type *type_without_array = type->without_array();
524   if (type_without_array->is_interface()) {
525      for (unsigned i = 0; i < type_without_array->length; i++) {
526         glsl_struct_field *field = &type_without_array->fields.structure[i];
527         unsigned field_location = field->location -
528            (field->patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0);
529         unsigned field_slots = field->type->count_attribute_slots(false);
530         if (!check_location_aliasing(explicit_locations, var,
531                                      field_location,
532                                      0,
533                                      field_location + field_slots,
534                                      field->type,
535                                      field->interpolation,
536                                      field->centroid,
537                                      field->sample,
538                                      field->patch,
539                                      prog, sh->Stage)) {
540            return false;
541         }
542      }
543   } else if (!check_location_aliasing(explicit_locations, var,
544                                       idx, var->data.location_frac,
545                                       slot_limit, type,
546                                       var->data.interpolation,
547                                       var->data.centroid,
548                                       var->data.sample,
549                                       var->data.patch,
550                                       prog, sh->Stage)) {
551      return false;
552   }
553
554   return true;
555}
556
557/**
558 * Validate explicit locations for the inputs to the first stage and the
559 * outputs of the last stage in a program, if those are not the VS and FS
560 * shaders.
561 */
562void
563validate_first_and_last_interface_explicit_locations(const struct gl_constants *consts,
564                                                     struct gl_shader_program *prog,
565                                                     gl_shader_stage first_stage,
566                                                     gl_shader_stage last_stage)
567{
568   /* VS inputs and FS outputs are validated in
569    * assign_attribute_or_color_locations()
570    */
571   bool validate_first_stage = first_stage != MESA_SHADER_VERTEX;
572   bool validate_last_stage = last_stage != MESA_SHADER_FRAGMENT;
573   if (!validate_first_stage && !validate_last_stage)
574      return;
575
576   struct explicit_location_info explicit_locations[MAX_VARYING][4];
577
578   gl_shader_stage stages[2] = { first_stage, last_stage };
579   bool validate_stage[2] = { validate_first_stage, validate_last_stage };
580   ir_variable_mode var_direction[2] = { ir_var_shader_in, ir_var_shader_out };
581
582   for (unsigned i = 0; i < 2; i++) {
583      if (!validate_stage[i])
584         continue;
585
586      gl_shader_stage stage = stages[i];
587
588      gl_linked_shader *sh = prog->_LinkedShaders[stage];
589      assert(sh);
590
591      memset(explicit_locations, 0, sizeof(explicit_locations));
592
593      foreach_in_list(ir_instruction, node, sh->ir) {
594         ir_variable *const var = node->as_variable();
595
596         if (var == NULL ||
597             !var->data.explicit_location ||
598             var->data.location < VARYING_SLOT_VAR0 ||
599             var->data.mode != var_direction[i])
600            continue;
601
602         if (!validate_explicit_variable_location(
603               consts, explicit_locations, var, prog, sh)) {
604            return;
605         }
606      }
607   }
608}
609
610/**
611 * Check if we should force input / output matching between shader
612 * interfaces.
613 *
614 * Section 4.3.4 (Inputs) of the GLSL 4.10 specifications say:
615 *
616 *   "Only the input variables that are actually read need to be
617 *    written by the previous stage; it is allowed to have
618 *    superfluous declarations of input variables."
619 *
620 * However it's not defined anywhere as to how we should handle
621 * inputs that are not written in the previous stage and it's not
622 * clear what "actually read" means.
623 *
624 * The GLSL 4.20 spec however is much clearer:
625 *
626 *    "Only the input variables that are statically read need to
627 *     be written by the previous stage; it is allowed to have
628 *     superfluous declarations of input variables."
629 *
630 * It also has a table that states it is an error to statically
631 * read an input that is not defined in the previous stage. While
632 * it is not an error to not statically write to the output (it
633 * just needs to be defined to not be an error).
634 *
635 * The text in the GLSL 4.20 spec was an attempt to clarify the
636 * previous spec iterations. However given the difference in spec
637 * and that some applications seem to depend on not erroring when
638 * the input is not actually read in control flow we only apply
639 * this rule to GLSL 4.20 and higher. GLSL 4.10 shaders have been
640 * seen in the wild that depend on the less strict interpretation.
641 */
642static bool
643static_input_output_matching(struct gl_shader_program *prog)
644{
645   return prog->data->Version >= (prog->IsES ? 0 : 420);
646}
647
648/**
649 * Validate that outputs from one stage match inputs of another
650 */
651void
652cross_validate_outputs_to_inputs(const struct gl_constants *consts,
653                                 struct gl_shader_program *prog,
654                                 gl_linked_shader *producer,
655                                 gl_linked_shader *consumer)
656{
657   glsl_symbol_table parameters;
658   struct explicit_location_info output_explicit_locations[MAX_VARYING][4] = {};
659   struct explicit_location_info input_explicit_locations[MAX_VARYING][4] = {};
660
661   /* Find all shader outputs in the "producer" stage.
662    */
663   foreach_in_list(ir_instruction, node, producer->ir) {
664      ir_variable *const var = node->as_variable();
665
666      if (var == NULL || var->data.mode != ir_var_shader_out)
667         continue;
668
669      if (!var->data.explicit_location
670          || var->data.location < VARYING_SLOT_VAR0)
671         parameters.add_variable(var);
672      else {
673         /* User-defined varyings with explicit locations are handled
674          * differently because they do not need to have matching names.
675          */
676         if (!validate_explicit_variable_location(consts,
677                                                  output_explicit_locations,
678                                                  var, prog, producer)) {
679            return;
680         }
681      }
682   }
683
684
685   /* Find all shader inputs in the "consumer" stage.  Any variables that have
686    * matching outputs already in the symbol table must have the same type and
687    * qualifiers.
688    *
689    * Exception: if the consumer is the geometry shader, then the inputs
690    * should be arrays and the type of the array element should match the type
691    * of the corresponding producer output.
692    */
693   foreach_in_list(ir_instruction, node, consumer->ir) {
694      ir_variable *const input = node->as_variable();
695
696      if (input == NULL || input->data.mode != ir_var_shader_in)
697         continue;
698
699      if (strcmp(input->name, "gl_Color") == 0 && input->data.used) {
700         const ir_variable *const front_color =
701            parameters.get_variable("gl_FrontColor");
702
703         const ir_variable *const back_color =
704            parameters.get_variable("gl_BackColor");
705
706         cross_validate_front_and_back_color(consts, prog, input,
707                                             front_color, back_color,
708                                             consumer->Stage, producer->Stage);
709      } else if (strcmp(input->name, "gl_SecondaryColor") == 0 && input->data.used) {
710         const ir_variable *const front_color =
711            parameters.get_variable("gl_FrontSecondaryColor");
712
713         const ir_variable *const back_color =
714            parameters.get_variable("gl_BackSecondaryColor");
715
716         cross_validate_front_and_back_color(consts, prog, input,
717                                             front_color, back_color,
718                                             consumer->Stage, producer->Stage);
719      } else {
720         /* The rules for connecting inputs and outputs change in the presence
721          * of explicit locations.  In this case, we no longer care about the
722          * names of the variables.  Instead, we care only about the
723          * explicitly assigned location.
724          */
725         ir_variable *output = NULL;
726         if (input->data.explicit_location
727             && input->data.location >= VARYING_SLOT_VAR0) {
728
729            const glsl_type *type = get_varying_type(input, consumer->Stage);
730            unsigned num_elements = type->count_attribute_slots(false);
731            unsigned idx =
732               compute_variable_location_slot(input, consumer->Stage);
733            unsigned slot_limit = idx + num_elements;
734
735            if (!validate_explicit_variable_location(consts,
736                                                     input_explicit_locations,
737                                                     input, prog, consumer)) {
738               return;
739            }
740
741            while (idx < slot_limit) {
742               if (idx >= MAX_VARYING) {
743                  linker_error(prog,
744                               "Invalid location %u in %s shader\n", idx,
745                               _mesa_shader_stage_to_string(consumer->Stage));
746                  return;
747               }
748
749               output = output_explicit_locations[idx][input->data.location_frac].var;
750
751               if (output == NULL) {
752                  /* A linker failure should only happen when there is no
753                   * output declaration and there is Static Use of the
754                   * declared input.
755                   */
756                  if (input->data.used && static_input_output_matching(prog)) {
757                     linker_error(prog,
758                                  "%s shader input `%s' with explicit location "
759                                  "has no matching output\n",
760                                  _mesa_shader_stage_to_string(consumer->Stage),
761                                  input->name);
762                     break;
763                  }
764               } else if (input->data.location != output->data.location) {
765                  linker_error(prog,
766                               "%s shader input `%s' with explicit location "
767                               "has no matching output\n",
768                               _mesa_shader_stage_to_string(consumer->Stage),
769                               input->name);
770                  break;
771               }
772               idx++;
773            }
774         } else {
775            output = parameters.get_variable(input->name);
776         }
777
778         if (output != NULL) {
779            /* Interface blocks have their own validation elsewhere so don't
780             * try validating them here.
781             */
782            if (!(input->get_interface_type() &&
783                  output->get_interface_type()))
784               cross_validate_types_and_qualifiers(consts, prog, input, output,
785                                                   consumer->Stage,
786                                                   producer->Stage);
787         } else {
788            /* Check for input vars with unmatched output vars in prev stage
789             * taking into account that interface blocks could have a matching
790             * output but with different name, so we ignore them.
791             */
792            assert(!input->data.assigned);
793            if (input->data.used && !input->get_interface_type() &&
794                !input->data.explicit_location &&
795                static_input_output_matching(prog))
796               linker_error(prog,
797                            "%s shader input `%s' "
798                            "has no matching output in the previous stage\n",
799                            _mesa_shader_stage_to_string(consumer->Stage),
800                            input->name);
801         }
802      }
803   }
804}
805