c4c7584bcfab72d35adc74dce3267fb44c001746
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.
24 #include "main/imports.h"
25 #include "main/simple_list.h"
27 #include "glsl_types.h"
29 ir_assignment::ir_assignment(ir_rvalue
*lhs
, ir_rvalue
*rhs
,
35 this->condition
= condition
;
39 ir_expression::ir_expression(int op
, const struct glsl_type
*type
,
40 ir_rvalue
*op0
, ir_rvalue
*op1
)
44 this->operation
= ir_expression_operation(op
);
45 this->operands
[0] = op0
;
46 this->operands
[1] = op1
;
50 ir_label::ir_label(const char *label
)
51 : ir_instruction(), label(label
)
57 ir_constant::ir_constant(const struct glsl_type
*type
, const void *data
)
60 const unsigned elements
=
61 ((type
->vector_elements
== 0) ? 1 : type
->vector_elements
)
62 * ((type
->matrix_columns
== 0) ? 1 : type
->matrix_columns
);
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;
72 /* FINISHME: What to do? Exceptions are not the answer.
77 memcpy(& this->value
, data
, size
* elements
);
81 ir_dereference::ir_dereference(ir_instruction
*var
)
84 this->mode
= ir_reference_variable
;
86 this->type
= (var
!= NULL
) ? var
->type
: glsl_type::error_type
;
90 ir_dereference::ir_dereference(ir_instruction
*var
,
91 ir_rvalue
*array_index
)
92 : ir_rvalue(), mode(ir_reference_array
),
95 this->type
= (var
!= NULL
) ? var
->type
: glsl_type::error_type
;
96 this->selector
.array_index
= array_index
;
100 ir_swizzle::ir_swizzle(ir_rvalue
*val
, unsigned x
, unsigned y
, unsigned z
,
101 unsigned w
, unsigned count
)
104 assert((count
>= 1) && (count
<= 4));
106 const unsigned dup_mask
= 0
107 | ((count
> 1) ? ((1U << y
) & ((1U << x
) )) : 0)
108 | ((count
> 2) ? ((1U << z
) & ((1U << x
) | (1U << y
) )) : 0)
109 | ((count
> 3) ? ((1U << w
) & ((1U << x
) | (1U << y
) | (1U << z
))) : 0);
120 mask
.num_components
= count
;
121 mask
.has_duplicates
= dup_mask
!= 0;
123 /* Based on the number of elements in the swizzle and the base type
124 * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
125 * generate the type of the resulting value.
127 type
= glsl_type::get_instance(val
->type
->base_type
, mask
.num_components
, 1);
136 ir_swizzle::create(ir_rvalue
*val
, const char *str
, unsigned vector_length
)
138 /* For each possible swizzle character, this table encodes the value in
139 * \c idx_map that represents the 0th element of the vector. For invalid
140 * swizzle characters (e.g., 'k'), a special value is used that will allow
141 * detection of errors.
143 static const unsigned char base_idx
[26] = {
144 /* a b c d e f g h i j k l m */
145 R
, R
, I
, I
, I
, I
, R
, I
, I
, I
, I
, I
, I
,
146 /* n o p q r s t u v w x y z */
147 I
, I
, S
, S
, R
, S
, S
, I
, I
, X
, X
, X
, X
150 /* Each valid swizzle character has an entry in the previous table. This
151 * table encodes the base index encoded in the previous table plus the actual
152 * index of the swizzle character. When processing swizzles, the first
153 * character in the string is indexed in the previous table. Each character
154 * in the string is indexed in this table, and the value found there has the
155 * value form the first table subtracted. The result must be on the range
158 * For example, the string "wzyx" will get X from the first table. Each of
159 * the charcaters will get X+3, X+2, X+1, and X+0 from this table. After
160 * subtraction, the swizzle values are { 3, 2, 1, 0 }.
162 * The string "wzrg" will get X from the first table. Each of the characters
163 * will get X+3, X+2, R+0, and R+1 from this table. After subtraction, the
164 * swizzle values are { 3, 2, 4, 5 }. Since 4 and 5 are outside the range
165 * [0,3], the error is detected.
167 static const unsigned char idx_map
[26] = {
168 /* a b c d e f g h i j k l m */
169 R
+3, R
+2, 0, 0, 0, 0, R
+1, 0, 0, 0, 0, 0, 0,
170 /* n o p q r s t u v w x y z */
171 0, 0, S
+2, S
+3, R
+0, S
+0, S
+1, 0, 0, X
+3, X
+0, X
+1, X
+2
174 int swiz_idx
[4] = { 0, 0, 0, 0 };
178 /* Validate the first character in the swizzle string and look up the base
179 * index value as described above.
181 if ((str
[0] < 'a') || (str
[0] > 'z'))
184 const unsigned base
= base_idx
[str
[0] - 'a'];
187 for (i
= 0; (i
< 4) && (str
[i
] != '\0'); i
++) {
188 /* Validate the next character, and, as described above, convert it to a
191 if ((str
[i
] < 'a') || (str
[i
] > 'z'))
194 swiz_idx
[i
] = idx_map
[str
[i
] - 'a'] - base
;
195 if ((swiz_idx
[i
] < 0) || (swiz_idx
[i
] >= (int) vector_length
))
202 return new ir_swizzle(val
, swiz_idx
[0], swiz_idx
[1], swiz_idx
[2],
212 ir_variable::ir_variable(const struct glsl_type
*type
, const char *name
)
213 : ir_instruction(), read_only(false), centroid(false), invariant(false),
214 mode(ir_var_auto
), interpolation(ir_var_smooth
)
221 ir_function_signature::ir_function_signature(const glsl_type
*return_type
)
222 : ir_instruction(), return_type(return_type
), definition(NULL
)
228 ir_function::ir_function(const char *name
)
229 : ir_instruction(), name(name
)
236 ir_call::get_error_instruction()
238 ir_call
*call
= new ir_call
;
240 call
->type
= glsl_type::error_type
;