Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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/mtypes.h"
  33. #include "glsl_symbol_table.h"
  34. #include "glsl_parser_extras.h"
  35. #include "ir_optimization.h"
  36. #include "linker.h"
  37. #include "link_varyings.h"
  38. #include "main/macros.h"
  39. #include "program/hash_table.h"
  40. #include "program.h"
  41.  
  42.  
  43. /**
  44.  * Validate the types and qualifiers of an output from one stage against the
  45.  * matching input to another stage.
  46.  */
  47. static void
  48. cross_validate_types_and_qualifiers(struct gl_shader_program *prog,
  49.                                     const ir_variable *input,
  50.                                     const ir_variable *output,
  51.                                     gl_shader_stage consumer_stage,
  52.                                     gl_shader_stage producer_stage)
  53. {
  54.    /* Check that the types match between stages.
  55.     */
  56.    const glsl_type *type_to_match = input->type;
  57.    if (consumer_stage == MESA_SHADER_GEOMETRY) {
  58.       assert(type_to_match->is_array()); /* Enforced by ast_to_hir */
  59.       type_to_match = type_to_match->element_type();
  60.    }
  61.    if (type_to_match != output->type) {
  62.       /* There is a bit of a special case for gl_TexCoord.  This
  63.        * built-in is unsized by default.  Applications that variable
  64.        * access it must redeclare it with a size.  There is some
  65.        * language in the GLSL spec that implies the fragment shader
  66.        * and vertex shader do not have to agree on this size.  Other
  67.        * driver behave this way, and one or two applications seem to
  68.        * rely on it.
  69.        *
  70.        * Neither declaration needs to be modified here because the array
  71.        * sizes are fixed later when update_array_sizes is called.
  72.        *
  73.        * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
  74.        *
  75.        *     "Unlike user-defined varying variables, the built-in
  76.        *     varying variables don't have a strict one-to-one
  77.        *     correspondence between the vertex language and the
  78.        *     fragment language."
  79.        */
  80.       if (!output->type->is_array() || !is_gl_identifier(output->name)) {
  81.          linker_error(prog,
  82.                       "%s shader output `%s' declared as type `%s', "
  83.                       "but %s shader input declared as type `%s'\n",
  84.                       _mesa_shader_stage_to_string(producer_stage),
  85.                       output->name,
  86.                       output->type->name,
  87.                       _mesa_shader_stage_to_string(consumer_stage),
  88.                       input->type->name);
  89.          return;
  90.       }
  91.    }
  92.  
  93.    /* Check that all of the qualifiers match between stages.
  94.     */
  95.    if (input->data.centroid != output->data.centroid) {
  96.       linker_error(prog,
  97.                    "%s shader output `%s' %s centroid qualifier, "
  98.                    "but %s shader input %s centroid qualifier\n",
  99.                    _mesa_shader_stage_to_string(producer_stage),
  100.                    output->name,
  101.                    (output->data.centroid) ? "has" : "lacks",
  102.                    _mesa_shader_stage_to_string(consumer_stage),
  103.                    (input->data.centroid) ? "has" : "lacks");
  104.       return;
  105.    }
  106.  
  107.    if (input->data.sample != output->data.sample) {
  108.       linker_error(prog,
  109.                    "%s shader output `%s' %s sample qualifier, "
  110.                    "but %s shader input %s sample qualifier\n",
  111.                    _mesa_shader_stage_to_string(producer_stage),
  112.                    output->name,
  113.                    (output->data.sample) ? "has" : "lacks",
  114.                    _mesa_shader_stage_to_string(consumer_stage),
  115.                    (input->data.sample) ? "has" : "lacks");
  116.       return;
  117.    }
  118.  
  119.    if (!prog->IsES && input->data.invariant != output->data.invariant) {
  120.       linker_error(prog,
  121.                    "%s shader output `%s' %s invariant qualifier, "
  122.                    "but %s shader input %s invariant qualifier\n",
  123.                    _mesa_shader_stage_to_string(producer_stage),
  124.                    output->name,
  125.                    (output->data.invariant) ? "has" : "lacks",
  126.                    _mesa_shader_stage_to_string(consumer_stage),
  127.                    (input->data.invariant) ? "has" : "lacks");
  128.       return;
  129.    }
  130.  
  131.    if (input->data.interpolation != output->data.interpolation) {
  132.       linker_error(prog,
  133.                    "%s shader output `%s' specifies %s "
  134.                    "interpolation qualifier, "
  135.                    "but %s shader input specifies %s "
  136.                    "interpolation qualifier\n",
  137.                    _mesa_shader_stage_to_string(producer_stage),
  138.                    output->name,
  139.                    interpolation_string(output->data.interpolation),
  140.                    _mesa_shader_stage_to_string(consumer_stage),
  141.                    interpolation_string(input->data.interpolation));
  142.       return;
  143.    }
  144. }
  145.  
  146. /**
  147.  * Validate front and back color outputs against single color input
  148.  */
  149. static void
  150. cross_validate_front_and_back_color(struct gl_shader_program *prog,
  151.                                     const ir_variable *input,
  152.                                     const ir_variable *front_color,
  153.                                     const ir_variable *back_color,
  154.                                     gl_shader_stage consumer_stage,
  155.                                     gl_shader_stage producer_stage)
  156. {
  157.    if (front_color != NULL && front_color->data.assigned)
  158.       cross_validate_types_and_qualifiers(prog, input, front_color,
  159.                                           consumer_stage, producer_stage);
  160.  
  161.    if (back_color != NULL && back_color->data.assigned)
  162.       cross_validate_types_and_qualifiers(prog, input, back_color,
  163.                                           consumer_stage, producer_stage);
  164. }
  165.  
  166. /**
  167.  * Validate that outputs from one stage match inputs of another
  168.  */
  169. void
  170. cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
  171.                                  gl_shader *producer, gl_shader *consumer)
  172. {
  173.    glsl_symbol_table parameters;
  174.    ir_variable *explicit_locations[MAX_VARYING] = { NULL, };
  175.  
  176.    /* Find all shader outputs in the "producer" stage.
  177.     */
  178.    foreach_in_list(ir_instruction, node, producer->ir) {
  179.       ir_variable *const var = node->as_variable();
  180.  
  181.       if ((var == NULL) || (var->data.mode != ir_var_shader_out))
  182.          continue;
  183.  
  184.       if (!var->data.explicit_location
  185.           || var->data.location < VARYING_SLOT_VAR0)
  186.          parameters.add_variable(var);
  187.       else {
  188.          /* User-defined varyings with explicit locations are handled
  189.           * differently because they do not need to have matching names.
  190.           */
  191.          const unsigned idx = var->data.location - VARYING_SLOT_VAR0;
  192.  
  193.          if (explicit_locations[idx] != NULL) {
  194.             linker_error(prog,
  195.                          "%s shader has multiple outputs explicitly "
  196.                          "assigned to location %d\n",
  197.                          _mesa_shader_stage_to_string(producer->Stage),
  198.                          idx);
  199.             return;
  200.          }
  201.  
  202.          explicit_locations[idx] = var;
  203.       }
  204.    }
  205.  
  206.  
  207.    /* Find all shader inputs in the "consumer" stage.  Any variables that have
  208.     * matching outputs already in the symbol table must have the same type and
  209.     * qualifiers.
  210.     *
  211.     * Exception: if the consumer is the geometry shader, then the inputs
  212.     * should be arrays and the type of the array element should match the type
  213.     * of the corresponding producer output.
  214.     */
  215.    foreach_in_list(ir_instruction, node, consumer->ir) {
  216.       ir_variable *const input = node->as_variable();
  217.  
  218.       if ((input == NULL) || (input->data.mode != ir_var_shader_in))
  219.          continue;
  220.  
  221.       if (strcmp(input->name, "gl_Color") == 0 && input->data.used) {
  222.          const ir_variable *const front_color =
  223.             parameters.get_variable("gl_FrontColor");
  224.  
  225.          const ir_variable *const back_color =
  226.             parameters.get_variable("gl_BackColor");
  227.  
  228.          cross_validate_front_and_back_color(prog, input,
  229.                                              front_color, back_color,
  230.                                              consumer->Stage, producer->Stage);
  231.       } else if (strcmp(input->name, "gl_SecondaryColor") == 0 && input->data.used) {
  232.          const ir_variable *const front_color =
  233.             parameters.get_variable("gl_FrontSecondaryColor");
  234.  
  235.          const ir_variable *const back_color =
  236.             parameters.get_variable("gl_BackSecondaryColor");
  237.  
  238.          cross_validate_front_and_back_color(prog, input,
  239.                                              front_color, back_color,
  240.                                              consumer->Stage, producer->Stage);
  241.       } else {
  242.          /* The rules for connecting inputs and outputs change in the presence
  243.           * of explicit locations.  In this case, we no longer care about the
  244.           * names of the variables.  Instead, we care only about the
  245.           * explicitly assigned location.
  246.           */
  247.          ir_variable *output = NULL;
  248.          if (input->data.explicit_location
  249.              && input->data.location >= VARYING_SLOT_VAR0) {
  250.             output = explicit_locations[input->data.location - VARYING_SLOT_VAR0];
  251.  
  252.             if (output == NULL) {
  253.                linker_error(prog,
  254.                             "%s shader input `%s' with explicit location "
  255.                             "has no matching output\n",
  256.                             _mesa_shader_stage_to_string(consumer->Stage),
  257.                             input->name);
  258.             }
  259.          } else {
  260.             output = parameters.get_variable(input->name);
  261.          }
  262.  
  263.          if (output != NULL) {
  264.             cross_validate_types_and_qualifiers(prog, input, output,
  265.                                                 consumer->Stage, producer->Stage);
  266.          } else {
  267.             /* Check for input vars with unmatched output vars in prev stage
  268.              * taking into account that interface blocks could have a matching
  269.              * output but with different name, so we ignore them.
  270.              */
  271.             assert(!input->data.assigned);
  272.             if (input->data.used && !input->get_interface_type() &&
  273.                 !input->data.explicit_location && !prog->SeparateShader)
  274.                linker_error(prog,
  275.                             "%s shader input `%s' "
  276.                             "has no matching output in the previous stage\n",
  277.                             _mesa_shader_stage_to_string(consumer->Stage),
  278.                             input->name);
  279.          }
  280.       }
  281.    }
  282. }
  283.  
  284.  
  285. /**
  286.  * Initialize this object based on a string that was passed to
  287.  * glTransformFeedbackVaryings.
  288.  *
  289.  * If the input is mal-formed, this call still succeeds, but it sets
  290.  * this->var_name to a mal-formed input, so tfeedback_decl::find_output_var()
  291.  * will fail to find any matching variable.
  292.  */
  293. void
  294. tfeedback_decl::init(struct gl_context *ctx, const void *mem_ctx,
  295.                      const char *input)
  296. {
  297.    /* We don't have to be pedantic about what is a valid GLSL variable name,
  298.     * because any variable with an invalid name can't exist in the IR anyway.
  299.     */
  300.  
  301.    this->location = -1;
  302.    this->orig_name = input;
  303.    this->is_clip_distance_mesa = false;
  304.    this->skip_components = 0;
  305.    this->next_buffer_separator = false;
  306.    this->matched_candidate = NULL;
  307.    this->stream_id = 0;
  308.  
  309.    if (ctx->Extensions.ARB_transform_feedback3) {
  310.       /* Parse gl_NextBuffer. */
  311.       if (strcmp(input, "gl_NextBuffer") == 0) {
  312.          this->next_buffer_separator = true;
  313.          return;
  314.       }
  315.  
  316.       /* Parse gl_SkipComponents. */
  317.       if (strcmp(input, "gl_SkipComponents1") == 0)
  318.          this->skip_components = 1;
  319.       else if (strcmp(input, "gl_SkipComponents2") == 0)
  320.          this->skip_components = 2;
  321.       else if (strcmp(input, "gl_SkipComponents3") == 0)
  322.          this->skip_components = 3;
  323.       else if (strcmp(input, "gl_SkipComponents4") == 0)
  324.          this->skip_components = 4;
  325.  
  326.       if (this->skip_components)
  327.          return;
  328.    }
  329.  
  330.    /* Parse a declaration. */
  331.    const char *base_name_end;
  332.    long subscript = parse_program_resource_name(input, &base_name_end);
  333.    this->var_name = ralloc_strndup(mem_ctx, input, base_name_end - input);
  334.    if (this->var_name == NULL) {
  335.       _mesa_error_no_memory(__func__);
  336.       return;
  337.    }
  338.  
  339.    if (subscript >= 0) {
  340.       this->array_subscript = subscript;
  341.       this->is_subscripted = true;
  342.    } else {
  343.       this->is_subscripted = false;
  344.    }
  345.  
  346.    /* For drivers that lower gl_ClipDistance to gl_ClipDistanceMESA, this
  347.     * class must behave specially to account for the fact that gl_ClipDistance
  348.     * is converted from a float[8] to a vec4[2].
  349.     */
  350.    if (ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].LowerClipDistance &&
  351.        strcmp(this->var_name, "gl_ClipDistance") == 0) {
  352.       this->is_clip_distance_mesa = true;
  353.    }
  354. }
  355.  
  356.  
  357. /**
  358.  * Determine whether two tfeedback_decl objects refer to the same variable and
  359.  * array index (if applicable).
  360.  */
  361. bool
  362. tfeedback_decl::is_same(const tfeedback_decl &x, const tfeedback_decl &y)
  363. {
  364.    assert(x.is_varying() && y.is_varying());
  365.  
  366.    if (strcmp(x.var_name, y.var_name) != 0)
  367.       return false;
  368.    if (x.is_subscripted != y.is_subscripted)
  369.       return false;
  370.    if (x.is_subscripted && x.array_subscript != y.array_subscript)
  371.       return false;
  372.    return true;
  373. }
  374.  
  375.  
  376. /**
  377.  * Assign a location and stream ID for this tfeedback_decl object based on the
  378.  * transform feedback candidate found by find_candidate.
  379.  *
  380.  * If an error occurs, the error is reported through linker_error() and false
  381.  * is returned.
  382.  */
  383. bool
  384. tfeedback_decl::assign_location(struct gl_context *ctx,
  385.                                 struct gl_shader_program *prog)
  386. {
  387.    assert(this->is_varying());
  388.  
  389.    unsigned fine_location
  390.       = this->matched_candidate->toplevel_var->data.location * 4
  391.       + this->matched_candidate->toplevel_var->data.location_frac
  392.       + this->matched_candidate->offset;
  393.  
  394.    if (this->matched_candidate->type->is_array()) {
  395.       /* Array variable */
  396.       const unsigned matrix_cols =
  397.          this->matched_candidate->type->fields.array->matrix_columns;
  398.       const unsigned vector_elements =
  399.          this->matched_candidate->type->fields.array->vector_elements;
  400.       unsigned actual_array_size = this->is_clip_distance_mesa ?
  401.          prog->LastClipDistanceArraySize :
  402.          this->matched_candidate->type->array_size();
  403.  
  404.       if (this->is_subscripted) {
  405.          /* Check array bounds. */
  406.          if (this->array_subscript >= actual_array_size) {
  407.             linker_error(prog, "Transform feedback varying %s has index "
  408.                          "%i, but the array size is %u.",
  409.                          this->orig_name, this->array_subscript,
  410.                          actual_array_size);
  411.             return false;
  412.          }
  413.          unsigned array_elem_size = this->is_clip_distance_mesa ?
  414.             1 : vector_elements * matrix_cols;
  415.          fine_location += array_elem_size * this->array_subscript;
  416.          this->size = 1;
  417.       } else {
  418.          this->size = actual_array_size;
  419.       }
  420.       this->vector_elements = vector_elements;
  421.       this->matrix_columns = matrix_cols;
  422.       if (this->is_clip_distance_mesa)
  423.          this->type = GL_FLOAT;
  424.       else
  425.          this->type = this->matched_candidate->type->fields.array->gl_type;
  426.    } else {
  427.       /* Regular variable (scalar, vector, or matrix) */
  428.       if (this->is_subscripted) {
  429.          linker_error(prog, "Transform feedback varying %s requested, "
  430.                       "but %s is not an array.",
  431.                       this->orig_name, this->var_name);
  432.          return false;
  433.       }
  434.       this->size = 1;
  435.       this->vector_elements = this->matched_candidate->type->vector_elements;
  436.       this->matrix_columns = this->matched_candidate->type->matrix_columns;
  437.       this->type = this->matched_candidate->type->gl_type;
  438.    }
  439.    this->location = fine_location / 4;
  440.    this->location_frac = fine_location % 4;
  441.  
  442.    /* From GL_EXT_transform_feedback:
  443.     *   A program will fail to link if:
  444.     *
  445.     *   * the total number of components to capture in any varying
  446.     *     variable in <varyings> is greater than the constant
  447.     *     MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT and the
  448.     *     buffer mode is SEPARATE_ATTRIBS_EXT;
  449.     */
  450.    if (prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS &&
  451.        this->num_components() >
  452.        ctx->Const.MaxTransformFeedbackSeparateComponents) {
  453.       linker_error(prog, "Transform feedback varying %s exceeds "
  454.                    "MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS.",
  455.                    this->orig_name);
  456.       return false;
  457.    }
  458.  
  459.    /* Only transform feedback varyings can be assigned to non-zero streams,
  460.     * so assign the stream id here.
  461.     */
  462.    this->stream_id = this->matched_candidate->toplevel_var->data.stream;
  463.  
  464.    return true;
  465. }
  466.  
  467.  
  468. unsigned
  469. tfeedback_decl::get_num_outputs() const
  470. {
  471.    if (!this->is_varying()) {
  472.       return 0;
  473.    }
  474.  
  475.    return (this->num_components() + this->location_frac + 3)/4;
  476. }
  477.  
  478.  
  479. /**
  480.  * Update gl_transform_feedback_info to reflect this tfeedback_decl.
  481.  *
  482.  * If an error occurs, the error is reported through linker_error() and false
  483.  * is returned.
  484.  */
  485. bool
  486. tfeedback_decl::store(struct gl_context *ctx, struct gl_shader_program *prog,
  487.                       struct gl_transform_feedback_info *info,
  488.                       unsigned buffer, const unsigned max_outputs) const
  489. {
  490.    assert(!this->next_buffer_separator);
  491.  
  492.    /* Handle gl_SkipComponents. */
  493.    if (this->skip_components) {
  494.       info->BufferStride[buffer] += this->skip_components;
  495.       return true;
  496.    }
  497.  
  498.    /* From GL_EXT_transform_feedback:
  499.     *   A program will fail to link if:
  500.     *
  501.     *     * the total number of components to capture is greater than
  502.     *       the constant MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT
  503.     *       and the buffer mode is INTERLEAVED_ATTRIBS_EXT.
  504.     */
  505.    if (prog->TransformFeedback.BufferMode == GL_INTERLEAVED_ATTRIBS &&
  506.        info->BufferStride[buffer] + this->num_components() >
  507.        ctx->Const.MaxTransformFeedbackInterleavedComponents) {
  508.       linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
  509.                    "limit has been exceeded.");
  510.       return false;
  511.    }
  512.  
  513.    unsigned location = this->location;
  514.    unsigned location_frac = this->location_frac;
  515.    unsigned num_components = this->num_components();
  516.    while (num_components > 0) {
  517.       unsigned output_size = MIN2(num_components, 4 - location_frac);
  518.       assert(info->NumOutputs < max_outputs);
  519.       info->Outputs[info->NumOutputs].ComponentOffset = location_frac;
  520.       info->Outputs[info->NumOutputs].OutputRegister = location;
  521.       info->Outputs[info->NumOutputs].NumComponents = output_size;
  522.       info->Outputs[info->NumOutputs].StreamId = stream_id;
  523.       info->Outputs[info->NumOutputs].OutputBuffer = buffer;
  524.       info->Outputs[info->NumOutputs].DstOffset = info->BufferStride[buffer];
  525.       ++info->NumOutputs;
  526.       info->BufferStride[buffer] += output_size;
  527.       num_components -= output_size;
  528.       location++;
  529.       location_frac = 0;
  530.    }
  531.  
  532.    info->Varyings[info->NumVarying].Name = ralloc_strdup(prog, this->orig_name);
  533.    info->Varyings[info->NumVarying].Type = this->type;
  534.    info->Varyings[info->NumVarying].Size = this->size;
  535.    info->NumVarying++;
  536.  
  537.    return true;
  538. }
  539.  
  540.  
  541. const tfeedback_candidate *
  542. tfeedback_decl::find_candidate(gl_shader_program *prog,
  543.                                hash_table *tfeedback_candidates)
  544. {
  545.    const char *name = this->is_clip_distance_mesa
  546.       ? "gl_ClipDistanceMESA" : this->var_name;
  547.    this->matched_candidate = (const tfeedback_candidate *)
  548.       hash_table_find(tfeedback_candidates, name);
  549.    if (!this->matched_candidate) {
  550.       /* From GL_EXT_transform_feedback:
  551.        *   A program will fail to link if:
  552.        *
  553.        *   * any variable name specified in the <varyings> array is not
  554.        *     declared as an output in the geometry shader (if present) or
  555.        *     the vertex shader (if no geometry shader is present);
  556.        */
  557.       linker_error(prog, "Transform feedback varying %s undeclared.",
  558.                    this->orig_name);
  559.    }
  560.    return this->matched_candidate;
  561. }
  562.  
  563.  
  564. /**
  565.  * Parse all the transform feedback declarations that were passed to
  566.  * glTransformFeedbackVaryings() and store them in tfeedback_decl objects.
  567.  *
  568.  * If an error occurs, the error is reported through linker_error() and false
  569.  * is returned.
  570.  */
  571. bool
  572. parse_tfeedback_decls(struct gl_context *ctx, struct gl_shader_program *prog,
  573.                       const void *mem_ctx, unsigned num_names,
  574.                       char **varying_names, tfeedback_decl *decls)
  575. {
  576.    for (unsigned i = 0; i < num_names; ++i) {
  577.       decls[i].init(ctx, mem_ctx, varying_names[i]);
  578.  
  579.       if (!decls[i].is_varying())
  580.          continue;
  581.  
  582.       /* From GL_EXT_transform_feedback:
  583.        *   A program will fail to link if:
  584.        *
  585.        *   * any two entries in the <varyings> array specify the same varying
  586.        *     variable;
  587.        *
  588.        * We interpret this to mean "any two entries in the <varyings> array
  589.        * specify the same varying variable and array index", since transform
  590.        * feedback of arrays would be useless otherwise.
  591.        */
  592.       for (unsigned j = 0; j < i; ++j) {
  593.          if (!decls[j].is_varying())
  594.             continue;
  595.  
  596.          if (tfeedback_decl::is_same(decls[i], decls[j])) {
  597.             linker_error(prog, "Transform feedback varying %s specified "
  598.                          "more than once.", varying_names[i]);
  599.             return false;
  600.          }
  601.       }
  602.    }
  603.    return true;
  604. }
  605.  
  606.  
  607. /**
  608.  * Store transform feedback location assignments into
  609.  * prog->LinkedTransformFeedback based on the data stored in tfeedback_decls.
  610.  *
  611.  * If an error occurs, the error is reported through linker_error() and false
  612.  * is returned.
  613.  */
  614. bool
  615. store_tfeedback_info(struct gl_context *ctx, struct gl_shader_program *prog,
  616.                      unsigned num_tfeedback_decls,
  617.                      tfeedback_decl *tfeedback_decls)
  618. {
  619.    bool separate_attribs_mode =
  620.       prog->TransformFeedback.BufferMode == GL_SEPARATE_ATTRIBS;
  621.  
  622.    ralloc_free(prog->LinkedTransformFeedback.Varyings);
  623.    ralloc_free(prog->LinkedTransformFeedback.Outputs);
  624.  
  625.    memset(&prog->LinkedTransformFeedback, 0,
  626.           sizeof(prog->LinkedTransformFeedback));
  627.  
  628.    prog->LinkedTransformFeedback.Varyings =
  629.       rzalloc_array(prog,
  630.                     struct gl_transform_feedback_varying_info,
  631.                     num_tfeedback_decls);
  632.  
  633.    unsigned num_outputs = 0;
  634.    for (unsigned i = 0; i < num_tfeedback_decls; ++i)
  635.       num_outputs += tfeedback_decls[i].get_num_outputs();
  636.  
  637.    prog->LinkedTransformFeedback.Outputs =
  638.       rzalloc_array(prog,
  639.                     struct gl_transform_feedback_output,
  640.                     num_outputs);
  641.  
  642.    unsigned num_buffers = 0;
  643.  
  644.    if (separate_attribs_mode) {
  645.       /* GL_SEPARATE_ATTRIBS */
  646.       for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
  647.          if (!tfeedback_decls[i].store(ctx, prog, &prog->LinkedTransformFeedback,
  648.                                        num_buffers, num_outputs))
  649.             return false;
  650.  
  651.          num_buffers++;
  652.       }
  653.    }
  654.    else {
  655.       /* GL_INVERLEAVED_ATTRIBS */
  656.       int buffer_stream_id = -1;
  657.       for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
  658.          if (tfeedback_decls[i].is_next_buffer_separator()) {
  659.             num_buffers++;
  660.             buffer_stream_id = -1;
  661.             continue;
  662.          } else if (buffer_stream_id == -1)  {
  663.             /* First varying writing to this buffer: remember its stream */
  664.             buffer_stream_id = (int) tfeedback_decls[i].get_stream_id();
  665.          } else if (buffer_stream_id !=
  666.                     (int) tfeedback_decls[i].get_stream_id()) {
  667.             /* Varying writes to the same buffer from a different stream */
  668.             linker_error(prog,
  669.                          "Transform feedback can't capture varyings belonging "
  670.                          "to different vertex streams in a single buffer. "
  671.                          "Varying %s writes to buffer from stream %u, other "
  672.                          "varyings in the same buffer write from stream %u.",
  673.                          tfeedback_decls[i].name(),
  674.                          tfeedback_decls[i].get_stream_id(),
  675.                          buffer_stream_id);
  676.             return false;
  677.          }
  678.  
  679.          if (!tfeedback_decls[i].store(ctx, prog,
  680.                                        &prog->LinkedTransformFeedback,
  681.                                        num_buffers, num_outputs))
  682.             return false;
  683.       }
  684.       num_buffers++;
  685.    }
  686.  
  687.    assert(prog->LinkedTransformFeedback.NumOutputs == num_outputs);
  688.  
  689.    prog->LinkedTransformFeedback.NumBuffers = num_buffers;
  690.    return true;
  691. }
  692.  
  693. namespace {
  694.  
  695. /**
  696.  * Data structure recording the relationship between outputs of one shader
  697.  * stage (the "producer") and inputs of another (the "consumer").
  698.  */
  699. class varying_matches
  700. {
  701. public:
  702.    varying_matches(bool disable_varying_packing, bool consumer_is_fs);
  703.    ~varying_matches();
  704.    void record(ir_variable *producer_var, ir_variable *consumer_var);
  705.    unsigned assign_locations();
  706.    void store_locations() const;
  707.  
  708. private:
  709.    /**
  710.     * If true, this driver disables varying packing, so all varyings need to
  711.     * be aligned on slot boundaries, and take up a number of slots equal to
  712.     * their number of matrix columns times their array size.
  713.     */
  714.    const bool disable_varying_packing;
  715.  
  716.    /**
  717.     * Enum representing the order in which varyings are packed within a
  718.     * packing class.
  719.     *
  720.     * Currently we pack vec4's first, then vec2's, then scalar values, then
  721.     * vec3's.  This order ensures that the only vectors that are at risk of
  722.     * having to be "double parked" (split between two adjacent varying slots)
  723.     * are the vec3's.
  724.     */
  725.    enum packing_order_enum {
  726.       PACKING_ORDER_VEC4,
  727.       PACKING_ORDER_VEC2,
  728.       PACKING_ORDER_SCALAR,
  729.       PACKING_ORDER_VEC3,
  730.    };
  731.  
  732.    static unsigned compute_packing_class(const ir_variable *var);
  733.    static packing_order_enum compute_packing_order(const ir_variable *var);
  734.    static int match_comparator(const void *x_generic, const void *y_generic);
  735.  
  736.    /**
  737.     * Structure recording the relationship between a single producer output
  738.     * and a single consumer input.
  739.     */
  740.    struct match {
  741.       /**
  742.        * Packing class for this varying, computed by compute_packing_class().
  743.        */
  744.       unsigned packing_class;
  745.  
  746.       /**
  747.        * Packing order for this varying, computed by compute_packing_order().
  748.        */
  749.       packing_order_enum packing_order;
  750.       unsigned num_components;
  751.  
  752.       /**
  753.        * The output variable in the producer stage.
  754.        */
  755.       ir_variable *producer_var;
  756.  
  757.       /**
  758.        * The input variable in the consumer stage.
  759.        */
  760.       ir_variable *consumer_var;
  761.  
  762.       /**
  763.        * The location which has been assigned for this varying.  This is
  764.        * expressed in multiples of a float, with the first generic varying
  765.        * (i.e. the one referred to by VARYING_SLOT_VAR0) represented by the
  766.        * value 0.
  767.        */
  768.       unsigned generic_location;
  769.    } *matches;
  770.  
  771.    /**
  772.     * The number of elements in the \c matches array that are currently in
  773.     * use.
  774.     */
  775.    unsigned num_matches;
  776.  
  777.    /**
  778.     * The number of elements that were set aside for the \c matches array when
  779.     * it was allocated.
  780.     */
  781.    unsigned matches_capacity;
  782.  
  783.    const bool consumer_is_fs;
  784. };
  785.  
  786. } /* anonymous namespace */
  787.  
  788. varying_matches::varying_matches(bool disable_varying_packing,
  789.                                  bool consumer_is_fs)
  790.    : disable_varying_packing(disable_varying_packing),
  791.      consumer_is_fs(consumer_is_fs)
  792. {
  793.    /* Note: this initial capacity is rather arbitrarily chosen to be large
  794.     * enough for many cases without wasting an unreasonable amount of space.
  795.     * varying_matches::record() will resize the array if there are more than
  796.     * this number of varyings.
  797.     */
  798.    this->matches_capacity = 8;
  799.    this->matches = (match *)
  800.       malloc(sizeof(*this->matches) * this->matches_capacity);
  801.    this->num_matches = 0;
  802. }
  803.  
  804.  
  805. varying_matches::~varying_matches()
  806. {
  807.    free(this->matches);
  808. }
  809.  
  810.  
  811. /**
  812.  * Record the given producer/consumer variable pair in the list of variables
  813.  * that should later be assigned locations.
  814.  *
  815.  * It is permissible for \c consumer_var to be NULL (this happens if a
  816.  * variable is output by the producer and consumed by transform feedback, but
  817.  * not consumed by the consumer).
  818.  *
  819.  * If \c producer_var has already been paired up with a consumer_var, or
  820.  * producer_var is part of fixed pipeline functionality (and hence already has
  821.  * a location assigned), this function has no effect.
  822.  *
  823.  * Note: as a side effect this function may change the interpolation type of
  824.  * \c producer_var, but only when the change couldn't possibly affect
  825.  * rendering.
  826.  */
  827. void
  828. varying_matches::record(ir_variable *producer_var, ir_variable *consumer_var)
  829. {
  830.    assert(producer_var != NULL || consumer_var != NULL);
  831.  
  832.    if ((producer_var && !producer_var->data.is_unmatched_generic_inout)
  833.        || (consumer_var && !consumer_var->data.is_unmatched_generic_inout)) {
  834.       /* Either a location already exists for this variable (since it is part
  835.        * of fixed functionality), or it has already been recorded as part of a
  836.        * previous match.
  837.        */
  838.       return;
  839.    }
  840.  
  841.    if ((consumer_var == NULL && producer_var->type->contains_integer()) ||
  842.        !consumer_is_fs) {
  843.       /* Since this varying is not being consumed by the fragment shader, its
  844.        * interpolation type varying cannot possibly affect rendering.  Also,
  845.        * this variable is non-flat and is (or contains) an integer.
  846.        *
  847.        * lower_packed_varyings requires all integer varyings to flat,
  848.        * regardless of where they appear.  We can trivially satisfy that
  849.        * requirement by changing the interpolation type to flat here.
  850.        */
  851.       if (producer_var) {
  852.          producer_var->data.centroid = false;
  853.          producer_var->data.sample = false;
  854.          producer_var->data.interpolation = INTERP_QUALIFIER_FLAT;
  855.       }
  856.  
  857.       if (consumer_var) {
  858.          consumer_var->data.centroid = false;
  859.          consumer_var->data.sample = false;
  860.          consumer_var->data.interpolation = INTERP_QUALIFIER_FLAT;
  861.       }
  862.    }
  863.  
  864.    if (this->num_matches == this->matches_capacity) {
  865.       this->matches_capacity *= 2;
  866.       this->matches = (match *)
  867.          realloc(this->matches,
  868.                  sizeof(*this->matches) * this->matches_capacity);
  869.    }
  870.  
  871.    const ir_variable *const var = (producer_var != NULL)
  872.       ? producer_var : consumer_var;
  873.  
  874.    this->matches[this->num_matches].packing_class
  875.       = this->compute_packing_class(var);
  876.    this->matches[this->num_matches].packing_order
  877.       = this->compute_packing_order(var);
  878.    if (this->disable_varying_packing) {
  879.       unsigned slots = var->type->is_array()
  880.          ? (var->type->length * var->type->fields.array->matrix_columns)
  881.          : var->type->matrix_columns;
  882.       this->matches[this->num_matches].num_components = 4 * slots;
  883.    } else {
  884.       this->matches[this->num_matches].num_components
  885.          = var->type->component_slots();
  886.    }
  887.    this->matches[this->num_matches].producer_var = producer_var;
  888.    this->matches[this->num_matches].consumer_var = consumer_var;
  889.    this->num_matches++;
  890.    if (producer_var)
  891.       producer_var->data.is_unmatched_generic_inout = 0;
  892.    if (consumer_var)
  893.       consumer_var->data.is_unmatched_generic_inout = 0;
  894. }
  895.  
  896.  
  897. /**
  898.  * Choose locations for all of the variable matches that were previously
  899.  * passed to varying_matches::record().
  900.  */
  901. unsigned
  902. varying_matches::assign_locations()
  903. {
  904.    /* Sort varying matches into an order that makes them easy to pack. */
  905.    qsort(this->matches, this->num_matches, sizeof(*this->matches),
  906.          &varying_matches::match_comparator);
  907.  
  908.    unsigned generic_location = 0;
  909.  
  910.    for (unsigned i = 0; i < this->num_matches; i++) {
  911.       /* Advance to the next slot if this varying has a different packing
  912.        * class than the previous one, and we're not already on a slot
  913.        * boundary.
  914.        */
  915.       if (i > 0 &&
  916.           this->matches[i - 1].packing_class
  917.           != this->matches[i].packing_class) {
  918.          generic_location = ALIGN(generic_location, 4);
  919.       }
  920.  
  921.       this->matches[i].generic_location = generic_location;
  922.  
  923.       generic_location += this->matches[i].num_components;
  924.    }
  925.  
  926.    return (generic_location + 3) / 4;
  927. }
  928.  
  929.  
  930. /**
  931.  * Update the producer and consumer shaders to reflect the locations
  932.  * assignments that were made by varying_matches::assign_locations().
  933.  */
  934. void
  935. varying_matches::store_locations() const
  936. {
  937.    for (unsigned i = 0; i < this->num_matches; i++) {
  938.       ir_variable *producer_var = this->matches[i].producer_var;
  939.       ir_variable *consumer_var = this->matches[i].consumer_var;
  940.       unsigned generic_location = this->matches[i].generic_location;
  941.       unsigned slot = generic_location / 4;
  942.       unsigned offset = generic_location % 4;
  943.  
  944.       if (producer_var) {
  945.          producer_var->data.location = VARYING_SLOT_VAR0 + slot;
  946.          producer_var->data.location_frac = offset;
  947.       }
  948.  
  949.       if (consumer_var) {
  950.          assert(consumer_var->data.location == -1);
  951.          consumer_var->data.location = VARYING_SLOT_VAR0 + slot;
  952.          consumer_var->data.location_frac = offset;
  953.       }
  954.    }
  955. }
  956.  
  957.  
  958. /**
  959.  * Compute the "packing class" of the given varying.  This is an unsigned
  960.  * integer with the property that two variables in the same packing class can
  961.  * be safely backed into the same vec4.
  962.  */
  963. unsigned
  964. varying_matches::compute_packing_class(const ir_variable *var)
  965. {
  966.    /* Without help from the back-end, there is no way to pack together
  967.     * variables with different interpolation types, because
  968.     * lower_packed_varyings must choose exactly one interpolation type for
  969.     * each packed varying it creates.
  970.     *
  971.     * However, we can safely pack together floats, ints, and uints, because:
  972.     *
  973.     * - varyings of base type "int" and "uint" must use the "flat"
  974.     *   interpolation type, which can only occur in GLSL 1.30 and above.
  975.     *
  976.     * - On platforms that support GLSL 1.30 and above, lower_packed_varyings
  977.     *   can store flat floats as ints without losing any information (using
  978.     *   the ir_unop_bitcast_* opcodes).
  979.     *
  980.     * Therefore, the packing class depends only on the interpolation type.
  981.     */
  982.    unsigned packing_class = var->data.centroid | (var->data.sample << 1);
  983.    packing_class *= 4;
  984.    packing_class += var->data.interpolation;
  985.    return packing_class;
  986. }
  987.  
  988.  
  989. /**
  990.  * Compute the "packing order" of the given varying.  This is a sort key we
  991.  * use to determine when to attempt to pack the given varying relative to
  992.  * other varyings in the same packing class.
  993.  */
  994. varying_matches::packing_order_enum
  995. varying_matches::compute_packing_order(const ir_variable *var)
  996. {
  997.    const glsl_type *element_type = var->type;
  998.  
  999.    while (element_type->base_type == GLSL_TYPE_ARRAY) {
  1000.       element_type = element_type->fields.array;
  1001.    }
  1002.  
  1003.    switch (element_type->component_slots() % 4) {
  1004.    case 1: return PACKING_ORDER_SCALAR;
  1005.    case 2: return PACKING_ORDER_VEC2;
  1006.    case 3: return PACKING_ORDER_VEC3;
  1007.    case 0: return PACKING_ORDER_VEC4;
  1008.    default:
  1009.       assert(!"Unexpected value of vector_elements");
  1010.       return PACKING_ORDER_VEC4;
  1011.    }
  1012. }
  1013.  
  1014.  
  1015. /**
  1016.  * Comparison function passed to qsort() to sort varyings by packing_class and
  1017.  * then by packing_order.
  1018.  */
  1019. int
  1020. varying_matches::match_comparator(const void *x_generic, const void *y_generic)
  1021. {
  1022.    const match *x = (const match *) x_generic;
  1023.    const match *y = (const match *) y_generic;
  1024.  
  1025.    if (x->packing_class != y->packing_class)
  1026.       return x->packing_class - y->packing_class;
  1027.    return x->packing_order - y->packing_order;
  1028. }
  1029.  
  1030.  
  1031. /**
  1032.  * Is the given variable a varying variable to be counted against the
  1033.  * limit in ctx->Const.MaxVarying?
  1034.  * This includes variables such as texcoords, colors and generic
  1035.  * varyings, but excludes variables such as gl_FrontFacing and gl_FragCoord.
  1036.  */
  1037. static bool
  1038. var_counts_against_varying_limit(gl_shader_stage stage, const ir_variable *var)
  1039. {
  1040.    /* Only fragment shaders will take a varying variable as an input */
  1041.    if (stage == MESA_SHADER_FRAGMENT &&
  1042.        var->data.mode == ir_var_shader_in) {
  1043.       switch (var->data.location) {
  1044.       case VARYING_SLOT_POS:
  1045.       case VARYING_SLOT_FACE:
  1046.       case VARYING_SLOT_PNTC:
  1047.          return false;
  1048.       default:
  1049.          return true;
  1050.       }
  1051.    }
  1052.    return false;
  1053. }
  1054.  
  1055.  
  1056. /**
  1057.  * Visitor class that generates tfeedback_candidate structs describing all
  1058.  * possible targets of transform feedback.
  1059.  *
  1060.  * tfeedback_candidate structs are stored in the hash table
  1061.  * tfeedback_candidates, which is passed to the constructor.  This hash table
  1062.  * maps varying names to instances of the tfeedback_candidate struct.
  1063.  */
  1064. class tfeedback_candidate_generator : public program_resource_visitor
  1065. {
  1066. public:
  1067.    tfeedback_candidate_generator(void *mem_ctx,
  1068.                                  hash_table *tfeedback_candidates)
  1069.       : mem_ctx(mem_ctx),
  1070.         tfeedback_candidates(tfeedback_candidates),
  1071.         toplevel_var(NULL),
  1072.         varying_floats(0)
  1073.    {
  1074.    }
  1075.  
  1076.    void process(ir_variable *var)
  1077.    {
  1078.       this->toplevel_var = var;
  1079.       this->varying_floats = 0;
  1080.       if (var->is_interface_instance())
  1081.          program_resource_visitor::process(var->get_interface_type(),
  1082.                                            var->get_interface_type()->name);
  1083.       else
  1084.          program_resource_visitor::process(var);
  1085.    }
  1086.  
  1087. private:
  1088.    virtual void visit_field(const glsl_type *type, const char *name,
  1089.                             bool row_major)
  1090.    {
  1091.       assert(!type->without_array()->is_record());
  1092.       assert(!type->without_array()->is_interface());
  1093.  
  1094.       (void) row_major;
  1095.  
  1096.       tfeedback_candidate *candidate
  1097.          = rzalloc(this->mem_ctx, tfeedback_candidate);
  1098.       candidate->toplevel_var = this->toplevel_var;
  1099.       candidate->type = type;
  1100.       candidate->offset = this->varying_floats;
  1101.       hash_table_insert(this->tfeedback_candidates, candidate,
  1102.                         ralloc_strdup(this->mem_ctx, name));
  1103.       this->varying_floats += type->component_slots();
  1104.    }
  1105.  
  1106.    /**
  1107.     * Memory context used to allocate hash table keys and values.
  1108.     */
  1109.    void * const mem_ctx;
  1110.  
  1111.    /**
  1112.     * Hash table in which tfeedback_candidate objects should be stored.
  1113.     */
  1114.    hash_table * const tfeedback_candidates;
  1115.  
  1116.    /**
  1117.     * Pointer to the toplevel variable that is being traversed.
  1118.     */
  1119.    ir_variable *toplevel_var;
  1120.  
  1121.    /**
  1122.     * Total number of varying floats that have been visited so far.  This is
  1123.     * used to determine the offset to each varying within the toplevel
  1124.     * variable.
  1125.     */
  1126.    unsigned varying_floats;
  1127. };
  1128.  
  1129.  
  1130. namespace linker {
  1131.  
  1132. bool
  1133. populate_consumer_input_sets(void *mem_ctx, exec_list *ir,
  1134.                              hash_table *consumer_inputs,
  1135.                              hash_table *consumer_interface_inputs,
  1136.                              ir_variable *consumer_inputs_with_locations[VARYING_SLOT_MAX])
  1137. {
  1138.    memset(consumer_inputs_with_locations,
  1139.           0,
  1140.           sizeof(consumer_inputs_with_locations[0]) * VARYING_SLOT_MAX);
  1141.  
  1142.    foreach_in_list(ir_instruction, node, ir) {
  1143.       ir_variable *const input_var = node->as_variable();
  1144.  
  1145.       if ((input_var != NULL) && (input_var->data.mode == ir_var_shader_in)) {
  1146.          if (input_var->type->is_interface())
  1147.             return false;
  1148.  
  1149.          if (input_var->data.explicit_location) {
  1150.             /* assign_varying_locations only cares about finding the
  1151.              * ir_variable at the start of a contiguous location block.
  1152.              *
  1153.              *     - For !producer, consumer_inputs_with_locations isn't used.
  1154.              *
  1155.              *     - For !consumer, consumer_inputs_with_locations is empty.
  1156.              *
  1157.              * For consumer && producer, if you were trying to set some
  1158.              * ir_variable to the middle of a location block on the other side
  1159.              * of producer/consumer, cross_validate_outputs_to_inputs() should
  1160.              * be link-erroring due to either type mismatch or location
  1161.              * overlaps.  If the variables do match up, then they've got a
  1162.              * matching data.location and you only looked at
  1163.              * consumer_inputs_with_locations[var->data.location], not any
  1164.              * following entries for the array/structure.
  1165.              */
  1166.             consumer_inputs_with_locations[input_var->data.location] =
  1167.                input_var;
  1168.          } else if (input_var->get_interface_type() != NULL) {
  1169.             char *const iface_field_name =
  1170.                ralloc_asprintf(mem_ctx, "%s.%s",
  1171.                                input_var->get_interface_type()->name,
  1172.                                input_var->name);
  1173.             hash_table_insert(consumer_interface_inputs, input_var,
  1174.                               iface_field_name);
  1175.          } else {
  1176.             hash_table_insert(consumer_inputs, input_var,
  1177.                               ralloc_strdup(mem_ctx, input_var->name));
  1178.          }
  1179.       }
  1180.    }
  1181.  
  1182.    return true;
  1183. }
  1184.  
  1185. /**
  1186.  * Find a variable from the consumer that "matches" the specified variable
  1187.  *
  1188.  * This function only finds inputs with names that match.  There is no
  1189.  * validation (here) that the types, etc. are compatible.
  1190.  */
  1191. ir_variable *
  1192. get_matching_input(void *mem_ctx,
  1193.                    const ir_variable *output_var,
  1194.                    hash_table *consumer_inputs,
  1195.                    hash_table *consumer_interface_inputs,
  1196.                    ir_variable *consumer_inputs_with_locations[VARYING_SLOT_MAX])
  1197. {
  1198.    ir_variable *input_var;
  1199.  
  1200.    if (output_var->data.explicit_location) {
  1201.       input_var = consumer_inputs_with_locations[output_var->data.location];
  1202.    } else if (output_var->get_interface_type() != NULL) {
  1203.       char *const iface_field_name =
  1204.          ralloc_asprintf(mem_ctx, "%s.%s",
  1205.                          output_var->get_interface_type()->name,
  1206.                          output_var->name);
  1207.       input_var =
  1208.          (ir_variable *) hash_table_find(consumer_interface_inputs,
  1209.                                          iface_field_name);
  1210.    } else {
  1211.       input_var =
  1212.          (ir_variable *) hash_table_find(consumer_inputs, output_var->name);
  1213.    }
  1214.  
  1215.    return (input_var == NULL || input_var->data.mode != ir_var_shader_in)
  1216.       ? NULL : input_var;
  1217. }
  1218.  
  1219. }
  1220.  
  1221. static int
  1222. io_variable_cmp(const void *_a, const void *_b)
  1223. {
  1224.    const ir_variable *const a = *(const ir_variable **) _a;
  1225.    const ir_variable *const b = *(const ir_variable **) _b;
  1226.  
  1227.    if (a->data.explicit_location && b->data.explicit_location)
  1228.       return b->data.location - a->data.location;
  1229.  
  1230.    if (a->data.explicit_location && !b->data.explicit_location)
  1231.       return 1;
  1232.  
  1233.    if (!a->data.explicit_location && b->data.explicit_location)
  1234.       return -1;
  1235.  
  1236.    return -strcmp(a->name, b->name);
  1237. }
  1238.  
  1239. /**
  1240.  * Sort the shader IO variables into canonical order
  1241.  */
  1242. static void
  1243. canonicalize_shader_io(exec_list *ir, enum ir_variable_mode io_mode)
  1244. {
  1245.    ir_variable *var_table[MAX_PROGRAM_OUTPUTS * 4];
  1246.    unsigned num_variables = 0;
  1247.  
  1248.    foreach_in_list(ir_instruction, node, ir) {
  1249.       ir_variable *const var = node->as_variable();
  1250.  
  1251.       if (var == NULL || var->data.mode != io_mode)
  1252.          continue;
  1253.  
  1254.       /* If we have already encountered more I/O variables that could
  1255.        * successfully link, bail.
  1256.        */
  1257.       if (num_variables == ARRAY_SIZE(var_table))
  1258.          return;
  1259.  
  1260.       var_table[num_variables++] = var;
  1261.    }
  1262.  
  1263.    if (num_variables == 0)
  1264.       return;
  1265.  
  1266.    /* Sort the list in reverse order (io_variable_cmp handles this).  Later
  1267.     * we're going to push the variables on to the IR list as a stack, so we
  1268.     * want the last variable (in canonical order) to be first in the list.
  1269.     */
  1270.    qsort(var_table, num_variables, sizeof(var_table[0]), io_variable_cmp);
  1271.  
  1272.    /* Remove the variable from it's current location in the IR, and put it at
  1273.     * the front.
  1274.     */
  1275.    for (unsigned i = 0; i < num_variables; i++) {
  1276.       var_table[i]->remove();
  1277.       ir->push_head(var_table[i]);
  1278.    }
  1279. }
  1280.  
  1281. /**
  1282.  * Assign locations for all variables that are produced in one pipeline stage
  1283.  * (the "producer") and consumed in the next stage (the "consumer").
  1284.  *
  1285.  * Variables produced by the producer may also be consumed by transform
  1286.  * feedback.
  1287.  *
  1288.  * \param num_tfeedback_decls is the number of declarations indicating
  1289.  *        variables that may be consumed by transform feedback.
  1290.  *
  1291.  * \param tfeedback_decls is a pointer to an array of tfeedback_decl objects
  1292.  *        representing the result of parsing the strings passed to
  1293.  *        glTransformFeedbackVaryings().  assign_location() will be called for
  1294.  *        each of these objects that matches one of the outputs of the
  1295.  *        producer.
  1296.  *
  1297.  * \param gs_input_vertices: if \c consumer is a geometry shader, this is the
  1298.  *        number of input vertices it accepts.  Otherwise zero.
  1299.  *
  1300.  * When num_tfeedback_decls is nonzero, it is permissible for the consumer to
  1301.  * be NULL.  In this case, varying locations are assigned solely based on the
  1302.  * requirements of transform feedback.
  1303.  */
  1304. bool
  1305. assign_varying_locations(struct gl_context *ctx,
  1306.                          void *mem_ctx,
  1307.                          struct gl_shader_program *prog,
  1308.                          gl_shader *producer, gl_shader *consumer,
  1309.                          unsigned num_tfeedback_decls,
  1310.                          tfeedback_decl *tfeedback_decls,
  1311.                          unsigned gs_input_vertices)
  1312. {
  1313.    varying_matches matches(ctx->Const.DisableVaryingPacking,
  1314.                            consumer && consumer->Stage == MESA_SHADER_FRAGMENT);
  1315.    hash_table *tfeedback_candidates
  1316.       = hash_table_ctor(0, hash_table_string_hash, hash_table_string_compare);
  1317.    hash_table *consumer_inputs
  1318.       = hash_table_ctor(0, hash_table_string_hash, hash_table_string_compare);
  1319.    hash_table *consumer_interface_inputs
  1320.       = hash_table_ctor(0, hash_table_string_hash, hash_table_string_compare);
  1321.    ir_variable *consumer_inputs_with_locations[VARYING_SLOT_MAX] = {
  1322.       NULL,
  1323.    };
  1324.  
  1325.    /* Operate in a total of four passes.
  1326.     *
  1327.     * 1. Sort inputs / outputs into a canonical order.  This is necessary so
  1328.     *    that inputs / outputs of separable shaders will be assigned
  1329.     *    predictable locations regardless of the order in which declarations
  1330.     *    appeared in the shader source.
  1331.     *
  1332.     * 2. Assign locations for any matching inputs and outputs.
  1333.     *
  1334.     * 3. Mark output variables in the producer that do not have locations as
  1335.     *    not being outputs.  This lets the optimizer eliminate them.
  1336.     *
  1337.     * 4. Mark input variables in the consumer that do not have locations as
  1338.     *    not being inputs.  This lets the optimizer eliminate them.
  1339.     */
  1340.    if (consumer)
  1341.       canonicalize_shader_io(consumer->ir, ir_var_shader_in);
  1342.  
  1343.    if (producer)
  1344.       canonicalize_shader_io(producer->ir, ir_var_shader_out);
  1345.  
  1346.    if (consumer
  1347.        && !linker::populate_consumer_input_sets(mem_ctx,
  1348.                                                 consumer->ir,
  1349.                                                 consumer_inputs,
  1350.                                                 consumer_interface_inputs,
  1351.                                                 consumer_inputs_with_locations)) {
  1352.       assert(!"populate_consumer_input_sets failed");
  1353.       hash_table_dtor(tfeedback_candidates);
  1354.       hash_table_dtor(consumer_inputs);
  1355.       hash_table_dtor(consumer_interface_inputs);
  1356.       return false;
  1357.    }
  1358.  
  1359.    if (producer) {
  1360.       foreach_in_list(ir_instruction, node, producer->ir) {
  1361.          ir_variable *const output_var = node->as_variable();
  1362.  
  1363.          if ((output_var == NULL) ||
  1364.              (output_var->data.mode != ir_var_shader_out))
  1365.             continue;
  1366.  
  1367.          /* Only geometry shaders can use non-zero streams */
  1368.          assert(output_var->data.stream == 0 ||
  1369.                 (output_var->data.stream < MAX_VERTEX_STREAMS &&
  1370.                  producer->Stage == MESA_SHADER_GEOMETRY));
  1371.  
  1372.          tfeedback_candidate_generator g(mem_ctx, tfeedback_candidates);
  1373.          g.process(output_var);
  1374.  
  1375.          ir_variable *const input_var =
  1376.             linker::get_matching_input(mem_ctx, output_var, consumer_inputs,
  1377.                                        consumer_interface_inputs,
  1378.                                        consumer_inputs_with_locations);
  1379.  
  1380.          /* If a matching input variable was found, add this ouptut (and the
  1381.           * input) to the set.  If this is a separable program and there is no
  1382.           * consumer stage, add the output.
  1383.           */
  1384.          if (input_var || (prog->SeparateShader && consumer == NULL)) {
  1385.             matches.record(output_var, input_var);
  1386.          }
  1387.  
  1388.          /* Only stream 0 outputs can be consumed in the next stage */
  1389.          if (input_var && output_var->data.stream != 0) {
  1390.             linker_error(prog, "output %s is assigned to stream=%d but "
  1391.                          "is linked to an input, which requires stream=0",
  1392.                          output_var->name, output_var->data.stream);
  1393.             return false;
  1394.          }
  1395.       }
  1396.    } else {
  1397.       /* If there's no producer stage, then this must be a separable program.
  1398.        * For example, we may have a program that has just a fragment shader.
  1399.        * Later this program will be used with some arbitrary vertex (or
  1400.        * geometry) shader program.  This means that locations must be assigned
  1401.        * for all the inputs.
  1402.        */
  1403.       foreach_in_list(ir_instruction, node, consumer->ir) {
  1404.          ir_variable *const input_var = node->as_variable();
  1405.  
  1406.          if ((input_var == NULL) ||
  1407.              (input_var->data.mode != ir_var_shader_in))
  1408.             continue;
  1409.  
  1410.          matches.record(NULL, input_var);
  1411.       }
  1412.    }
  1413.  
  1414.    for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
  1415.       if (!tfeedback_decls[i].is_varying())
  1416.          continue;
  1417.  
  1418.       const tfeedback_candidate *matched_candidate
  1419.          = tfeedback_decls[i].find_candidate(prog, tfeedback_candidates);
  1420.  
  1421.       if (matched_candidate == NULL) {
  1422.          hash_table_dtor(tfeedback_candidates);
  1423.          hash_table_dtor(consumer_inputs);
  1424.          hash_table_dtor(consumer_interface_inputs);
  1425.          return false;
  1426.       }
  1427.  
  1428.       if (matched_candidate->toplevel_var->data.is_unmatched_generic_inout)
  1429.          matches.record(matched_candidate->toplevel_var, NULL);
  1430.    }
  1431.  
  1432.    const unsigned slots_used = matches.assign_locations();
  1433.    matches.store_locations();
  1434.  
  1435.    for (unsigned i = 0; i < num_tfeedback_decls; ++i) {
  1436.       if (!tfeedback_decls[i].is_varying())
  1437.          continue;
  1438.  
  1439.       if (!tfeedback_decls[i].assign_location(ctx, prog)) {
  1440.          hash_table_dtor(tfeedback_candidates);
  1441.          hash_table_dtor(consumer_inputs);
  1442.          hash_table_dtor(consumer_interface_inputs);
  1443.          return false;
  1444.       }
  1445.    }
  1446.  
  1447.    hash_table_dtor(tfeedback_candidates);
  1448.    hash_table_dtor(consumer_inputs);
  1449.    hash_table_dtor(consumer_interface_inputs);
  1450.  
  1451.    if (ctx->Const.DisableVaryingPacking) {
  1452.       /* Transform feedback code assumes varyings are packed, so if the driver
  1453.        * has disabled varying packing, make sure it does not support transform
  1454.        * feedback.
  1455.        */
  1456.       assert(!ctx->Extensions.EXT_transform_feedback);
  1457.    } else {
  1458.       if (producer) {
  1459.          lower_packed_varyings(mem_ctx, slots_used, ir_var_shader_out,
  1460.                                0, producer);
  1461.       }
  1462.       if (consumer) {
  1463.          lower_packed_varyings(mem_ctx, slots_used, ir_var_shader_in,
  1464.                                gs_input_vertices, consumer);
  1465.       }
  1466.    }
  1467.  
  1468.    if (consumer && producer) {
  1469.       foreach_in_list(ir_instruction, node, consumer->ir) {
  1470.          ir_variable *const var = node->as_variable();
  1471.  
  1472.          if (var && var->data.mode == ir_var_shader_in &&
  1473.              var->data.is_unmatched_generic_inout) {
  1474.             if (prog->IsES) {
  1475.                /*
  1476.                 * On Page 91 (Page 97 of the PDF) of the GLSL ES 1.0 spec:
  1477.                 *
  1478.                 *     If the vertex shader declares but doesn't write to a
  1479.                 *     varying and the fragment shader declares and reads it,
  1480.                 *     is this an error?
  1481.                 *
  1482.                 *     RESOLUTION: No.
  1483.                 */
  1484.                linker_warning(prog, "%s shader varying %s not written "
  1485.                               "by %s shader\n.",
  1486.                               _mesa_shader_stage_to_string(consumer->Stage),
  1487.                               var->name,
  1488.                               _mesa_shader_stage_to_string(producer->Stage));
  1489.             } else if (prog->Version <= 120) {
  1490.                /* On page 25 (page 31 of the PDF) of the GLSL 1.20 spec:
  1491.                 *
  1492.                 *     Only those varying variables used (i.e. read) in
  1493.                 *     the fragment shader executable must be written to
  1494.                 *     by the vertex shader executable; declaring
  1495.                 *     superfluous varying variables in a vertex shader is
  1496.                 *     permissible.
  1497.                 *
  1498.                 * We interpret this text as meaning that the VS must
  1499.                 * write the variable for the FS to read it.  See
  1500.                 * "glsl1-varying read but not written" in piglit.
  1501.                 */
  1502.                linker_error(prog, "%s shader varying %s not written "
  1503.                             "by %s shader\n.",
  1504.                             _mesa_shader_stage_to_string(consumer->Stage),
  1505.                             var->name,
  1506.                             _mesa_shader_stage_to_string(producer->Stage));
  1507.             }
  1508.  
  1509.             /* An 'in' variable is only really a shader input if its
  1510.              * value is written by the previous stage.
  1511.              */
  1512.             var->data.mode = ir_var_auto;
  1513.          }
  1514.       }
  1515.    }
  1516.  
  1517.    return true;
  1518. }
  1519.  
  1520. bool
  1521. check_against_output_limit(struct gl_context *ctx,
  1522.                            struct gl_shader_program *prog,
  1523.                            gl_shader *producer)
  1524. {
  1525.    unsigned output_vectors = 0;
  1526.  
  1527.    foreach_in_list(ir_instruction, node, producer->ir) {
  1528.       ir_variable *const var = node->as_variable();
  1529.  
  1530.       if (var && var->data.mode == ir_var_shader_out &&
  1531.           var_counts_against_varying_limit(producer->Stage, var)) {
  1532.          output_vectors += var->type->count_attribute_slots();
  1533.       }
  1534.    }
  1535.  
  1536.    assert(producer->Stage != MESA_SHADER_FRAGMENT);
  1537.    unsigned max_output_components =
  1538.       ctx->Const.Program[producer->Stage].MaxOutputComponents;
  1539.  
  1540.    const unsigned output_components = output_vectors * 4;
  1541.    if (output_components > max_output_components) {
  1542.       if (ctx->API == API_OPENGLES2 || prog->IsES)
  1543.          linker_error(prog, "shader uses too many output vectors "
  1544.                       "(%u > %u)\n",
  1545.                       output_vectors,
  1546.                       max_output_components / 4);
  1547.       else
  1548.          linker_error(prog, "shader uses too many output components "
  1549.                       "(%u > %u)\n",
  1550.                       output_components,
  1551.                       max_output_components);
  1552.  
  1553.       return false;
  1554.    }
  1555.  
  1556.    return true;
  1557. }
  1558.  
  1559. bool
  1560. check_against_input_limit(struct gl_context *ctx,
  1561.                           struct gl_shader_program *prog,
  1562.                           gl_shader *consumer)
  1563. {
  1564.    unsigned input_vectors = 0;
  1565.  
  1566.    foreach_in_list(ir_instruction, node, consumer->ir) {
  1567.       ir_variable *const var = node->as_variable();
  1568.  
  1569.       if (var && var->data.mode == ir_var_shader_in &&
  1570.           var_counts_against_varying_limit(consumer->Stage, var)) {
  1571.          input_vectors += var->type->count_attribute_slots();
  1572.       }
  1573.    }
  1574.  
  1575.    assert(consumer->Stage != MESA_SHADER_VERTEX);
  1576.    unsigned max_input_components =
  1577.       ctx->Const.Program[consumer->Stage].MaxInputComponents;
  1578.  
  1579.    const unsigned input_components = input_vectors * 4;
  1580.    if (input_components > max_input_components) {
  1581.       if (ctx->API == API_OPENGLES2 || prog->IsES)
  1582.          linker_error(prog, "shader uses too many input vectors "
  1583.                       "(%u > %u)\n",
  1584.                       input_vectors,
  1585.                       max_input_components / 4);
  1586.       else
  1587.          linker_error(prog, "shader uses too many input components "
  1588.                       "(%u > %u)\n",
  1589.                       input_components,
  1590.                       max_input_components);
  1591.  
  1592.       return false;
  1593.    }
  1594.  
  1595.    return true;
  1596. }
  1597.