2
* Copyright © 2010 Intel Corporation
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:
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
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.
25
#include "util/half_float.h"
26
#include "util/bitscan.h"
27
#include "compiler/glsl_types.h"
28
#include "glsl_parser_extras.h"
31
ir_rvalue::ir_rvalue(enum ir_node_type t)
34
this->type = glsl_type::error_type;
37
bool ir_rvalue::is_zero() const
42
bool ir_rvalue::is_one() const
47
bool ir_rvalue::is_negative_one() const
53
* Modify the swizzle make to move one component to another
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
60
update_rhs_swizzle(ir_swizzle_mask &m, unsigned from, unsigned 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.");
72
ir_assignment::set_lhs(ir_rvalue *lhs)
75
bool swizzled = false;
78
ir_swizzle *swiz = lhs->as_swizzle();
83
unsigned write_mask = 0;
84
ir_swizzle_mask rhs_swiz = { 0, 0, 0, 0, 0, 0 };
86
for (unsigned i = 0; i < swiz->mask.num_components; 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.");
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;
102
this->write_mask = write_mask;
105
this->rhs = new(mem_ctx) ir_swizzle(this->rhs, rhs_swiz);
110
/* Now, RHS channels line up with the LHS writemask. Collapse it
111
* to just the channels that will be written.
113
ir_swizzle_mask rhs_swiz = { 0, 0, 0, 0, 0, 0 };
115
for (int i = 0; i < 4; i++) {
116
if (write_mask & (1 << i))
117
update_rhs_swizzle(rhs_swiz, i, rhs_chan++);
119
rhs_swiz.num_components = rhs_chan;
120
this->rhs = new(mem_ctx) ir_swizzle(this->rhs, rhs_swiz);
123
assert((lhs == NULL) || lhs->as_dereference());
125
this->lhs = (ir_dereference *) lhs;
129
ir_assignment::whole_variable_written()
131
ir_variable *v = this->lhs->whole_variable_referenced();
136
if (v->type->is_scalar())
139
if (v->type->is_vector()) {
140
const unsigned mask = (1U << v->type->vector_elements) - 1;
142
if (mask != this->write_mask)
146
/* Either all the vector components are assigned or the variable is some
147
* composite type (and the whole thing is assigned.
152
ir_assignment::ir_assignment(ir_dereference *lhs, ir_rvalue *rhs,
154
: ir_instruction(ir_type_assignment)
158
this->write_mask = write_mask;
160
if (lhs->type->is_scalar() || lhs->type->is_vector())
161
assert(util_bitcount(write_mask) == this->rhs->type->vector_elements);
164
ir_assignment::ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs)
165
: ir_instruction(ir_type_assignment)
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:
174
* (assign (...) (xyz) (var_ref lhs) (var_ref rhs))
176
if (rhs->type->is_vector())
177
this->write_mask = (1U << rhs->type->vector_elements) - 1;
178
else if (rhs->type->is_scalar())
179
this->write_mask = 1;
181
this->write_mask = 0;
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)
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;
200
for (unsigned i = num_operands; i < 4; i++) {
201
assert(this->operands[i] == NULL);
204
for (unsigned i = 0; i < num_operands; i++) {
205
assert(this->operands[i] != NULL);
210
ir_expression::ir_expression(int op, ir_rvalue *op0)
211
: ir_rvalue(ir_type_expression)
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;
219
assert(op <= ir_last_unop);
221
assert(num_operands == 1);
222
assert(this->operands[0]);
224
switch (this->operation) {
225
case ir_unop_bit_not:
226
case ir_unop_logic_not:
241
case ir_unop_round_even:
245
case ir_unop_dFdx_coarse:
246
case ir_unop_dFdx_fine:
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:
253
case ir_unop_saturate:
255
this->type = op0->type;
262
case ir_unop_bitcast_f2i:
263
case ir_unop_bit_count:
264
case ir_unop_find_msb:
265
case ir_unop_find_lsb:
266
case ir_unop_subroutine_to_int:
269
this->type = glsl_type::get_instance(GLSL_TYPE_INT,
270
op0->type->vector_elements, 1);
278
case ir_unop_bitcast_i2f:
279
case ir_unop_bitcast_u2f:
282
this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT,
283
op0->type->vector_elements, 1);
289
this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT16,
290
op0->type->vector_elements, 1);
294
this->type = glsl_type::get_instance(GLSL_TYPE_INT16,
295
op0->type->vector_elements, 1);
299
if (op0->type->base_type == GLSL_TYPE_INT) {
300
this->type = glsl_type::get_instance(GLSL_TYPE_INT16,
301
op0->type->vector_elements, 1);
303
assert(op0->type->base_type == GLSL_TYPE_INT16);
304
this->type = glsl_type::get_instance(GLSL_TYPE_INT,
305
op0->type->vector_elements, 1);
310
if (op0->type->base_type == GLSL_TYPE_UINT) {
311
this->type = glsl_type::get_instance(GLSL_TYPE_UINT16,
312
op0->type->vector_elements, 1);
314
assert(op0->type->base_type == GLSL_TYPE_UINT16);
315
this->type = glsl_type::get_instance(GLSL_TYPE_UINT,
316
op0->type->vector_elements, 1);
321
this->type = glsl_type::get_instance(GLSL_TYPE_UINT16,
322
op0->type->vector_elements, 1);
330
this->type = glsl_type::get_instance(GLSL_TYPE_BOOL,
331
op0->type->vector_elements, 1);
339
this->type = glsl_type::get_instance(GLSL_TYPE_DOUBLE,
340
op0->type->vector_elements, 1);
346
case ir_unop_bitcast_f2u:
349
this->type = glsl_type::get_instance(GLSL_TYPE_UINT,
350
op0->type->vector_elements, 1);
358
case ir_unop_u642i64:
359
this->type = glsl_type::get_instance(GLSL_TYPE_INT64,
360
op0->type->vector_elements, 1);
367
case ir_unop_i642u64:
368
this->type = glsl_type::get_instance(GLSL_TYPE_UINT64,
369
op0->type->vector_elements, 1);
372
case ir_unop_unpack_double_2x32:
373
case ir_unop_unpack_uint_2x32:
374
this->type = glsl_type::uvec2_type;
377
case ir_unop_unpack_int_2x32:
378
this->type = glsl_type::ivec2_type;
381
case ir_unop_pack_snorm_2x16:
382
case ir_unop_pack_snorm_4x8:
383
case ir_unop_pack_unorm_2x16:
384
case ir_unop_pack_unorm_4x8:
385
case ir_unop_pack_half_2x16:
386
this->type = glsl_type::uint_type;
389
case ir_unop_pack_double_2x32:
390
this->type = glsl_type::double_type;
393
case ir_unop_pack_int_2x32:
394
this->type = glsl_type::int64_t_type;
397
case ir_unop_pack_uint_2x32:
398
this->type = glsl_type::uint64_t_type;
401
case ir_unop_unpack_snorm_2x16:
402
case ir_unop_unpack_unorm_2x16:
403
case ir_unop_unpack_half_2x16:
404
this->type = glsl_type::vec2_type;
407
case ir_unop_unpack_snorm_4x8:
408
case ir_unop_unpack_unorm_4x8:
409
this->type = glsl_type::vec4_type;
412
case ir_unop_unpack_sampler_2x32:
413
case ir_unop_unpack_image_2x32:
414
this->type = glsl_type::uvec2_type;
417
case ir_unop_pack_sampler_2x32:
418
case ir_unop_pack_image_2x32:
419
this->type = op0->type;
422
case ir_unop_frexp_sig:
423
this->type = op0->type;
425
case ir_unop_frexp_exp:
426
this->type = glsl_type::get_instance(GLSL_TYPE_INT,
427
op0->type->vector_elements, 1);
430
case ir_unop_get_buffer_size:
431
case ir_unop_ssbo_unsized_array_length:
432
case ir_unop_implicitly_sized_array_length:
433
this->type = glsl_type::int_type;
436
case ir_unop_bitcast_i642d:
437
case ir_unop_bitcast_u642d:
438
this->type = glsl_type::get_instance(GLSL_TYPE_DOUBLE,
439
op0->type->vector_elements, 1);
442
case ir_unop_bitcast_d2i64:
443
this->type = glsl_type::get_instance(GLSL_TYPE_INT64,
444
op0->type->vector_elements, 1);
446
case ir_unop_bitcast_d2u64:
447
this->type = glsl_type::get_instance(GLSL_TYPE_UINT64,
448
op0->type->vector_elements, 1);
452
assert(!"not reached: missing automatic type setup for ir_expression");
453
this->type = op0->type;
458
ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1)
459
: ir_rvalue(ir_type_expression)
461
this->operation = ir_expression_operation(op);
462
this->operands[0] = op0;
463
this->operands[1] = op1;
464
this->operands[2] = NULL;
465
this->operands[3] = NULL;
467
assert(op > ir_last_unop);
469
assert(num_operands == 2);
470
for (unsigned i = 0; i < num_operands; i++) {
471
assert(this->operands[i] != NULL);
474
switch (this->operation) {
475
case ir_binop_all_equal:
476
case ir_binop_any_nequal:
477
this->type = glsl_type::bool_type;
489
if (op0->type->is_scalar()) {
490
this->type = op1->type;
491
} else if (op1->type->is_scalar()) {
492
this->type = op0->type;
494
if (this->operation == ir_binop_mul) {
495
this->type = glsl_type::get_mul_type(op0->type, op1->type);
497
assert(op0->type == op1->type);
498
this->type = op0->type;
503
case ir_binop_logic_and:
504
case ir_binop_logic_xor:
505
case ir_binop_logic_or:
506
case ir_binop_bit_and:
507
case ir_binop_bit_xor:
508
case ir_binop_bit_or:
509
assert(!op0->type->is_matrix());
510
assert(!op1->type->is_matrix());
511
if (op0->type->is_scalar()) {
512
this->type = op1->type;
513
} else if (op1->type->is_scalar()) {
514
this->type = op0->type;
516
assert(op0->type->vector_elements == op1->type->vector_elements);
517
this->type = op0->type;
522
case ir_binop_nequal:
523
case ir_binop_gequal:
525
assert(op0->type == op1->type);
526
this->type = glsl_type::get_instance(GLSL_TYPE_BOOL,
527
op0->type->vector_elements, 1);
531
this->type = op0->type->get_base_type();
534
case ir_binop_imul_high:
535
case ir_binop_mul_32x16:
537
case ir_binop_borrow:
538
case ir_binop_lshift:
539
case ir_binop_rshift:
541
case ir_binop_interpolate_at_offset:
542
case ir_binop_interpolate_at_sample:
543
this->type = op0->type;
546
case ir_binop_add_sat:
547
case ir_binop_sub_sat:
549
case ir_binop_avg_round:
550
assert(op0->type == op1->type);
551
this->type = op0->type;
554
case ir_binop_abs_sub: {
555
enum glsl_base_type base;
557
assert(op0->type == op1->type);
559
switch (op0->type->base_type) {
562
base = GLSL_TYPE_UINT;
564
case GLSL_TYPE_UINT8:
566
base = GLSL_TYPE_UINT8;
568
case GLSL_TYPE_UINT16:
569
case GLSL_TYPE_INT16:
570
base = GLSL_TYPE_UINT16;
572
case GLSL_TYPE_UINT64:
573
case GLSL_TYPE_INT64:
574
base = GLSL_TYPE_UINT64;
577
unreachable(!"Invalid base type.");
580
this->type = glsl_type::get_instance(base, op0->type->vector_elements, 1);
584
case ir_binop_vector_extract:
585
this->type = op0->type->get_scalar_type();
589
assert(!"not reached: missing automatic type setup for ir_expression");
590
this->type = glsl_type::float_type;
594
ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1,
596
: ir_rvalue(ir_type_expression)
598
this->operation = ir_expression_operation(op);
599
this->operands[0] = op0;
600
this->operands[1] = op1;
601
this->operands[2] = op2;
602
this->operands[3] = NULL;
604
assert(op > ir_last_binop && op <= ir_last_triop);
606
assert(num_operands == 3);
607
for (unsigned i = 0; i < num_operands; i++) {
608
assert(this->operands[i] != NULL);
611
switch (this->operation) {
614
case ir_triop_bitfield_extract:
615
case ir_triop_vector_insert:
616
this->type = op0->type;
620
this->type = op1->type;
624
assert(!"not reached: missing automatic type setup for ir_expression");
625
this->type = glsl_type::float_type;
630
* This is only here for ir_reader to used for testing purposes. Please use
631
* the precomputed num_operands field if you need the number of operands.
634
ir_expression::get_num_operands(ir_expression_operation op)
636
assert(op <= ir_last_opcode);
638
if (op <= ir_last_unop)
641
if (op <= ir_last_binop)
644
if (op <= ir_last_triop)
647
if (op <= ir_last_quadop)
650
unreachable("Could not calculate number of operands");
653
#include "ir_expression_operation_strings.h"
656
depth_layout_string(ir_depth_layout layout)
659
case ir_depth_layout_none: return "";
660
case ir_depth_layout_any: return "depth_any";
661
case ir_depth_layout_greater: return "depth_greater";
662
case ir_depth_layout_less: return "depth_less";
663
case ir_depth_layout_unchanged: return "depth_unchanged";
671
ir_expression_operation
672
ir_expression::get_operator(const char *str)
674
for (int op = 0; op <= int(ir_last_opcode); op++) {
675
if (strcmp(str, ir_expression_operation_strings[op]) == 0)
676
return (ir_expression_operation) op;
678
return (ir_expression_operation) -1;
682
ir_expression::variable_referenced() const
685
case ir_binop_vector_extract:
686
case ir_triop_vector_insert:
687
/* We get these for things like a[0] where a is a vector type. In these
688
* cases we want variable_referenced() to return the actual vector
689
* variable this is wrapping.
691
return operands[0]->variable_referenced();
693
return ir_rvalue::variable_referenced();
697
ir_constant::ir_constant()
698
: ir_rvalue(ir_type_constant)
700
this->const_elements = NULL;
703
ir_constant::ir_constant(const struct glsl_type *type,
704
const ir_constant_data *data)
705
: ir_rvalue(ir_type_constant)
707
this->const_elements = NULL;
709
assert((type->base_type >= GLSL_TYPE_UINT)
710
&& (type->base_type <= GLSL_TYPE_IMAGE));
713
memcpy(& this->value, data, sizeof(this->value));
716
ir_constant::ir_constant(float16_t f16, unsigned vector_elements)
717
: ir_rvalue(ir_type_constant)
719
this->const_elements = NULL;
720
assert(vector_elements <= 4);
721
this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT16, vector_elements, 1);
722
for (unsigned i = 0; i < vector_elements; i++) {
723
this->value.f16[i] = f16.bits;
725
for (unsigned i = vector_elements; i < 16; i++) {
726
this->value.f[i] = 0;
730
ir_constant::ir_constant(float f, unsigned vector_elements)
731
: ir_rvalue(ir_type_constant)
733
this->const_elements = NULL;
734
assert(vector_elements <= 4);
735
this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT, vector_elements, 1);
736
for (unsigned i = 0; i < vector_elements; i++) {
737
this->value.f[i] = f;
739
for (unsigned i = vector_elements; i < 16; i++) {
740
this->value.f[i] = 0;
744
ir_constant::ir_constant(double d, unsigned vector_elements)
745
: ir_rvalue(ir_type_constant)
747
this->const_elements = NULL;
748
assert(vector_elements <= 4);
749
this->type = glsl_type::get_instance(GLSL_TYPE_DOUBLE, vector_elements, 1);
750
for (unsigned i = 0; i < vector_elements; i++) {
751
this->value.d[i] = d;
753
for (unsigned i = vector_elements; i < 16; i++) {
754
this->value.d[i] = 0.0;
758
ir_constant::ir_constant(int16_t i16, unsigned vector_elements)
759
: ir_rvalue(ir_type_constant)
761
this->const_elements = NULL;
762
assert(vector_elements <= 4);
763
this->type = glsl_type::get_instance(GLSL_TYPE_INT16, vector_elements, 1);
764
for (unsigned i = 0; i < vector_elements; i++) {
765
this->value.i16[i] = i16;
767
for (unsigned i = vector_elements; i < 16; i++) {
768
this->value.i16[i] = 0;
772
ir_constant::ir_constant(uint16_t u16, unsigned vector_elements)
773
: ir_rvalue(ir_type_constant)
775
this->const_elements = NULL;
776
assert(vector_elements <= 4);
777
this->type = glsl_type::get_instance(GLSL_TYPE_UINT16, vector_elements, 1);
778
for (unsigned i = 0; i < vector_elements; i++) {
779
this->value.u16[i] = u16;
781
for (unsigned i = vector_elements; i < 16; i++) {
782
this->value.u16[i] = 0;
786
ir_constant::ir_constant(unsigned int u, unsigned vector_elements)
787
: ir_rvalue(ir_type_constant)
789
this->const_elements = NULL;
790
assert(vector_elements <= 4);
791
this->type = glsl_type::get_instance(GLSL_TYPE_UINT, vector_elements, 1);
792
for (unsigned i = 0; i < vector_elements; i++) {
793
this->value.u[i] = u;
795
for (unsigned i = vector_elements; i < 16; i++) {
796
this->value.u[i] = 0;
800
ir_constant::ir_constant(int integer, unsigned vector_elements)
801
: ir_rvalue(ir_type_constant)
803
this->const_elements = NULL;
804
assert(vector_elements <= 4);
805
this->type = glsl_type::get_instance(GLSL_TYPE_INT, vector_elements, 1);
806
for (unsigned i = 0; i < vector_elements; i++) {
807
this->value.i[i] = integer;
809
for (unsigned i = vector_elements; i < 16; i++) {
810
this->value.i[i] = 0;
814
ir_constant::ir_constant(uint64_t u64, unsigned vector_elements)
815
: ir_rvalue(ir_type_constant)
817
this->const_elements = NULL;
818
assert(vector_elements <= 4);
819
this->type = glsl_type::get_instance(GLSL_TYPE_UINT64, vector_elements, 1);
820
for (unsigned i = 0; i < vector_elements; i++) {
821
this->value.u64[i] = u64;
823
for (unsigned i = vector_elements; i < 16; i++) {
824
this->value.u64[i] = 0;
828
ir_constant::ir_constant(int64_t int64, unsigned vector_elements)
829
: ir_rvalue(ir_type_constant)
831
this->const_elements = NULL;
832
assert(vector_elements <= 4);
833
this->type = glsl_type::get_instance(GLSL_TYPE_INT64, vector_elements, 1);
834
for (unsigned i = 0; i < vector_elements; i++) {
835
this->value.i64[i] = int64;
837
for (unsigned i = vector_elements; i < 16; i++) {
838
this->value.i64[i] = 0;
842
ir_constant::ir_constant(bool b, unsigned vector_elements)
843
: ir_rvalue(ir_type_constant)
845
this->const_elements = NULL;
846
assert(vector_elements <= 4);
847
this->type = glsl_type::get_instance(GLSL_TYPE_BOOL, vector_elements, 1);
848
for (unsigned i = 0; i < vector_elements; i++) {
849
this->value.b[i] = b;
851
for (unsigned i = vector_elements; i < 16; i++) {
852
this->value.b[i] = false;
856
ir_constant::ir_constant(const ir_constant *c, unsigned i)
857
: ir_rvalue(ir_type_constant)
859
this->const_elements = NULL;
860
this->type = c->type->get_base_type();
862
/* Section 5.11 (Out-of-Bounds Accesses) of the GLSL 4.60 spec says:
864
* In the subsections described above for array, vector, matrix and
865
* structure accesses, any out-of-bounds access produced undefined
866
* behavior....Out-of-bounds reads return undefined values, which
867
* include values from other variables of the active program or zero.
869
* GL_KHR_robustness and GL_ARB_robustness encourage us to return zero.
871
if (i >= c->type->vector_elements) {
872
this->value = { { 0 } };
876
switch (this->type->base_type) {
877
case GLSL_TYPE_UINT16: this->value.u16[0] = c->value.u16[i]; break;
878
case GLSL_TYPE_INT16: this->value.i16[0] = c->value.i16[i]; break;
879
case GLSL_TYPE_UINT: this->value.u[0] = c->value.u[i]; break;
880
case GLSL_TYPE_INT: this->value.i[0] = c->value.i[i]; break;
881
case GLSL_TYPE_FLOAT: this->value.f[0] = c->value.f[i]; break;
882
case GLSL_TYPE_FLOAT16: this->value.f16[0] = c->value.f16[i]; break;
883
case GLSL_TYPE_BOOL: this->value.b[0] = c->value.b[i]; break;
884
case GLSL_TYPE_DOUBLE: this->value.d[0] = c->value.d[i]; break;
885
default: assert(!"Should not get here."); break;
889
ir_constant::ir_constant(const struct glsl_type *type, exec_list *value_list)
890
: ir_rvalue(ir_type_constant)
892
this->const_elements = NULL;
895
assert(type->is_scalar() || type->is_vector() || type->is_matrix()
896
|| type->is_struct() || type->is_array());
898
/* If the constant is a record, the types of each of the entries in
899
* value_list must be a 1-for-1 match with the structure components. Each
900
* entry must also be a constant. Just move the nodes from the value_list
901
* to the list in the ir_constant.
903
if (type->is_array() || type->is_struct()) {
904
this->const_elements = ralloc_array(this, ir_constant *, type->length);
906
foreach_in_list(ir_constant, value, value_list) {
907
assert(value->as_constant() != NULL);
909
this->const_elements[i++] = value;
914
for (unsigned i = 0; i < 16; i++) {
915
this->value.u[i] = 0;
918
ir_constant *value = (ir_constant *) (value_list->get_head_raw());
920
/* Constructors with exactly one scalar argument are special for vectors
921
* and matrices. For vectors, the scalar value is replicated to fill all
922
* the components. For matrices, the scalar fills the components of the
923
* diagonal while the rest is filled with 0.
925
if (value->type->is_scalar() && value->next->is_tail_sentinel()) {
926
if (type->is_matrix()) {
927
/* Matrix - fill diagonal (rest is already set to 0) */
928
for (unsigned i = 0; i < type->matrix_columns; i++) {
929
switch (type->base_type) {
930
case GLSL_TYPE_FLOAT:
931
this->value.f[i * type->vector_elements + i] =
934
case GLSL_TYPE_DOUBLE:
935
this->value.d[i * type->vector_elements + i] =
938
case GLSL_TYPE_FLOAT16:
939
this->value.f16[i * type->vector_elements + i] =
943
assert(!"unexpected matrix base type");
947
/* Vector or scalar - fill all components */
948
switch (type->base_type) {
949
case GLSL_TYPE_UINT16:
950
case GLSL_TYPE_INT16:
951
for (unsigned i = 0; i < type->components(); i++)
952
this->value.u16[i] = value->value.u16[0];
956
for (unsigned i = 0; i < type->components(); i++)
957
this->value.u[i] = value->value.u[0];
959
case GLSL_TYPE_FLOAT:
960
for (unsigned i = 0; i < type->components(); i++)
961
this->value.f[i] = value->value.f[0];
963
case GLSL_TYPE_FLOAT16:
964
for (unsigned i = 0; i < type->components(); i++)
965
this->value.f16[i] = value->value.f16[0];
967
case GLSL_TYPE_DOUBLE:
968
for (unsigned i = 0; i < type->components(); i++)
969
this->value.d[i] = value->value.d[0];
971
case GLSL_TYPE_UINT64:
972
case GLSL_TYPE_INT64:
973
for (unsigned i = 0; i < type->components(); i++)
974
this->value.u64[i] = value->value.u64[0];
977
for (unsigned i = 0; i < type->components(); i++)
978
this->value.b[i] = value->value.b[0];
980
case GLSL_TYPE_SAMPLER:
981
case GLSL_TYPE_IMAGE:
982
this->value.u64[0] = value->value.u64[0];
985
assert(!"Should not get here.");
992
if (type->is_matrix() && value->type->is_matrix()) {
993
assert(value->next->is_tail_sentinel());
995
/* From section 5.4.2 of the GLSL 1.20 spec:
996
* "If a matrix is constructed from a matrix, then each component
997
* (column i, row j) in the result that has a corresponding component
998
* (column i, row j) in the argument will be initialized from there."
1000
unsigned cols = MIN2(type->matrix_columns, value->type->matrix_columns);
1001
unsigned rows = MIN2(type->vector_elements, value->type->vector_elements);
1002
for (unsigned i = 0; i < cols; i++) {
1003
for (unsigned j = 0; j < rows; j++) {
1004
const unsigned src = i * value->type->vector_elements + j;
1005
const unsigned dst = i * type->vector_elements + j;
1006
this->value.f[dst] = value->value.f[src];
1010
/* "All other components will be initialized to the identity matrix." */
1011
for (unsigned i = cols; i < type->matrix_columns; i++)
1012
this->value.f[i * type->vector_elements + i] = 1.0;
1017
/* Use each component from each entry in the value_list to initialize one
1018
* component of the constant being constructed.
1022
assert(value->as_constant() != NULL);
1023
assert(!value->is_tail_sentinel());
1025
for (unsigned j = 0; j < value->type->components(); j++) {
1026
switch (type->base_type) {
1027
case GLSL_TYPE_UINT16:
1028
this->value.u16[i] = value->get_uint16_component(j);
1030
case GLSL_TYPE_INT16:
1031
this->value.i16[i] = value->get_int16_component(j);
1033
case GLSL_TYPE_UINT:
1034
this->value.u[i] = value->get_uint_component(j);
1037
this->value.i[i] = value->get_int_component(j);
1039
case GLSL_TYPE_FLOAT:
1040
this->value.f[i] = value->get_float_component(j);
1042
case GLSL_TYPE_FLOAT16:
1043
this->value.f16[i] = value->get_float16_component(j);
1045
case GLSL_TYPE_BOOL:
1046
this->value.b[i] = value->get_bool_component(j);
1048
case GLSL_TYPE_DOUBLE:
1049
this->value.d[i] = value->get_double_component(j);
1051
case GLSL_TYPE_UINT64:
1052
this->value.u64[i] = value->get_uint64_component(j);
1054
case GLSL_TYPE_INT64:
1055
this->value.i64[i] = value->get_int64_component(j);
1058
/* FINISHME: What to do? Exceptions are not the answer.
1064
if (i >= type->components())
1068
if (i >= type->components())
1069
break; /* avoid downcasting a list sentinel */
1070
value = (ir_constant *) value->next;
1075
ir_constant::zero(void *mem_ctx, const glsl_type *type)
1077
assert(type->is_scalar() || type->is_vector() || type->is_matrix()
1078
|| type->is_struct() || type->is_array());
1080
ir_constant *c = new(mem_ctx) ir_constant;
1082
memset(&c->value, 0, sizeof(c->value));
1084
if (type->is_array()) {
1085
c->const_elements = ralloc_array(c, ir_constant *, type->length);
1087
for (unsigned i = 0; i < type->length; i++)
1088
c->const_elements[i] = ir_constant::zero(c, type->fields.array);
1091
if (type->is_struct()) {
1092
c->const_elements = ralloc_array(c, ir_constant *, type->length);
1094
for (unsigned i = 0; i < type->length; i++) {
1095
c->const_elements[i] =
1096
ir_constant::zero(mem_ctx, type->fields.structure[i].type);
1104
ir_constant::get_bool_component(unsigned i) const
1106
switch (this->type->base_type) {
1107
case GLSL_TYPE_UINT16:return this->value.u16[i] != 0;
1108
case GLSL_TYPE_INT16: return this->value.i16[i] != 0;
1109
case GLSL_TYPE_UINT: return this->value.u[i] != 0;
1110
case GLSL_TYPE_INT: return this->value.i[i] != 0;
1111
case GLSL_TYPE_FLOAT: return ((int)this->value.f[i]) != 0;
1112
case GLSL_TYPE_FLOAT16: return ((int)_mesa_half_to_float(this->value.f16[i])) != 0;
1113
case GLSL_TYPE_BOOL: return this->value.b[i];
1114
case GLSL_TYPE_DOUBLE: return this->value.d[i] != 0.0;
1115
case GLSL_TYPE_SAMPLER:
1116
case GLSL_TYPE_IMAGE:
1117
case GLSL_TYPE_UINT64: return this->value.u64[i] != 0;
1118
case GLSL_TYPE_INT64: return this->value.i64[i] != 0;
1119
default: assert(!"Should not get here."); break;
1122
/* Must return something to make the compiler happy. This is clearly an
1129
ir_constant::get_float_component(unsigned i) const
1131
switch (this->type->base_type) {
1132
case GLSL_TYPE_UINT16:return (float) this->value.u16[i];
1133
case GLSL_TYPE_INT16: return (float) this->value.i16[i];
1134
case GLSL_TYPE_UINT: return (float) this->value.u[i];
1135
case GLSL_TYPE_INT: return (float) this->value.i[i];
1136
case GLSL_TYPE_FLOAT: return this->value.f[i];
1137
case GLSL_TYPE_FLOAT16: return _mesa_half_to_float(this->value.f16[i]);
1138
case GLSL_TYPE_BOOL: return this->value.b[i] ? 1.0f : 0.0f;
1139
case GLSL_TYPE_DOUBLE: return (float) this->value.d[i];
1140
case GLSL_TYPE_SAMPLER:
1141
case GLSL_TYPE_IMAGE:
1142
case GLSL_TYPE_UINT64: return (float) this->value.u64[i];
1143
case GLSL_TYPE_INT64: return (float) this->value.i64[i];
1144
default: assert(!"Should not get here."); break;
1147
/* Must return something to make the compiler happy. This is clearly an
1154
ir_constant::get_float16_component(unsigned i) const
1156
if (this->type->base_type == GLSL_TYPE_FLOAT16)
1157
return this->value.f16[i];
1159
return _mesa_float_to_half(get_float_component(i));
1163
ir_constant::get_double_component(unsigned i) const
1165
switch (this->type->base_type) {
1166
case GLSL_TYPE_UINT16:return (double) this->value.u16[i];
1167
case GLSL_TYPE_INT16: return (double) this->value.i16[i];
1168
case GLSL_TYPE_UINT: return (double) this->value.u[i];
1169
case GLSL_TYPE_INT: return (double) this->value.i[i];
1170
case GLSL_TYPE_FLOAT: return (double) this->value.f[i];
1171
case GLSL_TYPE_FLOAT16: return (double) _mesa_half_to_float(this->value.f16[i]);
1172
case GLSL_TYPE_BOOL: return this->value.b[i] ? 1.0 : 0.0;
1173
case GLSL_TYPE_DOUBLE: return this->value.d[i];
1174
case GLSL_TYPE_SAMPLER:
1175
case GLSL_TYPE_IMAGE:
1176
case GLSL_TYPE_UINT64: return (double) this->value.u64[i];
1177
case GLSL_TYPE_INT64: return (double) this->value.i64[i];
1178
default: assert(!"Should not get here."); break;
1181
/* Must return something to make the compiler happy. This is clearly an
1188
ir_constant::get_int16_component(unsigned i) const
1190
switch (this->type->base_type) {
1191
case GLSL_TYPE_UINT16:return this->value.u16[i];
1192
case GLSL_TYPE_INT16: return this->value.i16[i];
1193
case GLSL_TYPE_UINT: return this->value.u[i];
1194
case GLSL_TYPE_INT: return this->value.i[i];
1195
case GLSL_TYPE_FLOAT: return (int16_t) this->value.f[i];
1196
case GLSL_TYPE_FLOAT16: return (int16_t) _mesa_half_to_float(this->value.f16[i]);
1197
case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1198
case GLSL_TYPE_DOUBLE: return (int16_t) this->value.d[i];
1199
case GLSL_TYPE_SAMPLER:
1200
case GLSL_TYPE_IMAGE:
1201
case GLSL_TYPE_UINT64: return (int16_t) this->value.u64[i];
1202
case GLSL_TYPE_INT64: return (int16_t) this->value.i64[i];
1203
default: assert(!"Should not get here."); break;
1206
/* Must return something to make the compiler happy. This is clearly an
1213
ir_constant::get_uint16_component(unsigned i) const
1215
switch (this->type->base_type) {
1216
case GLSL_TYPE_UINT16:return this->value.u16[i];
1217
case GLSL_TYPE_INT16: return this->value.i16[i];
1218
case GLSL_TYPE_UINT: return this->value.u[i];
1219
case GLSL_TYPE_INT: return this->value.i[i];
1220
case GLSL_TYPE_FLOAT: return (uint16_t) this->value.f[i];
1221
case GLSL_TYPE_FLOAT16: return (uint16_t) _mesa_half_to_float(this->value.f16[i]);
1222
case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1223
case GLSL_TYPE_DOUBLE: return (uint16_t) this->value.d[i];
1224
case GLSL_TYPE_SAMPLER:
1225
case GLSL_TYPE_IMAGE:
1226
case GLSL_TYPE_UINT64: return (uint16_t) this->value.u64[i];
1227
case GLSL_TYPE_INT64: return (uint16_t) this->value.i64[i];
1228
default: assert(!"Should not get here."); break;
1231
/* Must return something to make the compiler happy. This is clearly an
1238
ir_constant::get_int_component(unsigned i) const
1240
switch (this->type->base_type) {
1241
case GLSL_TYPE_UINT16:return this->value.u16[i];
1242
case GLSL_TYPE_INT16: return this->value.i16[i];
1243
case GLSL_TYPE_UINT: return this->value.u[i];
1244
case GLSL_TYPE_INT: return this->value.i[i];
1245
case GLSL_TYPE_FLOAT: return (int) this->value.f[i];
1246
case GLSL_TYPE_FLOAT16: return (int) _mesa_half_to_float(this->value.f16[i]);
1247
case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1248
case GLSL_TYPE_DOUBLE: return (int) this->value.d[i];
1249
case GLSL_TYPE_SAMPLER:
1250
case GLSL_TYPE_IMAGE:
1251
case GLSL_TYPE_UINT64: return (int) this->value.u64[i];
1252
case GLSL_TYPE_INT64: return (int) this->value.i64[i];
1253
default: assert(!"Should not get here."); break;
1256
/* Must return something to make the compiler happy. This is clearly an
1263
ir_constant::get_uint_component(unsigned i) const
1265
switch (this->type->base_type) {
1266
case GLSL_TYPE_UINT16:return this->value.u16[i];
1267
case GLSL_TYPE_INT16: return this->value.i16[i];
1268
case GLSL_TYPE_UINT: return this->value.u[i];
1269
case GLSL_TYPE_INT: return this->value.i[i];
1270
case GLSL_TYPE_FLOAT: return (unsigned) this->value.f[i];
1271
case GLSL_TYPE_FLOAT16: return (unsigned) _mesa_half_to_float(this->value.f16[i]);
1272
case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1273
case GLSL_TYPE_DOUBLE: return (unsigned) this->value.d[i];
1274
case GLSL_TYPE_SAMPLER:
1275
case GLSL_TYPE_IMAGE:
1276
case GLSL_TYPE_UINT64: return (unsigned) this->value.u64[i];
1277
case GLSL_TYPE_INT64: return (unsigned) this->value.i64[i];
1278
default: assert(!"Should not get here."); break;
1281
/* Must return something to make the compiler happy. This is clearly an
1288
ir_constant::get_int64_component(unsigned i) const
1290
switch (this->type->base_type) {
1291
case GLSL_TYPE_UINT16:return this->value.u16[i];
1292
case GLSL_TYPE_INT16: return this->value.i16[i];
1293
case GLSL_TYPE_UINT: return this->value.u[i];
1294
case GLSL_TYPE_INT: return this->value.i[i];
1295
case GLSL_TYPE_FLOAT: return (int64_t) this->value.f[i];
1296
case GLSL_TYPE_FLOAT16: return (int64_t) _mesa_half_to_float(this->value.f16[i]);
1297
case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1298
case GLSL_TYPE_DOUBLE: return (int64_t) this->value.d[i];
1299
case GLSL_TYPE_SAMPLER:
1300
case GLSL_TYPE_IMAGE:
1301
case GLSL_TYPE_UINT64: return (int64_t) this->value.u64[i];
1302
case GLSL_TYPE_INT64: return this->value.i64[i];
1303
default: assert(!"Should not get here."); break;
1306
/* Must return something to make the compiler happy. This is clearly an
1313
ir_constant::get_uint64_component(unsigned i) const
1315
switch (this->type->base_type) {
1316
case GLSL_TYPE_UINT16:return this->value.u16[i];
1317
case GLSL_TYPE_INT16: return this->value.i16[i];
1318
case GLSL_TYPE_UINT: return this->value.u[i];
1319
case GLSL_TYPE_INT: return this->value.i[i];
1320
case GLSL_TYPE_FLOAT: return (uint64_t) this->value.f[i];
1321
case GLSL_TYPE_FLOAT16: return (uint64_t) _mesa_half_to_float(this->value.f16[i]);
1322
case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1323
case GLSL_TYPE_DOUBLE: return (uint64_t) this->value.d[i];
1324
case GLSL_TYPE_SAMPLER:
1325
case GLSL_TYPE_IMAGE:
1326
case GLSL_TYPE_UINT64: return this->value.u64[i];
1327
case GLSL_TYPE_INT64: return (uint64_t) this->value.i64[i];
1328
default: assert(!"Should not get here."); break;
1331
/* Must return something to make the compiler happy. This is clearly an
1338
ir_constant::get_array_element(unsigned i) const
1340
assert(this->type->is_array());
1342
/* From page 35 (page 41 of the PDF) of the GLSL 1.20 spec:
1344
* "Behavior is undefined if a shader subscripts an array with an index
1345
* less than 0 or greater than or equal to the size the array was
1348
* Most out-of-bounds accesses are removed before things could get this far.
1349
* There are cases where non-constant array index values can get constant
1354
else if (i >= this->type->length)
1355
i = this->type->length - 1;
1357
return const_elements[i];
1361
ir_constant::get_record_field(int idx)
1363
assert(this->type->is_struct());
1364
assert(idx >= 0 && (unsigned) idx < this->type->length);
1366
return const_elements[idx];
1370
ir_constant::copy_offset(ir_constant *src, int offset)
1372
switch (this->type->base_type) {
1373
case GLSL_TYPE_UINT16:
1374
case GLSL_TYPE_INT16:
1375
case GLSL_TYPE_UINT:
1377
case GLSL_TYPE_FLOAT:
1378
case GLSL_TYPE_FLOAT16:
1379
case GLSL_TYPE_DOUBLE:
1380
case GLSL_TYPE_SAMPLER:
1381
case GLSL_TYPE_IMAGE:
1382
case GLSL_TYPE_UINT64:
1383
case GLSL_TYPE_INT64:
1384
case GLSL_TYPE_BOOL: {
1385
unsigned int size = src->type->components();
1386
assert (size <= this->type->components() - offset);
1387
for (unsigned int i=0; i<size; i++) {
1388
switch (this->type->base_type) {
1389
case GLSL_TYPE_UINT16:
1390
value.u16[i+offset] = src->get_uint16_component(i);
1392
case GLSL_TYPE_INT16:
1393
value.i16[i+offset] = src->get_int16_component(i);
1395
case GLSL_TYPE_UINT:
1396
value.u[i+offset] = src->get_uint_component(i);
1399
value.i[i+offset] = src->get_int_component(i);
1401
case GLSL_TYPE_FLOAT:
1402
value.f[i+offset] = src->get_float_component(i);
1404
case GLSL_TYPE_FLOAT16:
1405
value.f16[i+offset] = src->get_float16_component(i);
1407
case GLSL_TYPE_BOOL:
1408
value.b[i+offset] = src->get_bool_component(i);
1410
case GLSL_TYPE_DOUBLE:
1411
value.d[i+offset] = src->get_double_component(i);
1413
case GLSL_TYPE_SAMPLER:
1414
case GLSL_TYPE_IMAGE:
1415
case GLSL_TYPE_UINT64:
1416
value.u64[i+offset] = src->get_uint64_component(i);
1418
case GLSL_TYPE_INT64:
1419
value.i64[i+offset] = src->get_int64_component(i);
1421
default: // Shut up the compiler
1428
case GLSL_TYPE_STRUCT:
1429
case GLSL_TYPE_ARRAY: {
1430
assert (src->type == this->type);
1431
for (unsigned i = 0; i < this->type->length; i++) {
1432
this->const_elements[i] = src->const_elements[i]->clone(this, NULL);
1438
assert(!"Should not get here.");
1444
ir_constant::copy_masked_offset(ir_constant *src, int offset, unsigned int mask)
1446
assert (!type->is_array() && !type->is_struct());
1448
if (!type->is_vector() && !type->is_matrix()) {
1454
for (int i=0; i<4; i++) {
1455
if (mask & (1 << i)) {
1456
switch (this->type->base_type) {
1457
case GLSL_TYPE_UINT16:
1458
value.u16[i+offset] = src->get_uint16_component(id++);
1460
case GLSL_TYPE_INT16:
1461
value.i16[i+offset] = src->get_int16_component(id++);
1463
case GLSL_TYPE_UINT:
1464
value.u[i+offset] = src->get_uint_component(id++);
1467
value.i[i+offset] = src->get_int_component(id++);
1469
case GLSL_TYPE_FLOAT:
1470
value.f[i+offset] = src->get_float_component(id++);
1472
case GLSL_TYPE_FLOAT16:
1473
value.f16[i+offset] = src->get_float16_component(id++);
1475
case GLSL_TYPE_BOOL:
1476
value.b[i+offset] = src->get_bool_component(id++);
1478
case GLSL_TYPE_DOUBLE:
1479
value.d[i+offset] = src->get_double_component(id++);
1481
case GLSL_TYPE_SAMPLER:
1482
case GLSL_TYPE_IMAGE:
1483
case GLSL_TYPE_UINT64:
1484
value.u64[i+offset] = src->get_uint64_component(id++);
1486
case GLSL_TYPE_INT64:
1487
value.i64[i+offset] = src->get_int64_component(id++);
1490
assert(!"Should not get here.");
1498
ir_constant::has_value(const ir_constant *c) const
1500
if (this->type != c->type)
1503
if (this->type->is_array() || this->type->is_struct()) {
1504
for (unsigned i = 0; i < this->type->length; i++) {
1505
if (!this->const_elements[i]->has_value(c->const_elements[i]))
1511
for (unsigned i = 0; i < this->type->components(); i++) {
1512
switch (this->type->base_type) {
1513
case GLSL_TYPE_UINT16:
1514
if (this->value.u16[i] != c->value.u16[i])
1517
case GLSL_TYPE_INT16:
1518
if (this->value.i16[i] != c->value.i16[i])
1521
case GLSL_TYPE_UINT:
1522
if (this->value.u[i] != c->value.u[i])
1526
if (this->value.i[i] != c->value.i[i])
1529
case GLSL_TYPE_FLOAT:
1530
if (this->value.f[i] != c->value.f[i])
1533
case GLSL_TYPE_FLOAT16:
1534
/* Convert to float to make sure NaN and ±0.0 compares correctly */
1535
if (_mesa_half_to_float(this->value.f16[i]) !=
1536
_mesa_half_to_float(c->value.f16[i]))
1539
case GLSL_TYPE_BOOL:
1540
if (this->value.b[i] != c->value.b[i])
1543
case GLSL_TYPE_DOUBLE:
1544
if (this->value.d[i] != c->value.d[i])
1547
case GLSL_TYPE_SAMPLER:
1548
case GLSL_TYPE_IMAGE:
1549
case GLSL_TYPE_UINT64:
1550
if (this->value.u64[i] != c->value.u64[i])
1553
case GLSL_TYPE_INT64:
1554
if (this->value.i64[i] != c->value.i64[i])
1558
assert(!"Should not get here.");
1567
ir_constant::is_value(float f, int i) const
1569
if (!this->type->is_scalar() && !this->type->is_vector())
1572
/* Only accept boolean values for 0/1. */
1573
if (int(bool(i)) != i && this->type->is_boolean())
1576
for (unsigned c = 0; c < this->type->vector_elements; c++) {
1577
switch (this->type->base_type) {
1578
case GLSL_TYPE_FLOAT:
1579
if (this->value.f[c] != f)
1582
case GLSL_TYPE_FLOAT16:
1583
if (_mesa_half_to_float(this->value.f16[c]) != f)
1586
case GLSL_TYPE_INT16:
1587
if (this->value.i16[c] != int16_t(i))
1590
case GLSL_TYPE_UINT16:
1591
if (this->value.u16[c] != uint16_t(i))
1595
if (this->value.i[c] != i)
1598
case GLSL_TYPE_UINT:
1599
if (this->value.u[c] != unsigned(i))
1602
case GLSL_TYPE_BOOL:
1603
if (this->value.b[c] != bool(i))
1606
case GLSL_TYPE_DOUBLE:
1607
if (this->value.d[c] != double(f))
1610
case GLSL_TYPE_SAMPLER:
1611
case GLSL_TYPE_IMAGE:
1612
case GLSL_TYPE_UINT64:
1613
if (this->value.u64[c] != uint64_t(i))
1616
case GLSL_TYPE_INT64:
1617
if (this->value.i64[c] != i)
1621
/* The only other base types are structures, arrays, and samplers.
1622
* Samplers cannot be constants, and the others should have been
1623
* filtered out above.
1625
assert(!"Should not get here.");
1634
ir_constant::is_zero() const
1636
return is_value(0.0, 0);
1640
ir_constant::is_one() const
1642
return is_value(1.0, 1);
1646
ir_constant::is_negative_one() const
1648
return is_value(-1.0, -1);
1652
ir_constant::is_uint16_constant() const
1654
if (!type->is_integer_32())
1657
return value.u[0] < (1 << 16);
1661
: ir_instruction(ir_type_loop)
1666
ir_dereference_variable::ir_dereference_variable(ir_variable *var)
1667
: ir_dereference(ir_type_dereference_variable)
1669
assert(var != NULL);
1672
this->type = var->type;
1676
ir_dereference_array::ir_dereference_array(ir_rvalue *value,
1677
ir_rvalue *array_index)
1678
: ir_dereference(ir_type_dereference_array)
1680
this->array_index = array_index;
1681
this->set_array(value);
1685
ir_dereference_array::ir_dereference_array(ir_variable *var,
1686
ir_rvalue *array_index)
1687
: ir_dereference(ir_type_dereference_array)
1689
void *ctx = ralloc_parent(var);
1691
this->array_index = array_index;
1692
this->set_array(new(ctx) ir_dereference_variable(var));
1697
ir_dereference_array::set_array(ir_rvalue *value)
1699
assert(value != NULL);
1701
this->array = value;
1703
const glsl_type *const vt = this->array->type;
1705
if (vt->is_array()) {
1706
type = vt->fields.array;
1707
} else if (vt->is_matrix()) {
1708
type = vt->column_type();
1709
} else if (vt->is_vector()) {
1710
type = vt->get_base_type();
1715
ir_dereference_record::ir_dereference_record(ir_rvalue *value,
1717
: ir_dereference(ir_type_dereference_record)
1719
assert(value != NULL);
1721
this->record = value;
1722
this->type = this->record->type->field_type(field);
1723
this->field_idx = this->record->type->field_index(field);
1727
ir_dereference_record::ir_dereference_record(ir_variable *var,
1729
: ir_dereference(ir_type_dereference_record)
1731
void *ctx = ralloc_parent(var);
1733
this->record = new(ctx) ir_dereference_variable(var);
1734
this->type = this->record->type->field_type(field);
1735
this->field_idx = this->record->type->field_index(field);
1739
ir_dereference::is_lvalue(const struct _mesa_glsl_parse_state *state) const
1741
ir_variable *var = this->variable_referenced();
1743
/* Every l-value dereference chain eventually ends in a variable.
1745
if ((var == NULL) || var->data.read_only)
1748
/* From section 4.1.7 of the ARB_bindless_texture spec:
1750
* "Samplers can be used as l-values, so can be assigned into and used as
1751
* "out" and "inout" function parameters."
1753
* From section 4.1.X of the ARB_bindless_texture spec:
1755
* "Images can be used as l-values, so can be assigned into and used as
1756
* "out" and "inout" function parameters."
1758
if ((!state || state->has_bindless()) &&
1759
(this->type->contains_sampler() || this->type->contains_image()))
1762
/* From section 4.1.7 of the GLSL 4.40 spec:
1764
* "Opaque variables cannot be treated as l-values; hence cannot
1765
* be used as out or inout function parameters, nor can they be
1768
if (this->type->contains_opaque())
1775
static const char * const tex_opcode_strs[] = { "tex", "txb", "txl", "txd", "txf", "txf_ms", "txs", "lod", "tg4", "query_levels", "texture_samples", "samples_identical" };
1777
const char *ir_texture::opcode_string()
1779
assert((unsigned int) op < ARRAY_SIZE(tex_opcode_strs));
1780
return tex_opcode_strs[op];
1784
ir_texture::get_opcode(const char *str)
1786
const int count = sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]);
1787
for (int op = 0; op < count; op++) {
1788
if (strcmp(str, tex_opcode_strs[op]) == 0)
1789
return (ir_texture_opcode) op;
1791
return (ir_texture_opcode) -1;
1796
ir_texture::set_sampler(ir_dereference *sampler, const glsl_type *type)
1798
assert(sampler != NULL);
1799
assert(type != NULL);
1800
this->sampler = sampler;
1802
if (this->is_sparse) {
1803
/* code holds residency info */
1804
glsl_struct_field fields[2] = {
1805
glsl_struct_field(glsl_type::int_type, "code"),
1806
glsl_struct_field(type, "texel"),
1808
this->type = glsl_type::get_struct_instance(fields, 2, "struct");
1812
if (this->op == ir_txs || this->op == ir_query_levels ||
1813
this->op == ir_texture_samples) {
1814
assert(type->base_type == GLSL_TYPE_INT);
1815
} else if (this->op == ir_lod) {
1816
assert(type->vector_elements == 2);
1817
assert(type->is_float());
1818
} else if (this->op == ir_samples_identical) {
1819
assert(type == glsl_type::bool_type);
1820
assert(sampler->type->is_sampler());
1821
assert(sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS);
1823
assert(sampler->type->sampled_type == (int) type->base_type);
1824
if (sampler->type->sampler_shadow)
1825
assert(type->vector_elements == 4 || type->vector_elements == 1);
1827
assert(type->vector_elements == 4);
1833
ir_swizzle::init_mask(const unsigned *comp, unsigned count)
1835
assert((count >= 1) && (count <= 4));
1837
memset(&this->mask, 0, sizeof(this->mask));
1838
this->mask.num_components = count;
1840
unsigned dup_mask = 0;
1843
assert(comp[3] <= 3);
1844
dup_mask |= (1U << comp[3])
1845
& ((1U << comp[0]) | (1U << comp[1]) | (1U << comp[2]));
1846
this->mask.w = comp[3];
1849
assert(comp[2] <= 3);
1850
dup_mask |= (1U << comp[2])
1851
& ((1U << comp[0]) | (1U << comp[1]));
1852
this->mask.z = comp[2];
1855
assert(comp[1] <= 3);
1856
dup_mask |= (1U << comp[1])
1857
& ((1U << comp[0]));
1858
this->mask.y = comp[1];
1861
assert(comp[0] <= 3);
1862
this->mask.x = comp[0];
1865
this->mask.has_duplicates = dup_mask != 0;
1867
/* Based on the number of elements in the swizzle and the base type
1868
* (i.e., float, int, unsigned, or bool) of the vector being swizzled,
1869
* generate the type of the resulting value.
1871
type = glsl_type::get_instance(val->type->base_type, mask.num_components, 1);
1874
ir_swizzle::ir_swizzle(ir_rvalue *val, unsigned x, unsigned y, unsigned z,
1875
unsigned w, unsigned count)
1876
: ir_rvalue(ir_type_swizzle), val(val)
1878
const unsigned components[4] = { x, y, z, w };
1879
this->init_mask(components, count);
1882
ir_swizzle::ir_swizzle(ir_rvalue *val, const unsigned *comp,
1884
: ir_rvalue(ir_type_swizzle), val(val)
1886
this->init_mask(comp, count);
1889
ir_swizzle::ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask)
1890
: ir_rvalue(ir_type_swizzle), val(val), mask(mask)
1892
this->type = glsl_type::get_instance(val->type->base_type,
1893
mask.num_components, 1);
1902
ir_swizzle::create(ir_rvalue *val, const char *str, unsigned vector_length)
1904
void *ctx = ralloc_parent(val);
1906
/* For each possible swizzle character, this table encodes the value in
1907
* \c idx_map that represents the 0th element of the vector. For invalid
1908
* swizzle characters (e.g., 'k'), a special value is used that will allow
1909
* detection of errors.
1911
static const unsigned char base_idx[26] = {
1912
/* a b c d e f g h i j k l m */
1913
R, R, I, I, I, I, R, I, I, I, I, I, I,
1914
/* n o p q r s t u v w x y z */
1915
I, I, S, S, R, S, S, I, I, X, X, X, X
1918
/* Each valid swizzle character has an entry in the previous table. This
1919
* table encodes the base index encoded in the previous table plus the actual
1920
* index of the swizzle character. When processing swizzles, the first
1921
* character in the string is indexed in the previous table. Each character
1922
* in the string is indexed in this table, and the value found there has the
1923
* value form the first table subtracted. The result must be on the range
1926
* For example, the string "wzyx" will get X from the first table. Each of
1927
* the charcaters will get X+3, X+2, X+1, and X+0 from this table. After
1928
* subtraction, the swizzle values are { 3, 2, 1, 0 }.
1930
* The string "wzrg" will get X from the first table. Each of the characters
1931
* will get X+3, X+2, R+0, and R+1 from this table. After subtraction, the
1932
* swizzle values are { 3, 2, 4, 5 }. Since 4 and 5 are outside the range
1933
* [0,3], the error is detected.
1935
static const unsigned char idx_map[26] = {
1936
/* a b c d e f g h i j k l m */
1937
R+3, R+2, 0, 0, 0, 0, R+1, 0, 0, 0, 0, 0, 0,
1938
/* n o p q r s t u v w x y z */
1939
0, 0, S+2, S+3, R+0, S+0, S+1, 0, 0, X+3, X+0, X+1, X+2
1942
int swiz_idx[4] = { 0, 0, 0, 0 };
1946
/* Validate the first character in the swizzle string and look up the base
1947
* index value as described above.
1949
if ((str[0] < 'a') || (str[0] > 'z'))
1952
const unsigned base = base_idx[str[0] - 'a'];
1955
for (i = 0; (i < 4) && (str[i] != '\0'); i++) {
1956
/* Validate the next character, and, as described above, convert it to a
1959
if ((str[i] < 'a') || (str[i] > 'z'))
1962
swiz_idx[i] = idx_map[str[i] - 'a'] - base;
1963
if ((swiz_idx[i] < 0) || (swiz_idx[i] >= (int) vector_length))
1970
return new(ctx) ir_swizzle(val, swiz_idx[0], swiz_idx[1], swiz_idx[2],
1980
ir_swizzle::variable_referenced() const
1982
return this->val->variable_referenced();
1986
bool ir_variable::temporaries_allocate_names = false;
1988
const char ir_variable::tmp_name[] = "compiler_temp";
1990
ir_variable::ir_variable(const struct glsl_type *type, const char *name,
1991
ir_variable_mode mode)
1992
: ir_instruction(ir_type_variable)
1996
if (mode == ir_var_temporary && !ir_variable::temporaries_allocate_names)
1999
/* The ir_variable clone method may call this constructor with name set to
2003
|| mode == ir_var_temporary
2004
|| mode == ir_var_function_in
2005
|| mode == ir_var_function_out
2006
|| mode == ir_var_function_inout);
2007
assert(name != ir_variable::tmp_name
2008
|| mode == ir_var_temporary);
2009
if (mode == ir_var_temporary
2010
&& (name == NULL || name == ir_variable::tmp_name)) {
2011
this->name = ir_variable::tmp_name;
2012
} else if (name == NULL ||
2013
strlen(name) < ARRAY_SIZE(this->name_storage)) {
2014
strcpy(this->name_storage, name ? name : "");
2015
this->name = this->name_storage;
2017
this->name = ralloc_strdup(this, name);
2020
this->u.max_ifc_array_access = NULL;
2022
this->data.explicit_location = false;
2023
this->data.explicit_index = false;
2024
this->data.explicit_binding = false;
2025
this->data.explicit_component = false;
2026
this->data.has_initializer = false;
2027
this->data.is_implicit_initializer = false;
2028
this->data.is_xfb = false;
2029
this->data.is_xfb_only = false;
2030
this->data.explicit_xfb_buffer = false;
2031
this->data.explicit_xfb_offset = false;
2032
this->data.explicit_xfb_stride = false;
2033
this->data.location = -1;
2034
this->data.location_frac = 0;
2035
this->data.matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED;
2036
this->data.from_named_ifc_block = false;
2037
this->data.must_be_shader_input = false;
2038
this->data.index = 0;
2039
this->data.binding = 0;
2040
this->data.warn_extension_index = 0;
2041
this->constant_value = NULL;
2042
this->constant_initializer = NULL;
2043
this->data.depth_layout = ir_depth_layout_none;
2044
this->data.used = false;
2045
this->data.assigned = false;
2046
this->data.always_active_io = false;
2047
this->data.read_only = false;
2048
this->data.centroid = false;
2049
this->data.sample = false;
2050
this->data.patch = false;
2051
this->data.explicit_invariant = false;
2052
this->data.invariant = false;
2053
this->data.precise = false;
2054
this->data.how_declared = ir_var_declared_normally;
2055
this->data.mode = mode;
2056
this->data.interpolation = INTERP_MODE_NONE;
2057
this->data.max_array_access = -1;
2058
this->data.offset = 0;
2059
this->data.precision = GLSL_PRECISION_NONE;
2060
this->data.memory_read_only = false;
2061
this->data.memory_write_only = false;
2062
this->data.memory_coherent = false;
2063
this->data.memory_volatile = false;
2064
this->data.memory_restrict = false;
2065
this->data.from_ssbo_unsized_array = false;
2066
this->data.implicit_sized_array = false;
2067
this->data.fb_fetch_output = false;
2068
this->data.bindless = false;
2069
this->data.bound = false;
2070
this->data.image_format = PIPE_FORMAT_NONE;
2071
this->data._num_state_slots = 0;
2072
this->data.param_index = 0;
2073
this->data.stream = 0;
2074
this->data.xfb_buffer = -1;
2075
this->data.xfb_stride = -1;
2076
this->data.implicit_conversion_prohibited = false;
2078
this->interface_type = NULL;
2081
if (type->is_interface())
2082
this->init_interface_type(type);
2083
else if (type->without_array()->is_interface())
2084
this->init_interface_type(type->without_array());
2090
interpolation_string(unsigned interpolation)
2092
switch (interpolation) {
2093
case INTERP_MODE_NONE: return "no";
2094
case INTERP_MODE_SMOOTH: return "smooth";
2095
case INTERP_MODE_FLAT: return "flat";
2096
case INTERP_MODE_NOPERSPECTIVE: return "noperspective";
2099
assert(!"Should not get here.");
2103
const char *const ir_variable::warn_extension_table[] = {
2105
"GL_ARB_shader_stencil_export",
2106
"GL_AMD_shader_stencil_export",
2110
ir_variable::enable_extension_warning(const char *extension)
2112
for (unsigned i = 0; i < ARRAY_SIZE(warn_extension_table); i++) {
2113
if (strcmp(warn_extension_table[i], extension) == 0) {
2114
this->data.warn_extension_index = i;
2119
assert(!"Should not get here.");
2120
this->data.warn_extension_index = 0;
2124
ir_variable::get_extension_warning() const
2126
return this->data.warn_extension_index == 0
2127
? NULL : warn_extension_table[this->data.warn_extension_index];
2130
ir_function_signature::ir_function_signature(const glsl_type *return_type,
2131
builtin_available_predicate b)
2132
: ir_instruction(ir_type_function_signature),
2133
return_type(return_type), is_defined(false),
2134
return_precision(GLSL_PRECISION_NONE),
2135
intrinsic_id(ir_intrinsic_invalid), builtin_avail(b), _function(NULL)
2137
this->origin = NULL;
2142
ir_function_signature::is_builtin() const
2144
return builtin_avail != NULL;
2149
ir_function_signature::is_builtin_available(const _mesa_glsl_parse_state *state) const
2151
/* We can't call the predicate without a state pointer, so just say that
2152
* the signature is available. At compile time, we need the filtering,
2153
* but also receive a valid state pointer. At link time, we're resolving
2154
* imported built-in prototypes to their definitions, which will always
2155
* be an exact match. So we can skip the filtering.
2160
assert(builtin_avail != NULL);
2161
return builtin_avail(state);
2166
modes_match(unsigned a, unsigned b)
2171
/* Accept "in" vs. "const in" */
2172
if ((a == ir_var_const_in && b == ir_var_function_in) ||
2173
(b == ir_var_const_in && a == ir_var_function_in))
2181
ir_function_signature::qualifiers_match(exec_list *params)
2183
/* check that the qualifiers match. */
2184
foreach_two_lists(a_node, &this->parameters, b_node, params) {
2185
ir_variable *a = (ir_variable *) a_node;
2186
ir_variable *b = (ir_variable *) b_node;
2188
if (a->data.read_only != b->data.read_only ||
2189
!modes_match(a->data.mode, b->data.mode) ||
2190
a->data.interpolation != b->data.interpolation ||
2191
a->data.centroid != b->data.centroid ||
2192
a->data.sample != b->data.sample ||
2193
a->data.patch != b->data.patch ||
2194
a->data.memory_read_only != b->data.memory_read_only ||
2195
a->data.memory_write_only != b->data.memory_write_only ||
2196
a->data.memory_coherent != b->data.memory_coherent ||
2197
a->data.memory_volatile != b->data.memory_volatile ||
2198
a->data.memory_restrict != b->data.memory_restrict) {
2200
/* parameter a's qualifiers don't match */
2209
ir_function_signature::replace_parameters(exec_list *new_params)
2211
/* Destroy all of the previous parameter information. If the previous
2212
* parameter information comes from the function prototype, it may either
2213
* specify incorrect parameter names or not have names at all.
2215
new_params->move_nodes_to(¶meters);
2219
ir_function::ir_function(const char *name)
2220
: ir_instruction(ir_type_function)
2222
this->subroutine_index = -1;
2223
this->name = ralloc_strdup(this, name);
2228
ir_function::has_user_signature()
2230
foreach_in_list(ir_function_signature, sig, &this->signatures) {
2231
if (!sig->is_builtin())
2239
ir_rvalue::error_value(void *mem_ctx)
2241
ir_rvalue *v = new(mem_ctx) ir_rvalue(ir_type_unset);
2243
v->type = glsl_type::error_type;
2249
visit_exec_list(exec_list *list, ir_visitor *visitor)
2251
foreach_in_list_safe(ir_instruction, node, list) {
2252
node->accept(visitor);
2258
steal_memory(ir_instruction *ir, void *new_ctx)
2260
ir_variable *var = ir->as_variable();
2261
ir_function *fn = ir->as_function();
2262
ir_constant *constant = ir->as_constant();
2263
if (var != NULL && var->constant_value != NULL)
2264
steal_memory(var->constant_value, ir);
2266
if (var != NULL && var->constant_initializer != NULL)
2267
steal_memory(var->constant_initializer, ir);
2269
if (fn != NULL && fn->subroutine_types)
2270
ralloc_steal(new_ctx, fn->subroutine_types);
2272
/* The components of aggregate constants are not visited by the normal
2273
* visitor, so steal their values by hand.
2275
if (constant != NULL &&
2276
(constant->type->is_array() || constant->type->is_struct())) {
2277
for (unsigned int i = 0; i < constant->type->length; i++) {
2278
steal_memory(constant->const_elements[i], ir);
2282
ralloc_steal(new_ctx, ir);
2287
reparent_ir(exec_list *list, void *mem_ctx)
2289
foreach_in_list(ir_instruction, node, list) {
2290
visit_tree(node, steal_memory, mem_ctx);
2296
try_min_one(ir_rvalue *ir)
2298
ir_expression *expr = ir->as_expression();
2300
if (!expr || expr->operation != ir_binop_min)
2303
if (expr->operands[0]->is_one())
2304
return expr->operands[1];
2306
if (expr->operands[1]->is_one())
2307
return expr->operands[0];
2313
try_max_zero(ir_rvalue *ir)
2315
ir_expression *expr = ir->as_expression();
2317
if (!expr || expr->operation != ir_binop_max)
2320
if (expr->operands[0]->is_zero())
2321
return expr->operands[1];
2323
if (expr->operands[1]->is_zero())
2324
return expr->operands[0];
2330
ir_rvalue::as_rvalue_to_saturate()
2332
ir_expression *expr = this->as_expression();
2337
ir_rvalue *max_zero = try_max_zero(expr);
2339
return try_min_one(max_zero);
2341
ir_rvalue *min_one = try_min_one(expr);
2343
return try_max_zero(min_one);
2352
vertices_per_prim(GLenum prim)
2361
case GL_LINES_ADJACENCY:
2363
case GL_TRIANGLES_ADJACENCY:
2366
assert(!"Bad primitive");
2372
* Generate a string describing the mode of a variable
2375
mode_string(const ir_variable *var)
2377
switch (var->data.mode) {
2379
return (var->data.read_only) ? "global constant" : "global variable";
2381
case ir_var_uniform:
2384
case ir_var_shader_storage:
2387
case ir_var_shader_in:
2388
return "shader input";
2390
case ir_var_shader_out:
2391
return "shader output";
2393
case ir_var_function_in:
2394
case ir_var_const_in:
2395
return "function input";
2397
case ir_var_function_out:
2398
return "function output";
2400
case ir_var_function_inout:
2401
return "function inout";
2403
case ir_var_system_value:
2404
return "shader input";
2406
case ir_var_temporary:
2407
return "compiler temporary";
2409
case ir_var_mode_count:
2413
assert(!"Should not get here.");
2414
return "invalid variable";