2 * Copyright © 2018 Intel Corporation
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:
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
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
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"
33 * This file do the common link for GLSL uniforms, using NIR, instead of IR as
34 * the counter-part glsl/link_uniforms.cpp
37 #define UNMAPPED_UNIFORM_LOC ~0u
40 * Built-in / reserved GL variables names start with "gl_"
43 is_gl_identifier(const char *s
)
45 return s
&& s
[0] == 'g' && s
[1] == 'l' && s
[2] == '_';
49 uniform_storage_size(const struct glsl_type
*type
)
51 switch (glsl_get_base_type(type
)) {
52 case GLSL_TYPE_STRUCT
:
53 case GLSL_TYPE_INTERFACE
: {
55 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++)
56 size
+= uniform_storage_size(glsl_get_struct_field(type
, i
));
59 case GLSL_TYPE_ARRAY
: {
60 const struct glsl_type
*e_type
= glsl_get_array_element(type
);
61 enum glsl_base_type e_base_type
= glsl_get_base_type(e_type
);
62 if (e_base_type
== GLSL_TYPE_STRUCT
||
63 e_base_type
== GLSL_TYPE_INTERFACE
||
64 e_base_type
== GLSL_TYPE_ARRAY
) {
65 unsigned length
= !glsl_type_is_unsized_array(type
) ?
66 glsl_get_length(type
) : 1;
67 return length
* uniform_storage_size(e_type
);
77 nir_setup_uniform_remap_tables(struct gl_context
*ctx
,
78 struct gl_shader_program
*prog
)
80 unsigned total_entries
= prog
->NumExplicitUniformLocations
;
82 /* For glsl this may have been allocated by reserve_explicit_locations() so
83 * that we can keep track of unused uniforms with explicit locations.
85 assert(!prog
->data
->spirv
||
86 (prog
->data
->spirv
&& !prog
->UniformRemapTable
));
87 if (!prog
->UniformRemapTable
) {
88 prog
->UniformRemapTable
= rzalloc_array(prog
,
89 struct gl_uniform_storage
*,
90 prog
->NumUniformRemapTable
);
93 union gl_constant_value
*data
=
94 rzalloc_array(prog
->data
,
95 union gl_constant_value
, prog
->data
->NumUniformDataSlots
);
96 if (!prog
->UniformRemapTable
|| !data
) {
97 linker_error(prog
, "Out of memory during linking.\n");
100 prog
->data
->UniformDataSlots
= data
;
102 prog
->data
->UniformDataDefaults
=
103 rzalloc_array(prog
->data
->UniformDataSlots
,
104 union gl_constant_value
, prog
->data
->NumUniformDataSlots
);
106 unsigned data_pos
= 0;
108 /* Reserve all the explicit locations of the active uniforms. */
109 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
110 struct gl_uniform_storage
*uniform
= &prog
->data
->UniformStorage
[i
];
112 if (uniform
->is_shader_storage
||
113 glsl_get_base_type(uniform
->type
) == GLSL_TYPE_SUBROUTINE
)
116 if (prog
->data
->UniformStorage
[i
].remap_location
== UNMAPPED_UNIFORM_LOC
)
119 /* How many new entries for this uniform? */
120 const unsigned entries
= MAX2(1, uniform
->array_elements
);
121 unsigned num_slots
= glsl_get_component_slots(uniform
->type
);
123 uniform
->storage
= &data
[data_pos
];
125 /* Set remap table entries point to correct gl_uniform_storage. */
126 for (unsigned j
= 0; j
< entries
; j
++) {
127 unsigned element_loc
= uniform
->remap_location
+ j
;
128 prog
->UniformRemapTable
[element_loc
] = uniform
;
130 data_pos
+= num_slots
;
134 /* Reserve locations for rest of the uniforms. */
135 if (prog
->data
->spirv
)
136 link_util_update_empty_uniform_locations(prog
);
138 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
139 struct gl_uniform_storage
*uniform
= &prog
->data
->UniformStorage
[i
];
141 if (uniform
->is_shader_storage
||
142 glsl_get_base_type(uniform
->type
) == GLSL_TYPE_SUBROUTINE
)
145 /* Built-in uniforms should not get any location. */
146 if (uniform
->builtin
)
149 /* Explicit ones have been set already. */
150 if (uniform
->remap_location
!= UNMAPPED_UNIFORM_LOC
)
153 /* How many entries for this uniform? */
154 const unsigned entries
= MAX2(1, uniform
->array_elements
);
156 /* Add new entries to the total amount for checking against MAX_UNIFORM-
157 * _LOCATIONS. This only applies to the default uniform block (-1),
158 * because locations of uniform block entries are not assignable.
160 if (prog
->data
->UniformStorage
[i
].block_index
== -1)
161 total_entries
+= entries
;
164 link_util_find_empty_block(prog
, &prog
->data
->UniformStorage
[i
]);
166 if (location
== -1) {
167 location
= prog
->NumUniformRemapTable
;
169 /* resize remap table to fit new entries */
170 prog
->UniformRemapTable
=
172 prog
->UniformRemapTable
,
173 struct gl_uniform_storage
*,
174 prog
->NumUniformRemapTable
+ entries
);
175 prog
->NumUniformRemapTable
+= entries
;
178 /* set the base location in remap table for the uniform */
179 uniform
->remap_location
= location
;
181 unsigned num_slots
= glsl_get_component_slots(uniform
->type
);
183 if (uniform
->block_index
== -1)
184 uniform
->storage
= &data
[data_pos
];
186 /* Set remap table entries point to correct gl_uniform_storage. */
187 for (unsigned j
= 0; j
< entries
; j
++) {
188 unsigned element_loc
= uniform
->remap_location
+ j
;
189 prog
->UniformRemapTable
[element_loc
] = uniform
;
191 if (uniform
->block_index
== -1)
192 data_pos
+= num_slots
;
196 /* Verify that total amount of entries for explicit and implicit locations
197 * is less than MAX_UNIFORM_LOCATIONS.
199 if (total_entries
> ctx
->Const
.MaxUserAssignableUniformLocations
) {
200 linker_error(prog
, "count of uniform locations > MAX_UNIFORM_LOCATIONS"
201 "(%u > %u)", total_entries
,
202 ctx
->Const
.MaxUserAssignableUniformLocations
);
205 /* Reserve all the explicit locations of the active subroutine uniforms. */
206 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
207 struct gl_uniform_storage
*uniform
= &prog
->data
->UniformStorage
[i
];
209 if (glsl_get_base_type(uniform
->type
) != GLSL_TYPE_SUBROUTINE
)
212 if (prog
->data
->UniformStorage
[i
].remap_location
== UNMAPPED_UNIFORM_LOC
)
215 /* How many new entries for this uniform? */
216 const unsigned entries
=
217 MAX2(1, prog
->data
->UniformStorage
[i
].array_elements
);
219 uniform
->storage
= &data
[data_pos
];
221 unsigned num_slots
= glsl_get_component_slots(uniform
->type
);
222 unsigned mask
= prog
->data
->linked_stages
;
224 const int j
= u_bit_scan(&mask
);
225 struct gl_program
*p
= prog
->_LinkedShaders
[j
]->Program
;
227 if (!prog
->data
->UniformStorage
[i
].opaque
[j
].active
)
230 /* Set remap table entries point to correct gl_uniform_storage. */
231 for (unsigned k
= 0; k
< entries
; k
++) {
232 unsigned element_loc
=
233 prog
->data
->UniformStorage
[i
].remap_location
+ k
;
234 p
->sh
.SubroutineUniformRemapTable
[element_loc
] =
235 &prog
->data
->UniformStorage
[i
];
237 data_pos
+= num_slots
;
242 /* reserve subroutine locations */
243 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
244 struct gl_uniform_storage
*uniform
= &prog
->data
->UniformStorage
[i
];
246 if (glsl_get_base_type(uniform
->type
) != GLSL_TYPE_SUBROUTINE
)
249 if (prog
->data
->UniformStorage
[i
].remap_location
!=
250 UNMAPPED_UNIFORM_LOC
)
253 const unsigned entries
=
254 MAX2(1, prog
->data
->UniformStorage
[i
].array_elements
);
256 uniform
->storage
= &data
[data_pos
];
258 unsigned num_slots
= glsl_get_component_slots(uniform
->type
);
259 unsigned mask
= prog
->data
->linked_stages
;
261 const int j
= u_bit_scan(&mask
);
262 struct gl_program
*p
= prog
->_LinkedShaders
[j
]->Program
;
264 if (!prog
->data
->UniformStorage
[i
].opaque
[j
].active
)
267 p
->sh
.SubroutineUniformRemapTable
=
269 p
->sh
.SubroutineUniformRemapTable
,
270 struct gl_uniform_storage
*,
271 p
->sh
.NumSubroutineUniformRemapTable
+ entries
);
273 for (unsigned k
= 0; k
< entries
; k
++) {
274 p
->sh
.SubroutineUniformRemapTable
[p
->sh
.NumSubroutineUniformRemapTable
+ k
] =
275 &prog
->data
->UniformStorage
[i
];
277 data_pos
+= num_slots
;
279 prog
->data
->UniformStorage
[i
].remap_location
=
280 p
->sh
.NumSubroutineUniformRemapTable
;
281 p
->sh
.NumSubroutineUniformRemapTable
+= entries
;
287 add_var_use_deref(nir_deref_instr
*deref
, struct hash_table
*live
,
288 struct array_deref_range
**derefs
, unsigned *derefs_size
)
291 nir_deref_path_init(&path
, deref
, NULL
);
293 deref
= path
.path
[0];
294 if (deref
->deref_type
!= nir_deref_type_var
||
295 deref
->mode
& ~(nir_var_uniform
| nir_var_mem_ubo
| nir_var_mem_ssbo
)) {
296 nir_deref_path_finish(&path
);
300 /* Number of derefs used in current processing. */
301 unsigned num_derefs
= 0;
303 const struct glsl_type
*deref_type
= deref
->var
->type
;
304 nir_deref_instr
**p
= &path
.path
[1];
306 if ((*p
)->deref_type
== nir_deref_type_array
) {
308 /* Skip matrix derefences */
309 if (!glsl_type_is_array(deref_type
))
312 if ((num_derefs
+ 1) * sizeof(struct array_deref_range
) > *derefs_size
) {
313 void *ptr
= reralloc_size(NULL
, *derefs
, *derefs_size
+ 4096);
316 nir_deref_path_finish(&path
);
320 *derefs_size
+= 4096;
321 *derefs
= (struct array_deref_range
*)ptr
;
324 struct array_deref_range
*dr
= &(*derefs
)[num_derefs
];
327 dr
->size
= glsl_get_length(deref_type
);
329 if (nir_src_is_const((*p
)->arr
.index
)) {
330 dr
->index
= nir_src_as_uint((*p
)->arr
.index
);
332 /* An unsized array can occur at the end of an SSBO. We can't track
333 * accesses to such an array, so bail.
336 nir_deref_path_finish(&path
);
340 dr
->index
= dr
->size
;
343 deref_type
= glsl_get_array_element(deref_type
);
344 } else if ((*p
)->deref_type
== nir_deref_type_struct
) {
345 /* We have reached the end of the array. */
350 nir_deref_path_finish(&path
);
352 /** Set of bit-flags to note which array elements have been accessed. */
353 BITSET_WORD
*bits
= NULL
;
355 struct hash_entry
*entry
=
356 _mesa_hash_table_search(live
, deref
->var
);
357 if (!entry
&& glsl_type_is_array(deref
->var
->type
)) {
358 unsigned num_bits
= MAX2(1, glsl_get_aoa_size(deref
->var
->type
));
359 bits
= rzalloc_array(live
, BITSET_WORD
, BITSET_WORDS(num_bits
));
363 bits
= (BITSET_WORD
*) entry
->data
;
365 if (glsl_type_is_array(deref
->var
->type
)) {
366 /* Count the "depth" of the arrays-of-arrays. */
367 unsigned array_depth
= 0;
368 for (const struct glsl_type
*type
= deref
->var
->type
;
369 glsl_type_is_array(type
);
370 type
= glsl_get_array_element(type
)) {
374 link_util_mark_array_elements_referenced(*derefs
, num_derefs
, array_depth
,
378 assert(deref
->mode
== deref
->var
->data
.mode
);
379 _mesa_hash_table_insert(live
, deref
->var
, bits
);
382 /* Iterate over the shader and collect infomation about uniform use */
384 add_var_use_shader(nir_shader
*shader
, struct hash_table
*live
)
386 /* Currently allocated buffer block of derefs. */
387 struct array_deref_range
*derefs
= NULL
;
389 /* Size of the derefs buffer in bytes. */
390 unsigned derefs_size
= 0;
392 nir_foreach_function(function
, shader
) {
393 if (function
->impl
) {
394 nir_foreach_block(block
, function
->impl
) {
395 nir_foreach_instr(instr
, block
) {
396 if (instr
->type
== nir_instr_type_intrinsic
) {
397 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
398 switch (intr
->intrinsic
) {
399 case nir_intrinsic_atomic_counter_read_deref
:
400 case nir_intrinsic_atomic_counter_inc_deref
:
401 case nir_intrinsic_atomic_counter_pre_dec_deref
:
402 case nir_intrinsic_atomic_counter_post_dec_deref
:
403 case nir_intrinsic_atomic_counter_add_deref
:
404 case nir_intrinsic_atomic_counter_min_deref
:
405 case nir_intrinsic_atomic_counter_max_deref
:
406 case nir_intrinsic_atomic_counter_and_deref
:
407 case nir_intrinsic_atomic_counter_or_deref
:
408 case nir_intrinsic_atomic_counter_xor_deref
:
409 case nir_intrinsic_atomic_counter_exchange_deref
:
410 case nir_intrinsic_atomic_counter_comp_swap_deref
:
411 case nir_intrinsic_image_deref_load
:
412 case nir_intrinsic_image_deref_store
:
413 case nir_intrinsic_image_deref_atomic_add
:
414 case nir_intrinsic_image_deref_atomic_umin
:
415 case nir_intrinsic_image_deref_atomic_imin
:
416 case nir_intrinsic_image_deref_atomic_umax
:
417 case nir_intrinsic_image_deref_atomic_imax
:
418 case nir_intrinsic_image_deref_atomic_and
:
419 case nir_intrinsic_image_deref_atomic_or
:
420 case nir_intrinsic_image_deref_atomic_xor
:
421 case nir_intrinsic_image_deref_atomic_exchange
:
422 case nir_intrinsic_image_deref_atomic_comp_swap
:
423 case nir_intrinsic_image_deref_size
:
424 case nir_intrinsic_image_deref_samples
:
425 case nir_intrinsic_load_deref
:
426 case nir_intrinsic_store_deref
:
427 add_var_use_deref(nir_src_as_deref(intr
->src
[0]), live
,
428 &derefs
, &derefs_size
);
435 } else if (instr
->type
== nir_instr_type_tex
) {
436 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
438 nir_tex_instr_src_index(tex_instr
,
439 nir_tex_src_sampler_deref
);
441 nir_tex_instr_src_index(tex_instr
,
442 nir_tex_src_texture_deref
);
444 if (sampler_idx
>= 0) {
445 nir_deref_instr
*deref
=
446 nir_src_as_deref(tex_instr
->src
[sampler_idx
].src
);
447 add_var_use_deref(deref
, live
, &derefs
, &derefs_size
);
450 if (texture_idx
>= 0) {
451 nir_deref_instr
*deref
=
452 nir_src_as_deref(tex_instr
->src
[texture_idx
].src
);
453 add_var_use_deref(deref
, live
, &derefs
, &derefs_size
);
465 mark_stage_as_active(struct gl_uniform_storage
*uniform
,
468 uniform
->active_shader_mask
|= 1 << stage
;
471 /* Used to build a tree representing the glsl_type so that we can have a place
472 * to store the next index for opaque types. Array types are expanded so that
473 * they have a single child which is used for all elements of the array.
474 * Struct types have a child for each member. The tree is walked while
475 * processing a uniform so that we can recognise when an opaque type is
476 * encountered a second time in order to reuse the same range of indices that
477 * was reserved the first time. That way the sampler indices can be arranged
478 * so that members of an array are placed sequentially even if the array is an
479 * array of structs containing other opaque members.
481 struct type_tree_entry
{
482 /* For opaque types, this will be the next index to use. If we haven’t
483 * encountered this member yet, it will be UINT_MAX.
487 struct type_tree_entry
*parent
;
488 struct type_tree_entry
*next_sibling
;
489 struct type_tree_entry
*children
;
492 struct nir_link_uniforms_state
{
493 /* per-whole program */
494 unsigned num_hidden_uniforms
;
496 unsigned max_uniform_location
;
498 /* per-shader stage */
499 unsigned next_bindless_image_index
;
500 unsigned next_bindless_sampler_index
;
501 unsigned next_image_index
;
502 unsigned next_sampler_index
;
503 unsigned next_subroutine
;
504 unsigned num_shader_samplers
;
505 unsigned num_shader_images
;
506 unsigned num_shader_uniform_components
;
507 unsigned shader_samplers_used
;
508 unsigned shader_shadow_samplers
;
509 unsigned shader_storage_blocks_write_access
;
510 struct gl_program_parameter_list
*params
;
513 nir_variable
*current_var
;
514 const struct glsl_type
*current_ifc_type
;
516 bool var_is_in_block
;
517 bool set_top_level_array
;
518 int top_level_array_size
;
519 int top_level_array_stride
;
521 struct type_tree_entry
*current_type
;
522 struct hash_table
*referenced_uniforms
;
523 struct hash_table
*uniform_hash
;
527 add_parameter(struct gl_uniform_storage
*uniform
,
528 struct gl_context
*ctx
,
529 struct gl_shader_program
*prog
,
530 const struct glsl_type
*type
,
531 struct nir_link_uniforms_state
*state
)
533 if (!state
->params
|| uniform
->is_shader_storage
||
534 (glsl_contains_opaque(type
) && !state
->current_var
->data
.bindless
))
537 unsigned num_params
= glsl_get_aoa_size(type
);
538 num_params
= MAX2(num_params
, 1);
539 num_params
*= glsl_get_matrix_columns(glsl_without_array(type
));
541 bool is_dual_slot
= glsl_type_is_dual_slot(glsl_without_array(type
));
545 struct gl_program_parameter_list
*params
= state
->params
;
546 int base_index
= params
->NumParameters
;
547 _mesa_reserve_parameter_storage(params
, num_params
);
549 if (ctx
->Const
.PackedDriverUniformStorage
) {
550 for (unsigned i
= 0; i
< num_params
; i
++) {
551 unsigned dmul
= glsl_type_is_64bit(glsl_without_array(type
)) ? 2 : 1;
552 unsigned comps
= glsl_get_vector_elements(glsl_without_array(type
)) * dmul
;
560 _mesa_add_parameter(params
, PROGRAM_UNIFORM
, uniform
->name
, comps
,
561 glsl_get_gl_type(type
), NULL
, NULL
, false);
564 for (unsigned i
= 0; i
< num_params
; i
++) {
565 _mesa_add_parameter(params
, PROGRAM_UNIFORM
, uniform
->name
, 4,
566 glsl_get_gl_type(type
), NULL
, NULL
, true);
570 /* Each Parameter will hold the index to the backing uniform storage.
571 * This avoids relying on names to match parameters and uniform
574 for (unsigned i
= 0; i
< num_params
; i
++) {
575 struct gl_program_parameter
*param
= ¶ms
->Parameters
[base_index
+ i
];
576 param
->UniformStorageIndex
= uniform
- prog
->data
->UniformStorage
;
577 param
->MainUniformStorageIndex
= state
->current_var
->data
.location
;
582 get_next_index(struct nir_link_uniforms_state
*state
,
583 const struct gl_uniform_storage
*uniform
,
584 unsigned *next_index
, bool *initialised
)
586 /* If we’ve already calculated an index for this member then we can just
589 if (state
->current_type
->next_index
== UINT_MAX
) {
590 /* Otherwise we need to reserve enough indices for all of the arrays
591 * enclosing this member.
594 unsigned array_size
= 1;
596 for (const struct type_tree_entry
*p
= state
->current_type
;
599 array_size
*= p
->array_size
;
602 state
->current_type
->next_index
= *next_index
;
603 *next_index
+= array_size
;
606 *initialised
= false;
608 unsigned index
= state
->current_type
->next_index
;
610 state
->current_type
->next_index
+= MAX2(1, uniform
->array_elements
);
615 /* Update the uniforms info for the current shader stage */
617 update_uniforms_shader_info(struct gl_shader_program
*prog
,
618 struct nir_link_uniforms_state
*state
,
619 struct gl_uniform_storage
*uniform
,
620 const struct glsl_type
*type
,
623 unsigned values
= glsl_get_component_slots(type
);
624 const struct glsl_type
*type_no_array
= glsl_without_array(type
);
626 if (glsl_type_is_sampler(type_no_array
)) {
628 unsigned *next_index
= state
->current_var
->data
.bindless
?
629 &state
->next_bindless_sampler_index
:
630 &state
->next_sampler_index
;
631 int sampler_index
= get_next_index(state
, uniform
, next_index
, &init_idx
);
632 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[stage
];
634 if (state
->current_var
->data
.bindless
) {
636 sh
->Program
->sh
.BindlessSamplers
=
637 rerzalloc(sh
->Program
, sh
->Program
->sh
.BindlessSamplers
,
638 struct gl_bindless_sampler
,
639 sh
->Program
->sh
.NumBindlessSamplers
,
640 state
->next_bindless_sampler_index
);
642 for (unsigned j
= sh
->Program
->sh
.NumBindlessSamplers
;
643 j
< state
->next_bindless_sampler_index
; j
++) {
644 sh
->Program
->sh
.BindlessSamplers
[j
].target
=
645 glsl_get_sampler_target(type_no_array
);
648 sh
->Program
->sh
.NumBindlessSamplers
=
649 state
->next_bindless_sampler_index
;
652 if (!state
->var_is_in_block
)
653 state
->num_shader_uniform_components
+= values
;
655 /* Samplers (bound or bindless) are counted as two components
656 * as specified by ARB_bindless_texture.
658 state
->num_shader_samplers
+= values
/ 2;
661 const unsigned shadow
= glsl_sampler_type_is_shadow(type_no_array
);
662 for (unsigned i
= sampler_index
;
663 i
< MIN2(state
->next_sampler_index
, MAX_SAMPLERS
); i
++) {
664 sh
->Program
->sh
.SamplerTargets
[i
] =
665 glsl_get_sampler_target(type_no_array
);
666 state
->shader_samplers_used
|= 1U << i
;
667 state
->shader_shadow_samplers
|= shadow
<< i
;
672 uniform
->opaque
[stage
].active
= true;
673 uniform
->opaque
[stage
].index
= sampler_index
;
674 } else if (glsl_type_is_image(type_no_array
)) {
675 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[stage
];
677 /* Set image access qualifiers */
678 enum gl_access_qualifier image_access
=
679 state
->current_var
->data
.access
;
680 const GLenum access
=
681 (image_access
& ACCESS_NON_WRITEABLE
) ?
682 ((image_access
& ACCESS_NON_READABLE
) ? GL_NONE
:
684 ((image_access
& ACCESS_NON_READABLE
) ? GL_WRITE_ONLY
:
688 if (state
->current_var
->data
.bindless
) {
689 image_index
= state
->next_bindless_image_index
;
690 state
->next_bindless_image_index
+= MAX2(1, uniform
->array_elements
);
692 sh
->Program
->sh
.BindlessImages
=
693 rerzalloc(sh
->Program
, sh
->Program
->sh
.BindlessImages
,
694 struct gl_bindless_image
,
695 sh
->Program
->sh
.NumBindlessImages
,
696 state
->next_bindless_image_index
);
698 for (unsigned j
= sh
->Program
->sh
.NumBindlessImages
;
699 j
< state
->next_bindless_image_index
; j
++) {
700 sh
->Program
->sh
.BindlessImages
[j
].access
= access
;
703 sh
->Program
->sh
.NumBindlessImages
= state
->next_bindless_image_index
;
706 image_index
= state
->next_image_index
;
707 state
->next_image_index
+= MAX2(1, uniform
->array_elements
);
709 /* Images (bound or bindless) are counted as two components as
710 * specified by ARB_bindless_texture.
712 state
->num_shader_images
+= values
/ 2;
714 for (unsigned i
= image_index
;
715 i
< MIN2(state
->next_image_index
, MAX_IMAGE_UNIFORMS
); i
++) {
716 sh
->Program
->sh
.ImageAccess
[i
] = access
;
720 uniform
->opaque
[stage
].active
= true;
721 uniform
->opaque
[stage
].index
= image_index
;
723 if (!uniform
->is_shader_storage
)
724 state
->num_shader_uniform_components
+= values
;
726 if (glsl_get_base_type(type_no_array
) == GLSL_TYPE_SUBROUTINE
) {
727 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[stage
];
729 uniform
->opaque
[stage
].index
= state
->next_subroutine
;
730 uniform
->opaque
[stage
].active
= true;
732 sh
->Program
->sh
.NumSubroutineUniforms
++;
734 /* Increment the subroutine index by 1 for non-arrays and by the
735 * number of array elements for arrays.
737 state
->next_subroutine
+= MAX2(1, uniform
->array_elements
);
740 if (!state
->var_is_in_block
)
741 state
->num_shader_uniform_components
+= values
;
746 find_and_update_named_uniform_storage(struct gl_context
*ctx
,
747 struct gl_shader_program
*prog
,
748 struct nir_link_uniforms_state
*state
,
749 nir_variable
*var
, char **name
,
751 const struct glsl_type
*type
,
752 unsigned stage
, bool *first_element
)
754 /* gl_uniform_storage can cope with one level of array, so if the type is a
755 * composite type or an array where each element occupies more than one
756 * location than we need to recursively process it.
758 if (glsl_type_is_struct_or_ifc(type
) ||
759 (glsl_type_is_array(type
) &&
760 (glsl_type_is_array(glsl_get_array_element(type
)) ||
761 glsl_type_is_struct_or_ifc(glsl_get_array_element(type
))))) {
763 struct type_tree_entry
*old_type
= state
->current_type
;
764 state
->current_type
= old_type
->children
;
766 /* Shader storage block unsized arrays: add subscript [0] to variable
769 unsigned length
= glsl_get_length(type
);
770 if (glsl_type_is_unsized_array(type
))
774 for (unsigned i
= 0; i
< length
; i
++) {
775 const struct glsl_type
*field_type
;
776 size_t new_length
= name_length
;
778 if (glsl_type_is_struct_or_ifc(type
)) {
779 field_type
= glsl_get_struct_field(type
, i
);
781 /* Append '.field' to the current variable name. */
783 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s",
784 glsl_get_struct_elem_name(type
, i
));
787 field_type
= glsl_get_array_element(type
);
789 /* Append the subscript to the current variable name */
791 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
794 result
= find_and_update_named_uniform_storage(ctx
, prog
, state
,
795 var
, name
, new_length
,
799 if (glsl_type_is_struct_or_ifc(type
))
800 state
->current_type
= state
->current_type
->next_sibling
;
803 state
->current_type
= old_type
;
808 state
->current_type
= old_type
;
812 struct hash_entry
*entry
=
813 _mesa_hash_table_search(state
->uniform_hash
, *name
);
815 unsigned i
= (unsigned) (intptr_t) entry
->data
;
816 struct gl_uniform_storage
*uniform
= &prog
->data
->UniformStorage
[i
];
818 if (*first_element
&& !state
->var_is_in_block
) {
819 *first_element
= false;
820 var
->data
.location
= uniform
- prog
->data
->UniformStorage
;
823 update_uniforms_shader_info(prog
, state
, uniform
, type
, stage
);
825 const struct glsl_type
*type_no_array
= glsl_without_array(type
);
826 struct hash_entry
*entry
=
827 _mesa_hash_table_search(state
->referenced_uniforms
,
830 glsl_get_base_type(type_no_array
) == GLSL_TYPE_SUBROUTINE
)
831 uniform
->active_shader_mask
|= 1 << stage
;
833 if (!state
->var_is_in_block
)
834 add_parameter(uniform
, ctx
, prog
, type
, state
);
844 * Finds, returns, and updates the stage info for any uniform in UniformStorage
845 * defined by @var. For GLSL this is done using the name, for SPIR-V in general
846 * is this done using the explicit location, except:
848 * * UBOs/SSBOs: as they lack explicit location, binding is used to locate
849 * them. That means that more that one entry at the uniform storage can be
850 * found. In that case all of them are updated, and the first entry is
851 * returned, in order to update the location of the nir variable.
853 * * Special uniforms: like atomic counters. They lack a explicit location,
854 * so they are skipped. They will be handled and assigned a location later.
858 find_and_update_previous_uniform_storage(struct gl_context
*ctx
,
859 struct gl_shader_program
*prog
,
860 struct nir_link_uniforms_state
*state
,
861 nir_variable
*var
, char *name
,
862 const struct glsl_type
*type
,
865 if (!prog
->data
->spirv
) {
866 bool first_element
= true;
867 char *name_tmp
= ralloc_strdup(NULL
, name
);
868 bool r
= find_and_update_named_uniform_storage(ctx
, prog
, state
, var
,
870 strlen(name_tmp
), type
,
871 stage
, &first_element
);
872 ralloc_free(name_tmp
);
877 if (nir_variable_is_in_block(var
)) {
878 struct gl_uniform_storage
*uniform
= NULL
;
880 ASSERTED
unsigned num_blks
= nir_variable_is_in_ubo(var
) ?
881 prog
->data
->NumUniformBlocks
:
882 prog
->data
->NumShaderStorageBlocks
;
884 struct gl_uniform_block
*blks
= nir_variable_is_in_ubo(var
) ?
885 prog
->data
->UniformBlocks
: prog
->data
->ShaderStorageBlocks
;
888 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
889 /* UniformStorage contains both variables from ubos and ssbos */
890 if ( prog
->data
->UniformStorage
[i
].is_shader_storage
!=
891 nir_variable_is_in_ssbo(var
))
894 int block_index
= prog
->data
->UniformStorage
[i
].block_index
;
895 if (block_index
!= -1) {
896 assert(block_index
< num_blks
);
898 if (var
->data
.binding
== blks
[block_index
].Binding
) {
900 uniform
= &prog
->data
->UniformStorage
[i
];
901 mark_stage_as_active(&prog
->data
->UniformStorage
[i
],
909 var
->data
.location
= uniform
- prog
->data
->UniformStorage
;
913 /* Beyond blocks, there are still some corner cases of uniforms without
914 * location (ie: atomic counters) that would have a initial location equal
915 * to -1. We just return on that case. Those uniforms will be handled
918 if (var
->data
.location
== -1)
921 /* TODO: following search can be problematic with shaders with a lot of
922 * uniforms. Would it be better to use some type of hash
924 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
925 if (prog
->data
->UniformStorage
[i
].remap_location
== var
->data
.location
) {
926 mark_stage_as_active(&prog
->data
->UniformStorage
[i
], stage
);
928 struct gl_uniform_storage
*uniform
= &prog
->data
->UniformStorage
[i
];
929 var
->data
.location
= uniform
- prog
->data
->UniformStorage
;
930 add_parameter(uniform
, ctx
, prog
, var
->type
, state
);
938 static struct type_tree_entry
*
939 build_type_tree_for_type(const struct glsl_type
*type
)
941 struct type_tree_entry
*entry
= malloc(sizeof *entry
);
943 entry
->array_size
= 1;
944 entry
->next_index
= UINT_MAX
;
945 entry
->children
= NULL
;
946 entry
->next_sibling
= NULL
;
947 entry
->parent
= NULL
;
949 if (glsl_type_is_array(type
)) {
950 entry
->array_size
= glsl_get_length(type
);
951 entry
->children
= build_type_tree_for_type(glsl_get_array_element(type
));
952 entry
->children
->parent
= entry
;
953 } else if (glsl_type_is_struct_or_ifc(type
)) {
954 struct type_tree_entry
*last
= NULL
;
956 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
957 const struct glsl_type
*field_type
= glsl_get_struct_field(type
, i
);
958 struct type_tree_entry
*field_entry
=
959 build_type_tree_for_type(field_type
);
962 entry
->children
= field_entry
;
964 last
->next_sibling
= field_entry
;
966 field_entry
->parent
= entry
;
976 free_type_tree(struct type_tree_entry
*entry
)
978 struct type_tree_entry
*p
, *next
;
980 for (p
= entry
->children
; p
; p
= next
) {
981 next
= p
->next_sibling
;
989 hash_free_uniform_name(struct hash_entry
*entry
)
991 free((void*)entry
->key
);
995 enter_record(struct nir_link_uniforms_state
*state
,
996 struct gl_context
*ctx
,
997 const struct glsl_type
*type
,
1000 assert(glsl_type_is_struct(type
));
1001 if (!state
->var_is_in_block
)
1004 bool use_std430
= ctx
->Const
.UseSTD430AsDefaultPacking
;
1005 const enum glsl_interface_packing packing
=
1006 glsl_get_internal_ifc_packing(state
->current_var
->interface_type
,
1009 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
1010 state
->offset
= glsl_align(
1011 state
->offset
, glsl_get_std430_base_alignment(type
, row_major
));
1013 state
->offset
= glsl_align(
1014 state
->offset
, glsl_get_std140_base_alignment(type
, row_major
));
1018 leave_record(struct nir_link_uniforms_state
*state
,
1019 struct gl_context
*ctx
,
1020 const struct glsl_type
*type
,
1023 assert(glsl_type_is_struct(type
));
1024 if (!state
->var_is_in_block
)
1027 bool use_std430
= ctx
->Const
.UseSTD430AsDefaultPacking
;
1028 const enum glsl_interface_packing packing
=
1029 glsl_get_internal_ifc_packing(state
->current_var
->interface_type
,
1032 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
1033 state
->offset
= glsl_align(
1034 state
->offset
, glsl_get_std430_base_alignment(type
, row_major
));
1036 state
->offset
= glsl_align(
1037 state
->offset
, glsl_get_std140_base_alignment(type
, row_major
));
1041 * Creates the neccessary entries in UniformStorage for the uniform. Returns
1042 * the number of locations used or -1 on failure.
1045 nir_link_uniform(struct gl_context
*ctx
,
1046 struct gl_shader_program
*prog
,
1047 struct gl_program
*stage_program
,
1048 gl_shader_stage stage
,
1049 const struct glsl_type
*type
,
1050 unsigned index_in_parent
,
1052 struct nir_link_uniforms_state
*state
,
1053 char **name
, size_t name_length
, bool row_major
)
1055 struct gl_uniform_storage
*uniform
= NULL
;
1057 if (state
->set_top_level_array
&&
1058 nir_variable_is_in_ssbo(state
->current_var
)) {
1059 /* Type is the top level SSBO member */
1060 if (glsl_type_is_array(type
) &&
1061 (glsl_type_is_array(glsl_get_array_element(type
)) ||
1062 glsl_type_is_struct_or_ifc(glsl_get_array_element(type
)))) {
1063 /* Type is a top-level array (array of aggregate types) */
1064 state
->top_level_array_size
= glsl_get_length(type
);
1065 state
->top_level_array_stride
= glsl_get_explicit_stride(type
);
1067 state
->top_level_array_size
= 1;
1068 state
->top_level_array_stride
= 0;
1071 state
->set_top_level_array
= false;
1074 /* gl_uniform_storage can cope with one level of array, so if the type is a
1075 * composite type or an array where each element occupies more than one
1076 * location than we need to recursively process it.
1078 if (glsl_type_is_struct_or_ifc(type
) ||
1079 (glsl_type_is_array(type
) &&
1080 (glsl_type_is_array(glsl_get_array_element(type
)) ||
1081 glsl_type_is_struct_or_ifc(glsl_get_array_element(type
))))) {
1082 int location_count
= 0;
1083 struct type_tree_entry
*old_type
= state
->current_type
;
1084 unsigned int struct_base_offset
= state
->offset
;
1086 state
->current_type
= old_type
->children
;
1088 /* Shader storage block unsized arrays: add subscript [0] to variable
1091 unsigned length
= glsl_get_length(type
);
1092 if (glsl_type_is_unsized_array(type
))
1095 if (glsl_type_is_struct(type
) && !prog
->data
->spirv
)
1096 enter_record(state
, ctx
, type
, row_major
);
1098 for (unsigned i
= 0; i
< length
; i
++) {
1099 const struct glsl_type
*field_type
;
1100 size_t new_length
= name_length
;
1101 bool field_row_major
= row_major
;
1103 if (glsl_type_is_struct_or_ifc(type
)) {
1104 field_type
= glsl_get_struct_field(type
, i
);
1105 /* Use the offset inside the struct only for variables backed by
1106 * a buffer object. For variables not backed by a buffer object,
1109 if (state
->var_is_in_block
) {
1110 if (prog
->data
->spirv
) {
1112 struct_base_offset
+ glsl_get_struct_field_offset(type
, i
);
1113 } else if (glsl_get_struct_field_offset(type
, i
) != -1 &&
1114 type
== state
->current_ifc_type
) {
1115 state
->offset
= glsl_get_struct_field_offset(type
, i
);
1118 if (glsl_type_is_interface(type
))
1119 state
->set_top_level_array
= true;
1122 /* Append '.field' to the current variable name. */
1124 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s",
1125 glsl_get_struct_elem_name(type
, i
));
1129 /* The layout of structures at the top level of the block is set
1130 * during parsing. For matrices contained in multiple levels of
1131 * structures in the block, the inner structures have no layout.
1132 * These cases must potentially inherit the layout from the outer
1135 const enum glsl_matrix_layout matrix_layout
=
1136 glsl_get_struct_field_data(type
, i
)->matrix_layout
;
1137 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1138 field_row_major
= true;
1139 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1140 field_row_major
= false;
1143 field_type
= glsl_get_array_element(type
);
1145 /* Append the subscript to the current variable name */
1147 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
1150 int entries
= nir_link_uniform(ctx
, prog
, stage_program
, stage
,
1151 field_type
, i
, location
,
1152 state
, name
, new_length
,
1159 location
+= entries
;
1160 location_count
+= entries
;
1162 if (glsl_type_is_struct_or_ifc(type
))
1163 state
->current_type
= state
->current_type
->next_sibling
;
1166 if (glsl_type_is_struct(type
) && !prog
->data
->spirv
)
1167 leave_record(state
, ctx
, type
, row_major
);
1169 state
->current_type
= old_type
;
1171 return location_count
;
1173 /* TODO: reallocating storage is slow, we should figure out a way to
1174 * allocate storage up front for spirv like we do for GLSL.
1176 if (prog
->data
->spirv
) {
1177 /* Create a new uniform storage entry */
1178 prog
->data
->UniformStorage
=
1179 reralloc(prog
->data
,
1180 prog
->data
->UniformStorage
,
1181 struct gl_uniform_storage
,
1182 prog
->data
->NumUniformStorage
+ 1);
1183 if (!prog
->data
->UniformStorage
) {
1184 linker_error(prog
, "Out of memory during linking.\n");
1189 uniform
= &prog
->data
->UniformStorage
[prog
->data
->NumUniformStorage
];
1190 prog
->data
->NumUniformStorage
++;
1192 /* Initialize its members */
1193 memset(uniform
, 0x00, sizeof(struct gl_uniform_storage
));
1196 name
? ralloc_strdup(prog
->data
->UniformStorage
, *name
) : NULL
;
1198 const struct glsl_type
*type_no_array
= glsl_without_array(type
);
1199 if (glsl_type_is_array(type
)) {
1200 uniform
->type
= type_no_array
;
1201 uniform
->array_elements
= glsl_get_length(type
);
1203 uniform
->type
= type
;
1204 uniform
->array_elements
= 0;
1206 uniform
->top_level_array_size
= state
->top_level_array_size
;
1207 uniform
->top_level_array_stride
= state
->top_level_array_stride
;
1209 struct hash_entry
*entry
=
1210 _mesa_hash_table_search(state
->referenced_uniforms
,
1211 state
->current_var
);
1212 if (entry
!= NULL
||
1213 glsl_get_base_type(type_no_array
) == GLSL_TYPE_SUBROUTINE
)
1214 uniform
->active_shader_mask
|= 1 << stage
;
1216 if (location
>= 0) {
1217 /* Uniform has an explicit location */
1218 uniform
->remap_location
= location
;
1220 uniform
->remap_location
= UNMAPPED_UNIFORM_LOC
;
1223 uniform
->hidden
= state
->current_var
->data
.how_declared
== nir_var_hidden
;
1224 if (uniform
->hidden
)
1225 state
->num_hidden_uniforms
++;
1227 uniform
->is_shader_storage
= nir_variable_is_in_ssbo(state
->current_var
);
1228 uniform
->is_bindless
= state
->current_var
->data
.bindless
;
1230 /* Set fields whose default value depend on the variable being inside a
1233 * From the OpenGL 4.6 spec, 7.3 Program objects:
1235 * "For the property ARRAY_STRIDE, ... For active variables not declared
1236 * as an array of basic types, zero is written to params. For active
1237 * variables not backed by a buffer object, -1 is written to params,
1238 * regardless of the variable type."
1240 * "For the property MATRIX_STRIDE, ... For active variables not declared
1241 * as a matrix or array of matrices, zero is written to params. For active
1242 * variables not backed by a buffer object, -1 is written to params,
1243 * regardless of the variable type."
1245 * For the property IS_ROW_MAJOR, ... For active variables backed by a
1246 * buffer object, declared as a single matrix or array of matrices, and
1247 * stored in row-major order, one is written to params. For all other
1248 * active variables, zero is written to params.
1250 uniform
->array_stride
= -1;
1251 uniform
->matrix_stride
= -1;
1252 uniform
->row_major
= false;
1254 if (state
->var_is_in_block
) {
1255 uniform
->array_stride
= glsl_type_is_array(type
) ?
1256 glsl_get_explicit_stride(type
) : 0;
1258 if (glsl_type_is_matrix(uniform
->type
)) {
1259 uniform
->matrix_stride
= glsl_get_explicit_stride(uniform
->type
);
1260 uniform
->row_major
= glsl_matrix_type_is_row_major(uniform
->type
);
1262 uniform
->matrix_stride
= 0;
1265 if (!prog
->data
->spirv
) {
1266 bool use_std430
= ctx
->Const
.UseSTD430AsDefaultPacking
;
1267 const enum glsl_interface_packing packing
=
1268 glsl_get_internal_ifc_packing(state
->current_var
->interface_type
,
1271 unsigned alignment
=
1272 glsl_get_std140_base_alignment(type
, uniform
->row_major
);
1273 if (packing
== GLSL_INTERFACE_PACKING_STD430
) {
1275 glsl_get_std430_base_alignment(type
, uniform
->row_major
);
1277 state
->offset
= glsl_align(state
->offset
, alignment
);
1281 uniform
->offset
= state
->var_is_in_block
? state
->offset
: -1;
1283 int buffer_block_index
= -1;
1284 /* If the uniform is inside a uniform block determine its block index by
1285 * comparing the bindings, we can not use names.
1287 if (state
->var_is_in_block
) {
1288 struct gl_uniform_block
*blocks
= nir_variable_is_in_ssbo(state
->current_var
) ?
1289 prog
->data
->ShaderStorageBlocks
: prog
->data
->UniformBlocks
;
1291 int num_blocks
= nir_variable_is_in_ssbo(state
->current_var
) ?
1292 prog
->data
->NumShaderStorageBlocks
: prog
->data
->NumUniformBlocks
;
1294 if (!prog
->data
->spirv
) {
1295 bool is_interface_array
=
1296 glsl_without_array(state
->current_var
->type
) == state
->current_var
->interface_type
&&
1297 glsl_type_is_array(state
->current_var
->type
);
1299 const char *ifc_name
=
1300 glsl_get_type_name(state
->current_var
->interface_type
);
1301 if (is_interface_array
) {
1302 unsigned l
= strlen(ifc_name
);
1303 for (unsigned i
= 0; i
< num_blocks
; i
++) {
1304 if (strncmp(ifc_name
, blocks
[i
].Name
, l
) == 0 &&
1305 blocks
[i
].Name
[l
] == '[') {
1306 buffer_block_index
= i
;
1311 for (unsigned i
= 0; i
< num_blocks
; i
++) {
1312 if (strcmp(ifc_name
, blocks
[i
].Name
) == 0) {
1313 buffer_block_index
= i
;
1319 /* Compute the next offset. */
1320 bool use_std430
= ctx
->Const
.UseSTD430AsDefaultPacking
;
1321 const enum glsl_interface_packing packing
=
1322 glsl_get_internal_ifc_packing(state
->current_var
->interface_type
,
1324 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
1325 state
->offset
+= glsl_get_std430_size(type
, uniform
->row_major
);
1327 state
->offset
+= glsl_get_std140_size(type
, uniform
->row_major
);
1329 for (unsigned i
= 0; i
< num_blocks
; i
++) {
1330 if (state
->current_var
->data
.binding
== blocks
[i
].Binding
) {
1331 buffer_block_index
= i
;
1336 /* Compute the next offset. */
1337 state
->offset
+= glsl_get_explicit_size(type
, true);
1339 assert(buffer_block_index
>= 0);
1342 uniform
->block_index
= buffer_block_index
;
1343 uniform
->builtin
= is_gl_identifier(uniform
->name
);
1344 uniform
->atomic_buffer_index
= -1;
1346 /* The following are not for features not supported by ARB_gl_spirv */
1347 uniform
->num_compatible_subroutines
= 0;
1349 unsigned entries
= MAX2(1, uniform
->array_elements
);
1350 unsigned values
= glsl_get_component_slots(type
);
1352 update_uniforms_shader_info(prog
, state
, uniform
, type
, stage
);
1354 if (uniform
->remap_location
!= UNMAPPED_UNIFORM_LOC
&&
1355 state
->max_uniform_location
< uniform
->remap_location
+ entries
)
1356 state
->max_uniform_location
= uniform
->remap_location
+ entries
;
1358 if (!state
->var_is_in_block
)
1359 add_parameter(uniform
, ctx
, prog
, type
, state
);
1362 _mesa_hash_table_insert(state
->uniform_hash
, strdup(*name
),
1364 (prog
->data
->NumUniformStorage
- 1));
1367 if (!is_gl_identifier(uniform
->name
) && !uniform
->is_shader_storage
&&
1368 !state
->var_is_in_block
)
1369 state
->num_values
+= values
;
1371 return MAX2(uniform
->array_elements
, 1);
1376 gl_nir_link_uniforms(struct gl_context
*ctx
,
1377 struct gl_shader_program
*prog
,
1378 bool fill_parameters
)
1380 /* First free up any previous UniformStorage items */
1381 ralloc_free(prog
->data
->UniformStorage
);
1382 prog
->data
->UniformStorage
= NULL
;
1383 prog
->data
->NumUniformStorage
= 0;
1385 /* Count total number of uniforms and allocate storage */
1386 unsigned storage_size
= 0;
1387 if (!prog
->data
->spirv
) {
1388 struct set
*storage_counted
=
1389 _mesa_set_create(NULL
, _mesa_hash_string
, _mesa_key_string_equal
);
1390 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1391 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[stage
];
1395 nir_foreach_variable(var
, &sh
->Program
->nir
->uniforms
) {
1396 const struct glsl_type
*type
= var
->type
;
1397 const char *name
= var
->name
;
1398 if (nir_variable_is_in_block(var
) &&
1399 glsl_without_array(type
) == var
->interface_type
) {
1400 type
= glsl_without_array(var
->type
);
1401 name
= glsl_get_type_name(type
);
1404 struct set_entry
*entry
= _mesa_set_search(storage_counted
, name
);
1406 storage_size
+= uniform_storage_size(type
);
1407 _mesa_set_add(storage_counted
, name
);
1411 _mesa_set_destroy(storage_counted
, NULL
);
1413 prog
->data
->UniformStorage
= rzalloc_array(prog
->data
,
1414 struct gl_uniform_storage
,
1416 if (!prog
->data
->UniformStorage
) {
1417 linker_error(prog
, "Out of memory while linking uniforms.\n");
1422 /* Iterate through all linked shaders */
1423 struct nir_link_uniforms_state state
= {0,};
1424 state
.uniform_hash
= _mesa_hash_table_create(NULL
, _mesa_hash_string
,
1425 _mesa_key_string_equal
);
1427 for (unsigned shader_type
= 0; shader_type
< MESA_SHADER_STAGES
; shader_type
++) {
1428 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[shader_type
];
1432 nir_shader
*nir
= sh
->Program
->nir
;
1435 state
.referenced_uniforms
=
1436 _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1437 _mesa_key_pointer_equal
);
1438 state
.next_bindless_image_index
= 0;
1439 state
.next_bindless_sampler_index
= 0;
1440 state
.next_image_index
= 0;
1441 state
.next_sampler_index
= 0;
1442 state
.num_shader_samplers
= 0;
1443 state
.num_shader_images
= 0;
1444 state
.num_shader_uniform_components
= 0;
1445 state
.shader_storage_blocks_write_access
= 0;
1446 state
.shader_samplers_used
= 0;
1447 state
.shader_shadow_samplers
= 0;
1448 state
.params
= fill_parameters
? sh
->Program
->Parameters
: NULL
;
1450 add_var_use_shader(nir
, state
.referenced_uniforms
);
1452 nir_foreach_variable(var
, &nir
->uniforms
) {
1453 state
.current_var
= var
;
1454 state
.current_ifc_type
= NULL
;
1456 state
.var_is_in_block
= nir_variable_is_in_block(var
);
1457 state
.set_top_level_array
= false;
1458 state
.top_level_array_size
= 0;
1459 state
.top_level_array_stride
= 0;
1462 * From ARB_program_interface spec, issue (16):
1464 * "RESOLVED: We will follow the default rule for enumerating block
1465 * members in the OpenGL API, which is:
1467 * * If a variable is a member of an interface block without an
1468 * instance name, it is enumerated using just the variable name.
1470 * * If a variable is a member of an interface block with an
1471 * instance name, it is enumerated as "BlockName.Member", where
1472 * "BlockName" is the name of the interface block (not the
1473 * instance name) and "Member" is the name of the variable.
1475 * For example, in the following code:
1485 * } instance3[2]; // uses two separate buffer bindings
1487 * the three uniforms (if active) are enumerated as "member1",
1488 * "Block2.member2", and "Block3.member3"."
1490 * Note that in the last example, with an array of ubo, only one
1491 * uniform is generated. For that reason, while unrolling the
1492 * uniforms of a ubo, or the variables of a ssbo, we need to treat
1493 * arrays of instance as a single block.
1496 const struct glsl_type
*type
= var
->type
;
1497 if (state
.var_is_in_block
&&
1498 ((!prog
->data
->spirv
&& glsl_without_array(type
) == var
->interface_type
) ||
1499 (prog
->data
->spirv
&& type
== var
->interface_type
))) {
1500 type
= glsl_without_array(var
->type
);
1501 state
.current_ifc_type
= type
;
1502 name
= ralloc_strdup(NULL
, glsl_get_type_name(type
));
1504 state
.set_top_level_array
= true;
1505 name
= ralloc_strdup(NULL
, var
->name
);
1508 struct type_tree_entry
*type_tree
=
1509 build_type_tree_for_type(type
);
1510 state
.current_type
= type_tree
;
1512 int location
= var
->data
.location
;
1514 struct gl_uniform_block
*blocks
;
1516 int buffer_block_index
= -1;
1517 if (!prog
->data
->spirv
&& state
.var_is_in_block
) {
1518 /* If the uniform is inside a uniform block determine its block index by
1519 * comparing the bindings, we can not use names.
1521 blocks
= nir_variable_is_in_ssbo(state
.current_var
) ?
1522 prog
->data
->ShaderStorageBlocks
: prog
->data
->UniformBlocks
;
1523 num_blocks
= nir_variable_is_in_ssbo(state
.current_var
) ?
1524 prog
->data
->NumShaderStorageBlocks
: prog
->data
->NumUniformBlocks
;
1526 bool is_interface_array
=
1527 glsl_without_array(state
.current_var
->type
) == state
.current_var
->interface_type
&&
1528 glsl_type_is_array(state
.current_var
->type
);
1530 const char *ifc_name
=
1531 glsl_get_type_name(state
.current_var
->interface_type
);
1533 if (is_interface_array
) {
1534 unsigned l
= strlen(ifc_name
);
1536 /* Even when a match is found, do not "break" here. As this is
1537 * an array of instances, all elements of the array need to be
1538 * marked as referenced.
1540 for (unsigned i
= 0; i
< num_blocks
; i
++) {
1541 if (strncmp(ifc_name
, blocks
[i
].Name
, l
) == 0 &&
1542 blocks
[i
].Name
[l
] == '[') {
1543 if (buffer_block_index
== -1)
1544 buffer_block_index
= i
;
1546 struct hash_entry
*entry
=
1547 _mesa_hash_table_search(state
.referenced_uniforms
, var
);
1549 BITSET_WORD
*bits
= (BITSET_WORD
*) entry
->data
;
1550 if (BITSET_TEST(bits
, blocks
[i
].linearized_array_index
))
1551 blocks
[i
].stageref
|= 1U << shader_type
;
1556 for (unsigned i
= 0; i
< num_blocks
; i
++) {
1557 if (strcmp(ifc_name
, blocks
[i
].Name
) == 0) {
1558 buffer_block_index
= i
;
1560 struct hash_entry
*entry
=
1561 _mesa_hash_table_search(state
.referenced_uniforms
, var
);
1563 blocks
[i
].stageref
|= 1U << shader_type
;
1570 if (nir_variable_is_in_ssbo(var
) &&
1571 !(var
->data
.access
& ACCESS_NON_WRITEABLE
)) {
1572 unsigned array_size
= is_interface_array
?
1573 glsl_get_length(var
->type
) : 1;
1575 STATIC_ASSERT(MAX_SHADER_STORAGE_BUFFERS
<= 32);
1577 /* Shaders that use too many SSBOs will fail to compile, which
1578 * we don't care about.
1580 * This is true for shaders that do not use too many SSBOs:
1582 if (buffer_block_index
+ array_size
<= 32) {
1583 state
.shader_storage_blocks_write_access
|=
1584 u_bit_consecutive(buffer_block_index
, array_size
);
1589 if (!prog
->data
->spirv
&& state
.var_is_in_block
&&
1590 glsl_without_array(state
.current_var
->type
) != state
.current_var
->interface_type
) {
1593 char sentinel
= '\0';
1595 if (glsl_type_is_struct(state
.current_var
->type
)) {
1597 } else if (glsl_type_is_array(state
.current_var
->type
) &&
1598 (glsl_type_is_array(glsl_get_array_element(state
.current_var
->type
))
1599 || glsl_type_is_struct(glsl_without_array(state
.current_var
->type
)))) {
1603 const unsigned l
= strlen(state
.current_var
->name
);
1604 for (unsigned i
= 0; i
< num_blocks
; i
++) {
1605 for (unsigned j
= 0; j
< blocks
[i
].NumUniforms
; j
++) {
1607 const char *begin
= blocks
[i
].Uniforms
[j
].Name
;
1608 const char *end
= strchr(begin
, sentinel
);
1613 if ((ptrdiff_t) l
!= (end
- begin
))
1615 found
= strncmp(state
.current_var
->name
, begin
, l
) == 0;
1617 found
= strcmp(state
.current_var
->name
, blocks
[i
].Uniforms
[j
].Name
) == 0;
1623 struct hash_entry
*entry
=
1624 _mesa_hash_table_search(state
.referenced_uniforms
, var
);
1626 blocks
[i
].stageref
|= 1U << shader_type
;
1637 const struct gl_uniform_block
*const block
=
1638 &blocks
[buffer_block_index
];
1639 assert(location
!= -1);
1641 const struct gl_uniform_buffer_variable
*const ubo_var
=
1642 &block
->Uniforms
[location
];
1644 state
.offset
= ubo_var
->Offset
;
1645 var
->data
.location
= location
;
1648 /* Check if the uniform has been processed already for
1649 * other stage. If so, validate they are compatible and update
1650 * the active stage mask.
1652 if (find_and_update_previous_uniform_storage(ctx
, prog
, &state
, var
,
1653 name
, type
, shader_type
)) {
1655 free_type_tree(type_tree
);
1659 /* From now on the variable’s location will be its uniform index */
1660 if (!state
.var_is_in_block
)
1661 var
->data
.location
= prog
->data
->NumUniformStorage
;
1666 var
->data
.matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
;
1667 int res
= nir_link_uniform(ctx
, prog
, sh
->Program
, shader_type
, type
,
1670 !prog
->data
->spirv
? &name
: NULL
,
1671 !prog
->data
->spirv
? strlen(name
) : 0,
1674 free_type_tree(type_tree
);
1681 _mesa_hash_table_destroy(state
.referenced_uniforms
, NULL
);
1683 if (state
.num_shader_samplers
>
1684 ctx
->Const
.Program
[shader_type
].MaxTextureImageUnits
) {
1685 linker_error(prog
, "Too many %s shader texture samplers\n",
1686 _mesa_shader_stage_to_string(shader_type
));
1690 if (state
.num_shader_images
>
1691 ctx
->Const
.Program
[shader_type
].MaxImageUniforms
) {
1692 linker_error(prog
, "Too many %s shader image uniforms (%u > %u)\n",
1693 _mesa_shader_stage_to_string(shader_type
),
1694 state
.num_shader_images
,
1695 ctx
->Const
.Program
[shader_type
].MaxImageUniforms
);
1699 sh
->Program
->SamplersUsed
= state
.shader_samplers_used
;
1700 sh
->Program
->sh
.ShaderStorageBlocksWriteAccess
=
1701 state
.shader_storage_blocks_write_access
;
1702 sh
->shadow_samplers
= state
.shader_shadow_samplers
;
1703 sh
->Program
->info
.num_textures
= state
.num_shader_samplers
;
1704 sh
->Program
->info
.num_images
= state
.num_shader_images
;
1705 sh
->num_uniform_components
= state
.num_shader_uniform_components
;
1706 sh
->num_combined_uniform_components
= sh
->num_uniform_components
;
1709 prog
->data
->NumHiddenUniforms
= state
.num_hidden_uniforms
;
1710 prog
->data
->NumUniformDataSlots
= state
.num_values
;
1712 assert(prog
->data
->spirv
|| prog
->data
->NumUniformStorage
== storage_size
);
1714 if (prog
->data
->spirv
)
1715 prog
->NumUniformRemapTable
= state
.max_uniform_location
;
1717 nir_setup_uniform_remap_tables(ctx
, prog
);
1718 gl_nir_set_uniform_initializers(ctx
, prog
);
1720 _mesa_hash_table_destroy(state
.uniform_hash
, hash_free_uniform_name
);