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