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 "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"
32 * This file do the common link for GLSL uniforms, using NIR, instead of IR as
33 * the counter-part glsl/link_uniforms.cpp
36 #define UNMAPPED_UNIFORM_LOC ~0u
39 nir_setup_uniform_remap_tables(struct gl_context
*ctx
,
40 struct gl_shader_program
*prog
)
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");
52 prog
->data
->UniformDataSlots
= data
;
54 prog
->data
->UniformDataDefaults
=
55 rzalloc_array(prog
->data
->UniformDataSlots
,
56 union gl_constant_value
, prog
->data
->NumUniformDataSlots
);
58 unsigned data_pos
= 0;
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
];
64 if (prog
->data
->UniformStorage
[i
].remap_location
== UNMAPPED_UNIFORM_LOC
)
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
);
71 uniform
->storage
= &data
[data_pos
];
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
;
78 data_pos
+= num_slots
;
82 /* Reserve locations for rest of the uniforms. */
83 link_util_update_empty_uniform_locations(prog
);
85 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
86 struct gl_uniform_storage
*uniform
= &prog
->data
->UniformStorage
[i
];
88 if (uniform
->is_shader_storage
)
91 /* Built-in uniforms should not get any location. */
95 /* Explicit ones have been set already. */
96 if (uniform
->remap_location
!= UNMAPPED_UNIFORM_LOC
)
99 /* How many entries for this uniform? */
100 const unsigned entries
= MAX2(1, uniform
->array_elements
);
103 link_util_find_empty_block(prog
, &prog
->data
->UniformStorage
[i
]);
105 if (location
== -1 || location
+ entries
>= prog
->NumUniformRemapTable
) {
106 unsigned new_entries
= entries
;
108 location
= prog
->NumUniformRemapTable
;
110 new_entries
= location
- prog
->NumUniformRemapTable
+ entries
;
112 /* resize remap table to fit new entries */
113 prog
->UniformRemapTable
=
115 prog
->UniformRemapTable
,
116 struct gl_uniform_storage
*,
117 prog
->NumUniformRemapTable
+ new_entries
);
118 prog
->NumUniformRemapTable
+= new_entries
;
121 /* set the base location in remap table for the uniform */
122 uniform
->remap_location
= location
;
124 unsigned num_slots
= glsl_get_component_slots(uniform
->type
);
126 uniform
->storage
= &data
[data_pos
];
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
;
133 data_pos
+= num_slots
;
139 mark_stage_as_active(struct gl_uniform_storage
*uniform
,
142 uniform
->active_shader_mask
|= 1 << stage
;
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.
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.
161 struct type_tree_entry
*parent
;
162 struct type_tree_entry
*next_sibling
;
163 struct type_tree_entry
*children
;
166 struct nir_link_uniforms_state
{
167 /* per-whole program */
168 unsigned num_hidden_uniforms
;
170 unsigned max_uniform_location
;
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
;
184 nir_variable
*current_var
;
185 const struct glsl_type
*current_ifc_type
;
187 bool var_is_in_block
;
188 bool set_top_level_array
;
189 int top_level_array_size
;
190 int top_level_array_stride
;
192 struct type_tree_entry
*current_type
;
193 struct hash_table
*uniform_hash
;
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
)
203 if (!state
->params
|| uniform
->is_shader_storage
|| glsl_contains_opaque(type
))
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
));
210 bool is_dual_slot
= glsl_type_is_dual_slot(glsl_without_array(type
));
214 struct gl_program_parameter_list
*params
= state
->params
;
215 int base_index
= params
->NumParameters
;
216 _mesa_reserve_parameter_storage(params
, num_params
);
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
;
229 _mesa_add_parameter(params
, PROGRAM_UNIFORM
, uniform
->name
, comps
,
230 glsl_get_gl_type(type
), NULL
, NULL
, false);
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);
239 /* Each Parameter will hold the index to the backing uniform storage.
240 * This avoids relying on names to match parameters and uniform
243 for (unsigned i
= 0; i
< num_params
; i
++) {
244 struct gl_program_parameter
*param
= ¶ms
->Parameters
[base_index
+ i
];
245 param
->UniformStorageIndex
= uniform
- prog
->data
->UniformStorage
;
246 param
->MainUniformStorageIndex
= state
->current_var
->data
.location
;
251 get_next_index(struct nir_link_uniforms_state
*state
,
252 const struct gl_uniform_storage
*uniform
,
253 unsigned *next_index
, bool *initialised
)
255 /* If we’ve already calculated an index for this member then we can just
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.
263 unsigned array_size
= 1;
265 for (const struct type_tree_entry
*p
= state
->current_type
;
268 array_size
*= p
->array_size
;
271 state
->current_type
->next_index
= *next_index
;
272 *next_index
+= array_size
;
275 *initialised
= false;
277 unsigned index
= state
->current_type
->next_index
;
279 state
->current_type
->next_index
+= MAX2(1, uniform
->array_elements
);
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
,
290 const struct glsl_type
*type
,
291 unsigned stage
, bool *first_element
)
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.
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
))))) {
302 struct type_tree_entry
*old_type
= state
->current_type
;
303 state
->current_type
= old_type
->children
;
305 /* Shader storage block unsized arrays: add subscript [0] to variable
308 unsigned length
= glsl_get_length(type
);
309 if (glsl_type_is_unsized_array(type
))
313 for (unsigned i
= 0; i
< length
; i
++) {
314 const struct glsl_type
*field_type
;
315 size_t new_length
= name_length
;
317 if (glsl_type_is_struct_or_ifc(type
)) {
318 field_type
= glsl_get_struct_field(type
, i
);
320 /* Append '.field' to the current variable name. */
322 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s",
323 glsl_get_struct_elem_name(type
, i
));
326 field_type
= glsl_get_array_element(type
);
328 /* Append the subscript to the current variable name */
330 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
333 result
= find_and_update_named_uniform_storage(ctx
, prog
, state
,
334 var
, name
, new_length
,
338 if (glsl_type_is_struct_or_ifc(type
))
339 state
->current_type
= state
->current_type
->next_sibling
;
342 state
->current_type
= old_type
;
347 state
->current_type
= old_type
;
351 struct hash_entry
*entry
=
352 _mesa_hash_table_search(state
->uniform_hash
, *name
);
354 unsigned i
= (unsigned) (intptr_t) entry
->data
;
355 mark_stage_as_active(&prog
->data
->UniformStorage
[i
], stage
);
357 struct gl_uniform_storage
*uniform
= &prog
->data
->UniformStorage
[i
];
359 if (*first_element
&& !state
->var_is_in_block
) {
360 *first_element
= false;
361 var
->data
.location
= uniform
- prog
->data
->UniformStorage
;
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
];
369 unsigned sampler_index
=
370 get_next_index(state
, uniform
, &state
->next_sampler_index
,
373 /* Samplers (bound or bindless) are counted as two components as
374 * specified by ARB_bindless_texture.
376 state
->num_shader_samplers
+= values
/ 2;
378 uniform
->opaque
[stage
].active
= true;
379 uniform
->opaque
[stage
].index
= sampler_index
;
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
);
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
;
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
);
399 /* Images (bound or bindless) are counted as two components as
400 * specified by ARB_bindless_texture.
402 state
->num_shader_images
+= values
/ 2;
404 uniform
->opaque
[stage
].active
= true;
405 uniform
->opaque
[stage
].index
= image_index
;
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
:
414 ((image_access
& ACCESS_NON_READABLE
) ? GL_WRITE_ONLY
:
416 for (unsigned i
= image_index
;
417 i
< MIN2(state
->next_image_index
, MAX_IMAGE_UNIFORMS
);
419 sh
->Program
->sh
.ImageAccess
[i
] = access
;
423 uniform
->active_shader_mask
|= 1 << stage
;
425 if (!state
->var_is_in_block
)
426 add_parameter(uniform
, ctx
, prog
, type
, state
);
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:
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.
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.
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
,
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
,
462 strlen(name_tmp
), type
,
463 stage
, &first_element
);
464 ralloc_free(name_tmp
);
469 if (nir_variable_is_in_block(var
)) {
470 struct gl_uniform_storage
*uniform
= NULL
;
472 ASSERTED
unsigned num_blks
= nir_variable_is_in_ubo(var
) ?
473 prog
->data
->NumUniformBlocks
:
474 prog
->data
->NumShaderStorageBlocks
;
476 struct gl_uniform_block
*blks
= nir_variable_is_in_ubo(var
) ?
477 prog
->data
->UniformBlocks
: prog
->data
->ShaderStorageBlocks
;
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
))
486 int block_index
= prog
->data
->UniformStorage
[i
].block_index
;
487 if (block_index
!= -1) {
488 assert(block_index
< num_blks
);
490 if (var
->data
.binding
== blks
[block_index
].Binding
) {
492 uniform
= &prog
->data
->UniformStorage
[i
];
493 mark_stage_as_active(&prog
->data
->UniformStorage
[i
],
501 var
->data
.location
= uniform
- prog
->data
->UniformStorage
;
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
510 if (var
->data
.location
== -1)
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
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
);
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
);
530 static struct type_tree_entry
*
531 build_type_tree_for_type(const struct glsl_type
*type
)
533 struct type_tree_entry
*entry
= malloc(sizeof *entry
);
535 entry
->array_size
= 1;
536 entry
->next_index
= UINT_MAX
;
537 entry
->children
= NULL
;
538 entry
->next_sibling
= NULL
;
539 entry
->parent
= NULL
;
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
;
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
);
554 entry
->children
= field_entry
;
556 last
->next_sibling
= field_entry
;
558 field_entry
->parent
= entry
;
568 free_type_tree(struct type_tree_entry
*entry
)
570 struct type_tree_entry
*p
, *next
;
572 for (p
= entry
->children
; p
; p
= next
) {
573 next
= p
->next_sibling
;
581 hash_free_uniform_name(struct hash_entry
*entry
)
583 free((void*)entry
->key
);
587 * Creates the neccessary entries in UniformStorage for the uniform. Returns
588 * the number of locations used or -1 on failure.
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
,
598 struct nir_link_uniforms_state
*state
,
599 char **name
, size_t name_length
)
601 struct gl_uniform_storage
*uniform
= NULL
;
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
);
613 state
->top_level_array_size
= 1;
614 state
->top_level_array_stride
= 0;
617 state
->set_top_level_array
= false;
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.
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
;
632 state
->current_type
= old_type
->children
;
634 /* Shader storage block unsized arrays: add subscript [0] to variable
637 unsigned length
= glsl_get_length(type
);
638 if (glsl_type_is_unsized_array(type
))
641 for (unsigned i
= 0; i
< length
; i
++) {
642 const struct glsl_type
*field_type
;
643 size_t new_length
= name_length
;
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,
651 if (state
->var_is_in_block
) {
652 if (prog
->data
->spirv
) {
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
);
660 if (glsl_type_is_interface(type
))
661 state
->set_top_level_array
= true;
664 /* Append '.field' to the current variable name. */
666 ralloc_asprintf_rewrite_tail(name
, &new_length
, ".%s",
667 glsl_get_struct_elem_name(type
, i
));
670 field_type
= glsl_get_array_element(type
);
672 /* Append the subscript to the current variable name */
674 ralloc_asprintf_rewrite_tail(name
, &new_length
, "[%u]", i
);
677 int entries
= nir_link_uniform(ctx
, prog
, stage_program
, stage
,
678 field_type
, i
, location
,
679 state
, name
, new_length
);
685 location_count
+= entries
;
687 if (glsl_type_is_struct_or_ifc(type
))
688 state
->current_type
= state
->current_type
->next_sibling
;
691 state
->current_type
= old_type
;
693 return location_count
;
695 /* Create a new uniform storage entry */
696 prog
->data
->UniformStorage
=
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");
706 uniform
= &prog
->data
->UniformStorage
[prog
->data
->NumUniformStorage
];
707 prog
->data
->NumUniformStorage
++;
709 /* Initialize its members */
710 memset(uniform
, 0x00, sizeof(struct gl_uniform_storage
));
713 name
? ralloc_strdup(prog
->data
->UniformStorage
, *name
) : NULL
;
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
);
720 uniform
->type
= type
;
721 uniform
->array_elements
= 0;
723 uniform
->top_level_array_size
= state
->top_level_array_size
;
724 uniform
->top_level_array_stride
= state
->top_level_array_stride
;
726 uniform
->active_shader_mask
|= 1 << stage
;
729 /* Uniform has an explicit location */
730 uniform
->remap_location
= location
;
732 uniform
->remap_location
= UNMAPPED_UNIFORM_LOC
;
735 uniform
->hidden
= state
->current_var
->data
.how_declared
== nir_var_hidden
;
737 state
->num_hidden_uniforms
++;
739 uniform
->is_shader_storage
= nir_variable_is_in_ssbo(state
->current_var
);
741 /* Set fields whose default value depend on the variable being inside a
744 * From the OpenGL 4.6 spec, 7.3 Program objects:
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."
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."
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.
761 uniform
->array_stride
= -1;
762 uniform
->matrix_stride
= -1;
763 uniform
->row_major
= false;
765 if (state
->var_is_in_block
) {
766 uniform
->array_stride
= glsl_type_is_array(type
) ?
767 glsl_get_explicit_stride(type
) : 0;
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
);
773 uniform
->matrix_stride
= 0;
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
,
783 glsl_get_std140_base_alignment(type
, uniform
->row_major
);
784 if (packing
== GLSL_INTERFACE_PACKING_STD430
) {
786 glsl_get_std430_base_alignment(type
, uniform
->row_major
);
788 state
->offset
= glsl_align(state
->offset
, alignment
);
792 uniform
->offset
= state
->var_is_in_block
? state
->offset
: -1;
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.
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
;
802 int num_blocks
= nir_variable_is_in_ssbo(state
->current_var
) ?
803 prog
->data
->NumShaderStorageBlocks
: prog
->data
->NumUniformBlocks
;
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
);
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
;
822 for (unsigned i
= 0; i
< num_blocks
; i
++) {
823 if (strcmp(ifc_name
, blocks
[i
].Name
) == 0) {
824 buffer_block_index
= i
;
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
,
835 if (packing
== GLSL_INTERFACE_PACKING_STD430
)
836 state
->offset
+= glsl_get_std430_size(type
, uniform
->row_major
);
838 state
->offset
+= glsl_get_std140_size(type
, uniform
->row_major
);
840 for (unsigned i
= 0; i
< num_blocks
; i
++) {
841 if (state
->current_var
->data
.binding
== blocks
[i
].Binding
) {
842 buffer_block_index
= i
;
847 /* Compute the next offset. */
848 state
->offset
+= glsl_get_explicit_size(type
, true);
850 assert(buffer_block_index
>= 0);
853 uniform
->block_index
= buffer_block_index
;
855 /* @FIXME: the initialization of the following will be done as we
856 * implement support for their specific features, like SSBO, atomics,
859 uniform
->builtin
= false;
860 uniform
->atomic_buffer_index
= -1;
861 uniform
->is_bindless
= false;
863 /* The following are not for features not supported by ARB_gl_spirv */
864 uniform
->num_compatible_subroutines
= 0;
866 unsigned entries
= MAX2(1, uniform
->array_elements
);
867 unsigned values
= glsl_get_component_slots(type
);
869 if (glsl_type_is_sampler(type_no_array
)) {
872 get_next_index(state
, uniform
, &state
->next_sampler_index
,
875 /* Samplers (bound or bindless) are counted as two components as
876 * specified by ARB_bindless_texture.
878 state
->num_shader_samplers
+= values
/ 2;
880 uniform
->opaque
[stage
].active
= true;
881 uniform
->opaque
[stage
].index
= sampler_index
;
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
);
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
;
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).
902 int image_index
= state
->next_image_index
;
903 state
->next_image_index
+= entries
;
905 /* Images (bound or bindless) are counted as two components as
906 * specified by ARB_bindless_texture.
908 state
->num_shader_images
+= values
/ 2;
910 uniform
->opaque
[stage
].active
= true;
911 uniform
->opaque
[stage
].index
= image_index
;
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
:
920 ((image_access
& ACCESS_NON_READABLE
) ? GL_WRITE_ONLY
:
922 for (unsigned i
= image_index
;
923 i
< MIN2(state
->next_image_index
, MAX_IMAGE_UNIFORMS
);
925 stage_program
->sh
.ImageAccess
[i
] = access
;
928 if (!uniform
->is_shader_storage
) {
929 state
->num_shader_uniform_components
+= values
;
930 state
->num_values
+= values
;
933 if (!state
->var_is_in_block
) {
934 state
->num_shader_uniform_components
+= values
;
935 state
->num_values
+= values
;
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
;
943 if (!state
->var_is_in_block
)
944 add_parameter(uniform
, ctx
, prog
, type
, state
);
947 _mesa_hash_table_insert(state
->uniform_hash
, strdup(*name
),
949 (prog
->data
->NumUniformStorage
- 1));
952 return MAX2(uniform
->array_elements
, 1);
957 gl_nir_link_uniforms(struct gl_context
*ctx
,
958 struct gl_shader_program
*prog
,
959 bool fill_parameters
)
961 /* First free up any previous UniformStorage items */
962 ralloc_free(prog
->data
->UniformStorage
);
963 prog
->data
->UniformStorage
= NULL
;
964 prog
->data
->NumUniformStorage
= 0;
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
);
971 for (unsigned shader_type
= 0; shader_type
< MESA_SHADER_STAGES
; shader_type
++) {
972 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[shader_type
];
976 nir_shader
*nir
= sh
->Program
->nir
;
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
;
989 nir_foreach_variable(var
, &nir
->uniforms
) {
990 state
.current_var
= var
;
991 state
.current_ifc_type
= NULL
;
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;
999 * From ARB_program_interface spec, issue (16):
1001 * "RESOLVED: We will follow the default rule for enumerating block
1002 * members in the OpenGL API, which is:
1004 * * If a variable is a member of an interface block without an
1005 * instance name, it is enumerated using just the variable name.
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.
1012 * For example, in the following code:
1022 * } instance3[2]; // uses two separate buffer bindings
1024 * the three uniforms (if active) are enumerated as "member1",
1025 * "Block2.member2", and "Block3.member3"."
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.
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
));
1041 state
.set_top_level_array
= true;
1042 name
= ralloc_strdup(NULL
, var
->name
);
1045 struct type_tree_entry
*type_tree
=
1046 build_type_tree_for_type(type
);
1047 state
.current_type
= type_tree
;
1049 int location
= var
->data
.location
;
1051 struct gl_uniform_block
*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.
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
;
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
);
1067 const char *ifc_name
=
1068 glsl_get_type_name(state
.current_var
->interface_type
);
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
;
1077 blocks
[i
].stageref
|= 1U << shader_type
;
1081 for (unsigned i
= 0; i
< num_blocks
; i
++) {
1082 if (strcmp(ifc_name
, blocks
[i
].Name
) == 0) {
1083 buffer_block_index
= i
;
1085 blocks
[i
].stageref
|= 1U << shader_type
;
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;
1096 STATIC_ASSERT(MAX_SHADER_STORAGE_BUFFERS
<= 32);
1098 /* Shaders that use too many SSBOs will fail to compile, which
1099 * we don't care about.
1101 * This is true for shaders that do not use too many SSBOs:
1103 if (buffer_block_index
+ array_size
<= 32) {
1104 state
.shader_storage_blocks_write_access
|=
1105 u_bit_consecutive(buffer_block_index
, array_size
);
1110 if (!prog
->data
->spirv
&& state
.var_is_in_block
&&
1111 glsl_without_array(state
.current_var
->type
) != state
.current_var
->interface_type
) {
1114 char sentinel
= '\0';
1116 if (glsl_type_is_struct(state
.current_var
->type
)) {
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
)))) {
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
++) {
1128 const char *begin
= blocks
[i
].Uniforms
[j
].Name
;
1129 const char *end
= strchr(begin
, sentinel
);
1134 if ((ptrdiff_t) l
!= (end
- begin
))
1136 found
= strncmp(state
.current_var
->name
, begin
, l
) == 0;
1138 found
= strcmp(state
.current_var
->name
, blocks
[i
].Uniforms
[j
].Name
) == 0;
1144 blocks
[i
].stageref
|= 1U << shader_type
;
1155 const struct gl_uniform_block
*const block
=
1156 &blocks
[buffer_block_index
];
1157 assert(location
!= -1);
1159 const struct gl_uniform_buffer_variable
*const ubo_var
=
1160 &block
->Uniforms
[location
];
1162 state
.offset
= ubo_var
->Offset
;
1163 var
->data
.location
= location
;
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.
1170 if (find_and_update_previous_uniform_storage(ctx
, prog
, &state
, var
,
1171 name
, type
, shader_type
)) {
1173 free_type_tree(type_tree
);
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
;
1183 int res
= nir_link_uniform(ctx
, prog
, sh
->Program
, shader_type
, type
,
1186 !prog
->data
->spirv
? &name
: NULL
,
1187 !prog
->data
->spirv
? strlen(name
) : 0);
1189 free_type_tree(type_tree
);
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
));
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
);
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
;
1222 prog
->data
->NumHiddenUniforms
= state
.num_hidden_uniforms
;
1223 prog
->NumUniformRemapTable
= state
.max_uniform_location
;
1224 prog
->data
->NumUniformDataSlots
= state
.num_values
;
1226 nir_setup_uniform_remap_tables(ctx
, prog
);
1227 gl_nir_set_uniform_initializers(ctx
, prog
);
1229 _mesa_hash_table_destroy(state
.uniform_hash
, hash_free_uniform_name
);