glsl: set ShaderStorageBlocksWriteAccess 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 "gl_nir_linker.h"
26 #include "compiler/glsl/ir_uniform.h" /* for gl_uniform_storage */
27 #include "linker_util.h"
28 #include "main/context.h"
29 #include "main/mtypes.h"
30
31 /**
32 * This file do the common link for GLSL uniforms, using NIR, instead of IR as
33 * the counter-part glsl/link_uniforms.cpp
34 */
35
36 #define UNMAPPED_UNIFORM_LOC ~0u
37
38 static void
39 nir_setup_uniform_remap_tables(struct gl_context *ctx,
40 struct gl_shader_program *prog)
41 {
42 prog->UniformRemapTable = rzalloc_array(prog,
43 struct gl_uniform_storage *,
44 prog->NumUniformRemapTable);
45 union gl_constant_value *data =
46 rzalloc_array(prog->data,
47 union gl_constant_value, prog->data->NumUniformDataSlots);
48 if (!prog->UniformRemapTable || !data) {
49 linker_error(prog, "Out of memory during linking.\n");
50 return;
51 }
52 prog->data->UniformDataSlots = data;
53
54 prog->data->UniformDataDefaults =
55 rzalloc_array(prog->data->UniformDataSlots,
56 union gl_constant_value, prog->data->NumUniformDataSlots);
57
58 unsigned data_pos = 0;
59
60 /* Reserve all the explicit locations of the active uniforms. */
61 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
62 struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
63
64 if (prog->data->UniformStorage[i].remap_location == UNMAPPED_UNIFORM_LOC)
65 continue;
66
67 /* How many new entries for this uniform? */
68 const unsigned entries = MAX2(1, uniform->array_elements);
69 unsigned num_slots = glsl_get_component_slots(uniform->type);
70
71 uniform->storage = &data[data_pos];
72
73 /* Set remap table entries point to correct gl_uniform_storage. */
74 for (unsigned j = 0; j < entries; j++) {
75 unsigned element_loc = uniform->remap_location + j;
76 prog->UniformRemapTable[element_loc] = uniform;
77
78 data_pos += num_slots;
79 }
80 }
81
82 /* Reserve locations for rest of the uniforms. */
83 link_util_update_empty_uniform_locations(prog);
84
85 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
86 struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
87
88 if (uniform->is_shader_storage)
89 continue;
90
91 /* Built-in uniforms should not get any location. */
92 if (uniform->builtin)
93 continue;
94
95 /* Explicit ones have been set already. */
96 if (uniform->remap_location != UNMAPPED_UNIFORM_LOC)
97 continue;
98
99 /* How many entries for this uniform? */
100 const unsigned entries = MAX2(1, uniform->array_elements);
101
102 unsigned location =
103 link_util_find_empty_block(prog, &prog->data->UniformStorage[i]);
104
105 if (location == -1 || location + entries >= prog->NumUniformRemapTable) {
106 unsigned new_entries = entries;
107 if (location == -1)
108 location = prog->NumUniformRemapTable;
109 else
110 new_entries = location - prog->NumUniformRemapTable + entries;
111
112 /* resize remap table to fit new entries */
113 prog->UniformRemapTable =
114 reralloc(prog,
115 prog->UniformRemapTable,
116 struct gl_uniform_storage *,
117 prog->NumUniformRemapTable + new_entries);
118 prog->NumUniformRemapTable += new_entries;
119 }
120
121 /* set the base location in remap table for the uniform */
122 uniform->remap_location = location;
123
124 unsigned num_slots = glsl_get_component_slots(uniform->type);
125
126 uniform->storage = &data[data_pos];
127
128 /* Set remap table entries point to correct gl_uniform_storage. */
129 for (unsigned j = 0; j < entries; j++) {
130 unsigned element_loc = uniform->remap_location + j;
131 prog->UniformRemapTable[element_loc] = uniform;
132
133 data_pos += num_slots;
134 }
135 }
136 }
137
138 static void
139 mark_stage_as_active(struct gl_uniform_storage *uniform,
140 unsigned stage)
141 {
142 uniform->active_shader_mask |= 1 << stage;
143 }
144
145 /* Used to build a tree representing the glsl_type so that we can have a place
146 * to store the next index for opaque types. Array types are expanded so that
147 * they have a single child which is used for all elements of the array.
148 * Struct types have a child for each member. The tree is walked while
149 * processing a uniform so that we can recognise when an opaque type is
150 * encountered a second time in order to reuse the same range of indices that
151 * was reserved the first time. That way the sampler indices can be arranged
152 * so that members of an array are placed sequentially even if the array is an
153 * array of structs containing other opaque members.
154 */
155 struct type_tree_entry {
156 /* For opaque types, this will be the next index to use. If we haven’t
157 * encountered this member yet, it will be UINT_MAX.
158 */
159 unsigned next_index;
160 unsigned array_size;
161 struct type_tree_entry *parent;
162 struct type_tree_entry *next_sibling;
163 struct type_tree_entry *children;
164 };
165
166 struct nir_link_uniforms_state {
167 /* per-whole program */
168 unsigned num_hidden_uniforms;
169 unsigned num_values;
170 unsigned max_uniform_location;
171
172 /* per-shader stage */
173 unsigned next_image_index;
174 unsigned next_sampler_index;
175 unsigned num_shader_samplers;
176 unsigned num_shader_images;
177 unsigned num_shader_uniform_components;
178 unsigned shader_samplers_used;
179 unsigned shader_shadow_samplers;
180 unsigned shader_storage_blocks_write_access;
181 struct gl_program_parameter_list *params;
182
183 /* per-variable */
184 nir_variable *current_var;
185 const struct glsl_type *current_ifc_type;
186 int offset;
187 bool var_is_in_block;
188 bool set_top_level_array;
189 int top_level_array_size;
190 int top_level_array_stride;
191
192 struct type_tree_entry *current_type;
193 struct hash_table *uniform_hash;
194 };
195
196 static void
197 add_parameter(struct gl_uniform_storage *uniform,
198 struct gl_context *ctx,
199 struct gl_shader_program *prog,
200 const struct glsl_type *type,
201 struct nir_link_uniforms_state *state)
202 {
203 if (!state->params || uniform->is_shader_storage || glsl_contains_opaque(type))
204 return;
205
206 unsigned num_params = glsl_get_aoa_size(type);
207 num_params = MAX2(num_params, 1);
208 num_params *= glsl_get_matrix_columns(glsl_without_array(type));
209
210 bool is_dual_slot = glsl_type_is_dual_slot(glsl_without_array(type));
211 if (is_dual_slot)
212 num_params *= 2;
213
214 struct gl_program_parameter_list *params = state->params;
215 int base_index = params->NumParameters;
216 _mesa_reserve_parameter_storage(params, num_params);
217
218 if (ctx->Const.PackedDriverUniformStorage) {
219 for (unsigned i = 0; i < num_params; i++) {
220 unsigned dmul = glsl_type_is_64bit(glsl_without_array(type)) ? 2 : 1;
221 unsigned comps = glsl_get_vector_elements(glsl_without_array(type)) * dmul;
222 if (is_dual_slot) {
223 if (i & 0x1)
224 comps -= 4;
225 else
226 comps = 4;
227 }
228
229 _mesa_add_parameter(params, PROGRAM_UNIFORM, uniform->name, comps,
230 glsl_get_gl_type(type), NULL, NULL, false);
231 }
232 } else {
233 for (unsigned i = 0; i < num_params; i++) {
234 _mesa_add_parameter(params, PROGRAM_UNIFORM, uniform->name, 4,
235 glsl_get_gl_type(type), NULL, NULL, true);
236 }
237 }
238
239 /* Each Parameter will hold the index to the backing uniform storage.
240 * This avoids relying on names to match parameters and uniform
241 * storages.
242 */
243 for (unsigned i = 0; i < num_params; i++) {
244 struct gl_program_parameter *param = &params->Parameters[base_index + i];
245 param->UniformStorageIndex = uniform - prog->data->UniformStorage;
246 param->MainUniformStorageIndex = state->current_var->data.location;
247 }
248 }
249
250 static unsigned
251 get_next_index(struct nir_link_uniforms_state *state,
252 const struct gl_uniform_storage *uniform,
253 unsigned *next_index, bool *initialised)
254 {
255 /* If we’ve already calculated an index for this member then we can just
256 * offset from there.
257 */
258 if (state->current_type->next_index == UINT_MAX) {
259 /* Otherwise we need to reserve enough indices for all of the arrays
260 * enclosing this member.
261 */
262
263 unsigned array_size = 1;
264
265 for (const struct type_tree_entry *p = state->current_type;
266 p;
267 p = p->parent) {
268 array_size *= p->array_size;
269 }
270
271 state->current_type->next_index = *next_index;
272 *next_index += array_size;
273 *initialised = true;
274 } else
275 *initialised = false;
276
277 unsigned index = state->current_type->next_index;
278
279 state->current_type->next_index += MAX2(1, uniform->array_elements);
280
281 return index;
282 }
283
284 static bool
285 find_and_update_named_uniform_storage(struct gl_context *ctx,
286 struct gl_shader_program *prog,
287 struct nir_link_uniforms_state *state,
288 nir_variable *var, char **name,
289 size_t name_length,
290 const struct glsl_type *type,
291 unsigned stage, bool *first_element)
292 {
293 /* gl_uniform_storage can cope with one level of array, so if the type is a
294 * composite type or an array where each element occupies more than one
295 * location than we need to recursively process it.
296 */
297 if (glsl_type_is_struct_or_ifc(type) ||
298 (glsl_type_is_array(type) &&
299 (glsl_type_is_array(glsl_get_array_element(type)) ||
300 glsl_type_is_struct_or_ifc(glsl_get_array_element(type))))) {
301
302 struct type_tree_entry *old_type = state->current_type;
303 state->current_type = old_type->children;
304
305 /* Shader storage block unsized arrays: add subscript [0] to variable
306 * names.
307 */
308 unsigned length = glsl_get_length(type);
309 if (glsl_type_is_unsized_array(type))
310 length = 1;
311
312 bool result = false;
313 for (unsigned i = 0; i < length; i++) {
314 const struct glsl_type *field_type;
315 size_t new_length = name_length;
316
317 if (glsl_type_is_struct_or_ifc(type)) {
318 field_type = glsl_get_struct_field(type, i);
319
320 /* Append '.field' to the current variable name. */
321 if (name) {
322 ralloc_asprintf_rewrite_tail(name, &new_length, ".%s",
323 glsl_get_struct_elem_name(type, i));
324 }
325 } else {
326 field_type = glsl_get_array_element(type);
327
328 /* Append the subscript to the current variable name */
329 if (name)
330 ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
331 }
332
333 result = find_and_update_named_uniform_storage(ctx, prog, state,
334 var, name, new_length,
335 field_type, stage,
336 first_element);
337
338 if (glsl_type_is_struct_or_ifc(type))
339 state->current_type = state->current_type->next_sibling;
340
341 if (!result) {
342 state->current_type = old_type;
343 return false;
344 }
345 }
346
347 state->current_type = old_type;
348
349 return result;
350 } else {
351 struct hash_entry *entry =
352 _mesa_hash_table_search(state->uniform_hash, *name);
353 if (entry) {
354 unsigned i = (unsigned) (intptr_t) entry->data;
355 mark_stage_as_active(&prog->data->UniformStorage[i], stage);
356
357 struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
358
359 if (*first_element && !state->var_is_in_block) {
360 *first_element = false;
361 var->data.location = uniform - prog->data->UniformStorage;
362 }
363
364 unsigned values = glsl_get_component_slots(type);
365 const struct glsl_type *type_no_array = glsl_without_array(type);
366 if (glsl_type_is_sampler(type_no_array)) {
367 struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
368 bool init_idx;
369 unsigned sampler_index =
370 get_next_index(state, uniform, &state->next_sampler_index,
371 &init_idx);
372
373 /* Samplers (bound or bindless) are counted as two components as
374 * specified by ARB_bindless_texture.
375 */
376 state->num_shader_samplers += values / 2;
377
378 uniform->opaque[stage].active = true;
379 uniform->opaque[stage].index = sampler_index;
380
381 if (init_idx) {
382 const unsigned shadow =
383 glsl_sampler_type_is_shadow(type_no_array);
384 for (unsigned i = sampler_index;
385 i < MIN2(state->next_sampler_index, MAX_SAMPLERS);
386 i++) {
387 sh->Program->sh.SamplerTargets[i] =
388 glsl_get_sampler_target(type_no_array);
389 state->shader_samplers_used |= 1U << i;
390 state->shader_shadow_samplers |= shadow << i;
391 }
392 }
393 } else if (glsl_type_is_image(type_no_array)) {
394 struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
395 int image_index = state->next_image_index;
396 /* TODO: handle structs when bindless support is added */
397 state->next_image_index += MAX2(1, uniform->array_elements);
398
399 /* Images (bound or bindless) are counted as two components as
400 * specified by ARB_bindless_texture.
401 */
402 state->num_shader_images += values / 2;
403
404 uniform->opaque[stage].active = true;
405 uniform->opaque[stage].index = image_index;
406
407 /* Set image access qualifiers */
408 enum gl_access_qualifier image_access =
409 state->current_var->data.access;
410 const GLenum access =
411 (image_access & ACCESS_NON_WRITEABLE) ?
412 ((image_access & ACCESS_NON_READABLE) ? GL_NONE :
413 GL_READ_ONLY) :
414 ((image_access & ACCESS_NON_READABLE) ? GL_WRITE_ONLY :
415 GL_READ_WRITE);
416 for (unsigned i = image_index;
417 i < MIN2(state->next_image_index, MAX_IMAGE_UNIFORMS);
418 i++) {
419 sh->Program->sh.ImageAccess[i] = access;
420 }
421 }
422
423 uniform->active_shader_mask |= 1 << stage;
424
425 if (!state->var_is_in_block)
426 add_parameter(uniform, ctx, prog, type, state);
427
428 return true;
429 }
430 }
431
432 return false;
433 }
434
435 /**
436 * Finds, returns, and updates the stage info for any uniform in UniformStorage
437 * defined by @var. For GLSL this is done using the name, for SPIR-V in general
438 * is this done using the explicit location, except:
439 *
440 * * UBOs/SSBOs: as they lack explicit location, binding is used to locate
441 * them. That means that more that one entry at the uniform storage can be
442 * found. In that case all of them are updated, and the first entry is
443 * returned, in order to update the location of the nir variable.
444 *
445 * * Special uniforms: like atomic counters. They lack a explicit location,
446 * so they are skipped. They will be handled and assigned a location later.
447 *
448 */
449 static bool
450 find_and_update_previous_uniform_storage(struct gl_context *ctx,
451 struct gl_shader_program *prog,
452 struct nir_link_uniforms_state *state,
453 nir_variable *var, char *name,
454 const struct glsl_type *type,
455 unsigned stage)
456 {
457 if (!prog->data->spirv) {
458 bool first_element = true;
459 char *name_tmp = ralloc_strdup(NULL, name);
460 bool r = find_and_update_named_uniform_storage(ctx, prog, state, var,
461 &name_tmp,
462 strlen(name_tmp), type,
463 stage, &first_element);
464 ralloc_free(name_tmp);
465
466 return r;
467 }
468
469 if (nir_variable_is_in_block(var)) {
470 struct gl_uniform_storage *uniform = NULL;
471
472 ASSERTED unsigned num_blks = nir_variable_is_in_ubo(var) ?
473 prog->data->NumUniformBlocks :
474 prog->data->NumShaderStorageBlocks;
475
476 struct gl_uniform_block *blks = nir_variable_is_in_ubo(var) ?
477 prog->data->UniformBlocks : prog->data->ShaderStorageBlocks;
478
479 bool result = false;
480 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
481 /* UniformStorage contains both variables from ubos and ssbos */
482 if ( prog->data->UniformStorage[i].is_shader_storage !=
483 nir_variable_is_in_ssbo(var))
484 continue;
485
486 int block_index = prog->data->UniformStorage[i].block_index;
487 if (block_index != -1) {
488 assert(block_index < num_blks);
489
490 if (var->data.binding == blks[block_index].Binding) {
491 if (!uniform)
492 uniform = &prog->data->UniformStorage[i];
493 mark_stage_as_active(&prog->data->UniformStorage[i],
494 stage);
495 result = true;
496 }
497 }
498 }
499
500 if (result)
501 var->data.location = uniform - prog->data->UniformStorage;
502 return result;
503 }
504
505 /* Beyond blocks, there are still some corner cases of uniforms without
506 * location (ie: atomic counters) that would have a initial location equal
507 * to -1. We just return on that case. Those uniforms will be handled
508 * later.
509 */
510 if (var->data.location == -1)
511 return false;
512
513 /* TODO: following search can be problematic with shaders with a lot of
514 * uniforms. Would it be better to use some type of hash
515 */
516 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
517 if (prog->data->UniformStorage[i].remap_location == var->data.location) {
518 mark_stage_as_active(&prog->data->UniformStorage[i], stage);
519
520 struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
521 var->data.location = uniform - prog->data->UniformStorage;
522 add_parameter(uniform, ctx, prog, var->type, state);
523 return true;
524 }
525 }
526
527 return false;
528 }
529
530 static struct type_tree_entry *
531 build_type_tree_for_type(const struct glsl_type *type)
532 {
533 struct type_tree_entry *entry = malloc(sizeof *entry);
534
535 entry->array_size = 1;
536 entry->next_index = UINT_MAX;
537 entry->children = NULL;
538 entry->next_sibling = NULL;
539 entry->parent = NULL;
540
541 if (glsl_type_is_array(type)) {
542 entry->array_size = glsl_get_length(type);
543 entry->children = build_type_tree_for_type(glsl_get_array_element(type));
544 entry->children->parent = entry;
545 } else if (glsl_type_is_struct_or_ifc(type)) {
546 struct type_tree_entry *last = NULL;
547
548 for (unsigned i = 0; i < glsl_get_length(type); i++) {
549 const struct glsl_type *field_type = glsl_get_struct_field(type, i);
550 struct type_tree_entry *field_entry =
551 build_type_tree_for_type(field_type);
552
553 if (last == NULL)
554 entry->children = field_entry;
555 else
556 last->next_sibling = field_entry;
557
558 field_entry->parent = entry;
559
560 last = field_entry;
561 }
562 }
563
564 return entry;
565 }
566
567 static void
568 free_type_tree(struct type_tree_entry *entry)
569 {
570 struct type_tree_entry *p, *next;
571
572 for (p = entry->children; p; p = next) {
573 next = p->next_sibling;
574 free_type_tree(p);
575 }
576
577 free(entry);
578 }
579
580 static void
581 hash_free_uniform_name(struct hash_entry *entry)
582 {
583 free((void*)entry->key);
584 }
585
586 /**
587 * Creates the neccessary entries in UniformStorage for the uniform. Returns
588 * the number of locations used or -1 on failure.
589 */
590 static int
591 nir_link_uniform(struct gl_context *ctx,
592 struct gl_shader_program *prog,
593 struct gl_program *stage_program,
594 gl_shader_stage stage,
595 const struct glsl_type *type,
596 unsigned index_in_parent,
597 int location,
598 struct nir_link_uniforms_state *state,
599 char **name, size_t name_length)
600 {
601 struct gl_uniform_storage *uniform = NULL;
602
603 if (state->set_top_level_array &&
604 nir_variable_is_in_ssbo(state->current_var)) {
605 /* Type is the top level SSBO member */
606 if (glsl_type_is_array(type) &&
607 (glsl_type_is_array(glsl_get_array_element(type)) ||
608 glsl_type_is_struct_or_ifc(glsl_get_array_element(type)))) {
609 /* Type is a top-level array (array of aggregate types) */
610 state->top_level_array_size = glsl_get_length(type);
611 state->top_level_array_stride = glsl_get_explicit_stride(type);
612 } else {
613 state->top_level_array_size = 1;
614 state->top_level_array_stride = 0;
615 }
616
617 state->set_top_level_array = false;
618 }
619
620 /* gl_uniform_storage can cope with one level of array, so if the type is a
621 * composite type or an array where each element occupies more than one
622 * location than we need to recursively process it.
623 */
624 if (glsl_type_is_struct_or_ifc(type) ||
625 (glsl_type_is_array(type) &&
626 (glsl_type_is_array(glsl_get_array_element(type)) ||
627 glsl_type_is_struct_or_ifc(glsl_get_array_element(type))))) {
628 int location_count = 0;
629 struct type_tree_entry *old_type = state->current_type;
630 unsigned int struct_base_offset = state->offset;
631
632 state->current_type = old_type->children;
633
634 /* Shader storage block unsized arrays: add subscript [0] to variable
635 * names.
636 */
637 unsigned length = glsl_get_length(type);
638 if (glsl_type_is_unsized_array(type))
639 length = 1;
640
641 for (unsigned i = 0; i < length; i++) {
642 const struct glsl_type *field_type;
643 size_t new_length = name_length;
644
645 if (glsl_type_is_struct_or_ifc(type)) {
646 field_type = glsl_get_struct_field(type, i);
647 /* Use the offset inside the struct only for variables backed by
648 * a buffer object. For variables not backed by a buffer object,
649 * offset is -1.
650 */
651 if (state->var_is_in_block) {
652 if (prog->data->spirv) {
653 state->offset =
654 struct_base_offset + glsl_get_struct_field_offset(type, i);
655 } else if (glsl_get_struct_field_offset(type, i) != -1 &&
656 type == state->current_ifc_type) {
657 state->offset = glsl_get_struct_field_offset(type, i);
658 }
659
660 if (glsl_type_is_interface(type))
661 state->set_top_level_array = true;
662 }
663
664 /* Append '.field' to the current variable name. */
665 if (name) {
666 ralloc_asprintf_rewrite_tail(name, &new_length, ".%s",
667 glsl_get_struct_elem_name(type, i));
668 }
669 } else {
670 field_type = glsl_get_array_element(type);
671
672 /* Append the subscript to the current variable name */
673 if (name)
674 ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
675 }
676
677 int entries = nir_link_uniform(ctx, prog, stage_program, stage,
678 field_type, i, location,
679 state, name, new_length);
680 if (entries == -1)
681 return -1;
682
683 if (location != -1)
684 location += entries;
685 location_count += entries;
686
687 if (glsl_type_is_struct_or_ifc(type))
688 state->current_type = state->current_type->next_sibling;
689 }
690
691 state->current_type = old_type;
692
693 return location_count;
694 } else {
695 /* Create a new uniform storage entry */
696 prog->data->UniformStorage =
697 reralloc(prog->data,
698 prog->data->UniformStorage,
699 struct gl_uniform_storage,
700 prog->data->NumUniformStorage + 1);
701 if (!prog->data->UniformStorage) {
702 linker_error(prog, "Out of memory during linking.\n");
703 return -1;
704 }
705
706 uniform = &prog->data->UniformStorage[prog->data->NumUniformStorage];
707 prog->data->NumUniformStorage++;
708
709 /* Initialize its members */
710 memset(uniform, 0x00, sizeof(struct gl_uniform_storage));
711
712 uniform->name =
713 name ? ralloc_strdup(prog->data->UniformStorage, *name) : NULL;
714
715 const struct glsl_type *type_no_array = glsl_without_array(type);
716 if (glsl_type_is_array(type)) {
717 uniform->type = type_no_array;
718 uniform->array_elements = glsl_get_length(type);
719 } else {
720 uniform->type = type;
721 uniform->array_elements = 0;
722 }
723 uniform->top_level_array_size = state->top_level_array_size;
724 uniform->top_level_array_stride = state->top_level_array_stride;
725
726 uniform->active_shader_mask |= 1 << stage;
727
728 if (location >= 0) {
729 /* Uniform has an explicit location */
730 uniform->remap_location = location;
731 } else {
732 uniform->remap_location = UNMAPPED_UNIFORM_LOC;
733 }
734
735 uniform->hidden = state->current_var->data.how_declared == nir_var_hidden;
736 if (uniform->hidden)
737 state->num_hidden_uniforms++;
738
739 uniform->is_shader_storage = nir_variable_is_in_ssbo(state->current_var);
740
741 /* Set fields whose default value depend on the variable being inside a
742 * block.
743 *
744 * From the OpenGL 4.6 spec, 7.3 Program objects:
745 *
746 * "For the property ARRAY_STRIDE, ... For active variables not declared
747 * as an array of basic types, zero is written to params. For active
748 * variables not backed by a buffer object, -1 is written to params,
749 * regardless of the variable type."
750 *
751 * "For the property MATRIX_STRIDE, ... For active variables not declared
752 * as a matrix or array of matrices, zero is written to params. For active
753 * variables not backed by a buffer object, -1 is written to params,
754 * regardless of the variable type."
755 *
756 * For the property IS_ROW_MAJOR, ... For active variables backed by a
757 * buffer object, declared as a single matrix or array of matrices, and
758 * stored in row-major order, one is written to params. For all other
759 * active variables, zero is written to params.
760 */
761 uniform->array_stride = -1;
762 uniform->matrix_stride = -1;
763 uniform->row_major = false;
764
765 if (state->var_is_in_block) {
766 uniform->array_stride = glsl_type_is_array(type) ?
767 glsl_get_explicit_stride(type) : 0;
768
769 if (glsl_type_is_matrix(uniform->type)) {
770 uniform->matrix_stride = glsl_get_explicit_stride(uniform->type);
771 uniform->row_major = glsl_matrix_type_is_row_major(uniform->type);
772 } else {
773 uniform->matrix_stride = 0;
774 }
775
776 if (!prog->data->spirv) {
777 bool use_std430 = ctx->Const.UseSTD430AsDefaultPacking;
778 const enum glsl_interface_packing packing =
779 glsl_get_internal_ifc_packing(state->current_var->interface_type,
780 use_std430);
781
782 unsigned alignment =
783 glsl_get_std140_base_alignment(type, uniform->row_major);
784 if (packing == GLSL_INTERFACE_PACKING_STD430) {
785 alignment =
786 glsl_get_std430_base_alignment(type, uniform->row_major);
787 }
788 state->offset = glsl_align(state->offset, alignment);
789 }
790 }
791
792 uniform->offset = state->var_is_in_block ? state->offset : -1;
793
794 int buffer_block_index = -1;
795 /* If the uniform is inside a uniform block determine its block index by
796 * comparing the bindings, we can not use names.
797 */
798 if (state->var_is_in_block) {
799 struct gl_uniform_block *blocks = nir_variable_is_in_ssbo(state->current_var) ?
800 prog->data->ShaderStorageBlocks : prog->data->UniformBlocks;
801
802 int num_blocks = nir_variable_is_in_ssbo(state->current_var) ?
803 prog->data->NumShaderStorageBlocks : prog->data->NumUniformBlocks;
804
805 if (!prog->data->spirv) {
806 bool is_interface_array =
807 glsl_without_array(state->current_var->type) == state->current_var->interface_type &&
808 glsl_type_is_array(state->current_var->type);
809
810 const char *ifc_name =
811 glsl_get_type_name(state->current_var->interface_type);
812 if (is_interface_array) {
813 unsigned l = strlen(ifc_name);
814 for (unsigned i = 0; i < num_blocks; i++) {
815 if (strncmp(ifc_name, blocks[i].Name, l) == 0 &&
816 blocks[i].Name[l] == '[') {
817 buffer_block_index = i;
818 break;
819 }
820 }
821 } else {
822 for (unsigned i = 0; i < num_blocks; i++) {
823 if (strcmp(ifc_name, blocks[i].Name) == 0) {
824 buffer_block_index = i;
825 break;
826 }
827 }
828 }
829
830 /* Compute the next offset. */
831 bool use_std430 = ctx->Const.UseSTD430AsDefaultPacking;
832 const enum glsl_interface_packing packing =
833 glsl_get_internal_ifc_packing(state->current_var->interface_type,
834 use_std430);
835 if (packing == GLSL_INTERFACE_PACKING_STD430)
836 state->offset += glsl_get_std430_size(type, uniform->row_major);
837 else
838 state->offset += glsl_get_std140_size(type, uniform->row_major);
839 } else {
840 for (unsigned i = 0; i < num_blocks; i++) {
841 if (state->current_var->data.binding == blocks[i].Binding) {
842 buffer_block_index = i;
843 break;
844 }
845 }
846
847 /* Compute the next offset. */
848 state->offset += glsl_get_explicit_size(type, true);
849 }
850 assert(buffer_block_index >= 0);
851 }
852
853 uniform->block_index = buffer_block_index;
854
855 /* @FIXME: the initialization of the following will be done as we
856 * implement support for their specific features, like SSBO, atomics,
857 * etc.
858 */
859 uniform->builtin = false;
860 uniform->atomic_buffer_index = -1;
861 uniform->is_bindless = false;
862
863 /* The following are not for features not supported by ARB_gl_spirv */
864 uniform->num_compatible_subroutines = 0;
865
866 unsigned entries = MAX2(1, uniform->array_elements);
867 unsigned values = glsl_get_component_slots(type);
868
869 if (glsl_type_is_sampler(type_no_array)) {
870 bool init_idx;
871 int sampler_index =
872 get_next_index(state, uniform, &state->next_sampler_index,
873 &init_idx);
874
875 /* Samplers (bound or bindless) are counted as two components as
876 * specified by ARB_bindless_texture.
877 */
878 state->num_shader_samplers += values / 2;
879
880 uniform->opaque[stage].active = true;
881 uniform->opaque[stage].index = sampler_index;
882
883 if (init_idx) {
884 const unsigned shadow = glsl_sampler_type_is_shadow(type_no_array);
885 for (unsigned i = sampler_index;
886 i < MIN2(state->next_sampler_index, MAX_SAMPLERS);
887 i++) {
888 stage_program->sh.SamplerTargets[i] =
889 glsl_get_sampler_target(type_no_array);
890 state->shader_samplers_used |= 1U << i;
891 state->shader_shadow_samplers |= shadow << i;
892 }
893 }
894
895 state->num_values += values;
896 } else if (glsl_type_is_image(type_no_array)) {
897 /* @FIXME: image_index should match that of the same image
898 * uniform in other shaders. This means we need to match image
899 * uniforms by location (GLSL does it by variable name, but we
900 * want to avoid that).
901 */
902 int image_index = state->next_image_index;
903 state->next_image_index += entries;
904
905 /* Images (bound or bindless) are counted as two components as
906 * specified by ARB_bindless_texture.
907 */
908 state->num_shader_images += values / 2;
909
910 uniform->opaque[stage].active = true;
911 uniform->opaque[stage].index = image_index;
912
913 /* Set image access qualifiers */
914 enum gl_access_qualifier image_access =
915 state->current_var->data.access;
916 const GLenum access =
917 (image_access & ACCESS_NON_WRITEABLE) ?
918 ((image_access & ACCESS_NON_READABLE) ? GL_NONE :
919 GL_READ_ONLY) :
920 ((image_access & ACCESS_NON_READABLE) ? GL_WRITE_ONLY :
921 GL_READ_WRITE);
922 for (unsigned i = image_index;
923 i < MIN2(state->next_image_index, MAX_IMAGE_UNIFORMS);
924 i++) {
925 stage_program->sh.ImageAccess[i] = access;
926 }
927
928 if (!uniform->is_shader_storage) {
929 state->num_shader_uniform_components += values;
930 state->num_values += values;
931 }
932 } else {
933 if (!state->var_is_in_block) {
934 state->num_shader_uniform_components += values;
935 state->num_values += values;
936 }
937 }
938
939 if (uniform->remap_location != UNMAPPED_UNIFORM_LOC &&
940 state->max_uniform_location < uniform->remap_location + entries)
941 state->max_uniform_location = uniform->remap_location + entries;
942
943 if (!state->var_is_in_block)
944 add_parameter(uniform, ctx, prog, type, state);
945
946 if (name) {
947 _mesa_hash_table_insert(state->uniform_hash, strdup(*name),
948 (void *) (intptr_t)
949 (prog->data->NumUniformStorage - 1));
950 }
951
952 return MAX2(uniform->array_elements, 1);
953 }
954 }
955
956 bool
957 gl_nir_link_uniforms(struct gl_context *ctx,
958 struct gl_shader_program *prog,
959 bool fill_parameters)
960 {
961 /* First free up any previous UniformStorage items */
962 ralloc_free(prog->data->UniformStorage);
963 prog->data->UniformStorage = NULL;
964 prog->data->NumUniformStorage = 0;
965
966 /* Iterate through all linked shaders */
967 struct nir_link_uniforms_state state = {0,};
968 state.uniform_hash = _mesa_hash_table_create(NULL, _mesa_hash_string,
969 _mesa_key_string_equal);
970
971 for (unsigned shader_type = 0; shader_type < MESA_SHADER_STAGES; shader_type++) {
972 struct gl_linked_shader *sh = prog->_LinkedShaders[shader_type];
973 if (!sh)
974 continue;
975
976 nir_shader *nir = sh->Program->nir;
977 assert(nir);
978
979 state.next_image_index = 0;
980 state.next_sampler_index = 0;
981 state.num_shader_samplers = 0;
982 state.num_shader_images = 0;
983 state.num_shader_uniform_components = 0;
984 state.shader_storage_blocks_write_access = 0;
985 state.shader_samplers_used = 0;
986 state.shader_shadow_samplers = 0;
987 state.params = fill_parameters ? sh->Program->Parameters : NULL;
988
989 nir_foreach_variable(var, &nir->uniforms) {
990 state.current_var = var;
991 state.current_ifc_type = NULL;
992 state.offset = 0;
993 state.var_is_in_block = nir_variable_is_in_block(var);
994 state.set_top_level_array = false;
995 state.top_level_array_size = 0;
996 state.top_level_array_stride = 0;
997
998 /*
999 * From ARB_program_interface spec, issue (16):
1000 *
1001 * "RESOLVED: We will follow the default rule for enumerating block
1002 * members in the OpenGL API, which is:
1003 *
1004 * * If a variable is a member of an interface block without an
1005 * instance name, it is enumerated using just the variable name.
1006 *
1007 * * If a variable is a member of an interface block with an
1008 * instance name, it is enumerated as "BlockName.Member", where
1009 * "BlockName" is the name of the interface block (not the
1010 * instance name) and "Member" is the name of the variable.
1011 *
1012 * For example, in the following code:
1013 *
1014 * uniform Block1 {
1015 * int member1;
1016 * };
1017 * uniform Block2 {
1018 * int member2;
1019 * } instance2;
1020 * uniform Block3 {
1021 * int member3;
1022 * } instance3[2]; // uses two separate buffer bindings
1023 *
1024 * the three uniforms (if active) are enumerated as "member1",
1025 * "Block2.member2", and "Block3.member3"."
1026 *
1027 * Note that in the last example, with an array of ubo, only one
1028 * uniform is generated. For that reason, while unrolling the
1029 * uniforms of a ubo, or the variables of a ssbo, we need to treat
1030 * arrays of instance as a single block.
1031 */
1032 char *name;
1033 const struct glsl_type *type = var->type;
1034 if (state.var_is_in_block &&
1035 ((!prog->data->spirv && glsl_without_array(type) == var->interface_type) ||
1036 (prog->data->spirv && type == var->interface_type))) {
1037 type = glsl_without_array(var->type);
1038 state.current_ifc_type = type;
1039 name = ralloc_strdup(NULL, glsl_get_type_name(type));
1040 } else {
1041 state.set_top_level_array = true;
1042 name = ralloc_strdup(NULL, var->name);
1043 }
1044
1045 struct type_tree_entry *type_tree =
1046 build_type_tree_for_type(type);
1047 state.current_type = type_tree;
1048
1049 int location = var->data.location;
1050
1051 struct gl_uniform_block *blocks;
1052 int num_blocks;
1053 int buffer_block_index = -1;
1054 if (!prog->data->spirv && state.var_is_in_block) {
1055 /* If the uniform is inside a uniform block determine its block index by
1056 * comparing the bindings, we can not use names.
1057 */
1058 blocks = nir_variable_is_in_ssbo(state.current_var) ?
1059 prog->data->ShaderStorageBlocks : prog->data->UniformBlocks;
1060 num_blocks = nir_variable_is_in_ssbo(state.current_var) ?
1061 prog->data->NumShaderStorageBlocks : prog->data->NumUniformBlocks;
1062
1063 bool is_interface_array =
1064 glsl_without_array(state.current_var->type) == state.current_var->interface_type &&
1065 glsl_type_is_array(state.current_var->type);
1066
1067 const char *ifc_name =
1068 glsl_get_type_name(state.current_var->interface_type);
1069
1070 if (is_interface_array) {
1071 unsigned l = strlen(ifc_name);
1072 for (unsigned i = 0; i < num_blocks; i++) {
1073 if (strncmp(ifc_name, blocks[i].Name, l) == 0 &&
1074 blocks[i].Name[l] == '[') {
1075 buffer_block_index = i;
1076
1077 blocks[i].stageref |= 1U << shader_type;
1078 }
1079 }
1080 } else {
1081 for (unsigned i = 0; i < num_blocks; i++) {
1082 if (strcmp(ifc_name, blocks[i].Name) == 0) {
1083 buffer_block_index = i;
1084
1085 blocks[i].stageref |= 1U << shader_type;
1086 break;
1087 }
1088 }
1089 }
1090
1091 if (nir_variable_is_in_ssbo(var) &&
1092 !(var->data.access & ACCESS_NON_WRITEABLE)) {
1093 unsigned array_size = is_interface_array ?
1094 glsl_get_length(var->type) : 1;
1095
1096 STATIC_ASSERT(MAX_SHADER_STORAGE_BUFFERS <= 32);
1097
1098 /* Shaders that use too many SSBOs will fail to compile, which
1099 * we don't care about.
1100 *
1101 * This is true for shaders that do not use too many SSBOs:
1102 */
1103 if (buffer_block_index + array_size <= 32) {
1104 state.shader_storage_blocks_write_access |=
1105 u_bit_consecutive(buffer_block_index, array_size);
1106 }
1107 }
1108 }
1109
1110 if (!prog->data->spirv && state.var_is_in_block &&
1111 glsl_without_array(state.current_var->type) != state.current_var->interface_type) {
1112
1113 bool found = false;
1114 char sentinel = '\0';
1115
1116 if (glsl_type_is_struct(state.current_var->type)) {
1117 sentinel = '.';
1118 } else if (glsl_type_is_array(state.current_var->type) &&
1119 (glsl_type_is_array(glsl_get_array_element(state.current_var->type))
1120 || glsl_type_is_struct(glsl_without_array(state.current_var->type)))) {
1121 sentinel = '[';
1122 }
1123
1124 const unsigned l = strlen(state.current_var->name);
1125 for (unsigned i = 0; i < num_blocks; i++) {
1126 for (unsigned j = 0; j < blocks[i].NumUniforms; j++) {
1127 if (sentinel) {
1128 const char *begin = blocks[i].Uniforms[j].Name;
1129 const char *end = strchr(begin, sentinel);
1130
1131 if (end == NULL)
1132 continue;
1133
1134 if ((ptrdiff_t) l != (end - begin))
1135 continue;
1136 found = strncmp(state.current_var->name, begin, l) == 0;
1137 } else {
1138 found = strcmp(state.current_var->name, blocks[i].Uniforms[j].Name) == 0;
1139 }
1140
1141 if (found) {
1142 location = j;
1143
1144 blocks[i].stageref |= 1U << shader_type;
1145
1146 break;
1147 }
1148 }
1149
1150 if (found)
1151 break;
1152 }
1153 assert(found);
1154
1155 const struct gl_uniform_block *const block =
1156 &blocks[buffer_block_index];
1157 assert(location != -1);
1158
1159 const struct gl_uniform_buffer_variable *const ubo_var =
1160 &block->Uniforms[location];
1161
1162 state.offset = ubo_var->Offset;
1163 var->data.location = location;
1164 }
1165
1166 /* Check if the uniform has been processed already for
1167 * other stage. If so, validate they are compatible and update
1168 * the active stage mask.
1169 */
1170 if (find_and_update_previous_uniform_storage(ctx, prog, &state, var,
1171 name, type, shader_type)) {
1172 ralloc_free(name);
1173 free_type_tree(type_tree);
1174 continue;
1175 }
1176
1177 /* From now on the variable’s location will be its uniform index */
1178 if (!state.var_is_in_block)
1179 var->data.location = prog->data->NumUniformStorage;
1180 else
1181 location = -1;
1182
1183 int res = nir_link_uniform(ctx, prog, sh->Program, shader_type, type,
1184 0, location,
1185 &state,
1186 !prog->data->spirv ? &name : NULL,
1187 !prog->data->spirv ? strlen(name) : 0);
1188
1189 free_type_tree(type_tree);
1190 ralloc_free(name);
1191
1192 if (res == -1)
1193 return false;
1194 }
1195
1196 if (state.num_shader_samplers >
1197 ctx->Const.Program[shader_type].MaxTextureImageUnits) {
1198 linker_error(prog, "Too many %s shader texture samplers\n",
1199 _mesa_shader_stage_to_string(shader_type));
1200 continue;
1201 }
1202
1203 if (state.num_shader_images >
1204 ctx->Const.Program[shader_type].MaxImageUniforms) {
1205 linker_error(prog, "Too many %s shader image uniforms (%u > %u)\n",
1206 _mesa_shader_stage_to_string(shader_type),
1207 state.num_shader_images,
1208 ctx->Const.Program[shader_type].MaxImageUniforms);
1209 continue;
1210 }
1211
1212 sh->Program->SamplersUsed = state.shader_samplers_used;
1213 sh->Program->sh.ShaderStorageBlocksWriteAccess =
1214 state.shader_storage_blocks_write_access;
1215 sh->shadow_samplers = state.shader_shadow_samplers;
1216 sh->Program->info.num_textures = state.num_shader_samplers;
1217 sh->Program->info.num_images = state.num_shader_images;
1218 sh->num_uniform_components = state.num_shader_uniform_components;
1219 sh->num_combined_uniform_components = sh->num_uniform_components;
1220 }
1221
1222 prog->data->NumHiddenUniforms = state.num_hidden_uniforms;
1223 prog->NumUniformRemapTable = state.max_uniform_location;
1224 prog->data->NumUniformDataSlots = state.num_values;
1225
1226 nir_setup_uniform_remap_tables(ctx, prog);
1227 gl_nir_set_uniform_initializers(ctx, prog);
1228
1229 _mesa_hash_table_destroy(state.uniform_hash, hash_free_uniform_name);
1230
1231 return true;
1232 }