glsl: Add IR builder support for conditional assignments.
[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_swizzle *
80 swizzle(operand a, int swizzle, int components)
81 {
82 void *mem_ctx = ralloc_parent(a.val);
83
84 return new(mem_ctx) ir_swizzle(a.val,
85 GET_SWZ(swizzle, 0),
86 GET_SWZ(swizzle, 1),
87 GET_SWZ(swizzle, 2),
88 GET_SWZ(swizzle, 3),
89 components);
90 }
91
92 ir_swizzle *
93 swizzle_for_size(operand a, unsigned components)
94 {
95 void *mem_ctx = ralloc_parent(a.val);
96
97 if (a.val->type->vector_elements < components)
98 components = a.val->type->vector_elements;
99
100 unsigned s[4] = { 0, 1, 2, 3 };
101 for (int i = components; i < 4; i++)
102 s[i] = components - 1;
103
104 return new(mem_ctx) ir_swizzle(a.val, s, components);
105 }
106
107 ir_swizzle *
108 swizzle_xxxx(operand a)
109 {
110 return swizzle(a, SWIZZLE_XXXX, 4);
111 }
112
113 ir_swizzle *
114 swizzle_yyyy(operand a)
115 {
116 return swizzle(a, SWIZZLE_YYYY, 4);
117 }
118
119 ir_swizzle *
120 swizzle_zzzz(operand a)
121 {
122 return swizzle(a, SWIZZLE_ZZZZ, 4);
123 }
124
125 ir_swizzle *
126 swizzle_wwww(operand a)
127 {
128 return swizzle(a, SWIZZLE_WWWW, 4);
129 }
130
131 ir_swizzle *
132 swizzle_x(operand a)
133 {
134 return swizzle(a, SWIZZLE_XXXX, 1);
135 }
136
137 ir_swizzle *
138 swizzle_y(operand a)
139 {
140 return swizzle(a, SWIZZLE_YYYY, 1);
141 }
142
143 ir_swizzle *
144 swizzle_z(operand a)
145 {
146 return swizzle(a, SWIZZLE_ZZZZ, 1);
147 }
148
149 ir_swizzle *
150 swizzle_w(operand a)
151 {
152 return swizzle(a, SWIZZLE_WWWW, 1);
153 }
154
155 ir_swizzle *
156 swizzle_xy(operand a)
157 {
158 return swizzle(a, SWIZZLE_XYZW, 2);
159 }
160
161 ir_swizzle *
162 swizzle_xyz(operand a)
163 {
164 return swizzle(a, SWIZZLE_XYZW, 3);
165 }
166
167 ir_swizzle *
168 swizzle_xyzw(operand a)
169 {
170 return swizzle(a, SWIZZLE_XYZW, 4);
171 }
172
173 ir_expression *
174 expr(ir_expression_operation op, operand a)
175 {
176 void *mem_ctx = ralloc_parent(a.val);
177
178 return new(mem_ctx) ir_expression(op, a.val);
179 }
180
181 ir_expression *
182 expr(ir_expression_operation op, operand a, operand b)
183 {
184 void *mem_ctx = ralloc_parent(a.val);
185
186 return new(mem_ctx) ir_expression(op, a.val, b.val);
187 }
188
189 ir_expression *
190 expr(ir_expression_operation op, operand a, operand b, operand c)
191 {
192 void *mem_ctx = ralloc_parent(a.val);
193
194 return new(mem_ctx) ir_expression(op, a.val, b.val, c.val);
195 }
196
197 ir_expression *add(operand a, operand b)
198 {
199 return expr(ir_binop_add, a, b);
200 }
201
202 ir_expression *sub(operand a, operand b)
203 {
204 return expr(ir_binop_sub, a, b);
205 }
206
207 ir_expression *mul(operand a, operand b)
208 {
209 return expr(ir_binop_mul, a, b);
210 }
211
212 ir_expression *div(operand a, operand b)
213 {
214 return expr(ir_binop_div, a, b);
215 }
216
217 ir_expression *round_even(operand a)
218 {
219 return expr(ir_unop_round_even, a);
220 }
221
222 ir_expression *dot(operand a, operand b)
223 {
224 return expr(ir_binop_dot, a, b);
225 }
226
227 ir_expression*
228 clamp(operand a, operand b, operand c)
229 {
230 return expr(ir_binop_min, expr(ir_binop_max, a, b), c);
231 }
232
233 ir_expression *
234 saturate(operand a)
235 {
236 void *mem_ctx = ralloc_parent(a.val);
237
238 return expr(ir_binop_max,
239 expr(ir_binop_min, a, new(mem_ctx) ir_constant(1.0f)),
240 new(mem_ctx) ir_constant(0.0f));
241 }
242
243 ir_expression *
244 abs(operand a)
245 {
246 return expr(ir_unop_abs, a);
247 }
248
249 ir_expression*
250 equal(operand a, operand b)
251 {
252 return expr(ir_binop_equal, a, b);
253 }
254
255 ir_expression*
256 nequal(operand a, operand b)
257 {
258 return expr(ir_binop_nequal, a, b);
259 }
260
261 ir_expression*
262 less(operand a, operand b)
263 {
264 return expr(ir_binop_less, a, b);
265 }
266
267 ir_expression*
268 greater(operand a, operand b)
269 {
270 return expr(ir_binop_greater, a, b);
271 }
272
273 ir_expression*
274 lequal(operand a, operand b)
275 {
276 return expr(ir_binop_lequal, a, b);
277 }
278
279 ir_expression*
280 gequal(operand a, operand b)
281 {
282 return expr(ir_binop_gequal, a, b);
283 }
284
285 ir_expression*
286 logic_not(operand a)
287 {
288 return expr(ir_unop_logic_not, a);
289 }
290
291 ir_expression*
292 logic_and(operand a, operand b)
293 {
294 return expr(ir_binop_logic_and, a, b);
295 }
296
297 ir_expression*
298 logic_or(operand a, operand b)
299 {
300 return expr(ir_binop_logic_or, a, b);
301 }
302
303 ir_expression*
304 bit_not(operand a)
305 {
306 return expr(ir_unop_bit_not, a);
307 }
308
309 ir_expression*
310 bit_and(operand a, operand b)
311 {
312 return expr(ir_binop_bit_and, a, b);
313 }
314
315 ir_expression*
316 bit_or(operand a, operand b)
317 {
318 return expr(ir_binop_bit_or, a, b);
319 }
320
321 ir_expression*
322 lshift(operand a, operand b)
323 {
324 return expr(ir_binop_lshift, a, b);
325 }
326
327 ir_expression*
328 rshift(operand a, operand b)
329 {
330 return expr(ir_binop_rshift, a, b);
331 }
332
333 ir_expression*
334 f2i(operand a)
335 {
336 return expr(ir_unop_f2i, a);
337 }
338
339 ir_expression*
340 bitcast_f2i(operand a)
341 {
342 return expr(ir_unop_bitcast_f2i, a);
343 }
344
345 ir_expression*
346 i2f(operand a)
347 {
348 return expr(ir_unop_i2f, a);
349 }
350
351 ir_expression*
352 bitcast_i2f(operand a)
353 {
354 return expr(ir_unop_bitcast_i2f, a);
355 }
356
357 ir_expression*
358 i2u(operand a)
359 {
360 return expr(ir_unop_i2u, a);
361 }
362
363 ir_expression*
364 u2i(operand a)
365 {
366 return expr(ir_unop_u2i, a);
367 }
368
369 ir_expression*
370 f2u(operand a)
371 {
372 return expr(ir_unop_f2u, a);
373 }
374
375 ir_expression*
376 bitcast_f2u(operand a)
377 {
378 return expr(ir_unop_bitcast_f2u, a);
379 }
380
381 ir_expression*
382 u2f(operand a)
383 {
384 return expr(ir_unop_u2f, a);
385 }
386
387 ir_expression*
388 bitcast_u2f(operand a)
389 {
390 return expr(ir_unop_bitcast_u2f, a);
391 }
392
393 ir_expression*
394 i2b(operand a)
395 {
396 return expr(ir_unop_i2b, a);
397 }
398
399 ir_expression*
400 b2i(operand a)
401 {
402 return expr(ir_unop_b2i, a);
403 }
404
405 ir_if*
406 if_tree(operand condition,
407 ir_instruction *then_branch)
408 {
409 assert(then_branch != NULL);
410
411 void *mem_ctx = ralloc_parent(condition.val);
412
413 ir_if *result = new(mem_ctx) ir_if(condition.val);
414 result->then_instructions.push_tail(then_branch);
415 return result;
416 }
417
418 ir_if*
419 if_tree(operand condition,
420 ir_instruction *then_branch,
421 ir_instruction *else_branch)
422 {
423 assert(then_branch != NULL);
424 assert(else_branch != NULL);
425
426 void *mem_ctx = ralloc_parent(condition.val);
427
428 ir_if *result = new(mem_ctx) ir_if(condition.val);
429 result->then_instructions.push_tail(then_branch);
430 result->else_instructions.push_tail(else_branch);
431 return result;
432 }
433
434 } /* namespace ir_builder */