1 /*
2 * Copyright (c) 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 /**
25 * \file lower_named_interface_blocks.cpp
26 *
27 * This lowering pass converts all interface blocks with instance names
28 * into interface blocks without an instance name.
29 *
30 * For example, the following shader:
31 *
32 * out block {
33 * float block_var;
34 * } inst_name;
35 *
36 * main()
37 * {
38 * inst_name.block_var = 0.0;
39 * }
40 *
41 * Is rewritten to:
42 *
43 * out block {
44 * float block_var;
45 * };
46 *
47 * main()
48 * {
49 * block_var = 0.0;
50 * }
51 *
52 * This takes place after the shader code has already been verified with
53 * the interface name in place.
54 *
55 * The linking phase will use the interface block name rather than the
56 * interface's instance name when linking interfaces.
57 *
58 * This modification to the ir allows our currently existing dead code
59 * elimination to work with interface blocks without changes.
60 */
61
62 #include "glsl_symbol_table.h"
63 #include "ir.h"
64 #include "ir_optimization.h"
65 #include "ir_rvalue_visitor.h"
66 #include "util/hash_table.h"
67
68 static const glsl_type *
process_array_type(const glsl_type * type,unsigned idx)69 process_array_type(const glsl_type *type, unsigned idx)
70 {
71 const glsl_type *element_type = type->fields.array;
72 if (element_type->is_array()) {
73 const glsl_type *new_array_type = process_array_type(element_type, idx);
74 return glsl_type::get_array_instance(new_array_type, type->length);
75 } else {
76 return glsl_type::get_array_instance(
77 element_type->fields.structure[idx].type, type->length);
78 }
79 }
80
81 static ir_rvalue *
process_array_ir(void * const mem_ctx,ir_dereference_array * deref_array_prev,ir_rvalue * deref_var)82 process_array_ir(void * const mem_ctx,
83 ir_dereference_array *deref_array_prev,
84 ir_rvalue *deref_var)
85 {
86 ir_dereference_array *deref_array =
87 deref_array_prev->array->as_dereference_array();
88
89 if (deref_array == NULL) {
90 return new(mem_ctx) ir_dereference_array(deref_var,
91 deref_array_prev->array_index);
92 } else {
93 deref_array = (ir_dereference_array *) process_array_ir(mem_ctx,
94 deref_array,
95 deref_var);
96 return new(mem_ctx) ir_dereference_array(deref_array,
97 deref_array_prev->array_index);
98 }
99 }
100
101 namespace {
102
103 class flatten_named_interface_blocks_declarations : public ir_rvalue_visitor
104 {
105 public:
106 void * const mem_ctx;
107 hash_table *interface_namespace;
108
flatten_named_interface_blocks_declarations(void * mem_ctx)109 flatten_named_interface_blocks_declarations(void *mem_ctx)
110 : mem_ctx(mem_ctx),
111 interface_namespace(NULL)
112 {
113 }
114
115 void run(exec_list *instructions);
116
117 virtual ir_visitor_status visit_leave(ir_assignment *);
118 virtual ir_visitor_status visit_leave(ir_expression *);
119 virtual void handle_rvalue(ir_rvalue **rvalue);
120 };
121
122 } /* anonymous namespace */
123
124 void
run(exec_list * instructions)125 flatten_named_interface_blocks_declarations::run(exec_list *instructions)
126 {
127 interface_namespace = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
128 _mesa_key_string_equal);
129
130 /* First pass: adjust instance block variables with an instance name
131 * to not have an instance name.
132 *
133 * The interface block variables are stored in the interface_namespace
134 * hash table so they can be used in the second pass.
135 */
136 foreach_in_list_safe(ir_instruction, node, instructions) {
137 ir_variable *var = node->as_variable();
138 if (!var || !var->is_interface_instance())
139 continue;
140
141 /* It should be possible to handle uniforms during this pass,
142 * but, this will require changes to the other uniform block
143 * support code.
144 */
145 if (var->data.mode == ir_var_uniform ||
146 var->data.mode == ir_var_shader_storage)
147 continue;
148
149 const glsl_type * iface_t = var->type->without_array();
150 exec_node *insert_pos = var;
151
152 assert (iface_t->is_interface());
153
154 for (unsigned i = 0; i < iface_t->length; i++) {
155 const char * field_name = iface_t->fields.structure[i].name;
156 char *iface_field_name =
157 ralloc_asprintf(mem_ctx, "%s %s.%s.%s",
158 var->data.mode == ir_var_shader_in ? "in" : "out",
159 iface_t->name, var->name, field_name);
160
161 hash_entry *entry = _mesa_hash_table_search(interface_namespace,
162 iface_field_name);
163 ir_variable *found_var = entry ? (ir_variable *) entry->data : NULL;
164 if (!found_var) {
165 ir_variable *new_var;
166 char *var_name =
167 ralloc_strdup(mem_ctx, iface_t->fields.structure[i].name);
168 if (!var->type->is_array()) {
169 new_var =
170 new(mem_ctx) ir_variable(iface_t->fields.structure[i].type,
171 var_name,
172 (ir_variable_mode) var->data.mode);
173 } else {
174 const glsl_type *new_array_type =
175 process_array_type(var->type, i);
176 new_var =
177 new(mem_ctx) ir_variable(new_array_type,
178 var_name,
179 (ir_variable_mode) var->data.mode);
180 }
181 new_var->data.location = iface_t->fields.structure[i].location;
182 new_var->data.explicit_location = (new_var->data.location >= 0);
183 new_var->data.offset = iface_t->fields.structure[i].offset;
184 new_var->data.explicit_xfb_offset =
185 (iface_t->fields.structure[i].offset >= 0);
186 new_var->data.xfb_buffer =
187 iface_t->fields.structure[i].xfb_buffer;
188 new_var->data.explicit_xfb_buffer =
189 iface_t->fields.structure[i].explicit_xfb_buffer;
190 new_var->data.interpolation =
191 iface_t->fields.structure[i].interpolation;
192 new_var->data.centroid = iface_t->fields.structure[i].centroid;
193 new_var->data.sample = iface_t->fields.structure[i].sample;
194 new_var->data.patch = iface_t->fields.structure[i].patch;
195 new_var->data.stream = var->data.stream;
196 new_var->data.how_declared = var->data.how_declared;
197 new_var->data.from_named_ifc_block = 1;
198
199 new_var->init_interface_type(var->type);
200 _mesa_hash_table_insert(interface_namespace, iface_field_name,
201 new_var);
202 insert_pos->insert_after(new_var);
203 insert_pos = new_var;
204 }
205 }
206 var->remove();
207 }
208
209 /* Second pass: visit all ir_dereference_record instances, and if they
210 * reference an interface block, then flatten the refererence out.
211 */
212 visit_list_elements(this, instructions);
213 _mesa_hash_table_destroy(interface_namespace, NULL);
214 interface_namespace = NULL;
215 }
216
217 ir_visitor_status
visit_leave(ir_assignment * ir)218 flatten_named_interface_blocks_declarations::visit_leave(ir_assignment *ir)
219 {
220 ir_dereference_record *lhs_rec = ir->lhs->as_dereference_record();
221
222 ir_variable *lhs_var = ir->lhs->variable_referenced();
223 if (lhs_var && lhs_var->get_interface_type()) {
224 lhs_var->data.assigned = 1;
225 }
226
227 if (lhs_rec) {
228 ir_rvalue *lhs_rec_tmp = lhs_rec;
229 handle_rvalue(&lhs_rec_tmp);
230 if (lhs_rec_tmp != lhs_rec) {
231 ir->set_lhs(lhs_rec_tmp);
232 }
233
234 ir_variable *lhs_var = lhs_rec_tmp->variable_referenced();
235 if (lhs_var) {
236 lhs_var->data.assigned = 1;
237 }
238 }
239 return rvalue_visit(ir);
240 }
241
242 ir_visitor_status
visit_leave(ir_expression * ir)243 flatten_named_interface_blocks_declarations::visit_leave(ir_expression *ir)
244 {
245 ir_visitor_status status = rvalue_visit(ir);
246
247 if (ir->operation == ir_unop_interpolate_at_centroid ||
248 ir->operation == ir_binop_interpolate_at_offset ||
249 ir->operation == ir_binop_interpolate_at_sample) {
250 const ir_rvalue *val = ir->operands[0];
251
252 /* This disables varying packing for this input. */
253 val->variable_referenced()->data.must_be_shader_input = 1;
254 }
255
256 return status;
257 }
258
259 void
handle_rvalue(ir_rvalue ** rvalue)260 flatten_named_interface_blocks_declarations::handle_rvalue(ir_rvalue **rvalue)
261 {
262 if (*rvalue == NULL)
263 return;
264
265 ir_dereference_record *ir = (*rvalue)->as_dereference_record();
266 if (ir == NULL)
267 return;
268
269 ir_variable *var = ir->variable_referenced();
270 if (var == NULL)
271 return;
272
273 if (!var->is_interface_instance())
274 return;
275
276 /* It should be possible to handle uniforms during this pass,
277 * but, this will require changes to the other uniform block
278 * support code.
279 */
280 if (var->data.mode == ir_var_uniform || var->data.mode == ir_var_shader_storage)
281 return;
282
283 if (var->get_interface_type() != NULL) {
284 char *iface_field_name =
285 ralloc_asprintf(mem_ctx, "%s %s.%s.%s",
286 var->data.mode == ir_var_shader_in ? "in" : "out",
287 var->get_interface_type()->name,
288 var->name,
289 ir->record->type->fields.structure[ir->field_idx].name);
290
291 /* Find the variable in the set of flattened interface blocks */
292 hash_entry *entry = _mesa_hash_table_search(interface_namespace,
293 iface_field_name);
294 assert(entry);
295 ir_variable *found_var = (ir_variable *) entry->data;
296
297 ir_dereference_variable *deref_var =
298 new(mem_ctx) ir_dereference_variable(found_var);
299
300 ir_dereference_array *deref_array =
301 ir->record->as_dereference_array();
302 if (deref_array != NULL) {
303 *rvalue = process_array_ir(mem_ctx, deref_array,
304 (ir_rvalue *)deref_var);
305 } else {
306 *rvalue = deref_var;
307 }
308 }
309 }
310
311 void
lower_named_interface_blocks(void * mem_ctx,gl_linked_shader * shader)312 lower_named_interface_blocks(void *mem_ctx, gl_linked_shader *shader)
313 {
314 flatten_named_interface_blocks_declarations v_decl(mem_ctx);
315 v_decl.run(shader->ir);
316 }
317
318