2 * Copyright © 2013 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
21 * DEALINGS IN THE SOFTWARE.
24 #include "link_uniform_block_active_visitor.h"
26 #include "linker_util.h"
28 static link_uniform_block_active
*
29 process_block(void *mem_ctx
, struct hash_table
*ht
, ir_variable
*var
)
31 const hash_entry
*const existing_block
=
32 _mesa_hash_table_search(ht
, var
->get_interface_type()->name
);
34 const glsl_type
*const block_type
= var
->is_interface_instance()
35 ? var
->type
: var
->get_interface_type();
38 /* If a block with this block-name has not previously been seen, add it.
39 * If a block with this block-name has been seen, it must be identical to
40 * the block currently being examined.
42 if (existing_block
== NULL
) {
43 link_uniform_block_active
*const b
=
44 rzalloc(mem_ctx
, struct link_uniform_block_active
);
47 b
->has_instance_name
= var
->is_interface_instance();
48 b
->is_shader_storage
= var
->data
.mode
== ir_var_shader_storage
;
50 if (var
->data
.explicit_binding
) {
51 b
->has_binding
= true;
52 b
->binding
= var
->data
.binding
;
54 b
->has_binding
= false;
58 _mesa_hash_table_insert(ht
, var
->get_interface_type()->name
, (void *) b
);
61 link_uniform_block_active
*const b
=
62 (link_uniform_block_active
*) existing_block
->data
;
64 if (b
->type
!= block_type
65 || b
->has_instance_name
!= var
->is_interface_instance())
71 assert(!"Should not get here.");
75 /* For arrays of arrays this function will give us a middle ground between
76 * detecting inactive uniform blocks and structuring them in a way that makes
77 * it easy to calculate the offset for indirect indexing.
79 * For example given the shader:
81 * uniform ArraysOfArraysBlock
88 * vec4 b = i[0][1][1].a;
89 * gl_Position = i[2][2][3].a + b;
92 * There are only 2 active blocks above but for the sake of indirect indexing
93 * and not over complicating the code we will end up with a count of 8. Here
94 * each dimension has 2 different indices counted so we end up with 2*2*2
96 static struct uniform_block_array_elements
**
97 process_arrays(void *mem_ctx
, ir_dereference_array
*ir
,
98 struct link_uniform_block_active
*block
)
101 struct uniform_block_array_elements
**ub_array_ptr
=
102 process_arrays(mem_ctx
, ir
->array
->as_dereference_array(), block
);
103 if (*ub_array_ptr
== NULL
) {
104 *ub_array_ptr
= rzalloc(mem_ctx
, struct uniform_block_array_elements
);
105 (*ub_array_ptr
)->ir
= ir
;
106 (*ub_array_ptr
)->aoa_size
=
107 ir
->array
->type
->arrays_of_arrays_size();
110 struct uniform_block_array_elements
*ub_array
= *ub_array_ptr
;
111 ir_constant
*c
= ir
->array_index
->as_constant();
113 /* Index is a constant, so mark just that element used, if not
116 const unsigned idx
= c
->get_uint_component(0);
119 for (i
= 0; i
< ub_array
->num_array_elements
; i
++) {
120 if (ub_array
->array_elements
[i
] == idx
)
124 assert(i
<= ub_array
->num_array_elements
);
126 if (i
== ub_array
->num_array_elements
) {
127 ub_array
->array_elements
= reralloc(mem_ctx
,
128 ub_array
->array_elements
,
130 ub_array
->num_array_elements
+ 1);
132 ub_array
->array_elements
[ub_array
->num_array_elements
] = idx
;
134 ub_array
->num_array_elements
++;
137 /* The array index is not a constant, so mark the entire array used. */
138 assert(ir
->array
->type
->is_array());
139 if (ub_array
->num_array_elements
< ir
->array
->type
->length
) {
140 ub_array
->num_array_elements
= ir
->array
->type
->length
;
141 ub_array
->array_elements
= reralloc(mem_ctx
,
142 ub_array
->array_elements
,
144 ub_array
->num_array_elements
);
146 for (unsigned i
= 0; i
< ub_array
->num_array_elements
; i
++) {
147 ub_array
->array_elements
[i
] = i
;
152 return &ub_array
->array
;
154 return &block
->array
;
159 link_uniform_block_active_visitor::visit(ir_variable
*var
)
161 if (!var
->is_in_buffer_block())
162 return visit_continue
;
164 /* Section 2.11.6 (Uniform Variables) of the OpenGL ES 3.0.3 spec says:
166 * "All members of a named uniform block declared with a shared or
167 * std140 layout qualifier are considered active, even if they are not
168 * referenced in any shader in the program. The uniform block itself is
169 * also considered active, even if no member of the block is
172 if (var
->get_interface_type_packing() == GLSL_INTERFACE_PACKING_PACKED
)
173 return visit_continue
;
175 /* Process the block. Bail if there was an error. */
176 link_uniform_block_active
*const b
=
177 process_block(this->mem_ctx
, this->ht
, var
);
179 linker_error(this->prog
,
180 "uniform block `%s' has mismatching definitions",
181 var
->get_interface_type()->name
);
182 this->success
= false;
186 assert(b
->array
== NULL
);
187 assert(b
->type
!= NULL
);
188 assert(!b
->type
->is_array() || b
->has_instance_name
);
190 /* For uniform block arrays declared with a shared or std140 layout
191 * qualifier, mark all its instances as used.
193 const glsl_type
*type
= b
->type
;
194 struct uniform_block_array_elements
**ub_array
= &b
->array
;
195 while (type
->is_array()) {
196 assert(b
->type
->length
> 0);
198 *ub_array
= rzalloc(this->mem_ctx
, struct uniform_block_array_elements
);
199 (*ub_array
)->num_array_elements
= type
->length
;
200 (*ub_array
)->array_elements
= reralloc(this->mem_ctx
,
201 (*ub_array
)->array_elements
,
203 (*ub_array
)->num_array_elements
);
204 (*ub_array
)->aoa_size
= type
->arrays_of_arrays_size();
206 for (unsigned i
= 0; i
< (*ub_array
)->num_array_elements
; i
++) {
207 (*ub_array
)->array_elements
[i
] = i
;
209 ub_array
= &(*ub_array
)->array
;
210 type
= type
->fields
.array
;
213 return visit_continue
;
217 link_uniform_block_active_visitor::visit_enter(ir_dereference_array
*ir
)
219 /* cycle through arrays of arrays */
220 ir_dereference_array
*base_ir
= ir
;
221 while (base_ir
->array
->ir_type
== ir_type_dereference_array
)
222 base_ir
= base_ir
->array
->as_dereference_array();
224 ir_dereference_variable
*const d
=
225 base_ir
->array
->as_dereference_variable();
226 ir_variable
*const var
= (d
== NULL
) ? NULL
: d
->var
;
228 /* If the r-value being dereferenced is not a variable (e.g., a field of a
229 * structure) or is not a uniform block instance, continue.
231 * WARNING: It is not enough for the variable to be part of uniform block.
232 * It must represent the entire block. Arrays (or matrices) inside blocks
233 * that lack an instance name are handled by the ir_dereference_variable
237 || !var
->is_in_buffer_block()
238 || !var
->is_interface_instance())
239 return visit_continue
;
241 /* Process the block. Bail if there was an error. */
242 link_uniform_block_active
*const b
=
243 process_block(this->mem_ctx
, this->ht
, var
);
246 "uniform block `%s' has mismatching definitions",
247 var
->get_interface_type()->name
);
248 this->success
= false;
252 /* Block arrays must be declared with an instance name.
254 assert(b
->has_instance_name
);
255 assert(b
->type
!= NULL
);
257 /* If the block array was declared with a shared or std140 layout
258 * qualifier, all its instances have been already marked as used in
259 * link_uniform_block_active_visitor::visit(ir_variable *).
261 if (var
->get_interface_type_packing() == GLSL_INTERFACE_PACKING_PACKED
) {
263 process_arrays(this->mem_ctx
, ir
, b
);
266 return visit_continue_with_parent
;
270 link_uniform_block_active_visitor::visit(ir_dereference_variable
*ir
)
272 ir_variable
*var
= ir
->var
;
274 if (!var
->is_in_buffer_block())
275 return visit_continue
;
277 assert(!var
->is_interface_instance() || !var
->type
->is_array());
279 /* Process the block. Bail if there was an error. */
280 link_uniform_block_active
*const b
=
281 process_block(this->mem_ctx
, this->ht
, var
);
283 linker_error(this->prog
,
284 "uniform block `%s' has mismatching definitions",
285 var
->get_interface_type()->name
);
286 this->success
= false;
290 assert(b
->array
== NULL
);
291 assert(b
->type
!= NULL
);
293 return visit_continue
;