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