radeon / r200: Pass the API into _mesa_initialize_context
[mesa.git] / src / glsl / link_varyings.cpp
index b9c3f5d434d71fd9bff44214b23e211b916296ff..c925c00e39a1c2c1b95c727de323615cf838cf4b 100644 (file)
 
 #include "main/mtypes.h"
 #include "glsl_symbol_table.h"
+#include "glsl_parser_extras.h"
 #include "ir_optimization.h"
 #include "linker.h"
 #include "link_varyings.h"
 #include "main/macros.h"
+#include "program/hash_table.h"
+#include "program.h"
 
 
+/**
+ * Validate the types and qualifiers of an output from one stage against the
+ * matching input to another stage.
+ */
+static void
+cross_validate_types_and_qualifiers(struct gl_shader_program *prog,
+                                    const ir_variable *input,
+                                    const ir_variable *output,
+                                    gl_shader_stage consumer_stage,
+                                    gl_shader_stage producer_stage)
+{
+   /* Check that the types match between stages.
+    */
+   const glsl_type *type_to_match = input->type;
+   if (consumer_stage == MESA_SHADER_GEOMETRY) {
+      assert(type_to_match->is_array()); /* Enforced by ast_to_hir */
+      type_to_match = type_to_match->element_type();
+   }
+   if (type_to_match != output->type) {
+      /* There is a bit of a special case for gl_TexCoord.  This
+       * built-in is unsized by default.  Applications that variable
+       * access it must redeclare it with a size.  There is some
+       * language in the GLSL spec that implies the fragment shader
+       * and vertex shader do not have to agree on this size.  Other
+       * driver behave this way, and one or two applications seem to
+       * rely on it.
+       *
+       * Neither declaration needs to be modified here because the array
+       * sizes are fixed later when update_array_sizes is called.
+       *
+       * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
+       *
+       *     "Unlike user-defined varying variables, the built-in
+       *     varying variables don't have a strict one-to-one
+       *     correspondence between the vertex language and the
+       *     fragment language."
+       */
+      if (!output->type->is_array()
+          || (strncmp("gl_", output->name, 3) != 0)) {
+         linker_error(prog,
+                      "%s shader output `%s' declared as type `%s', "
+                      "but %s shader input declared as type `%s'\n",
+                      _mesa_shader_stage_to_string(producer_stage),
+                      output->name,
+                      output->type->name,
+                      _mesa_shader_stage_to_string(consumer_stage),
+                      input->type->name);
+         return;
+      }
+   }
+
+   /* Check that all of the qualifiers match between stages.
+    */
+   if (input->data.centroid != output->data.centroid) {
+      linker_error(prog,
+                   "%s shader output `%s' %s centroid qualifier, "
+                   "but %s shader input %s centroid qualifier\n",
+                   _mesa_shader_stage_to_string(producer_stage),
+                   output->name,
+                   (output->data.centroid) ? "has" : "lacks",
+                   _mesa_shader_stage_to_string(consumer_stage),
+                   (input->data.centroid) ? "has" : "lacks");
+      return;
+   }
+
+   if (input->data.sample != output->data.sample) {
+      linker_error(prog,
+                   "%s shader output `%s' %s sample qualifier, "
+                   "but %s shader input %s sample qualifier\n",
+                   _mesa_shader_stage_to_string(producer_stage),
+                   output->name,
+                   (output->data.sample) ? "has" : "lacks",
+                   _mesa_shader_stage_to_string(consumer_stage),
+                   (input->data.sample) ? "has" : "lacks");
+      return;
+   }
+
+   if (input->data.invariant != output->data.invariant) {
+      linker_error(prog,
+                   "%s shader output `%s' %s invariant qualifier, "
+                   "but %s shader input %s invariant qualifier\n",
+                   _mesa_shader_stage_to_string(producer_stage),
+                   output->name,
+                   (output->data.invariant) ? "has" : "lacks",
+                   _mesa_shader_stage_to_string(consumer_stage),
+                   (input->data.invariant) ? "has" : "lacks");
+      return;
+   }
+
+   if (input->data.interpolation != output->data.interpolation) {
+      linker_error(prog,
+                   "%s shader output `%s' specifies %s "
+                   "interpolation qualifier, "
+                   "but %s shader input specifies %s "
+                   "interpolation qualifier\n",
+                   _mesa_shader_stage_to_string(producer_stage),
+                   output->name,
+                   interpolation_string(output->data.interpolation),
+                   _mesa_shader_stage_to_string(consumer_stage),
+                   interpolation_string(input->data.interpolation));
+      return;
+   }
+}
+
+/**
+ * Validate front and back color outputs against single color input
+ */
+static void
+cross_validate_front_and_back_color(struct gl_shader_program *prog,
+                                    const ir_variable *input,
+                                    const ir_variable *front_color,
+                                    const ir_variable *back_color,
+                                    gl_shader_stage consumer_stage,
+                                    gl_shader_stage producer_stage)
+{
+   if (front_color != NULL && front_color->data.assigned)
+      cross_validate_types_and_qualifiers(prog, input, front_color,
+                                          consumer_stage, producer_stage);
+
+   if (back_color != NULL && back_color->data.assigned)
+      cross_validate_types_and_qualifiers(prog, input, back_color,
+                                          consumer_stage, producer_stage);
+}
+
 /**
  * Validate that outputs from one stage match inputs of another
  */
-bool
+void
 cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
                                 gl_shader *producer, gl_shader *consumer)
 {
    glsl_symbol_table parameters;
-   /* FINISHME: Figure these out dynamically. */
-   const char *const producer_stage = "vertex";
-   const char *const consumer_stage = "fragment";
 
    /* Find all shader outputs in the "producer" stage.
     */
    foreach_list(node, producer->ir) {
       ir_variable *const var = ((ir_instruction *) node)->as_variable();
 
-      /* FINISHME: For geometry shaders, this should also look for inout
-       * FINISHME: variables.
-       */
-      if ((var == NULL) || (var->mode != ir_var_out))
+      if ((var == NULL) || (var->data.mode != ir_var_shader_out))
         continue;
 
       parameters.add_variable(var);
@@ -67,105 +188,59 @@ cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
    /* Find all shader inputs in the "consumer" stage.  Any variables that have
     * matching outputs already in the symbol table must have the same type and
     * qualifiers.
+    *
+    * Exception: if the consumer is the geometry shader, then the inputs
+    * should be arrays and the type of the array element should match the type
+    * of the corresponding producer output.
     */
    foreach_list(node, consumer->ir) {
       ir_variable *const input = ((ir_instruction *) node)->as_variable();
 
-      /* FINISHME: For geometry shaders, this should also look for inout
-       * FINISHME: variables.
-       */
-      if ((input == NULL) || (input->mode != ir_var_in))
+      if ((input == NULL) || (input->data.mode != ir_var_shader_in))
         continue;
 
-      ir_variable *const output = parameters.get_variable(input->name);
-      if (output != NULL) {
-        /* Check that the types match between stages.
-         */
-        if (input->type != output->type) {
-           /* There is a bit of a special case for gl_TexCoord.  This
-            * built-in is unsized by default.  Applications that variable
-            * access it must redeclare it with a size.  There is some
-            * language in the GLSL spec that implies the fragment shader
-            * and vertex shader do not have to agree on this size.  Other
-            * driver behave this way, and one or two applications seem to
-            * rely on it.
-            *
-            * Neither declaration needs to be modified here because the array
-            * sizes are fixed later when update_array_sizes is called.
-            *
-            * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
-            *
-            *     "Unlike user-defined varying variables, the built-in
-            *     varying variables don't have a strict one-to-one
-            *     correspondence between the vertex language and the
-            *     fragment language."
-            */
-           if (!output->type->is_array()
-               || (strncmp("gl_", output->name, 3) != 0)) {
-              linker_error(prog,
-                           "%s shader output `%s' declared as type `%s', "
-                           "but %s shader input declared as type `%s'\n",
-                           producer_stage, output->name,
-                           output->type->name,
-                           consumer_stage, input->type->name);
-              return false;
-           }
-        }
-
-        /* Check that all of the qualifiers match between stages.
-         */
-        if (input->centroid != output->centroid) {
-           linker_error(prog,
-                        "%s shader output `%s' %s centroid qualifier, "
-                        "but %s shader input %s centroid qualifier\n",
-                        producer_stage,
-                        output->name,
-                        (output->centroid) ? "has" : "lacks",
-                        consumer_stage,
-                        (input->centroid) ? "has" : "lacks");
-           return false;
-        }
-
-        if (input->invariant != output->invariant) {
-           linker_error(prog,
-                        "%s shader output `%s' %s invariant qualifier, "
-                        "but %s shader input %s invariant qualifier\n",
-                        producer_stage,
-                        output->name,
-                        (output->invariant) ? "has" : "lacks",
-                        consumer_stage,
-                        (input->invariant) ? "has" : "lacks");
-           return false;
-        }
-
-        if (input->interpolation != output->interpolation) {
-           linker_error(prog,
-                        "%s shader output `%s' specifies %s "
-                        "interpolation qualifier, "
-                        "but %s shader input specifies %s "
-                        "interpolation qualifier\n",
-                        producer_stage,
-                        output->name,
-                        output->interpolation_string(),
-                        consumer_stage,
-                        input->interpolation_string());
-           return false;
-        }
+      if (strcmp(input->name, "gl_Color") == 0 && input->data.used) {
+         const ir_variable *const front_color =
+            parameters.get_variable("gl_FrontColor");
+
+         const ir_variable *const back_color =
+            parameters.get_variable("gl_BackColor");
+
+         cross_validate_front_and_back_color(prog, input,
+                                             front_color, back_color,
+                                             consumer->Stage, producer->Stage);
+      } else if (strcmp(input->name, "gl_SecondaryColor") == 0 && input->data.used) {
+         const ir_variable *const front_color =
+            parameters.get_variable("gl_FrontSecondaryColor");
+
+         const ir_variable *const back_color =
+            parameters.get_variable("gl_BackSecondaryColor");
+
+         cross_validate_front_and_back_color(prog, input,
+                                             front_color, back_color,
+                                             consumer->Stage, producer->Stage);
+      } else {
+         ir_variable *const output = parameters.get_variable(input->name);
+         if (output != NULL) {
+            cross_validate_types_and_qualifiers(prog, input, output,
+                                                consumer->Stage, producer->Stage);
+         }
       }
    }
-
-   return true;
 }
 
 
 /**
  * Initialize this object based on a string that was passed to
- * glTransformFeedbackVaryings.  If there is a parse error, the error is
- * reported using linker_error(), and false is returned.
+ * glTransformFeedbackVaryings.
+ *
+ * If the input is mal-formed, this call still succeeds, but it sets
+ * this->var_name to a mal-formed input, so tfeedback_decl::find_output_var()
+ * will fail to find any matching variable.
  */
-bool
-tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
-                     const void *mem_ctx, const char *input)
+void
+tfeedback_decl::init(struct gl_context *ctx, const void *mem_ctx,
+                     const char *input)
 {
    /* We don't have to be pedantic about what is a valid GLSL variable name,
     * because any variable with an invalid name can't exist in the IR anyway.
@@ -176,12 +251,13 @@ tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
    this->is_clip_distance_mesa = false;
    this->skip_components = 0;
    this->next_buffer_separator = false;
+   this->matched_candidate = NULL;
 
    if (ctx->Extensions.ARB_transform_feedback3) {
       /* Parse gl_NextBuffer. */
       if (strcmp(input, "gl_NextBuffer") == 0) {
          this->next_buffer_separator = true;
-         return true;
+         return;
       }
 
       /* Parse gl_SkipComponents. */
@@ -195,21 +271,17 @@ tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
          this->skip_components = 4;
 
       if (this->skip_components)
-         return true;
+         return;
    }
 
    /* Parse a declaration. */
-   const char *bracket = strrchr(input, '[');
-
-   if (bracket) {
-      this->var_name = ralloc_strndup(mem_ctx, input, bracket - input);
-      if (sscanf(bracket, "[%u]", &this->array_subscript) != 1) {
-         linker_error(prog, "Cannot parse transform feedback varying %s", input);
-         return false;
-      }
+   const char *base_name_end;
+   long subscript = parse_program_resource_name(input, &base_name_end);
+   this->var_name = ralloc_strndup(mem_ctx, input, base_name_end - input);
+   if (subscript >= 0) {
+      this->array_subscript = subscript;
       this->is_subscripted = true;
    } else {
-      this->var_name = ralloc_strdup(mem_ctx, input);
       this->is_subscripted = false;
    }
 
@@ -221,8 +293,6 @@ tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
        strcmp(this->var_name, "gl_ClipDistance") == 0) {
       this->is_clip_distance_mesa = true;
    }
-
-   return true;
 }
 
 
@@ -246,27 +316,32 @@ tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
 
 
 /**
- * Assign a location for this tfeedback_decl object based on the location
- * assignment in output_var.
+ * Assign a location for this tfeedback_decl object based on the transform
+ * feedback candidate found by find_candidate.
  *
  * If an error occurs, the error is reported through linker_error() and false
  * is returned.
  */
 bool
 tfeedback_decl::assign_location(struct gl_context *ctx,
-                                struct gl_shader_program *prog,
-                                ir_variable *output_var)
+                                struct gl_shader_program *prog)
 {
    assert(this->is_varying());
 
-   if (output_var->type->is_array()) {
+   unsigned fine_location
+      = this->matched_candidate->toplevel_var->data.location * 4
+      + this->matched_candidate->toplevel_var->data.location_frac
+      + this->matched_candidate->offset;
+
+   if (this->matched_candidate->type->is_array()) {
       /* Array variable */
       const unsigned matrix_cols =
-         output_var->type->fields.array->matrix_columns;
+         this->matched_candidate->type->fields.array->matrix_columns;
       const unsigned vector_elements =
-         output_var->type->fields.array->vector_elements;
+         this->matched_candidate->type->fields.array->vector_elements;
       unsigned actual_array_size = this->is_clip_distance_mesa ?
-         prog->Vert.ClipDistanceArraySize : output_var->type->array_size();
+         prog->LastClipDistanceArraySize :
+         this->matched_candidate->type->array_size();
 
       if (this->is_subscripted) {
          /* Check array bounds. */
@@ -277,22 +352,11 @@ tfeedback_decl::assign_location(struct gl_context *ctx,
                          actual_array_size);
             return false;
          }
-         if (this->is_clip_distance_mesa) {
-            this->location =
-               output_var->location + this->array_subscript / 4;
-            this->location_frac = this->array_subscript % 4;
-         } else {
-            unsigned fine_location
-               = output_var->location * 4 + output_var->location_frac;
-            unsigned array_elem_size = vector_elements * matrix_cols;
-            fine_location += array_elem_size * this->array_subscript;
-            this->location = fine_location / 4;
-            this->location_frac = fine_location % 4;
-         }
+         unsigned array_elem_size = this->is_clip_distance_mesa ?
+            1 : vector_elements * matrix_cols;
+         fine_location += array_elem_size * this->array_subscript;
          this->size = 1;
       } else {
-         this->location = output_var->location;
-         this->location_frac = output_var->location_frac;
          this->size = actual_array_size;
       }
       this->vector_elements = vector_elements;
@@ -300,7 +364,7 @@ tfeedback_decl::assign_location(struct gl_context *ctx,
       if (this->is_clip_distance_mesa)
          this->type = GL_FLOAT;
       else
-         this->type = output_var->type->fields.array->gl_type;
+         this->type = this->matched_candidate->type->fields.array->gl_type;
    } else {
       /* Regular variable (scalar, vector, or matrix) */
       if (this->is_subscripted) {
@@ -309,13 +373,13 @@ tfeedback_decl::assign_location(struct gl_context *ctx,
                       this->orig_name, this->var_name);
          return false;
       }
-      this->location = output_var->location;
-      this->location_frac = output_var->location_frac;
       this->size = 1;
-      this->vector_elements = output_var->type->vector_elements;
-      this->matrix_columns = output_var->type->matrix_columns;
-      this->type = output_var->type->gl_type;
+      this->vector_elements = this->matched_candidate->type->vector_elements;
+      this->matrix_columns = this->matched_candidate->type->matrix_columns;
+      this->type = this->matched_candidate->type->gl_type;
    }
+   this->location = fine_location / 4;
+   this->location_frac = fine_location % 4;
 
    /* From GL_EXT_transform_feedback:
     *   A program will fail to link if:
@@ -410,26 +474,26 @@ tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
 }
 
 
-ir_variable *
-tfeedback_decl::find_output_var(gl_shader_program *prog,
-                                gl_shader *producer) const
+const tfeedback_candidate *
+tfeedback_decl::find_candidate(gl_shader_program *prog,
+                               hash_table *tfeedback_candidates)
 {
    const char *name = this->is_clip_distance_mesa
       ? "gl_ClipDistanceMESA" : this->var_name;
-   ir_variable *var = producer->symbols->get_variable(name);
-   if (var && var->mode == ir_var_out)
-      return var;
-
-   /* From GL_EXT_transform_feedback:
-    *   A program will fail to link if:
-    *
-    *   * any variable name specified in the <varyings> array is not
-    *     declared as an output in the geometry shader (if present) or
-    *     the vertex shader (if no geometry shader is present);
-    */
-   linker_error(prog, "Transform feedback varying %s undeclared.",
-                this->orig_name);
-   return NULL;
+   this->matched_candidate = (const tfeedback_candidate *)
+      hash_table_find(tfeedback_candidates, name);
+   if (!this->matched_candidate) {
+      /* From GL_EXT_transform_feedback:
+       *   A program will fail to link if:
+       *
+       *   * any variable name specified in the <varyings> array is not
+       *     declared as an output in the geometry shader (if present) or
+       *     the vertex shader (if no geometry shader is present);
+       */
+      linker_error(prog, "Transform feedback varying %s undeclared.",
+                   this->orig_name);
+   }
+   return this->matched_candidate;
 }
 
 
@@ -446,8 +510,7 @@ parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
                       char **varying_names, tfeedback_decl *decls)
 {
    for (unsigned i = 0; i < num_names; ++i) {
-      if (!decls[i].init(ctx, prog, mem_ctx, varying_names[i]))
-         return false;
+      decls[i].init(ctx, mem_ctx, varying_names[i]);
 
       if (!decls[i].is_varying())
          continue;
@@ -546,6 +609,7 @@ store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
    return true;
 }
 
+namespace {
 
 /**
  * Data structure recording the relationship between outputs of one shader
@@ -554,7 +618,7 @@ store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
 class varying_matches
 {
 public:
-   varying_matches(bool disable_varying_packing);
+   varying_matches(bool disable_varying_packing, bool consumer_is_fs);
    ~varying_matches();
    void record(ir_variable *producer_var, ir_variable *consumer_var);
    unsigned assign_locations();
@@ -617,8 +681,8 @@ private:
       /**
        * The location which has been assigned for this varying.  This is
        * expressed in multiples of a float, with the first generic varying
-       * (i.e. the one referred to by VERT_RESULT_VAR0 or FRAG_ATTRIB_VAR0)
-       * represented by the value 0.
+       * (i.e. the one referred to by VARYING_SLOT_VAR0) represented by the
+       * value 0.
        */
       unsigned generic_location;
    } *matches;
@@ -634,11 +698,16 @@ private:
     * it was allocated.
     */
    unsigned matches_capacity;
+
+   const bool consumer_is_fs;
 };
 
+} /* anonymous namespace */
 
-varying_matches::varying_matches(bool disable_varying_packing)
-   : disable_varying_packing(disable_varying_packing)
+varying_matches::varying_matches(bool disable_varying_packing,
+                                 bool consumer_is_fs)
+   : disable_varying_packing(disable_varying_packing),
+     consumer_is_fs(consumer_is_fs)
 {
    /* Note: this initial capacity is rather arbitrarily chosen to be large
     * enough for many cases without wasting an unreasonable amount of space.
@@ -669,11 +738,15 @@ varying_matches::~varying_matches()
  * If \c producer_var has already been paired up with a consumer_var, or
  * producer_var is part of fixed pipeline functionality (and hence already has
  * a location assigned), this function has no effect.
+ *
+ * Note: as a side effect this function may change the interpolation type of
+ * \c producer_var, but only when the change couldn't possibly affect
+ * rendering.
  */
 void
 varying_matches::record(ir_variable *producer_var, ir_variable *consumer_var)
 {
-   if (!producer_var->is_unmatched_generic_inout) {
+   if (!producer_var->data.is_unmatched_generic_inout) {
       /* Either a location already exists for this variable (since it is part
        * of fixed functionality), or it has already been recorded as part of a
        * previous match.
@@ -681,6 +754,27 @@ varying_matches::record(ir_variable *producer_var, ir_variable *consumer_var)
       return;
    }
 
+   if ((consumer_var == NULL && producer_var->type->contains_integer()) ||
+       !consumer_is_fs) {
+      /* Since this varying is not being consumed by the fragment shader, its
+       * interpolation type varying cannot possibly affect rendering.  Also,
+       * this variable is non-flat and is (or contains) an integer.
+       *
+       * lower_packed_varyings requires all integer varyings to flat,
+       * regardless of where they appear.  We can trivially satisfy that
+       * requirement by changing the interpolation type to flat here.
+       */
+      producer_var->data.centroid = false;
+      producer_var->data.sample = false;
+      producer_var->data.interpolation = INTERP_QUALIFIER_FLAT;
+
+      if (consumer_var) {
+         consumer_var->data.centroid = false;
+         consumer_var->data.sample = false;
+         consumer_var->data.interpolation = INTERP_QUALIFIER_FLAT;
+      }
+   }
+
    if (this->num_matches == this->matches_capacity) {
       this->matches_capacity *= 2;
       this->matches = (match *)
@@ -704,9 +798,9 @@ varying_matches::record(ir_variable *producer_var, ir_variable *consumer_var)
    this->matches[this->num_matches].producer_var = producer_var;
    this->matches[this->num_matches].consumer_var = consumer_var;
    this->num_matches++;
-   producer_var->is_unmatched_generic_inout = 0;
+   producer_var->data.is_unmatched_generic_inout = 0;
    if (consumer_var)
-      consumer_var->is_unmatched_generic_inout = 0;
+      consumer_var->data.is_unmatched_generic_inout = 0;
 }
 
 
@@ -758,12 +852,12 @@ varying_matches::store_locations(unsigned producer_base,
       unsigned slot = generic_location / 4;
       unsigned offset = generic_location % 4;
 
-      producer_var->location = producer_base + slot;
-      producer_var->location_frac = offset;
+      producer_var->data.location = producer_base + slot;
+      producer_var->data.location_frac = offset;
       if (consumer_var) {
-         assert(consumer_var->location == -1);
-         consumer_var->location = consumer_base + slot;
-         consumer_var->location_frac = offset;
+         assert(consumer_var->data.location == -1);
+         consumer_var->data.location = consumer_base + slot;
+         consumer_var->data.location_frac = offset;
       }
    }
 }
@@ -777,17 +871,25 @@ varying_matches::store_locations(unsigned producer_base,
 unsigned
 varying_matches::compute_packing_class(ir_variable *var)
 {
-   /* In this initial implementation we conservatively assume that variables
-    * can only be packed if their base type (float/int/uint/bool) matches and
-    * their interpolation and centroid qualifiers match.
+   /* Without help from the back-end, there is no way to pack together
+    * variables with different interpolation types, because
+    * lower_packed_varyings must choose exactly one interpolation type for
+    * each packed varying it creates.
+    *
+    * However, we can safely pack together floats, ints, and uints, because:
+    *
+    * - varyings of base type "int" and "uint" must use the "flat"
+    *   interpolation type, which can only occur in GLSL 1.30 and above.
+    *
+    * - On platforms that support GLSL 1.30 and above, lower_packed_varyings
+    *   can store flat floats as ints without losing any information (using
+    *   the ir_unop_bitcast_* opcodes).
     *
-    * TODO: relax these restrictions when the driver back-end permits.
+    * Therefore, the packing class depends only on the interpolation type.
     */
-   unsigned packing_class = var->centroid ? 1 : 0;
+   unsigned packing_class = var->data.centroid | (var->data.sample << 1);
    packing_class *= 4;
-   packing_class += var->interpolation;
-   packing_class *= GLSL_TYPE_ERROR;
-   packing_class += var->type->get_scalar_type()->base_type;
+   packing_class += var->data.interpolation;
    return packing_class;
 }
 
@@ -802,16 +904,15 @@ varying_matches::compute_packing_order(ir_variable *var)
 {
    const glsl_type *element_type = var->type;
 
-   /* FINISHME: Support for "varying" records in GLSL 1.50. */
    while (element_type->base_type == GLSL_TYPE_ARRAY) {
       element_type = element_type->fields.array;
    }
 
-   switch (element_type->vector_elements) {
+   switch (element_type->component_slots() % 4) {
    case 1: return PACKING_ORDER_SCALAR;
    case 2: return PACKING_ORDER_VEC2;
    case 3: return PACKING_ORDER_VEC3;
-   case 4: return PACKING_ORDER_VEC4;
+   case 0: return PACKING_ORDER_VEC4;
    default:
       assert(!"Unexpected value of vector_elements");
       return PACKING_ORDER_VEC4;
@@ -842,15 +943,15 @@ varying_matches::match_comparator(const void *x_generic, const void *y_generic)
  * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
  */
 static bool
-is_varying_var(GLenum shaderType, const ir_variable *var)
+is_varying_var(gl_shader_stage stage, const ir_variable *var)
 {
    /* Only fragment shaders will take a varying variable as an input */
-   if (shaderType == GL_FRAGMENT_SHADER &&
-       var->mode == ir_var_in) {
-      switch (var->location) {
-      case FRAG_ATTRIB_WPOS:
-      case FRAG_ATTRIB_FACE:
-      case FRAG_ATTRIB_PNTC:
+   if (stage == MESA_SHADER_FRAGMENT &&
+       var->data.mode == ir_var_shader_in) {
+      switch (var->data.location) {
+      case VARYING_SLOT_POS:
+      case VARYING_SLOT_FACE:
+      case VARYING_SLOT_PNTC:
          return false;
       default:
          return true;
@@ -860,6 +961,82 @@ is_varying_var(GLenum shaderType, const ir_variable *var)
 }
 
 
+/**
+ * Visitor class that generates tfeedback_candidate structs describing all
+ * possible targets of transform feedback.
+ *
+ * tfeedback_candidate structs are stored in the hash table
+ * tfeedback_candidates, which is passed to the constructor.  This hash table
+ * maps varying names to instances of the tfeedback_candidate struct.
+ */
+class tfeedback_candidate_generator : public program_resource_visitor
+{
+public:
+   tfeedback_candidate_generator(void *mem_ctx,
+                                 hash_table *tfeedback_candidates)
+      : mem_ctx(mem_ctx),
+        tfeedback_candidates(tfeedback_candidates),
+        toplevel_var(NULL),
+        varying_floats(0)
+   {
+   }
+
+   void process(ir_variable *var)
+   {
+      this->toplevel_var = var;
+      this->varying_floats = 0;
+      if (var->is_interface_instance())
+         program_resource_visitor::process(var->get_interface_type(),
+                                           var->get_interface_type()->name);
+      else
+         program_resource_visitor::process(var);
+   }
+
+private:
+   virtual void visit_field(const glsl_type *type, const char *name,
+                            bool row_major)
+   {
+      assert(!type->is_record());
+      assert(!(type->is_array() && type->fields.array->is_record()));
+      assert(!type->is_interface());
+      assert(!(type->is_array() && type->fields.array->is_interface()));
+
+      (void) row_major;
+
+      tfeedback_candidate *candidate
+         = rzalloc(this->mem_ctx, tfeedback_candidate);
+      candidate->toplevel_var = this->toplevel_var;
+      candidate->type = type;
+      candidate->offset = this->varying_floats;
+      hash_table_insert(this->tfeedback_candidates, candidate,
+                        ralloc_strdup(this->mem_ctx, name));
+      this->varying_floats += type->component_slots();
+   }
+
+   /**
+    * Memory context used to allocate hash table keys and values.
+    */
+   void * const mem_ctx;
+
+   /**
+    * Hash table in which tfeedback_candidate objects should be stored.
+    */
+   hash_table * const tfeedback_candidates;
+
+   /**
+    * Pointer to the toplevel variable that is being traversed.
+    */
+   ir_variable *toplevel_var;
+
+   /**
+    * Total number of varying floats that have been visited so far.  This is
+    * used to determine the offset to each varying within the toplevel
+    * variable.
+    */
+   unsigned varying_floats;
+};
+
+
 /**
  * Assign locations for all variables that are produced in one pipeline stage
  * (the "producer") and consumed in the next stage (the "consumer").
@@ -876,6 +1053,9 @@ is_varying_var(GLenum shaderType, const ir_variable *var)
  *        each of these objects that matches one of the outputs of the
  *        producer.
  *
+ * \param gs_input_vertices: if \c consumer is a geometry shader, this is the
+ *        number of input vertices it accepts.  Otherwise zero.
+ *
  * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
  * be NULL.  In this case, varying locations are assigned solely based on the
  * requirements of transform feedback.
@@ -886,12 +1066,19 @@ assign_varying_locations(struct gl_context *ctx,
                         struct gl_shader_program *prog,
                         gl_shader *producer, gl_shader *consumer,
                          unsigned num_tfeedback_decls,
-                         tfeedback_decl *tfeedback_decls)
+                         tfeedback_decl *tfeedback_decls,
+                         unsigned gs_input_vertices)
 {
-   /* FINISHME: Set dynamically when geometry shader support is added. */
-   const unsigned producer_base = VERT_RESULT_VAR0;
-   const unsigned consumer_base = FRAG_ATTRIB_VAR0;
-   varying_matches matches(ctx->Const.DisableVaryingPacking);
+   const unsigned producer_base = VARYING_SLOT_VAR0;
+   const unsigned consumer_base = VARYING_SLOT_VAR0;
+   varying_matches matches(ctx->Const.DisableVaryingPacking,
+                           consumer && consumer->Stage == MESA_SHADER_FRAGMENT);
+   hash_table *tfeedback_candidates
+      = hash_table_ctor(0, hash_table_string_hash, hash_table_string_compare);
+   hash_table *consumer_inputs
+      = hash_table_ctor(0, hash_table_string_hash, hash_table_string_compare);
+   hash_table *consumer_interface_inputs
+      = hash_table_ctor(0, hash_table_string_hash, hash_table_string_compare);
 
    /* Operate in a total of three passes.
     *
@@ -904,16 +1091,51 @@ assign_varying_locations(struct gl_context *ctx,
     *    not being inputs.  This lets the optimizer eliminate them.
     */
 
+   if (consumer) {
+      foreach_list(node, consumer->ir) {
+         ir_variable *const input_var =
+            ((ir_instruction *) node)->as_variable();
+
+         if ((input_var != NULL) && (input_var->data.mode == ir_var_shader_in)) {
+            if (input_var->get_interface_type() != NULL) {
+               char *const iface_field_name =
+                  ralloc_asprintf(mem_ctx, "%s.%s",
+                                  input_var->get_interface_type()->name,
+                                  input_var->name);
+               hash_table_insert(consumer_interface_inputs, input_var,
+                                 iface_field_name);
+            } else {
+               hash_table_insert(consumer_inputs, input_var,
+                                 ralloc_strdup(mem_ctx, input_var->name));
+            }
+         }
+      }
+   }
+
    foreach_list(node, producer->ir) {
       ir_variable *const output_var = ((ir_instruction *) node)->as_variable();
 
-      if ((output_var == NULL) || (output_var->mode != ir_var_out))
+      if ((output_var == NULL) || (output_var->data.mode != ir_var_shader_out))
         continue;
 
-      ir_variable *input_var =
-        consumer ? consumer->symbols->get_variable(output_var->name) : NULL;
+      tfeedback_candidate_generator g(mem_ctx, tfeedback_candidates);
+      g.process(output_var);
+
+      ir_variable *input_var;
+      if (output_var->get_interface_type() != NULL) {
+         char *const iface_field_name =
+            ralloc_asprintf(mem_ctx, "%s.%s",
+                            output_var->get_interface_type()->name,
+                            output_var->name);
+         input_var =
+            (ir_variable *) hash_table_find(consumer_interface_inputs,
+                                            iface_field_name);
+      } else {
+         input_var =
+            (ir_variable *) hash_table_find(consumer_inputs, output_var->name);
+      }
 
-      if (input_var && input_var->mode != ir_var_in)
+      if (input_var && input_var->data.mode != ir_var_shader_in)
          input_var = NULL;
 
       if (input_var) {
@@ -925,15 +1147,18 @@ assign_varying_locations(struct gl_context *ctx,
       if (!tfeedback_decls[i].is_varying())
          continue;
 
-      ir_variable *output_var
-         = tfeedback_decls[i].find_output_var(prog, producer);
+      const tfeedback_candidate *matched_candidate
+         = tfeedback_decls[i].find_candidate(prog, tfeedback_candidates);
 
-      if (output_var == NULL)
+      if (matched_candidate == NULL) {
+         hash_table_dtor(tfeedback_candidates);
+         hash_table_dtor(consumer_inputs);
+         hash_table_dtor(consumer_interface_inputs);
          return false;
-
-      if (output_var->is_unmatched_generic_inout) {
-         matches.record(output_var, NULL);
       }
+
+      if (matched_candidate->toplevel_var->data.is_unmatched_generic_inout)
+         matches.record(matched_candidate->toplevel_var, NULL);
    }
 
    const unsigned slots_used = matches.assign_locations();
@@ -943,13 +1168,18 @@ assign_varying_locations(struct gl_context *ctx,
       if (!tfeedback_decls[i].is_varying())
          continue;
 
-      ir_variable *output_var
-         = tfeedback_decls[i].find_output_var(prog, producer);
-
-      if (!tfeedback_decls[i].assign_location(ctx, prog, output_var))
+      if (!tfeedback_decls[i].assign_location(ctx, prog)) {
+         hash_table_dtor(tfeedback_candidates);
+         hash_table_dtor(consumer_inputs);
+         hash_table_dtor(consumer_interface_inputs);
          return false;
+      }
    }
 
+   hash_table_dtor(tfeedback_candidates);
+   hash_table_dtor(consumer_inputs);
+   hash_table_dtor(consumer_interface_inputs);
+
    if (ctx->Const.DisableVaryingPacking) {
       /* Transform feedback code assumes varyings are packed, so if the driver
        * has disabled varying packing, make sure it does not support transform
@@ -957,24 +1187,20 @@ assign_varying_locations(struct gl_context *ctx,
        */
       assert(!ctx->Extensions.EXT_transform_feedback);
    } else {
-      lower_packed_varyings(mem_ctx, producer_base, slots_used, ir_var_out,
-                            producer);
+      lower_packed_varyings(mem_ctx, producer_base, slots_used,
+                            ir_var_shader_out, 0, producer);
       if (consumer) {
-         lower_packed_varyings(mem_ctx, consumer_base, slots_used, ir_var_in,
-                               consumer);
+         lower_packed_varyings(mem_ctx, consumer_base, slots_used,
+                               ir_var_shader_in, gs_input_vertices, consumer);
       }
    }
 
-   unsigned varying_vectors = 0;
-
    if (consumer) {
       foreach_list(node, consumer->ir) {
          ir_variable *const var = ((ir_instruction *) node)->as_variable();
 
-         if ((var == NULL) || (var->mode != ir_var_in))
-            continue;
-
-         if (var->is_unmatched_generic_inout) {
+         if (var && var->data.mode == ir_var_shader_in &&
+             var->data.is_unmatched_generic_inout) {
             if (prog->Version <= 120) {
                /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
                 *
@@ -989,55 +1215,98 @@ assign_varying_locations(struct gl_context *ctx,
                 * "glsl1-varying read but not written" in piglit.
                 */
 
-               linker_error(prog, "fragment shader varying %s not written "
-                            "by vertex shader\n.", var->name);
+               linker_error(prog, "%s shader varying %s not written "
+                            "by %s shader\n.",
+                            _mesa_shader_stage_to_string(consumer->Stage),
+                           var->name,
+                            _mesa_shader_stage_to_string(producer->Stage));
             }
 
             /* An 'in' variable is only really a shader input if its
              * value is written by the previous stage.
              */
-            var->mode = ir_var_auto;
-         } else if (is_varying_var(consumer->Type, var)) {
-            /* The packing rules are used for vertex shader inputs are also
-             * used for fragment shader inputs.
-             */
-            varying_vectors += count_attribute_slots(var->type);
+            var->data.mode = ir_var_auto;
          }
       }
    }
 
-   if (ctx->API == API_OPENGLES2 || prog->IsES) {
-      if (varying_vectors > ctx->Const.MaxVarying) {
-         if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
-            linker_warning(prog, "shader uses too many varying vectors "
-                           "(%u > %u), but the driver will try to optimize "
-                           "them out; this is non-portable out-of-spec "
-                           "behavior\n",
-                           varying_vectors, ctx->Const.MaxVarying);
-         } else {
-            linker_error(prog, "shader uses too many varying vectors "
-                         "(%u > %u)\n",
-                         varying_vectors, ctx->Const.MaxVarying);
-            return false;
-         }
+   return true;
+}
+
+bool
+check_against_output_limit(struct gl_context *ctx,
+                           struct gl_shader_program *prog,
+                           gl_shader *producer)
+{
+   unsigned output_vectors = 0;
+
+   foreach_list(node, producer->ir) {
+      ir_variable *const var = ((ir_instruction *) node)->as_variable();
+
+      if (var && var->data.mode == ir_var_shader_out &&
+          is_varying_var(producer->Stage, var)) {
+         output_vectors += var->type->count_attribute_slots();
       }
-   } else {
-      const unsigned float_components = varying_vectors * 4;
-      if (float_components > ctx->Const.MaxVarying * 4) {
-         if (ctx->Const.GLSLSkipStrictMaxVaryingLimitCheck) {
-            linker_warning(prog, "shader uses too many varying components "
-                           "(%u > %u), but the driver will try to optimize "
-                           "them out; this is non-portable out-of-spec "
-                           "behavior\n",
-                           float_components, ctx->Const.MaxVarying * 4);
-         } else {
-            linker_error(prog, "shader uses too many varying components "
-                         "(%u > %u)\n",
-                         float_components, ctx->Const.MaxVarying * 4);
-            return false;
-         }
+   }
+
+   assert(producer->Stage != MESA_SHADER_FRAGMENT);
+   unsigned max_output_components =
+      ctx->Const.Program[producer->Stage].MaxOutputComponents;
+
+   const unsigned output_components = output_vectors * 4;
+   if (output_components > max_output_components) {
+      if (ctx->API == API_OPENGLES2 || prog->IsES)
+         linker_error(prog, "shader uses too many output vectors "
+                      "(%u > %u)\n",
+                      output_vectors,
+                      max_output_components / 4);
+      else
+         linker_error(prog, "shader uses too many output components "
+                      "(%u > %u)\n",
+                      output_components,
+                      max_output_components);
+
+      return false;
+   }
+
+   return true;
+}
+
+bool
+check_against_input_limit(struct gl_context *ctx,
+                          struct gl_shader_program *prog,
+                          gl_shader *consumer)
+{
+   unsigned input_vectors = 0;
+
+   foreach_list(node, consumer->ir) {
+      ir_variable *const var = ((ir_instruction *) node)->as_variable();
+
+      if (var && var->data.mode == ir_var_shader_in &&
+          is_varying_var(consumer->Stage, var)) {
+         input_vectors += var->type->count_attribute_slots();
       }
    }
 
+   assert(consumer->Stage != MESA_SHADER_VERTEX);
+   unsigned max_input_components =
+      ctx->Const.Program[consumer->Stage].MaxInputComponents;
+
+   const unsigned input_components = input_vectors * 4;
+   if (input_components > max_input_components) {
+      if (ctx->API == API_OPENGLES2 || prog->IsES)
+         linker_error(prog, "shader uses too many input vectors "
+                      "(%u > %u)\n",
+                      input_vectors,
+                      max_input_components / 4);
+      else
+         linker_error(prog, "shader uses too many input components "
+                      "(%u > %u)\n",
+                      input_components,
+                      max_input_components);
+
+      return false;
+   }
+
    return true;
 }