• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2018 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 DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "nir.h"
25 #include "nir_deref.h"
26 #include "gl_nir_linker.h"
27 #include "compiler/glsl/ir_uniform.h" /* for gl_uniform_storage */
28 #include "linker_util.h"
29 #include "main/context.h"
30 #include "main/mtypes.h"
31 
32 /**
33  * This file do the common link for GLSL uniforms, using NIR, instead of IR as
34  * the counter-part glsl/link_uniforms.cpp
35  */
36 
37 #define UNMAPPED_UNIFORM_LOC ~0u
38 
39 struct uniform_array_info {
40    /** List of dereferences of the uniform array. */
41    struct util_dynarray *deref_list;
42 
43    /** Set of bit-flags to note which array elements have been accessed. */
44    BITSET_WORD *indices;
45 };
46 
47 /**
48  * Built-in / reserved GL variables names start with "gl_"
49  */
50 static inline bool
is_gl_identifier(const char * s)51 is_gl_identifier(const char *s)
52 {
53    return s && s[0] == 'g' && s[1] == 'l' && s[2] == '_';
54 }
55 
56 static unsigned
uniform_storage_size(const struct glsl_type * type)57 uniform_storage_size(const struct glsl_type *type)
58 {
59    switch (glsl_get_base_type(type)) {
60    case GLSL_TYPE_STRUCT:
61    case GLSL_TYPE_INTERFACE: {
62       unsigned size = 0;
63       for (unsigned i = 0; i < glsl_get_length(type); i++)
64          size += uniform_storage_size(glsl_get_struct_field(type, i));
65       return size;
66    }
67    case GLSL_TYPE_ARRAY: {
68       const struct glsl_type *e_type = glsl_get_array_element(type);
69       enum glsl_base_type e_base_type = glsl_get_base_type(e_type);
70       if (e_base_type == GLSL_TYPE_STRUCT ||
71           e_base_type == GLSL_TYPE_INTERFACE ||
72           e_base_type == GLSL_TYPE_ARRAY) {
73          unsigned length = !glsl_type_is_unsized_array(type) ?
74             glsl_get_length(type) : 1;
75          return length * uniform_storage_size(e_type);
76       } else
77          return 1;
78    }
79    default:
80       return 1;
81    }
82 }
83 
84 /**
85  * Update the sizes of linked shader uniform arrays to the maximum
86  * array index used.
87  *
88  * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
89  *
90  *     If one or more elements of an array are active,
91  *     GetActiveUniform will return the name of the array in name,
92  *     subject to the restrictions listed above. The type of the array
93  *     is returned in type. The size parameter contains the highest
94  *     array element index used, plus one. The compiler or linker
95  *     determines the highest index used.  There will be only one
96  *     active uniform reported by the GL per uniform array.
97  */
98 static void
update_array_sizes(struct gl_shader_program * prog,nir_variable * var,struct hash_table ** referenced_uniforms,unsigned current_var_stage)99 update_array_sizes(struct gl_shader_program *prog, nir_variable *var,
100                    struct hash_table **referenced_uniforms,
101                    unsigned current_var_stage)
102 {
103    /* For now we only resize 1D arrays.
104     * TODO: add support for resizing more complex array types ??
105     */
106    if (!glsl_type_is_array(var->type) ||
107        glsl_type_is_array(glsl_get_array_element(var->type)))
108       return;
109 
110    /* GL_ARB_uniform_buffer_object says that std140 uniforms
111     * will not be eliminated.  Since we always do std140, just
112     * don't resize arrays in UBOs.
113     *
114     * Atomic counters are supposed to get deterministic
115     * locations assigned based on the declaration ordering and
116     * sizes, array compaction would mess that up.
117     *
118     * Subroutine uniforms are not removed.
119     */
120    if (nir_variable_is_in_block(var) || glsl_contains_atomic(var->type) ||
121        glsl_get_base_type(glsl_without_array(var->type)) == GLSL_TYPE_SUBROUTINE ||
122        var->constant_initializer)
123       return;
124 
125    struct uniform_array_info *ainfo = NULL;
126    int words = BITSET_WORDS(glsl_array_size(var->type));
127    int max_array_size = 0;
128    for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
129       struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
130       if (!sh)
131          continue;
132 
133       struct hash_entry *entry =
134          _mesa_hash_table_search(referenced_uniforms[stage], var->name);
135       if (entry) {
136          ainfo = (struct uniform_array_info *)  entry->data;
137          max_array_size = MAX2(BITSET_LAST_BIT(ainfo->indices, words),
138                                max_array_size);
139       }
140 
141       if (max_array_size == glsl_array_size(var->type))
142          return;
143    }
144 
145    if (max_array_size != glsl_array_size(var->type)) {
146       /* If this is a built-in uniform (i.e., it's backed by some
147        * fixed-function state), adjust the number of state slots to
148        * match the new array size.  The number of slots per array entry
149        * is not known.  It seems safe to assume that the total number of
150        * slots is an integer multiple of the number of array elements.
151        * Determine the number of slots per array element by dividing by
152        * the old (total) size.
153        */
154       const unsigned num_slots = var->num_state_slots;
155       if (num_slots > 0) {
156          var->num_state_slots =
157             (max_array_size * (num_slots / glsl_array_size(var->type)));
158       }
159 
160       var->type = glsl_array_type(glsl_get_array_element(var->type),
161                                   max_array_size, 0);
162 
163       /* Update the types of dereferences in case we changed any. */
164       struct hash_entry *entry =
165          _mesa_hash_table_search(referenced_uniforms[current_var_stage], var->name);
166       if (entry) {
167          struct uniform_array_info *ainfo =
168             (struct uniform_array_info *) entry->data;
169          util_dynarray_foreach(ainfo->deref_list, nir_deref_instr *, deref) {
170             (*deref)->type = var->type;
171          }
172       }
173    }
174 }
175 
176 static void
nir_setup_uniform_remap_tables(struct gl_context * ctx,struct gl_shader_program * prog)177 nir_setup_uniform_remap_tables(struct gl_context *ctx,
178                                struct gl_shader_program *prog)
179 {
180    unsigned total_entries = prog->NumExplicitUniformLocations;
181 
182    /* For glsl this may have been allocated by reserve_explicit_locations() so
183     * that we can keep track of unused uniforms with explicit locations.
184     */
185    assert(!prog->data->spirv ||
186           (prog->data->spirv && !prog->UniformRemapTable));
187    if (!prog->UniformRemapTable) {
188       prog->UniformRemapTable = rzalloc_array(prog,
189                                               struct gl_uniform_storage *,
190                                               prog->NumUniformRemapTable);
191    }
192 
193    union gl_constant_value *data =
194       rzalloc_array(prog->data,
195                     union gl_constant_value, prog->data->NumUniformDataSlots);
196    if (!prog->UniformRemapTable || !data) {
197       linker_error(prog, "Out of memory during linking.\n");
198       return;
199    }
200    prog->data->UniformDataSlots = data;
201 
202    prog->data->UniformDataDefaults =
203          rzalloc_array(prog->data->UniformDataSlots,
204                        union gl_constant_value, prog->data->NumUniformDataSlots);
205 
206    unsigned data_pos = 0;
207 
208    /* Reserve all the explicit locations of the active uniforms. */
209    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
210       struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
211 
212       if (uniform->is_shader_storage ||
213           glsl_get_base_type(uniform->type) == GLSL_TYPE_SUBROUTINE)
214          continue;
215 
216       if (prog->data->UniformStorage[i].remap_location == UNMAPPED_UNIFORM_LOC)
217          continue;
218 
219       /* How many new entries for this uniform? */
220       const unsigned entries = MAX2(1, uniform->array_elements);
221       unsigned num_slots = glsl_get_component_slots(uniform->type);
222 
223       uniform->storage = &data[data_pos];
224 
225       /* Set remap table entries point to correct gl_uniform_storage. */
226       for (unsigned j = 0; j < entries; j++) {
227          unsigned element_loc = uniform->remap_location + j;
228          prog->UniformRemapTable[element_loc] = uniform;
229 
230          data_pos += num_slots;
231       }
232    }
233 
234    /* Reserve locations for rest of the uniforms. */
235    if (prog->data->spirv)
236       link_util_update_empty_uniform_locations(prog);
237 
238    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
239       struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
240 
241       if (uniform->is_shader_storage ||
242           glsl_get_base_type(uniform->type) == GLSL_TYPE_SUBROUTINE)
243          continue;
244 
245       /* Built-in uniforms should not get any location. */
246       if (uniform->builtin)
247          continue;
248 
249       /* Explicit ones have been set already. */
250       if (uniform->remap_location != UNMAPPED_UNIFORM_LOC)
251          continue;
252 
253       /* How many entries for this uniform? */
254       const unsigned entries = MAX2(1, uniform->array_elements);
255 
256       /* Add new entries to the total amount for checking against MAX_UNIFORM-
257        * _LOCATIONS. This only applies to the default uniform block (-1),
258        * because locations of uniform block entries are not assignable.
259        */
260       if (prog->data->UniformStorage[i].block_index == -1)
261          total_entries += entries;
262 
263       unsigned location =
264          link_util_find_empty_block(prog, &prog->data->UniformStorage[i]);
265 
266       if (location == -1) {
267          location = prog->NumUniformRemapTable;
268 
269          /* resize remap table to fit new entries */
270          prog->UniformRemapTable =
271             reralloc(prog,
272                      prog->UniformRemapTable,
273                      struct gl_uniform_storage *,
274                      prog->NumUniformRemapTable + entries);
275          prog->NumUniformRemapTable += entries;
276       }
277 
278       /* set the base location in remap table for the uniform */
279       uniform->remap_location = location;
280 
281       unsigned num_slots = glsl_get_component_slots(uniform->type);
282 
283       if (uniform->block_index == -1)
284          uniform->storage = &data[data_pos];
285 
286       /* Set remap table entries point to correct gl_uniform_storage. */
287       for (unsigned j = 0; j < entries; j++) {
288          unsigned element_loc = uniform->remap_location + j;
289          prog->UniformRemapTable[element_loc] = uniform;
290 
291          if (uniform->block_index == -1)
292             data_pos += num_slots;
293       }
294    }
295 
296    /* Verify that total amount of entries for explicit and implicit locations
297     * is less than MAX_UNIFORM_LOCATIONS.
298     */
299    if (total_entries > ctx->Const.MaxUserAssignableUniformLocations) {
300       linker_error(prog, "count of uniform locations > MAX_UNIFORM_LOCATIONS"
301                    "(%u > %u)", total_entries,
302                    ctx->Const.MaxUserAssignableUniformLocations);
303    }
304 
305    /* Reserve all the explicit locations of the active subroutine uniforms. */
306    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
307       struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
308 
309       if (glsl_get_base_type(uniform->type) != GLSL_TYPE_SUBROUTINE)
310          continue;
311 
312       if (prog->data->UniformStorage[i].remap_location == UNMAPPED_UNIFORM_LOC)
313          continue;
314 
315       /* How many new entries for this uniform? */
316       const unsigned entries =
317          MAX2(1, prog->data->UniformStorage[i].array_elements);
318 
319       uniform->storage = &data[data_pos];
320 
321       unsigned num_slots = glsl_get_component_slots(uniform->type);
322       unsigned mask = prog->data->linked_stages;
323       while (mask) {
324          const int j = u_bit_scan(&mask);
325          struct gl_program *p = prog->_LinkedShaders[j]->Program;
326 
327          if (!prog->data->UniformStorage[i].opaque[j].active)
328             continue;
329 
330          /* Set remap table entries point to correct gl_uniform_storage. */
331          for (unsigned k = 0; k < entries; k++) {
332             unsigned element_loc =
333                prog->data->UniformStorage[i].remap_location + k;
334             p->sh.SubroutineUniformRemapTable[element_loc] =
335                &prog->data->UniformStorage[i];
336 
337             data_pos += num_slots;
338          }
339       }
340    }
341 
342    /* reserve subroutine locations */
343    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
344       struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
345 
346       if (glsl_get_base_type(uniform->type) != GLSL_TYPE_SUBROUTINE)
347          continue;
348 
349       if (prog->data->UniformStorage[i].remap_location !=
350           UNMAPPED_UNIFORM_LOC)
351          continue;
352 
353       const unsigned entries =
354          MAX2(1, prog->data->UniformStorage[i].array_elements);
355 
356       uniform->storage = &data[data_pos];
357 
358       unsigned num_slots = glsl_get_component_slots(uniform->type);
359       unsigned mask = prog->data->linked_stages;
360       while (mask) {
361          const int j = u_bit_scan(&mask);
362          struct gl_program *p = prog->_LinkedShaders[j]->Program;
363 
364          if (!prog->data->UniformStorage[i].opaque[j].active)
365             continue;
366 
367          p->sh.SubroutineUniformRemapTable =
368             reralloc(p,
369                      p->sh.SubroutineUniformRemapTable,
370                      struct gl_uniform_storage *,
371                      p->sh.NumSubroutineUniformRemapTable + entries);
372 
373          for (unsigned k = 0; k < entries; k++) {
374             p->sh.SubroutineUniformRemapTable[p->sh.NumSubroutineUniformRemapTable + k] =
375                &prog->data->UniformStorage[i];
376 
377             data_pos += num_slots;
378          }
379          prog->data->UniformStorage[i].remap_location =
380             p->sh.NumSubroutineUniformRemapTable;
381          p->sh.NumSubroutineUniformRemapTable += entries;
382       }
383    }
384 }
385 
386 static void
add_var_use_deref(nir_deref_instr * deref,struct hash_table * live,struct array_deref_range ** derefs,unsigned * derefs_size)387 add_var_use_deref(nir_deref_instr *deref, struct hash_table *live,
388                   struct array_deref_range **derefs, unsigned *derefs_size)
389 {
390    nir_deref_path path;
391    nir_deref_path_init(&path, deref, NULL);
392 
393    deref = path.path[0];
394    if (deref->deref_type != nir_deref_type_var ||
395        !nir_deref_mode_is_one_of(deref, nir_var_uniform |
396                                         nir_var_mem_ubo |
397                                         nir_var_mem_ssbo)) {
398       nir_deref_path_finish(&path);
399       return;
400    }
401 
402    /* Number of derefs used in current processing. */
403    unsigned num_derefs = 0;
404 
405    const struct glsl_type *deref_type = deref->var->type;
406    nir_deref_instr **p = &path.path[1];
407    for (; *p; p++) {
408       if ((*p)->deref_type == nir_deref_type_array) {
409 
410          /* Skip matrix derefences */
411          if (!glsl_type_is_array(deref_type))
412             break;
413 
414          if ((num_derefs + 1) * sizeof(struct array_deref_range) > *derefs_size) {
415             void *ptr = reralloc_size(NULL, *derefs, *derefs_size + 4096);
416 
417             if (ptr == NULL) {
418                nir_deref_path_finish(&path);
419                return;
420             }
421 
422             *derefs_size += 4096;
423             *derefs = (struct array_deref_range *)ptr;
424          }
425 
426          struct array_deref_range *dr = &(*derefs)[num_derefs];
427          num_derefs++;
428 
429          dr->size = glsl_get_length(deref_type);
430 
431          if (nir_src_is_const((*p)->arr.index)) {
432             dr->index = nir_src_as_uint((*p)->arr.index);
433          } else {
434             /* An unsized array can occur at the end of an SSBO.  We can't track
435              * accesses to such an array, so bail.
436              */
437             if (dr->size == 0) {
438                nir_deref_path_finish(&path);
439                return;
440             }
441 
442             dr->index = dr->size;
443          }
444 
445          deref_type = glsl_get_array_element(deref_type);
446       } else if ((*p)->deref_type == nir_deref_type_struct) {
447          /* We have reached the end of the array. */
448          break;
449       }
450    }
451 
452    nir_deref_path_finish(&path);
453 
454 
455    struct uniform_array_info *ainfo = NULL;
456 
457    struct hash_entry *entry =
458       _mesa_hash_table_search(live, deref->var->name);
459    if (!entry && glsl_type_is_array(deref->var->type)) {
460       ainfo = ralloc(live, struct uniform_array_info);
461 
462       unsigned num_bits = MAX2(1, glsl_get_aoa_size(deref->var->type));
463       ainfo->indices = rzalloc_array(live, BITSET_WORD, BITSET_WORDS(num_bits));
464 
465       ainfo->deref_list = ralloc(live, struct util_dynarray);
466       util_dynarray_init(ainfo->deref_list, live);
467    }
468 
469    if (entry)
470       ainfo = (struct uniform_array_info *) entry->data;
471 
472    if (glsl_type_is_array(deref->var->type)) {
473       /* Count the "depth" of the arrays-of-arrays. */
474       unsigned array_depth = 0;
475       for (const struct glsl_type *type = deref->var->type;
476            glsl_type_is_array(type);
477            type = glsl_get_array_element(type)) {
478          array_depth++;
479       }
480 
481       link_util_mark_array_elements_referenced(*derefs, num_derefs, array_depth,
482                                                ainfo->indices);
483 
484       util_dynarray_append(ainfo->deref_list, nir_deref_instr *, deref);
485    }
486 
487    assert(deref->modes == deref->var->data.mode);
488    _mesa_hash_table_insert(live, deref->var->name, ainfo);
489 }
490 
491 /* Iterate over the shader and collect infomation about uniform use */
492 static void
add_var_use_shader(nir_shader * shader,struct hash_table * live)493 add_var_use_shader(nir_shader *shader, struct hash_table *live)
494 {
495    /* Currently allocated buffer block of derefs. */
496    struct array_deref_range *derefs = NULL;
497 
498    /* Size of the derefs buffer in bytes. */
499    unsigned derefs_size = 0;
500 
501    nir_foreach_function(function, shader) {
502       if (function->impl) {
503          nir_foreach_block(block, function->impl) {
504             nir_foreach_instr(instr, block) {
505                if (instr->type == nir_instr_type_intrinsic) {
506                   nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
507                   switch (intr->intrinsic) {
508                   case nir_intrinsic_atomic_counter_read_deref:
509                   case nir_intrinsic_atomic_counter_inc_deref:
510                   case nir_intrinsic_atomic_counter_pre_dec_deref:
511                   case nir_intrinsic_atomic_counter_post_dec_deref:
512                   case nir_intrinsic_atomic_counter_add_deref:
513                   case nir_intrinsic_atomic_counter_min_deref:
514                   case nir_intrinsic_atomic_counter_max_deref:
515                   case nir_intrinsic_atomic_counter_and_deref:
516                   case nir_intrinsic_atomic_counter_or_deref:
517                   case nir_intrinsic_atomic_counter_xor_deref:
518                   case nir_intrinsic_atomic_counter_exchange_deref:
519                   case nir_intrinsic_atomic_counter_comp_swap_deref:
520                   case nir_intrinsic_image_deref_load:
521                   case nir_intrinsic_image_deref_store:
522                   case nir_intrinsic_image_deref_atomic_add:
523                   case nir_intrinsic_image_deref_atomic_umin:
524                   case nir_intrinsic_image_deref_atomic_imin:
525                   case nir_intrinsic_image_deref_atomic_umax:
526                   case nir_intrinsic_image_deref_atomic_imax:
527                   case nir_intrinsic_image_deref_atomic_and:
528                   case nir_intrinsic_image_deref_atomic_or:
529                   case nir_intrinsic_image_deref_atomic_xor:
530                   case nir_intrinsic_image_deref_atomic_exchange:
531                   case nir_intrinsic_image_deref_atomic_comp_swap:
532                   case nir_intrinsic_image_deref_size:
533                   case nir_intrinsic_image_deref_samples:
534                   case nir_intrinsic_load_deref:
535                   case nir_intrinsic_store_deref:
536                      add_var_use_deref(nir_src_as_deref(intr->src[0]), live,
537                                        &derefs, &derefs_size);
538                      break;
539 
540                   default:
541                      /* Nothing to do */
542                      break;
543                   }
544                } else if (instr->type == nir_instr_type_tex) {
545                   nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
546                   int sampler_idx =
547                      nir_tex_instr_src_index(tex_instr,
548                                              nir_tex_src_sampler_deref);
549                   int texture_idx =
550                      nir_tex_instr_src_index(tex_instr,
551                                              nir_tex_src_texture_deref);
552 
553                   if (sampler_idx >= 0) {
554                      nir_deref_instr *deref =
555                         nir_src_as_deref(tex_instr->src[sampler_idx].src);
556                      add_var_use_deref(deref, live, &derefs, &derefs_size);
557                   }
558 
559                   if (texture_idx >= 0) {
560                      nir_deref_instr *deref =
561                         nir_src_as_deref(tex_instr->src[texture_idx].src);
562                      add_var_use_deref(deref, live, &derefs, &derefs_size);
563                   }
564                }
565             }
566          }
567       }
568    }
569 
570    ralloc_free(derefs);
571 }
572 
573 static void
mark_stage_as_active(struct gl_uniform_storage * uniform,unsigned stage)574 mark_stage_as_active(struct gl_uniform_storage *uniform,
575                      unsigned stage)
576 {
577    uniform->active_shader_mask |= 1 << stage;
578 }
579 
580 /* Used to build a tree representing the glsl_type so that we can have a place
581  * to store the next index for opaque types. Array types are expanded so that
582  * they have a single child which is used for all elements of the array.
583  * Struct types have a child for each member. The tree is walked while
584  * processing a uniform so that we can recognise when an opaque type is
585  * encountered a second time in order to reuse the same range of indices that
586  * was reserved the first time. That way the sampler indices can be arranged
587  * so that members of an array are placed sequentially even if the array is an
588  * array of structs containing other opaque members.
589  */
590 struct type_tree_entry {
591    /* For opaque types, this will be the next index to use. If we haven’t
592     * encountered this member yet, it will be UINT_MAX.
593     */
594    unsigned next_index;
595    unsigned array_size;
596    struct type_tree_entry *parent;
597    struct type_tree_entry *next_sibling;
598    struct type_tree_entry *children;
599 };
600 
601 struct nir_link_uniforms_state {
602    /* per-whole program */
603    unsigned num_hidden_uniforms;
604    unsigned num_values;
605    unsigned max_uniform_location;
606 
607    /* per-shader stage */
608    unsigned next_bindless_image_index;
609    unsigned next_bindless_sampler_index;
610    unsigned next_image_index;
611    unsigned next_sampler_index;
612    unsigned next_subroutine;
613    unsigned num_shader_samplers;
614    unsigned num_shader_images;
615    unsigned num_shader_uniform_components;
616    unsigned shader_samplers_used;
617    unsigned shader_shadow_samplers;
618    unsigned shader_storage_blocks_write_access;
619    struct gl_program_parameter_list *params;
620 
621    /* per-variable */
622    nir_variable *current_var;
623    const struct glsl_type *current_ifc_type;
624    int offset;
625    bool var_is_in_block;
626    bool set_top_level_array;
627    int top_level_array_size;
628    int top_level_array_stride;
629 
630    struct type_tree_entry *current_type;
631    struct hash_table *referenced_uniforms[MESA_SHADER_STAGES];
632    struct hash_table *uniform_hash;
633 };
634 
635 static void
add_parameter(struct gl_uniform_storage * uniform,struct gl_context * ctx,struct gl_shader_program * prog,const struct glsl_type * type,struct nir_link_uniforms_state * state)636 add_parameter(struct gl_uniform_storage *uniform,
637               struct gl_context *ctx,
638               struct gl_shader_program *prog,
639               const struct glsl_type *type,
640               struct nir_link_uniforms_state *state)
641 {
642    /* Builtin uniforms are backed by PROGRAM_STATE_VAR, so don't add them as
643     * uniforms.
644     */
645    if (uniform->builtin)
646       return;
647 
648    if (!state->params || uniform->is_shader_storage ||
649        (glsl_contains_opaque(type) && !state->current_var->data.bindless))
650       return;
651 
652    unsigned num_params = glsl_get_aoa_size(type);
653    num_params = MAX2(num_params, 1);
654    num_params *= glsl_get_matrix_columns(glsl_without_array(type));
655 
656    bool is_dual_slot = glsl_type_is_dual_slot(glsl_without_array(type));
657    if (is_dual_slot)
658       num_params *= 2;
659 
660    struct gl_program_parameter_list *params = state->params;
661    int base_index = params->NumParameters;
662    _mesa_reserve_parameter_storage(params, num_params);
663 
664    if (ctx->Const.PackedDriverUniformStorage) {
665       for (unsigned i = 0; i < num_params; i++) {
666          unsigned dmul = glsl_type_is_64bit(glsl_without_array(type)) ? 2 : 1;
667          unsigned comps = glsl_get_vector_elements(glsl_without_array(type)) * dmul;
668          if (is_dual_slot) {
669             if (i & 0x1)
670                comps -= 4;
671             else
672                comps = 4;
673          }
674 
675          _mesa_add_parameter(params, PROGRAM_UNIFORM, uniform->name, comps,
676                              glsl_get_gl_type(type), NULL, NULL, false);
677       }
678    } else {
679       for (unsigned i = 0; i < num_params; i++) {
680          _mesa_add_parameter(params, PROGRAM_UNIFORM, uniform->name, 4,
681                              glsl_get_gl_type(type), NULL, NULL, true);
682       }
683    }
684 
685    /* Each Parameter will hold the index to the backing uniform storage.
686     * This avoids relying on names to match parameters and uniform
687     * storages.
688     */
689    for (unsigned i = 0; i < num_params; i++) {
690       struct gl_program_parameter *param = &params->Parameters[base_index + i];
691       param->UniformStorageIndex = uniform - prog->data->UniformStorage;
692       param->MainUniformStorageIndex = state->current_var->data.location;
693    }
694 }
695 
696 static unsigned
get_next_index(struct nir_link_uniforms_state * state,const struct gl_uniform_storage * uniform,unsigned * next_index,bool * initialised)697 get_next_index(struct nir_link_uniforms_state *state,
698                const struct gl_uniform_storage *uniform,
699                unsigned *next_index, bool *initialised)
700 {
701    /* If we’ve already calculated an index for this member then we can just
702     * offset from there.
703     */
704    if (state->current_type->next_index == UINT_MAX) {
705       /* Otherwise we need to reserve enough indices for all of the arrays
706        * enclosing this member.
707        */
708 
709       unsigned array_size = 1;
710 
711       for (const struct type_tree_entry *p = state->current_type;
712            p;
713            p = p->parent) {
714          array_size *= p->array_size;
715       }
716 
717       state->current_type->next_index = *next_index;
718       *next_index += array_size;
719       *initialised = true;
720    } else
721       *initialised = false;
722 
723    unsigned index = state->current_type->next_index;
724 
725    state->current_type->next_index += MAX2(1, uniform->array_elements);
726 
727    return index;
728 }
729 
730 /* Update the uniforms info for the current shader stage */
731 static void
update_uniforms_shader_info(struct gl_shader_program * prog,struct nir_link_uniforms_state * state,struct gl_uniform_storage * uniform,const struct glsl_type * type,unsigned stage)732 update_uniforms_shader_info(struct gl_shader_program *prog,
733                             struct nir_link_uniforms_state *state,
734                             struct gl_uniform_storage *uniform,
735                             const struct glsl_type *type,
736                             unsigned stage)
737 {
738    unsigned values = glsl_get_component_slots(type);
739    const struct glsl_type *type_no_array = glsl_without_array(type);
740 
741    if (glsl_type_is_sampler(type_no_array)) {
742       bool init_idx;
743       unsigned *next_index = state->current_var->data.bindless ?
744          &state->next_bindless_sampler_index :
745          &state->next_sampler_index;
746       int sampler_index = get_next_index(state, uniform, next_index, &init_idx);
747       struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
748 
749       if (state->current_var->data.bindless) {
750          if (init_idx) {
751             sh->Program->sh.BindlessSamplers =
752                rerzalloc(sh->Program, sh->Program->sh.BindlessSamplers,
753                          struct gl_bindless_sampler,
754                          sh->Program->sh.NumBindlessSamplers,
755                          state->next_bindless_sampler_index);
756 
757             for (unsigned j = sh->Program->sh.NumBindlessSamplers;
758                  j < state->next_bindless_sampler_index; j++) {
759                sh->Program->sh.BindlessSamplers[j].target =
760                   glsl_get_sampler_target(type_no_array);
761             }
762 
763             sh->Program->sh.NumBindlessSamplers =
764                state->next_bindless_sampler_index;
765          }
766 
767          if (!state->var_is_in_block)
768             state->num_shader_uniform_components += values;
769       } else {
770          /* Samplers (bound or bindless) are counted as two components
771           * as specified by ARB_bindless_texture.
772           */
773          state->num_shader_samplers += values / 2;
774 
775          if (init_idx) {
776             const unsigned shadow = glsl_sampler_type_is_shadow(type_no_array);
777             for (unsigned i = sampler_index;
778                  i < MIN2(state->next_sampler_index, MAX_SAMPLERS); i++) {
779                sh->Program->sh.SamplerTargets[i] =
780                   glsl_get_sampler_target(type_no_array);
781                state->shader_samplers_used |= 1U << i;
782                state->shader_shadow_samplers |= shadow << i;
783             }
784          }
785       }
786 
787       uniform->opaque[stage].active = true;
788       uniform->opaque[stage].index = sampler_index;
789    } else if (glsl_type_is_image(type_no_array)) {
790       struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
791 
792       /* Set image access qualifiers */
793       enum gl_access_qualifier image_access =
794          state->current_var->data.access;
795       const GLenum access =
796          (image_access & ACCESS_NON_WRITEABLE) ?
797          ((image_access & ACCESS_NON_READABLE) ? GL_NONE :
798                                                  GL_READ_ONLY) :
799          ((image_access & ACCESS_NON_READABLE) ? GL_WRITE_ONLY :
800                                                  GL_READ_WRITE);
801 
802       int image_index;
803       if (state->current_var->data.bindless) {
804          image_index = state->next_bindless_image_index;
805          state->next_bindless_image_index += MAX2(1, uniform->array_elements);
806 
807          sh->Program->sh.BindlessImages =
808             rerzalloc(sh->Program, sh->Program->sh.BindlessImages,
809                       struct gl_bindless_image,
810                       sh->Program->sh.NumBindlessImages,
811                       state->next_bindless_image_index);
812 
813          for (unsigned j = sh->Program->sh.NumBindlessImages;
814               j < state->next_bindless_image_index; j++) {
815             sh->Program->sh.BindlessImages[j].access = access;
816          }
817 
818          sh->Program->sh.NumBindlessImages = state->next_bindless_image_index;
819 
820       } else {
821          image_index = state->next_image_index;
822          state->next_image_index += MAX2(1, uniform->array_elements);
823 
824          /* Images (bound or bindless) are counted as two components as
825           * specified by ARB_bindless_texture.
826           */
827          state->num_shader_images += values / 2;
828 
829          for (unsigned i = image_index;
830               i < MIN2(state->next_image_index, MAX_IMAGE_UNIFORMS); i++) {
831             sh->Program->sh.ImageAccess[i] = access;
832          }
833       }
834 
835       uniform->opaque[stage].active = true;
836       uniform->opaque[stage].index = image_index;
837 
838       if (!uniform->is_shader_storage)
839          state->num_shader_uniform_components += values;
840    } else {
841       if (glsl_get_base_type(type_no_array) == GLSL_TYPE_SUBROUTINE) {
842          struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
843 
844          uniform->opaque[stage].index = state->next_subroutine;
845          uniform->opaque[stage].active = true;
846 
847          sh->Program->sh.NumSubroutineUniforms++;
848 
849          /* Increment the subroutine index by 1 for non-arrays and by the
850           * number of array elements for arrays.
851           */
852          state->next_subroutine += MAX2(1, uniform->array_elements);
853       }
854 
855       if (!state->var_is_in_block)
856          state->num_shader_uniform_components += values;
857    }
858 }
859 
860 static bool
find_and_update_named_uniform_storage(struct gl_context * ctx,struct gl_shader_program * prog,struct nir_link_uniforms_state * state,nir_variable * var,char ** name,size_t name_length,const struct glsl_type * type,unsigned stage,bool * first_element)861 find_and_update_named_uniform_storage(struct gl_context *ctx,
862                                       struct gl_shader_program *prog,
863                                       struct nir_link_uniforms_state *state,
864                                       nir_variable *var, char **name,
865                                       size_t name_length,
866                                       const struct glsl_type *type,
867                                       unsigned stage, bool *first_element)
868 {
869    /* gl_uniform_storage can cope with one level of array, so if the type is a
870     * composite type or an array where each element occupies more than one
871     * location than we need to recursively process it.
872     */
873    if (glsl_type_is_struct_or_ifc(type) ||
874        (glsl_type_is_array(type) &&
875         (glsl_type_is_array(glsl_get_array_element(type)) ||
876          glsl_type_is_struct_or_ifc(glsl_get_array_element(type))))) {
877 
878       struct type_tree_entry *old_type = state->current_type;
879       state->current_type = old_type->children;
880 
881       /* Shader storage block unsized arrays: add subscript [0] to variable
882        * names.
883        */
884       unsigned length = glsl_get_length(type);
885       if (glsl_type_is_unsized_array(type))
886          length = 1;
887 
888       bool result = false;
889       for (unsigned i = 0; i < length; i++) {
890          const struct glsl_type *field_type;
891          size_t new_length = name_length;
892 
893          if (glsl_type_is_struct_or_ifc(type)) {
894             field_type = glsl_get_struct_field(type, i);
895 
896             /* Append '.field' to the current variable name. */
897             if (name) {
898                ralloc_asprintf_rewrite_tail(name, &new_length, ".%s",
899                                             glsl_get_struct_elem_name(type, i));
900             }
901          } else {
902             field_type = glsl_get_array_element(type);
903 
904             /* Append the subscript to the current variable name */
905             if (name)
906                ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
907          }
908 
909          result = find_and_update_named_uniform_storage(ctx, prog, state,
910                                                         var, name, new_length,
911                                                         field_type, stage,
912                                                         first_element);
913 
914          if (glsl_type_is_struct_or_ifc(type))
915             state->current_type = state->current_type->next_sibling;
916 
917          if (!result) {
918             state->current_type = old_type;
919             return false;
920          }
921       }
922 
923       state->current_type = old_type;
924 
925       return result;
926    } else {
927       struct hash_entry *entry =
928          _mesa_hash_table_search(state->uniform_hash, *name);
929       if (entry) {
930          unsigned i = (unsigned) (intptr_t) entry->data;
931          struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
932 
933          if (*first_element && !state->var_is_in_block) {
934             *first_element = false;
935             var->data.location = uniform - prog->data->UniformStorage;
936          }
937 
938          update_uniforms_shader_info(prog, state, uniform, type, stage);
939 
940          const struct glsl_type *type_no_array = glsl_without_array(type);
941          struct hash_entry *entry = prog->data->spirv ? NULL :
942             _mesa_hash_table_search(state->referenced_uniforms[stage],
943                                     state->current_var->name);
944          if (entry != NULL ||
945              glsl_get_base_type(type_no_array) == GLSL_TYPE_SUBROUTINE ||
946              prog->data->spirv)
947             uniform->active_shader_mask |= 1 << stage;
948 
949          if (!state->var_is_in_block)
950             add_parameter(uniform, ctx, prog, type, state);
951 
952          return true;
953       }
954    }
955 
956    return false;
957 }
958 
959 /**
960  * Finds, returns, and updates the stage info for any uniform in UniformStorage
961  * defined by @var. For GLSL this is done using the name, for SPIR-V in general
962  * is this done using the explicit location, except:
963  *
964  * * UBOs/SSBOs: as they lack explicit location, binding is used to locate
965  *   them. That means that more that one entry at the uniform storage can be
966  *   found. In that case all of them are updated, and the first entry is
967  *   returned, in order to update the location of the nir variable.
968  *
969  * * Special uniforms: like atomic counters. They lack a explicit location,
970  *   so they are skipped. They will be handled and assigned a location later.
971  *
972  */
973 static bool
find_and_update_previous_uniform_storage(struct gl_context * ctx,struct gl_shader_program * prog,struct nir_link_uniforms_state * state,nir_variable * var,char * name,const struct glsl_type * type,unsigned stage)974 find_and_update_previous_uniform_storage(struct gl_context *ctx,
975                                          struct gl_shader_program *prog,
976                                          struct nir_link_uniforms_state *state,
977                                          nir_variable *var, char *name,
978                                          const struct glsl_type *type,
979                                          unsigned stage)
980 {
981    if (!prog->data->spirv) {
982       bool first_element = true;
983       char *name_tmp = ralloc_strdup(NULL, name);
984       bool r = find_and_update_named_uniform_storage(ctx, prog, state, var,
985                                                      &name_tmp,
986                                                      strlen(name_tmp), type,
987                                                      stage, &first_element);
988       ralloc_free(name_tmp);
989 
990       return r;
991    }
992 
993    if (nir_variable_is_in_block(var)) {
994       struct gl_uniform_storage *uniform = NULL;
995 
996       ASSERTED unsigned num_blks = nir_variable_is_in_ubo(var) ?
997          prog->data->NumUniformBlocks :
998          prog->data->NumShaderStorageBlocks;
999 
1000       struct gl_uniform_block *blks = nir_variable_is_in_ubo(var) ?
1001          prog->data->UniformBlocks : prog->data->ShaderStorageBlocks;
1002 
1003       bool result = false;
1004       for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1005          /* UniformStorage contains both variables from ubos and ssbos */
1006          if ( prog->data->UniformStorage[i].is_shader_storage !=
1007               nir_variable_is_in_ssbo(var))
1008             continue;
1009 
1010          int block_index = prog->data->UniformStorage[i].block_index;
1011          if (block_index != -1) {
1012             assert(block_index < num_blks);
1013 
1014             if (var->data.binding == blks[block_index].Binding) {
1015                if (!uniform)
1016                   uniform = &prog->data->UniformStorage[i];
1017                mark_stage_as_active(&prog->data->UniformStorage[i],
1018                                       stage);
1019                result = true;
1020             }
1021          }
1022       }
1023 
1024       if (result)
1025          var->data.location = uniform - prog->data->UniformStorage;
1026       return result;
1027    }
1028 
1029    /* Beyond blocks, there are still some corner cases of uniforms without
1030     * location (ie: atomic counters) that would have a initial location equal
1031     * to -1. We just return on that case. Those uniforms will be handled
1032     * later.
1033     */
1034    if (var->data.location == -1)
1035       return false;
1036 
1037    /* TODO: following search can be problematic with shaders with a lot of
1038     * uniforms. Would it be better to use some type of hash
1039     */
1040    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1041       if (prog->data->UniformStorage[i].remap_location == var->data.location) {
1042          mark_stage_as_active(&prog->data->UniformStorage[i], stage);
1043 
1044          struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
1045          var->data.location = uniform - prog->data->UniformStorage;
1046          add_parameter(uniform, ctx, prog, var->type, state);
1047          return true;
1048       }
1049    }
1050 
1051    return false;
1052 }
1053 
1054 static struct type_tree_entry *
build_type_tree_for_type(const struct glsl_type * type)1055 build_type_tree_for_type(const struct glsl_type *type)
1056 {
1057    struct type_tree_entry *entry = malloc(sizeof *entry);
1058 
1059    entry->array_size = 1;
1060    entry->next_index = UINT_MAX;
1061    entry->children = NULL;
1062    entry->next_sibling = NULL;
1063    entry->parent = NULL;
1064 
1065    if (glsl_type_is_array(type)) {
1066       entry->array_size = glsl_get_length(type);
1067       entry->children = build_type_tree_for_type(glsl_get_array_element(type));
1068       entry->children->parent = entry;
1069    } else if (glsl_type_is_struct_or_ifc(type)) {
1070       struct type_tree_entry *last = NULL;
1071 
1072       for (unsigned i = 0; i < glsl_get_length(type); i++) {
1073          const struct glsl_type *field_type = glsl_get_struct_field(type, i);
1074          struct type_tree_entry *field_entry =
1075             build_type_tree_for_type(field_type);
1076 
1077          if (last == NULL)
1078             entry->children = field_entry;
1079          else
1080             last->next_sibling = field_entry;
1081 
1082          field_entry->parent = entry;
1083 
1084          last = field_entry;
1085       }
1086    }
1087 
1088    return entry;
1089 }
1090 
1091 static void
free_type_tree(struct type_tree_entry * entry)1092 free_type_tree(struct type_tree_entry *entry)
1093 {
1094    struct type_tree_entry *p, *next;
1095 
1096    for (p = entry->children; p; p = next) {
1097       next = p->next_sibling;
1098       free_type_tree(p);
1099    }
1100 
1101    free(entry);
1102 }
1103 
1104 static void
hash_free_uniform_name(struct hash_entry * entry)1105 hash_free_uniform_name(struct hash_entry *entry)
1106 {
1107    free((void*)entry->key);
1108 }
1109 
1110 static void
enter_record(struct nir_link_uniforms_state * state,struct gl_context * ctx,const struct glsl_type * type,bool row_major)1111 enter_record(struct nir_link_uniforms_state *state,
1112              struct gl_context *ctx,
1113              const struct glsl_type *type,
1114              bool row_major)
1115 {
1116    assert(glsl_type_is_struct(type));
1117    if (!state->var_is_in_block)
1118       return;
1119 
1120    bool use_std430 = ctx->Const.UseSTD430AsDefaultPacking;
1121    const enum glsl_interface_packing packing =
1122       glsl_get_internal_ifc_packing(state->current_var->interface_type,
1123                                     use_std430);
1124 
1125    if (packing == GLSL_INTERFACE_PACKING_STD430)
1126       state->offset = glsl_align(
1127          state->offset, glsl_get_std430_base_alignment(type, row_major));
1128    else
1129       state->offset = glsl_align(
1130          state->offset, glsl_get_std140_base_alignment(type, row_major));
1131 }
1132 
1133 static void
leave_record(struct nir_link_uniforms_state * state,struct gl_context * ctx,const struct glsl_type * type,bool row_major)1134 leave_record(struct nir_link_uniforms_state *state,
1135              struct gl_context *ctx,
1136              const struct glsl_type *type,
1137              bool row_major)
1138 {
1139    assert(glsl_type_is_struct(type));
1140    if (!state->var_is_in_block)
1141       return;
1142 
1143    bool use_std430 = ctx->Const.UseSTD430AsDefaultPacking;
1144    const enum glsl_interface_packing packing =
1145       glsl_get_internal_ifc_packing(state->current_var->interface_type,
1146                                     use_std430);
1147 
1148    if (packing == GLSL_INTERFACE_PACKING_STD430)
1149       state->offset = glsl_align(
1150          state->offset, glsl_get_std430_base_alignment(type, row_major));
1151    else
1152       state->offset = glsl_align(
1153          state->offset, glsl_get_std140_base_alignment(type, row_major));
1154 }
1155 
1156 /**
1157  * Creates the neccessary entries in UniformStorage for the uniform. Returns
1158  * the number of locations used or -1 on failure.
1159  */
1160 static int
nir_link_uniform(struct gl_context * ctx,struct gl_shader_program * prog,struct gl_program * stage_program,gl_shader_stage stage,const struct glsl_type * type,unsigned index_in_parent,int location,struct nir_link_uniforms_state * state,char ** name,size_t name_length,bool row_major)1161 nir_link_uniform(struct gl_context *ctx,
1162                  struct gl_shader_program *prog,
1163                  struct gl_program *stage_program,
1164                  gl_shader_stage stage,
1165                  const struct glsl_type *type,
1166                  unsigned index_in_parent,
1167                  int location,
1168                  struct nir_link_uniforms_state *state,
1169                  char **name, size_t name_length, bool row_major)
1170 {
1171    struct gl_uniform_storage *uniform = NULL;
1172 
1173    if (state->set_top_level_array &&
1174        nir_variable_is_in_ssbo(state->current_var)) {
1175       /* Type is the top level SSBO member */
1176       if (glsl_type_is_array(type) &&
1177           (glsl_type_is_array(glsl_get_array_element(type)) ||
1178            glsl_type_is_struct_or_ifc(glsl_get_array_element(type)))) {
1179          /* Type is a top-level array (array of aggregate types) */
1180          state->top_level_array_size = glsl_get_length(type);
1181          state->top_level_array_stride = glsl_get_explicit_stride(type);
1182       } else {
1183          state->top_level_array_size = 1;
1184          state->top_level_array_stride = 0;
1185       }
1186 
1187       state->set_top_level_array = false;
1188    }
1189 
1190    /* gl_uniform_storage can cope with one level of array, so if the type is a
1191     * composite type or an array where each element occupies more than one
1192     * location than we need to recursively process it.
1193     */
1194    if (glsl_type_is_struct_or_ifc(type) ||
1195        (glsl_type_is_array(type) &&
1196         (glsl_type_is_array(glsl_get_array_element(type)) ||
1197          glsl_type_is_struct_or_ifc(glsl_get_array_element(type))))) {
1198       int location_count = 0;
1199       struct type_tree_entry *old_type = state->current_type;
1200       unsigned int struct_base_offset = state->offset;
1201 
1202       state->current_type = old_type->children;
1203 
1204       /* Shader storage block unsized arrays: add subscript [0] to variable
1205        * names.
1206        */
1207       unsigned length = glsl_get_length(type);
1208       if (glsl_type_is_unsized_array(type))
1209          length = 1;
1210 
1211       if (glsl_type_is_struct(type) && !prog->data->spirv)
1212          enter_record(state, ctx, type, row_major);
1213 
1214       for (unsigned i = 0; i < length; i++) {
1215          const struct glsl_type *field_type;
1216          size_t new_length = name_length;
1217          bool field_row_major = row_major;
1218 
1219          if (glsl_type_is_struct_or_ifc(type)) {
1220             field_type = glsl_get_struct_field(type, i);
1221             /* Use the offset inside the struct only for variables backed by
1222              * a buffer object. For variables not backed by a buffer object,
1223              * offset is -1.
1224              */
1225             if (state->var_is_in_block) {
1226                if (prog->data->spirv) {
1227                   state->offset =
1228                      struct_base_offset + glsl_get_struct_field_offset(type, i);
1229                } else if (glsl_get_struct_field_offset(type, i) != -1 &&
1230                           type == state->current_ifc_type) {
1231                   state->offset = glsl_get_struct_field_offset(type, i);
1232                }
1233 
1234                if (glsl_type_is_interface(type))
1235                   state->set_top_level_array = true;
1236             }
1237 
1238             /* Append '.field' to the current variable name. */
1239             if (name) {
1240                ralloc_asprintf_rewrite_tail(name, &new_length, ".%s",
1241                                             glsl_get_struct_elem_name(type, i));
1242             }
1243 
1244 
1245             /* The layout of structures at the top level of the block is set
1246              * during parsing.  For matrices contained in multiple levels of
1247              * structures in the block, the inner structures have no layout.
1248              * These cases must potentially inherit the layout from the outer
1249              * levels.
1250              */
1251             const enum glsl_matrix_layout matrix_layout =
1252                glsl_get_struct_field_data(type, i)->matrix_layout;
1253             if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1254                field_row_major = true;
1255             } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1256                field_row_major = false;
1257             }
1258          } else {
1259             field_type = glsl_get_array_element(type);
1260 
1261             /* Append the subscript to the current variable name */
1262             if (name)
1263                ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
1264          }
1265 
1266          int entries = nir_link_uniform(ctx, prog, stage_program, stage,
1267                                         field_type, i, location,
1268                                         state, name, new_length,
1269                                         field_row_major);
1270 
1271          if (entries == -1)
1272             return -1;
1273 
1274          if (location != -1)
1275             location += entries;
1276          location_count += entries;
1277 
1278          if (glsl_type_is_struct_or_ifc(type))
1279             state->current_type = state->current_type->next_sibling;
1280       }
1281 
1282       if (glsl_type_is_struct(type) && !prog->data->spirv)
1283          leave_record(state, ctx, type, row_major);
1284 
1285       state->current_type = old_type;
1286 
1287       return location_count;
1288    } else {
1289       /* TODO: reallocating storage is slow, we should figure out a way to
1290        * allocate storage up front for spirv like we do for GLSL.
1291        */
1292       if (prog->data->spirv) {
1293          /* Create a new uniform storage entry */
1294          prog->data->UniformStorage =
1295             reralloc(prog->data,
1296                      prog->data->UniformStorage,
1297                      struct gl_uniform_storage,
1298                      prog->data->NumUniformStorage + 1);
1299          if (!prog->data->UniformStorage) {
1300             linker_error(prog, "Out of memory during linking.\n");
1301             return -1;
1302          }
1303       }
1304 
1305       uniform = &prog->data->UniformStorage[prog->data->NumUniformStorage];
1306       prog->data->NumUniformStorage++;
1307 
1308       /* Initialize its members */
1309       memset(uniform, 0x00, sizeof(struct gl_uniform_storage));
1310 
1311       uniform->name =
1312          name ? ralloc_strdup(prog->data->UniformStorage, *name) : NULL;
1313 
1314       const struct glsl_type *type_no_array = glsl_without_array(type);
1315       if (glsl_type_is_array(type)) {
1316          uniform->type = type_no_array;
1317          uniform->array_elements = glsl_get_length(type);
1318       } else {
1319          uniform->type = type;
1320          uniform->array_elements = 0;
1321       }
1322       uniform->top_level_array_size = state->top_level_array_size;
1323       uniform->top_level_array_stride = state->top_level_array_stride;
1324 
1325       struct hash_entry *entry = prog->data->spirv ? NULL :
1326          _mesa_hash_table_search(state->referenced_uniforms[stage],
1327                                  state->current_var->name);
1328       if (entry != NULL ||
1329           glsl_get_base_type(type_no_array) == GLSL_TYPE_SUBROUTINE ||
1330           prog->data->spirv)
1331          uniform->active_shader_mask |= 1 << stage;
1332 
1333       if (location >= 0) {
1334          /* Uniform has an explicit location */
1335          uniform->remap_location = location;
1336       } else {
1337          uniform->remap_location = UNMAPPED_UNIFORM_LOC;
1338       }
1339 
1340       uniform->hidden = state->current_var->data.how_declared == nir_var_hidden;
1341       if (uniform->hidden)
1342          state->num_hidden_uniforms++;
1343 
1344       uniform->is_shader_storage = nir_variable_is_in_ssbo(state->current_var);
1345       uniform->is_bindless = state->current_var->data.bindless;
1346 
1347       /* Set fields whose default value depend on the variable being inside a
1348        * block.
1349        *
1350        * From the OpenGL 4.6 spec, 7.3 Program objects:
1351        *
1352        * "For the property ARRAY_STRIDE, ... For active variables not declared
1353        * as an array of basic types, zero is written to params. For active
1354        * variables not backed by a buffer object, -1 is written to params,
1355        * regardless of the variable type."
1356        *
1357        * "For the property MATRIX_STRIDE, ... For active variables not declared
1358        * as a matrix or array of matrices, zero is written to params. For active
1359        * variables not backed by a buffer object, -1 is written to params,
1360        * regardless of the variable type."
1361        *
1362        * For the property IS_ROW_MAJOR, ... For active variables backed by a
1363        * buffer object, declared as a single matrix or array of matrices, and
1364        * stored in row-major order, one is written to params. For all other
1365        * active variables, zero is written to params.
1366        */
1367       uniform->array_stride = -1;
1368       uniform->matrix_stride = -1;
1369       uniform->row_major = false;
1370 
1371       if (state->var_is_in_block) {
1372          uniform->array_stride = glsl_type_is_array(type) ?
1373             glsl_get_explicit_stride(type) : 0;
1374 
1375          if (glsl_type_is_matrix(uniform->type)) {
1376             uniform->matrix_stride = glsl_get_explicit_stride(uniform->type);
1377             uniform->row_major = glsl_matrix_type_is_row_major(uniform->type);
1378          } else {
1379             uniform->matrix_stride = 0;
1380          }
1381 
1382          if (!prog->data->spirv) {
1383             bool use_std430 = ctx->Const.UseSTD430AsDefaultPacking;
1384             const enum glsl_interface_packing packing =
1385                glsl_get_internal_ifc_packing(state->current_var->interface_type,
1386                                              use_std430);
1387 
1388             unsigned alignment =
1389                glsl_get_std140_base_alignment(type, uniform->row_major);
1390             if (packing == GLSL_INTERFACE_PACKING_STD430) {
1391                alignment =
1392                   glsl_get_std430_base_alignment(type, uniform->row_major);
1393             }
1394             state->offset = glsl_align(state->offset, alignment);
1395          }
1396       }
1397 
1398       uniform->offset = state->var_is_in_block ? state->offset : -1;
1399 
1400       int buffer_block_index = -1;
1401       /* If the uniform is inside a uniform block determine its block index by
1402        * comparing the bindings, we can not use names.
1403        */
1404       if (state->var_is_in_block) {
1405          struct gl_uniform_block *blocks = nir_variable_is_in_ssbo(state->current_var) ?
1406             prog->data->ShaderStorageBlocks : prog->data->UniformBlocks;
1407 
1408          int num_blocks = nir_variable_is_in_ssbo(state->current_var) ?
1409             prog->data->NumShaderStorageBlocks : prog->data->NumUniformBlocks;
1410 
1411          if (!prog->data->spirv) {
1412             bool is_interface_array =
1413                glsl_without_array(state->current_var->type) == state->current_var->interface_type &&
1414                glsl_type_is_array(state->current_var->type);
1415 
1416             const char *ifc_name =
1417                glsl_get_type_name(state->current_var->interface_type);
1418             if (is_interface_array) {
1419                unsigned l = strlen(ifc_name);
1420                for (unsigned i = 0; i < num_blocks; i++) {
1421                   if (strncmp(ifc_name, blocks[i].Name, l) == 0 &&
1422                       blocks[i].Name[l] == '[') {
1423                      buffer_block_index = i;
1424                      break;
1425                   }
1426                }
1427             } else {
1428                for (unsigned i = 0; i < num_blocks; i++) {
1429                   if (strcmp(ifc_name, blocks[i].Name) == 0) {
1430                      buffer_block_index = i;
1431                      break;
1432                   }
1433                }
1434             }
1435 
1436             /* Compute the next offset. */
1437             bool use_std430 = ctx->Const.UseSTD430AsDefaultPacking;
1438             const enum glsl_interface_packing packing =
1439                glsl_get_internal_ifc_packing(state->current_var->interface_type,
1440                                              use_std430);
1441             if (packing == GLSL_INTERFACE_PACKING_STD430)
1442                state->offset += glsl_get_std430_size(type, uniform->row_major);
1443             else
1444                state->offset += glsl_get_std140_size(type, uniform->row_major);
1445          } else {
1446             for (unsigned i = 0; i < num_blocks; i++) {
1447                if (state->current_var->data.binding == blocks[i].Binding) {
1448                   buffer_block_index = i;
1449                   break;
1450                }
1451             }
1452 
1453             /* Compute the next offset. */
1454             state->offset += glsl_get_explicit_size(type, true);
1455          }
1456          assert(buffer_block_index >= 0);
1457       }
1458 
1459       uniform->block_index = buffer_block_index;
1460       uniform->builtin = is_gl_identifier(uniform->name);
1461       uniform->atomic_buffer_index = -1;
1462 
1463       /* The following are not for features not supported by ARB_gl_spirv */
1464       uniform->num_compatible_subroutines = 0;
1465 
1466       unsigned entries = MAX2(1, uniform->array_elements);
1467       unsigned values = glsl_get_component_slots(type);
1468 
1469       update_uniforms_shader_info(prog, state, uniform, type, stage);
1470 
1471       if (uniform->remap_location != UNMAPPED_UNIFORM_LOC &&
1472           state->max_uniform_location < uniform->remap_location + entries)
1473          state->max_uniform_location = uniform->remap_location + entries;
1474 
1475       if (!state->var_is_in_block)
1476          add_parameter(uniform, ctx, prog, type, state);
1477 
1478       if (name) {
1479          _mesa_hash_table_insert(state->uniform_hash, strdup(*name),
1480                                  (void *) (intptr_t)
1481                                     (prog->data->NumUniformStorage - 1));
1482       }
1483 
1484       if (!is_gl_identifier(uniform->name) && !uniform->is_shader_storage &&
1485           !state->var_is_in_block)
1486          state->num_values += values;
1487 
1488       return MAX2(uniform->array_elements, 1);
1489    }
1490 }
1491 
1492 bool
gl_nir_link_uniforms(struct gl_context * ctx,struct gl_shader_program * prog,bool fill_parameters)1493 gl_nir_link_uniforms(struct gl_context *ctx,
1494                      struct gl_shader_program *prog,
1495                      bool fill_parameters)
1496 {
1497    /* First free up any previous UniformStorage items */
1498    ralloc_free(prog->data->UniformStorage);
1499    prog->data->UniformStorage = NULL;
1500    prog->data->NumUniformStorage = 0;
1501 
1502    /* Iterate through all linked shaders */
1503    struct nir_link_uniforms_state state = {0,};
1504 
1505    if (!prog->data->spirv) {
1506       /* Gather information on uniform use */
1507       for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
1508          struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
1509          if (!sh)
1510             continue;
1511 
1512          state.referenced_uniforms[stage] =
1513             _mesa_hash_table_create(NULL, _mesa_hash_string,
1514                                     _mesa_key_string_equal);
1515 
1516          nir_shader *nir = sh->Program->nir;
1517          add_var_use_shader(nir, state.referenced_uniforms[stage]);
1518       }
1519 
1520       /* Resize uniform arrays based on the maximum array index */
1521       for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
1522          struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
1523          if (!sh)
1524             continue;
1525 
1526          nir_foreach_gl_uniform_variable(var, sh->Program->nir)
1527             update_array_sizes(prog, var, state.referenced_uniforms, stage);
1528       }
1529    }
1530 
1531    /* Count total number of uniforms and allocate storage */
1532    unsigned storage_size = 0;
1533    if (!prog->data->spirv) {
1534       struct set *storage_counted =
1535          _mesa_set_create(NULL, _mesa_hash_string, _mesa_key_string_equal);
1536       for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
1537          struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
1538          if (!sh)
1539             continue;
1540 
1541          nir_foreach_gl_uniform_variable(var, sh->Program->nir) {
1542             const struct glsl_type *type = var->type;
1543             const char *name = var->name;
1544             if (nir_variable_is_in_block(var) &&
1545                 glsl_without_array(type) == var->interface_type) {
1546                type = glsl_without_array(var->type);
1547                name = glsl_get_type_name(type);
1548             }
1549 
1550             struct set_entry *entry = _mesa_set_search(storage_counted, name);
1551             if (!entry) {
1552                storage_size += uniform_storage_size(type);
1553                _mesa_set_add(storage_counted, name);
1554             }
1555          }
1556       }
1557       _mesa_set_destroy(storage_counted, NULL);
1558 
1559       prog->data->UniformStorage = rzalloc_array(prog->data,
1560                                                  struct gl_uniform_storage,
1561                                                  storage_size);
1562       if (!prog->data->UniformStorage) {
1563          linker_error(prog, "Out of memory while linking uniforms.\n");
1564          return false;
1565       }
1566    }
1567 
1568    /* Iterate through all linked shaders */
1569    state.uniform_hash = _mesa_hash_table_create(NULL, _mesa_hash_string,
1570                                                 _mesa_key_string_equal);
1571 
1572    for (unsigned shader_type = 0; shader_type < MESA_SHADER_STAGES; shader_type++) {
1573       struct gl_linked_shader *sh = prog->_LinkedShaders[shader_type];
1574       if (!sh)
1575          continue;
1576 
1577       nir_shader *nir = sh->Program->nir;
1578       assert(nir);
1579 
1580       state.next_bindless_image_index = 0;
1581       state.next_bindless_sampler_index = 0;
1582       state.next_image_index = 0;
1583       state.next_sampler_index = 0;
1584       state.num_shader_samplers = 0;
1585       state.num_shader_images = 0;
1586       state.num_shader_uniform_components = 0;
1587       state.shader_storage_blocks_write_access = 0;
1588       state.shader_samplers_used = 0;
1589       state.shader_shadow_samplers = 0;
1590       state.params = fill_parameters ? sh->Program->Parameters : NULL;
1591 
1592       nir_foreach_gl_uniform_variable(var, nir) {
1593          state.current_var = var;
1594          state.current_ifc_type = NULL;
1595          state.offset = 0;
1596          state.var_is_in_block = nir_variable_is_in_block(var);
1597          state.set_top_level_array = false;
1598          state.top_level_array_size = 0;
1599          state.top_level_array_stride = 0;
1600 
1601          /*
1602           * From ARB_program_interface spec, issue (16):
1603           *
1604           * "RESOLVED: We will follow the default rule for enumerating block
1605           *  members in the OpenGL API, which is:
1606           *
1607           *  * If a variable is a member of an interface block without an
1608           *    instance name, it is enumerated using just the variable name.
1609           *
1610           *  * If a variable is a member of an interface block with an
1611           *    instance name, it is enumerated as "BlockName.Member", where
1612           *    "BlockName" is the name of the interface block (not the
1613           *    instance name) and "Member" is the name of the variable.
1614           *
1615           * For example, in the following code:
1616           *
1617           * uniform Block1 {
1618           *   int member1;
1619           * };
1620           * uniform Block2 {
1621           *   int member2;
1622           * } instance2;
1623           * uniform Block3 {
1624           *  int member3;
1625           * } instance3[2];  // uses two separate buffer bindings
1626           *
1627           * the three uniforms (if active) are enumerated as "member1",
1628           * "Block2.member2", and "Block3.member3"."
1629           *
1630           * Note that in the last example, with an array of ubo, only one
1631           * uniform is generated. For that reason, while unrolling the
1632           * uniforms of a ubo, or the variables of a ssbo, we need to treat
1633           * arrays of instance as a single block.
1634           */
1635          char *name;
1636          const struct glsl_type *type = var->type;
1637          if (state.var_is_in_block &&
1638              ((!prog->data->spirv && glsl_without_array(type) == var->interface_type) ||
1639               (prog->data->spirv && type == var->interface_type))) {
1640             type = glsl_without_array(var->type);
1641             state.current_ifc_type = type;
1642             name = ralloc_strdup(NULL, glsl_get_type_name(type));
1643          } else {
1644             state.set_top_level_array = true;
1645             name = ralloc_strdup(NULL, var->name);
1646          }
1647 
1648          struct type_tree_entry *type_tree =
1649             build_type_tree_for_type(type);
1650          state.current_type = type_tree;
1651 
1652          int location = var->data.location;
1653 
1654          struct gl_uniform_block *blocks;
1655          int num_blocks;
1656          int buffer_block_index = -1;
1657          if (!prog->data->spirv && state.var_is_in_block) {
1658             /* If the uniform is inside a uniform block determine its block index by
1659              * comparing the bindings, we can not use names.
1660              */
1661             blocks = nir_variable_is_in_ssbo(state.current_var) ?
1662                prog->data->ShaderStorageBlocks : prog->data->UniformBlocks;
1663             num_blocks = nir_variable_is_in_ssbo(state.current_var) ?
1664                prog->data->NumShaderStorageBlocks : prog->data->NumUniformBlocks;
1665 
1666             bool is_interface_array =
1667                glsl_without_array(state.current_var->type) == state.current_var->interface_type &&
1668                glsl_type_is_array(state.current_var->type);
1669 
1670             const char *ifc_name =
1671                glsl_get_type_name(state.current_var->interface_type);
1672 
1673             if (is_interface_array) {
1674                unsigned l = strlen(ifc_name);
1675 
1676                /* Even when a match is found, do not "break" here.  As this is
1677                 * an array of instances, all elements of the array need to be
1678                 * marked as referenced.
1679                 */
1680                for (unsigned i = 0; i < num_blocks; i++) {
1681                   if (strncmp(ifc_name, blocks[i].Name, l) == 0 &&
1682                       blocks[i].Name[l] == '[') {
1683                      if (buffer_block_index == -1)
1684                         buffer_block_index = i;
1685 
1686                      struct hash_entry *entry =
1687                         _mesa_hash_table_search(state.referenced_uniforms[shader_type],
1688                                                 var->name);
1689                      if (entry) {
1690                         struct uniform_array_info *ainfo =
1691                            (struct uniform_array_info *) entry->data;
1692                         if (BITSET_TEST(ainfo->indices, blocks[i].linearized_array_index))
1693                            blocks[i].stageref |= 1U << shader_type;
1694                      }
1695                   }
1696                }
1697             } else {
1698                for (unsigned i = 0; i < num_blocks; i++) {
1699                   if (strcmp(ifc_name, blocks[i].Name) == 0) {
1700                      buffer_block_index = i;
1701 
1702                      struct hash_entry *entry =
1703                         _mesa_hash_table_search(state.referenced_uniforms[shader_type],
1704                                                 var->name);
1705                      if (entry)
1706                         blocks[i].stageref |= 1U << shader_type;
1707 
1708                      break;
1709                   }
1710                }
1711             }
1712 
1713             if (nir_variable_is_in_ssbo(var) &&
1714                 !(var->data.access & ACCESS_NON_WRITEABLE)) {
1715                unsigned array_size = is_interface_array ?
1716                   glsl_get_length(var->type) : 1;
1717 
1718                STATIC_ASSERT(MAX_SHADER_STORAGE_BUFFERS <= 32);
1719 
1720                /* Shaders that use too many SSBOs will fail to compile, which
1721                 * we don't care about.
1722                 *
1723                 * This is true for shaders that do not use too many SSBOs:
1724                 */
1725                if (buffer_block_index + array_size <= 32) {
1726                   state.shader_storage_blocks_write_access |=
1727                      u_bit_consecutive(buffer_block_index, array_size);
1728                }
1729             }
1730          }
1731 
1732          if (!prog->data->spirv && state.var_is_in_block) {
1733             if (glsl_without_array(state.current_var->type) != state.current_var->interface_type) {
1734                /* this is nested at some offset inside the block */
1735                bool found = false;
1736                char sentinel = '\0';
1737 
1738                if (glsl_type_is_struct(state.current_var->type)) {
1739                   sentinel = '.';
1740                } else if (glsl_type_is_array(state.current_var->type) &&
1741                           (glsl_type_is_array(glsl_get_array_element(state.current_var->type))
1742                            || glsl_type_is_struct(glsl_without_array(state.current_var->type)))) {
1743                  sentinel = '[';
1744                }
1745 
1746                const unsigned l = strlen(state.current_var->name);
1747                for (unsigned i = 0; i < num_blocks; i++) {
1748                   for (unsigned j = 0; j < blocks[i].NumUniforms; j++) {
1749                     if (sentinel) {
1750                         const char *begin = blocks[i].Uniforms[j].Name;
1751                         const char *end = strchr(begin, sentinel);
1752 
1753                         if (end == NULL)
1754                            continue;
1755 
1756                         if ((ptrdiff_t) l != (end - begin))
1757                            continue;
1758                         found = strncmp(state.current_var->name, begin, l) == 0;
1759                      } else {
1760                         found = strcmp(state.current_var->name, blocks[i].Uniforms[j].Name) == 0;
1761                      }
1762 
1763                      if (found) {
1764                         location = j;
1765 
1766                         struct hash_entry *entry =
1767                            _mesa_hash_table_search(state.referenced_uniforms[shader_type], var->name);
1768                         if (entry)
1769                            blocks[i].stageref |= 1U << shader_type;
1770 
1771                         break;
1772                      }
1773                   }
1774 
1775                   if (found)
1776                      break;
1777                }
1778                assert(found);
1779                var->data.location = location;
1780             } else {
1781                /* this is the base block offset */
1782                var->data.location = buffer_block_index;
1783                location = 0;
1784             }
1785             assert(buffer_block_index >= 0);
1786             const struct gl_uniform_block *const block =
1787                &blocks[buffer_block_index];
1788             assert(location >= 0 && location < block->NumUniforms);
1789 
1790             const struct gl_uniform_buffer_variable *const ubo_var =
1791                &block->Uniforms[location];
1792 
1793             state.offset = ubo_var->Offset;
1794          }
1795 
1796          /* Check if the uniform has been processed already for
1797           * other stage. If so, validate they are compatible and update
1798           * the active stage mask.
1799           */
1800          if (find_and_update_previous_uniform_storage(ctx, prog, &state, var,
1801                                                       name, type, shader_type)) {
1802             ralloc_free(name);
1803             free_type_tree(type_tree);
1804             continue;
1805          }
1806 
1807          /* From now on the variable’s location will be its uniform index */
1808          if (!state.var_is_in_block)
1809             var->data.location = prog->data->NumUniformStorage;
1810          else
1811             location = -1;
1812 
1813          bool row_major =
1814             var->data.matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR;
1815          int res = nir_link_uniform(ctx, prog, sh->Program, shader_type, type,
1816                                     0, location,
1817                                     &state,
1818                                     !prog->data->spirv ? &name : NULL,
1819                                     !prog->data->spirv ? strlen(name) : 0,
1820                                     row_major);
1821 
1822          free_type_tree(type_tree);
1823          ralloc_free(name);
1824 
1825          if (res == -1)
1826             return false;
1827       }
1828 
1829       if (!prog->data->spirv) {
1830          _mesa_hash_table_destroy(state.referenced_uniforms[shader_type],
1831                                   NULL);
1832       }
1833 
1834       if (state.num_shader_samplers >
1835           ctx->Const.Program[shader_type].MaxTextureImageUnits) {
1836          linker_error(prog, "Too many %s shader texture samplers\n",
1837                       _mesa_shader_stage_to_string(shader_type));
1838          continue;
1839       }
1840 
1841       if (state.num_shader_images >
1842           ctx->Const.Program[shader_type].MaxImageUniforms) {
1843          linker_error(prog, "Too many %s shader image uniforms (%u > %u)\n",
1844                       _mesa_shader_stage_to_string(shader_type),
1845                       state.num_shader_images,
1846                       ctx->Const.Program[shader_type].MaxImageUniforms);
1847          continue;
1848       }
1849 
1850       sh->Program->SamplersUsed = state.shader_samplers_used;
1851       sh->Program->sh.ShaderStorageBlocksWriteAccess =
1852          state.shader_storage_blocks_write_access;
1853       sh->shadow_samplers = state.shader_shadow_samplers;
1854       sh->Program->info.num_textures = state.num_shader_samplers;
1855       sh->Program->info.num_images = state.num_shader_images;
1856       sh->num_uniform_components = state.num_shader_uniform_components;
1857       sh->num_combined_uniform_components = sh->num_uniform_components;
1858    }
1859 
1860    prog->data->NumHiddenUniforms = state.num_hidden_uniforms;
1861    prog->data->NumUniformDataSlots = state.num_values;
1862 
1863    assert(prog->data->spirv || prog->data->NumUniformStorage == storage_size);
1864 
1865    if (prog->data->spirv)
1866       prog->NumUniformRemapTable = state.max_uniform_location;
1867 
1868    nir_setup_uniform_remap_tables(ctx, prog);
1869    gl_nir_set_uniform_initializers(ctx, prog);
1870 
1871    _mesa_hash_table_destroy(state.uniform_hash, hash_free_uniform_name);
1872 
1873    return true;
1874 }
1875