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