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 #include <string.h>
24 #include "ir.h"
25 #include "util/half_float.h"
26 #include "util/bitscan.h"
27 #include "compiler/glsl_types.h"
28 #include "glsl_parser_extras.h"
29
30
ir_rvalue(enum ir_node_type t)31 ir_rvalue::ir_rvalue(enum ir_node_type t)
32 : ir_instruction(t)
33 {
34 this->type = &glsl_type_builtin_error;
35 }
36
is_zero() const37 bool ir_rvalue::is_zero() const
38 {
39 return false;
40 }
41
is_one() const42 bool ir_rvalue::is_one() const
43 {
44 return false;
45 }
46
is_negative_one() const47 bool ir_rvalue::is_negative_one() const
48 {
49 return false;
50 }
51
52 /**
53 * Modify the swizzle make to move one component to another
54 *
55 * \param m IR swizzle to be modified
56 * \param from Component in the RHS that is to be swizzled
57 * \param to Desired swizzle location of \c from
58 */
59 static void
update_rhs_swizzle(ir_swizzle_mask & m,unsigned from,unsigned to)60 update_rhs_swizzle(ir_swizzle_mask &m, unsigned from, unsigned to)
61 {
62 switch (to) {
63 case 0: m.x = from; break;
64 case 1: m.y = from; break;
65 case 2: m.z = from; break;
66 case 3: m.w = from; break;
67 default: assert(!"Should not get here.");
68 }
69 }
70
71 void
set_lhs(ir_rvalue * lhs)72 ir_assignment::set_lhs(ir_rvalue *lhs)
73 {
74 void *mem_ctx = this;
75 bool swizzled = false;
76
77 while (lhs != NULL) {
78 ir_swizzle *swiz = lhs->as_swizzle();
79
80 if (swiz == NULL)
81 break;
82
83 unsigned write_mask = 0;
84 ir_swizzle_mask rhs_swiz = { 0, 0, 0, 0, 0, 0 };
85
86 for (unsigned i = 0; i < swiz->mask.num_components; i++) {
87 unsigned c = 0;
88
89 switch (i) {
90 case 0: c = swiz->mask.x; break;
91 case 1: c = swiz->mask.y; break;
92 case 2: c = swiz->mask.z; break;
93 case 3: c = swiz->mask.w; break;
94 default: assert(!"Should not get here.");
95 }
96
97 write_mask |= (((this->write_mask >> i) & 1) << c);
98 update_rhs_swizzle(rhs_swiz, i, c);
99 rhs_swiz.num_components = swiz->val->type->vector_elements;
100 }
101
102 this->write_mask = write_mask;
103 lhs = swiz->val;
104
105 this->rhs = new(mem_ctx) ir_swizzle(this->rhs, rhs_swiz);
106 swizzled = true;
107 }
108
109 if (swizzled) {
110 /* Now, RHS channels line up with the LHS writemask. Collapse it
111 * to just the channels that will be written.
112 */
113 ir_swizzle_mask rhs_swiz = { 0, 0, 0, 0, 0, 0 };
114 int rhs_chan = 0;
115 for (int i = 0; i < 4; i++) {
116 if (write_mask & (1 << i))
117 update_rhs_swizzle(rhs_swiz, i, rhs_chan++);
118 }
119 rhs_swiz.num_components = rhs_chan;
120 this->rhs = new(mem_ctx) ir_swizzle(this->rhs, rhs_swiz);
121 }
122
123 assert((lhs == NULL) || lhs->as_dereference());
124
125 this->lhs = (ir_dereference *) lhs;
126 }
127
128 ir_variable *
whole_variable_written()129 ir_assignment::whole_variable_written()
130 {
131 ir_variable *v = this->lhs->whole_variable_referenced();
132
133 if (v == NULL)
134 return NULL;
135
136 if (glsl_type_is_scalar(v->type))
137 return v;
138
139 if (glsl_type_is_vector(v->type)) {
140 const unsigned mask = (1U << v->type->vector_elements) - 1;
141
142 if (mask != this->write_mask)
143 return NULL;
144 }
145
146 /* Either all the vector components are assigned or the variable is some
147 * composite type (and the whole thing is assigned.
148 */
149 return v;
150 }
151
ir_assignment(ir_dereference * lhs,ir_rvalue * rhs,unsigned write_mask)152 ir_assignment::ir_assignment(ir_dereference *lhs, ir_rvalue *rhs,
153 unsigned write_mask)
154 : ir_instruction(ir_type_assignment)
155 {
156 this->rhs = rhs;
157 this->lhs = lhs;
158 this->write_mask = write_mask;
159
160 if (glsl_type_is_scalar(lhs->type) || glsl_type_is_vector(lhs->type))
161 assert(util_bitcount(write_mask) == this->rhs->type->vector_elements);
162 }
163
ir_assignment(ir_rvalue * lhs,ir_rvalue * rhs)164 ir_assignment::ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs)
165 : ir_instruction(ir_type_assignment)
166 {
167 this->rhs = rhs;
168
169 /* If the RHS is a vector type, assume that all components of the vector
170 * type are being written to the LHS. The write mask comes from the RHS
171 * because we can have a case where the LHS is a vec4 and the RHS is a
172 * vec3. In that case, the assignment is:
173 *
174 * (assign (...) (xyz) (var_ref lhs) (var_ref rhs))
175 */
176 if (glsl_type_is_vector(rhs->type))
177 this->write_mask = (1U << rhs->type->vector_elements) - 1;
178 else if (glsl_type_is_scalar(rhs->type))
179 this->write_mask = 1;
180 else
181 this->write_mask = 0;
182
183 this->set_lhs(lhs);
184 }
185
ir_expression(int op,const struct glsl_type * type,ir_rvalue * op0,ir_rvalue * op1,ir_rvalue * op2,ir_rvalue * op3)186 ir_expression::ir_expression(int op, const struct glsl_type *type,
187 ir_rvalue *op0, ir_rvalue *op1,
188 ir_rvalue *op2, ir_rvalue *op3)
189 : ir_rvalue(ir_type_expression)
190 {
191 this->type = type;
192 this->operation = ir_expression_operation(op);
193 this->operands[0] = op0;
194 this->operands[1] = op1;
195 this->operands[2] = op2;
196 this->operands[3] = op3;
197 init_num_operands();
198
199 #ifndef NDEBUG
200 for (unsigned i = num_operands; i < 4; i++) {
201 assert(this->operands[i] == NULL);
202 }
203
204 for (unsigned i = 0; i < num_operands; i++) {
205 assert(this->operands[i] != NULL);
206 }
207 #endif
208 }
209
ir_expression(int op,ir_rvalue * op0)210 ir_expression::ir_expression(int op, ir_rvalue *op0)
211 : ir_rvalue(ir_type_expression)
212 {
213 this->operation = ir_expression_operation(op);
214 this->operands[0] = op0;
215 this->operands[1] = NULL;
216 this->operands[2] = NULL;
217 this->operands[3] = NULL;
218
219 assert(op <= ir_last_unop);
220 init_num_operands();
221 assert(num_operands == 1);
222 assert(this->operands[0]);
223
224 switch (this->operation) {
225 case ir_unop_bit_not:
226 case ir_unop_logic_not:
227 case ir_unop_neg:
228 case ir_unop_abs:
229 case ir_unop_sign:
230 case ir_unop_rcp:
231 case ir_unop_rsq:
232 case ir_unop_sqrt:
233 case ir_unop_exp:
234 case ir_unop_log:
235 case ir_unop_exp2:
236 case ir_unop_log2:
237 case ir_unop_trunc:
238 case ir_unop_ceil:
239 case ir_unop_floor:
240 case ir_unop_fract:
241 case ir_unop_round_even:
242 case ir_unop_sin:
243 case ir_unop_cos:
244 case ir_unop_dFdx:
245 case ir_unop_dFdx_coarse:
246 case ir_unop_dFdx_fine:
247 case ir_unop_dFdy:
248 case ir_unop_dFdy_coarse:
249 case ir_unop_dFdy_fine:
250 case ir_unop_bitfield_reverse:
251 case ir_unop_interpolate_at_centroid:
252 case ir_unop_clz:
253 case ir_unop_saturate:
254 case ir_unop_atan:
255 this->type = op0->type;
256 break;
257
258 case ir_unop_f162i:
259 case ir_unop_f2i:
260 case ir_unop_b2i:
261 case ir_unop_u2i:
262 case ir_unop_d2i:
263 case ir_unop_bitcast_f2i:
264 case ir_unop_bit_count:
265 case ir_unop_find_msb:
266 case ir_unop_find_lsb:
267 case ir_unop_subroutine_to_int:
268 case ir_unop_i642i:
269 case ir_unop_u642i:
270 this->type = glsl_simple_type(GLSL_TYPE_INT, op0->type->vector_elements, 1);
271 break;
272
273 case ir_unop_b2f:
274 case ir_unop_i2f:
275 case ir_unop_u2f:
276 case ir_unop_d2f:
277 case ir_unop_f162f:
278 case ir_unop_bitcast_i2f:
279 case ir_unop_bitcast_u2f:
280 case ir_unop_i642f:
281 case ir_unop_u642f:
282 this->type = glsl_simple_type(GLSL_TYPE_FLOAT, op0->type->vector_elements, 1);
283 break;
284
285 case ir_unop_f2f16:
286 case ir_unop_f2fmp:
287 case ir_unop_b2f16:
288 case ir_unop_i2f16:
289 case ir_unop_u2f16:
290 case ir_unop_d2f16:
291 case ir_unop_i642f16:
292 case ir_unop_u642f16:
293 this->type = glsl_simple_type(GLSL_TYPE_FLOAT16, op0->type->vector_elements, 1);
294 break;
295
296 case ir_unop_i2imp:
297 this->type = glsl_simple_type(GLSL_TYPE_INT16, op0->type->vector_elements, 1);
298 break;
299
300 case ir_unop_i2i:
301 if (op0->type->base_type == GLSL_TYPE_INT) {
302 this->type = glsl_simple_type(GLSL_TYPE_INT16, op0->type->vector_elements, 1);
303 } else {
304 assert(op0->type->base_type == GLSL_TYPE_INT16);
305 this->type = glsl_simple_type(GLSL_TYPE_INT, op0->type->vector_elements, 1);
306 }
307 break;
308
309 case ir_unop_u2u:
310 if (op0->type->base_type == GLSL_TYPE_UINT) {
311 this->type = glsl_simple_type(GLSL_TYPE_UINT16, op0->type->vector_elements, 1);
312 } else {
313 assert(op0->type->base_type == GLSL_TYPE_UINT16);
314 this->type = glsl_simple_type(GLSL_TYPE_UINT, op0->type->vector_elements, 1);
315 }
316 break;
317
318 case ir_unop_u2ump:
319 this->type = glsl_simple_type(GLSL_TYPE_UINT16, op0->type->vector_elements, 1);
320 break;
321
322 case ir_unop_f2b:
323 case ir_unop_i2b:
324 case ir_unop_d2b:
325 case ir_unop_f162b:
326 case ir_unop_i642b:
327 this->type = glsl_simple_type(GLSL_TYPE_BOOL, op0->type->vector_elements, 1);
328 break;
329
330 case ir_unop_f162d:
331 case ir_unop_f2d:
332 case ir_unop_i2d:
333 case ir_unop_u2d:
334 case ir_unop_i642d:
335 case ir_unop_u642d:
336 this->type = glsl_simple_type(GLSL_TYPE_DOUBLE, op0->type->vector_elements, 1);
337 break;
338
339 case ir_unop_i2u:
340 case ir_unop_f162u:
341 case ir_unop_f2u:
342 case ir_unop_d2u:
343 case ir_unop_bitcast_f2u:
344 case ir_unop_i642u:
345 case ir_unop_u642u:
346 this->type = glsl_simple_type(GLSL_TYPE_UINT, op0->type->vector_elements, 1);
347 break;
348
349 case ir_unop_i2i64:
350 case ir_unop_u2i64:
351 case ir_unop_b2i64:
352 case ir_unop_f162i64:
353 case ir_unop_f2i64:
354 case ir_unop_d2i64:
355 case ir_unop_u642i64:
356 this->type = glsl_simple_type(GLSL_TYPE_INT64, op0->type->vector_elements, 1);
357 break;
358
359 case ir_unop_i2u64:
360 case ir_unop_u2u64:
361 case ir_unop_f162u64:
362 case ir_unop_f2u64:
363 case ir_unop_d2u64:
364 case ir_unop_i642u64:
365 this->type = glsl_simple_type(GLSL_TYPE_UINT64, op0->type->vector_elements, 1);
366 break;
367
368 case ir_unop_unpack_double_2x32:
369 case ir_unop_unpack_uint_2x32:
370 this->type = &glsl_type_builtin_uvec2;
371 break;
372
373 case ir_unop_unpack_int_2x32:
374 this->type = &glsl_type_builtin_ivec2;
375 break;
376
377 case ir_unop_pack_snorm_2x16:
378 case ir_unop_pack_snorm_4x8:
379 case ir_unop_pack_unorm_2x16:
380 case ir_unop_pack_unorm_4x8:
381 case ir_unop_pack_half_2x16:
382 this->type = &glsl_type_builtin_uint;
383 break;
384
385 case ir_unop_pack_double_2x32:
386 this->type = &glsl_type_builtin_double;
387 break;
388
389 case ir_unop_pack_int_2x32:
390 this->type = &glsl_type_builtin_int64_t;
391 break;
392
393 case ir_unop_pack_uint_2x32:
394 this->type = &glsl_type_builtin_uint64_t;
395 break;
396
397 case ir_unop_unpack_snorm_2x16:
398 case ir_unop_unpack_unorm_2x16:
399 case ir_unop_unpack_half_2x16:
400 this->type = &glsl_type_builtin_vec2;
401 break;
402
403 case ir_unop_unpack_snorm_4x8:
404 case ir_unop_unpack_unorm_4x8:
405 this->type = &glsl_type_builtin_vec4;
406 break;
407
408 case ir_unop_unpack_sampler_2x32:
409 case ir_unop_unpack_image_2x32:
410 this->type = &glsl_type_builtin_uvec2;
411 break;
412
413 case ir_unop_pack_sampler_2x32:
414 case ir_unop_pack_image_2x32:
415 this->type = op0->type;
416 break;
417
418 case ir_unop_frexp_sig:
419 this->type = op0->type;
420 break;
421 case ir_unop_frexp_exp:
422 this->type = glsl_simple_type(GLSL_TYPE_INT, op0->type->vector_elements, 1);
423 break;
424
425 case ir_unop_get_buffer_size:
426 case ir_unop_ssbo_unsized_array_length:
427 case ir_unop_implicitly_sized_array_length:
428 this->type = &glsl_type_builtin_int;
429 break;
430
431 case ir_unop_bitcast_i642d:
432 case ir_unop_bitcast_u642d:
433 this->type = glsl_simple_type(GLSL_TYPE_DOUBLE, op0->type->vector_elements, 1);
434 break;
435
436 case ir_unop_bitcast_d2i64:
437 this->type = glsl_simple_type(GLSL_TYPE_INT64, op0->type->vector_elements, 1);
438 break;
439 case ir_unop_bitcast_d2u64:
440 this->type = glsl_simple_type(GLSL_TYPE_UINT64, op0->type->vector_elements, 1);
441 break;
442
443 default:
444 assert(!"not reached: missing automatic type setup for ir_expression");
445 this->type = op0->type;
446 break;
447 }
448 }
449
ir_expression(int op,ir_rvalue * op0,ir_rvalue * op1)450 ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1)
451 : ir_rvalue(ir_type_expression)
452 {
453 this->operation = ir_expression_operation(op);
454 this->operands[0] = op0;
455 this->operands[1] = op1;
456 this->operands[2] = NULL;
457 this->operands[3] = NULL;
458
459 assert(op > ir_last_unop);
460 init_num_operands();
461 assert(num_operands == 2);
462 for (unsigned i = 0; i < num_operands; i++) {
463 assert(this->operands[i] != NULL);
464 }
465
466 switch (this->operation) {
467 case ir_binop_all_equal:
468 case ir_binop_any_nequal:
469 this->type = &glsl_type_builtin_bool;
470 break;
471
472 case ir_binop_add:
473 case ir_binop_sub:
474 case ir_binop_min:
475 case ir_binop_max:
476 case ir_binop_pow:
477 case ir_binop_mul:
478 case ir_binop_div:
479 case ir_binop_mod:
480 case ir_binop_atan2:
481 if (glsl_type_is_scalar(op0->type)) {
482 this->type = op1->type;
483 } else if (glsl_type_is_scalar(op1->type)) {
484 this->type = op0->type;
485 } else {
486 if (this->operation == ir_binop_mul) {
487 this->type = glsl_get_mul_type(op0->type, op1->type);
488 } else {
489 assert(op0->type == op1->type);
490 this->type = op0->type;
491 }
492 }
493 break;
494
495 case ir_binop_logic_and:
496 case ir_binop_logic_xor:
497 case ir_binop_logic_or:
498 case ir_binop_bit_and:
499 case ir_binop_bit_xor:
500 case ir_binop_bit_or:
501 assert(!glsl_type_is_matrix(op0->type));
502 assert(!glsl_type_is_matrix(op1->type));
503 if (glsl_type_is_scalar(op0->type)) {
504 this->type = op1->type;
505 } else if (glsl_type_is_scalar(op1->type)) {
506 this->type = op0->type;
507 } else {
508 assert(op0->type->vector_elements == op1->type->vector_elements);
509 this->type = op0->type;
510 }
511 break;
512
513 case ir_binop_equal:
514 case ir_binop_nequal:
515 case ir_binop_gequal:
516 case ir_binop_less:
517 assert(op0->type == op1->type);
518 this->type = glsl_simple_type(GLSL_TYPE_BOOL, op0->type->vector_elements, 1);
519 break;
520
521 case ir_binop_dot:
522 this->type = glsl_get_base_glsl_type(op0->type);
523 break;
524
525 case ir_binop_imul_high:
526 case ir_binop_mul_32x16:
527 case ir_binop_carry:
528 case ir_binop_borrow:
529 case ir_binop_lshift:
530 case ir_binop_rshift:
531 case ir_binop_ldexp:
532 case ir_binop_interpolate_at_offset:
533 case ir_binop_interpolate_at_sample:
534 this->type = op0->type;
535 break;
536
537 case ir_binop_add_sat:
538 case ir_binop_sub_sat:
539 case ir_binop_avg:
540 case ir_binop_avg_round:
541 assert(op0->type == op1->type);
542 this->type = op0->type;
543 break;
544
545 case ir_binop_abs_sub: {
546 enum glsl_base_type base;
547
548 assert(op0->type == op1->type);
549
550 switch (op0->type->base_type) {
551 case GLSL_TYPE_UINT:
552 case GLSL_TYPE_INT:
553 base = GLSL_TYPE_UINT;
554 break;
555 case GLSL_TYPE_UINT8:
556 case GLSL_TYPE_INT8:
557 base = GLSL_TYPE_UINT8;
558 break;
559 case GLSL_TYPE_UINT16:
560 case GLSL_TYPE_INT16:
561 base = GLSL_TYPE_UINT16;
562 break;
563 case GLSL_TYPE_UINT64:
564 case GLSL_TYPE_INT64:
565 base = GLSL_TYPE_UINT64;
566 break;
567 default:
568 unreachable("Invalid base type.");
569 }
570
571 this->type = glsl_simple_type(base, op0->type->vector_elements, 1);
572 break;
573 }
574
575 case ir_binop_vector_extract:
576 this->type = glsl_get_scalar_type(op0->type);
577 break;
578
579 default:
580 assert(!"not reached: missing automatic type setup for ir_expression");
581 this->type = &glsl_type_builtin_float;
582 }
583 }
584
ir_expression(int op,ir_rvalue * op0,ir_rvalue * op1,ir_rvalue * op2)585 ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1,
586 ir_rvalue *op2)
587 : ir_rvalue(ir_type_expression)
588 {
589 this->operation = ir_expression_operation(op);
590 this->operands[0] = op0;
591 this->operands[1] = op1;
592 this->operands[2] = op2;
593 this->operands[3] = NULL;
594
595 assert(op > ir_last_binop && op <= ir_last_triop);
596 init_num_operands();
597 assert(num_operands == 3);
598 for (unsigned i = 0; i < num_operands; i++) {
599 assert(this->operands[i] != NULL);
600 }
601
602 switch (this->operation) {
603 case ir_triop_fma:
604 case ir_triop_lrp:
605 case ir_triop_bitfield_extract:
606 case ir_triop_vector_insert:
607 this->type = op0->type;
608 break;
609
610 case ir_triop_csel:
611 this->type = op1->type;
612 break;
613
614 default:
615 assert(!"not reached: missing automatic type setup for ir_expression");
616 this->type = &glsl_type_builtin_float;
617 }
618 }
619
620 /**
621 * This is only here for ir_reader to used for testing purposes. Please use
622 * the precomputed num_operands field if you need the number of operands.
623 */
624 unsigned
get_num_operands(ir_expression_operation op)625 ir_expression::get_num_operands(ir_expression_operation op)
626 {
627 assert(op <= ir_last_opcode);
628
629 if (op <= ir_last_unop)
630 return 1;
631
632 if (op <= ir_last_binop)
633 return 2;
634
635 if (op <= ir_last_triop)
636 return 3;
637
638 if (op <= ir_last_quadop)
639 return 4;
640
641 unreachable("Could not calculate number of operands");
642 }
643
644 #include "ir_expression_operation_strings.h"
645
646 const char*
depth_layout_string(ir_depth_layout layout)647 depth_layout_string(ir_depth_layout layout)
648 {
649 switch(layout) {
650 case ir_depth_layout_none: return "";
651 case ir_depth_layout_any: return "depth_any";
652 case ir_depth_layout_greater: return "depth_greater";
653 case ir_depth_layout_less: return "depth_less";
654 case ir_depth_layout_unchanged: return "depth_unchanged";
655
656 default:
657 assert(0);
658 return "";
659 }
660 }
661
662 ir_expression_operation
get_operator(const char * str)663 ir_expression::get_operator(const char *str)
664 {
665 for (int op = 0; op <= int(ir_last_opcode); op++) {
666 if (strcmp(str, ir_expression_operation_strings[op]) == 0)
667 return (ir_expression_operation) op;
668 }
669 return (ir_expression_operation) -1;
670 }
671
672 ir_variable *
variable_referenced() const673 ir_expression::variable_referenced() const
674 {
675 switch (operation) {
676 case ir_binop_vector_extract:
677 case ir_triop_vector_insert:
678 /* We get these for things like a[0] where a is a vector type. In these
679 * cases we want variable_referenced() to return the actual vector
680 * variable this is wrapping.
681 */
682 return operands[0]->variable_referenced();
683 default:
684 return ir_rvalue::variable_referenced();
685 }
686 }
687
ir_constant()688 ir_constant::ir_constant()
689 : ir_rvalue(ir_type_constant)
690 {
691 this->const_elements = NULL;
692 }
693
ir_constant(const struct glsl_type * type,const ir_constant_data * data)694 ir_constant::ir_constant(const struct glsl_type *type,
695 const ir_constant_data *data)
696 : ir_rvalue(ir_type_constant)
697 {
698 this->const_elements = NULL;
699
700 assert((type->base_type >= GLSL_TYPE_UINT)
701 && (type->base_type <= GLSL_TYPE_IMAGE));
702
703 this->type = type;
704 memcpy(& this->value, data, sizeof(this->value));
705 }
706
ir_constant(float16_t f16,unsigned vector_elements)707 ir_constant::ir_constant(float16_t f16, unsigned vector_elements)
708 : ir_rvalue(ir_type_constant)
709 {
710 this->const_elements = NULL;
711 assert(vector_elements <= 4);
712 this->type = glsl_simple_type(GLSL_TYPE_FLOAT16, vector_elements, 1);
713 for (unsigned i = 0; i < vector_elements; i++) {
714 this->value.f16[i] = f16.bits;
715 }
716 for (unsigned i = vector_elements; i < 16; i++) {
717 this->value.f[i] = 0;
718 }
719 }
720
ir_constant(float f,unsigned vector_elements)721 ir_constant::ir_constant(float f, unsigned vector_elements)
722 : ir_rvalue(ir_type_constant)
723 {
724 this->const_elements = NULL;
725 assert(vector_elements <= 4);
726 this->type = glsl_simple_type(GLSL_TYPE_FLOAT, vector_elements, 1);
727 for (unsigned i = 0; i < vector_elements; i++) {
728 this->value.f[i] = f;
729 }
730 for (unsigned i = vector_elements; i < 16; i++) {
731 this->value.f[i] = 0;
732 }
733 }
734
ir_constant(double d,unsigned vector_elements)735 ir_constant::ir_constant(double d, unsigned vector_elements)
736 : ir_rvalue(ir_type_constant)
737 {
738 this->const_elements = NULL;
739 assert(vector_elements <= 4);
740 this->type = glsl_simple_type(GLSL_TYPE_DOUBLE, vector_elements, 1);
741 for (unsigned i = 0; i < vector_elements; i++) {
742 this->value.d[i] = d;
743 }
744 for (unsigned i = vector_elements; i < 16; i++) {
745 this->value.d[i] = 0.0;
746 }
747 }
748
ir_constant(int16_t i16,unsigned vector_elements)749 ir_constant::ir_constant(int16_t i16, unsigned vector_elements)
750 : ir_rvalue(ir_type_constant)
751 {
752 this->const_elements = NULL;
753 assert(vector_elements <= 4);
754 this->type = glsl_simple_type(GLSL_TYPE_INT16, vector_elements, 1);
755 for (unsigned i = 0; i < vector_elements; i++) {
756 this->value.i16[i] = i16;
757 }
758 for (unsigned i = vector_elements; i < 16; i++) {
759 this->value.i16[i] = 0;
760 }
761 }
762
ir_constant(uint16_t u16,unsigned vector_elements)763 ir_constant::ir_constant(uint16_t u16, unsigned vector_elements)
764 : ir_rvalue(ir_type_constant)
765 {
766 this->const_elements = NULL;
767 assert(vector_elements <= 4);
768 this->type = glsl_simple_type(GLSL_TYPE_UINT16, vector_elements, 1);
769 for (unsigned i = 0; i < vector_elements; i++) {
770 this->value.u16[i] = u16;
771 }
772 for (unsigned i = vector_elements; i < 16; i++) {
773 this->value.u16[i] = 0;
774 }
775 }
776
ir_constant(unsigned int u,unsigned vector_elements)777 ir_constant::ir_constant(unsigned int u, unsigned vector_elements)
778 : ir_rvalue(ir_type_constant)
779 {
780 this->const_elements = NULL;
781 assert(vector_elements <= 4);
782 this->type = glsl_simple_type(GLSL_TYPE_UINT, vector_elements, 1);
783 for (unsigned i = 0; i < vector_elements; i++) {
784 this->value.u[i] = u;
785 }
786 for (unsigned i = vector_elements; i < 16; i++) {
787 this->value.u[i] = 0;
788 }
789 }
790
ir_constant(int integer,unsigned vector_elements)791 ir_constant::ir_constant(int integer, unsigned vector_elements)
792 : ir_rvalue(ir_type_constant)
793 {
794 this->const_elements = NULL;
795 assert(vector_elements <= 4);
796 this->type = glsl_simple_type(GLSL_TYPE_INT, vector_elements, 1);
797 for (unsigned i = 0; i < vector_elements; i++) {
798 this->value.i[i] = integer;
799 }
800 for (unsigned i = vector_elements; i < 16; i++) {
801 this->value.i[i] = 0;
802 }
803 }
804
ir_constant(uint64_t u64,unsigned vector_elements)805 ir_constant::ir_constant(uint64_t u64, unsigned vector_elements)
806 : ir_rvalue(ir_type_constant)
807 {
808 this->const_elements = NULL;
809 assert(vector_elements <= 4);
810 this->type = glsl_simple_type(GLSL_TYPE_UINT64, vector_elements, 1);
811 for (unsigned i = 0; i < vector_elements; i++) {
812 this->value.u64[i] = u64;
813 }
814 for (unsigned i = vector_elements; i < 16; i++) {
815 this->value.u64[i] = 0;
816 }
817 }
818
ir_constant(int64_t int64,unsigned vector_elements)819 ir_constant::ir_constant(int64_t int64, unsigned vector_elements)
820 : ir_rvalue(ir_type_constant)
821 {
822 this->const_elements = NULL;
823 assert(vector_elements <= 4);
824 this->type = glsl_simple_type(GLSL_TYPE_INT64, vector_elements, 1);
825 for (unsigned i = 0; i < vector_elements; i++) {
826 this->value.i64[i] = int64;
827 }
828 for (unsigned i = vector_elements; i < 16; i++) {
829 this->value.i64[i] = 0;
830 }
831 }
832
ir_constant(bool b,unsigned vector_elements)833 ir_constant::ir_constant(bool b, unsigned vector_elements)
834 : ir_rvalue(ir_type_constant)
835 {
836 this->const_elements = NULL;
837 assert(vector_elements <= 4);
838 this->type = glsl_simple_type(GLSL_TYPE_BOOL, vector_elements, 1);
839 for (unsigned i = 0; i < vector_elements; i++) {
840 this->value.b[i] = b;
841 }
842 for (unsigned i = vector_elements; i < 16; i++) {
843 this->value.b[i] = false;
844 }
845 }
846
ir_constant(const ir_constant * c,unsigned i)847 ir_constant::ir_constant(const ir_constant *c, unsigned i)
848 : ir_rvalue(ir_type_constant)
849 {
850 this->const_elements = NULL;
851 this->type = glsl_get_base_glsl_type(c->type);
852
853 /* Section 5.11 (Out-of-Bounds Accesses) of the GLSL 4.60 spec says:
854 *
855 * In the subsections described above for array, vector, matrix and
856 * structure accesses, any out-of-bounds access produced undefined
857 * behavior....Out-of-bounds reads return undefined values, which
858 * include values from other variables of the active program or zero.
859 *
860 * GL_KHR_robustness and GL_ARB_robustness encourage us to return zero.
861 */
862 if (i >= c->type->vector_elements) {
863 this->value = { { 0 } };
864 return;
865 }
866
867 switch (this->type->base_type) {
868 case GLSL_TYPE_UINT16: this->value.u16[0] = c->value.u16[i]; break;
869 case GLSL_TYPE_INT16: this->value.i16[0] = c->value.i16[i]; break;
870 case GLSL_TYPE_UINT: this->value.u[0] = c->value.u[i]; break;
871 case GLSL_TYPE_INT: this->value.i[0] = c->value.i[i]; break;
872 case GLSL_TYPE_FLOAT: this->value.f[0] = c->value.f[i]; break;
873 case GLSL_TYPE_FLOAT16: this->value.f16[0] = c->value.f16[i]; break;
874 case GLSL_TYPE_BOOL: this->value.b[0] = c->value.b[i]; break;
875 case GLSL_TYPE_DOUBLE: this->value.d[0] = c->value.d[i]; break;
876 default: assert(!"Should not get here."); break;
877 }
878 }
879
ir_constant(const struct glsl_type * type,exec_list * value_list)880 ir_constant::ir_constant(const struct glsl_type *type, exec_list *value_list)
881 : ir_rvalue(ir_type_constant)
882 {
883 this->const_elements = NULL;
884 this->type = type;
885
886 assert(glsl_type_is_scalar(type) || glsl_type_is_vector(type) || glsl_type_is_matrix(type)
887 || glsl_type_is_struct(type) || glsl_type_is_array(type));
888
889 /* If the constant is a record, the types of each of the entries in
890 * value_list must be a 1-for-1 match with the structure components. Each
891 * entry must also be a constant. Just move the nodes from the value_list
892 * to the list in the ir_constant.
893 */
894 if (glsl_type_is_array(type) || glsl_type_is_struct(type)) {
895 this->const_elements = ralloc_array(this, ir_constant *, type->length);
896 unsigned i = 0;
897 foreach_in_list(ir_constant, value, value_list) {
898 assert(value->as_constant() != NULL);
899
900 this->const_elements[i++] = value;
901 }
902 return;
903 }
904
905 for (unsigned i = 0; i < 16; i++) {
906 this->value.u[i] = 0;
907 }
908
909 ir_constant *value = (ir_constant *) (value_list->get_head_raw());
910
911 /* Constructors with exactly one scalar argument are special for vectors
912 * and matrices. For vectors, the scalar value is replicated to fill all
913 * the components. For matrices, the scalar fills the components of the
914 * diagonal while the rest is filled with 0.
915 */
916 if (glsl_type_is_scalar(value->type) && value->next->is_tail_sentinel()) {
917 if (glsl_type_is_matrix(type)) {
918 /* Matrix - fill diagonal (rest is already set to 0) */
919 for (unsigned i = 0; i < type->matrix_columns; i++) {
920 switch (type->base_type) {
921 case GLSL_TYPE_FLOAT:
922 this->value.f[i * type->vector_elements + i] =
923 value->value.f[0];
924 break;
925 case GLSL_TYPE_DOUBLE:
926 this->value.d[i * type->vector_elements + i] =
927 value->value.d[0];
928 break;
929 case GLSL_TYPE_FLOAT16:
930 this->value.f16[i * type->vector_elements + i] =
931 value->value.f16[0];
932 break;
933 default:
934 assert(!"unexpected matrix base type");
935 }
936 }
937 } else {
938 /* Vector or scalar - fill all components */
939 switch (type->base_type) {
940 case GLSL_TYPE_UINT16:
941 case GLSL_TYPE_INT16:
942 for (unsigned i = 0; i < glsl_get_components(type); i++)
943 this->value.u16[i] = value->value.u16[0];
944 break;
945 case GLSL_TYPE_UINT:
946 case GLSL_TYPE_INT:
947 for (unsigned i = 0; i < glsl_get_components(type); i++)
948 this->value.u[i] = value->value.u[0];
949 break;
950 case GLSL_TYPE_FLOAT:
951 for (unsigned i = 0; i < glsl_get_components(type); i++)
952 this->value.f[i] = value->value.f[0];
953 break;
954 case GLSL_TYPE_FLOAT16:
955 for (unsigned i = 0; i < glsl_get_components(type); i++)
956 this->value.f16[i] = value->value.f16[0];
957 break;
958 case GLSL_TYPE_DOUBLE:
959 for (unsigned i = 0; i < glsl_get_components(type); i++)
960 this->value.d[i] = value->value.d[0];
961 break;
962 case GLSL_TYPE_UINT64:
963 case GLSL_TYPE_INT64:
964 for (unsigned i = 0; i < glsl_get_components(type); i++)
965 this->value.u64[i] = value->value.u64[0];
966 break;
967 case GLSL_TYPE_BOOL:
968 for (unsigned i = 0; i < glsl_get_components(type); i++)
969 this->value.b[i] = value->value.b[0];
970 break;
971 case GLSL_TYPE_SAMPLER:
972 case GLSL_TYPE_IMAGE:
973 this->value.u64[0] = value->value.u64[0];
974 break;
975 default:
976 assert(!"Should not get here.");
977 break;
978 }
979 }
980 return;
981 }
982
983 if (glsl_type_is_matrix(type) && glsl_type_is_matrix(value->type)) {
984 assert(value->next->is_tail_sentinel());
985
986 /* From section 5.4.2 of the GLSL 1.20 spec:
987 * "If a matrix is constructed from a matrix, then each component
988 * (column i, row j) in the result that has a corresponding component
989 * (column i, row j) in the argument will be initialized from there."
990 */
991 unsigned cols = MIN2(type->matrix_columns, value->type->matrix_columns);
992 unsigned rows = MIN2(type->vector_elements, value->type->vector_elements);
993 for (unsigned i = 0; i < cols; i++) {
994 for (unsigned j = 0; j < rows; j++) {
995 const unsigned src = i * value->type->vector_elements + j;
996 const unsigned dst = i * type->vector_elements + j;
997 this->value.f[dst] = value->value.f[src];
998 }
999 }
1000
1001 /* "All other components will be initialized to the identity matrix." */
1002 for (unsigned i = cols; i < type->matrix_columns; i++)
1003 this->value.f[i * type->vector_elements + i] = 1.0;
1004
1005 return;
1006 }
1007
1008 /* Use each component from each entry in the value_list to initialize one
1009 * component of the constant being constructed.
1010 */
1011 unsigned i = 0;
1012 for (;;) {
1013 assert(value->as_constant() != NULL);
1014 assert(!value->is_tail_sentinel());
1015
1016 for (unsigned j = 0; j < glsl_get_components(value->type); j++) {
1017 switch (type->base_type) {
1018 case GLSL_TYPE_UINT16:
1019 this->value.u16[i] = value->get_uint16_component(j);
1020 break;
1021 case GLSL_TYPE_INT16:
1022 this->value.i16[i] = value->get_int16_component(j);
1023 break;
1024 case GLSL_TYPE_UINT:
1025 this->value.u[i] = value->get_uint_component(j);
1026 break;
1027 case GLSL_TYPE_INT:
1028 this->value.i[i] = value->get_int_component(j);
1029 break;
1030 case GLSL_TYPE_FLOAT:
1031 this->value.f[i] = value->get_float_component(j);
1032 break;
1033 case GLSL_TYPE_FLOAT16:
1034 this->value.f16[i] = value->get_float16_component(j);
1035 break;
1036 case GLSL_TYPE_BOOL:
1037 this->value.b[i] = value->get_bool_component(j);
1038 break;
1039 case GLSL_TYPE_DOUBLE:
1040 this->value.d[i] = value->get_double_component(j);
1041 break;
1042 case GLSL_TYPE_UINT64:
1043 this->value.u64[i] = value->get_uint64_component(j);
1044 break;
1045 case GLSL_TYPE_INT64:
1046 this->value.i64[i] = value->get_int64_component(j);
1047 break;
1048 default:
1049 /* FINISHME: What to do? Exceptions are not the answer.
1050 */
1051 break;
1052 }
1053
1054 i++;
1055 if (i >= glsl_get_components(type))
1056 break;
1057 }
1058
1059 if (i >= glsl_get_components(type))
1060 break; /* avoid downcasting a list sentinel */
1061 value = (ir_constant *) value->next;
1062 }
1063 }
1064
1065 ir_constant *
zero(void * mem_ctx,const glsl_type * type)1066 ir_constant::zero(void *mem_ctx, const glsl_type *type)
1067 {
1068 assert(glsl_type_is_scalar(type) || glsl_type_is_vector(type) || glsl_type_is_matrix(type)
1069 || glsl_type_is_struct(type) || glsl_type_is_array(type));
1070
1071 ir_constant *c = new(mem_ctx) ir_constant;
1072 c->type = type;
1073 memset(&c->value, 0, sizeof(c->value));
1074
1075 if (glsl_type_is_array(type)) {
1076 c->const_elements = ralloc_array(c, ir_constant *, type->length);
1077
1078 for (unsigned i = 0; i < type->length; i++)
1079 c->const_elements[i] = ir_constant::zero(c, type->fields.array);
1080 }
1081
1082 if (glsl_type_is_struct(type)) {
1083 c->const_elements = ralloc_array(c, ir_constant *, type->length);
1084
1085 for (unsigned i = 0; i < type->length; i++) {
1086 c->const_elements[i] =
1087 ir_constant::zero(mem_ctx, type->fields.structure[i].type);
1088 }
1089 }
1090
1091 return c;
1092 }
1093
1094 bool
get_bool_component(unsigned i) const1095 ir_constant::get_bool_component(unsigned i) const
1096 {
1097 switch (this->type->base_type) {
1098 case GLSL_TYPE_UINT16:return this->value.u16[i] != 0;
1099 case GLSL_TYPE_INT16: return this->value.i16[i] != 0;
1100 case GLSL_TYPE_UINT: return this->value.u[i] != 0;
1101 case GLSL_TYPE_INT: return this->value.i[i] != 0;
1102 case GLSL_TYPE_FLOAT: return ((int)this->value.f[i]) != 0;
1103 case GLSL_TYPE_FLOAT16: return ((int)_mesa_half_to_float(this->value.f16[i])) != 0;
1104 case GLSL_TYPE_BOOL: return this->value.b[i];
1105 case GLSL_TYPE_DOUBLE: return this->value.d[i] != 0.0;
1106 case GLSL_TYPE_SAMPLER:
1107 case GLSL_TYPE_IMAGE:
1108 case GLSL_TYPE_UINT64: return this->value.u64[i] != 0;
1109 case GLSL_TYPE_INT64: return this->value.i64[i] != 0;
1110 default: assert(!"Should not get here."); break;
1111 }
1112
1113 /* Must return something to make the compiler happy. This is clearly an
1114 * error case.
1115 */
1116 return false;
1117 }
1118
1119 float
get_float_component(unsigned i) const1120 ir_constant::get_float_component(unsigned i) const
1121 {
1122 switch (this->type->base_type) {
1123 case GLSL_TYPE_UINT16:return (float) this->value.u16[i];
1124 case GLSL_TYPE_INT16: return (float) this->value.i16[i];
1125 case GLSL_TYPE_UINT: return (float) this->value.u[i];
1126 case GLSL_TYPE_INT: return (float) this->value.i[i];
1127 case GLSL_TYPE_FLOAT: return this->value.f[i];
1128 case GLSL_TYPE_FLOAT16: return _mesa_half_to_float(this->value.f16[i]);
1129 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1.0f : 0.0f;
1130 case GLSL_TYPE_DOUBLE: return (float) this->value.d[i];
1131 case GLSL_TYPE_SAMPLER:
1132 case GLSL_TYPE_IMAGE:
1133 case GLSL_TYPE_UINT64: return (float) this->value.u64[i];
1134 case GLSL_TYPE_INT64: return (float) this->value.i64[i];
1135 default: assert(!"Should not get here."); break;
1136 }
1137
1138 /* Must return something to make the compiler happy. This is clearly an
1139 * error case.
1140 */
1141 return 0.0;
1142 }
1143
1144 uint16_t
get_float16_component(unsigned i) const1145 ir_constant::get_float16_component(unsigned i) const
1146 {
1147 if (this->type->base_type == GLSL_TYPE_FLOAT16)
1148 return this->value.f16[i];
1149 else
1150 return _mesa_float_to_half(get_float_component(i));
1151 }
1152
1153 double
get_double_component(unsigned i) const1154 ir_constant::get_double_component(unsigned i) const
1155 {
1156 switch (this->type->base_type) {
1157 case GLSL_TYPE_UINT16:return (double) this->value.u16[i];
1158 case GLSL_TYPE_INT16: return (double) this->value.i16[i];
1159 case GLSL_TYPE_UINT: return (double) this->value.u[i];
1160 case GLSL_TYPE_INT: return (double) this->value.i[i];
1161 case GLSL_TYPE_FLOAT: return (double) this->value.f[i];
1162 case GLSL_TYPE_FLOAT16: return (double) _mesa_half_to_float(this->value.f16[i]);
1163 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1.0 : 0.0;
1164 case GLSL_TYPE_DOUBLE: return this->value.d[i];
1165 case GLSL_TYPE_SAMPLER:
1166 case GLSL_TYPE_IMAGE:
1167 case GLSL_TYPE_UINT64: return (double) this->value.u64[i];
1168 case GLSL_TYPE_INT64: return (double) this->value.i64[i];
1169 default: assert(!"Should not get here."); break;
1170 }
1171
1172 /* Must return something to make the compiler happy. This is clearly an
1173 * error case.
1174 */
1175 return 0.0;
1176 }
1177
1178 int16_t
get_int16_component(unsigned i) const1179 ir_constant::get_int16_component(unsigned i) const
1180 {
1181 switch (this->type->base_type) {
1182 case GLSL_TYPE_UINT16:return this->value.u16[i];
1183 case GLSL_TYPE_INT16: return this->value.i16[i];
1184 case GLSL_TYPE_UINT: return this->value.u[i];
1185 case GLSL_TYPE_INT: return this->value.i[i];
1186 case GLSL_TYPE_FLOAT: return (int16_t) this->value.f[i];
1187 case GLSL_TYPE_FLOAT16: return (int16_t) _mesa_half_to_float(this->value.f16[i]);
1188 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1189 case GLSL_TYPE_DOUBLE: return (int16_t) this->value.d[i];
1190 case GLSL_TYPE_SAMPLER:
1191 case GLSL_TYPE_IMAGE:
1192 case GLSL_TYPE_UINT64: return (int16_t) this->value.u64[i];
1193 case GLSL_TYPE_INT64: return (int16_t) this->value.i64[i];
1194 default: assert(!"Should not get here."); break;
1195 }
1196
1197 /* Must return something to make the compiler happy. This is clearly an
1198 * error case.
1199 */
1200 return 0;
1201 }
1202
1203 uint16_t
get_uint16_component(unsigned i) const1204 ir_constant::get_uint16_component(unsigned i) const
1205 {
1206 switch (this->type->base_type) {
1207 case GLSL_TYPE_UINT16:return this->value.u16[i];
1208 case GLSL_TYPE_INT16: return this->value.i16[i];
1209 case GLSL_TYPE_UINT: return this->value.u[i];
1210 case GLSL_TYPE_INT: return this->value.i[i];
1211 case GLSL_TYPE_FLOAT: return (uint16_t) this->value.f[i];
1212 case GLSL_TYPE_FLOAT16: return (uint16_t) _mesa_half_to_float(this->value.f16[i]);
1213 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1214 case GLSL_TYPE_DOUBLE: return (uint16_t) this->value.d[i];
1215 case GLSL_TYPE_SAMPLER:
1216 case GLSL_TYPE_IMAGE:
1217 case GLSL_TYPE_UINT64: return (uint16_t) this->value.u64[i];
1218 case GLSL_TYPE_INT64: return (uint16_t) this->value.i64[i];
1219 default: assert(!"Should not get here."); break;
1220 }
1221
1222 /* Must return something to make the compiler happy. This is clearly an
1223 * error case.
1224 */
1225 return 0;
1226 }
1227
1228 int
get_int_component(unsigned i) const1229 ir_constant::get_int_component(unsigned i) const
1230 {
1231 switch (this->type->base_type) {
1232 case GLSL_TYPE_UINT16:return this->value.u16[i];
1233 case GLSL_TYPE_INT16: return this->value.i16[i];
1234 case GLSL_TYPE_UINT: return this->value.u[i];
1235 case GLSL_TYPE_INT: return this->value.i[i];
1236 case GLSL_TYPE_FLOAT: return (int) this->value.f[i];
1237 case GLSL_TYPE_FLOAT16: return (int) _mesa_half_to_float(this->value.f16[i]);
1238 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1239 case GLSL_TYPE_DOUBLE: return (int) this->value.d[i];
1240 case GLSL_TYPE_SAMPLER:
1241 case GLSL_TYPE_IMAGE:
1242 case GLSL_TYPE_UINT64: return (int) this->value.u64[i];
1243 case GLSL_TYPE_INT64: return (int) this->value.i64[i];
1244 default: assert(!"Should not get here."); break;
1245 }
1246
1247 /* Must return something to make the compiler happy. This is clearly an
1248 * error case.
1249 */
1250 return 0;
1251 }
1252
1253 unsigned
get_uint_component(unsigned i) const1254 ir_constant::get_uint_component(unsigned i) const
1255 {
1256 switch (this->type->base_type) {
1257 case GLSL_TYPE_UINT16:return this->value.u16[i];
1258 case GLSL_TYPE_INT16: return this->value.i16[i];
1259 case GLSL_TYPE_UINT: return this->value.u[i];
1260 case GLSL_TYPE_INT: return this->value.i[i];
1261 case GLSL_TYPE_FLOAT: return (unsigned) this->value.f[i];
1262 case GLSL_TYPE_FLOAT16: return (unsigned) _mesa_half_to_float(this->value.f16[i]);
1263 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1264 case GLSL_TYPE_DOUBLE: return (unsigned) this->value.d[i];
1265 case GLSL_TYPE_SAMPLER:
1266 case GLSL_TYPE_IMAGE:
1267 case GLSL_TYPE_UINT64: return (unsigned) this->value.u64[i];
1268 case GLSL_TYPE_INT64: return (unsigned) this->value.i64[i];
1269 default: assert(!"Should not get here."); break;
1270 }
1271
1272 /* Must return something to make the compiler happy. This is clearly an
1273 * error case.
1274 */
1275 return 0;
1276 }
1277
1278 int64_t
get_int64_component(unsigned i) const1279 ir_constant::get_int64_component(unsigned i) const
1280 {
1281 switch (this->type->base_type) {
1282 case GLSL_TYPE_UINT16:return this->value.u16[i];
1283 case GLSL_TYPE_INT16: return this->value.i16[i];
1284 case GLSL_TYPE_UINT: return this->value.u[i];
1285 case GLSL_TYPE_INT: return this->value.i[i];
1286 case GLSL_TYPE_FLOAT: return (int64_t) this->value.f[i];
1287 case GLSL_TYPE_FLOAT16: return (int64_t) _mesa_half_to_float(this->value.f16[i]);
1288 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1289 case GLSL_TYPE_DOUBLE: return (int64_t) this->value.d[i];
1290 case GLSL_TYPE_SAMPLER:
1291 case GLSL_TYPE_IMAGE:
1292 case GLSL_TYPE_UINT64: return (int64_t) this->value.u64[i];
1293 case GLSL_TYPE_INT64: return this->value.i64[i];
1294 default: assert(!"Should not get here."); break;
1295 }
1296
1297 /* Must return something to make the compiler happy. This is clearly an
1298 * error case.
1299 */
1300 return 0;
1301 }
1302
1303 uint64_t
get_uint64_component(unsigned i) const1304 ir_constant::get_uint64_component(unsigned i) const
1305 {
1306 switch (this->type->base_type) {
1307 case GLSL_TYPE_UINT16:return this->value.u16[i];
1308 case GLSL_TYPE_INT16: return this->value.i16[i];
1309 case GLSL_TYPE_UINT: return this->value.u[i];
1310 case GLSL_TYPE_INT: return this->value.i[i];
1311 case GLSL_TYPE_FLOAT: return (uint64_t) this->value.f[i];
1312 case GLSL_TYPE_FLOAT16: return (uint64_t) _mesa_half_to_float(this->value.f16[i]);
1313 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1314 case GLSL_TYPE_DOUBLE: return (uint64_t) this->value.d[i];
1315 case GLSL_TYPE_SAMPLER:
1316 case GLSL_TYPE_IMAGE:
1317 case GLSL_TYPE_UINT64: return this->value.u64[i];
1318 case GLSL_TYPE_INT64: return (uint64_t) this->value.i64[i];
1319 default: assert(!"Should not get here."); break;
1320 }
1321
1322 /* Must return something to make the compiler happy. This is clearly an
1323 * error case.
1324 */
1325 return 0;
1326 }
1327
1328 ir_constant *
get_array_element(unsigned i) const1329 ir_constant::get_array_element(unsigned i) const
1330 {
1331 assert(glsl_type_is_array(this->type));
1332
1333 /* From page 35 (page 41 of the PDF) of the GLSL 1.20 spec:
1334 *
1335 * "Behavior is undefined if a shader subscripts an array with an index
1336 * less than 0 or greater than or equal to the size the array was
1337 * declared with."
1338 *
1339 * Most out-of-bounds accesses are removed before things could get this far.
1340 * There are cases where non-constant array index values can get constant
1341 * folded.
1342 */
1343 if (int(i) < 0)
1344 i = 0;
1345 else if (i >= this->type->length)
1346 i = this->type->length - 1;
1347
1348 return const_elements[i];
1349 }
1350
1351 ir_constant *
get_record_field(int idx)1352 ir_constant::get_record_field(int idx)
1353 {
1354 assert(glsl_type_is_struct(this->type));
1355 assert(idx >= 0 && (unsigned) idx < this->type->length);
1356
1357 return const_elements[idx];
1358 }
1359
1360 void
copy_offset(ir_constant * src,int offset)1361 ir_constant::copy_offset(ir_constant *src, int offset)
1362 {
1363 switch (this->type->base_type) {
1364 case GLSL_TYPE_UINT16:
1365 case GLSL_TYPE_INT16:
1366 case GLSL_TYPE_UINT:
1367 case GLSL_TYPE_INT:
1368 case GLSL_TYPE_FLOAT:
1369 case GLSL_TYPE_FLOAT16:
1370 case GLSL_TYPE_DOUBLE:
1371 case GLSL_TYPE_SAMPLER:
1372 case GLSL_TYPE_IMAGE:
1373 case GLSL_TYPE_UINT64:
1374 case GLSL_TYPE_INT64:
1375 case GLSL_TYPE_BOOL: {
1376 unsigned int size = glsl_get_components(src->type);
1377 assert (size <= glsl_get_components(this->type) - offset);
1378 for (unsigned int i=0; i<size; i++) {
1379 switch (this->type->base_type) {
1380 case GLSL_TYPE_UINT16:
1381 value.u16[i+offset] = src->get_uint16_component(i);
1382 break;
1383 case GLSL_TYPE_INT16:
1384 value.i16[i+offset] = src->get_int16_component(i);
1385 break;
1386 case GLSL_TYPE_UINT:
1387 value.u[i+offset] = src->get_uint_component(i);
1388 break;
1389 case GLSL_TYPE_INT:
1390 value.i[i+offset] = src->get_int_component(i);
1391 break;
1392 case GLSL_TYPE_FLOAT:
1393 value.f[i+offset] = src->get_float_component(i);
1394 break;
1395 case GLSL_TYPE_FLOAT16:
1396 value.f16[i+offset] = src->get_float16_component(i);
1397 break;
1398 case GLSL_TYPE_BOOL:
1399 value.b[i+offset] = src->get_bool_component(i);
1400 break;
1401 case GLSL_TYPE_DOUBLE:
1402 value.d[i+offset] = src->get_double_component(i);
1403 break;
1404 case GLSL_TYPE_SAMPLER:
1405 case GLSL_TYPE_IMAGE:
1406 case GLSL_TYPE_UINT64:
1407 value.u64[i+offset] = src->get_uint64_component(i);
1408 break;
1409 case GLSL_TYPE_INT64:
1410 value.i64[i+offset] = src->get_int64_component(i);
1411 break;
1412 default: // Shut up the compiler
1413 break;
1414 }
1415 }
1416 break;
1417 }
1418
1419 case GLSL_TYPE_STRUCT:
1420 case GLSL_TYPE_ARRAY: {
1421 assert (src->type == this->type);
1422 for (unsigned i = 0; i < this->type->length; i++) {
1423 this->const_elements[i] = src->const_elements[i]->clone(this, NULL);
1424 }
1425 break;
1426 }
1427
1428 default:
1429 assert(!"Should not get here.");
1430 break;
1431 }
1432 }
1433
1434 void
copy_masked_offset(ir_constant * src,int offset,unsigned int mask)1435 ir_constant::copy_masked_offset(ir_constant *src, int offset, unsigned int mask)
1436 {
1437 assert (!glsl_type_is_array(type) && !glsl_type_is_struct(type));
1438
1439 if (!glsl_type_is_vector(type) && !glsl_type_is_matrix(type)) {
1440 offset = 0;
1441 mask = 1;
1442 }
1443
1444 int id = 0;
1445 for (int i=0; i<4; i++) {
1446 if (mask & (1 << i)) {
1447 switch (this->type->base_type) {
1448 case GLSL_TYPE_UINT16:
1449 value.u16[i+offset] = src->get_uint16_component(id++);
1450 break;
1451 case GLSL_TYPE_INT16:
1452 value.i16[i+offset] = src->get_int16_component(id++);
1453 break;
1454 case GLSL_TYPE_UINT:
1455 value.u[i+offset] = src->get_uint_component(id++);
1456 break;
1457 case GLSL_TYPE_INT:
1458 value.i[i+offset] = src->get_int_component(id++);
1459 break;
1460 case GLSL_TYPE_FLOAT:
1461 value.f[i+offset] = src->get_float_component(id++);
1462 break;
1463 case GLSL_TYPE_FLOAT16:
1464 value.f16[i+offset] = src->get_float16_component(id++);
1465 break;
1466 case GLSL_TYPE_BOOL:
1467 value.b[i+offset] = src->get_bool_component(id++);
1468 break;
1469 case GLSL_TYPE_DOUBLE:
1470 value.d[i+offset] = src->get_double_component(id++);
1471 break;
1472 case GLSL_TYPE_SAMPLER:
1473 case GLSL_TYPE_IMAGE:
1474 case GLSL_TYPE_UINT64:
1475 value.u64[i+offset] = src->get_uint64_component(id++);
1476 break;
1477 case GLSL_TYPE_INT64:
1478 value.i64[i+offset] = src->get_int64_component(id++);
1479 break;
1480 default:
1481 assert(!"Should not get here.");
1482 return;
1483 }
1484 }
1485 }
1486 }
1487
1488 bool
has_value(const ir_constant * c) const1489 ir_constant::has_value(const ir_constant *c) const
1490 {
1491 if (this->type != c->type)
1492 return false;
1493
1494 if (glsl_type_is_array(this->type) || glsl_type_is_struct(this->type)) {
1495 for (unsigned i = 0; i < this->type->length; i++) {
1496 if (!this->const_elements[i]->has_value(c->const_elements[i]))
1497 return false;
1498 }
1499 return true;
1500 }
1501
1502 for (unsigned i = 0; i < glsl_get_components(this->type); i++) {
1503 switch (this->type->base_type) {
1504 case GLSL_TYPE_UINT16:
1505 if (this->value.u16[i] != c->value.u16[i])
1506 return false;
1507 break;
1508 case GLSL_TYPE_INT16:
1509 if (this->value.i16[i] != c->value.i16[i])
1510 return false;
1511 break;
1512 case GLSL_TYPE_UINT:
1513 if (this->value.u[i] != c->value.u[i])
1514 return false;
1515 break;
1516 case GLSL_TYPE_INT:
1517 if (this->value.i[i] != c->value.i[i])
1518 return false;
1519 break;
1520 case GLSL_TYPE_FLOAT:
1521 if (this->value.f[i] != c->value.f[i])
1522 return false;
1523 break;
1524 case GLSL_TYPE_FLOAT16:
1525 /* Convert to float to make sure NaN and ±0.0 compares correctly */
1526 if (_mesa_half_to_float(this->value.f16[i]) !=
1527 _mesa_half_to_float(c->value.f16[i]))
1528 return false;
1529 break;
1530 case GLSL_TYPE_BOOL:
1531 if (this->value.b[i] != c->value.b[i])
1532 return false;
1533 break;
1534 case GLSL_TYPE_DOUBLE:
1535 if (this->value.d[i] != c->value.d[i])
1536 return false;
1537 break;
1538 case GLSL_TYPE_SAMPLER:
1539 case GLSL_TYPE_IMAGE:
1540 case GLSL_TYPE_UINT64:
1541 if (this->value.u64[i] != c->value.u64[i])
1542 return false;
1543 break;
1544 case GLSL_TYPE_INT64:
1545 if (this->value.i64[i] != c->value.i64[i])
1546 return false;
1547 break;
1548 default:
1549 assert(!"Should not get here.");
1550 return false;
1551 }
1552 }
1553
1554 return true;
1555 }
1556
1557 bool
is_value(float f,int i) const1558 ir_constant::is_value(float f, int i) const
1559 {
1560 if (!glsl_type_is_scalar(this->type) && !glsl_type_is_vector(this->type))
1561 return false;
1562
1563 /* Only accept boolean values for 0/1. */
1564 if (int(bool(i)) != i && glsl_type_is_boolean(this->type))
1565 return false;
1566
1567 for (unsigned c = 0; c < this->type->vector_elements; c++) {
1568 switch (this->type->base_type) {
1569 case GLSL_TYPE_FLOAT:
1570 if (this->value.f[c] != f)
1571 return false;
1572 break;
1573 case GLSL_TYPE_FLOAT16:
1574 if (_mesa_half_to_float(this->value.f16[c]) != f)
1575 return false;
1576 break;
1577 case GLSL_TYPE_INT16:
1578 if (this->value.i16[c] != int16_t(i))
1579 return false;
1580 break;
1581 case GLSL_TYPE_UINT16:
1582 if (this->value.u16[c] != uint16_t(i))
1583 return false;
1584 break;
1585 case GLSL_TYPE_INT:
1586 if (this->value.i[c] != i)
1587 return false;
1588 break;
1589 case GLSL_TYPE_UINT:
1590 if (this->value.u[c] != unsigned(i))
1591 return false;
1592 break;
1593 case GLSL_TYPE_BOOL:
1594 if (this->value.b[c] != bool(i))
1595 return false;
1596 break;
1597 case GLSL_TYPE_DOUBLE:
1598 if (this->value.d[c] != double(f))
1599 return false;
1600 break;
1601 case GLSL_TYPE_SAMPLER:
1602 case GLSL_TYPE_IMAGE:
1603 case GLSL_TYPE_UINT64:
1604 if (this->value.u64[c] != uint64_t(i))
1605 return false;
1606 break;
1607 case GLSL_TYPE_INT64:
1608 if (this->value.i64[c] != i)
1609 return false;
1610 break;
1611 default:
1612 /* The only other base types are structures, arrays, and samplers.
1613 * Samplers cannot be constants, and the others should have been
1614 * filtered out above.
1615 */
1616 assert(!"Should not get here.");
1617 return false;
1618 }
1619 }
1620
1621 return true;
1622 }
1623
1624 bool
is_zero() const1625 ir_constant::is_zero() const
1626 {
1627 return is_value(0.0, 0);
1628 }
1629
1630 bool
is_one() const1631 ir_constant::is_one() const
1632 {
1633 return is_value(1.0, 1);
1634 }
1635
1636 bool
is_negative_one() const1637 ir_constant::is_negative_one() const
1638 {
1639 return is_value(-1.0, -1);
1640 }
1641
1642 bool
is_uint16_constant() const1643 ir_constant::is_uint16_constant() const
1644 {
1645 if (!glsl_type_is_integer_32(type))
1646 return false;
1647
1648 return value.u[0] < (1 << 16);
1649 }
1650
ir_loop()1651 ir_loop::ir_loop()
1652 : ir_instruction(ir_type_loop)
1653 {
1654 }
1655
1656
ir_dereference_variable(ir_variable * var)1657 ir_dereference_variable::ir_dereference_variable(ir_variable *var)
1658 : ir_dereference(ir_type_dereference_variable)
1659 {
1660 assert(var != NULL);
1661
1662 this->var = var;
1663 this->type = var->type;
1664 }
1665
1666
ir_dereference_array(ir_rvalue * value,ir_rvalue * array_index)1667 ir_dereference_array::ir_dereference_array(ir_rvalue *value,
1668 ir_rvalue *array_index)
1669 : ir_dereference(ir_type_dereference_array)
1670 {
1671 this->array_index = array_index;
1672 this->set_array(value);
1673 }
1674
1675
ir_dereference_array(ir_variable * var,ir_rvalue * array_index)1676 ir_dereference_array::ir_dereference_array(ir_variable *var,
1677 ir_rvalue *array_index)
1678 : ir_dereference(ir_type_dereference_array)
1679 {
1680 void *ctx = ralloc_parent(var);
1681
1682 this->array_index = array_index;
1683 this->set_array(new(ctx) ir_dereference_variable(var));
1684 }
1685
1686
1687 void
set_array(ir_rvalue * value)1688 ir_dereference_array::set_array(ir_rvalue *value)
1689 {
1690 assert(value != NULL);
1691
1692 this->array = value;
1693
1694 const glsl_type *const vt = this->array->type;
1695
1696 if (glsl_type_is_array(vt)) {
1697 type = vt->fields.array;
1698 } else if (glsl_type_is_matrix(vt)) {
1699 type = glsl_get_column_type(vt);
1700 } else if (glsl_type_is_vector(vt)) {
1701 type = glsl_get_base_glsl_type(vt);
1702 }
1703 }
1704
1705
ir_dereference_record(ir_rvalue * value,const char * field)1706 ir_dereference_record::ir_dereference_record(ir_rvalue *value,
1707 const char *field)
1708 : ir_dereference(ir_type_dereference_record)
1709 {
1710 assert(value != NULL);
1711
1712 this->record = value;
1713 this->type = glsl_get_field_type(this->record->type, field);
1714 this->field_idx = glsl_get_field_index(this->record->type, field);
1715 }
1716
1717
ir_dereference_record(ir_variable * var,const char * field)1718 ir_dereference_record::ir_dereference_record(ir_variable *var,
1719 const char *field)
1720 : ir_dereference(ir_type_dereference_record)
1721 {
1722 void *ctx = ralloc_parent(var);
1723
1724 this->record = new(ctx) ir_dereference_variable(var);
1725 this->type = glsl_get_field_type(this->record->type, field);
1726 this->field_idx = glsl_get_field_index(this->record->type, field);
1727 }
1728
1729 bool
is_lvalue(const struct _mesa_glsl_parse_state * state) const1730 ir_dereference::is_lvalue(const struct _mesa_glsl_parse_state *state) const
1731 {
1732 ir_variable *var = this->variable_referenced();
1733
1734 /* Every l-value dereference chain eventually ends in a variable.
1735 */
1736 if ((var == NULL) || var->data.read_only)
1737 return false;
1738
1739 /* From section 4.1.7 of the ARB_bindless_texture spec:
1740 *
1741 * "Samplers can be used as l-values, so can be assigned into and used as
1742 * "out" and "inout" function parameters."
1743 *
1744 * From section 4.1.X of the ARB_bindless_texture spec:
1745 *
1746 * "Images can be used as l-values, so can be assigned into and used as
1747 * "out" and "inout" function parameters."
1748 */
1749 if ((!state || state->has_bindless()) &&
1750 (glsl_contains_sampler(this->type) || glsl_type_contains_image(this->type)))
1751 return true;
1752
1753 /* From section 4.1.7 of the GLSL 4.40 spec:
1754 *
1755 * "Opaque variables cannot be treated as l-values; hence cannot
1756 * be used as out or inout function parameters, nor can they be
1757 * assigned into."
1758 */
1759 if (glsl_contains_opaque(this->type))
1760 return false;
1761
1762 return true;
1763 }
1764
1765
1766 static const char * const tex_opcode_strs[] = { "tex", "txb", "txl", "txd", "txf", "txf_ms", "txs", "lod", "tg4", "query_levels", "texture_samples", "samples_identical" };
1767
opcode_string()1768 const char *ir_texture::opcode_string()
1769 {
1770 assert((unsigned int) op < ARRAY_SIZE(tex_opcode_strs));
1771 return tex_opcode_strs[op];
1772 }
1773
1774 ir_texture_opcode
get_opcode(const char * str)1775 ir_texture::get_opcode(const char *str)
1776 {
1777 const int count = sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]);
1778 for (int op = 0; op < count; op++) {
1779 if (strcmp(str, tex_opcode_strs[op]) == 0)
1780 return (ir_texture_opcode) op;
1781 }
1782 return (ir_texture_opcode) -1;
1783 }
1784
1785
1786 void
set_sampler(ir_dereference * sampler,const glsl_type * type)1787 ir_texture::set_sampler(ir_dereference *sampler, const glsl_type *type)
1788 {
1789 assert(sampler != NULL);
1790 assert(type != NULL);
1791 this->sampler = sampler;
1792
1793 if (this->is_sparse) {
1794 /* code holds residency info */
1795 glsl_struct_field fields[2] = {
1796 glsl_struct_field(&glsl_type_builtin_int, "code"),
1797 glsl_struct_field(type, "texel"),
1798 };
1799 this->type = glsl_struct_type(fields, 2, "struct", false /* packed */);
1800 } else
1801 this->type = type;
1802
1803 if (this->op == ir_txs || this->op == ir_query_levels ||
1804 this->op == ir_texture_samples) {
1805 assert(type->base_type == GLSL_TYPE_INT);
1806 } else if (this->op == ir_lod) {
1807 assert(type->vector_elements == 2);
1808 assert(glsl_type_is_float(type));
1809 } else if (this->op == ir_samples_identical) {
1810 assert(type == &glsl_type_builtin_bool);
1811 assert(glsl_type_is_sampler(sampler->type));
1812 assert(sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS);
1813 } else {
1814 assert(sampler->type->sampled_type == (int) type->base_type);
1815 if (sampler->type->sampler_shadow)
1816 assert(type->vector_elements == 4 || type->vector_elements == 1);
1817 else
1818 assert(type->vector_elements == 4);
1819 }
1820 }
1821
1822
1823 void
init_mask(const unsigned * comp,unsigned count)1824 ir_swizzle::init_mask(const unsigned *comp, unsigned count)
1825 {
1826 assert((count >= 1) && (count <= 4));
1827
1828 memset(&this->mask, 0, sizeof(this->mask));
1829 this->mask.num_components = count;
1830
1831 unsigned dup_mask = 0;
1832 switch (count) {
1833 case 4:
1834 assert(comp[3] <= 3);
1835 dup_mask |= (1U << comp[3])
1836 & ((1U << comp[0]) | (1U << comp[1]) | (1U << comp[2]));
1837 this->mask.w = comp[3];
1838
1839 case 3:
1840 assert(comp[2] <= 3);
1841 dup_mask |= (1U << comp[2])
1842 & ((1U << comp[0]) | (1U << comp[1]));
1843 this->mask.z = comp[2];
1844
1845 case 2:
1846 assert(comp[1] <= 3);
1847 dup_mask |= (1U << comp[1])
1848 & ((1U << comp[0]));
1849 this->mask.y = comp[1];
1850
1851 case 1:
1852 assert(comp[0] <= 3);
1853 this->mask.x = comp[0];
1854 }
1855
1856 this->mask.has_duplicates = dup_mask != 0;
1857
1858 /* Based on the number of elements in the swizzle and the base type
1859 * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
1860 * generate the type of the resulting value.
1861 */
1862 type = glsl_simple_type(val->type->base_type, mask.num_components, 1);
1863 }
1864
ir_swizzle(ir_rvalue * val,unsigned x,unsigned y,unsigned z,unsigned w,unsigned count)1865 ir_swizzle::ir_swizzle(ir_rvalue *val, unsigned x, unsigned y, unsigned z,
1866 unsigned w, unsigned count)
1867 : ir_rvalue(ir_type_swizzle), val(val)
1868 {
1869 const unsigned components[4] = { x, y, z, w };
1870 this->init_mask(components, count);
1871 }
1872
ir_swizzle(ir_rvalue * val,const unsigned * comp,unsigned count)1873 ir_swizzle::ir_swizzle(ir_rvalue *val, const unsigned *comp,
1874 unsigned count)
1875 : ir_rvalue(ir_type_swizzle), val(val)
1876 {
1877 this->init_mask(comp, count);
1878 }
1879
ir_swizzle(ir_rvalue * val,ir_swizzle_mask mask)1880 ir_swizzle::ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask)
1881 : ir_rvalue(ir_type_swizzle), val(val), mask(mask)
1882 {
1883 this->type = glsl_simple_type(val->type->base_type, mask.num_components, 1);
1884 }
1885
1886 #define X 1
1887 #define R 5
1888 #define S 9
1889 #define I 13
1890
1891 ir_swizzle *
create(ir_rvalue * val,const char * str,unsigned vector_length)1892 ir_swizzle::create(ir_rvalue *val, const char *str, unsigned vector_length)
1893 {
1894 void *ctx = ralloc_parent(val);
1895
1896 /* For each possible swizzle character, this table encodes the value in
1897 * \c idx_map that represents the 0th element of the vector. For invalid
1898 * swizzle characters (e.g., 'k'), a special value is used that will allow
1899 * detection of errors.
1900 */
1901 static const unsigned char base_idx[26] = {
1902 /* a b c d e f g h i j k l m */
1903 R, R, I, I, I, I, R, I, I, I, I, I, I,
1904 /* n o p q r s t u v w x y z */
1905 I, I, S, S, R, S, S, I, I, X, X, X, X
1906 };
1907
1908 /* Each valid swizzle character has an entry in the previous table. This
1909 * table encodes the base index encoded in the previous table plus the actual
1910 * index of the swizzle character. When processing swizzles, the first
1911 * character in the string is indexed in the previous table. Each character
1912 * in the string is indexed in this table, and the value found there has the
1913 * value form the first table subtracted. The result must be on the range
1914 * [0,3].
1915 *
1916 * For example, the string "wzyx" will get X from the first table. Each of
1917 * the charcaters will get X+3, X+2, X+1, and X+0 from this table. After
1918 * subtraction, the swizzle values are { 3, 2, 1, 0 }.
1919 *
1920 * The string "wzrg" will get X from the first table. Each of the characters
1921 * will get X+3, X+2, R+0, and R+1 from this table. After subtraction, the
1922 * swizzle values are { 3, 2, 4, 5 }. Since 4 and 5 are outside the range
1923 * [0,3], the error is detected.
1924 */
1925 static const unsigned char idx_map[26] = {
1926 /* a b c d e f g h i j k l m */
1927 R+3, R+2, 0, 0, 0, 0, R+1, 0, 0, 0, 0, 0, 0,
1928 /* n o p q r s t u v w x y z */
1929 0, 0, S+2, S+3, R+0, S+0, S+1, 0, 0, X+3, X+0, X+1, X+2
1930 };
1931
1932 int swiz_idx[4] = { 0, 0, 0, 0 };
1933 unsigned i;
1934
1935
1936 /* Validate the first character in the swizzle string and look up the base
1937 * index value as described above.
1938 */
1939 if ((str[0] < 'a') || (str[0] > 'z'))
1940 return NULL;
1941
1942 const unsigned base = base_idx[str[0] - 'a'];
1943
1944
1945 for (i = 0; (i < 4) && (str[i] != '\0'); i++) {
1946 /* Validate the next character, and, as described above, convert it to a
1947 * swizzle index.
1948 */
1949 if ((str[i] < 'a') || (str[i] > 'z'))
1950 return NULL;
1951
1952 swiz_idx[i] = idx_map[str[i] - 'a'] - base;
1953 if ((swiz_idx[i] < 0) || (swiz_idx[i] >= (int) vector_length))
1954 return NULL;
1955 }
1956
1957 if (str[i] != '\0')
1958 return NULL;
1959
1960 return new(ctx) ir_swizzle(val, swiz_idx[0], swiz_idx[1], swiz_idx[2],
1961 swiz_idx[3], i);
1962 }
1963
1964 #undef X
1965 #undef R
1966 #undef S
1967 #undef I
1968
1969 ir_variable *
variable_referenced() const1970 ir_swizzle::variable_referenced() const
1971 {
1972 return this->val->variable_referenced();
1973 }
1974
1975
1976 bool ir_variable::temporaries_allocate_names = false;
1977
1978 const char ir_variable::tmp_name[] = "compiler_temp";
1979
ir_variable(const struct glsl_type * type,const char * name,ir_variable_mode mode)1980 ir_variable::ir_variable(const struct glsl_type *type, const char *name,
1981 ir_variable_mode mode)
1982 : ir_instruction(ir_type_variable)
1983 {
1984 this->type = type;
1985
1986 if (mode == ir_var_temporary && !ir_variable::temporaries_allocate_names)
1987 name = NULL;
1988
1989 /* The ir_variable clone method may call this constructor with name set to
1990 * tmp_name.
1991 */
1992 assert(name != NULL
1993 || mode == ir_var_temporary
1994 || mode == ir_var_function_in
1995 || mode == ir_var_function_out
1996 || mode == ir_var_function_inout);
1997 assert(name != ir_variable::tmp_name
1998 || mode == ir_var_temporary);
1999 if (mode == ir_var_temporary
2000 && (name == NULL || name == ir_variable::tmp_name)) {
2001 this->name = ir_variable::tmp_name;
2002 } else if (name == NULL ||
2003 strlen(name) < ARRAY_SIZE(this->name_storage)) {
2004 strcpy(this->name_storage, name ? name : "");
2005 this->name = this->name_storage;
2006 } else {
2007 this->name = ralloc_strdup(this, name);
2008 }
2009
2010 this->u.max_ifc_array_access = NULL;
2011
2012 this->data.explicit_location = false;
2013 this->data.explicit_index = false;
2014 this->data.explicit_binding = false;
2015 this->data.explicit_component = false;
2016 this->data.has_initializer = false;
2017 this->data.is_implicit_initializer = false;
2018 this->data.is_xfb = false;
2019 this->data.is_xfb_only = false;
2020 this->data.explicit_xfb_buffer = false;
2021 this->data.explicit_xfb_offset = false;
2022 this->data.explicit_xfb_stride = false;
2023 this->data.location = -1;
2024 this->data.location_frac = 0;
2025 this->data.matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED;
2026 this->data.from_named_ifc_block = false;
2027 this->data.must_be_shader_input = false;
2028 this->data.index = 0;
2029 this->data.binding = 0;
2030 this->data.warn_extension_index = 0;
2031 this->constant_value = NULL;
2032 this->constant_initializer = NULL;
2033 this->data.depth_layout = ir_depth_layout_none;
2034 this->data.used = false;
2035 this->data.assigned = false;
2036 this->data.read_only = false;
2037 this->data.centroid = false;
2038 this->data.sample = false;
2039 this->data.patch = false;
2040 this->data.explicit_invariant = false;
2041 this->data.invariant = false;
2042 this->data.precise = false;
2043 this->data.how_declared =
2044 mode == ir_var_temporary ? ir_var_hidden : ir_var_declared_normally;
2045 this->data.mode = mode;
2046 this->data.interpolation = INTERP_MODE_NONE;
2047 this->data.max_array_access = -1;
2048 this->data.offset = 0;
2049 this->data.precision = GLSL_PRECISION_NONE;
2050 this->data.memory_read_only = false;
2051 this->data.memory_write_only = false;
2052 this->data.memory_coherent = false;
2053 this->data.memory_volatile = false;
2054 this->data.memory_restrict = false;
2055 this->data.from_ssbo_unsized_array = false;
2056 this->data.implicit_sized_array = false;
2057 this->data.fb_fetch_output = false;
2058 this->data.bindless = false;
2059 this->data.bound = false;
2060 this->data.image_format = PIPE_FORMAT_NONE;
2061 this->data._num_state_slots = 0;
2062 this->data.param_index = 0;
2063 this->data.stream = 0;
2064 this->data.xfb_buffer = -1;
2065 this->data.xfb_stride = -1;
2066 this->data.implicit_conversion_prohibited = false;
2067
2068 this->interface_type = NULL;
2069
2070 if (type != NULL) {
2071 if (glsl_type_is_interface(type))
2072 this->init_interface_type(type);
2073 else if (glsl_type_is_interface(glsl_without_array(type)))
2074 this->init_interface_type(glsl_without_array(type));
2075 }
2076 }
2077
2078 const char *const ir_variable::warn_extension_table[] = {
2079 "",
2080 "GL_ARB_shader_stencil_export",
2081 "GL_AMD_shader_stencil_export",
2082 };
2083
2084 void
enable_extension_warning(const char * extension)2085 ir_variable::enable_extension_warning(const char *extension)
2086 {
2087 for (unsigned i = 0; i < ARRAY_SIZE(warn_extension_table); i++) {
2088 if (strcmp(warn_extension_table[i], extension) == 0) {
2089 this->data.warn_extension_index = i;
2090 return;
2091 }
2092 }
2093
2094 assert(!"Should not get here.");
2095 this->data.warn_extension_index = 0;
2096 }
2097
2098 const char *
get_extension_warning() const2099 ir_variable::get_extension_warning() const
2100 {
2101 return this->data.warn_extension_index == 0
2102 ? NULL : warn_extension_table[this->data.warn_extension_index];
2103 }
2104
ir_function_signature(const glsl_type * return_type,builtin_available_predicate b)2105 ir_function_signature::ir_function_signature(const glsl_type *return_type,
2106 builtin_available_predicate b)
2107 : ir_instruction(ir_type_function_signature),
2108 return_type(return_type), is_defined(false),
2109 return_precision(GLSL_PRECISION_NONE),
2110 intrinsic_id(ir_intrinsic_invalid), builtin_avail(b), _function(NULL)
2111 {
2112 this->origin = NULL;
2113 }
2114
2115
2116 bool
is_builtin() const2117 ir_function_signature::is_builtin() const
2118 {
2119 return builtin_avail != NULL;
2120 }
2121
2122
2123 bool
is_builtin_available(const _mesa_glsl_parse_state * state) const2124 ir_function_signature::is_builtin_available(const _mesa_glsl_parse_state *state) const
2125 {
2126 /* We can't call the predicate without a state pointer, so just say that
2127 * the signature is available. At compile time, we need the filtering,
2128 * but also receive a valid state pointer. At link time, we're resolving
2129 * imported built-in prototypes to their definitions, which will always
2130 * be an exact match. So we can skip the filtering.
2131 */
2132 if (state == NULL)
2133 return true;
2134
2135 assert(builtin_avail != NULL);
2136 return builtin_avail(state);
2137 }
2138
2139
2140 static bool
modes_match(unsigned a,unsigned b)2141 modes_match(unsigned a, unsigned b)
2142 {
2143 if (a == b)
2144 return true;
2145
2146 /* Accept "in" vs. "const in" */
2147 if ((a == ir_var_const_in && b == ir_var_function_in) ||
2148 (b == ir_var_const_in && a == ir_var_function_in))
2149 return true;
2150
2151 return false;
2152 }
2153
2154
2155 const char *
qualifiers_match(exec_list * params)2156 ir_function_signature::qualifiers_match(exec_list *params)
2157 {
2158 /* check that the qualifiers match. */
2159 foreach_two_lists(a_node, &this->parameters, b_node, params) {
2160 ir_variable *a = (ir_variable *) a_node;
2161 ir_variable *b = (ir_variable *) b_node;
2162
2163 if (a->data.read_only != b->data.read_only ||
2164 !modes_match(a->data.mode, b->data.mode) ||
2165 a->data.interpolation != b->data.interpolation ||
2166 a->data.centroid != b->data.centroid ||
2167 a->data.sample != b->data.sample ||
2168 a->data.patch != b->data.patch ||
2169 a->data.memory_read_only != b->data.memory_read_only ||
2170 a->data.memory_write_only != b->data.memory_write_only ||
2171 a->data.memory_coherent != b->data.memory_coherent ||
2172 a->data.memory_volatile != b->data.memory_volatile ||
2173 a->data.memory_restrict != b->data.memory_restrict) {
2174
2175 /* parameter a's qualifiers don't match */
2176 return a->name;
2177 }
2178 }
2179 return NULL;
2180 }
2181
2182
2183 void
replace_parameters(exec_list * new_params)2184 ir_function_signature::replace_parameters(exec_list *new_params)
2185 {
2186 /* Destroy all of the previous parameter information. If the previous
2187 * parameter information comes from the function prototype, it may either
2188 * specify incorrect parameter names or not have names at all.
2189 */
2190 new_params->move_nodes_to(¶meters);
2191 }
2192
2193
ir_function(const char * name)2194 ir_function::ir_function(const char *name)
2195 : ir_instruction(ir_type_function)
2196 {
2197 this->subroutine_index = -1;
2198 this->name = ralloc_strdup(this, name);
2199 }
2200
2201
2202 bool
has_user_signature()2203 ir_function::has_user_signature()
2204 {
2205 foreach_in_list(ir_function_signature, sig, &this->signatures) {
2206 if (!sig->is_builtin())
2207 return true;
2208 }
2209 return false;
2210 }
2211
2212
2213 ir_rvalue *
error_value(void * mem_ctx)2214 ir_rvalue::error_value(void *mem_ctx)
2215 {
2216 ir_rvalue *v = new(mem_ctx) ir_rvalue(ir_type_error);
2217
2218 v->type = &glsl_type_builtin_error;
2219 return v;
2220 }
2221
2222
2223 void
visit_exec_list(exec_list * list,ir_visitor * visitor)2224 visit_exec_list(exec_list *list, ir_visitor *visitor)
2225 {
2226 foreach_in_list(ir_instruction, node, list) {
2227 node->accept(visitor);
2228 }
2229 }
2230
2231 void
visit_exec_list_safe(exec_list * list,ir_visitor * visitor)2232 visit_exec_list_safe(exec_list *list, ir_visitor *visitor)
2233 {
2234 foreach_in_list_safe(ir_instruction, node, list) {
2235 node->accept(visitor);
2236 }
2237 }
2238
2239
2240 static void
steal_memory(ir_instruction * ir,void * new_ctx)2241 steal_memory(ir_instruction *ir, void *new_ctx)
2242 {
2243 ir_variable *var = ir->as_variable();
2244 ir_function *fn = ir->as_function();
2245 ir_constant *constant = ir->as_constant();
2246 if (var != NULL && var->constant_value != NULL)
2247 steal_memory(var->constant_value, ir);
2248
2249 if (var != NULL && var->constant_initializer != NULL)
2250 steal_memory(var->constant_initializer, ir);
2251
2252 if (fn != NULL && fn->subroutine_types)
2253 ralloc_steal(new_ctx, fn->subroutine_types);
2254
2255 /* The components of aggregate constants are not visited by the normal
2256 * visitor, so steal their values by hand.
2257 */
2258 if (constant != NULL &&
2259 (glsl_type_is_array(constant->type) || glsl_type_is_struct(constant->type))) {
2260 for (unsigned int i = 0; i < constant->type->length; i++) {
2261 steal_memory(constant->const_elements[i], ir);
2262 }
2263 }
2264
2265 ralloc_steal(new_ctx, ir);
2266 }
2267
2268
2269 void
reparent_ir(exec_list * list,void * mem_ctx)2270 reparent_ir(exec_list *list, void *mem_ctx)
2271 {
2272 foreach_in_list(ir_instruction, node, list) {
2273 visit_tree(node, steal_memory, mem_ctx);
2274 }
2275 }
2276
2277 enum mesa_prim
gl_to_mesa_prim(GLenum prim)2278 gl_to_mesa_prim(GLenum prim)
2279 {
2280 STATIC_ASSERT(GL_POINTS == MESA_PRIM_POINTS);
2281 STATIC_ASSERT(GL_LINES == MESA_PRIM_LINES);
2282 STATIC_ASSERT(GL_LINES_ADJACENCY == MESA_PRIM_LINES_ADJACENCY);
2283 STATIC_ASSERT(GL_LINE_STRIP == MESA_PRIM_LINE_STRIP);
2284 STATIC_ASSERT(GL_TRIANGLES == MESA_PRIM_TRIANGLES);
2285 STATIC_ASSERT(GL_TRIANGLES_ADJACENCY == MESA_PRIM_TRIANGLES_ADJACENCY);
2286 STATIC_ASSERT(GL_TRIANGLE_STRIP == MESA_PRIM_TRIANGLE_STRIP);
2287
2288 return (enum mesa_prim)prim;
2289 }
2290
2291 /**
2292 * Generate a string describing the mode of a variable
2293 */
2294 const char *
mode_string(const ir_variable * var)2295 mode_string(const ir_variable *var)
2296 {
2297 switch (var->data.mode) {
2298 case ir_var_auto:
2299 return (var->data.read_only) ? "global constant" : "global variable";
2300
2301 case ir_var_uniform:
2302 return "uniform";
2303
2304 case ir_var_shader_storage:
2305 return "buffer";
2306
2307 case ir_var_shader_in:
2308 return "shader input";
2309
2310 case ir_var_shader_out:
2311 return "shader output";
2312
2313 case ir_var_function_in:
2314 case ir_var_const_in:
2315 return "function input";
2316
2317 case ir_var_function_out:
2318 return "function output";
2319
2320 case ir_var_function_inout:
2321 return "function inout";
2322
2323 case ir_var_system_value:
2324 return "shader input";
2325
2326 case ir_var_temporary:
2327 return "compiler temporary";
2328
2329 case ir_var_mode_count:
2330 break;
2331 }
2332
2333 assert(!"Should not get here.");
2334 return "invalid variable";
2335 }
2336