Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1901 | serge | 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 | |||
24 | /** |
||
25 | * \file ir_constant_expression.cpp |
||
26 | * Evaluate and process constant valued expressions |
||
27 | * |
||
28 | * In GLSL, constant valued expressions are used in several places. These |
||
29 | * must be processed and evaluated very early in the compilation process. |
||
30 | * |
||
31 | * * Sizes of arrays |
||
32 | * * Initializers for uniforms |
||
33 | * * Initializers for \c const variables |
||
34 | */ |
||
35 | |||
36 | #include |
||
37 | #include "main/core.h" /* for MAX2, MIN2, CLAMP */ |
||
38 | #include "ir.h" |
||
39 | #include "ir_visitor.h" |
||
40 | #include "glsl_types.h" |
||
41 | |||
42 | static float |
||
43 | dot(ir_constant *op0, ir_constant *op1) |
||
44 | { |
||
45 | assert(op0->type->is_float() && op1->type->is_float()); |
||
46 | |||
47 | float result = 0; |
||
48 | for (unsigned c = 0; c < op0->type->components(); c++) |
||
49 | result += op0->value.f[c] * op1->value.f[c]; |
||
50 | |||
51 | return result; |
||
52 | } |
||
53 | |||
54 | ir_constant * |
||
55 | ir_expression::constant_expression_value() |
||
56 | { |
||
57 | if (this->type->is_error()) |
||
58 | return NULL; |
||
59 | |||
60 | ir_constant *op[Elements(this->operands)] = { NULL, }; |
||
61 | ir_constant_data data; |
||
62 | |||
63 | memset(&data, 0, sizeof(data)); |
||
64 | |||
65 | for (unsigned operand = 0; operand < this->get_num_operands(); operand++) { |
||
66 | op[operand] = this->operands[operand]->constant_expression_value(); |
||
67 | if (!op[operand]) |
||
68 | return NULL; |
||
69 | } |
||
70 | |||
71 | if (op[1] != NULL) |
||
72 | assert(op[0]->type->base_type == op[1]->type->base_type); |
||
73 | |||
74 | bool op0_scalar = op[0]->type->is_scalar(); |
||
75 | bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar(); |
||
76 | |||
77 | /* When iterating over a vector or matrix's components, we want to increase |
||
78 | * the loop counter. However, for scalars, we want to stay at 0. |
||
79 | */ |
||
80 | unsigned c0_inc = op0_scalar ? 0 : 1; |
||
81 | unsigned c1_inc = op1_scalar ? 0 : 1; |
||
82 | unsigned components; |
||
83 | if (op1_scalar || !op[1]) { |
||
84 | components = op[0]->type->components(); |
||
85 | } else { |
||
86 | components = op[1]->type->components(); |
||
87 | } |
||
88 | |||
89 | void *ctx = ralloc_parent(this); |
||
90 | |||
91 | /* Handle array operations here, rather than below. */ |
||
92 | if (op[0]->type->is_array()) { |
||
93 | assert(op[1] != NULL && op[1]->type->is_array()); |
||
94 | switch (this->operation) { |
||
95 | case ir_binop_all_equal: |
||
96 | return new(ctx) ir_constant(op[0]->has_value(op[1])); |
||
97 | case ir_binop_any_nequal: |
||
98 | return new(ctx) ir_constant(!op[0]->has_value(op[1])); |
||
99 | default: |
||
100 | break; |
||
101 | } |
||
102 | return NULL; |
||
103 | } |
||
104 | |||
105 | switch (this->operation) { |
||
106 | case ir_unop_bit_not: |
||
107 | switch (op[0]->type->base_type) { |
||
108 | case GLSL_TYPE_INT: |
||
109 | for (unsigned c = 0; c < components; c++) |
||
110 | data.i[c] = ~ op[0]->value.i[c]; |
||
111 | break; |
||
112 | case GLSL_TYPE_UINT: |
||
113 | for (unsigned c = 0; c < components; c++) |
||
114 | data.u[c] = ~ op[0]->value.u[c]; |
||
115 | break; |
||
116 | default: |
||
117 | assert(0); |
||
118 | } |
||
119 | break; |
||
120 | |||
121 | case ir_unop_logic_not: |
||
122 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
||
123 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
124 | data.b[c] = !op[0]->value.b[c]; |
||
125 | break; |
||
126 | |||
127 | case ir_unop_f2i: |
||
128 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
129 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
130 | data.i[c] = (int) op[0]->value.f[c]; |
||
131 | } |
||
132 | break; |
||
133 | case ir_unop_i2f: |
||
134 | assert(op[0]->type->base_type == GLSL_TYPE_INT); |
||
135 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
136 | data.f[c] = (float) op[0]->value.i[c]; |
||
137 | } |
||
138 | break; |
||
139 | case ir_unop_u2f: |
||
140 | assert(op[0]->type->base_type == GLSL_TYPE_UINT); |
||
141 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
142 | data.f[c] = (float) op[0]->value.u[c]; |
||
143 | } |
||
144 | break; |
||
145 | case ir_unop_b2f: |
||
146 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
||
147 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
148 | data.f[c] = op[0]->value.b[c] ? 1.0F : 0.0F; |
||
149 | } |
||
150 | break; |
||
151 | case ir_unop_f2b: |
||
152 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
153 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
154 | data.b[c] = op[0]->value.f[c] != 0.0F ? true : false; |
||
155 | } |
||
156 | break; |
||
157 | case ir_unop_b2i: |
||
158 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
||
159 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
160 | data.u[c] = op[0]->value.b[c] ? 1 : 0; |
||
161 | } |
||
162 | break; |
||
163 | case ir_unop_i2b: |
||
164 | assert(op[0]->type->is_integer()); |
||
165 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
166 | data.b[c] = op[0]->value.u[c] ? true : false; |
||
167 | } |
||
168 | break; |
||
169 | |||
170 | case ir_unop_any: |
||
171 | assert(op[0]->type->is_boolean()); |
||
172 | data.b[0] = false; |
||
173 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
174 | if (op[0]->value.b[c]) |
||
175 | data.b[0] = true; |
||
176 | } |
||
177 | break; |
||
178 | |||
179 | case ir_unop_trunc: |
||
180 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
181 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
182 | data.f[c] = truncf(op[0]->value.f[c]); |
||
183 | } |
||
184 | break; |
||
185 | |||
186 | case ir_unop_ceil: |
||
187 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
188 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
189 | data.f[c] = ceilf(op[0]->value.f[c]); |
||
190 | } |
||
191 | break; |
||
192 | |||
193 | case ir_unop_floor: |
||
194 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
195 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
196 | data.f[c] = floorf(op[0]->value.f[c]); |
||
197 | } |
||
198 | break; |
||
199 | |||
200 | case ir_unop_fract: |
||
201 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
202 | switch (this->type->base_type) { |
||
203 | case GLSL_TYPE_UINT: |
||
204 | data.u[c] = 0; |
||
205 | break; |
||
206 | case GLSL_TYPE_INT: |
||
207 | data.i[c] = 0; |
||
208 | break; |
||
209 | case GLSL_TYPE_FLOAT: |
||
210 | data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]); |
||
211 | break; |
||
212 | default: |
||
213 | assert(0); |
||
214 | } |
||
215 | } |
||
216 | break; |
||
217 | |||
218 | case ir_unop_sin: |
||
219 | case ir_unop_sin_reduced: |
||
220 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
221 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
222 | data.f[c] = sinf(op[0]->value.f[c]); |
||
223 | } |
||
224 | break; |
||
225 | |||
226 | case ir_unop_cos: |
||
227 | case ir_unop_cos_reduced: |
||
228 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
229 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
230 | data.f[c] = cosf(op[0]->value.f[c]); |
||
231 | } |
||
232 | break; |
||
233 | |||
234 | case ir_unop_neg: |
||
235 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
236 | switch (this->type->base_type) { |
||
237 | case GLSL_TYPE_UINT: |
||
238 | data.u[c] = -((int) op[0]->value.u[c]); |
||
239 | break; |
||
240 | case GLSL_TYPE_INT: |
||
241 | data.i[c] = -op[0]->value.i[c]; |
||
242 | break; |
||
243 | case GLSL_TYPE_FLOAT: |
||
244 | data.f[c] = -op[0]->value.f[c]; |
||
245 | break; |
||
246 | default: |
||
247 | assert(0); |
||
248 | } |
||
249 | } |
||
250 | break; |
||
251 | |||
252 | case ir_unop_abs: |
||
253 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
254 | switch (this->type->base_type) { |
||
255 | case GLSL_TYPE_UINT: |
||
256 | data.u[c] = op[0]->value.u[c]; |
||
257 | break; |
||
258 | case GLSL_TYPE_INT: |
||
259 | data.i[c] = op[0]->value.i[c]; |
||
260 | if (data.i[c] < 0) |
||
261 | data.i[c] = -data.i[c]; |
||
262 | break; |
||
263 | case GLSL_TYPE_FLOAT: |
||
264 | data.f[c] = fabs(op[0]->value.f[c]); |
||
265 | break; |
||
266 | default: |
||
267 | assert(0); |
||
268 | } |
||
269 | } |
||
270 | break; |
||
271 | |||
272 | case ir_unop_sign: |
||
273 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
274 | switch (this->type->base_type) { |
||
275 | case GLSL_TYPE_UINT: |
||
276 | data.u[c] = op[0]->value.i[c] > 0; |
||
277 | break; |
||
278 | case GLSL_TYPE_INT: |
||
279 | data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0); |
||
280 | break; |
||
281 | case GLSL_TYPE_FLOAT: |
||
282 | data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0)); |
||
283 | break; |
||
284 | default: |
||
285 | assert(0); |
||
286 | } |
||
287 | } |
||
288 | break; |
||
289 | |||
290 | case ir_unop_rcp: |
||
291 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
292 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
293 | switch (this->type->base_type) { |
||
294 | case GLSL_TYPE_UINT: |
||
295 | if (op[0]->value.u[c] != 0.0) |
||
296 | data.u[c] = 1 / op[0]->value.u[c]; |
||
297 | break; |
||
298 | case GLSL_TYPE_INT: |
||
299 | if (op[0]->value.i[c] != 0.0) |
||
300 | data.i[c] = 1 / op[0]->value.i[c]; |
||
301 | break; |
||
302 | case GLSL_TYPE_FLOAT: |
||
303 | if (op[0]->value.f[c] != 0.0) |
||
304 | data.f[c] = 1.0F / op[0]->value.f[c]; |
||
305 | break; |
||
306 | default: |
||
307 | assert(0); |
||
308 | } |
||
309 | } |
||
310 | break; |
||
311 | |||
312 | case ir_unop_rsq: |
||
313 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
314 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
315 | data.f[c] = 1.0F / sqrtf(op[0]->value.f[c]); |
||
316 | } |
||
317 | break; |
||
318 | |||
319 | case ir_unop_sqrt: |
||
320 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
321 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
322 | data.f[c] = sqrtf(op[0]->value.f[c]); |
||
323 | } |
||
324 | break; |
||
325 | |||
326 | case ir_unop_exp: |
||
327 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
328 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
329 | data.f[c] = expf(op[0]->value.f[c]); |
||
330 | } |
||
331 | break; |
||
332 | |||
333 | case ir_unop_exp2: |
||
334 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
335 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
336 | data.f[c] = exp2f(op[0]->value.f[c]); |
||
337 | } |
||
338 | break; |
||
339 | |||
340 | case ir_unop_log: |
||
341 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
342 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
343 | data.f[c] = logf(op[0]->value.f[c]); |
||
344 | } |
||
345 | break; |
||
346 | |||
347 | case ir_unop_log2: |
||
348 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
349 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
350 | data.f[c] = log2f(op[0]->value.f[c]); |
||
351 | } |
||
352 | break; |
||
353 | |||
354 | case ir_unop_dFdx: |
||
355 | case ir_unop_dFdy: |
||
356 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
357 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
358 | data.f[c] = 0.0; |
||
359 | } |
||
360 | break; |
||
361 | |||
362 | case ir_binop_pow: |
||
363 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
||
364 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
365 | data.f[c] = powf(op[0]->value.f[c], op[1]->value.f[c]); |
||
366 | } |
||
367 | break; |
||
368 | |||
369 | case ir_binop_dot: |
||
370 | data.f[0] = dot(op[0], op[1]); |
||
371 | break; |
||
372 | |||
373 | case ir_binop_min: |
||
374 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
||
375 | for (unsigned c = 0, c0 = 0, c1 = 0; |
||
376 | c < components; |
||
377 | c0 += c0_inc, c1 += c1_inc, c++) { |
||
378 | |||
379 | switch (op[0]->type->base_type) { |
||
380 | case GLSL_TYPE_UINT: |
||
381 | data.u[c] = MIN2(op[0]->value.u[c0], op[1]->value.u[c1]); |
||
382 | break; |
||
383 | case GLSL_TYPE_INT: |
||
384 | data.i[c] = MIN2(op[0]->value.i[c0], op[1]->value.i[c1]); |
||
385 | break; |
||
386 | case GLSL_TYPE_FLOAT: |
||
387 | data.f[c] = MIN2(op[0]->value.f[c0], op[1]->value.f[c1]); |
||
388 | break; |
||
389 | default: |
||
390 | assert(0); |
||
391 | } |
||
392 | } |
||
393 | |||
394 | break; |
||
395 | case ir_binop_max: |
||
396 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
||
397 | for (unsigned c = 0, c0 = 0, c1 = 0; |
||
398 | c < components; |
||
399 | c0 += c0_inc, c1 += c1_inc, c++) { |
||
400 | |||
401 | switch (op[0]->type->base_type) { |
||
402 | case GLSL_TYPE_UINT: |
||
403 | data.u[c] = MAX2(op[0]->value.u[c0], op[1]->value.u[c1]); |
||
404 | break; |
||
405 | case GLSL_TYPE_INT: |
||
406 | data.i[c] = MAX2(op[0]->value.i[c0], op[1]->value.i[c1]); |
||
407 | break; |
||
408 | case GLSL_TYPE_FLOAT: |
||
409 | data.f[c] = MAX2(op[0]->value.f[c0], op[1]->value.f[c1]); |
||
410 | break; |
||
411 | default: |
||
412 | assert(0); |
||
413 | } |
||
414 | } |
||
415 | break; |
||
416 | |||
417 | case ir_binop_add: |
||
418 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
||
419 | for (unsigned c = 0, c0 = 0, c1 = 0; |
||
420 | c < components; |
||
421 | c0 += c0_inc, c1 += c1_inc, c++) { |
||
422 | |||
423 | switch (op[0]->type->base_type) { |
||
424 | case GLSL_TYPE_UINT: |
||
425 | data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1]; |
||
426 | break; |
||
427 | case GLSL_TYPE_INT: |
||
428 | data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1]; |
||
429 | break; |
||
430 | case GLSL_TYPE_FLOAT: |
||
431 | data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1]; |
||
432 | break; |
||
433 | default: |
||
434 | assert(0); |
||
435 | } |
||
436 | } |
||
437 | |||
438 | break; |
||
439 | case ir_binop_sub: |
||
440 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
||
441 | for (unsigned c = 0, c0 = 0, c1 = 0; |
||
442 | c < components; |
||
443 | c0 += c0_inc, c1 += c1_inc, c++) { |
||
444 | |||
445 | switch (op[0]->type->base_type) { |
||
446 | case GLSL_TYPE_UINT: |
||
447 | data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1]; |
||
448 | break; |
||
449 | case GLSL_TYPE_INT: |
||
450 | data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1]; |
||
451 | break; |
||
452 | case GLSL_TYPE_FLOAT: |
||
453 | data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1]; |
||
454 | break; |
||
455 | default: |
||
456 | assert(0); |
||
457 | } |
||
458 | } |
||
459 | |||
460 | break; |
||
461 | case ir_binop_mul: |
||
462 | /* Check for equal types, or unequal types involving scalars */ |
||
463 | if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix()) |
||
464 | || op0_scalar || op1_scalar) { |
||
465 | for (unsigned c = 0, c0 = 0, c1 = 0; |
||
466 | c < components; |
||
467 | c0 += c0_inc, c1 += c1_inc, c++) { |
||
468 | |||
469 | switch (op[0]->type->base_type) { |
||
470 | case GLSL_TYPE_UINT: |
||
471 | data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1]; |
||
472 | break; |
||
473 | case GLSL_TYPE_INT: |
||
474 | data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1]; |
||
475 | break; |
||
476 | case GLSL_TYPE_FLOAT: |
||
477 | data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1]; |
||
478 | break; |
||
479 | default: |
||
480 | assert(0); |
||
481 | } |
||
482 | } |
||
483 | } else { |
||
484 | assert(op[0]->type->is_matrix() || op[1]->type->is_matrix()); |
||
485 | |||
486 | /* Multiply an N-by-M matrix with an M-by-P matrix. Since either |
||
487 | * matrix can be a GLSL vector, either N or P can be 1. |
||
488 | * |
||
489 | * For vec*mat, the vector is treated as a row vector. This |
||
490 | * means the vector is a 1-row x M-column matrix. |
||
491 | * |
||
492 | * For mat*vec, the vector is treated as a column vector. Since |
||
493 | * matrix_columns is 1 for vectors, this just works. |
||
494 | */ |
||
495 | const unsigned n = op[0]->type->is_vector() |
||
496 | ? 1 : op[0]->type->vector_elements; |
||
497 | const unsigned m = op[1]->type->vector_elements; |
||
498 | const unsigned p = op[1]->type->matrix_columns; |
||
499 | for (unsigned j = 0; j < p; j++) { |
||
500 | for (unsigned i = 0; i < n; i++) { |
||
501 | for (unsigned k = 0; k < m; k++) { |
||
502 | data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j]; |
||
503 | } |
||
504 | } |
||
505 | } |
||
506 | } |
||
507 | |||
508 | break; |
||
509 | case ir_binop_div: |
||
510 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
||
511 | for (unsigned c = 0, c0 = 0, c1 = 0; |
||
512 | c < components; |
||
513 | c0 += c0_inc, c1 += c1_inc, c++) { |
||
514 | |||
515 | switch (op[0]->type->base_type) { |
||
516 | case GLSL_TYPE_UINT: |
||
517 | if (op[1]->value.u[c1] == 0) { |
||
518 | data.u[c] = 0; |
||
519 | } else { |
||
520 | data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1]; |
||
521 | } |
||
522 | break; |
||
523 | case GLSL_TYPE_INT: |
||
524 | if (op[1]->value.i[c1] == 0) { |
||
525 | data.i[c] = 0; |
||
526 | } else { |
||
527 | data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1]; |
||
528 | } |
||
529 | break; |
||
530 | case GLSL_TYPE_FLOAT: |
||
531 | data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1]; |
||
532 | break; |
||
533 | default: |
||
534 | assert(0); |
||
535 | } |
||
536 | } |
||
537 | |||
538 | break; |
||
539 | case ir_binop_mod: |
||
540 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
||
541 | for (unsigned c = 0, c0 = 0, c1 = 0; |
||
542 | c < components; |
||
543 | c0 += c0_inc, c1 += c1_inc, c++) { |
||
544 | |||
545 | switch (op[0]->type->base_type) { |
||
546 | case GLSL_TYPE_UINT: |
||
547 | if (op[1]->value.u[c1] == 0) { |
||
548 | data.u[c] = 0; |
||
549 | } else { |
||
550 | data.u[c] = op[0]->value.u[c0] % op[1]->value.u[c1]; |
||
551 | } |
||
552 | break; |
||
553 | case GLSL_TYPE_INT: |
||
554 | if (op[1]->value.i[c1] == 0) { |
||
555 | data.i[c] = 0; |
||
556 | } else { |
||
557 | data.i[c] = op[0]->value.i[c0] % op[1]->value.i[c1]; |
||
558 | } |
||
559 | break; |
||
560 | case GLSL_TYPE_FLOAT: |
||
561 | /* We don't use fmod because it rounds toward zero; GLSL specifies |
||
562 | * the use of floor. |
||
563 | */ |
||
564 | data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1] |
||
565 | * floorf(op[0]->value.f[c0] / op[1]->value.f[c1]); |
||
566 | break; |
||
567 | default: |
||
568 | assert(0); |
||
569 | } |
||
570 | } |
||
571 | |||
572 | break; |
||
573 | |||
574 | case ir_binop_logic_and: |
||
575 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
||
576 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
577 | data.b[c] = op[0]->value.b[c] && op[1]->value.b[c]; |
||
578 | break; |
||
579 | case ir_binop_logic_xor: |
||
580 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
||
581 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
582 | data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c]; |
||
583 | break; |
||
584 | case ir_binop_logic_or: |
||
585 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
||
586 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
587 | data.b[c] = op[0]->value.b[c] || op[1]->value.b[c]; |
||
588 | break; |
||
589 | |||
590 | case ir_binop_less: |
||
591 | assert(op[0]->type == op[1]->type); |
||
592 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
593 | switch (op[0]->type->base_type) { |
||
594 | case GLSL_TYPE_UINT: |
||
595 | data.b[0] = op[0]->value.u[0] < op[1]->value.u[0]; |
||
596 | break; |
||
597 | case GLSL_TYPE_INT: |
||
598 | data.b[0] = op[0]->value.i[0] < op[1]->value.i[0]; |
||
599 | break; |
||
600 | case GLSL_TYPE_FLOAT: |
||
601 | data.b[0] = op[0]->value.f[0] < op[1]->value.f[0]; |
||
602 | break; |
||
603 | default: |
||
604 | assert(0); |
||
605 | } |
||
606 | } |
||
607 | break; |
||
608 | case ir_binop_greater: |
||
609 | assert(op[0]->type == op[1]->type); |
||
610 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
611 | switch (op[0]->type->base_type) { |
||
612 | case GLSL_TYPE_UINT: |
||
613 | data.b[c] = op[0]->value.u[c] > op[1]->value.u[c]; |
||
614 | break; |
||
615 | case GLSL_TYPE_INT: |
||
616 | data.b[c] = op[0]->value.i[c] > op[1]->value.i[c]; |
||
617 | break; |
||
618 | case GLSL_TYPE_FLOAT: |
||
619 | data.b[c] = op[0]->value.f[c] > op[1]->value.f[c]; |
||
620 | break; |
||
621 | default: |
||
622 | assert(0); |
||
623 | } |
||
624 | } |
||
625 | break; |
||
626 | case ir_binop_lequal: |
||
627 | assert(op[0]->type == op[1]->type); |
||
628 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
629 | switch (op[0]->type->base_type) { |
||
630 | case GLSL_TYPE_UINT: |
||
631 | data.b[0] = op[0]->value.u[0] <= op[1]->value.u[0]; |
||
632 | break; |
||
633 | case GLSL_TYPE_INT: |
||
634 | data.b[0] = op[0]->value.i[0] <= op[1]->value.i[0]; |
||
635 | break; |
||
636 | case GLSL_TYPE_FLOAT: |
||
637 | data.b[0] = op[0]->value.f[0] <= op[1]->value.f[0]; |
||
638 | break; |
||
639 | default: |
||
640 | assert(0); |
||
641 | } |
||
642 | } |
||
643 | break; |
||
644 | case ir_binop_gequal: |
||
645 | assert(op[0]->type == op[1]->type); |
||
646 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
647 | switch (op[0]->type->base_type) { |
||
648 | case GLSL_TYPE_UINT: |
||
649 | data.b[0] = op[0]->value.u[0] >= op[1]->value.u[0]; |
||
650 | break; |
||
651 | case GLSL_TYPE_INT: |
||
652 | data.b[0] = op[0]->value.i[0] >= op[1]->value.i[0]; |
||
653 | break; |
||
654 | case GLSL_TYPE_FLOAT: |
||
655 | data.b[0] = op[0]->value.f[0] >= op[1]->value.f[0]; |
||
656 | break; |
||
657 | default: |
||
658 | assert(0); |
||
659 | } |
||
660 | } |
||
661 | break; |
||
662 | case ir_binop_equal: |
||
663 | assert(op[0]->type == op[1]->type); |
||
664 | for (unsigned c = 0; c < components; c++) { |
||
665 | switch (op[0]->type->base_type) { |
||
666 | case GLSL_TYPE_UINT: |
||
667 | data.b[c] = op[0]->value.u[c] == op[1]->value.u[c]; |
||
668 | break; |
||
669 | case GLSL_TYPE_INT: |
||
670 | data.b[c] = op[0]->value.i[c] == op[1]->value.i[c]; |
||
671 | break; |
||
672 | case GLSL_TYPE_FLOAT: |
||
673 | data.b[c] = op[0]->value.f[c] == op[1]->value.f[c]; |
||
674 | break; |
||
675 | default: |
||
676 | assert(0); |
||
677 | } |
||
678 | } |
||
679 | break; |
||
680 | case ir_binop_nequal: |
||
681 | assert(op[0]->type != op[1]->type); |
||
682 | for (unsigned c = 0; c < components; c++) { |
||
683 | switch (op[0]->type->base_type) { |
||
684 | case GLSL_TYPE_UINT: |
||
685 | data.b[c] = op[0]->value.u[c] != op[1]->value.u[c]; |
||
686 | break; |
||
687 | case GLSL_TYPE_INT: |
||
688 | data.b[c] = op[0]->value.i[c] != op[1]->value.i[c]; |
||
689 | break; |
||
690 | case GLSL_TYPE_FLOAT: |
||
691 | data.b[c] = op[0]->value.f[c] != op[1]->value.f[c]; |
||
692 | break; |
||
693 | default: |
||
694 | assert(0); |
||
695 | } |
||
696 | } |
||
697 | break; |
||
698 | case ir_binop_all_equal: |
||
699 | data.b[0] = op[0]->has_value(op[1]); |
||
700 | break; |
||
701 | case ir_binop_any_nequal: |
||
702 | data.b[0] = !op[0]->has_value(op[1]); |
||
703 | break; |
||
704 | |||
705 | case ir_binop_lshift: |
||
706 | for (unsigned c = 0, c0 = 0, c1 = 0; |
||
707 | c < components; |
||
708 | c0 += c0_inc, c1 += c1_inc, c++) { |
||
709 | |||
710 | if (op[0]->type->base_type == GLSL_TYPE_INT && |
||
711 | op[1]->type->base_type == GLSL_TYPE_INT) { |
||
712 | data.i[c] = op[0]->value.i[c0] << op[1]->value.i[c1]; |
||
713 | |||
714 | } else if (op[0]->type->base_type == GLSL_TYPE_INT && |
||
715 | op[1]->type->base_type == GLSL_TYPE_UINT) { |
||
716 | data.i[c] = op[0]->value.i[c0] << op[1]->value.u[c1]; |
||
717 | |||
718 | } else if (op[0]->type->base_type == GLSL_TYPE_UINT && |
||
719 | op[1]->type->base_type == GLSL_TYPE_INT) { |
||
720 | data.u[c] = op[0]->value.u[c0] << op[1]->value.i[c1]; |
||
721 | |||
722 | } else if (op[0]->type->base_type == GLSL_TYPE_UINT && |
||
723 | op[1]->type->base_type == GLSL_TYPE_UINT) { |
||
724 | data.u[c] = op[0]->value.u[c0] << op[1]->value.u[c1]; |
||
725 | } |
||
726 | } |
||
727 | break; |
||
728 | |||
729 | case ir_binop_rshift: |
||
730 | for (unsigned c = 0, c0 = 0, c1 = 0; |
||
731 | c < components; |
||
732 | c0 += c0_inc, c1 += c1_inc, c++) { |
||
733 | |||
734 | if (op[0]->type->base_type == GLSL_TYPE_INT && |
||
735 | op[1]->type->base_type == GLSL_TYPE_INT) { |
||
736 | data.i[c] = op[0]->value.i[c0] >> op[1]->value.i[c1]; |
||
737 | |||
738 | } else if (op[0]->type->base_type == GLSL_TYPE_INT && |
||
739 | op[1]->type->base_type == GLSL_TYPE_UINT) { |
||
740 | data.i[c] = op[0]->value.i[c0] >> op[1]->value.u[c1]; |
||
741 | |||
742 | } else if (op[0]->type->base_type == GLSL_TYPE_UINT && |
||
743 | op[1]->type->base_type == GLSL_TYPE_INT) { |
||
744 | data.u[c] = op[0]->value.u[c0] >> op[1]->value.i[c1]; |
||
745 | |||
746 | } else if (op[0]->type->base_type == GLSL_TYPE_UINT && |
||
747 | op[1]->type->base_type == GLSL_TYPE_UINT) { |
||
748 | data.u[c] = op[0]->value.u[c0] >> op[1]->value.u[c1]; |
||
749 | } |
||
750 | } |
||
751 | break; |
||
752 | |||
753 | case ir_binop_bit_and: |
||
754 | for (unsigned c = 0, c0 = 0, c1 = 0; |
||
755 | c < components; |
||
756 | c0 += c0_inc, c1 += c1_inc, c++) { |
||
757 | |||
758 | switch (op[0]->type->base_type) { |
||
759 | case GLSL_TYPE_INT: |
||
760 | data.i[c] = op[0]->value.i[c0] & op[1]->value.i[c1]; |
||
761 | break; |
||
762 | case GLSL_TYPE_UINT: |
||
763 | data.u[c] = op[0]->value.u[c0] & op[1]->value.u[c1]; |
||
764 | break; |
||
765 | default: |
||
766 | assert(0); |
||
767 | } |
||
768 | } |
||
769 | break; |
||
770 | |||
771 | case ir_binop_bit_or: |
||
772 | for (unsigned c = 0, c0 = 0, c1 = 0; |
||
773 | c < components; |
||
774 | c0 += c0_inc, c1 += c1_inc, c++) { |
||
775 | |||
776 | switch (op[0]->type->base_type) { |
||
777 | case GLSL_TYPE_INT: |
||
778 | data.i[c] = op[0]->value.i[c0] | op[1]->value.i[c1]; |
||
779 | break; |
||
780 | case GLSL_TYPE_UINT: |
||
781 | data.u[c] = op[0]->value.u[c0] | op[1]->value.u[c1]; |
||
782 | break; |
||
783 | default: |
||
784 | assert(0); |
||
785 | } |
||
786 | } |
||
787 | break; |
||
788 | |||
789 | case ir_binop_bit_xor: |
||
790 | for (unsigned c = 0, c0 = 0, c1 = 0; |
||
791 | c < components; |
||
792 | c0 += c0_inc, c1 += c1_inc, c++) { |
||
793 | |||
794 | switch (op[0]->type->base_type) { |
||
795 | case GLSL_TYPE_INT: |
||
796 | data.i[c] = op[0]->value.i[c0] ^ op[1]->value.i[c1]; |
||
797 | break; |
||
798 | case GLSL_TYPE_UINT: |
||
799 | data.u[c] = op[0]->value.u[c0] ^ op[1]->value.u[c1]; |
||
800 | break; |
||
801 | default: |
||
802 | assert(0); |
||
803 | } |
||
804 | } |
||
805 | break; |
||
806 | |||
807 | case ir_quadop_vector: |
||
808 | for (unsigned c = 0; c < this->type->vector_elements; c++) { |
||
809 | switch (this->type->base_type) { |
||
810 | case GLSL_TYPE_INT: |
||
811 | data.i[c] = op[c]->value.i[0]; |
||
812 | break; |
||
813 | case GLSL_TYPE_UINT: |
||
814 | data.u[c] = op[c]->value.u[0]; |
||
815 | break; |
||
816 | case GLSL_TYPE_FLOAT: |
||
817 | data.f[c] = op[c]->value.f[0]; |
||
818 | break; |
||
819 | default: |
||
820 | assert(0); |
||
821 | } |
||
822 | } |
||
823 | break; |
||
824 | |||
825 | default: |
||
826 | /* FINISHME: Should handle all expression types. */ |
||
827 | return NULL; |
||
828 | } |
||
829 | |||
830 | return new(ctx) ir_constant(this->type, &data); |
||
831 | } |
||
832 | |||
833 | |||
834 | ir_constant * |
||
835 | ir_texture::constant_expression_value() |
||
836 | { |
||
837 | /* texture lookups aren't constant expressions */ |
||
838 | return NULL; |
||
839 | } |
||
840 | |||
841 | |||
842 | ir_constant * |
||
843 | ir_swizzle::constant_expression_value() |
||
844 | { |
||
845 | ir_constant *v = this->val->constant_expression_value(); |
||
846 | |||
847 | if (v != NULL) { |
||
848 | ir_constant_data data = { { 0 } }; |
||
849 | |||
850 | const unsigned swiz_idx[4] = { |
||
851 | this->mask.x, this->mask.y, this->mask.z, this->mask.w |
||
852 | }; |
||
853 | |||
854 | for (unsigned i = 0; i < this->mask.num_components; i++) { |
||
855 | switch (v->type->base_type) { |
||
856 | case GLSL_TYPE_UINT: |
||
857 | case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break; |
||
858 | case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break; |
||
859 | case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break; |
||
860 | default: assert(!"Should not get here."); break; |
||
861 | } |
||
862 | } |
||
863 | |||
864 | void *ctx = ralloc_parent(this); |
||
865 | return new(ctx) ir_constant(this->type, &data); |
||
866 | } |
||
867 | return NULL; |
||
868 | } |
||
869 | |||
870 | |||
871 | ir_constant * |
||
872 | ir_dereference_variable::constant_expression_value() |
||
873 | { |
||
874 | /* This may occur during compile and var->type is glsl_type::error_type */ |
||
875 | if (!var) |
||
876 | return NULL; |
||
877 | |||
878 | /* The constant_value of a uniform variable is its initializer, |
||
879 | * not the lifetime constant value of the uniform. |
||
880 | */ |
||
881 | if (var->mode == ir_var_uniform) |
||
882 | return NULL; |
||
883 | |||
884 | if (!var->constant_value) |
||
885 | return NULL; |
||
886 | |||
887 | return var->constant_value->clone(ralloc_parent(var), NULL); |
||
888 | } |
||
889 | |||
890 | |||
891 | ir_constant * |
||
892 | ir_dereference_array::constant_expression_value() |
||
893 | { |
||
894 | ir_constant *array = this->array->constant_expression_value(); |
||
895 | ir_constant *idx = this->array_index->constant_expression_value(); |
||
896 | |||
897 | if ((array != NULL) && (idx != NULL)) { |
||
898 | void *ctx = ralloc_parent(this); |
||
899 | if (array->type->is_matrix()) { |
||
900 | /* Array access of a matrix results in a vector. |
||
901 | */ |
||
902 | const unsigned column = idx->value.u[0]; |
||
903 | |||
904 | const glsl_type *const column_type = array->type->column_type(); |
||
905 | |||
906 | /* Offset in the constant matrix to the first element of the column |
||
907 | * to be extracted. |
||
908 | */ |
||
909 | const unsigned mat_idx = column * column_type->vector_elements; |
||
910 | |||
911 | ir_constant_data data = { { 0 } }; |
||
912 | |||
913 | switch (column_type->base_type) { |
||
914 | case GLSL_TYPE_UINT: |
||
915 | case GLSL_TYPE_INT: |
||
916 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
||
917 | data.u[i] = array->value.u[mat_idx + i]; |
||
918 | |||
919 | break; |
||
920 | |||
921 | case GLSL_TYPE_FLOAT: |
||
922 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
||
923 | data.f[i] = array->value.f[mat_idx + i]; |
||
924 | |||
925 | break; |
||
926 | |||
927 | default: |
||
928 | assert(!"Should not get here."); |
||
929 | break; |
||
930 | } |
||
931 | |||
932 | return new(ctx) ir_constant(column_type, &data); |
||
933 | } else if (array->type->is_vector()) { |
||
934 | const unsigned component = idx->value.u[0]; |
||
935 | |||
936 | return new(ctx) ir_constant(array, component); |
||
937 | } else { |
||
938 | const unsigned index = idx->value.u[0]; |
||
939 | return array->get_array_element(index)->clone(ctx, NULL); |
||
940 | } |
||
941 | } |
||
942 | return NULL; |
||
943 | } |
||
944 | |||
945 | |||
946 | ir_constant * |
||
947 | ir_dereference_record::constant_expression_value() |
||
948 | { |
||
949 | ir_constant *v = this->record->constant_expression_value(); |
||
950 | |||
951 | return (v != NULL) ? v->get_record_field(this->field) : NULL; |
||
952 | } |
||
953 | |||
954 | |||
955 | ir_constant * |
||
956 | ir_assignment::constant_expression_value() |
||
957 | { |
||
958 | /* FINISHME: Handle CEs involving assignment (return RHS) */ |
||
959 | return NULL; |
||
960 | } |
||
961 | |||
962 | |||
963 | ir_constant * |
||
964 | ir_constant::constant_expression_value() |
||
965 | { |
||
966 | return this; |
||
967 | } |
||
968 | |||
969 | |||
970 | ir_constant * |
||
971 | ir_call::constant_expression_value() |
||
972 | { |
||
973 | if (this->type == glsl_type::error_type) |
||
974 | return NULL; |
||
975 | |||
976 | /* From the GLSL 1.20 spec, page 23: |
||
977 | * "Function calls to user-defined functions (non-built-in functions) |
||
978 | * cannot be used to form constant expressions." |
||
979 | */ |
||
980 | if (!this->callee->is_builtin) |
||
981 | return NULL; |
||
982 | |||
983 | unsigned num_parameters = 0; |
||
984 | |||
985 | /* Check if all parameters are constant */ |
||
986 | ir_constant *op[3]; |
||
987 | foreach_list(n, &this->actual_parameters) { |
||
988 | ir_constant *constant = ((ir_rvalue *) n)->constant_expression_value(); |
||
989 | if (constant == NULL) |
||
990 | return NULL; |
||
991 | |||
992 | op[num_parameters] = constant; |
||
993 | |||
994 | assert(num_parameters < 3); |
||
995 | num_parameters++; |
||
996 | } |
||
997 | |||
998 | /* Individual cases below can either: |
||
999 | * - Assign "expr" a new ir_expression to evaluate (for basic opcodes) |
||
1000 | * - Fill "data" with appopriate constant data |
||
1001 | * - Return an ir_constant directly. |
||
1002 | */ |
||
1003 | void *mem_ctx = ralloc_parent(this); |
||
1004 | ir_expression *expr = NULL; |
||
1005 | |||
1006 | ir_constant_data data; |
||
1007 | memset(&data, 0, sizeof(data)); |
||
1008 | |||
1009 | const char *callee = this->callee_name(); |
||
1010 | if (strcmp(callee, "abs") == 0) { |
||
1011 | expr = new(mem_ctx) ir_expression(ir_unop_abs, type, op[0], NULL); |
||
1012 | } else if (strcmp(callee, "all") == 0) { |
||
1013 | assert(op[0]->type->is_boolean()); |
||
1014 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
1015 | if (!op[0]->value.b[c]) |
||
1016 | return new(mem_ctx) ir_constant(false); |
||
1017 | } |
||
1018 | return new(mem_ctx) ir_constant(true); |
||
1019 | } else if (strcmp(callee, "any") == 0) { |
||
1020 | assert(op[0]->type->is_boolean()); |
||
1021 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
1022 | if (op[0]->value.b[c]) |
||
1023 | return new(mem_ctx) ir_constant(true); |
||
1024 | } |
||
1025 | return new(mem_ctx) ir_constant(false); |
||
1026 | } else if (strcmp(callee, "acos") == 0) { |
||
1027 | assert(op[0]->type->is_float()); |
||
1028 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1029 | data.f[c] = acosf(op[0]->value.f[c]); |
||
1030 | } else if (strcmp(callee, "acosh") == 0) { |
||
1031 | assert(op[0]->type->is_float()); |
||
1032 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1033 | data.f[c] = acoshf(op[0]->value.f[c]); |
||
1034 | } else if (strcmp(callee, "asin") == 0) { |
||
1035 | assert(op[0]->type->is_float()); |
||
1036 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1037 | data.f[c] = asinf(op[0]->value.f[c]); |
||
1038 | } else if (strcmp(callee, "asinh") == 0) { |
||
1039 | assert(op[0]->type->is_float()); |
||
1040 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1041 | data.f[c] = asinhf(op[0]->value.f[c]); |
||
1042 | } else if (strcmp(callee, "atan") == 0) { |
||
1043 | assert(op[0]->type->is_float()); |
||
1044 | if (num_parameters == 2) { |
||
1045 | assert(op[1]->type->is_float()); |
||
1046 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1047 | data.f[c] = atan2f(op[0]->value.f[c], op[1]->value.f[c]); |
||
1048 | } else { |
||
1049 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1050 | data.f[c] = atanf(op[0]->value.f[c]); |
||
1051 | } |
||
1052 | } else if (strcmp(callee, "atanh") == 0) { |
||
1053 | assert(op[0]->type->is_float()); |
||
1054 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1055 | data.f[c] = atanhf(op[0]->value.f[c]); |
||
1056 | } else if (strcmp(callee, "dFdx") == 0 || strcmp(callee, "dFdy") == 0) { |
||
1057 | return ir_constant::zero(mem_ctx, this->type); |
||
1058 | } else if (strcmp(callee, "ceil") == 0) { |
||
1059 | expr = new(mem_ctx) ir_expression(ir_unop_ceil, type, op[0], NULL); |
||
1060 | } else if (strcmp(callee, "clamp") == 0) { |
||
1061 | assert(num_parameters == 3); |
||
1062 | unsigned c1_inc = op[1]->type->is_scalar() ? 0 : 1; |
||
1063 | unsigned c2_inc = op[2]->type->is_scalar() ? 0 : 1; |
||
1064 | for (unsigned c = 0, c1 = 0, c2 = 0; |
||
1065 | c < op[0]->type->components(); |
||
1066 | c1 += c1_inc, c2 += c2_inc, c++) { |
||
1067 | |||
1068 | switch (op[0]->type->base_type) { |
||
1069 | case GLSL_TYPE_UINT: |
||
1070 | data.u[c] = CLAMP(op[0]->value.u[c], op[1]->value.u[c1], |
||
1071 | op[2]->value.u[c2]); |
||
1072 | break; |
||
1073 | case GLSL_TYPE_INT: |
||
1074 | data.i[c] = CLAMP(op[0]->value.i[c], op[1]->value.i[c1], |
||
1075 | op[2]->value.i[c2]); |
||
1076 | break; |
||
1077 | case GLSL_TYPE_FLOAT: |
||
1078 | data.f[c] = CLAMP(op[0]->value.f[c], op[1]->value.f[c1], |
||
1079 | op[2]->value.f[c2]); |
||
1080 | break; |
||
1081 | default: |
||
1082 | assert(!"Should not get here."); |
||
1083 | } |
||
1084 | } |
||
1085 | } else if (strcmp(callee, "cos") == 0) { |
||
1086 | expr = new(mem_ctx) ir_expression(ir_unop_cos, type, op[0], NULL); |
||
1087 | } else if (strcmp(callee, "cosh") == 0) { |
||
1088 | assert(op[0]->type->is_float()); |
||
1089 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1090 | data.f[c] = coshf(op[0]->value.f[c]); |
||
1091 | } else if (strcmp(callee, "cross") == 0) { |
||
1092 | assert(op[0]->type == glsl_type::vec3_type); |
||
1093 | assert(op[1]->type == glsl_type::vec3_type); |
||
1094 | data.f[0] = (op[0]->value.f[1] * op[1]->value.f[2] - |
||
1095 | op[1]->value.f[1] * op[0]->value.f[2]); |
||
1096 | data.f[1] = (op[0]->value.f[2] * op[1]->value.f[0] - |
||
1097 | op[1]->value.f[2] * op[0]->value.f[0]); |
||
1098 | data.f[2] = (op[0]->value.f[0] * op[1]->value.f[1] - |
||
1099 | op[1]->value.f[0] * op[0]->value.f[1]); |
||
1100 | } else if (strcmp(callee, "degrees") == 0) { |
||
1101 | assert(op[0]->type->is_float()); |
||
1102 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1103 | data.f[c] = 180.0F / M_PI * op[0]->value.f[c]; |
||
1104 | } else if (strcmp(callee, "distance") == 0) { |
||
1105 | assert(op[0]->type->is_float() && op[1]->type->is_float()); |
||
1106 | float length_squared = 0.0; |
||
1107 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
1108 | float t = op[0]->value.f[c] - op[1]->value.f[c]; |
||
1109 | length_squared += t * t; |
||
1110 | } |
||
1111 | return new(mem_ctx) ir_constant(sqrtf(length_squared)); |
||
1112 | } else if (strcmp(callee, "dot") == 0) { |
||
1113 | return new(mem_ctx) ir_constant(dot(op[0], op[1])); |
||
1114 | } else if (strcmp(callee, "equal") == 0) { |
||
1115 | assert(op[0]->type->is_vector() && op[1] && op[1]->type->is_vector()); |
||
1116 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
1117 | switch (op[0]->type->base_type) { |
||
1118 | case GLSL_TYPE_UINT: |
||
1119 | data.b[c] = op[0]->value.u[c] == op[1]->value.u[c]; |
||
1120 | break; |
||
1121 | case GLSL_TYPE_INT: |
||
1122 | data.b[c] = op[0]->value.i[c] == op[1]->value.i[c]; |
||
1123 | break; |
||
1124 | case GLSL_TYPE_FLOAT: |
||
1125 | data.b[c] = op[0]->value.f[c] == op[1]->value.f[c]; |
||
1126 | break; |
||
1127 | case GLSL_TYPE_BOOL: |
||
1128 | data.b[c] = op[0]->value.b[c] == op[1]->value.b[c]; |
||
1129 | break; |
||
1130 | default: |
||
1131 | assert(!"Should not get here."); |
||
1132 | } |
||
1133 | } |
||
1134 | } else if (strcmp(callee, "exp") == 0) { |
||
1135 | expr = new(mem_ctx) ir_expression(ir_unop_exp, type, op[0], NULL); |
||
1136 | } else if (strcmp(callee, "exp2") == 0) { |
||
1137 | expr = new(mem_ctx) ir_expression(ir_unop_exp2, type, op[0], NULL); |
||
1138 | } else if (strcmp(callee, "faceforward") == 0) { |
||
1139 | if (dot(op[2], op[1]) < 0) |
||
1140 | return op[0]; |
||
1141 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1142 | data.f[c] = -op[0]->value.f[c]; |
||
1143 | } else if (strcmp(callee, "floor") == 0) { |
||
1144 | expr = new(mem_ctx) ir_expression(ir_unop_floor, type, op[0], NULL); |
||
1145 | } else if (strcmp(callee, "fract") == 0) { |
||
1146 | expr = new(mem_ctx) ir_expression(ir_unop_fract, type, op[0], NULL); |
||
1147 | } else if (strcmp(callee, "fwidth") == 0) { |
||
1148 | return ir_constant::zero(mem_ctx, this->type); |
||
1149 | } else if (strcmp(callee, "greaterThan") == 0) { |
||
1150 | assert(op[0]->type->is_vector() && op[1] && op[1]->type->is_vector()); |
||
1151 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
1152 | switch (op[0]->type->base_type) { |
||
1153 | case GLSL_TYPE_UINT: |
||
1154 | data.b[c] = op[0]->value.u[c] > op[1]->value.u[c]; |
||
1155 | break; |
||
1156 | case GLSL_TYPE_INT: |
||
1157 | data.b[c] = op[0]->value.i[c] > op[1]->value.i[c]; |
||
1158 | break; |
||
1159 | case GLSL_TYPE_FLOAT: |
||
1160 | data.b[c] = op[0]->value.f[c] > op[1]->value.f[c]; |
||
1161 | break; |
||
1162 | default: |
||
1163 | assert(!"Should not get here."); |
||
1164 | } |
||
1165 | } |
||
1166 | } else if (strcmp(callee, "greaterThanEqual") == 0) { |
||
1167 | assert(op[0]->type->is_vector() && op[1] && op[1]->type->is_vector()); |
||
1168 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
1169 | switch (op[0]->type->base_type) { |
||
1170 | case GLSL_TYPE_UINT: |
||
1171 | data.b[c] = op[0]->value.u[c] >= op[1]->value.u[c]; |
||
1172 | break; |
||
1173 | case GLSL_TYPE_INT: |
||
1174 | data.b[c] = op[0]->value.i[c] >= op[1]->value.i[c]; |
||
1175 | break; |
||
1176 | case GLSL_TYPE_FLOAT: |
||
1177 | data.b[c] = op[0]->value.f[c] >= op[1]->value.f[c]; |
||
1178 | break; |
||
1179 | default: |
||
1180 | assert(!"Should not get here."); |
||
1181 | } |
||
1182 | } |
||
1183 | } else if (strcmp(callee, "inversesqrt") == 0) { |
||
1184 | expr = new(mem_ctx) ir_expression(ir_unop_rsq, type, op[0], NULL); |
||
1185 | } else if (strcmp(callee, "length") == 0) { |
||
1186 | return new(mem_ctx) ir_constant(sqrtf(dot(op[0], op[0]))); |
||
1187 | } else if (strcmp(callee, "lessThan") == 0) { |
||
1188 | assert(op[0]->type->is_vector() && op[1] && op[1]->type->is_vector()); |
||
1189 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
1190 | switch (op[0]->type->base_type) { |
||
1191 | case GLSL_TYPE_UINT: |
||
1192 | data.b[c] = op[0]->value.u[c] < op[1]->value.u[c]; |
||
1193 | break; |
||
1194 | case GLSL_TYPE_INT: |
||
1195 | data.b[c] = op[0]->value.i[c] < op[1]->value.i[c]; |
||
1196 | break; |
||
1197 | case GLSL_TYPE_FLOAT: |
||
1198 | data.b[c] = op[0]->value.f[c] < op[1]->value.f[c]; |
||
1199 | break; |
||
1200 | default: |
||
1201 | assert(!"Should not get here."); |
||
1202 | } |
||
1203 | } |
||
1204 | } else if (strcmp(callee, "lessThanEqual") == 0) { |
||
1205 | assert(op[0]->type->is_vector() && op[1] && op[1]->type->is_vector()); |
||
1206 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
1207 | switch (op[0]->type->base_type) { |
||
1208 | case GLSL_TYPE_UINT: |
||
1209 | data.b[c] = op[0]->value.u[c] <= op[1]->value.u[c]; |
||
1210 | break; |
||
1211 | case GLSL_TYPE_INT: |
||
1212 | data.b[c] = op[0]->value.i[c] <= op[1]->value.i[c]; |
||
1213 | break; |
||
1214 | case GLSL_TYPE_FLOAT: |
||
1215 | data.b[c] = op[0]->value.f[c] <= op[1]->value.f[c]; |
||
1216 | break; |
||
1217 | default: |
||
1218 | assert(!"Should not get here."); |
||
1219 | } |
||
1220 | } |
||
1221 | } else if (strcmp(callee, "log") == 0) { |
||
1222 | expr = new(mem_ctx) ir_expression(ir_unop_log, type, op[0], NULL); |
||
1223 | } else if (strcmp(callee, "log2") == 0) { |
||
1224 | expr = new(mem_ctx) ir_expression(ir_unop_log2, type, op[0], NULL); |
||
1225 | } else if (strcmp(callee, "matrixCompMult") == 0) { |
||
1226 | assert(op[0]->type->is_float() && op[1]->type->is_float()); |
||
1227 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1228 | data.f[c] = op[0]->value.f[c] * op[1]->value.f[c]; |
||
1229 | } else if (strcmp(callee, "max") == 0) { |
||
1230 | expr = new(mem_ctx) ir_expression(ir_binop_max, type, op[0], op[1]); |
||
1231 | } else if (strcmp(callee, "min") == 0) { |
||
1232 | expr = new(mem_ctx) ir_expression(ir_binop_min, type, op[0], op[1]); |
||
1233 | } else if (strcmp(callee, "mix") == 0) { |
||
1234 | assert(op[0]->type->is_float() && op[1]->type->is_float()); |
||
1235 | if (op[2]->type->is_float()) { |
||
1236 | unsigned c2_inc = op[2]->type->is_scalar() ? 0 : 1; |
||
1237 | unsigned components = op[0]->type->components(); |
||
1238 | for (unsigned c = 0, c2 = 0; c < components; c2 += c2_inc, c++) { |
||
1239 | data.f[c] = op[0]->value.f[c] * (1 - op[2]->value.f[c2]) + |
||
1240 | op[1]->value.f[c] * op[2]->value.f[c2]; |
||
1241 | } |
||
1242 | } else { |
||
1243 | assert(op[2]->type->is_boolean()); |
||
1244 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1245 | data.f[c] = op[op[2]->value.b[c] ? 1 : 0]->value.f[c]; |
||
1246 | } |
||
1247 | } else if (strcmp(callee, "mod") == 0) { |
||
1248 | expr = new(mem_ctx) ir_expression(ir_binop_mod, type, op[0], op[1]); |
||
1249 | } else if (strcmp(callee, "normalize") == 0) { |
||
1250 | assert(op[0]->type->is_float()); |
||
1251 | float length = sqrtf(dot(op[0], op[0])); |
||
1252 | |||
1253 | if (length == 0) |
||
1254 | return ir_constant::zero(mem_ctx, this->type); |
||
1255 | |||
1256 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1257 | data.f[c] = op[0]->value.f[c] / length; |
||
1258 | } else if (strcmp(callee, "not") == 0) { |
||
1259 | expr = new(mem_ctx) ir_expression(ir_unop_logic_not, type, op[0], NULL); |
||
1260 | } else if (strcmp(callee, "notEqual") == 0) { |
||
1261 | assert(op[0]->type->is_vector() && op[1] && op[1]->type->is_vector()); |
||
1262 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
||
1263 | switch (op[0]->type->base_type) { |
||
1264 | case GLSL_TYPE_UINT: |
||
1265 | data.b[c] = op[0]->value.u[c] != op[1]->value.u[c]; |
||
1266 | break; |
||
1267 | case GLSL_TYPE_INT: |
||
1268 | data.b[c] = op[0]->value.i[c] != op[1]->value.i[c]; |
||
1269 | break; |
||
1270 | case GLSL_TYPE_FLOAT: |
||
1271 | data.b[c] = op[0]->value.f[c] != op[1]->value.f[c]; |
||
1272 | break; |
||
1273 | case GLSL_TYPE_BOOL: |
||
1274 | data.b[c] = op[0]->value.b[c] != op[1]->value.b[c]; |
||
1275 | break; |
||
1276 | default: |
||
1277 | assert(!"Should not get here."); |
||
1278 | } |
||
1279 | } |
||
1280 | } else if (strcmp(callee, "outerProduct") == 0) { |
||
1281 | assert(op[0]->type->is_vector() && op[1]->type->is_vector()); |
||
1282 | const unsigned m = op[0]->type->vector_elements; |
||
1283 | const unsigned n = op[1]->type->vector_elements; |
||
1284 | for (unsigned j = 0; j < n; j++) { |
||
1285 | for (unsigned i = 0; i < m; i++) { |
||
1286 | data.f[i+m*j] = op[0]->value.f[i] * op[1]->value.f[j]; |
||
1287 | } |
||
1288 | } |
||
1289 | } else if (strcmp(callee, "pow") == 0) { |
||
1290 | expr = new(mem_ctx) ir_expression(ir_binop_pow, type, op[0], op[1]); |
||
1291 | } else if (strcmp(callee, "radians") == 0) { |
||
1292 | assert(op[0]->type->is_float()); |
||
1293 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1294 | data.f[c] = M_PI / 180.0F * op[0]->value.f[c]; |
||
1295 | } else if (strcmp(callee, "reflect") == 0) { |
||
1296 | assert(op[0]->type->is_float()); |
||
1297 | float dot_NI = dot(op[1], op[0]); |
||
1298 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1299 | data.f[c] = op[0]->value.f[c] - 2 * dot_NI * op[1]->value.f[c]; |
||
1300 | } else if (strcmp(callee, "refract") == 0) { |
||
1301 | const float eta = op[2]->value.f[0]; |
||
1302 | const float dot_NI = dot(op[1], op[0]); |
||
1303 | const float k = 1.0F - eta * eta * (1.0F - dot_NI * dot_NI); |
||
1304 | if (k < 0.0) { |
||
1305 | return ir_constant::zero(mem_ctx, this->type); |
||
1306 | } else { |
||
1307 | for (unsigned c = 0; c < type->components(); c++) { |
||
1308 | data.f[c] = eta * op[0]->value.f[c] - (eta * dot_NI + sqrtf(k)) |
||
1309 | * op[1]->value.f[c]; |
||
1310 | } |
||
1311 | } |
||
1312 | } else if (strcmp(callee, "sign") == 0) { |
||
1313 | expr = new(mem_ctx) ir_expression(ir_unop_sign, type, op[0], NULL); |
||
1314 | } else if (strcmp(callee, "sin") == 0) { |
||
1315 | expr = new(mem_ctx) ir_expression(ir_unop_sin, type, op[0], NULL); |
||
1316 | } else if (strcmp(callee, "sinh") == 0) { |
||
1317 | assert(op[0]->type->is_float()); |
||
1318 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1319 | data.f[c] = sinhf(op[0]->value.f[c]); |
||
1320 | } else if (strcmp(callee, "smoothstep") == 0) { |
||
1321 | assert(num_parameters == 3); |
||
1322 | assert(op[1]->type == op[0]->type); |
||
1323 | unsigned edge_inc = op[0]->type->is_scalar() ? 0 : 1; |
||
1324 | for (unsigned c = 0, e = 0; c < type->components(); e += edge_inc, c++) { |
||
1325 | const float edge0 = op[0]->value.f[e]; |
||
1326 | const float edge1 = op[1]->value.f[e]; |
||
1327 | if (edge0 == edge1) { |
||
1328 | data.f[c] = 0.0; /* Avoid a crash - results are undefined anyway */ |
||
1329 | } else { |
||
1330 | const float numerator = op[2]->value.f[c] - edge0; |
||
1331 | const float denominator = edge1 - edge0; |
||
1332 | const float t = CLAMP(numerator/denominator, 0, 1); |
||
1333 | data.f[c] = t * t * (3 - 2 * t); |
||
1334 | } |
||
1335 | } |
||
1336 | } else if (strcmp(callee, "sqrt") == 0) { |
||
1337 | expr = new(mem_ctx) ir_expression(ir_unop_sqrt, type, op[0], NULL); |
||
1338 | } else if (strcmp(callee, "step") == 0) { |
||
1339 | assert(op[0]->type->is_float() && op[1]->type->is_float()); |
||
1340 | /* op[0] (edge) may be either a scalar or a vector */ |
||
1341 | const unsigned c0_inc = op[0]->type->is_scalar() ? 0 : 1; |
||
1342 | for (unsigned c = 0, c0 = 0; c < type->components(); c0 += c0_inc, c++) |
||
1343 | data.f[c] = (op[1]->value.f[c] < op[0]->value.f[c0]) ? 0.0F : 1.0F; |
||
1344 | } else if (strcmp(callee, "tan") == 0) { |
||
1345 | assert(op[0]->type->is_float()); |
||
1346 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1347 | data.f[c] = tanf(op[0]->value.f[c]); |
||
1348 | } else if (strcmp(callee, "tanh") == 0) { |
||
1349 | assert(op[0]->type->is_float()); |
||
1350 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
||
1351 | data.f[c] = tanhf(op[0]->value.f[c]); |
||
1352 | } else if (strcmp(callee, "transpose") == 0) { |
||
1353 | assert(op[0]->type->is_matrix()); |
||
1354 | const unsigned n = op[0]->type->vector_elements; |
||
1355 | const unsigned m = op[0]->type->matrix_columns; |
||
1356 | for (unsigned j = 0; j < m; j++) { |
||
1357 | for (unsigned i = 0; i < n; i++) { |
||
1358 | data.f[m*i+j] += op[0]->value.f[i+n*j]; |
||
1359 | } |
||
1360 | } |
||
1361 | } else { |
||
1362 | /* Unsupported builtin - some are not allowed in constant expressions. */ |
||
1363 | return NULL; |
||
1364 | } |
||
1365 | |||
1366 | if (expr != NULL) |
||
1367 | return expr->constant_expression_value(); |
||
1368 | |||
1369 | return new(mem_ctx) ir_constant(this->type, &data); |
||
1370 | }>>>>>>>>>>>>>>>>>>>=>=>=>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><>><>><>><>>>>>=>=>=>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |