glsl: Put `sample`-qualified varyings in their own packing classes
[mesa.git] / src / glsl / link_varyings.cpp
index 5abc61293ae5c56a02326e7bfc8c8e23664604df..097cee5e4cb8dddecaba3a2b97f7c1cca8b02fc4 100644 (file)
 #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,
+                                    GLenum consumer_type,
+                                    GLenum producer_type)
+{
+   /* Check that the types match between stages.
+    */
+   const glsl_type *type_to_match = input->type;
+   if (consumer_type == GL_GEOMETRY_SHADER) {
+      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_glsl_shader_target_name(producer_type),
+                      output->name,
+                      output->type->name,
+                      _mesa_glsl_shader_target_name(consumer_type),
+                      input->type->name);
+         return;
+      }
+   }
+
+   /* 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",
+                   _mesa_glsl_shader_target_name(producer_type),
+                   output->name,
+                   (output->centroid) ? "has" : "lacks",
+                   _mesa_glsl_shader_target_name(consumer_type),
+                   (input->centroid) ? "has" : "lacks");
+      return;
+   }
+
+   if (input->sample != output->sample) {
+      linker_error(prog,
+                   "%s shader output `%s' %s sample qualifier, "
+                   "but %s shader input %s sample qualifier\n",
+                   _mesa_glsl_shader_target_name(producer_type),
+                   output->name,
+                   (output->sample) ? "has" : "lacks",
+                   _mesa_glsl_shader_target_name(consumer_type),
+                   (input->sample) ? "has" : "lacks");
+      return;
+   }
+
+   if (input->invariant != output->invariant) {
+      linker_error(prog,
+                   "%s shader output `%s' %s invariant qualifier, "
+                   "but %s shader input %s invariant qualifier\n",
+                   _mesa_glsl_shader_target_name(producer_type),
+                   output->name,
+                   (output->invariant) ? "has" : "lacks",
+                   _mesa_glsl_shader_target_name(consumer_type),
+                   (input->invariant) ? "has" : "lacks");
+      return;
+   }
+
+   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",
+                   _mesa_glsl_shader_target_name(producer_type),
+                   output->name,
+                   interpolation_string(output->interpolation),
+                   _mesa_glsl_shader_target_name(consumer_type),
+                   interpolation_string(input->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,
+                                    GLenum consumer_type,
+                                    GLenum producer_type)
+{
+   if (front_color != NULL && front_color->assigned)
+      cross_validate_types_and_qualifiers(prog, input, front_color,
+                                          consumer_type, producer_type);
+
+   if (back_color != NULL && back_color->assigned)
+      cross_validate_types_and_qualifiers(prog, input, back_color,
+                                          consumer_type, producer_type);
+}
+
 /**
  * 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;
-   const char *const producer_stage =
-      _mesa_glsl_shader_target_name(producer->Type);
-   const char *const consumer_stage =
-      _mesa_glsl_shader_target_name(consumer->Type);
 
    /* Find all shader outputs in the "producer" stage.
     */
@@ -68,6 +188,10 @@ 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();
@@ -75,84 +199,34 @@ cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
       if ((input == NULL) || (input->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->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->Type, producer->Type);
+      } else if (strcmp(input->name, "gl_SecondaryColor") == 0 && input->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->Type, producer->Type);
+      } else {
+         ir_variable *const output = parameters.get_variable(input->name);
+         if (output != NULL) {
+            cross_validate_types_and_qualifiers(prog, input, output,
+                                                consumer->Type, producer->Type);
+         }
       }
    }
-
-   return true;
 }
 
 
@@ -165,8 +239,8 @@ cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
  * will fail to find any matching variable.
  */
 void
-tfeedback_decl::init(struct gl_context *ctx, struct gl_shader_program *prog,
-                     const void *mem_ctx, const char *input)
+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.
@@ -266,7 +340,7 @@ tfeedback_decl::assign_location(struct gl_context *ctx,
       const unsigned vector_elements =
          this->matched_candidate->type->fields.array->vector_elements;
       unsigned actual_array_size = this->is_clip_distance_mesa ?
-         prog->Vert.ClipDistanceArraySize :
+         prog->LastClipDistanceArraySize :
          this->matched_candidate->type->array_size();
 
       if (this->is_subscripted) {
@@ -436,7 +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) {
-      decls[i].init(ctx, prog, mem_ctx, varying_names[i]);
+      decls[i].init(ctx, mem_ctx, varying_names[i]);
 
       if (!decls[i].is_varying())
          continue;
@@ -535,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
@@ -627,6 +702,7 @@ private:
    const bool consumer_is_fs;
 };
 
+} /* anonymous namespace */
 
 varying_matches::varying_matches(bool disable_varying_packing,
                                  bool consumer_is_fs)
@@ -689,10 +765,12 @@ varying_matches::record(ir_variable *producer_var, ir_variable *consumer_var)
        * requirement by changing the interpolation type to flat here.
        */
       producer_var->centroid = false;
+      producer_var->sample = false;
       producer_var->interpolation = INTERP_QUALIFIER_FLAT;
 
       if (consumer_var) {
          consumer_var->centroid = false;
+         consumer_var->sample = false;
          consumer_var->interpolation = INTERP_QUALIFIER_FLAT;
       }
    }
@@ -809,7 +887,7 @@ varying_matches::compute_packing_class(ir_variable *var)
     *
     * Therefore, the packing class depends only on the interpolation type.
     */
-   unsigned packing_class = var->centroid ? 1 : 0;
+   unsigned packing_class = var->centroid | (var->sample << 1);
    packing_class *= 4;
    packing_class += var->interpolation;
    return packing_class;
@@ -908,8 +986,8 @@ public:
       this->toplevel_var = var;
       this->varying_floats = 0;
       if (var->is_interface_instance())
-         program_resource_visitor::process(var->interface_type,
-                                           var->interface_type->name);
+         program_resource_visitor::process(var->get_interface_type(),
+                                           var->get_interface_type()->name);
       else
          program_resource_visitor::process(var);
    }
@@ -975,6 +1053,9 @@ private:
  *        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.
@@ -985,7 +1066,8 @@ 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)
 {
    const unsigned producer_base = VARYING_SLOT_VAR0;
    const unsigned consumer_base = VARYING_SLOT_VAR0;
@@ -1015,10 +1097,10 @@ assign_varying_locations(struct gl_context *ctx,
             ((ir_instruction *) node)->as_variable();
 
          if ((input_var != NULL) && (input_var->mode == ir_var_shader_in)) {
-            if (input_var->interface_type != NULL) {
+            if (input_var->get_interface_type() != NULL) {
                char *const iface_field_name =
                   ralloc_asprintf(mem_ctx, "%s.%s",
-                                  input_var->interface_type->name,
+                                  input_var->get_interface_type()->name,
                                   input_var->name);
                hash_table_insert(consumer_interface_inputs, input_var,
                                  iface_field_name);
@@ -1040,10 +1122,10 @@ assign_varying_locations(struct gl_context *ctx,
       g.process(output_var);
 
       ir_variable *input_var;
-      if (output_var->interface_type != NULL) {
+      if (output_var->get_interface_type() != NULL) {
          char *const iface_field_name =
             ralloc_asprintf(mem_ctx, "%s.%s",
-                            output_var->interface_type->name,
+                            output_var->get_interface_type()->name,
                             output_var->name);
          input_var =
             (ir_variable *) hash_table_find(consumer_interface_inputs,
@@ -1106,10 +1188,10 @@ 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_shader_out, producer);
+                            ir_var_shader_out, 0, producer);
       if (consumer) {
          lower_packed_varyings(mem_ctx, consumer_base, slots_used,
-                               ir_var_shader_in, consumer);
+                               ir_var_shader_in, gs_input_vertices, consumer);
       }
    }
 
@@ -1152,55 +1234,98 @@ assign_varying_locations(struct gl_context *ctx,
 }
 
 bool
-check_against_varying_limit(struct gl_context *ctx,
-                            struct gl_shader_program *prog,
-                            gl_shader *consumer)
+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->mode == ir_var_shader_out &&
+          is_varying_var(producer->Type, var)) {
+         output_vectors += var->type->count_attribute_slots();
+      }
+   }
+
+   unsigned max_output_components;
+   switch (producer->Type) {
+   case GL_VERTEX_SHADER:
+      max_output_components = ctx->Const.VertexProgram.MaxOutputComponents;
+      break;
+   case GL_GEOMETRY_SHADER:
+      max_output_components = ctx->Const.GeometryProgram.MaxOutputComponents;
+      break;
+   case GL_FRAGMENT_SHADER:
+   default:
+      assert(!"Should not get here.");
+      return false;
+   }
+
+   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 varying_vectors = 0;
+   unsigned input_vectors = 0;
 
    foreach_list(node, consumer->ir) {
       ir_variable *const var = ((ir_instruction *) node)->as_variable();
 
       if (var && var->mode == ir_var_shader_in &&
           is_varying_var(consumer->Type, var)) {
-         /* The packing rules used for vertex shader inputs are also
-          * used for fragment shader inputs.
-          */
-         varying_vectors += count_attribute_slots(var->type);
+         input_vectors += var->type->count_attribute_slots();
       }
    }
 
-   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;
-         }
-      }
-   } 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;
-         }
-      }
+   unsigned max_input_components;
+   switch (consumer->Type) {
+   case GL_GEOMETRY_SHADER:
+      max_input_components = ctx->Const.GeometryProgram.MaxInputComponents;
+      break;
+   case GL_FRAGMENT_SHADER:
+      max_input_components = ctx->Const.FragmentProgram.MaxInputComponents;
+      break;
+   case GL_VERTEX_SHADER:
+   default:
+      assert(!"Should not get here.");
+      return false;
+   }
+
+   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;