Add vulkan driver for BDW
[mesa.git] / src / glsl / link_uniform_block_active_visitor.cpp
1 /*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "link_uniform_block_active_visitor.h"
25 #include "program.h"
26
27 link_uniform_block_active *
28 process_block(void *mem_ctx, struct hash_table *ht, ir_variable *var)
29 {
30 const hash_entry *const existing_block =
31 _mesa_hash_table_search(ht, var->get_interface_type()->name);
32
33 const glsl_type *const block_type = var->is_interface_instance()
34 ? var->type : var->get_interface_type();
35
36
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.
40 */
41 if (existing_block == NULL) {
42 link_uniform_block_active *const b =
43 rzalloc(mem_ctx, struct link_uniform_block_active);
44
45 b->type = block_type;
46 b->has_instance_name = var->is_interface_instance();
47
48 if (var->data.explicit_binding) {
49 b->has_binding = true;
50 b->binding = var->data.binding;
51 } else {
52 b->has_binding = false;
53 b->binding = 0;
54 }
55
56 if (var->data.vk_set) {
57 b->set = var->data.set;
58 b->index = var->data.index;
59 }
60
61 _mesa_hash_table_insert(ht, var->get_interface_type()->name, (void *) b);
62 return b;
63 } else {
64 link_uniform_block_active *const b =
65 (link_uniform_block_active *) existing_block->data;
66
67 if (b->type != block_type
68 || b->has_instance_name != var->is_interface_instance())
69 return NULL;
70 else
71 return b;
72 }
73
74 assert(!"Should not get here.");
75 return NULL;
76 }
77
78 ir_visitor_status
79 link_uniform_block_active_visitor::visit(ir_variable *var)
80 {
81 if (!var->is_in_uniform_block())
82 return visit_continue;
83
84 const glsl_type *const block_type = var->is_interface_instance()
85 ? var->type : var->get_interface_type();
86
87 /* Section 2.11.6 (Uniform Variables) of the OpenGL ES 3.0.3 spec says:
88 *
89 * "All members of a named uniform block declared with a shared or
90 * std140 layout qualifier are considered active, even if they are not
91 * referenced in any shader in the program. The uniform block itself is
92 * also considered active, even if no member of the block is
93 * referenced."
94 */
95 if (block_type->interface_packing == GLSL_INTERFACE_PACKING_PACKED)
96 return visit_continue;
97
98 /* Process the block. Bail if there was an error.
99 */
100 link_uniform_block_active *const b =
101 process_block(this->mem_ctx, this->ht, var);
102 if (b == NULL) {
103 linker_error(this->prog,
104 "uniform block `%s' has mismatching definitions",
105 var->get_interface_type()->name);
106 this->success = false;
107 return visit_stop;
108 }
109
110 assert(b->num_array_elements == 0);
111 assert(b->array_elements == NULL);
112 assert(b->type != NULL);
113
114 return visit_continue;
115 }
116
117 ir_visitor_status
118 link_uniform_block_active_visitor::visit_enter(ir_dereference_array *ir)
119 {
120 ir_dereference_variable *const d = ir->array->as_dereference_variable();
121 ir_variable *const var = (d == NULL) ? NULL : d->var;
122
123 /* If the r-value being dereferenced is not a variable (e.g., a field of a
124 * structure) or is not a uniform block instance, continue.
125 *
126 * WARNING: It is not enough for the variable to be part of uniform block.
127 * It must represent the entire block. Arrays (or matrices) inside blocks
128 * that lack an instance name are handled by the ir_dereference_variable
129 * function.
130 */
131 if (var == NULL
132 || !var->is_in_uniform_block()
133 || !var->is_interface_instance())
134 return visit_continue;
135
136 /* Process the block. Bail if there was an error.
137 */
138 link_uniform_block_active *const b =
139 process_block(this->mem_ctx, this->ht, var);
140 if (b == NULL) {
141 linker_error(prog,
142 "uniform block `%s' has mismatching definitions",
143 var->get_interface_type()->name);
144 this->success = false;
145 return visit_stop;
146 }
147
148 /* Block arrays must be declared with an instance name.
149 */
150 assert(b->has_instance_name);
151 assert((b->num_array_elements == 0) == (b->array_elements == NULL));
152 assert(b->type != NULL);
153
154 ir_constant *c = ir->array_index->as_constant();
155
156 if (c) {
157 /* Index is a constant, so mark just that element used, if not already */
158 const unsigned idx = c->get_uint_component(0);
159
160 unsigned i;
161 for (i = 0; i < b->num_array_elements; i++) {
162 if (b->array_elements[i] == idx)
163 break;
164 }
165
166 assert(i <= b->num_array_elements);
167
168 if (i == b->num_array_elements) {
169 b->array_elements = reralloc(this->mem_ctx,
170 b->array_elements,
171 unsigned,
172 b->num_array_elements + 1);
173
174 b->array_elements[b->num_array_elements] = idx;
175
176 b->num_array_elements++;
177 }
178 } else {
179 /* The array index is not a constant, so mark the entire array used. */
180 assert(b->type->is_array());
181 if (b->num_array_elements < b->type->length) {
182 b->num_array_elements = b->type->length;
183 b->array_elements = reralloc(this->mem_ctx,
184 b->array_elements,
185 unsigned,
186 b->num_array_elements);
187
188 for (unsigned i = 0; i < b->num_array_elements; i++) {
189 b->array_elements[i] = i;
190 }
191 }
192 }
193
194 return visit_continue_with_parent;
195 }
196
197 ir_visitor_status
198 link_uniform_block_active_visitor::visit(ir_dereference_variable *ir)
199 {
200 ir_variable *var = ir->var;
201
202 if (!var->is_in_uniform_block())
203 return visit_continue;
204
205 assert(!var->is_interface_instance() || !var->type->is_array());
206
207 /* Process the block. Bail if there was an error.
208 */
209 link_uniform_block_active *const b =
210 process_block(this->mem_ctx, this->ht, var);
211 if (b == NULL) {
212 linker_error(this->prog,
213 "uniform block `%s' has mismatching definitions",
214 var->get_interface_type()->name);
215 this->success = false;
216 return visit_stop;
217 }
218
219 assert(b->num_array_elements == 0);
220 assert(b->array_elements == NULL);
221 assert(b->type != NULL);
222
223 return visit_continue;
224 }