Home | History | Annotate | Download | only in glsl
      1 /*
      2  * Copyright  2011 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 #include "main/core.h"
     25 #include "ir.h"
     26 #include "linker.h"
     27 #include "ir_uniform.h"
     28 #include "glsl_symbol_table.h"
     29 #include "program.h"
     30 #include "string_to_uint_map.h"
     31 #include "ir_array_refcount.h"
     32 
     33 /**
     34  * \file link_uniforms.cpp
     35  * Assign locations for GLSL uniforms.
     36  *
     37  * \author Ian Romanick <ian.d.romanick (at) intel.com>
     38  */
     39 
     40 /**
     41  * Used by linker to indicate uniforms that have no location set.
     42  */
     43 #define UNMAPPED_UNIFORM_LOC ~0u
     44 
     45 void
     46 program_resource_visitor::process(const glsl_type *type, const char *name,
     47                                   bool use_std430_as_default)
     48 {
     49    assert(type->without_array()->is_record()
     50           || type->without_array()->is_interface());
     51 
     52    unsigned record_array_count = 1;
     53    char *name_copy = ralloc_strdup(NULL, name);
     54 
     55    enum glsl_interface_packing packing =
     56       type->get_internal_ifc_packing(use_std430_as_default);
     57 
     58    recursion(type, &name_copy, strlen(name), false, NULL, packing, false,
     59              record_array_count, NULL);
     60    ralloc_free(name_copy);
     61 }
     62 
     63 void
     64 program_resource_visitor::process(ir_variable *var, bool use_std430_as_default)
     65 {
     66    unsigned record_array_count = 1;
     67    const bool row_major =
     68       var->data.matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR;
     69 
     70    enum glsl_interface_packing packing = var->get_interface_type() ?
     71       var->get_interface_type()->
     72          get_internal_ifc_packing(use_std430_as_default) :
     73       var->type->get_internal_ifc_packing(use_std430_as_default);
     74 
     75    const glsl_type *t =
     76       var->data.from_named_ifc_block ? var->get_interface_type() : var->type;
     77    const glsl_type *t_without_array = t->without_array();
     78 
     79    /* false is always passed for the row_major parameter to the other
     80     * processing functions because no information is available to do
     81     * otherwise.  See the warning in linker.h.
     82     */
     83    if (t_without_array->is_record() ||
     84               (t->is_array() && t->fields.array->is_array())) {
     85       char *name = ralloc_strdup(NULL, var->name);
     86       recursion(var->type, &name, strlen(name), row_major, NULL, packing,
     87                 false, record_array_count, NULL);
     88       ralloc_free(name);
     89    } else if (t_without_array->is_interface()) {
     90       char *name = ralloc_strdup(NULL, t_without_array->name);
     91       const glsl_struct_field *ifc_member = var->data.from_named_ifc_block ?
     92          &t_without_array->
     93             fields.structure[t_without_array->field_index(var->name)] : NULL;
     94 
     95       recursion(t, &name, strlen(name), row_major, NULL, packing,
     96                 false, record_array_count, ifc_member);
     97       ralloc_free(name);
     98    } else {
     99       this->set_record_array_count(record_array_count);
    100       this->visit_field(t, var->name, row_major, NULL, packing, false);
    101    }
    102 }
    103 
    104 void
    105 program_resource_visitor::recursion(const glsl_type *t, char **name,
    106                                     size_t name_length, bool row_major,
    107                                     const glsl_type *record_type,
    108                                     const enum glsl_interface_packing packing,
    109                                     bool last_field,
    110                                     unsigned record_array_count,
    111                                     const glsl_struct_field *named_ifc_member)
    112 {
    113    /* Records need to have each field processed individually.
    114     *
    115     * Arrays of records need to have each array element processed
    116     * individually, then each field of the resulting array elements processed
    117     * individually.
    118     */
    119    if (t->is_interface() && named_ifc_member) {
    120       ralloc_asprintf_rewrite_tail(name, &name_length, ".%s",
    121                                    named_ifc_member->name);
    122       recursion(named_ifc_member->type, name, name_length, row_major, NULL,
    123                 packing, false, record_array_count, NULL);
    124    } else if (t->is_record() || t->is_interface()) {
    125       if (record_type == NULL && t->is_record())
    126          record_type = t;
    127 
    128       if (t->is_record())
    129          this->enter_record(t, *name, row_major, packing);
    130 
    131       for (unsigned i = 0; i < t->length; i++) {
    132          const char *field = t->fields.structure[i].name;
    133          size_t new_length = name_length;
    134 
    135          if (t->is_interface() && t->fields.structure[i].offset != -1)
    136             this->set_buffer_offset(t->fields.structure[i].offset);
    137 
    138          /* Append '.field' to the current variable name. */
    139          if (name_length == 0) {
    140             ralloc_asprintf_rewrite_tail(name, &new_length, "%s", field);
    141          } else {
    142             ralloc_asprintf_rewrite_tail(name, &new_length, ".%s", field);
    143          }
    144 
    145          /* The layout of structures at the top level of the block is set
    146           * during parsing.  For matrices contained in multiple levels of
    147           * structures in the block, the inner structures have no layout.
    148           * These cases must potentially inherit the layout from the outer
    149           * levels.
    150           */
    151          bool field_row_major = row_major;
    152          const enum glsl_matrix_layout matrix_layout =
    153             glsl_matrix_layout(t->fields.structure[i].matrix_layout);
    154          if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
    155             field_row_major = true;
    156          } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
    157             field_row_major = false;
    158          }
    159 
    160          recursion(t->fields.structure[i].type, name, new_length,
    161                    field_row_major,
    162                    record_type,
    163                    packing,
    164                    (i + 1) == t->length, record_array_count, NULL);
    165 
    166          /* Only the first leaf-field of the record gets called with the
    167           * record type pointer.
    168           */
    169          record_type = NULL;
    170       }
    171 
    172       if (t->is_record()) {
    173          (*name)[name_length] = '\0';
    174          this->leave_record(t, *name, row_major, packing);
    175       }
    176    } else if (t->without_array()->is_record() ||
    177               t->without_array()->is_interface() ||
    178               (t->is_array() && t->fields.array->is_array())) {
    179       if (record_type == NULL && t->fields.array->is_record())
    180          record_type = t->fields.array;
    181 
    182       unsigned length = t->length;
    183 
    184       /* Shader storage block unsized arrays: add subscript [0] to variable
    185        * names.
    186        */
    187       if (t->is_unsized_array())
    188          length = 1;
    189 
    190       record_array_count *= length;
    191 
    192       for (unsigned i = 0; i < length; i++) {
    193          size_t new_length = name_length;
    194 
    195          /* Append the subscript to the current variable name */
    196          ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
    197 
    198          recursion(t->fields.array, name, new_length, row_major,
    199                    record_type,
    200                    packing,
    201                    (i + 1) == t->length, record_array_count,
    202                    named_ifc_member);
    203 
    204          /* Only the first leaf-field of the record gets called with the
    205           * record type pointer.
    206           */
    207          record_type = NULL;
    208       }
    209    } else {
    210       this->set_record_array_count(record_array_count);
    211       this->visit_field(t, *name, row_major, record_type, packing, last_field);
    212    }
    213 }
    214 
    215 void
    216 program_resource_visitor::enter_record(const glsl_type *, const char *, bool,
    217                                        const enum glsl_interface_packing)
    218 {
    219 }
    220 
    221 void
    222 program_resource_visitor::leave_record(const glsl_type *, const char *, bool,
    223                                        const enum glsl_interface_packing)
    224 {
    225 }
    226 
    227 void
    228 program_resource_visitor::set_buffer_offset(unsigned)
    229 {
    230 }
    231 
    232 void
    233 program_resource_visitor::set_record_array_count(unsigned)
    234 {
    235 }
    236 
    237 namespace {
    238 
    239 /**
    240  * Class to help calculate the storage requirements for a set of uniforms
    241  *
    242  * As uniforms are added to the active set the number of active uniforms and
    243  * the storage requirements for those uniforms are accumulated.  The active
    244  * uniforms are added to the hash table supplied to the constructor.
    245  *
    246  * If the same uniform is added multiple times (i.e., once for each shader
    247  * target), it will only be accounted once.
    248  */
    249 class count_uniform_size : public program_resource_visitor {
    250 public:
    251    count_uniform_size(struct string_to_uint_map *map,
    252                       struct string_to_uint_map *hidden_map,
    253                       bool use_std430_as_default)
    254       : num_active_uniforms(0), num_hidden_uniforms(0), num_values(0),
    255         num_shader_samplers(0), num_shader_images(0),
    256         num_shader_uniform_components(0), num_shader_subroutines(0),
    257         is_buffer_block(false), is_shader_storage(false), map(map),
    258         hidden_map(hidden_map), current_var(NULL),
    259         use_std430_as_default(use_std430_as_default)
    260    {
    261       /* empty */
    262    }
    263 
    264    void start_shader()
    265    {
    266       this->num_shader_samplers = 0;
    267       this->num_shader_images = 0;
    268       this->num_shader_uniform_components = 0;
    269       this->num_shader_subroutines = 0;
    270    }
    271 
    272    void process(ir_variable *var)
    273    {
    274       this->current_var = var;
    275       this->is_buffer_block = var->is_in_buffer_block();
    276       this->is_shader_storage = var->is_in_shader_storage_block();
    277       if (var->is_interface_instance())
    278          program_resource_visitor::process(var->get_interface_type(),
    279                                            var->get_interface_type()->name,
    280                                            use_std430_as_default);
    281       else
    282          program_resource_visitor::process(var, use_std430_as_default);
    283    }
    284 
    285    /**
    286     * Total number of active uniforms counted
    287     */
    288    unsigned num_active_uniforms;
    289 
    290    unsigned num_hidden_uniforms;
    291 
    292    /**
    293     * Number of data values required to back the storage for the active uniforms
    294     */
    295    unsigned num_values;
    296 
    297    /**
    298     * Number of samplers used
    299     */
    300    unsigned num_shader_samplers;
    301 
    302    /**
    303     * Number of images used
    304     */
    305    unsigned num_shader_images;
    306 
    307    /**
    308     * Number of uniforms used in the current shader
    309     */
    310    unsigned num_shader_uniform_components;
    311 
    312    /**
    313     * Number of subroutine uniforms used
    314     */
    315    unsigned num_shader_subroutines;
    316 
    317    bool is_buffer_block;
    318    bool is_shader_storage;
    319 
    320    struct string_to_uint_map *map;
    321 
    322 private:
    323    virtual void visit_field(const glsl_type *type, const char *name,
    324                             bool /* row_major */,
    325                             const glsl_type * /* record_type */,
    326                             const enum glsl_interface_packing,
    327                             bool /* last_field */)
    328    {
    329       assert(!type->without_array()->is_record());
    330       assert(!type->without_array()->is_interface());
    331       assert(!(type->is_array() && type->fields.array->is_array()));
    332 
    333       /* Count the number of samplers regardless of whether the uniform is
    334        * already in the hash table.  The hash table prevents adding the same
    335        * uniform for multiple shader targets, but in this case we want to
    336        * count it for each shader target.
    337        */
    338       const unsigned values = type->component_slots();
    339       if (type->contains_subroutine()) {
    340          this->num_shader_subroutines += values;
    341       } else if (type->contains_sampler() && !current_var->data.bindless) {
    342          /* Samplers (bound or bindless) are counted as two components as
    343           * specified by ARB_bindless_texture. */
    344          this->num_shader_samplers += values / 2;
    345       } else if (type->contains_image() && !current_var->data.bindless) {
    346          /* Images (bound or bindless) are counted as two components as
    347           * specified by ARB_bindless_texture. */
    348          this->num_shader_images += values / 2;
    349 
    350          /* As drivers are likely to represent image uniforms as
    351           * scalar indices, count them against the limit of uniform
    352           * components in the default block.  The spec allows image
    353           * uniforms to use up no more than one scalar slot.
    354           */
    355          if (!is_shader_storage)
    356             this->num_shader_uniform_components += values;
    357       } else {
    358          /* Accumulate the total number of uniform slots used by this shader.
    359           * Note that samplers do not count against this limit because they
    360           * don't use any storage on current hardware.
    361           */
    362          if (!is_buffer_block)
    363             this->num_shader_uniform_components += values;
    364       }
    365 
    366       /* If the uniform is already in the map, there's nothing more to do.
    367        */
    368       unsigned id;
    369       if (this->map->get(id, name))
    370          return;
    371 
    372       if (this->current_var->data.how_declared == ir_var_hidden) {
    373          this->hidden_map->put(this->num_hidden_uniforms, name);
    374          this->num_hidden_uniforms++;
    375       } else {
    376          this->map->put(this->num_active_uniforms-this->num_hidden_uniforms,
    377                         name);
    378       }
    379 
    380       /* Each leaf uniform occupies one entry in the list of active
    381        * uniforms.
    382        */
    383       this->num_active_uniforms++;
    384 
    385       if(!is_gl_identifier(name) && !is_shader_storage && !is_buffer_block)
    386          this->num_values += values;
    387    }
    388 
    389    struct string_to_uint_map *hidden_map;
    390 
    391    /**
    392     * Current variable being processed.
    393     */
    394    ir_variable *current_var;
    395 
    396    bool use_std430_as_default;
    397 };
    398 
    399 } /* anonymous namespace */
    400 
    401 unsigned
    402 link_calculate_matrix_stride(const glsl_type *matrix, bool row_major,
    403                              enum glsl_interface_packing packing)
    404 {
    405    const unsigned N = matrix->is_double() ? 8 : 4;
    406    const unsigned items =
    407       row_major ? matrix->matrix_columns : matrix->vector_elements;
    408 
    409    assert(items <= 4);
    410 
    411    /* Matrix stride for std430 mat2xY matrices are not rounded up to
    412     * vec4 size.
    413     *
    414     * Section 7.6.2.2 "Standard Uniform Block Layout" of the OpenGL 4.3 spec
    415     * says:
    416     *
    417     *    2. If the member is a two- or four-component vector with components
    418     *       consuming N basic machine units, the base alignment is 2N or 4N,
    419     *       respectively.
    420     *    ...
    421     *    4. If the member is an array of scalars or vectors, the base
    422     *       alignment and array stride are set to match the base alignment of
    423     *       a single array element, according to rules (1), (2), and (3), and
    424     *       rounded up to the base alignment of a vec4.
    425     *    ...
    426     *    7. If the member is a row-major matrix with C columns and R rows, the
    427     *       matrix is stored identically to an array of R row vectors with C
    428     *       components each, according to rule (4).
    429     *    ...
    430     *
    431     *    When using the std430 storage layout, shader storage blocks will be
    432     *    laid out in buffer storage identically to uniform and shader storage
    433     *    blocks using the std140 layout, except that the base alignment and
    434     *    stride of arrays of scalars and vectors in rule 4 and of structures
    435     *    in rule 9 are not rounded up a multiple of the base alignment of a
    436     *    vec4.
    437     */
    438    return packing == GLSL_INTERFACE_PACKING_STD430
    439       ? (items < 3 ? items * N : glsl_align(items * N, 16))
    440       : glsl_align(items * N, 16);
    441 }
    442 
    443 /**
    444  * Class to help parcel out pieces of backing storage to uniforms
    445  *
    446  * Each uniform processed has some range of the \c gl_constant_value
    447  * structures associated with it.  The association is done by finding
    448  * the uniform in the \c string_to_uint_map and using the value from
    449  * the map to connect that slot in the \c gl_uniform_storage table
    450  * with the next available slot in the \c gl_constant_value array.
    451  *
    452  * \warning
    453  * This class assumes that every uniform that will be processed is
    454  * already in the \c string_to_uint_map.  In addition, it assumes that
    455  * the \c gl_uniform_storage and \c gl_constant_value arrays are "big
    456  * enough."
    457  */
    458 class parcel_out_uniform_storage : public program_resource_visitor {
    459 public:
    460    parcel_out_uniform_storage(struct gl_shader_program *prog,
    461                               struct string_to_uint_map *map,
    462                               struct gl_uniform_storage *uniforms,
    463                               union gl_constant_value *values,
    464                               bool use_std430_as_default)
    465       : prog(prog), map(map), uniforms(uniforms),
    466         use_std430_as_default(use_std430_as_default), values(values),
    467         bindless_targets(NULL), bindless_access(NULL)
    468    {
    469    }
    470 
    471    virtual ~parcel_out_uniform_storage()
    472    {
    473       free(this->bindless_targets);
    474       free(this->bindless_access);
    475    }
    476 
    477    void start_shader(gl_shader_stage shader_type)
    478    {
    479       assert(shader_type < MESA_SHADER_STAGES);
    480       this->shader_type = shader_type;
    481 
    482       this->shader_samplers_used = 0;
    483       this->shader_shadow_samplers = 0;
    484       this->next_sampler = 0;
    485       this->next_image = 0;
    486       this->next_subroutine = 0;
    487       this->record_array_count = 1;
    488       memset(this->targets, 0, sizeof(this->targets));
    489 
    490       this->num_bindless_samplers = 0;
    491       this->next_bindless_sampler = 0;
    492       free(this->bindless_targets);
    493       this->bindless_targets = NULL;
    494 
    495       this->num_bindless_images = 0;
    496       this->next_bindless_image = 0;
    497       free(this->bindless_access);
    498       this->bindless_access = NULL;
    499    }
    500 
    501    void set_and_process(ir_variable *var)
    502    {
    503       current_var = var;
    504       field_counter = 0;
    505       this->record_next_sampler = new string_to_uint_map;
    506       this->record_next_bindless_sampler = new string_to_uint_map;
    507       this->record_next_image = new string_to_uint_map;
    508       this->record_next_bindless_image = new string_to_uint_map;
    509 
    510       buffer_block_index = -1;
    511       if (var->is_in_buffer_block()) {
    512          struct gl_uniform_block *blks = var->is_in_shader_storage_block() ?
    513             prog->data->ShaderStorageBlocks : prog->data->UniformBlocks;
    514          unsigned num_blks = var->is_in_shader_storage_block() ?
    515             prog->data->NumShaderStorageBlocks : prog->data->NumUniformBlocks;
    516 
    517          if (var->is_interface_instance() && var->type->is_array()) {
    518             unsigned l = strlen(var->get_interface_type()->name);
    519 
    520             for (unsigned i = 0; i < num_blks; i++) {
    521                if (strncmp(var->get_interface_type()->name, blks[i].Name, l)
    522                    == 0 && blks[i].Name[l] == '[') {
    523                   buffer_block_index = i;
    524                   break;
    525                }
    526             }
    527          } else {
    528             for (unsigned i = 0; i < num_blks; i++) {
    529                if (strcmp(var->get_interface_type()->name, blks[i].Name) == 0) {
    530                   buffer_block_index = i;
    531                   break;
    532                }
    533             }
    534          }
    535          assert(buffer_block_index != -1);
    536 
    537          /* Uniform blocks that were specified with an instance name must be
    538           * handled a little bit differently.  The name of the variable is the
    539           * name used to reference the uniform block instead of being the name
    540           * of a variable within the block.  Therefore, searching for the name
    541           * within the block will fail.
    542           */
    543          if (var->is_interface_instance()) {
    544             ubo_byte_offset = 0;
    545             process(var->get_interface_type(),
    546                     var->get_interface_type()->name,
    547                     use_std430_as_default);
    548          } else {
    549             const struct gl_uniform_block *const block =
    550                &blks[buffer_block_index];
    551 
    552             assert(var->data.location != -1);
    553 
    554             const struct gl_uniform_buffer_variable *const ubo_var =
    555                &block->Uniforms[var->data.location];
    556 
    557             ubo_byte_offset = ubo_var->Offset;
    558             process(var, use_std430_as_default);
    559          }
    560       } else {
    561          /* Store any explicit location and reset data location so we can
    562           * reuse this variable for storing the uniform slot number.
    563           */
    564          this->explicit_location = current_var->data.location;
    565          current_var->data.location = -1;
    566 
    567          process(var, use_std430_as_default);
    568       }
    569       delete this->record_next_sampler;
    570       delete this->record_next_bindless_sampler;
    571       delete this->record_next_image;
    572       delete this->record_next_bindless_image;
    573    }
    574 
    575    int buffer_block_index;
    576    int ubo_byte_offset;
    577    gl_shader_stage shader_type;
    578 
    579 private:
    580    bool set_opaque_indices(const glsl_type *base_type,
    581                            struct gl_uniform_storage *uniform,
    582                            const char *name, unsigned &next_index,
    583                            struct string_to_uint_map *record_next_index)
    584    {
    585       assert(base_type->is_sampler() || base_type->is_image());
    586 
    587       if (this->record_array_count > 1) {
    588          unsigned inner_array_size = MAX2(1, uniform->array_elements);
    589          char *name_copy = ralloc_strdup(NULL, name);
    590 
    591          /* Remove all array subscripts from the sampler/image name */
    592          char *str_start;
    593          const char *str_end;
    594          while((str_start = strchr(name_copy, '[')) &&
    595                (str_end = strchr(name_copy, ']'))) {
    596             memmove(str_start, str_end + 1, 1 + strlen(str_end + 1));
    597          }
    598 
    599          unsigned index = 0;
    600          if (record_next_index->get(index, name_copy)) {
    601             /* In this case, we've already seen this uniform so we just use the
    602              * next sampler/image index recorded the last time we visited.
    603              */
    604             uniform->opaque[shader_type].index = index;
    605             index = inner_array_size + uniform->opaque[shader_type].index;
    606             record_next_index->put(index, name_copy);
    607 
    608             ralloc_free(name_copy);
    609             /* Return as everything else has already been initialised in a
    610              * previous pass.
    611              */
    612             return false;
    613          } else {
    614             /* We've never seen this uniform before so we need to allocate
    615              * enough indices to store it.
    616              *
    617              * Nested struct arrays behave like arrays of arrays so we need to
    618              * increase the index by the total number of elements of the
    619              * sampler/image in case there is more than one sampler/image
    620              * inside the structs. This allows the offset to be easily
    621              * calculated for indirect indexing.
    622              */
    623             uniform->opaque[shader_type].index = next_index;
    624             next_index += inner_array_size * this->record_array_count;
    625 
    626             /* Store the next index for future passes over the struct array
    627              */
    628             index = uniform->opaque[shader_type].index + inner_array_size;
    629             record_next_index->put(index, name_copy);
    630             ralloc_free(name_copy);
    631          }
    632       } else {
    633          /* Increment the sampler/image by 1 for non-arrays and by the number
    634           * of array elements for arrays.
    635           */
    636          uniform->opaque[shader_type].index = next_index;
    637          next_index += MAX2(1, uniform->array_elements);
    638       }
    639       return true;
    640    }
    641 
    642    void handle_samplers(const glsl_type *base_type,
    643                         struct gl_uniform_storage *uniform, const char *name)
    644    {
    645       if (base_type->is_sampler()) {
    646          uniform->opaque[shader_type].active = true;
    647 
    648          const gl_texture_index target = base_type->sampler_index();
    649          const unsigned shadow = base_type->sampler_shadow;
    650 
    651          if (current_var->data.bindless) {
    652             if (!set_opaque_indices(base_type, uniform, name,
    653                                     this->next_bindless_sampler,
    654                                     this->record_next_bindless_sampler))
    655                return;
    656 
    657             this->num_bindless_samplers = this->next_bindless_sampler;
    658 
    659             this->bindless_targets = (gl_texture_index *)
    660                realloc(this->bindless_targets,
    661                        this->num_bindless_samplers * sizeof(gl_texture_index));
    662 
    663             for (unsigned i = uniform->opaque[shader_type].index;
    664                  i < this->num_bindless_samplers;
    665                  i++) {
    666                this->bindless_targets[i] = target;
    667             }
    668          } else {
    669             if (!set_opaque_indices(base_type, uniform, name,
    670                                     this->next_sampler,
    671                                     this->record_next_sampler))
    672                return;
    673 
    674             for (unsigned i = uniform->opaque[shader_type].index;
    675                  i < MIN2(this->next_sampler, MAX_SAMPLERS);
    676                  i++) {
    677                this->targets[i] = target;
    678                this->shader_samplers_used |= 1U << i;
    679                this->shader_shadow_samplers |= shadow << i;
    680             }
    681          }
    682       }
    683    }
    684 
    685    void handle_images(const glsl_type *base_type,
    686                       struct gl_uniform_storage *uniform, const char *name)
    687    {
    688       if (base_type->is_image()) {
    689          uniform->opaque[shader_type].active = true;
    690 
    691          /* Set image access qualifiers */
    692          const GLenum access =
    693             (current_var->data.memory_read_only ? GL_READ_ONLY :
    694              current_var->data.memory_write_only ? GL_WRITE_ONLY :
    695                 GL_READ_WRITE);
    696 
    697          if (current_var->data.bindless) {
    698             if (!set_opaque_indices(base_type, uniform, name,
    699                                     this->next_bindless_image,
    700                                     this->record_next_bindless_image))
    701                return;
    702 
    703             this->num_bindless_images = this->next_bindless_image;
    704 
    705             this->bindless_access = (GLenum *)
    706                realloc(this->bindless_access,
    707                        this->num_bindless_images * sizeof(GLenum));
    708 
    709             for (unsigned i = uniform->opaque[shader_type].index;
    710                  i < this->num_bindless_images;
    711                  i++) {
    712                this->bindless_access[i] = access;
    713             }
    714          } else {
    715             if (!set_opaque_indices(base_type, uniform, name,
    716                                     this->next_image,
    717                                     this->record_next_image))
    718                return;
    719 
    720             for (unsigned i = uniform->opaque[shader_type].index;
    721                  i < MIN2(this->next_image, MAX_IMAGE_UNIFORMS);
    722                  i++) {
    723                prog->_LinkedShaders[shader_type]->Program->sh.ImageAccess[i] = access;
    724             }
    725          }
    726       }
    727    }
    728 
    729    void handle_subroutines(const glsl_type *base_type,
    730                            struct gl_uniform_storage *uniform)
    731    {
    732       if (base_type->is_subroutine()) {
    733          uniform->opaque[shader_type].index = this->next_subroutine;
    734          uniform->opaque[shader_type].active = true;
    735 
    736          prog->_LinkedShaders[shader_type]->Program->sh.NumSubroutineUniforms++;
    737 
    738          /* Increment the subroutine index by 1 for non-arrays and by the
    739           * number of array elements for arrays.
    740           */
    741          this->next_subroutine += MAX2(1, uniform->array_elements);
    742 
    743       }
    744    }
    745 
    746    virtual void set_buffer_offset(unsigned offset)
    747    {
    748       this->ubo_byte_offset = offset;
    749    }
    750 
    751    virtual void set_record_array_count(unsigned record_array_count)
    752    {
    753       this->record_array_count = record_array_count;
    754    }
    755 
    756    virtual void enter_record(const glsl_type *type, const char *,
    757                              bool row_major,
    758                              const enum glsl_interface_packing packing)
    759    {
    760       assert(type->is_record());
    761       if (this->buffer_block_index == -1)
    762          return;
    763       if (packing == GLSL_INTERFACE_PACKING_STD430)
    764          this->ubo_byte_offset = glsl_align(
    765             this->ubo_byte_offset, type->std430_base_alignment(row_major));
    766       else
    767          this->ubo_byte_offset = glsl_align(
    768             this->ubo_byte_offset, type->std140_base_alignment(row_major));
    769    }
    770 
    771    virtual void leave_record(const glsl_type *type, const char *,
    772                              bool row_major,
    773                              const enum glsl_interface_packing packing)
    774    {
    775       assert(type->is_record());
    776       if (this->buffer_block_index == -1)
    777          return;
    778       if (packing == GLSL_INTERFACE_PACKING_STD430)
    779          this->ubo_byte_offset = glsl_align(
    780             this->ubo_byte_offset, type->std430_base_alignment(row_major));
    781       else
    782          this->ubo_byte_offset = glsl_align(
    783             this->ubo_byte_offset, type->std140_base_alignment(row_major));
    784    }
    785 
    786    virtual void visit_field(const glsl_type *type, const char *name,
    787                             bool row_major, const glsl_type * /* record_type */,
    788                             const enum glsl_interface_packing packing,
    789                             bool /* last_field */)
    790    {
    791       assert(!type->without_array()->is_record());
    792       assert(!type->without_array()->is_interface());
    793       assert(!(type->is_array() && type->fields.array->is_array()));
    794 
    795       unsigned id;
    796       bool found = this->map->get(id, name);
    797       assert(found);
    798 
    799       if (!found)
    800          return;
    801 
    802       const glsl_type *base_type;
    803       if (type->is_array()) {
    804          this->uniforms[id].array_elements = type->length;
    805          base_type = type->fields.array;
    806       } else {
    807          this->uniforms[id].array_elements = 0;
    808          base_type = type;
    809       }
    810 
    811       /* Initialise opaque data */
    812       this->uniforms[id].opaque[shader_type].index = ~0;
    813       this->uniforms[id].opaque[shader_type].active = false;
    814 
    815       this->uniforms[id].active_shader_mask |= 1 << shader_type;
    816 
    817       /* This assigns uniform indices to sampler and image uniforms. */
    818       handle_samplers(base_type, &this->uniforms[id], name);
    819       handle_images(base_type, &this->uniforms[id], name);
    820       handle_subroutines(base_type, &this->uniforms[id]);
    821 
    822       /* For array of arrays or struct arrays the base location may have
    823        * already been set so don't set it again.
    824        */
    825       if (buffer_block_index == -1 && current_var->data.location == -1) {
    826          current_var->data.location = id;
    827       }
    828 
    829       /* If there is already storage associated with this uniform or if the
    830        * uniform is set as builtin, it means that it was set while processing
    831        * an earlier shader stage.  For example, we may be processing the
    832        * uniform in the fragment shader, but the uniform was already processed
    833        * in the vertex shader.
    834        */
    835       if (this->uniforms[id].storage != NULL || this->uniforms[id].builtin) {
    836          return;
    837       }
    838 
    839       /* Assign explicit locations. */
    840       if (current_var->data.explicit_location) {
    841          /* Set sequential locations for struct fields. */
    842          if (current_var->type->without_array()->is_record() ||
    843              current_var->type->is_array_of_arrays()) {
    844             const unsigned entries = MAX2(1, this->uniforms[id].array_elements);
    845             this->uniforms[id].remap_location =
    846                this->explicit_location + field_counter;
    847             field_counter += entries;
    848          } else {
    849             this->uniforms[id].remap_location = this->explicit_location;
    850          }
    851       } else {
    852          /* Initialize to to indicate that no location is set */
    853          this->uniforms[id].remap_location = UNMAPPED_UNIFORM_LOC;
    854       }
    855 
    856       this->uniforms[id].name = ralloc_strdup(this->uniforms, name);
    857       this->uniforms[id].type = base_type;
    858       this->uniforms[id].num_driver_storage = 0;
    859       this->uniforms[id].driver_storage = NULL;
    860       this->uniforms[id].atomic_buffer_index = -1;
    861       this->uniforms[id].hidden =
    862          current_var->data.how_declared == ir_var_hidden;
    863       this->uniforms[id].builtin = is_gl_identifier(name);
    864 
    865       this->uniforms[id].is_shader_storage =
    866          current_var->is_in_shader_storage_block();
    867       this->uniforms[id].is_bindless = current_var->data.bindless;
    868 
    869       /* Do not assign storage if the uniform is a builtin or buffer object */
    870       if (!this->uniforms[id].builtin &&
    871           !this->uniforms[id].is_shader_storage &&
    872           this->buffer_block_index == -1)
    873          this->uniforms[id].storage = this->values;
    874 
    875       if (this->buffer_block_index != -1) {
    876          this->uniforms[id].block_index = this->buffer_block_index;
    877 
    878          unsigned alignment = type->std140_base_alignment(row_major);
    879          if (packing == GLSL_INTERFACE_PACKING_STD430)
    880             alignment = type->std430_base_alignment(row_major);
    881          this->ubo_byte_offset = glsl_align(this->ubo_byte_offset, alignment);
    882          this->uniforms[id].offset = this->ubo_byte_offset;
    883          if (packing == GLSL_INTERFACE_PACKING_STD430)
    884             this->ubo_byte_offset += type->std430_size(row_major);
    885          else
    886             this->ubo_byte_offset += type->std140_size(row_major);
    887 
    888          if (type->is_array()) {
    889             if (packing == GLSL_INTERFACE_PACKING_STD430)
    890                this->uniforms[id].array_stride =
    891                   type->without_array()->std430_array_stride(row_major);
    892             else
    893                this->uniforms[id].array_stride =
    894                   glsl_align(type->without_array()->std140_size(row_major),
    895                              16);
    896          } else {
    897             this->uniforms[id].array_stride = 0;
    898          }
    899 
    900          if (type->without_array()->is_matrix()) {
    901             this->uniforms[id].matrix_stride =
    902                link_calculate_matrix_stride(type->without_array(),
    903                                             row_major,
    904                                             packing);
    905             this->uniforms[id].row_major = row_major;
    906          } else {
    907             this->uniforms[id].matrix_stride = 0;
    908             this->uniforms[id].row_major = false;
    909          }
    910       } else {
    911          this->uniforms[id].block_index = -1;
    912          this->uniforms[id].offset = -1;
    913          this->uniforms[id].array_stride = -1;
    914          this->uniforms[id].matrix_stride = -1;
    915          this->uniforms[id].row_major = false;
    916       }
    917 
    918       if (!this->uniforms[id].builtin &&
    919           !this->uniforms[id].is_shader_storage &&
    920           this->buffer_block_index == -1)
    921          this->values += type->component_slots();
    922    }
    923 
    924    /**
    925     * Current program being processed.
    926     */
    927    struct gl_shader_program *prog;
    928 
    929    struct string_to_uint_map *map;
    930 
    931    struct gl_uniform_storage *uniforms;
    932    unsigned next_sampler;
    933    unsigned next_bindless_sampler;
    934    unsigned next_image;
    935    unsigned next_bindless_image;
    936    unsigned next_subroutine;
    937 
    938    bool use_std430_as_default;
    939 
    940    /**
    941     * Field counter is used to take care that uniform structures
    942     * with explicit locations get sequential locations.
    943     */
    944    unsigned field_counter;
    945 
    946    /**
    947     * Current variable being processed.
    948     */
    949    ir_variable *current_var;
    950 
    951    /* Used to store the explicit location from current_var so that we can
    952     * reuse the location field for storing the uniform slot id.
    953     */
    954    int explicit_location;
    955 
    956    /* Stores total struct array elements including nested structs */
    957    unsigned record_array_count;
    958 
    959    /* Map for temporarily storing next sampler index when handling samplers in
    960     * struct arrays.
    961     */
    962    struct string_to_uint_map *record_next_sampler;
    963 
    964    /* Map for temporarily storing next imager index when handling images in
    965     * struct arrays.
    966     */
    967    struct string_to_uint_map *record_next_image;
    968 
    969    /* Map for temporarily storing next bindless sampler index when handling
    970     * bindless samplers in struct arrays.
    971     */
    972    struct string_to_uint_map *record_next_bindless_sampler;
    973 
    974    /* Map for temporarily storing next bindless image index when handling
    975     * bindless images in struct arrays.
    976     */
    977    struct string_to_uint_map *record_next_bindless_image;
    978 
    979 public:
    980    union gl_constant_value *values;
    981 
    982    gl_texture_index targets[MAX_SAMPLERS];
    983 
    984    /**
    985     * Mask of samplers used by the current shader stage.
    986     */
    987    unsigned shader_samplers_used;
    988 
    989    /**
    990     * Mask of samplers used by the current shader stage for shadows.
    991     */
    992    unsigned shader_shadow_samplers;
    993 
    994    /**
    995     * Number of bindless samplers used by the current shader stage.
    996     */
    997    unsigned num_bindless_samplers;
    998 
    999    /**
   1000     * Texture targets for bindless samplers used by the current stage.
   1001     */
   1002    gl_texture_index *bindless_targets;
   1003 
   1004    /**
   1005     * Number of bindless images used by the current shader stage.
   1006     */
   1007    unsigned num_bindless_images;
   1008 
   1009    /**
   1010     * Access types for bindless images used by the current stage.
   1011     */
   1012    GLenum *bindless_access;
   1013 
   1014 };
   1015 
   1016 static bool
   1017 variable_is_referenced(ir_array_refcount_visitor &v, ir_variable *var)
   1018 {
   1019    ir_array_refcount_entry *const entry = v.get_variable_entry(var);
   1020 
   1021    return entry->is_referenced;
   1022 
   1023 }
   1024 
   1025 /**
   1026  * Walks the IR and update the references to uniform blocks in the
   1027  * ir_variables to point at linked shader's list (previously, they
   1028  * would point at the uniform block list in one of the pre-linked
   1029  * shaders).
   1030  */
   1031 static void
   1032 link_update_uniform_buffer_variables(struct gl_linked_shader *shader,
   1033                                      unsigned stage)
   1034 {
   1035    ir_array_refcount_visitor v;
   1036 
   1037    v.run(shader->ir);
   1038 
   1039    foreach_in_list(ir_instruction, node, shader->ir) {
   1040       ir_variable *const var = node->as_variable();
   1041 
   1042       if (var == NULL || !var->is_in_buffer_block())
   1043          continue;
   1044 
   1045       assert(var->data.mode == ir_var_uniform ||
   1046              var->data.mode == ir_var_shader_storage);
   1047 
   1048       unsigned num_blocks = var->data.mode == ir_var_uniform ?
   1049          shader->Program->info.num_ubos : shader->Program->info.num_ssbos;
   1050       struct gl_uniform_block **blks = var->data.mode == ir_var_uniform ?
   1051          shader->Program->sh.UniformBlocks :
   1052          shader->Program->sh.ShaderStorageBlocks;
   1053 
   1054       if (var->is_interface_instance()) {
   1055          const ir_array_refcount_entry *const entry = v.get_variable_entry(var);
   1056 
   1057          if (entry->is_referenced) {
   1058             /* Since this is an interface instance, the instance type will be
   1059              * same as the array-stripped variable type.  If the variable type
   1060              * is an array, then the block names will be suffixed with [0]
   1061              * through [n-1].  Unlike for non-interface instances, there will
   1062              * not be structure types here, so the only name sentinel that we
   1063              * have to worry about is [.
   1064              */
   1065             assert(var->type->without_array() == var->get_interface_type());
   1066             const char sentinel = var->type->is_array() ? '[' : '\0';
   1067 
   1068             const ptrdiff_t len = strlen(var->get_interface_type()->name);
   1069             for (unsigned i = 0; i < num_blocks; i++) {
   1070                const char *const begin = blks[i]->Name;
   1071                const char *const end = strchr(begin, sentinel);
   1072 
   1073                if (end == NULL)
   1074                   continue;
   1075 
   1076                if (len != (end - begin))
   1077                   continue;
   1078 
   1079                /* Even when a match is found, do not "break" here.  This could
   1080                 * be an array of instances, and all elements of the array need
   1081                 * to be marked as referenced.
   1082                 */
   1083                if (strncmp(begin, var->get_interface_type()->name, len) == 0 &&
   1084                    (!var->type->is_array() ||
   1085                     entry->is_linearized_index_referenced(blks[i]->linearized_array_index))) {
   1086                   blks[i]->stageref |= 1U << stage;
   1087                }
   1088             }
   1089          }
   1090 
   1091          var->data.location = 0;
   1092          continue;
   1093       }
   1094 
   1095       bool found = false;
   1096       char sentinel = '\0';
   1097 
   1098       if (var->type->is_record()) {
   1099          sentinel = '.';
   1100       } else if (var->type->is_array() && (var->type->fields.array->is_array()
   1101                  || var->type->without_array()->is_record())) {
   1102          sentinel = '[';
   1103       }
   1104 
   1105       const unsigned l = strlen(var->name);
   1106       for (unsigned i = 0; i < num_blocks; i++) {
   1107          for (unsigned j = 0; j < blks[i]->NumUniforms; j++) {
   1108             if (sentinel) {
   1109                const char *begin = blks[i]->Uniforms[j].Name;
   1110                const char *end = strchr(begin, sentinel);
   1111 
   1112                if (end == NULL)
   1113                   continue;
   1114 
   1115                if ((ptrdiff_t) l != (end - begin))
   1116                   continue;
   1117 
   1118                found = strncmp(var->name, begin, l) == 0;
   1119             } else {
   1120                found = strcmp(var->name, blks[i]->Uniforms[j].Name) == 0;
   1121             }
   1122 
   1123             if (found) {
   1124                var->data.location = j;
   1125 
   1126                if (variable_is_referenced(v, var))
   1127                   blks[i]->stageref |= 1U << stage;
   1128 
   1129                break;
   1130             }
   1131          }
   1132 
   1133          if (found)
   1134             break;
   1135       }
   1136       assert(found);
   1137    }
   1138 }
   1139 
   1140 /**
   1141  * Combine the hidden uniform hash map with the uniform hash map so that the
   1142  * hidden uniforms will be given indicies at the end of the uniform storage
   1143  * array.
   1144  */
   1145 static void
   1146 assign_hidden_uniform_slot_id(const char *name, unsigned hidden_id,
   1147                               void *closure)
   1148 {
   1149    count_uniform_size *uniform_size = (count_uniform_size *) closure;
   1150    unsigned hidden_uniform_start = uniform_size->num_active_uniforms -
   1151       uniform_size->num_hidden_uniforms;
   1152 
   1153    uniform_size->map->put(hidden_uniform_start + hidden_id, name);
   1154 }
   1155 
   1156 /**
   1157  * Search through the list of empty blocks to find one that fits the current
   1158  * uniform.
   1159  */
   1160 static int
   1161 find_empty_block(struct gl_shader_program *prog,
   1162                  struct gl_uniform_storage *uniform)
   1163 {
   1164    const unsigned entries = MAX2(1, uniform->array_elements);
   1165 
   1166    foreach_list_typed(struct empty_uniform_block, block, link,
   1167                       &prog->EmptyUniformLocations) {
   1168       /* Found a block with enough slots to fit the uniform */
   1169       if (block->slots == entries) {
   1170          unsigned start = block->start;
   1171          exec_node_remove(&block->link);
   1172          ralloc_free(block);
   1173 
   1174          return start;
   1175       /* Found a block with more slots than needed. It can still be used. */
   1176       } else if (block->slots > entries) {
   1177          unsigned start = block->start;
   1178          block->start += entries;
   1179          block->slots -= entries;
   1180 
   1181          return start;
   1182       }
   1183    }
   1184 
   1185    return -1;
   1186 }
   1187 
   1188 static void
   1189 link_setup_uniform_remap_tables(struct gl_context *ctx,
   1190                                 struct gl_shader_program *prog)
   1191 {
   1192    unsigned total_entries = prog->NumExplicitUniformLocations;
   1193    unsigned empty_locs = prog->NumUniformRemapTable - total_entries;
   1194 
   1195    /* Reserve all the explicit locations of the active uniforms. */
   1196    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
   1197       if (prog->data->UniformStorage[i].type->is_subroutine() ||
   1198           prog->data->UniformStorage[i].is_shader_storage)
   1199          continue;
   1200 
   1201       if (prog->data->UniformStorage[i].remap_location !=
   1202           UNMAPPED_UNIFORM_LOC) {
   1203          /* How many new entries for this uniform? */
   1204          const unsigned entries =
   1205             MAX2(1, prog->data->UniformStorage[i].array_elements);
   1206 
   1207          /* Set remap table entries point to correct gl_uniform_storage. */
   1208          for (unsigned j = 0; j < entries; j++) {
   1209             unsigned element_loc =
   1210                prog->data->UniformStorage[i].remap_location + j;
   1211             assert(prog->UniformRemapTable[element_loc] ==
   1212                    INACTIVE_UNIFORM_EXPLICIT_LOCATION);
   1213             prog->UniformRemapTable[element_loc] =
   1214                &prog->data->UniformStorage[i];
   1215          }
   1216       }
   1217    }
   1218 
   1219    /* Reserve locations for rest of the uniforms. */
   1220    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
   1221 
   1222       if (prog->data->UniformStorage[i].type->is_subroutine() ||
   1223           prog->data->UniformStorage[i].is_shader_storage)
   1224          continue;
   1225 
   1226       /* Built-in uniforms should not get any location. */
   1227       if (prog->data->UniformStorage[i].builtin)
   1228          continue;
   1229 
   1230       /* Explicit ones have been set already. */
   1231       if (prog->data->UniformStorage[i].remap_location != UNMAPPED_UNIFORM_LOC)
   1232          continue;
   1233 
   1234       /* how many new entries for this uniform? */
   1235       const unsigned entries =
   1236          MAX2(1, prog->data->UniformStorage[i].array_elements);
   1237 
   1238       /* Find UniformRemapTable for empty blocks where we can fit this uniform. */
   1239       int chosen_location = -1;
   1240 
   1241       if (empty_locs)
   1242          chosen_location = find_empty_block(prog, &prog->data->UniformStorage[i]);
   1243 
   1244       /* Add new entries to the total amount of entries. */
   1245       total_entries += entries;
   1246 
   1247       if (chosen_location != -1) {
   1248          empty_locs -= entries;
   1249       } else {
   1250          chosen_location = prog->NumUniformRemapTable;
   1251 
   1252          /* resize remap table to fit new entries */
   1253          prog->UniformRemapTable =
   1254             reralloc(prog,
   1255                      prog->UniformRemapTable,
   1256                      gl_uniform_storage *,
   1257                      prog->NumUniformRemapTable + entries);
   1258          prog->NumUniformRemapTable += entries;
   1259       }
   1260 
   1261       /* set pointers for this uniform */
   1262       for (unsigned j = 0; j < entries; j++)
   1263          prog->UniformRemapTable[chosen_location + j] =
   1264             &prog->data->UniformStorage[i];
   1265 
   1266       /* set the base location in remap table for the uniform */
   1267       prog->data->UniformStorage[i].remap_location = chosen_location;
   1268    }
   1269 
   1270    /* Verify that total amount of entries for explicit and implicit locations
   1271     * is less than MAX_UNIFORM_LOCATIONS.
   1272     */
   1273 
   1274    if (total_entries > ctx->Const.MaxUserAssignableUniformLocations) {
   1275       linker_error(prog, "count of uniform locations > MAX_UNIFORM_LOCATIONS"
   1276                    "(%u > %u)", total_entries,
   1277                    ctx->Const.MaxUserAssignableUniformLocations);
   1278    }
   1279 
   1280    /* Reserve all the explicit locations of the active subroutine uniforms. */
   1281    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
   1282       if (!prog->data->UniformStorage[i].type->is_subroutine())
   1283          continue;
   1284 
   1285       if (prog->data->UniformStorage[i].remap_location == UNMAPPED_UNIFORM_LOC)
   1286          continue;
   1287 
   1288       /* How many new entries for this uniform? */
   1289       const unsigned entries =
   1290          MAX2(1, prog->data->UniformStorage[i].array_elements);
   1291 
   1292       unsigned mask = prog->data->linked_stages;
   1293       while (mask) {
   1294          const int j = u_bit_scan(&mask);
   1295          struct gl_program *p = prog->_LinkedShaders[j]->Program;
   1296 
   1297          if (!prog->data->UniformStorage[i].opaque[j].active)
   1298             continue;
   1299 
   1300          /* Set remap table entries point to correct gl_uniform_storage. */
   1301          for (unsigned k = 0; k < entries; k++) {
   1302             unsigned element_loc =
   1303                prog->data->UniformStorage[i].remap_location + k;
   1304             assert(p->sh.SubroutineUniformRemapTable[element_loc] ==
   1305                    INACTIVE_UNIFORM_EXPLICIT_LOCATION);
   1306             p->sh.SubroutineUniformRemapTable[element_loc] =
   1307                &prog->data->UniformStorage[i];
   1308          }
   1309       }
   1310    }
   1311 
   1312    /* reserve subroutine locations */
   1313    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
   1314       if (!prog->data->UniformStorage[i].type->is_subroutine())
   1315          continue;
   1316 
   1317       if (prog->data->UniformStorage[i].remap_location !=
   1318           UNMAPPED_UNIFORM_LOC)
   1319          continue;
   1320 
   1321       const unsigned entries =
   1322          MAX2(1, prog->data->UniformStorage[i].array_elements);
   1323 
   1324       unsigned mask = prog->data->linked_stages;
   1325       while (mask) {
   1326          const int j = u_bit_scan(&mask);
   1327          struct gl_program *p = prog->_LinkedShaders[j]->Program;
   1328 
   1329          if (!prog->data->UniformStorage[i].opaque[j].active)
   1330             continue;
   1331 
   1332          p->sh.SubroutineUniformRemapTable =
   1333             reralloc(p,
   1334                      p->sh.SubroutineUniformRemapTable,
   1335                      gl_uniform_storage *,
   1336                      p->sh.NumSubroutineUniformRemapTable + entries);
   1337 
   1338          for (unsigned k = 0; k < entries; k++) {
   1339             p->sh.SubroutineUniformRemapTable[p->sh.NumSubroutineUniformRemapTable + k] =
   1340                &prog->data->UniformStorage[i];
   1341          }
   1342          prog->data->UniformStorage[i].remap_location =
   1343             p->sh.NumSubroutineUniformRemapTable;
   1344          p->sh.NumSubroutineUniformRemapTable += entries;
   1345       }
   1346    }
   1347 }
   1348 
   1349 static void
   1350 link_assign_uniform_storage(struct gl_context *ctx,
   1351                             struct gl_shader_program *prog,
   1352                             const unsigned num_data_slots)
   1353 {
   1354    /* On the outside chance that there were no uniforms, bail out.
   1355     */
   1356    if (prog->data->NumUniformStorage == 0)
   1357       return;
   1358 
   1359    unsigned int boolean_true = ctx->Const.UniformBooleanTrue;
   1360 
   1361    union gl_constant_value *data;
   1362    if (prog->data->UniformStorage == NULL) {
   1363       prog->data->UniformStorage = rzalloc_array(prog->data,
   1364                                                  struct gl_uniform_storage,
   1365                                                  prog->data->NumUniformStorage);
   1366       data = rzalloc_array(prog->data->UniformStorage,
   1367                            union gl_constant_value, num_data_slots);
   1368       prog->data->UniformDataDefaults =
   1369          rzalloc_array(prog->data->UniformStorage,
   1370                        union gl_constant_value, num_data_slots);
   1371    } else {
   1372       data = prog->data->UniformDataSlots;
   1373    }
   1374 
   1375 #ifndef NDEBUG
   1376    union gl_constant_value *data_end = &data[num_data_slots];
   1377 #endif
   1378 
   1379    parcel_out_uniform_storage parcel(prog, prog->UniformHash,
   1380                                      prog->data->UniformStorage, data,
   1381                                      ctx->Const.UseSTD430AsDefaultPacking);
   1382 
   1383    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
   1384       struct gl_linked_shader *shader = prog->_LinkedShaders[i];
   1385 
   1386       if (!shader)
   1387          continue;
   1388 
   1389       parcel.start_shader((gl_shader_stage)i);
   1390 
   1391       foreach_in_list(ir_instruction, node, shader->ir) {
   1392          ir_variable *const var = node->as_variable();
   1393 
   1394          if ((var == NULL) || (var->data.mode != ir_var_uniform &&
   1395                                var->data.mode != ir_var_shader_storage))
   1396             continue;
   1397 
   1398          parcel.set_and_process(var);
   1399       }
   1400 
   1401       shader->Program->SamplersUsed = parcel.shader_samplers_used;
   1402       shader->shadow_samplers = parcel.shader_shadow_samplers;
   1403 
   1404       if (parcel.num_bindless_samplers > 0) {
   1405          shader->Program->sh.NumBindlessSamplers = parcel.num_bindless_samplers;
   1406          shader->Program->sh.BindlessSamplers =
   1407             rzalloc_array(shader->Program, gl_bindless_sampler,
   1408                           parcel.num_bindless_samplers);
   1409          for (unsigned j = 0; j < parcel.num_bindless_samplers; j++) {
   1410             shader->Program->sh.BindlessSamplers[j].target =
   1411                parcel.bindless_targets[j];
   1412          }
   1413       }
   1414 
   1415       if (parcel.num_bindless_images > 0) {
   1416          shader->Program->sh.NumBindlessImages = parcel.num_bindless_images;
   1417          shader->Program->sh.BindlessImages =
   1418             rzalloc_array(shader->Program, gl_bindless_image,
   1419                           parcel.num_bindless_images);
   1420          for (unsigned j = 0; j < parcel.num_bindless_images; j++) {
   1421             shader->Program->sh.BindlessImages[j].access =
   1422                parcel.bindless_access[j];
   1423          }
   1424       }
   1425 
   1426       STATIC_ASSERT(sizeof(shader->Program->sh.SamplerTargets) ==
   1427                     sizeof(parcel.targets));
   1428       memcpy(shader->Program->sh.SamplerTargets,
   1429              parcel.targets,
   1430              sizeof(shader->Program->sh.SamplerTargets));
   1431    }
   1432 
   1433 #ifndef NDEBUG
   1434    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
   1435       assert(prog->data->UniformStorage[i].storage != NULL ||
   1436              prog->data->UniformStorage[i].builtin ||
   1437              prog->data->UniformStorage[i].is_shader_storage ||
   1438              prog->data->UniformStorage[i].block_index != -1);
   1439    }
   1440 
   1441    assert(parcel.values == data_end);
   1442 #endif
   1443 
   1444    link_setup_uniform_remap_tables(ctx, prog);
   1445 
   1446    /* Set shader cache fields */
   1447    prog->data->NumUniformDataSlots = num_data_slots;
   1448    prog->data->UniformDataSlots = data;
   1449 
   1450    link_set_uniform_initializers(prog, boolean_true);
   1451 }
   1452 
   1453 void
   1454 link_assign_uniform_locations(struct gl_shader_program *prog,
   1455                               struct gl_context *ctx)
   1456 {
   1457    ralloc_free(prog->data->UniformStorage);
   1458    prog->data->UniformStorage = NULL;
   1459    prog->data->NumUniformStorage = 0;
   1460 
   1461    if (prog->UniformHash != NULL) {
   1462       prog->UniformHash->clear();
   1463    } else {
   1464       prog->UniformHash = new string_to_uint_map;
   1465    }
   1466 
   1467    /* First pass: Count the uniform resources used by the user-defined
   1468     * uniforms.  While this happens, each active uniform will have an index
   1469     * assigned to it.
   1470     *
   1471     * Note: this is *NOT* the index that is returned to the application by
   1472     * glGetUniformLocation.
   1473     */
   1474    struct string_to_uint_map *hiddenUniforms = new string_to_uint_map;
   1475    count_uniform_size uniform_size(prog->UniformHash, hiddenUniforms,
   1476                                    ctx->Const.UseSTD430AsDefaultPacking);
   1477    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
   1478       struct gl_linked_shader *sh = prog->_LinkedShaders[i];
   1479 
   1480       if (sh == NULL)
   1481          continue;
   1482 
   1483       link_update_uniform_buffer_variables(sh, i);
   1484 
   1485       /* Reset various per-shader target counts.
   1486        */
   1487       uniform_size.start_shader();
   1488 
   1489       foreach_in_list(ir_instruction, node, sh->ir) {
   1490          ir_variable *const var = node->as_variable();
   1491 
   1492          if ((var == NULL) || (var->data.mode != ir_var_uniform &&
   1493                                var->data.mode != ir_var_shader_storage))
   1494             continue;
   1495 
   1496          uniform_size.process(var);
   1497       }
   1498 
   1499       sh->Program->info.num_textures = uniform_size.num_shader_samplers;
   1500       sh->Program->info.num_images = uniform_size.num_shader_images;
   1501       sh->num_uniform_components = uniform_size.num_shader_uniform_components;
   1502       sh->num_combined_uniform_components = sh->num_uniform_components;
   1503 
   1504       for (unsigned i = 0; i < sh->Program->info.num_ubos; i++) {
   1505          sh->num_combined_uniform_components +=
   1506             sh->Program->sh.UniformBlocks[i]->UniformBufferSize / 4;
   1507       }
   1508    }
   1509 
   1510    prog->data->NumUniformStorage = uniform_size.num_active_uniforms;
   1511    prog->data->NumHiddenUniforms = uniform_size.num_hidden_uniforms;
   1512 
   1513    /* assign hidden uniforms a slot id */
   1514    hiddenUniforms->iterate(assign_hidden_uniform_slot_id, &uniform_size);
   1515    delete hiddenUniforms;
   1516 
   1517    link_assign_uniform_storage(ctx, prog, uniform_size.num_values);
   1518 }
   1519