glsl: Assign locations for uniforms in UBOs using the std140 rules.
[profile/ivi/mesa.git] / src / glsl / ir_clone.cpp
1 /*
2  * Copyright © 2010 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 <string.h>
25 #include "main/compiler.h"
26 #include "ir.h"
27 #include "glsl_types.h"
28 #include "program/hash_table.h"
29
30 ir_rvalue *
31 ir_rvalue::clone(void *mem_ctx, struct hash_table *ht) const
32 {
33    /* The only possible instantiation is the generic error value. */
34    return error_value(mem_ctx);
35 }
36
37 /**
38  * Duplicate an IR variable
39  *
40  * \note
41  * This will probably be made \c virtual and moved to the base class
42  * eventually.
43  */
44 ir_variable *
45 ir_variable::clone(void *mem_ctx, struct hash_table *ht) const
46 {
47    ir_variable *var = new(mem_ctx) ir_variable(this->type, this->name,
48                                                (ir_variable_mode) this->mode);
49
50    var->max_array_access = this->max_array_access;
51    var->read_only = this->read_only;
52    var->centroid = this->centroid;
53    var->invariant = this->invariant;
54    var->interpolation = this->interpolation;
55    var->location = this->location;
56    var->index = this->index;
57    var->uniform_block = this->uniform_block;
58    var->warn_extension = this->warn_extension;
59    var->origin_upper_left = this->origin_upper_left;
60    var->pixel_center_integer = this->pixel_center_integer;
61    var->explicit_location = this->explicit_location;
62    var->explicit_index = this->explicit_index;
63    var->has_initializer = this->has_initializer;
64    var->depth_layout = this->depth_layout;
65
66    var->num_state_slots = this->num_state_slots;
67    if (this->state_slots) {
68       /* FINISHME: This really wants to use something like talloc_reference, but
69        * FINISHME: ralloc doesn't have any similar function.
70        */
71       var->state_slots = ralloc_array(var, ir_state_slot,
72                                       this->num_state_slots);
73       memcpy(var->state_slots, this->state_slots,
74              sizeof(this->state_slots[0]) * var->num_state_slots);
75    }
76
77    if (this->constant_value)
78       var->constant_value = this->constant_value->clone(mem_ctx, ht);
79
80    if (this->constant_initializer)
81       var->constant_initializer =
82          this->constant_initializer->clone(mem_ctx, ht);
83
84    if (ht) {
85       hash_table_insert(ht, var, (void *)const_cast<ir_variable *>(this));
86    }
87
88    return var;
89 }
90
91 ir_swizzle *
92 ir_swizzle::clone(void *mem_ctx, struct hash_table *ht) const
93 {
94    return new(mem_ctx) ir_swizzle(this->val->clone(mem_ctx, ht), this->mask);
95 }
96
97 ir_return *
98 ir_return::clone(void *mem_ctx, struct hash_table *ht) const
99 {
100    ir_rvalue *new_value = NULL;
101
102    if (this->value)
103       new_value = this->value->clone(mem_ctx, ht);
104
105    return new(mem_ctx) ir_return(new_value);
106 }
107
108 ir_discard *
109 ir_discard::clone(void *mem_ctx, struct hash_table *ht) const
110 {
111    ir_rvalue *new_condition = NULL;
112
113    if (this->condition != NULL)
114       new_condition = this->condition->clone(mem_ctx, ht);
115
116    return new(mem_ctx) ir_discard(new_condition);
117 }
118
119 ir_loop_jump *
120 ir_loop_jump::clone(void *mem_ctx, struct hash_table *ht) const
121 {
122    (void)ht;
123
124    return new(mem_ctx) ir_loop_jump(this->mode);
125 }
126
127 ir_if *
128 ir_if::clone(void *mem_ctx, struct hash_table *ht) const
129 {
130    ir_if *new_if = new(mem_ctx) ir_if(this->condition->clone(mem_ctx, ht));
131
132    foreach_iter(exec_list_iterator, iter, this->then_instructions) {
133       ir_instruction *ir = (ir_instruction *)iter.get();
134       new_if->then_instructions.push_tail(ir->clone(mem_ctx, ht));
135    }
136
137    foreach_iter(exec_list_iterator, iter, this->else_instructions) {
138       ir_instruction *ir = (ir_instruction *)iter.get();
139       new_if->else_instructions.push_tail(ir->clone(mem_ctx, ht));
140    }
141
142    return new_if;
143 }
144
145 ir_loop *
146 ir_loop::clone(void *mem_ctx, struct hash_table *ht) const
147 {
148    ir_loop *new_loop = new(mem_ctx) ir_loop();
149
150    if (this->from)
151       new_loop->from = this->from->clone(mem_ctx, ht);
152    if (this->to)
153       new_loop->to = this->to->clone(mem_ctx, ht);
154    if (this->increment)
155       new_loop->increment = this->increment->clone(mem_ctx, ht);
156    new_loop->counter = counter;
157
158    foreach_iter(exec_list_iterator, iter, this->body_instructions) {
159       ir_instruction *ir = (ir_instruction *)iter.get();
160       new_loop->body_instructions.push_tail(ir->clone(mem_ctx, ht));
161    }
162
163    new_loop->cmp = this->cmp;
164    return new_loop;
165 }
166
167 ir_call *
168 ir_call::clone(void *mem_ctx, struct hash_table *ht) const
169 {
170    ir_dereference_variable *new_return_ref = NULL;
171    if (this->return_deref != NULL)
172       new_return_ref = this->return_deref->clone(mem_ctx, ht);
173
174    exec_list new_parameters;
175
176    foreach_iter(exec_list_iterator, iter, this->actual_parameters) {
177       ir_instruction *ir = (ir_instruction *)iter.get();
178       new_parameters.push_tail(ir->clone(mem_ctx, ht));
179    }
180
181    return new(mem_ctx) ir_call(this->callee, new_return_ref, &new_parameters);
182 }
183
184 ir_expression *
185 ir_expression::clone(void *mem_ctx, struct hash_table *ht) const
186 {
187    ir_rvalue *op[Elements(this->operands)] = { NULL, };
188    unsigned int i;
189
190    for (i = 0; i < get_num_operands(); i++) {
191       op[i] = this->operands[i]->clone(mem_ctx, ht);
192    }
193
194    return new(mem_ctx) ir_expression(this->operation, this->type,
195                                      op[0], op[1], op[2], op[3]);
196 }
197
198 ir_dereference_variable *
199 ir_dereference_variable::clone(void *mem_ctx, struct hash_table *ht) const
200 {
201    ir_variable *new_var;
202
203    if (ht) {
204       new_var = (ir_variable *)hash_table_find(ht, this->var);
205       if (!new_var)
206          new_var = this->var;
207    } else {
208       new_var = this->var;
209    }
210
211    return new(mem_ctx) ir_dereference_variable(new_var);
212 }
213
214 ir_dereference_array *
215 ir_dereference_array::clone(void *mem_ctx, struct hash_table *ht) const
216 {
217    return new(mem_ctx) ir_dereference_array(this->array->clone(mem_ctx, ht),
218                                             this->array_index->clone(mem_ctx,
219                                                                      ht));
220 }
221
222 ir_dereference_record *
223 ir_dereference_record::clone(void *mem_ctx, struct hash_table *ht) const
224 {
225    return new(mem_ctx) ir_dereference_record(this->record->clone(mem_ctx, ht),
226                                              this->field);
227 }
228
229 ir_texture *
230 ir_texture::clone(void *mem_ctx, struct hash_table *ht) const
231 {
232    ir_texture *new_tex = new(mem_ctx) ir_texture(this->op);
233    new_tex->type = this->type;
234
235    new_tex->sampler = this->sampler->clone(mem_ctx, ht);
236    if (this->coordinate)
237       new_tex->coordinate = this->coordinate->clone(mem_ctx, ht);
238    if (this->projector)
239       new_tex->projector = this->projector->clone(mem_ctx, ht);
240    if (this->shadow_comparitor) {
241       new_tex->shadow_comparitor = this->shadow_comparitor->clone(mem_ctx, ht);
242    }
243
244    if (this->offset != NULL)
245       new_tex->offset = this->offset->clone(mem_ctx, ht);
246
247    switch (this->op) {
248    case ir_tex:
249       break;
250    case ir_txb:
251       new_tex->lod_info.bias = this->lod_info.bias->clone(mem_ctx, ht);
252       break;
253    case ir_txl:
254    case ir_txf:
255    case ir_txs:
256       new_tex->lod_info.lod = this->lod_info.lod->clone(mem_ctx, ht);
257       break;
258    case ir_txd:
259       new_tex->lod_info.grad.dPdx = this->lod_info.grad.dPdx->clone(mem_ctx, ht);
260       new_tex->lod_info.grad.dPdy = this->lod_info.grad.dPdy->clone(mem_ctx, ht);
261       break;
262    }
263
264    return new_tex;
265 }
266
267 ir_assignment *
268 ir_assignment::clone(void *mem_ctx, struct hash_table *ht) const
269 {
270    ir_rvalue *new_condition = NULL;
271
272    if (this->condition)
273       new_condition = this->condition->clone(mem_ctx, ht);
274
275    return new(mem_ctx) ir_assignment(this->lhs->clone(mem_ctx, ht),
276                                      this->rhs->clone(mem_ctx, ht),
277                                      new_condition,
278                                      this->write_mask);
279 }
280
281 ir_function *
282 ir_function::clone(void *mem_ctx, struct hash_table *ht) const
283 {
284    ir_function *copy = new(mem_ctx) ir_function(this->name);
285
286    foreach_list_const(node, &this->signatures) {
287       const ir_function_signature *const sig =
288          (const ir_function_signature *const) node;
289
290       ir_function_signature *sig_copy = sig->clone(mem_ctx, ht);
291       copy->add_signature(sig_copy);
292
293       if (ht != NULL)
294          hash_table_insert(ht, sig_copy,
295                            (void *)const_cast<ir_function_signature *>(sig));
296    }
297
298    return copy;
299 }
300
301 ir_function_signature *
302 ir_function_signature::clone(void *mem_ctx, struct hash_table *ht) const
303 {
304    ir_function_signature *copy = this->clone_prototype(mem_ctx, ht);
305
306    copy->is_defined = this->is_defined;
307
308    /* Clone the instruction list.
309     */
310    foreach_list_const(node, &this->body) {
311       const ir_instruction *const inst = (const ir_instruction *) node;
312
313       ir_instruction *const inst_copy = inst->clone(mem_ctx, ht);
314       copy->body.push_tail(inst_copy);
315    }
316
317    return copy;
318 }
319
320 ir_function_signature *
321 ir_function_signature::clone_prototype(void *mem_ctx, struct hash_table *ht) const
322 {
323    ir_function_signature *copy =
324       new(mem_ctx) ir_function_signature(this->return_type);
325
326    copy->is_defined = false;
327    copy->is_builtin = this->is_builtin;
328    copy->origin = this;
329
330    /* Clone the parameter list, but NOT the body.
331     */
332    foreach_list_const(node, &this->parameters) {
333       const ir_variable *const param = (const ir_variable *) node;
334
335       assert(const_cast<ir_variable *>(param)->as_variable() != NULL);
336
337       ir_variable *const param_copy = param->clone(mem_ctx, ht);
338       copy->parameters.push_tail(param_copy);
339    }
340
341    return copy;
342 }
343
344 ir_constant *
345 ir_constant::clone(void *mem_ctx, struct hash_table *ht) const
346 {
347    (void)ht;
348
349    switch (this->type->base_type) {
350    case GLSL_TYPE_UINT:
351    case GLSL_TYPE_INT:
352    case GLSL_TYPE_FLOAT:
353    case GLSL_TYPE_BOOL:
354       return new(mem_ctx) ir_constant(this->type, &this->value);
355
356    case GLSL_TYPE_STRUCT: {
357       ir_constant *c = new(mem_ctx) ir_constant;
358
359       c->type = this->type;
360       for (exec_node *node = this->components.head
361               ; !node->is_tail_sentinel()
362               ; node = node->next) {
363          ir_constant *const orig = (ir_constant *) node;
364
365          c->components.push_tail(orig->clone(mem_ctx, NULL));
366       }
367
368       return c;
369    }
370
371    case GLSL_TYPE_ARRAY: {
372       ir_constant *c = new(mem_ctx) ir_constant;
373
374       c->type = this->type;
375       c->array_elements = ralloc_array(c, ir_constant *, this->type->length);
376       for (unsigned i = 0; i < this->type->length; i++) {
377          c->array_elements[i] = this->array_elements[i]->clone(mem_ctx, NULL);
378       }
379       return c;
380    }
381
382    default:
383       assert(!"Should not get here.");
384       return NULL;
385    }
386 }
387
388
389 class fixup_ir_call_visitor : public ir_hierarchical_visitor {
390 public:
391    fixup_ir_call_visitor(struct hash_table *ht)
392    {
393       this->ht = ht;
394    }
395
396    virtual ir_visitor_status visit_enter(ir_call *ir)
397    {
398       /* Try to find the function signature referenced by the ir_call in the
399        * table.  If it is found, replace it with the value from the table.
400        */
401       ir_function_signature *sig =
402          (ir_function_signature *) hash_table_find(this->ht, ir->callee);
403       if (sig != NULL)
404          ir->callee = sig;
405
406       /* Since this may be used before function call parameters are flattened,
407        * the children also need to be processed.
408        */
409       return visit_continue;
410    }
411
412 private:
413    struct hash_table *ht;
414 };
415
416
417 static void
418 fixup_function_calls(struct hash_table *ht, exec_list *instructions)
419 {
420    fixup_ir_call_visitor v(ht);
421    v.run(instructions);
422 }
423
424
425 void
426 clone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in)
427 {
428    struct hash_table *ht =
429       hash_table_ctor(0, hash_table_pointer_hash, hash_table_pointer_compare);
430
431    foreach_list_const(node, in) {
432       const ir_instruction *const original = (ir_instruction *) node;
433       ir_instruction *copy = original->clone(mem_ctx, ht);
434
435       out->push_tail(copy);
436    }
437
438    /* Make a pass over the cloned tree to fix up ir_call nodes to point to the
439     * cloned ir_function_signature nodes.  This cannot be done automatically
440     * during cloning because the ir_call might be a forward reference (i.e.,
441     * the function signature that it references may not have been cloned yet).
442     */
443    fixup_function_calls(ht, out);
444
445    hash_table_dtor(ht);
446 }