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