mesa: Remove _mesa_max_buffer_index
[mesa.git] / src / glsl / ir_builder.cpp
1 /*
2 * Copyright © 2012 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "ir_builder.h"
25 #include "program/prog_instruction.h"
26
27 using namespace ir_builder;
28
29 namespace ir_builder {
30
31 void
32 ir_factory::emit(ir_instruction *ir)
33 {
34 instructions->push_tail(ir);
35 }
36
37 ir_variable *
38 ir_factory::make_temp(const glsl_type *type, const char *name)
39 {
40 ir_variable *var;
41
42 var = new(mem_ctx) ir_variable(type, name, ir_var_temporary);
43 emit(var);
44
45 return var;
46 }
47
48 ir_assignment *
49 assign(deref lhs, operand rhs, operand condition, int writemask)
50 {
51 void *mem_ctx = ralloc_parent(lhs.val);
52
53 ir_assignment *assign = new(mem_ctx) ir_assignment(lhs.val,
54 rhs.val,
55 condition.val,
56 writemask);
57
58 return assign;
59 }
60
61 ir_assignment *
62 assign(deref lhs, operand rhs)
63 {
64 return assign(lhs, rhs, (1 << lhs.val->type->vector_elements) - 1);
65 }
66
67 ir_assignment *
68 assign(deref lhs, operand rhs, int writemask)
69 {
70 return assign(lhs, rhs, (ir_rvalue *) NULL, writemask);
71 }
72
73 ir_assignment *
74 assign(deref lhs, operand rhs, operand condition)
75 {
76 return assign(lhs, rhs, condition, (1 << lhs.val->type->vector_elements) - 1);
77 }
78
79 ir_return *
80 ret(operand retval)
81 {
82 void *mem_ctx = ralloc_parent(retval.val);
83 return new(mem_ctx) ir_return(retval.val);
84 }
85
86 ir_swizzle *
87 swizzle(operand a, int swizzle, int components)
88 {
89 void *mem_ctx = ralloc_parent(a.val);
90
91 return new(mem_ctx) ir_swizzle(a.val,
92 GET_SWZ(swizzle, 0),
93 GET_SWZ(swizzle, 1),
94 GET_SWZ(swizzle, 2),
95 GET_SWZ(swizzle, 3),
96 components);
97 }
98
99 ir_swizzle *
100 swizzle_for_size(operand a, unsigned components)
101 {
102 void *mem_ctx = ralloc_parent(a.val);
103
104 if (a.val->type->vector_elements < components)
105 components = a.val->type->vector_elements;
106
107 unsigned s[4] = { 0, 1, 2, 3 };
108 for (int i = components; i < 4; i++)
109 s[i] = components - 1;
110
111 return new(mem_ctx) ir_swizzle(a.val, s, components);
112 }
113
114 ir_swizzle *
115 swizzle_xxxx(operand a)
116 {
117 return swizzle(a, SWIZZLE_XXXX, 4);
118 }
119
120 ir_swizzle *
121 swizzle_yyyy(operand a)
122 {
123 return swizzle(a, SWIZZLE_YYYY, 4);
124 }
125
126 ir_swizzle *
127 swizzle_zzzz(operand a)
128 {
129 return swizzle(a, SWIZZLE_ZZZZ, 4);
130 }
131
132 ir_swizzle *
133 swizzle_wwww(operand a)
134 {
135 return swizzle(a, SWIZZLE_WWWW, 4);
136 }
137
138 ir_swizzle *
139 swizzle_x(operand a)
140 {
141 return swizzle(a, SWIZZLE_XXXX, 1);
142 }
143
144 ir_swizzle *
145 swizzle_y(operand a)
146 {
147 return swizzle(a, SWIZZLE_YYYY, 1);
148 }
149
150 ir_swizzle *
151 swizzle_z(operand a)
152 {
153 return swizzle(a, SWIZZLE_ZZZZ, 1);
154 }
155
156 ir_swizzle *
157 swizzle_w(operand a)
158 {
159 return swizzle(a, SWIZZLE_WWWW, 1);
160 }
161
162 ir_swizzle *
163 swizzle_xy(operand a)
164 {
165 return swizzle(a, SWIZZLE_XYZW, 2);
166 }
167
168 ir_swizzle *
169 swizzle_xyz(operand a)
170 {
171 return swizzle(a, SWIZZLE_XYZW, 3);
172 }
173
174 ir_swizzle *
175 swizzle_xyzw(operand a)
176 {
177 return swizzle(a, SWIZZLE_XYZW, 4);
178 }
179
180 ir_expression *
181 expr(ir_expression_operation op, operand a)
182 {
183 void *mem_ctx = ralloc_parent(a.val);
184
185 return new(mem_ctx) ir_expression(op, a.val);
186 }
187
188 ir_expression *
189 expr(ir_expression_operation op, operand a, operand b)
190 {
191 void *mem_ctx = ralloc_parent(a.val);
192
193 return new(mem_ctx) ir_expression(op, a.val, b.val);
194 }
195
196 ir_expression *
197 expr(ir_expression_operation op, operand a, operand b, operand c)
198 {
199 void *mem_ctx = ralloc_parent(a.val);
200
201 return new(mem_ctx) ir_expression(op, a.val, b.val, c.val);
202 }
203
204 ir_expression *add(operand a, operand b)
205 {
206 return expr(ir_binop_add, a, b);
207 }
208
209 ir_expression *sub(operand a, operand b)
210 {
211 return expr(ir_binop_sub, a, b);
212 }
213
214 ir_expression *min2(operand a, operand b)
215 {
216 return expr(ir_binop_min, a, b);
217 }
218
219 ir_expression *max2(operand a, operand b)
220 {
221 return expr(ir_binop_max, a, b);
222 }
223
224 ir_expression *mul(operand a, operand b)
225 {
226 return expr(ir_binop_mul, a, b);
227 }
228
229 ir_expression *imul_high(operand a, operand b)
230 {
231 return expr(ir_binop_imul_high, a, b);
232 }
233
234 ir_expression *div(operand a, operand b)
235 {
236 return expr(ir_binop_div, a, b);
237 }
238
239 ir_expression *carry(operand a, operand b)
240 {
241 return expr(ir_binop_carry, a, b);
242 }
243
244 ir_expression *borrow(operand a, operand b)
245 {
246 return expr(ir_binop_borrow, a, b);
247 }
248
249 ir_expression *round_even(operand a)
250 {
251 return expr(ir_unop_round_even, a);
252 }
253
254 /* dot for vectors, mul for scalars */
255 ir_expression *dot(operand a, operand b)
256 {
257 assert(a.val->type == b.val->type);
258
259 if (a.val->type->vector_elements == 1)
260 return expr(ir_binop_mul, a, b);
261
262 return expr(ir_binop_dot, a, b);
263 }
264
265 ir_expression*
266 clamp(operand a, operand b, operand c)
267 {
268 return expr(ir_binop_min, expr(ir_binop_max, a, b), c);
269 }
270
271 ir_expression *
272 saturate(operand a)
273 {
274 return expr(ir_unop_saturate, a);
275 }
276
277 ir_expression *
278 abs(operand a)
279 {
280 return expr(ir_unop_abs, a);
281 }
282
283 ir_expression *
284 neg(operand a)
285 {
286 return expr(ir_unop_neg, a);
287 }
288
289 ir_expression *
290 sin(operand a)
291 {
292 return expr(ir_unop_sin, a);
293 }
294
295 ir_expression *
296 cos(operand a)
297 {
298 return expr(ir_unop_cos, a);
299 }
300
301 ir_expression *
302 exp(operand a)
303 {
304 return expr(ir_unop_exp, a);
305 }
306
307 ir_expression *
308 rsq(operand a)
309 {
310 return expr(ir_unop_rsq, a);
311 }
312
313 ir_expression *
314 sqrt(operand a)
315 {
316 return expr(ir_unop_sqrt, a);
317 }
318
319 ir_expression *
320 log(operand a)
321 {
322 return expr(ir_unop_log, a);
323 }
324
325 ir_expression *
326 sign(operand a)
327 {
328 return expr(ir_unop_sign, a);
329 }
330
331 ir_expression*
332 equal(operand a, operand b)
333 {
334 return expr(ir_binop_equal, a, b);
335 }
336
337 ir_expression*
338 nequal(operand a, operand b)
339 {
340 return expr(ir_binop_nequal, a, b);
341 }
342
343 ir_expression*
344 less(operand a, operand b)
345 {
346 return expr(ir_binop_less, a, b);
347 }
348
349 ir_expression*
350 greater(operand a, operand b)
351 {
352 return expr(ir_binop_greater, a, b);
353 }
354
355 ir_expression*
356 lequal(operand a, operand b)
357 {
358 return expr(ir_binop_lequal, a, b);
359 }
360
361 ir_expression*
362 gequal(operand a, operand b)
363 {
364 return expr(ir_binop_gequal, a, b);
365 }
366
367 ir_expression*
368 logic_not(operand a)
369 {
370 return expr(ir_unop_logic_not, a);
371 }
372
373 ir_expression*
374 logic_and(operand a, operand b)
375 {
376 return expr(ir_binop_logic_and, a, b);
377 }
378
379 ir_expression*
380 logic_or(operand a, operand b)
381 {
382 return expr(ir_binop_logic_or, a, b);
383 }
384
385 ir_expression*
386 bit_not(operand a)
387 {
388 return expr(ir_unop_bit_not, a);
389 }
390
391 ir_expression*
392 bit_and(operand a, operand b)
393 {
394 return expr(ir_binop_bit_and, a, b);
395 }
396
397 ir_expression*
398 bit_or(operand a, operand b)
399 {
400 return expr(ir_binop_bit_or, a, b);
401 }
402
403 ir_expression*
404 lshift(operand a, operand b)
405 {
406 return expr(ir_binop_lshift, a, b);
407 }
408
409 ir_expression*
410 rshift(operand a, operand b)
411 {
412 return expr(ir_binop_rshift, a, b);
413 }
414
415 ir_expression*
416 f2i(operand a)
417 {
418 return expr(ir_unop_f2i, a);
419 }
420
421 ir_expression*
422 bitcast_f2i(operand a)
423 {
424 return expr(ir_unop_bitcast_f2i, a);
425 }
426
427 ir_expression*
428 i2f(operand a)
429 {
430 return expr(ir_unop_i2f, a);
431 }
432
433 ir_expression*
434 bitcast_i2f(operand a)
435 {
436 return expr(ir_unop_bitcast_i2f, a);
437 }
438
439 ir_expression*
440 i2u(operand a)
441 {
442 return expr(ir_unop_i2u, a);
443 }
444
445 ir_expression*
446 u2i(operand a)
447 {
448 return expr(ir_unop_u2i, a);
449 }
450
451 ir_expression*
452 f2u(operand a)
453 {
454 return expr(ir_unop_f2u, a);
455 }
456
457 ir_expression*
458 bitcast_f2u(operand a)
459 {
460 return expr(ir_unop_bitcast_f2u, a);
461 }
462
463 ir_expression*
464 u2f(operand a)
465 {
466 return expr(ir_unop_u2f, a);
467 }
468
469 ir_expression*
470 bitcast_u2f(operand a)
471 {
472 return expr(ir_unop_bitcast_u2f, a);
473 }
474
475 ir_expression*
476 i2b(operand a)
477 {
478 return expr(ir_unop_i2b, a);
479 }
480
481 ir_expression*
482 b2i(operand a)
483 {
484 return expr(ir_unop_b2i, a);
485 }
486
487 ir_expression *
488 f2b(operand a)
489 {
490 return expr(ir_unop_f2b, a);
491 }
492
493 ir_expression *
494 b2f(operand a)
495 {
496 return expr(ir_unop_b2f, a);
497 }
498
499 ir_expression *
500 interpolate_at_centroid(operand a)
501 {
502 return expr(ir_unop_interpolate_at_centroid, a);
503 }
504
505 ir_expression *
506 interpolate_at_offset(operand a, operand b)
507 {
508 return expr(ir_binop_interpolate_at_offset, a, b);
509 }
510
511 ir_expression *
512 interpolate_at_sample(operand a, operand b)
513 {
514 return expr(ir_binop_interpolate_at_sample, a, b);
515 }
516
517 ir_expression *
518 fma(operand a, operand b, operand c)
519 {
520 return expr(ir_triop_fma, a, b, c);
521 }
522
523 ir_expression *
524 lrp(operand x, operand y, operand a)
525 {
526 return expr(ir_triop_lrp, x, y, a);
527 }
528
529 ir_expression *
530 csel(operand a, operand b, operand c)
531 {
532 return expr(ir_triop_csel, a, b, c);
533 }
534
535 ir_expression *
536 bitfield_insert(operand a, operand b, operand c, operand d)
537 {
538 void *mem_ctx = ralloc_parent(a.val);
539 return new(mem_ctx) ir_expression(ir_quadop_bitfield_insert,
540 a.val->type, a.val, b.val, c.val, d.val);
541 }
542
543 ir_if*
544 if_tree(operand condition,
545 ir_instruction *then_branch)
546 {
547 assert(then_branch != NULL);
548
549 void *mem_ctx = ralloc_parent(condition.val);
550
551 ir_if *result = new(mem_ctx) ir_if(condition.val);
552 result->then_instructions.push_tail(then_branch);
553 return result;
554 }
555
556 ir_if*
557 if_tree(operand condition,
558 ir_instruction *then_branch,
559 ir_instruction *else_branch)
560 {
561 assert(then_branch != NULL);
562 assert(else_branch != NULL);
563
564 void *mem_ctx = ralloc_parent(condition.val);
565
566 ir_if *result = new(mem_ctx) ir_if(condition.val);
567 result->then_instructions.push_tail(then_branch);
568 result->else_instructions.push_tail(else_branch);
569 return result;
570 }
571
572 } /* namespace ir_builder */