Use ir_dereference::set_swizzle
[mesa.git] / ir.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 #include <string.h>
24 #include "main/imports.h"
25 #include "main/simple_list.h"
26 #include "ir.h"
27 #include "glsl_types.h"
28
29 ir_assignment::ir_assignment(ir_instruction *lhs, ir_instruction *rhs,
30 ir_expression *condition)
31 : ir_instruction(ir_op_assign)
32 {
33 this->lhs = (ir_dereference *) lhs;
34 this->rhs = rhs;
35 this->condition = condition;
36 }
37
38
39 ir_expression::ir_expression(int op, const struct glsl_type *type,
40 ir_instruction *op0, ir_instruction *op1)
41 : ir_instruction(ir_op_expression)
42 {
43 this->type = type;
44 this->operation = ir_expression_operation(op);
45 this->operands[0] = op0;
46 this->operands[1] = op1;
47 }
48
49
50 ir_label::ir_label(const char *label)
51 : ir_instruction(ir_op_label), label(label)
52 {
53 /* empty */
54 }
55
56
57 ir_constant::ir_constant(const struct glsl_type *type, const void *data)
58 : ir_instruction(ir_op_constant)
59 {
60 const unsigned elements =
61 ((type->vector_elements == 0) ? 1 : type->vector_elements)
62 * ((type->matrix_rows == 0) ? 1 : type->matrix_rows);
63 unsigned size = 0;
64
65 this->type = type;
66 switch (type->base_type) {
67 case GLSL_TYPE_UINT: size = sizeof(this->value.u[0]); break;
68 case GLSL_TYPE_INT: size = sizeof(this->value.i[0]); break;
69 case GLSL_TYPE_FLOAT: size = sizeof(this->value.f[0]); break;
70 case GLSL_TYPE_BOOL: size = sizeof(this->value.b[0]); break;
71 default:
72 /* FINISHME: What to do? Exceptions are not the answer.
73 */
74 break;
75 }
76
77 memcpy(& this->value, data, size * elements);
78 }
79
80
81 ir_dereference::ir_dereference(ir_instruction *var)
82 : ir_instruction(ir_op_dereference)
83 {
84 this->mode = ir_reference_variable;
85 this->var = var;
86 this->type = (var != NULL) ? var->type : glsl_error_type;
87 }
88
89
90 void
91 ir_dereference::set_swizzle(unsigned x, unsigned y, unsigned z, unsigned w,
92 unsigned count)
93 {
94 assert((count >= 1) && (count <= 4));
95
96 const unsigned dup_mask = 0
97 | ((count > 1) ? ((1U << y) & ((1U << x) )) : 0)
98 | ((count > 2) ? ((1U << z) & ((1U << x) | (1U << y) )) : 0)
99 | ((count > 3) ? ((1U << w) & ((1U << x) | (1U << y) | (1U << z))) : 0);
100
101 assert(x <= 3);
102 assert(y <= 3);
103 assert(z <= 3);
104 assert(w <= 3);
105
106 selector.swizzle.x = x;
107 selector.swizzle.y = y;
108 selector.swizzle.z = z;
109 selector.swizzle.w = w;
110 selector.swizzle.num_components = count;
111 selector.swizzle.has_duplicates = dup_mask != 0;
112 }
113
114
115
116 ir_variable::ir_variable(const struct glsl_type *type, const char *name)
117 : ir_instruction(ir_op_var_decl), read_only(false), centroid(false),
118 invariant(false), mode(ir_var_auto), interpolation(ir_var_smooth)
119 {
120 this->type = type;
121 this->name = name;
122 }
123
124
125 ir_function_signature::ir_function_signature(const glsl_type *return_type)
126 : ir_instruction(ir_op_func_sig), return_type(return_type), definition(NULL)
127 {
128 /* empty */
129 }
130
131
132 ir_function::ir_function(const char *name)
133 : ir_instruction(ir_op_func), name(name)
134 {
135 /* empty */
136 }
137
138
139 ir_call *
140 ir_call::get_error_instruction()
141 {
142 ir_call *call = new ir_call;
143
144 call->type = glsl_error_type;
145 return call;
146 }