Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2010 Intel Corporation
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21.  * DEALINGS IN THE SOFTWARE.
  22.  */
  23.  
  24. /**
  25.  * \file linker.cpp
  26.  * GLSL linker implementation
  27.  *
  28.  * Given a set of shaders that are to be linked to generate a final program,
  29.  * there are three distinct stages.
  30.  *
  31.  * In the first stage shaders are partitioned into groups based on the shader
  32.  * type.  All shaders of a particular type (e.g., vertex shaders) are linked
  33.  * together.
  34.  *
  35.  *   - Undefined references in each shader are resolve to definitions in
  36.  *     another shader.
  37.  *   - Types and qualifiers of uniforms, outputs, and global variables defined
  38.  *     in multiple shaders with the same name are verified to be the same.
  39.  *   - Initializers for uniforms and global variables defined
  40.  *     in multiple shaders with the same name are verified to be the same.
  41.  *
  42.  * The result, in the terminology of the GLSL spec, is a set of shader
  43.  * executables for each processing unit.
  44.  *
  45.  * After the first stage is complete, a series of semantic checks are performed
  46.  * on each of the shader executables.
  47.  *
  48.  *   - Each shader executable must define a \c main function.
  49.  *   - Each vertex shader executable must write to \c gl_Position.
  50.  *   - Each fragment shader executable must write to either \c gl_FragData or
  51.  *     \c gl_FragColor.
  52.  *
  53.  * In the final stage individual shader executables are linked to create a
  54.  * complete exectuable.
  55.  *
  56.  *   - Types of uniforms defined in multiple shader stages with the same name
  57.  *     are verified to be the same.
  58.  *   - Initializers for uniforms defined in multiple shader stages with the
  59.  *     same name are verified to be the same.
  60.  *   - Types and qualifiers of outputs defined in one stage are verified to
  61.  *     be the same as the types and qualifiers of inputs defined with the same
  62.  *     name in a later stage.
  63.  *
  64.  * \author Ian Romanick <ian.d.romanick@intel.com>
  65.  */
  66.  
  67. #include "main/core.h"
  68. #include "glsl_symbol_table.h"
  69. #include "glsl_parser_extras.h"
  70. #include "ir.h"
  71. #include "program.h"
  72. #include "program/hash_table.h"
  73. #include "linker.h"
  74. #include "link_varyings.h"
  75. #include "ir_optimization.h"
  76.  
  77. extern "C" {
  78. #include "main/shaderobj.h"
  79. }
  80.  
  81. /**
  82.  * Visitor that determines whether or not a variable is ever written.
  83.  */
  84. class find_assignment_visitor : public ir_hierarchical_visitor {
  85. public:
  86.    find_assignment_visitor(const char *name)
  87.       : name(name), found(false)
  88.    {
  89.       /* empty */
  90.    }
  91.  
  92.    virtual ir_visitor_status visit_enter(ir_assignment *ir)
  93.    {
  94.       ir_variable *const var = ir->lhs->variable_referenced();
  95.  
  96.       if (strcmp(name, var->name) == 0) {
  97.          found = true;
  98.          return visit_stop;
  99.       }
  100.  
  101.       return visit_continue_with_parent;
  102.    }
  103.  
  104.    virtual ir_visitor_status visit_enter(ir_call *ir)
  105.    {
  106.       exec_list_iterator sig_iter = ir->callee->parameters.iterator();
  107.       foreach_iter(exec_list_iterator, iter, *ir) {
  108.          ir_rvalue *param_rval = (ir_rvalue *)iter.get();
  109.          ir_variable *sig_param = (ir_variable *)sig_iter.get();
  110.  
  111.          if (sig_param->mode == ir_var_function_out ||
  112.              sig_param->mode == ir_var_function_inout) {
  113.             ir_variable *var = param_rval->variable_referenced();
  114.             if (var && strcmp(name, var->name) == 0) {
  115.                found = true;
  116.                return visit_stop;
  117.             }
  118.          }
  119.          sig_iter.next();
  120.       }
  121.  
  122.       if (ir->return_deref != NULL) {
  123.          ir_variable *const var = ir->return_deref->variable_referenced();
  124.  
  125.          if (strcmp(name, var->name) == 0) {
  126.             found = true;
  127.             return visit_stop;
  128.          }
  129.       }
  130.  
  131.       return visit_continue_with_parent;
  132.    }
  133.  
  134.    bool variable_found()
  135.    {
  136.       return found;
  137.    }
  138.  
  139. private:
  140.    const char *name;       /**< Find writes to a variable with this name. */
  141.    bool found;             /**< Was a write to the variable found? */
  142. };
  143.  
  144.  
  145. /**
  146.  * Visitor that determines whether or not a variable is ever read.
  147.  */
  148. class find_deref_visitor : public ir_hierarchical_visitor {
  149. public:
  150.    find_deref_visitor(const char *name)
  151.       : name(name), found(false)
  152.    {
  153.       /* empty */
  154.    }
  155.  
  156.    virtual ir_visitor_status visit(ir_dereference_variable *ir)
  157.    {
  158.       if (strcmp(this->name, ir->var->name) == 0) {
  159.          this->found = true;
  160.          return visit_stop;
  161.       }
  162.  
  163.       return visit_continue;
  164.    }
  165.  
  166.    bool variable_found() const
  167.    {
  168.       return this->found;
  169.    }
  170.  
  171. private:
  172.    const char *name;       /**< Find writes to a variable with this name. */
  173.    bool found;             /**< Was a write to the variable found? */
  174. };
  175.  
  176.  
  177. void
  178. linker_error(gl_shader_program *prog, const char *fmt, ...)
  179. {
  180.    va_list ap;
  181.  
  182.    ralloc_strcat(&prog->InfoLog, "error: ");
  183.    va_start(ap, fmt);
  184.    ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
  185.    va_end(ap);
  186.  
  187.    prog->LinkStatus = false;
  188. }
  189.  
  190.  
  191. void
  192. linker_warning(gl_shader_program *prog, const char *fmt, ...)
  193. {
  194.    va_list ap;
  195.  
  196.    ralloc_strcat(&prog->InfoLog, "error: ");
  197.    va_start(ap, fmt);
  198.    ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
  199.    va_end(ap);
  200.  
  201. }
  202.  
  203.  
  204. /**
  205.  * Given a string identifying a program resource, break it into a base name
  206.  * and an optional array index in square brackets.
  207.  *
  208.  * If an array index is present, \c out_base_name_end is set to point to the
  209.  * "[" that precedes the array index, and the array index itself is returned
  210.  * as a long.
  211.  *
  212.  * If no array index is present (or if the array index is negative or
  213.  * mal-formed), \c out_base_name_end, is set to point to the null terminator
  214.  * at the end of the input string, and -1 is returned.
  215.  *
  216.  * Only the final array index is parsed; if the string contains other array
  217.  * indices (or structure field accesses), they are left in the base name.
  218.  *
  219.  * No attempt is made to check that the base name is properly formed;
  220.  * typically the caller will look up the base name in a hash table, so
  221.  * ill-formed base names simply turn into hash table lookup failures.
  222.  */
  223. long
  224. parse_program_resource_name(const GLchar *name,
  225.                             const GLchar **out_base_name_end)
  226. {
  227.    /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
  228.     *
  229.     *     "When an integer array element or block instance number is part of
  230.     *     the name string, it will be specified in decimal form without a "+"
  231.     *     or "-" sign or any extra leading zeroes. Additionally, the name
  232.     *     string will not include white space anywhere in the string."
  233.     */
  234.  
  235.    const size_t len = strlen(name);
  236.    *out_base_name_end = name + len;
  237.  
  238.    if (len == 0 || name[len-1] != ']')
  239.       return -1;
  240.  
  241.    /* Walk backwards over the string looking for a non-digit character.  This
  242.     * had better be the opening bracket for an array index.
  243.     *
  244.     * Initially, i specifies the location of the ']'.  Since the string may
  245.     * contain only the ']' charcater, walk backwards very carefully.
  246.     */
  247.    unsigned i;
  248.    for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
  249.       /* empty */ ;
  250.  
  251.    if ((i == 0) || name[i-1] != '[')
  252.       return -1;
  253.  
  254.    long array_index = strtol(&name[i], NULL, 10);
  255.    if (array_index < 0)
  256.       return -1;
  257.  
  258.    *out_base_name_end = name + (i - 1);
  259.    return array_index;
  260. }
  261.  
  262.  
  263. void
  264. link_invalidate_variable_locations(gl_shader *sh, int input_base,
  265.                                    int output_base)
  266. {
  267.    foreach_list(node, sh->ir) {
  268.       ir_variable *const var = ((ir_instruction *) node)->as_variable();
  269.  
  270.       if (var == NULL)
  271.          continue;
  272.  
  273.       int base;
  274.       switch (var->mode) {
  275.       case ir_var_shader_in:
  276.          base = input_base;
  277.          break;
  278.       case ir_var_shader_out:
  279.          base = output_base;
  280.          break;
  281.       default:
  282.          continue;
  283.       }
  284.  
  285.       /* Only assign locations for generic attributes / varyings / etc.
  286.        */
  287.       if ((var->location >= base) && !var->explicit_location)
  288.          var->location = -1;
  289.  
  290.       if ((var->location == -1) && !var->explicit_location) {
  291.          var->is_unmatched_generic_inout = 1;
  292.          var->location_frac = 0;
  293.       } else {
  294.          var->is_unmatched_generic_inout = 0;
  295.       }
  296.    }
  297. }
  298.  
  299.  
  300. /**
  301.  * Determine the number of attribute slots required for a particular type
  302.  *
  303.  * This code is here because it implements the language rules of a specific
  304.  * GLSL version.  Since it's a property of the language and not a property of
  305.  * types in general, it doesn't really belong in glsl_type.
  306.  */
  307. unsigned
  308. count_attribute_slots(const glsl_type *t)
  309. {
  310.    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
  311.     *
  312.     *     "A scalar input counts the same amount against this limit as a vec4,
  313.     *     so applications may want to consider packing groups of four
  314.     *     unrelated float inputs together into a vector to better utilize the
  315.     *     capabilities of the underlying hardware. A matrix input will use up
  316.     *     multiple locations.  The number of locations used will equal the
  317.     *     number of columns in the matrix."
  318.     *
  319.     * The spec does not explicitly say how arrays are counted.  However, it
  320.     * should be safe to assume the total number of slots consumed by an array
  321.     * is the number of entries in the array multiplied by the number of slots
  322.     * consumed by a single element of the array.
  323.     */
  324.  
  325.    if (t->is_array())
  326.       return t->array_size() * count_attribute_slots(t->element_type());
  327.  
  328.    if (t->is_matrix())
  329.       return t->matrix_columns;
  330.  
  331.    return 1;
  332. }
  333.  
  334.  
  335. /**
  336.  * Verify that a vertex shader executable meets all semantic requirements.
  337.  *
  338.  * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
  339.  * as a side effect.
  340.  *
  341.  * \param shader  Vertex shader executable to be verified
  342.  */
  343. bool
  344. validate_vertex_shader_executable(struct gl_shader_program *prog,
  345.                                   struct gl_shader *shader)
  346. {
  347.    if (shader == NULL)
  348.       return true;
  349.  
  350.    /* From the GLSL 1.10 spec, page 48:
  351.     *
  352.     *     "The variable gl_Position is available only in the vertex
  353.     *      language and is intended for writing the homogeneous vertex
  354.     *      position. All executions of a well-formed vertex shader
  355.     *      executable must write a value into this variable. [...] The
  356.     *      variable gl_Position is available only in the vertex
  357.     *      language and is intended for writing the homogeneous vertex
  358.     *      position. All executions of a well-formed vertex shader
  359.     *      executable must write a value into this variable."
  360.     *
  361.     * while in GLSL 1.40 this text is changed to:
  362.     *
  363.     *     "The variable gl_Position is available only in the vertex
  364.     *      language and is intended for writing the homogeneous vertex
  365.     *      position. It can be written at any time during shader
  366.     *      execution. It may also be read back by a vertex shader
  367.     *      after being written. This value will be used by primitive
  368.     *      assembly, clipping, culling, and other fixed functionality
  369.     *      operations, if present, that operate on primitives after
  370.     *      vertex processing has occurred. Its value is undefined if
  371.     *      the vertex shader executable does not write gl_Position."
  372.     *
  373.     * GLSL ES 3.00 is similar to GLSL 1.40--failing to write to gl_Position is
  374.     * not an error.
  375.     */
  376.    if (prog->Version < (prog->IsES ? 300 : 140)) {
  377.       find_assignment_visitor find("gl_Position");
  378.       find.run(shader->ir);
  379.       if (!find.variable_found()) {
  380.          linker_error(prog, "vertex shader does not write to `gl_Position'\n");
  381.          return false;
  382.       }
  383.    }
  384.  
  385.    prog->Vert.ClipDistanceArraySize = 0;
  386.  
  387.    if (!prog->IsES && prog->Version >= 130) {
  388.       /* From section 7.1 (Vertex Shader Special Variables) of the
  389.        * GLSL 1.30 spec:
  390.        *
  391.        *   "It is an error for a shader to statically write both
  392.        *   gl_ClipVertex and gl_ClipDistance."
  393.        *
  394.        * This does not apply to GLSL ES shaders, since GLSL ES defines neither
  395.        * gl_ClipVertex nor gl_ClipDistance.
  396.        */
  397.       find_assignment_visitor clip_vertex("gl_ClipVertex");
  398.       find_assignment_visitor clip_distance("gl_ClipDistance");
  399.  
  400.       clip_vertex.run(shader->ir);
  401.       clip_distance.run(shader->ir);
  402.       if (clip_vertex.variable_found() && clip_distance.variable_found()) {
  403.          linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
  404.                       "and `gl_ClipDistance'\n");
  405.          return false;
  406.       }
  407.       prog->Vert.UsesClipDistance = clip_distance.variable_found();
  408.       ir_variable *clip_distance_var =
  409.          shader->symbols->get_variable("gl_ClipDistance");
  410.       if (clip_distance_var)
  411.          prog->Vert.ClipDistanceArraySize = clip_distance_var->type->length;
  412.    }
  413.  
  414.    return true;
  415. }
  416.  
  417.  
  418. /**
  419.  * Verify that a fragment shader executable meets all semantic requirements
  420.  *
  421.  * \param shader  Fragment shader executable to be verified
  422.  */
  423. bool
  424. validate_fragment_shader_executable(struct gl_shader_program *prog,
  425.                                     struct gl_shader *shader)
  426. {
  427.    if (shader == NULL)
  428.       return true;
  429.  
  430.    find_assignment_visitor frag_color("gl_FragColor");
  431.    find_assignment_visitor frag_data("gl_FragData");
  432.  
  433.    frag_color.run(shader->ir);
  434.    frag_data.run(shader->ir);
  435.  
  436.    if (frag_color.variable_found() && frag_data.variable_found()) {
  437.       linker_error(prog,  "fragment shader writes to both "
  438.                    "`gl_FragColor' and `gl_FragData'\n");
  439.       return false;
  440.    }
  441.  
  442.    return true;
  443. }
  444.  
  445.  
  446. /**
  447.  * Generate a string describing the mode of a variable
  448.  */
  449. static const char *
  450. mode_string(const ir_variable *var)
  451. {
  452.    switch (var->mode) {
  453.    case ir_var_auto:
  454.       return (var->read_only) ? "global constant" : "global variable";
  455.  
  456.    case ir_var_uniform:    return "uniform";
  457.    case ir_var_shader_in:  return "shader input";
  458.    case ir_var_shader_out: return "shader output";
  459.  
  460.    case ir_var_const_in:
  461.    case ir_var_temporary:
  462.    default:
  463.       assert(!"Should not get here.");
  464.       return "invalid variable";
  465.    }
  466. }
  467.  
  468.  
  469. /**
  470.  * Perform validation of global variables used across multiple shaders
  471.  */
  472. bool
  473. cross_validate_globals(struct gl_shader_program *prog,
  474.                        struct gl_shader **shader_list,
  475.                        unsigned num_shaders,
  476.                        bool uniforms_only)
  477. {
  478.    /* Examine all of the uniforms in all of the shaders and cross validate
  479.     * them.
  480.     */
  481.    glsl_symbol_table variables;
  482.    for (unsigned i = 0; i < num_shaders; i++) {
  483.       if (shader_list[i] == NULL)
  484.          continue;
  485.  
  486.       foreach_list(node, shader_list[i]->ir) {
  487.          ir_variable *const var = ((ir_instruction *) node)->as_variable();
  488.  
  489.          if (var == NULL)
  490.             continue;
  491.  
  492.          if (uniforms_only && (var->mode != ir_var_uniform))
  493.             continue;
  494.  
  495.          /* Don't cross validate temporaries that are at global scope.  These
  496.           * will eventually get pulled into the shaders 'main'.
  497.           */
  498.          if (var->mode == ir_var_temporary)
  499.             continue;
  500.  
  501.          /* If a global with this name has already been seen, verify that the
  502.           * new instance has the same type.  In addition, if the globals have
  503.           * initializers, the values of the initializers must be the same.
  504.           */
  505.          ir_variable *const existing = variables.get_variable(var->name);
  506.          if (existing != NULL) {
  507.             if (var->type != existing->type) {
  508.                /* Consider the types to be "the same" if both types are arrays
  509.                 * of the same type and one of the arrays is implicitly sized.
  510.                 * In addition, set the type of the linked variable to the
  511.                 * explicitly sized array.
  512.                 */
  513.                if (var->type->is_array()
  514.                    && existing->type->is_array()
  515.                    && (var->type->fields.array == existing->type->fields.array)
  516.                    && ((var->type->length == 0)
  517.                        || (existing->type->length == 0))) {
  518.                   if (var->type->length != 0) {
  519.                      existing->type = var->type;
  520.                   }
  521.                } else {
  522.                   linker_error(prog, "%s `%s' declared as type "
  523.                                "`%s' and type `%s'\n",
  524.                                mode_string(var),
  525.                                var->name, var->type->name,
  526.                                existing->type->name);
  527.                   return false;
  528.                }
  529.             }
  530.  
  531.             if (var->explicit_location) {
  532.                if (existing->explicit_location
  533.                    && (var->location != existing->location)) {
  534.                      linker_error(prog, "explicit locations for %s "
  535.                                   "`%s' have differing values\n",
  536.                                   mode_string(var), var->name);
  537.                      return false;
  538.                }
  539.  
  540.                existing->location = var->location;
  541.                existing->explicit_location = true;
  542.             }
  543.  
  544.             /* From the GLSL 4.20 specification:
  545.              * "A link error will result if two compilation units in a program
  546.              *  specify different integer-constant bindings for the same
  547.              *  opaque-uniform name.  However, it is not an error to specify a
  548.              *  binding on some but not all declarations for the same name"
  549.              */
  550.             if (var->explicit_binding) {
  551.                if (existing->explicit_binding &&
  552.                    var->binding != existing->binding) {
  553.                   linker_error(prog, "explicit bindings for %s "
  554.                                "`%s' have differing values\n",
  555.                                mode_string(var), var->name);
  556.                   return false;
  557.                }
  558.  
  559.                existing->binding = var->binding;
  560.                existing->explicit_binding = true;
  561.             }
  562.  
  563.             /* Validate layout qualifiers for gl_FragDepth.
  564.              *
  565.              * From the AMD/ARB_conservative_depth specs:
  566.              *
  567.              *    "If gl_FragDepth is redeclared in any fragment shader in a
  568.              *    program, it must be redeclared in all fragment shaders in
  569.              *    that program that have static assignments to
  570.              *    gl_FragDepth. All redeclarations of gl_FragDepth in all
  571.              *    fragment shaders in a single program must have the same set
  572.              *    of qualifiers."
  573.              */
  574.             if (strcmp(var->name, "gl_FragDepth") == 0) {
  575.                bool layout_declared = var->depth_layout != ir_depth_layout_none;
  576.                bool layout_differs =
  577.                   var->depth_layout != existing->depth_layout;
  578.  
  579.                if (layout_declared && layout_differs) {
  580.                   linker_error(prog,
  581.                                "All redeclarations of gl_FragDepth in all "
  582.                                "fragment shaders in a single program must have "
  583.                                "the same set of qualifiers.");
  584.                }
  585.  
  586.                if (var->used && layout_differs) {
  587.                   linker_error(prog,
  588.                                "If gl_FragDepth is redeclared with a layout "
  589.                                "qualifier in any fragment shader, it must be "
  590.                                "redeclared with the same layout qualifier in "
  591.                                "all fragment shaders that have assignments to "
  592.                                "gl_FragDepth");
  593.                }
  594.             }
  595.  
  596.             /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
  597.              *
  598.              *     "If a shared global has multiple initializers, the
  599.              *     initializers must all be constant expressions, and they
  600.              *     must all have the same value. Otherwise, a link error will
  601.              *     result. (A shared global having only one initializer does
  602.              *     not require that initializer to be a constant expression.)"
  603.              *
  604.              * Previous to 4.20 the GLSL spec simply said that initializers
  605.              * must have the same value.  In this case of non-constant
  606.              * initializers, this was impossible to determine.  As a result,
  607.              * no vendor actually implemented that behavior.  The 4.20
  608.              * behavior matches the implemented behavior of at least one other
  609.              * vendor, so we'll implement that for all GLSL versions.
  610.              */
  611.             if (var->constant_initializer != NULL) {
  612.                if (existing->constant_initializer != NULL) {
  613.                   if (!var->constant_initializer->has_value(existing->constant_initializer)) {
  614.                      linker_error(prog, "initializers for %s "
  615.                                   "`%s' have differing values\n",
  616.                                   mode_string(var), var->name);
  617.                      return false;
  618.                   }
  619.                } else {
  620.                   /* If the first-seen instance of a particular uniform did not
  621.                    * have an initializer but a later instance does, copy the
  622.                    * initializer to the version stored in the symbol table.
  623.                    */
  624.                   /* FINISHME: This is wrong.  The constant_value field should
  625.                    * FINISHME: not be modified!  Imagine a case where a shader
  626.                    * FINISHME: without an initializer is linked in two different
  627.                    * FINISHME: programs with shaders that have differing
  628.                    * FINISHME: initializers.  Linking with the first will
  629.                    * FINISHME: modify the shader, and linking with the second
  630.                    * FINISHME: will fail.
  631.                    */
  632.                   existing->constant_initializer =
  633.                      var->constant_initializer->clone(ralloc_parent(existing),
  634.                                                       NULL);
  635.                }
  636.             }
  637.  
  638.             if (var->has_initializer) {
  639.                if (existing->has_initializer
  640.                    && (var->constant_initializer == NULL
  641.                        || existing->constant_initializer == NULL)) {
  642.                   linker_error(prog,
  643.                                "shared global variable `%s' has multiple "
  644.                                "non-constant initializers.\n",
  645.                                var->name);
  646.                   return false;
  647.                }
  648.  
  649.                /* Some instance had an initializer, so keep track of that.  In
  650.                 * this location, all sorts of initializers (constant or
  651.                 * otherwise) will propagate the existence to the variable
  652.                 * stored in the symbol table.
  653.                 */
  654.                existing->has_initializer = true;
  655.             }
  656.  
  657.             if (existing->invariant != var->invariant) {
  658.                linker_error(prog, "declarations for %s `%s' have "
  659.                             "mismatching invariant qualifiers\n",
  660.                             mode_string(var), var->name);
  661.                return false;
  662.             }
  663.             if (existing->centroid != var->centroid) {
  664.                linker_error(prog, "declarations for %s `%s' have "
  665.                             "mismatching centroid qualifiers\n",
  666.                             mode_string(var), var->name);
  667.                return false;
  668.             }
  669.          } else
  670.             variables.add_variable(var);
  671.       }
  672.    }
  673.  
  674.    return true;
  675. }
  676.  
  677.  
  678. /**
  679.  * Perform validation of uniforms used across multiple shader stages
  680.  */
  681. bool
  682. cross_validate_uniforms(struct gl_shader_program *prog)
  683. {
  684.    return cross_validate_globals(prog, prog->_LinkedShaders,
  685.                                  MESA_SHADER_TYPES, true);
  686. }
  687.  
  688. /**
  689.  * Accumulates the array of prog->UniformBlocks and checks that all
  690.  * definitons of blocks agree on their contents.
  691.  */
  692. static bool
  693. interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
  694. {
  695.    unsigned max_num_uniform_blocks = 0;
  696.    for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
  697.       if (prog->_LinkedShaders[i])
  698.          max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
  699.    }
  700.  
  701.    for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
  702.       struct gl_shader *sh = prog->_LinkedShaders[i];
  703.  
  704.       prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
  705.                                                      max_num_uniform_blocks);
  706.       for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
  707.          prog->UniformBlockStageIndex[i][j] = -1;
  708.  
  709.       if (sh == NULL)
  710.          continue;
  711.  
  712.       for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
  713.          int index = link_cross_validate_uniform_block(prog,
  714.                                                        &prog->UniformBlocks,
  715.                                                        &prog->NumUniformBlocks,
  716.                                                        &sh->UniformBlocks[j]);
  717.  
  718.          if (index == -1) {
  719.             linker_error(prog, "uniform block `%s' has mismatching definitions",
  720.                          sh->UniformBlocks[j].Name);
  721.             return false;
  722.          }
  723.  
  724.          prog->UniformBlockStageIndex[i][index] = j;
  725.       }
  726.    }
  727.  
  728.    return true;
  729. }
  730.  
  731.  
  732. /**
  733.  * Populates a shaders symbol table with all global declarations
  734.  */
  735. static void
  736. populate_symbol_table(gl_shader *sh)
  737. {
  738.    sh->symbols = new(sh) glsl_symbol_table;
  739.  
  740.    foreach_list(node, sh->ir) {
  741.       ir_instruction *const inst = (ir_instruction *) node;
  742.       ir_variable *var;
  743.       ir_function *func;
  744.  
  745.       if ((func = inst->as_function()) != NULL) {
  746.          sh->symbols->add_function(func);
  747.       } else if ((var = inst->as_variable()) != NULL) {
  748.          sh->symbols->add_variable(var);
  749.       }
  750.    }
  751. }
  752.  
  753.  
  754. /**
  755.  * Remap variables referenced in an instruction tree
  756.  *
  757.  * This is used when instruction trees are cloned from one shader and placed in
  758.  * another.  These trees will contain references to \c ir_variable nodes that
  759.  * do not exist in the target shader.  This function finds these \c ir_variable
  760.  * references and replaces the references with matching variables in the target
  761.  * shader.
  762.  *
  763.  * If there is no matching variable in the target shader, a clone of the
  764.  * \c ir_variable is made and added to the target shader.  The new variable is
  765.  * added to \b both the instruction stream and the symbol table.
  766.  *
  767.  * \param inst         IR tree that is to be processed.
  768.  * \param symbols      Symbol table containing global scope symbols in the
  769.  *                     linked shader.
  770.  * \param instructions Instruction stream where new variable declarations
  771.  *                     should be added.
  772.  */
  773. void
  774. remap_variables(ir_instruction *inst, struct gl_shader *target,
  775.                 hash_table *temps)
  776. {
  777.    class remap_visitor : public ir_hierarchical_visitor {
  778.    public:
  779.          remap_visitor(struct gl_shader *target,
  780.                     hash_table *temps)
  781.       {
  782.          this->target = target;
  783.          this->symbols = target->symbols;
  784.          this->instructions = target->ir;
  785.          this->temps = temps;
  786.       }
  787.  
  788.       virtual ir_visitor_status visit(ir_dereference_variable *ir)
  789.       {
  790.          if (ir->var->mode == ir_var_temporary) {
  791.             ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
  792.  
  793.             assert(var != NULL);
  794.             ir->var = var;
  795.             return visit_continue;
  796.          }
  797.  
  798.          ir_variable *const existing =
  799.             this->symbols->get_variable(ir->var->name);
  800.          if (existing != NULL)
  801.             ir->var = existing;
  802.          else {
  803.             ir_variable *copy = ir->var->clone(this->target, NULL);
  804.  
  805.             this->symbols->add_variable(copy);
  806.             this->instructions->push_head(copy);
  807.             ir->var = copy;
  808.          }
  809.  
  810.          return visit_continue;
  811.       }
  812.  
  813.    private:
  814.       struct gl_shader *target;
  815.       glsl_symbol_table *symbols;
  816.       exec_list *instructions;
  817.       hash_table *temps;
  818.    };
  819.  
  820.    remap_visitor v(target, temps);
  821.  
  822.    inst->accept(&v);
  823. }
  824.  
  825.  
  826. /**
  827.  * Move non-declarations from one instruction stream to another
  828.  *
  829.  * The intended usage pattern of this function is to pass the pointer to the
  830.  * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
  831.  * pointer) for \c last and \c false for \c make_copies on the first
  832.  * call.  Successive calls pass the return value of the previous call for
  833.  * \c last and \c true for \c make_copies.
  834.  *
  835.  * \param instructions Source instruction stream
  836.  * \param last         Instruction after which new instructions should be
  837.  *                     inserted in the target instruction stream
  838.  * \param make_copies  Flag selecting whether instructions in \c instructions
  839.  *                     should be copied (via \c ir_instruction::clone) into the
  840.  *                     target list or moved.
  841.  *
  842.  * \return
  843.  * The new "last" instruction in the target instruction stream.  This pointer
  844.  * is suitable for use as the \c last parameter of a later call to this
  845.  * function.
  846.  */
  847. exec_node *
  848. move_non_declarations(exec_list *instructions, exec_node *last,
  849.                       bool make_copies, gl_shader *target)
  850. {
  851.    hash_table *temps = NULL;
  852.  
  853.    if (make_copies)
  854.       temps = hash_table_ctor(0, hash_table_pointer_hash,
  855.                               hash_table_pointer_compare);
  856.  
  857.    foreach_list_safe(node, instructions) {
  858.       ir_instruction *inst = (ir_instruction *) node;
  859.  
  860.       if (inst->as_function())
  861.          continue;
  862.  
  863.       ir_variable *var = inst->as_variable();
  864.       if ((var != NULL) && (var->mode != ir_var_temporary))
  865.          continue;
  866.  
  867.       assert(inst->as_assignment()
  868.              || inst->as_call()
  869.              || inst->as_if() /* for initializers with the ?: operator */
  870.              || ((var != NULL) && (var->mode == ir_var_temporary)));
  871.  
  872.       if (make_copies) {
  873.          inst = inst->clone(target, NULL);
  874.  
  875.          if (var != NULL)
  876.             hash_table_insert(temps, inst, var);
  877.          else
  878.             remap_variables(inst, target, temps);
  879.       } else {
  880.          inst->remove();
  881.       }
  882.  
  883.       last->insert_after(inst);
  884.       last = inst;
  885.    }
  886.  
  887.    if (make_copies)
  888.       hash_table_dtor(temps);
  889.  
  890.    return last;
  891. }
  892.  
  893. /**
  894.  * Get the function signature for main from a shader
  895.  */
  896. static ir_function_signature *
  897. get_main_function_signature(gl_shader *sh)
  898. {
  899.    ir_function *const f = sh->symbols->get_function("main");
  900.    if (f != NULL) {
  901.       exec_list void_parameters;
  902.  
  903.       /* Look for the 'void main()' signature and ensure that it's defined.
  904.        * This keeps the linker from accidentally pick a shader that just
  905.        * contains a prototype for main.
  906.        *
  907.        * We don't have to check for multiple definitions of main (in multiple
  908.        * shaders) because that would have already been caught above.
  909.        */
  910.       ir_function_signature *sig = f->matching_signature(&void_parameters);
  911.       if ((sig != NULL) && sig->is_defined) {
  912.          return sig;
  913.       }
  914.    }
  915.  
  916.    return NULL;
  917. }
  918.  
  919.  
  920. /**
  921.  * This class is only used in link_intrastage_shaders() below but declaring
  922.  * it inside that function leads to compiler warnings with some versions of
  923.  * gcc.
  924.  */
  925. class array_sizing_visitor : public ir_hierarchical_visitor {
  926. public:
  927.    virtual ir_visitor_status visit(ir_variable *var)
  928.    {
  929.       if (var->type->is_array() && (var->type->length == 0)) {
  930.          const glsl_type *type =
  931.             glsl_type::get_array_instance(var->type->fields.array,
  932.                                           var->max_array_access + 1);
  933.          assert(type != NULL);
  934.          var->type = type;
  935.       }
  936.       return visit_continue;
  937.    }
  938. };
  939.  
  940. /**
  941.  * Combine a group of shaders for a single stage to generate a linked shader
  942.  *
  943.  * \note
  944.  * If this function is supplied a single shader, it is cloned, and the new
  945.  * shader is returned.
  946.  */
  947. static struct gl_shader *
  948. link_intrastage_shaders(void *mem_ctx,
  949.                         struct gl_context *ctx,
  950.                         struct gl_shader_program *prog,
  951.                         struct gl_shader **shader_list,
  952.                         unsigned num_shaders)
  953. {
  954.    struct gl_uniform_block *uniform_blocks = NULL;
  955.  
  956.    /* Check that global variables defined in multiple shaders are consistent.
  957.     */
  958.    if (!cross_validate_globals(prog, shader_list, num_shaders, false))
  959.       return NULL;
  960.  
  961.    /* Check that interface blocks defined in multiple shaders are consistent.
  962.     */
  963.    if (!validate_intrastage_interface_blocks((const gl_shader **)shader_list,
  964.                                              num_shaders))
  965.       return NULL;
  966.  
  967.    /* Check that uniform blocks between shaders for a stage agree. */
  968.    const int num_uniform_blocks =
  969.       link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders,
  970.                           &uniform_blocks);
  971.    if (num_uniform_blocks < 0)
  972.       return NULL;
  973.  
  974.    /* Check that there is only a single definition of each function signature
  975.     * across all shaders.
  976.     */
  977.    for (unsigned i = 0; i < (num_shaders - 1); i++) {
  978.       foreach_list(node, shader_list[i]->ir) {
  979.          ir_function *const f = ((ir_instruction *) node)->as_function();
  980.  
  981.          if (f == NULL)
  982.             continue;
  983.  
  984.          for (unsigned j = i + 1; j < num_shaders; j++) {
  985.             ir_function *const other =
  986.                shader_list[j]->symbols->get_function(f->name);
  987.  
  988.             /* If the other shader has no function (and therefore no function
  989.              * signatures) with the same name, skip to the next shader.
  990.              */
  991.             if (other == NULL)
  992.                continue;
  993.  
  994.             foreach_iter (exec_list_iterator, iter, *f) {
  995.                ir_function_signature *sig =
  996.                   (ir_function_signature *) iter.get();
  997.  
  998.                if (!sig->is_defined || sig->is_builtin)
  999.                   continue;
  1000.  
  1001.                ir_function_signature *other_sig =
  1002.                   other->exact_matching_signature(& sig->parameters);
  1003.  
  1004.                if ((other_sig != NULL) && other_sig->is_defined
  1005.                    && !other_sig->is_builtin) {
  1006.                   linker_error(prog, "function `%s' is multiply defined",
  1007.                                f->name);
  1008.                   return NULL;
  1009.                }
  1010.             }
  1011.          }
  1012.       }
  1013.    }
  1014.  
  1015.    /* Find the shader that defines main, and make a clone of it.
  1016.     *
  1017.     * Starting with the clone, search for undefined references.  If one is
  1018.     * found, find the shader that defines it.  Clone the reference and add
  1019.     * it to the shader.  Repeat until there are no undefined references or
  1020.     * until a reference cannot be resolved.
  1021.     */
  1022.    gl_shader *main = NULL;
  1023.    for (unsigned i = 0; i < num_shaders; i++) {
  1024.       if (get_main_function_signature(shader_list[i]) != NULL) {
  1025.          main = shader_list[i];
  1026.          break;
  1027.       }
  1028.    }
  1029.  
  1030.    if (main == NULL) {
  1031.       linker_error(prog, "%s shader lacks `main'\n",
  1032.                    _mesa_glsl_shader_target_name(shader_list[0]->Type));
  1033.       return NULL;
  1034.    }
  1035.  
  1036.    gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
  1037.    linked->ir = new(linked) exec_list;
  1038.    clone_ir_list(mem_ctx, linked->ir, main->ir);
  1039.  
  1040.    linked->UniformBlocks = uniform_blocks;
  1041.    linked->NumUniformBlocks = num_uniform_blocks;
  1042.    ralloc_steal(linked, linked->UniformBlocks);
  1043.  
  1044.    populate_symbol_table(linked);
  1045.  
  1046.    /* The a pointer to the main function in the final linked shader (i.e., the
  1047.     * copy of the original shader that contained the main function).
  1048.     */
  1049.    ir_function_signature *const main_sig = get_main_function_signature(linked);
  1050.  
  1051.    /* Move any instructions other than variable declarations or function
  1052.     * declarations into main.
  1053.     */
  1054.    exec_node *insertion_point =
  1055.       move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
  1056.                             linked);
  1057.  
  1058.    for (unsigned i = 0; i < num_shaders; i++) {
  1059.       if (shader_list[i] == main)
  1060.          continue;
  1061.  
  1062.       insertion_point = move_non_declarations(shader_list[i]->ir,
  1063.                                               insertion_point, true, linked);
  1064.    }
  1065.  
  1066.    /* Resolve initializers for global variables in the linked shader.
  1067.     */
  1068.    unsigned num_linking_shaders = num_shaders;
  1069.    for (unsigned i = 0; i < num_shaders; i++)
  1070.       num_linking_shaders += shader_list[i]->num_builtins_to_link;
  1071.  
  1072.    gl_shader **linking_shaders =
  1073.       (gl_shader **) calloc(num_linking_shaders, sizeof(gl_shader *));
  1074.  
  1075.    memcpy(linking_shaders, shader_list,
  1076.           sizeof(linking_shaders[0]) * num_shaders);
  1077.  
  1078.    unsigned idx = num_shaders;
  1079.    for (unsigned i = 0; i < num_shaders; i++) {
  1080.       memcpy(&linking_shaders[idx], shader_list[i]->builtins_to_link,
  1081.              sizeof(linking_shaders[0]) * shader_list[i]->num_builtins_to_link);
  1082.       idx += shader_list[i]->num_builtins_to_link;
  1083.    }
  1084.  
  1085.    assert(idx == num_linking_shaders);
  1086.  
  1087.    if (!link_function_calls(prog, linked, linking_shaders,
  1088.                             num_linking_shaders)) {
  1089.       ctx->Driver.DeleteShader(ctx, linked);
  1090.       linked = NULL;
  1091.    }
  1092.  
  1093.    free(linking_shaders);
  1094.  
  1095.    /* At this point linked should contain all of the linked IR, so
  1096.     * validate it to make sure nothing went wrong.
  1097.     */
  1098.    if (linked)
  1099.       validate_ir_tree(linked->ir);
  1100.  
  1101.    /* Make a pass over all variable declarations to ensure that arrays with
  1102.     * unspecified sizes have a size specified.  The size is inferred from the
  1103.     * max_array_access field.
  1104.     */
  1105.    if (linked != NULL) {
  1106.       array_sizing_visitor v;
  1107.  
  1108.       v.run(linked->ir);
  1109.    }
  1110.  
  1111.    return linked;
  1112. }
  1113.  
  1114. /**
  1115.  * Update the sizes of linked shader uniform arrays to the maximum
  1116.  * array index used.
  1117.  *
  1118.  * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
  1119.  *
  1120.  *     If one or more elements of an array are active,
  1121.  *     GetActiveUniform will return the name of the array in name,
  1122.  *     subject to the restrictions listed above. The type of the array
  1123.  *     is returned in type. The size parameter contains the highest
  1124.  *     array element index used, plus one. The compiler or linker
  1125.  *     determines the highest index used.  There will be only one
  1126.  *     active uniform reported by the GL per uniform array.
  1127.  
  1128.  */
  1129. static void
  1130. update_array_sizes(struct gl_shader_program *prog)
  1131. {
  1132.    for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
  1133.          if (prog->_LinkedShaders[i] == NULL)
  1134.             continue;
  1135.  
  1136.       foreach_list(node, prog->_LinkedShaders[i]->ir) {
  1137.          ir_variable *const var = ((ir_instruction *) node)->as_variable();
  1138.  
  1139.          if ((var == NULL) || (var->mode != ir_var_uniform &&
  1140.                                var->mode != ir_var_shader_in &&
  1141.                                var->mode != ir_var_shader_out) ||
  1142.              !var->type->is_array())
  1143.             continue;
  1144.  
  1145.          /* GL_ARB_uniform_buffer_object says that std140 uniforms
  1146.           * will not be eliminated.  Since we always do std140, just
  1147.           * don't resize arrays in UBOs.
  1148.           */
  1149.          if (var->is_in_uniform_block())
  1150.             continue;
  1151.  
  1152.          unsigned int size = var->max_array_access;
  1153.          for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
  1154.                if (prog->_LinkedShaders[j] == NULL)
  1155.                   continue;
  1156.  
  1157.             foreach_list(node2, prog->_LinkedShaders[j]->ir) {
  1158.                ir_variable *other_var = ((ir_instruction *) node2)->as_variable();
  1159.                if (!other_var)
  1160.                   continue;
  1161.  
  1162.                if (strcmp(var->name, other_var->name) == 0 &&
  1163.                    other_var->max_array_access > size) {
  1164.                   size = other_var->max_array_access;
  1165.                }
  1166.             }
  1167.          }
  1168.  
  1169.          if (size + 1 != var->type->length) {
  1170.             /* If this is a built-in uniform (i.e., it's backed by some
  1171.              * fixed-function state), adjust the number of state slots to
  1172.              * match the new array size.  The number of slots per array entry
  1173.              * is not known.  It seems safe to assume that the total number of
  1174.              * slots is an integer multiple of the number of array elements.
  1175.              * Determine the number of slots per array element by dividing by
  1176.              * the old (total) size.
  1177.              */
  1178.             if (var->num_state_slots > 0) {
  1179.                var->num_state_slots = (size + 1)
  1180.                   * (var->num_state_slots / var->type->length);
  1181.             }
  1182.  
  1183.             var->type = glsl_type::get_array_instance(var->type->fields.array,
  1184.                                                       size + 1);
  1185.             /* FINISHME: We should update the types of array
  1186.              * dereferences of this variable now.
  1187.              */
  1188.          }
  1189.       }
  1190.    }
  1191. }
  1192.  
  1193. /**
  1194.  * Find a contiguous set of available bits in a bitmask.
  1195.  *
  1196.  * \param used_mask     Bits representing used (1) and unused (0) locations
  1197.  * \param needed_count  Number of contiguous bits needed.
  1198.  *
  1199.  * \return
  1200.  * Base location of the available bits on success or -1 on failure.
  1201.  */
  1202. int
  1203. find_available_slots(unsigned used_mask, unsigned needed_count)
  1204. {
  1205.    unsigned needed_mask = (1 << needed_count) - 1;
  1206.    const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
  1207.  
  1208.    /* The comparison to 32 is redundant, but without it GCC emits "warning:
  1209.     * cannot optimize possibly infinite loops" for the loop below.
  1210.     */
  1211.    if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
  1212.       return -1;
  1213.  
  1214.    for (int i = 0; i <= max_bit_to_test; i++) {
  1215.       if ((needed_mask & ~used_mask) == needed_mask)
  1216.          return i;
  1217.  
  1218.       needed_mask <<= 1;
  1219.    }
  1220.  
  1221.    return -1;
  1222. }
  1223.  
  1224.  
  1225. /**
  1226.  * Assign locations for either VS inputs for FS outputs
  1227.  *
  1228.  * \param prog          Shader program whose variables need locations assigned
  1229.  * \param target_index  Selector for the program target to receive location
  1230.  *                      assignmnets.  Must be either \c MESA_SHADER_VERTEX or
  1231.  *                      \c MESA_SHADER_FRAGMENT.
  1232.  * \param max_index     Maximum number of generic locations.  This corresponds
  1233.  *                      to either the maximum number of draw buffers or the
  1234.  *                      maximum number of generic attributes.
  1235.  *
  1236.  * \return
  1237.  * If locations are successfully assigned, true is returned.  Otherwise an
  1238.  * error is emitted to the shader link log and false is returned.
  1239.  */
  1240. bool
  1241. assign_attribute_or_color_locations(gl_shader_program *prog,
  1242.                                     unsigned target_index,
  1243.                                     unsigned max_index)
  1244. {
  1245.    /* Mark invalid locations as being used.
  1246.     */
  1247.    unsigned used_locations = (max_index >= 32)
  1248.       ? ~0 : ~((1 << max_index) - 1);
  1249.  
  1250.    assert((target_index == MESA_SHADER_VERTEX)
  1251.           || (target_index == MESA_SHADER_FRAGMENT));
  1252.  
  1253.    gl_shader *const sh = prog->_LinkedShaders[target_index];
  1254.    if (sh == NULL)
  1255.       return true;
  1256.  
  1257.    /* Operate in a total of four passes.
  1258.     *
  1259.     * 1. Invalidate the location assignments for all vertex shader inputs.
  1260.     *
  1261.     * 2. Assign locations for inputs that have user-defined (via
  1262.     *    glBindVertexAttribLocation) locations and outputs that have
  1263.     *    user-defined locations (via glBindFragDataLocation).
  1264.     *
  1265.     * 3. Sort the attributes without assigned locations by number of slots
  1266.     *    required in decreasing order.  Fragmentation caused by attribute
  1267.     *    locations assigned by the application may prevent large attributes
  1268.     *    from having enough contiguous space.
  1269.     *
  1270.     * 4. Assign locations to any inputs without assigned locations.
  1271.     */
  1272.  
  1273.    const int generic_base = (target_index == MESA_SHADER_VERTEX)
  1274.       ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
  1275.  
  1276.    const enum ir_variable_mode direction =
  1277.       (target_index == MESA_SHADER_VERTEX)
  1278.       ? ir_var_shader_in : ir_var_shader_out;
  1279.  
  1280.  
  1281.    /* Temporary storage for the set of attributes that need locations assigned.
  1282.     */
  1283.    struct temp_attr {
  1284.       unsigned slots;
  1285.       ir_variable *var;
  1286.  
  1287.       /* Used below in the call to qsort. */
  1288.       static int compare(const void *a, const void *b)
  1289.       {
  1290.          const temp_attr *const l = (const temp_attr *) a;
  1291.          const temp_attr *const r = (const temp_attr *) b;
  1292.  
  1293.          /* Reversed because we want a descending order sort below. */
  1294.          return r->slots - l->slots;
  1295.       }
  1296.    } to_assign[16];
  1297.  
  1298.    unsigned num_attr = 0;
  1299.  
  1300.    foreach_list(node, sh->ir) {
  1301.       ir_variable *const var = ((ir_instruction *) node)->as_variable();
  1302.  
  1303.       if ((var == NULL) || (var->mode != (unsigned) direction))
  1304.          continue;
  1305.  
  1306.       if (var->explicit_location) {
  1307.          if ((var->location >= (int)(max_index + generic_base))
  1308.              || (var->location < 0)) {
  1309.             linker_error(prog,
  1310.                          "invalid explicit location %d specified for `%s'\n",
  1311.                          (var->location < 0)
  1312.                          ? var->location : var->location - generic_base,
  1313.                          var->name);
  1314.             return false;
  1315.          }
  1316.       } else if (target_index == MESA_SHADER_VERTEX) {
  1317.          unsigned binding;
  1318.  
  1319.          if (prog->AttributeBindings->get(binding, var->name)) {
  1320.             assert(binding >= VERT_ATTRIB_GENERIC0);
  1321.             var->location = binding;
  1322.             var->is_unmatched_generic_inout = 0;
  1323.          }
  1324.       } else if (target_index == MESA_SHADER_FRAGMENT) {
  1325.          unsigned binding;
  1326.          unsigned index;
  1327.  
  1328.          if (prog->FragDataBindings->get(binding, var->name)) {
  1329.             assert(binding >= FRAG_RESULT_DATA0);
  1330.             var->location = binding;
  1331.             var->is_unmatched_generic_inout = 0;
  1332.  
  1333.             if (prog->FragDataIndexBindings->get(index, var->name)) {
  1334.                var->index = index;
  1335.             }
  1336.          }
  1337.       }
  1338.  
  1339.       /* If the variable is not a built-in and has a location statically
  1340.        * assigned in the shader (presumably via a layout qualifier), make sure
  1341.        * that it doesn't collide with other assigned locations.  Otherwise,
  1342.        * add it to the list of variables that need linker-assigned locations.
  1343.        */
  1344.       const unsigned slots = count_attribute_slots(var->type);
  1345.       if (var->location != -1) {
  1346.          if (var->location >= generic_base && var->index < 1) {
  1347.             /* From page 61 of the OpenGL 4.0 spec:
  1348.              *
  1349.              *     "LinkProgram will fail if the attribute bindings assigned
  1350.              *     by BindAttribLocation do not leave not enough space to
  1351.              *     assign a location for an active matrix attribute or an
  1352.              *     active attribute array, both of which require multiple
  1353.              *     contiguous generic attributes."
  1354.              *
  1355.              * Previous versions of the spec contain similar language but omit
  1356.              * the bit about attribute arrays.
  1357.              *
  1358.              * Page 61 of the OpenGL 4.0 spec also says:
  1359.              *
  1360.              *     "It is possible for an application to bind more than one
  1361.              *     attribute name to the same location. This is referred to as
  1362.              *     aliasing. This will only work if only one of the aliased
  1363.              *     attributes is active in the executable program, or if no
  1364.              *     path through the shader consumes more than one attribute of
  1365.              *     a set of attributes aliased to the same location. A link
  1366.              *     error can occur if the linker determines that every path
  1367.              *     through the shader consumes multiple aliased attributes,
  1368.              *     but implementations are not required to generate an error
  1369.              *     in this case."
  1370.              *
  1371.              * These two paragraphs are either somewhat contradictory, or I
  1372.              * don't fully understand one or both of them.
  1373.              */
  1374.             /* FINISHME: The code as currently written does not support
  1375.              * FINISHME: attribute location aliasing (see comment above).
  1376.              */
  1377.             /* Mask representing the contiguous slots that will be used by
  1378.              * this attribute.
  1379.              */
  1380.             const unsigned attr = var->location - generic_base;
  1381.             const unsigned use_mask = (1 << slots) - 1;
  1382.  
  1383.             /* Generate a link error if the set of bits requested for this
  1384.              * attribute overlaps any previously allocated bits.
  1385.              */
  1386.             if ((~(use_mask << attr) & used_locations) != used_locations) {
  1387.                const char *const string = (target_index == MESA_SHADER_VERTEX)
  1388.                   ? "vertex shader input" : "fragment shader output";
  1389.                linker_error(prog,
  1390.                             "insufficient contiguous locations "
  1391.                             "available for %s `%s' %d %d %d", string,
  1392.                             var->name, used_locations, use_mask, attr);
  1393.                return false;
  1394.             }
  1395.  
  1396.             used_locations |= (use_mask << attr);
  1397.          }
  1398.  
  1399.          continue;
  1400.       }
  1401.  
  1402.       to_assign[num_attr].slots = slots;
  1403.       to_assign[num_attr].var = var;
  1404.       num_attr++;
  1405.    }
  1406.  
  1407.    /* If all of the attributes were assigned locations by the application (or
  1408.     * are built-in attributes with fixed locations), return early.  This should
  1409.     * be the common case.
  1410.     */
  1411.    if (num_attr == 0)
  1412.       return true;
  1413.  
  1414.    qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
  1415.  
  1416.    if (target_index == MESA_SHADER_VERTEX) {
  1417.       /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS.  It can
  1418.        * only be explicitly assigned by via glBindAttribLocation.  Mark it as
  1419.        * reserved to prevent it from being automatically allocated below.
  1420.        */
  1421.       find_deref_visitor find("gl_Vertex");
  1422.       find.run(sh->ir);
  1423.       if (find.variable_found())
  1424.          used_locations |= (1 << 0);
  1425.    }
  1426.  
  1427.    for (unsigned i = 0; i < num_attr; i++) {
  1428.       /* Mask representing the contiguous slots that will be used by this
  1429.        * attribute.
  1430.        */
  1431.       const unsigned use_mask = (1 << to_assign[i].slots) - 1;
  1432.  
  1433.       int location = find_available_slots(used_locations, to_assign[i].slots);
  1434.  
  1435.       if (location < 0) {
  1436.          const char *const string = (target_index == MESA_SHADER_VERTEX)
  1437.             ? "vertex shader input" : "fragment shader output";
  1438.  
  1439.          linker_error(prog,
  1440.                       "insufficient contiguous locations "
  1441.                       "available for %s `%s'",
  1442.                       string, to_assign[i].var->name);
  1443.          return false;
  1444.       }
  1445.  
  1446.       to_assign[i].var->location = generic_base + location;
  1447.       to_assign[i].var->is_unmatched_generic_inout = 0;
  1448.       used_locations |= (use_mask << location);
  1449.    }
  1450.  
  1451.    return true;
  1452. }
  1453.  
  1454.  
  1455. /**
  1456.  * Demote shader inputs and outputs that are not used in other stages
  1457.  */
  1458. void
  1459. demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
  1460. {
  1461.    foreach_list(node, sh->ir) {
  1462.       ir_variable *const var = ((ir_instruction *) node)->as_variable();
  1463.  
  1464.       if ((var == NULL) || (var->mode != int(mode)))
  1465.          continue;
  1466.  
  1467.       /* A shader 'in' or 'out' variable is only really an input or output if
  1468.        * its value is used by other shader stages.  This will cause the variable
  1469.        * to have a location assigned.
  1470.        */
  1471.       if (var->is_unmatched_generic_inout) {
  1472.          var->mode = ir_var_auto;
  1473.       }
  1474.    }
  1475. }
  1476.  
  1477.  
  1478. /**
  1479.  * Store the gl_FragDepth layout in the gl_shader_program struct.
  1480.  */
  1481. static void
  1482. store_fragdepth_layout(struct gl_shader_program *prog)
  1483. {
  1484.    if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
  1485.       return;
  1486.    }
  1487.  
  1488.    struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
  1489.  
  1490.    /* We don't look up the gl_FragDepth symbol directly because if
  1491.     * gl_FragDepth is not used in the shader, it's removed from the IR.
  1492.     * However, the symbol won't be removed from the symbol table.
  1493.     *
  1494.     * We're only interested in the cases where the variable is NOT removed
  1495.     * from the IR.
  1496.     */
  1497.    foreach_list(node, ir) {
  1498.       ir_variable *const var = ((ir_instruction *) node)->as_variable();
  1499.  
  1500.       if (var == NULL || var->mode != ir_var_shader_out) {
  1501.          continue;
  1502.       }
  1503.  
  1504.       if (strcmp(var->name, "gl_FragDepth") == 0) {
  1505.          switch (var->depth_layout) {
  1506.          case ir_depth_layout_none:
  1507.             prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
  1508.             return;
  1509.          case ir_depth_layout_any:
  1510.             prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
  1511.             return;
  1512.          case ir_depth_layout_greater:
  1513.             prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
  1514.             return;
  1515.          case ir_depth_layout_less:
  1516.             prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
  1517.             return;
  1518.          case ir_depth_layout_unchanged:
  1519.             prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
  1520.             return;
  1521.          default:
  1522.             assert(0);
  1523.             return;
  1524.          }
  1525.       }
  1526.    }
  1527. }
  1528.  
  1529. /**
  1530.  * Validate the resources used by a program versus the implementation limits
  1531.  */
  1532. static bool
  1533. check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
  1534. {
  1535.    static const char *const shader_names[MESA_SHADER_TYPES] = {
  1536.       "vertex", "geometry", "fragment"
  1537.    };
  1538.  
  1539.    const unsigned max_samplers[MESA_SHADER_TYPES] = {
  1540.       ctx->Const.VertexProgram.MaxTextureImageUnits,
  1541.       ctx->Const.GeometryProgram.MaxTextureImageUnits,
  1542.       ctx->Const.FragmentProgram.MaxTextureImageUnits
  1543.    };
  1544.  
  1545.    const unsigned max_default_uniform_components[MESA_SHADER_TYPES] = {
  1546.       ctx->Const.VertexProgram.MaxUniformComponents,
  1547.       ctx->Const.GeometryProgram.MaxUniformComponents,
  1548.       ctx->Const.FragmentProgram.MaxUniformComponents
  1549.    };
  1550.  
  1551.    const unsigned max_combined_uniform_components[MESA_SHADER_TYPES] = {
  1552.       ctx->Const.VertexProgram.MaxCombinedUniformComponents,
  1553.       ctx->Const.GeometryProgram.MaxCombinedUniformComponents,
  1554.       ctx->Const.FragmentProgram.MaxCombinedUniformComponents
  1555.    };
  1556.  
  1557.    const unsigned max_uniform_blocks[MESA_SHADER_TYPES] = {
  1558.       ctx->Const.VertexProgram.MaxUniformBlocks,
  1559.       ctx->Const.GeometryProgram.MaxUniformBlocks,
  1560.       ctx->Const.FragmentProgram.MaxUniformBlocks
  1561.    };
  1562.  
  1563.    for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
  1564.       struct gl_shader *sh = prog->_LinkedShaders[i];
  1565.  
  1566.       if (sh == NULL)
  1567.          continue;
  1568.  
  1569.       if (sh->num_samplers > max_samplers[i]) {
  1570.          linker_error(prog, "Too many %s shader texture samplers",
  1571.                       shader_names[i]);
  1572.       }
  1573.  
  1574.       if (sh->num_uniform_components > max_default_uniform_components[i]) {
  1575.          if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
  1576.             linker_warning(prog, "Too many %s shader default uniform block "
  1577.                            "components, but the driver will try to optimize "
  1578.                            "them out; this is non-portable out-of-spec "
  1579.                            "behavior\n",
  1580.                            shader_names[i]);
  1581.          } else {
  1582.             linker_error(prog, "Too many %s shader default uniform block "
  1583.                          "components",
  1584.                          shader_names[i]);
  1585.          }
  1586.       }
  1587.  
  1588.       if (sh->num_combined_uniform_components >
  1589.           max_combined_uniform_components[i]) {
  1590.          if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
  1591.             linker_warning(prog, "Too many %s shader uniform components, "
  1592.                            "but the driver will try to optimize them out; "
  1593.                            "this is non-portable out-of-spec behavior\n",
  1594.                            shader_names[i]);
  1595.          } else {
  1596.             linker_error(prog, "Too many %s shader uniform components",
  1597.                          shader_names[i]);
  1598.          }
  1599.       }
  1600.    }
  1601.  
  1602.    unsigned blocks[MESA_SHADER_TYPES] = {0};
  1603.    unsigned total_uniform_blocks = 0;
  1604.  
  1605.    for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
  1606.       for (unsigned j = 0; j < MESA_SHADER_TYPES; j++) {
  1607.          if (prog->UniformBlockStageIndex[j][i] != -1) {
  1608.             blocks[j]++;
  1609.             total_uniform_blocks++;
  1610.          }
  1611.       }
  1612.  
  1613.       if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
  1614.          linker_error(prog, "Too many combined uniform blocks (%d/%d)",
  1615.                       prog->NumUniformBlocks,
  1616.                       ctx->Const.MaxCombinedUniformBlocks);
  1617.       } else {
  1618.          for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
  1619.             if (blocks[i] > max_uniform_blocks[i]) {
  1620.                linker_error(prog, "Too many %s uniform blocks (%d/%d)",
  1621.                             shader_names[i],
  1622.                             blocks[i],
  1623.                             max_uniform_blocks[i]);
  1624.                break;
  1625.             }
  1626.          }
  1627.       }
  1628.    }
  1629.  
  1630.    return prog->LinkStatus;
  1631. }
  1632.  
  1633. void
  1634. link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
  1635. {
  1636.    tfeedback_decl *tfeedback_decls = NULL;
  1637.    unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
  1638.  
  1639.    void *mem_ctx = ralloc_context(NULL); // temporary linker context
  1640.  
  1641.    prog->LinkStatus = false;
  1642.    prog->Validated = false;
  1643.    prog->_Used = false;
  1644.  
  1645.    ralloc_free(prog->InfoLog);
  1646.    prog->InfoLog = ralloc_strdup(NULL, "");
  1647.  
  1648.    ralloc_free(prog->UniformBlocks);
  1649.    prog->UniformBlocks = NULL;
  1650.    prog->NumUniformBlocks = 0;
  1651.    for (int i = 0; i < MESA_SHADER_TYPES; i++) {
  1652.       ralloc_free(prog->UniformBlockStageIndex[i]);
  1653.       prog->UniformBlockStageIndex[i] = NULL;
  1654.    }
  1655.  
  1656.    /* Separate the shaders into groups based on their type.
  1657.     */
  1658.    struct gl_shader **vert_shader_list;
  1659.    unsigned num_vert_shaders = 0;
  1660.    struct gl_shader **frag_shader_list;
  1661.    unsigned num_frag_shaders = 0;
  1662.  
  1663.    vert_shader_list = (struct gl_shader **)
  1664.       calloc(2 * prog->NumShaders, sizeof(struct gl_shader *));
  1665.    frag_shader_list =  &vert_shader_list[prog->NumShaders];
  1666.  
  1667.    unsigned min_version = UINT_MAX;
  1668.    unsigned max_version = 0;
  1669.    const bool is_es_prog =
  1670.       (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
  1671.    for (unsigned i = 0; i < prog->NumShaders; i++) {
  1672.       min_version = MIN2(min_version, prog->Shaders[i]->Version);
  1673.       max_version = MAX2(max_version, prog->Shaders[i]->Version);
  1674.  
  1675.       if (prog->Shaders[i]->IsES != is_es_prog) {
  1676.          linker_error(prog, "all shaders must use same shading "
  1677.                       "language version\n");
  1678.          goto done;
  1679.       }
  1680.  
  1681.       switch (prog->Shaders[i]->Type) {
  1682.       case GL_VERTEX_SHADER:
  1683.          vert_shader_list[num_vert_shaders] = prog->Shaders[i];
  1684.          num_vert_shaders++;
  1685.          break;
  1686.       case GL_FRAGMENT_SHADER:
  1687.          frag_shader_list[num_frag_shaders] = prog->Shaders[i];
  1688.          num_frag_shaders++;
  1689.          break;
  1690.       case GL_GEOMETRY_SHADER:
  1691.          /* FINISHME: Support geometry shaders. */
  1692.          assert(prog->Shaders[i]->Type != GL_GEOMETRY_SHADER);
  1693.          break;
  1694.       }
  1695.    }
  1696.  
  1697.    /* Previous to GLSL version 1.30, different compilation units could mix and
  1698.     * match shading language versions.  With GLSL 1.30 and later, the versions
  1699.     * of all shaders must match.
  1700.     *
  1701.     * GLSL ES has never allowed mixing of shading language versions.
  1702.     */
  1703.    if ((is_es_prog || max_version >= 130)
  1704.        && min_version != max_version) {
  1705.       linker_error(prog, "all shaders must use same shading "
  1706.                    "language version\n");
  1707.       goto done;
  1708.    }
  1709.  
  1710.    prog->Version = max_version;
  1711.    prog->IsES = is_es_prog;
  1712.  
  1713.    for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
  1714.       if (prog->_LinkedShaders[i] != NULL)
  1715.          ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
  1716.  
  1717.       prog->_LinkedShaders[i] = NULL;
  1718.    }
  1719.  
  1720.    /* Link all shaders for a particular stage and validate the result.
  1721.     */
  1722.    if (num_vert_shaders > 0) {
  1723.       gl_shader *const sh =
  1724.          link_intrastage_shaders(mem_ctx, ctx, prog, vert_shader_list,
  1725.                                  num_vert_shaders);
  1726.  
  1727.       if (sh == NULL)
  1728.          goto done;
  1729.  
  1730.       if (!validate_vertex_shader_executable(prog, sh))
  1731.          goto done;
  1732.  
  1733.       _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_VERTEX],
  1734.                              sh);
  1735.    }
  1736.  
  1737.    if (num_frag_shaders > 0) {
  1738.       gl_shader *const sh =
  1739.          link_intrastage_shaders(mem_ctx, ctx, prog, frag_shader_list,
  1740.                                  num_frag_shaders);
  1741.  
  1742.       if (sh == NULL)
  1743.          goto done;
  1744.  
  1745.       if (!validate_fragment_shader_executable(prog, sh))
  1746.          goto done;
  1747.  
  1748.       _mesa_reference_shader(ctx, &prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
  1749.                              sh);
  1750.    }
  1751.  
  1752.    /* Here begins the inter-stage linking phase.  Some initial validation is
  1753.     * performed, then locations are assigned for uniforms, attributes, and
  1754.     * varyings.
  1755.     */
  1756.    if (cross_validate_uniforms(prog)) {
  1757.       unsigned prev;
  1758.  
  1759.       for (prev = 0; prev < MESA_SHADER_TYPES; prev++) {
  1760.          if (prog->_LinkedShaders[prev] != NULL)
  1761.             break;
  1762.       }
  1763.  
  1764.       /* Validate the inputs of each stage with the output of the preceding
  1765.        * stage.
  1766.        */
  1767.       for (unsigned i = prev + 1; i < MESA_SHADER_TYPES; i++) {
  1768.          if (prog->_LinkedShaders[i] == NULL)
  1769.             continue;
  1770.  
  1771.          if (!validate_interstage_interface_blocks(prog->_LinkedShaders[prev],
  1772.                                                    prog->_LinkedShaders[i])) {
  1773.             linker_error(prog, "interface block mismatch between shader stages\n");
  1774.             goto done;
  1775.          }
  1776.  
  1777.          if (!cross_validate_outputs_to_inputs(prog,
  1778.                                                prog->_LinkedShaders[prev],
  1779.                                                prog->_LinkedShaders[i]))
  1780.             goto done;
  1781.  
  1782.          prev = i;
  1783.       }
  1784.  
  1785.       prog->LinkStatus = true;
  1786.    }
  1787.  
  1788.  
  1789.    for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
  1790.       if (prog->_LinkedShaders[i] != NULL)
  1791.          lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]);
  1792.    }
  1793.  
  1794.    /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
  1795.     * it before optimization because we want most of the checks to get
  1796.     * dropped thanks to constant propagation.
  1797.     *
  1798.     * This rule also applies to GLSL ES 3.00.
  1799.     */
  1800.    if (max_version >= (is_es_prog ? 300 : 130)) {
  1801.       struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
  1802.       if (sh) {
  1803.          lower_discard_flow(sh->ir);
  1804.       }
  1805.    }
  1806.  
  1807.    if (!interstage_cross_validate_uniform_blocks(prog))
  1808.       goto done;
  1809.  
  1810.    /* Do common optimization before assigning storage for attributes,
  1811.     * uniforms, and varyings.  Later optimization could possibly make
  1812.     * some of that unused.
  1813.     */
  1814.    for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
  1815.       if (prog->_LinkedShaders[i] == NULL)
  1816.          continue;
  1817.  
  1818.       detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
  1819.       if (!prog->LinkStatus)
  1820.          goto done;
  1821.  
  1822.       if (ctx->ShaderCompilerOptions[i].LowerClipDistance) {
  1823.          lower_clip_distance(prog->_LinkedShaders[i]);
  1824.       }
  1825.  
  1826.       unsigned max_unroll = ctx->ShaderCompilerOptions[i].MaxUnrollIterations;
  1827.  
  1828.       while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, max_unroll, &ctx->ShaderCompilerOptions[i]))
  1829.          ;
  1830.    }
  1831.  
  1832.    /* Mark all generic shader inputs and outputs as unpaired. */
  1833.    if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
  1834.       link_invalidate_variable_locations(
  1835.             prog->_LinkedShaders[MESA_SHADER_VERTEX],
  1836.             VERT_ATTRIB_GENERIC0, VARYING_SLOT_VAR0);
  1837.    }
  1838.    /* FINISHME: Geometry shaders not implemented yet */
  1839.    if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
  1840.       link_invalidate_variable_locations(
  1841.             prog->_LinkedShaders[MESA_SHADER_FRAGMENT],
  1842.             VARYING_SLOT_VAR0, FRAG_RESULT_DATA0);
  1843.    }
  1844.  
  1845.    /* FINISHME: The value of the max_attribute_index parameter is
  1846.     * FINISHME: implementation dependent based on the value of
  1847.     * FINISHME: GL_MAX_VERTEX_ATTRIBS.  GL_MAX_VERTEX_ATTRIBS must be
  1848.     * FINISHME: at least 16, so hardcode 16 for now.
  1849.     */
  1850.    if (!assign_attribute_or_color_locations(prog, MESA_SHADER_VERTEX, 16)) {
  1851.       goto done;
  1852.    }
  1853.  
  1854.    if (!assign_attribute_or_color_locations(prog, MESA_SHADER_FRAGMENT, MAX2(ctx->Const.MaxDrawBuffers, ctx->Const.MaxDualSourceDrawBuffers))) {
  1855.       goto done;
  1856.    }
  1857.  
  1858.    unsigned first;
  1859.    for (first = 0; first < MESA_SHADER_TYPES; first++) {
  1860.       if (prog->_LinkedShaders[first] != NULL)
  1861.          break;
  1862.    }
  1863.  
  1864.    if (num_tfeedback_decls != 0) {
  1865.       /* From GL_EXT_transform_feedback:
  1866.        *   A program will fail to link if:
  1867.        *
  1868.        *   * the <count> specified by TransformFeedbackVaryingsEXT is
  1869.        *     non-zero, but the program object has no vertex or geometry
  1870.        *     shader;
  1871.        */
  1872.       if (first >= MESA_SHADER_FRAGMENT) {
  1873.          linker_error(prog, "Transform feedback varyings specified, but "
  1874.                       "no vertex or geometry shader is present.");
  1875.          goto done;
  1876.       }
  1877.  
  1878.       tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
  1879.                                      prog->TransformFeedback.NumVarying);
  1880.       if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
  1881.                                  prog->TransformFeedback.VaryingNames,
  1882.                                  tfeedback_decls))
  1883.          goto done;
  1884.    }
  1885.  
  1886.    /* Linking the stages in the opposite order (from fragment to vertex)
  1887.     * ensures that inter-shader outputs written to in an earlier stage are
  1888.     * eliminated if they are (transitively) not used in a later stage.
  1889.     */
  1890.    int last, next;
  1891.    for (last = MESA_SHADER_TYPES-1; last >= 0; last--) {
  1892.       if (prog->_LinkedShaders[last] != NULL)
  1893.          break;
  1894.    }
  1895.  
  1896.    if (last >= 0 && last < MESA_SHADER_FRAGMENT) {
  1897.       gl_shader *const sh = prog->_LinkedShaders[last];
  1898.  
  1899.       if (num_tfeedback_decls != 0) {
  1900.          /* There was no fragment shader, but we still have to assign varying
  1901.           * locations for use by transform feedback.
  1902.           */
  1903.          if (!assign_varying_locations(ctx, mem_ctx, prog,
  1904.                                        sh, NULL,
  1905.                                        num_tfeedback_decls, tfeedback_decls))
  1906.             goto done;
  1907.       }
  1908.  
  1909.       do_dead_builtin_varyings(ctx, sh, NULL,
  1910.                                num_tfeedback_decls, tfeedback_decls);
  1911.  
  1912.       demote_shader_inputs_and_outputs(sh, ir_var_shader_out);
  1913.  
  1914.       /* Eliminate code that is now dead due to unused outputs being demoted.
  1915.        */
  1916.       while (do_dead_code(sh->ir, false))
  1917.          ;
  1918.    }
  1919.    else if (first == MESA_SHADER_FRAGMENT) {
  1920.       /* If the program only contains a fragment shader...
  1921.        */
  1922.       gl_shader *const sh = prog->_LinkedShaders[first];
  1923.  
  1924.       do_dead_builtin_varyings(ctx, NULL, sh,
  1925.                                num_tfeedback_decls, tfeedback_decls);
  1926.  
  1927.       demote_shader_inputs_and_outputs(sh, ir_var_shader_in);
  1928.  
  1929.       while (do_dead_code(sh->ir, false))
  1930.          ;
  1931.    }
  1932.  
  1933.    next = last;
  1934.    for (int i = next - 1; i >= 0; i--) {
  1935.       if (prog->_LinkedShaders[i] == NULL)
  1936.          continue;
  1937.  
  1938.       gl_shader *const sh_i = prog->_LinkedShaders[i];
  1939.       gl_shader *const sh_next = prog->_LinkedShaders[next];
  1940.  
  1941.       if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next,
  1942.                 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
  1943.                 tfeedback_decls))
  1944.          goto done;
  1945.  
  1946.       do_dead_builtin_varyings(ctx, sh_i, sh_next,
  1947.                 next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
  1948.                 tfeedback_decls);
  1949.  
  1950.       demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out);
  1951.       demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in);
  1952.  
  1953.       /* Eliminate code that is now dead due to unused outputs being demoted.
  1954.        */
  1955.       while (do_dead_code(sh_i->ir, false))
  1956.          ;
  1957.       while (do_dead_code(sh_next->ir, false))
  1958.          ;
  1959.  
  1960.       /* This must be done after all dead varyings are eliminated. */
  1961.       if (!check_against_varying_limit(ctx, prog, sh_next))
  1962.          goto done;
  1963.  
  1964.       next = i;
  1965.    }
  1966.  
  1967.    if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
  1968.       goto done;
  1969.  
  1970.    update_array_sizes(prog);
  1971.    link_assign_uniform_locations(prog);
  1972.    store_fragdepth_layout(prog);
  1973.  
  1974.    if (!check_resources(ctx, prog))
  1975.       goto done;
  1976.  
  1977.    /* OpenGL ES requires that a vertex shader and a fragment shader both be
  1978.     * present in a linked program.  By checking prog->IsES, we also
  1979.     * catch the GL_ARB_ES2_compatibility case.
  1980.     */
  1981.    if (!prog->InternalSeparateShader &&
  1982.        (ctx->API == API_OPENGLES2 || prog->IsES)) {
  1983.       if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
  1984.          linker_error(prog, "program lacks a vertex shader\n");
  1985.       } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
  1986.          linker_error(prog, "program lacks a fragment shader\n");
  1987.       }
  1988.    }
  1989.  
  1990.    /* FINISHME: Assign fragment shader output locations. */
  1991.  
  1992. done:
  1993.    free(vert_shader_list);
  1994.  
  1995.    for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
  1996.       if (prog->_LinkedShaders[i] == NULL)
  1997.          continue;
  1998.  
  1999.       /* Retain any live IR, but trash the rest. */
  2000.       reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
  2001.  
  2002.       /* The symbol table in the linked shaders may contain references to
  2003.        * variables that were removed (e.g., unused uniforms).  Since it may
  2004.        * contain junk, there is no possible valid use.  Delete it and set the
  2005.        * pointer to NULL.
  2006.        */
  2007.       delete prog->_LinkedShaders[i]->symbols;
  2008.       prog->_LinkedShaders[i]->symbols = NULL;
  2009.    }
  2010.  
  2011.    ralloc_free(mem_ctx);
  2012. }
  2013.