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"
27 link_uniform_block_active
*
28 process_block(void *mem_ctx
, struct hash_table
*ht
, ir_variable
*var
)
30 const hash_entry
*const existing_block
=
31 _mesa_hash_table_search(ht
, var
->get_interface_type()->name
);
33 const glsl_type
*const block_type
= var
->is_interface_instance()
34 ? var
->type
: var
->get_interface_type();
37 /* If a block with this block-name has not previously been seen, add it.
38 * If a block with this block-name has been seen, it must be identical to
39 * the block currently being examined.
41 if (existing_block
== NULL
) {
42 link_uniform_block_active
*const b
=
43 rzalloc(mem_ctx
, struct link_uniform_block_active
);
46 b
->has_instance_name
= var
->is_interface_instance();
47 b
->is_shader_storage
= var
->data
.mode
== ir_var_shader_storage
;
49 if (var
->data
.explicit_binding
) {
50 b
->has_binding
= true;
51 b
->binding
= var
->data
.binding
;
53 b
->has_binding
= false;
57 _mesa_hash_table_insert(ht
, var
->get_interface_type()->name
, (void *) b
);
60 link_uniform_block_active
*const b
=
61 (link_uniform_block_active
*) existing_block
->data
;
63 if (b
->type
!= block_type
64 || b
->has_instance_name
!= var
->is_interface_instance())
70 assert(!"Should not get here.");
75 link_uniform_block_active_visitor::visit(ir_variable
*var
)
77 if (!var
->is_in_buffer_block())
78 return visit_continue
;
80 const glsl_type
*const block_type
= var
->is_interface_instance()
81 ? var
->type
: var
->get_interface_type();
83 /* Section 2.11.6 (Uniform Variables) of the OpenGL ES 3.0.3 spec says:
85 * "All members of a named uniform block declared with a shared or
86 * std140 layout qualifier are considered active, even if they are not
87 * referenced in any shader in the program. The uniform block itself is
88 * also considered active, even if no member of the block is
91 if (block_type
->interface_packing
== GLSL_INTERFACE_PACKING_PACKED
)
92 return visit_continue
;
94 /* Process the block. Bail if there was an error.
96 link_uniform_block_active
*const b
=
97 process_block(this->mem_ctx
, this->ht
, var
);
99 linker_error(this->prog
,
100 "uniform block `%s' has mismatching definitions",
101 var
->get_interface_type()->name
);
102 this->success
= false;
106 assert(b
->num_array_elements
== 0);
107 assert(b
->array_elements
== NULL
);
108 assert(b
->type
!= NULL
);
110 return visit_continue
;
114 link_uniform_block_active_visitor::visit_enter(ir_dereference_array
*ir
)
116 ir_dereference_variable
*const d
= ir
->array
->as_dereference_variable();
117 ir_variable
*const var
= (d
== NULL
) ? NULL
: d
->var
;
119 /* If the r-value being dereferenced is not a variable (e.g., a field of a
120 * structure) or is not a uniform block instance, continue.
122 * WARNING: It is not enough for the variable to be part of uniform block.
123 * It must represent the entire block. Arrays (or matrices) inside blocks
124 * that lack an instance name are handled by the ir_dereference_variable
128 || !var
->is_in_buffer_block()
129 || !var
->is_interface_instance())
130 return visit_continue
;
132 /* Process the block. Bail if there was an error.
134 link_uniform_block_active
*const b
=
135 process_block(this->mem_ctx
, this->ht
, var
);
138 "uniform block `%s' has mismatching definitions",
139 var
->get_interface_type()->name
);
140 this->success
= false;
144 /* Block arrays must be declared with an instance name.
146 assert(b
->has_instance_name
);
147 assert((b
->num_array_elements
== 0) == (b
->array_elements
== NULL
));
148 assert(b
->type
!= NULL
);
150 ir_constant
*c
= ir
->array_index
->as_constant();
153 /* Index is a constant, so mark just that element used, if not already */
154 const unsigned idx
= c
->get_uint_component(0);
157 for (i
= 0; i
< b
->num_array_elements
; i
++) {
158 if (b
->array_elements
[i
] == idx
)
162 assert(i
<= b
->num_array_elements
);
164 if (i
== b
->num_array_elements
) {
165 b
->array_elements
= reralloc(this->mem_ctx
,
168 b
->num_array_elements
+ 1);
170 b
->array_elements
[b
->num_array_elements
] = idx
;
172 b
->num_array_elements
++;
175 /* The array index is not a constant, so mark the entire array used. */
176 assert(b
->type
->is_array());
177 if (b
->num_array_elements
< b
->type
->length
) {
178 b
->num_array_elements
= b
->type
->length
;
179 b
->array_elements
= reralloc(this->mem_ctx
,
182 b
->num_array_elements
);
184 for (unsigned i
= 0; i
< b
->num_array_elements
; i
++) {
185 b
->array_elements
[i
] = i
;
190 return visit_continue_with_parent
;
194 link_uniform_block_active_visitor::visit(ir_dereference_variable
*ir
)
196 ir_variable
*var
= ir
->var
;
198 if (!var
->is_in_buffer_block())
199 return visit_continue
;
201 assert(!var
->is_interface_instance() || !var
->type
->is_array());
203 /* Process the block. Bail if there was an error.
205 link_uniform_block_active
*const b
=
206 process_block(this->mem_ctx
, this->ht
, var
);
208 linker_error(this->prog
,
209 "uniform block `%s' has mismatching definitions",
210 var
->get_interface_type()->name
);
211 this->success
= false;
215 assert(b
->num_array_elements
== 0);
216 assert(b
->array_elements
== NULL
);
217 assert(b
->type
!= NULL
);
219 return visit_continue
;