nir: Don't produce nir_op_fexp from GLSL IR
[mesa.git] / src / glsl / nir / glsl_to_nir.cpp
1 /*
2 * Copyright © 2014 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 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "glsl_to_nir.h"
29 #include "ir_visitor.h"
30 #include "ir_hierarchical_visitor.h"
31 #include "ir.h"
32
33 /*
34 * pass to lower GLSL IR to NIR
35 *
36 * This will lower variable dereferences to loads/stores of corresponding
37 * variables in NIR - the variables will be converted to registers in a later
38 * pass.
39 */
40
41 namespace {
42
43 class nir_visitor : public ir_visitor
44 {
45 public:
46 nir_visitor(nir_shader *shader, gl_shader_stage stage);
47 ~nir_visitor();
48
49 virtual void visit(ir_variable *);
50 virtual void visit(ir_function *);
51 virtual void visit(ir_function_signature *);
52 virtual void visit(ir_loop *);
53 virtual void visit(ir_if *);
54 virtual void visit(ir_discard *);
55 virtual void visit(ir_loop_jump *);
56 virtual void visit(ir_return *);
57 virtual void visit(ir_call *);
58 virtual void visit(ir_assignment *);
59 virtual void visit(ir_emit_vertex *);
60 virtual void visit(ir_end_primitive *);
61 virtual void visit(ir_expression *);
62 virtual void visit(ir_swizzle *);
63 virtual void visit(ir_texture *);
64 virtual void visit(ir_constant *);
65 virtual void visit(ir_dereference_variable *);
66 virtual void visit(ir_dereference_record *);
67 virtual void visit(ir_dereference_array *);
68
69 void create_function(ir_function *ir);
70
71 private:
72 void create_overload(ir_function_signature *ir, nir_function *function);
73 void add_instr(nir_instr *instr, unsigned num_components);
74 nir_src evaluate_rvalue(ir_rvalue *ir);
75
76 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_src *srcs);
77 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_src src1);
78 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_src src1,
79 nir_src src2);
80 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_src src1,
81 nir_src src2, nir_src src3);
82
83 bool supports_ints;
84
85 nir_shader *shader;
86 gl_shader_stage stage;
87 nir_function_impl *impl;
88 exec_list *cf_node_list;
89 nir_instr *result; /* result of the expression tree last visited */
90
91 nir_deref_var *evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir);
92
93 /* the head of the dereference chain we're creating */
94 nir_deref_var *deref_head;
95 /* the tail of the dereference chain we're creating */
96 nir_deref *deref_tail;
97
98 nir_variable *var; /* variable created by ir_variable visitor */
99
100 /* whether the IR we're operating on is per-function or global */
101 bool is_global;
102
103 /* map of ir_variable -> nir_variable */
104 struct hash_table *var_table;
105
106 /* map of ir_function_signature -> nir_function_overload */
107 struct hash_table *overload_table;
108 };
109
110 /*
111 * This visitor runs before the main visitor, calling create_function() for
112 * each function so that the main visitor can resolve forward references in
113 * calls.
114 */
115
116 class nir_function_visitor : public ir_hierarchical_visitor
117 {
118 public:
119 nir_function_visitor(nir_visitor *v) : visitor(v)
120 {
121 }
122 virtual ir_visitor_status visit_enter(ir_function *);
123
124 private:
125 nir_visitor *visitor;
126 };
127
128 }; /* end of anonymous namespace */
129
130 nir_shader *
131 glsl_to_nir(struct gl_shader *sh, const nir_shader_compiler_options *options)
132 {
133 nir_shader *shader = nir_shader_create(NULL, options);
134
135 nir_visitor v1(shader, sh->Stage);
136 nir_function_visitor v2(&v1);
137 v2.run(sh->ir);
138 visit_exec_list(sh->ir, &v1);
139
140 return shader;
141 }
142
143 nir_visitor::nir_visitor(nir_shader *shader, gl_shader_stage stage)
144 {
145 this->supports_ints = shader->options->native_integers;
146 this->shader = shader;
147 this->stage = stage;
148 this->is_global = true;
149 this->var_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
150 _mesa_key_pointer_equal);
151 this->overload_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
152 _mesa_key_pointer_equal);
153 }
154
155 nir_visitor::~nir_visitor()
156 {
157 _mesa_hash_table_destroy(this->var_table, NULL);
158 _mesa_hash_table_destroy(this->overload_table, NULL);
159 }
160
161 nir_deref_var *
162 nir_visitor::evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir)
163 {
164 ir->accept(this);
165 ralloc_steal(mem_ctx, this->deref_head);
166 return this->deref_head;
167 }
168
169 static nir_constant *
170 constant_copy(ir_constant *ir, void *mem_ctx)
171 {
172 if (ir == NULL)
173 return NULL;
174
175 nir_constant *ret = ralloc(mem_ctx, nir_constant);
176
177 unsigned total_elems = ir->type->components();
178 unsigned i;
179 switch (ir->type->base_type) {
180 case GLSL_TYPE_UINT:
181 for (i = 0; i < total_elems; i++)
182 ret->value.u[i] = ir->value.u[i];
183 break;
184
185 case GLSL_TYPE_INT:
186 for (i = 0; i < total_elems; i++)
187 ret->value.i[i] = ir->value.i[i];
188 break;
189
190 case GLSL_TYPE_FLOAT:
191 for (i = 0; i < total_elems; i++)
192 ret->value.f[i] = ir->value.f[i];
193 break;
194
195 case GLSL_TYPE_BOOL:
196 for (i = 0; i < total_elems; i++)
197 ret->value.b[i] = ir->value.b[i];
198 break;
199
200 case GLSL_TYPE_STRUCT:
201 ret->elements = ralloc_array(mem_ctx, nir_constant *,
202 ir->type->length);
203 i = 0;
204 foreach_in_list(ir_constant, field, &ir->components) {
205 ret->elements[i] = constant_copy(field, mem_ctx);
206 i++;
207 }
208 break;
209
210 case GLSL_TYPE_ARRAY:
211 ret->elements = ralloc_array(mem_ctx, nir_constant *,
212 ir->type->length);
213
214 for (i = 0; i < ir->type->length; i++)
215 ret->elements[i] = constant_copy(ir->array_elements[i], mem_ctx);
216 break;
217
218 default:
219 unreachable("not reached");
220 }
221
222 return ret;
223 }
224
225 void
226 nir_visitor::visit(ir_variable *ir)
227 {
228 nir_variable *var = ralloc(shader, nir_variable);
229 var->type = ir->type;
230 var->name = ralloc_strdup(var, ir->name);
231
232 if (ir->is_interface_instance() && ir->get_max_ifc_array_access() != NULL) {
233 unsigned size = ir->get_interface_type()->length;
234 var->max_ifc_array_access = ralloc_array(var, unsigned, size);
235 memcpy(var->max_ifc_array_access, ir->get_max_ifc_array_access(),
236 size * sizeof(unsigned));
237 } else {
238 var->max_ifc_array_access = NULL;
239 }
240
241 var->data.read_only = ir->data.read_only;
242 var->data.centroid = ir->data.centroid;
243 var->data.sample = ir->data.sample;
244 var->data.invariant = ir->data.invariant;
245 var->data.location = ir->data.location;
246
247 switch(ir->data.mode) {
248 case ir_var_auto:
249 case ir_var_temporary:
250 if (is_global)
251 var->data.mode = nir_var_global;
252 else
253 var->data.mode = nir_var_local;
254 break;
255
256 case ir_var_function_in:
257 case ir_var_function_out:
258 case ir_var_function_inout:
259 case ir_var_const_in:
260 var->data.mode = nir_var_local;
261 break;
262
263 case ir_var_shader_in:
264 if (stage == MESA_SHADER_FRAGMENT &&
265 ir->data.location == VARYING_SLOT_FACE) {
266 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
267 var->data.location = SYSTEM_VALUE_FRONT_FACE;
268 var->data.mode = nir_var_system_value;
269 } else {
270 var->data.mode = nir_var_shader_in;
271 }
272 break;
273
274 case ir_var_shader_out:
275 var->data.mode = nir_var_shader_out;
276 break;
277
278 case ir_var_uniform:
279 var->data.mode = nir_var_uniform;
280 break;
281
282
283 case ir_var_system_value:
284 var->data.mode = nir_var_system_value;
285 break;
286
287 default:
288 unreachable("not reached");
289 }
290
291 var->data.interpolation = ir->data.interpolation;
292 var->data.origin_upper_left = ir->data.origin_upper_left;
293 var->data.pixel_center_integer = ir->data.pixel_center_integer;
294 var->data.explicit_location = ir->data.explicit_location;
295 var->data.explicit_index = ir->data.explicit_index;
296 var->data.explicit_binding = ir->data.explicit_binding;
297 var->data.has_initializer = ir->data.has_initializer;
298 var->data.is_unmatched_generic_inout = ir->data.is_unmatched_generic_inout;
299 var->data.location_frac = ir->data.location_frac;
300 var->data.from_named_ifc_block_array = ir->data.from_named_ifc_block_array;
301 var->data.from_named_ifc_block_nonarray = ir->data.from_named_ifc_block_nonarray;
302
303 switch (ir->data.depth_layout) {
304 case ir_depth_layout_none:
305 var->data.depth_layout = nir_depth_layout_none;
306 break;
307 case ir_depth_layout_any:
308 var->data.depth_layout = nir_depth_layout_any;
309 break;
310 case ir_depth_layout_greater:
311 var->data.depth_layout = nir_depth_layout_greater;
312 break;
313 case ir_depth_layout_less:
314 var->data.depth_layout = nir_depth_layout_less;
315 break;
316 case ir_depth_layout_unchanged:
317 var->data.depth_layout = nir_depth_layout_unchanged;
318 break;
319 default:
320 unreachable("not reached");
321 }
322
323 var->data.index = ir->data.index;
324 var->data.binding = ir->data.binding;
325 /* XXX Get rid of buffer_index */
326 var->data.atomic.buffer_index = ir->data.binding;
327 var->data.atomic.offset = ir->data.atomic.offset;
328 var->data.image.read_only = ir->data.image_read_only;
329 var->data.image.write_only = ir->data.image_write_only;
330 var->data.image.coherent = ir->data.image_coherent;
331 var->data.image._volatile = ir->data.image_volatile;
332 var->data.image.restrict_flag = ir->data.image_restrict;
333 var->data.image.format = ir->data.image_format;
334 var->data.max_array_access = ir->data.max_array_access;
335
336 var->num_state_slots = ir->get_num_state_slots();
337 if (var->num_state_slots > 0) {
338 var->state_slots = ralloc_array(var, nir_state_slot,
339 var->num_state_slots);
340
341 ir_state_slot *state_slots = ir->get_state_slots();
342 for (unsigned i = 0; i < var->num_state_slots; i++) {
343 for (unsigned j = 0; j < 5; j++)
344 var->state_slots[i].tokens[j] = state_slots[i].tokens[j];
345 var->state_slots[i].swizzle = state_slots[i].swizzle;
346 }
347 } else {
348 var->state_slots = NULL;
349 }
350
351 var->constant_initializer = constant_copy(ir->constant_initializer, var);
352
353 var->interface_type = ir->get_interface_type();
354
355 switch (var->data.mode) {
356 case nir_var_local:
357 exec_list_push_tail(&impl->locals, &var->node);
358 break;
359
360 case nir_var_global:
361 exec_list_push_tail(&shader->globals, &var->node);
362 break;
363
364 case nir_var_shader_in:
365 exec_list_push_tail(&shader->inputs, &var->node);
366 break;
367
368 case nir_var_shader_out:
369 exec_list_push_tail(&shader->outputs, &var->node);
370 break;
371
372 case nir_var_uniform:
373 exec_list_push_tail(&shader->uniforms, &var->node);
374 break;
375
376 case nir_var_system_value:
377 exec_list_push_tail(&shader->system_values, &var->node);
378 break;
379
380 default:
381 unreachable("not reached");
382 }
383
384 _mesa_hash_table_insert(var_table, ir, var);
385 this->var = var;
386 }
387
388 ir_visitor_status
389 nir_function_visitor::visit_enter(ir_function *ir)
390 {
391 visitor->create_function(ir);
392 return visit_continue_with_parent;
393 }
394
395
396 void
397 nir_visitor::create_function(ir_function *ir)
398 {
399 nir_function *func = nir_function_create(this->shader, ir->name);
400 foreach_in_list(ir_function_signature, sig, &ir->signatures) {
401 create_overload(sig, func);
402 }
403 }
404
405
406
407 void
408 nir_visitor::create_overload(ir_function_signature *ir, nir_function *function)
409 {
410 if (ir->is_intrinsic)
411 return;
412
413 nir_function_overload *overload = nir_function_overload_create(function);
414
415 unsigned num_params = ir->parameters.length();
416 overload->num_params = num_params;
417 overload->params = ralloc_array(shader, nir_parameter, num_params);
418
419 unsigned i = 0;
420 foreach_in_list(ir_variable, param, &ir->parameters) {
421 switch (param->data.mode) {
422 case ir_var_function_in:
423 overload->params[i].param_type = nir_parameter_in;
424 break;
425
426 case ir_var_function_out:
427 overload->params[i].param_type = nir_parameter_out;
428 break;
429
430 case ir_var_function_inout:
431 overload->params[i].param_type = nir_parameter_inout;
432 break;
433
434 default:
435 unreachable("not reached");
436 }
437
438 overload->params[i].type = param->type;
439 i++;
440 }
441
442 overload->return_type = ir->return_type;
443
444 _mesa_hash_table_insert(this->overload_table, ir, overload);
445 }
446
447 void
448 nir_visitor::visit(ir_function *ir)
449 {
450 foreach_in_list(ir_function_signature, sig, &ir->signatures)
451 sig->accept(this);
452 }
453
454 void
455 nir_visitor::visit(ir_function_signature *ir)
456 {
457 if (ir->is_intrinsic)
458 return;
459
460 struct hash_entry *entry =
461 _mesa_hash_table_search(this->overload_table, ir);
462
463 assert(entry);
464 nir_function_overload *overload = (nir_function_overload *) entry->data;
465
466 if (ir->is_defined) {
467 nir_function_impl *impl = nir_function_impl_create(overload);
468 this->impl = impl;
469
470 unsigned num_params = overload->num_params;
471 impl->num_params = num_params;
472 impl->params = ralloc_array(this->shader, nir_variable *, num_params);
473 unsigned i = 0;
474 foreach_in_list(ir_variable, param, &ir->parameters) {
475 param->accept(this);
476 impl->params[i] = this->var;
477 i++;
478 }
479
480 if (overload->return_type == glsl_type::void_type) {
481 impl->return_var = NULL;
482 } else {
483 impl->return_var = ralloc(this->shader, nir_variable);
484 impl->return_var->name = ralloc_strdup(impl->return_var,
485 "return_var");
486 impl->return_var->type = overload->return_type;
487 }
488
489 this->is_global = false;
490
491 this->cf_node_list = &impl->body;
492 visit_exec_list(&ir->body, this);
493
494 this->is_global = true;
495 } else {
496 overload->impl = NULL;
497 }
498 }
499
500 void
501 nir_visitor::visit(ir_loop *ir)
502 {
503 exec_list *old_list = this->cf_node_list;
504
505 nir_loop *loop = nir_loop_create(this->shader);
506 nir_cf_node_insert_end(old_list, &loop->cf_node);
507 this->cf_node_list = &loop->body;
508 visit_exec_list(&ir->body_instructions, this);
509
510 this->cf_node_list = old_list;
511 }
512
513 void
514 nir_visitor::visit(ir_if *ir)
515 {
516 nir_src condition = evaluate_rvalue(ir->condition);
517
518 exec_list *old_list = this->cf_node_list;
519
520 nir_if *if_stmt = nir_if_create(this->shader);
521 if_stmt->condition = condition;
522 nir_cf_node_insert_end(old_list, &if_stmt->cf_node);
523
524 this->cf_node_list = &if_stmt->then_list;
525 visit_exec_list(&ir->then_instructions, this);
526
527 this->cf_node_list = &if_stmt->else_list;
528 visit_exec_list(&ir->else_instructions, this);
529
530 this->cf_node_list = old_list;
531 }
532
533 void
534 nir_visitor::visit(ir_discard *ir)
535 {
536 /*
537 * discards aren't treated as control flow, because before we lower them
538 * they can appear anywhere in the shader and the stuff after them may still
539 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
540 * discards will be immediately followed by a return.
541 */
542
543 nir_intrinsic_instr *discard;
544 if (ir->condition) {
545 discard = nir_intrinsic_instr_create(this->shader,
546 nir_intrinsic_discard_if);
547 discard->src[0] = evaluate_rvalue(ir->condition);
548 } else {
549 discard = nir_intrinsic_instr_create(this->shader, nir_intrinsic_discard);
550 }
551 nir_instr_insert_after_cf_list(this->cf_node_list, &discard->instr);
552 }
553
554 void
555 nir_visitor::visit(ir_emit_vertex *ir)
556 {
557 nir_intrinsic_instr *instr =
558 nir_intrinsic_instr_create(this->shader, nir_intrinsic_emit_vertex);
559 instr->const_index[0] = ir->stream_id();
560 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
561 }
562
563 void
564 nir_visitor::visit(ir_end_primitive *ir)
565 {
566 nir_intrinsic_instr *instr =
567 nir_intrinsic_instr_create(this->shader, nir_intrinsic_end_primitive);
568 instr->const_index[0] = ir->stream_id();
569 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
570 }
571
572 void
573 nir_visitor::visit(ir_loop_jump *ir)
574 {
575 nir_jump_type type;
576 switch (ir->mode) {
577 case ir_loop_jump::jump_break:
578 type = nir_jump_break;
579 break;
580 case ir_loop_jump::jump_continue:
581 type = nir_jump_continue;
582 break;
583 default:
584 unreachable("not reached");
585 }
586
587 nir_jump_instr *instr = nir_jump_instr_create(this->shader, type);
588 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
589 }
590
591 void
592 nir_visitor::visit(ir_return *ir)
593 {
594 if (ir->value != NULL) {
595 nir_intrinsic_instr *copy =
596 nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
597
598 copy->variables[0] = nir_deref_var_create(copy, this->impl->return_var);
599 copy->variables[1] = evaluate_deref(&copy->instr, ir->value);
600 }
601
602 nir_jump_instr *instr = nir_jump_instr_create(this->shader, nir_jump_return);
603 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
604 }
605
606 void
607 nir_visitor::visit(ir_call *ir)
608 {
609 if (ir->callee->is_intrinsic) {
610 nir_intrinsic_op op;
611 if (strcmp(ir->callee_name(), "__intrinsic_atomic_read") == 0) {
612 op = nir_intrinsic_atomic_counter_read_var;
613 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_increment") == 0) {
614 op = nir_intrinsic_atomic_counter_inc_var;
615 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
616 op = nir_intrinsic_atomic_counter_dec_var;
617 } else {
618 unreachable("not reached");
619 }
620
621 nir_intrinsic_instr *instr = nir_intrinsic_instr_create(shader, op);
622 ir_dereference *param =
623 (ir_dereference *) ir->actual_parameters.get_head();
624 instr->variables[0] = evaluate_deref(&instr->instr, param);
625 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, NULL);
626
627 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
628
629 nir_intrinsic_instr *store_instr =
630 nir_intrinsic_instr_create(shader, nir_intrinsic_store_var);
631 store_instr->num_components = 1;
632
633 store_instr->variables[0] = evaluate_deref(&store_instr->instr, ir->return_deref);
634 store_instr->src[0].is_ssa = true;
635 store_instr->src[0].ssa = &instr->dest.ssa;
636
637 nir_instr_insert_after_cf_list(this->cf_node_list, &store_instr->instr);
638
639 return;
640 }
641
642 struct hash_entry *entry =
643 _mesa_hash_table_search(this->overload_table, ir->callee);
644 assert(entry);
645 nir_function_overload *callee = (nir_function_overload *) entry->data;
646
647 nir_call_instr *instr = nir_call_instr_create(this->shader, callee);
648
649 unsigned i = 0;
650 foreach_in_list(ir_dereference, param, &ir->actual_parameters) {
651 instr->params[i] = evaluate_deref(&instr->instr, param);
652 i++;
653 }
654
655 instr->return_deref = evaluate_deref(&instr->instr, ir->return_deref);
656 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
657 }
658
659 void
660 nir_visitor::visit(ir_assignment *ir)
661 {
662 unsigned num_components = ir->lhs->type->vector_elements;
663
664 if ((ir->rhs->as_dereference() || ir->rhs->as_constant()) &&
665 (ir->write_mask == (1 << num_components) - 1 || ir->write_mask == 0)) {
666 /* We're doing a plain-as-can-be copy, so emit a copy_var */
667 nir_intrinsic_instr *copy =
668 nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
669
670 copy->variables[0] = evaluate_deref(&copy->instr, ir->lhs);
671 copy->variables[1] = evaluate_deref(&copy->instr, ir->rhs);
672
673 if (ir->condition) {
674 nir_if *if_stmt = nir_if_create(this->shader);
675 if_stmt->condition = evaluate_rvalue(ir->condition);
676 nir_cf_node_insert_end(this->cf_node_list, &if_stmt->cf_node);
677 nir_instr_insert_after_cf_list(&if_stmt->then_list, &copy->instr);
678 } else {
679 nir_instr_insert_after_cf_list(this->cf_node_list, &copy->instr);
680 }
681 return;
682 }
683
684 assert(ir->rhs->type->is_scalar() || ir->rhs->type->is_vector());
685
686 ir->lhs->accept(this);
687 nir_deref_var *lhs_deref = this->deref_head;
688 nir_src src = evaluate_rvalue(ir->rhs);
689
690 if (ir->write_mask != (1 << num_components) - 1 && ir->write_mask != 0) {
691 /*
692 * We have no good way to update only part of a variable, so just load
693 * the LHS and do a vec operation to combine the old with the new, and
694 * then store it
695 * back into the LHS. Copy propagation should get rid of the mess.
696 */
697
698 nir_intrinsic_instr *load =
699 nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_var);
700 load->num_components = ir->lhs->type->vector_elements;
701 nir_ssa_dest_init(&load->instr, &load->dest, num_components, NULL);
702 load->variables[0] = lhs_deref;
703 ralloc_steal(load, load->variables[0]);
704 nir_instr_insert_after_cf_list(this->cf_node_list, &load->instr);
705
706 nir_op vec_op;
707 switch (ir->lhs->type->vector_elements) {
708 case 1: vec_op = nir_op_imov; break;
709 case 2: vec_op = nir_op_vec2; break;
710 case 3: vec_op = nir_op_vec3; break;
711 case 4: vec_op = nir_op_vec4; break;
712 default: unreachable("Invalid number of components"); break;
713 }
714 nir_alu_instr *vec = nir_alu_instr_create(this->shader, vec_op);
715 nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components, NULL);
716 vec->dest.write_mask = (1 << num_components) - 1;
717
718 unsigned component = 0;
719 for (unsigned i = 0; i < ir->lhs->type->vector_elements; i++) {
720 if (ir->write_mask & (1 << i)) {
721 vec->src[i].src = src;
722
723 /* GLSL IR will give us the input to the write-masked assignment
724 * in a single packed vector. So, for example, if the
725 * writemask is xzw, then we have to swizzle x -> x, y -> z,
726 * and z -> w and get the y component from the load.
727 */
728 vec->src[i].swizzle[0] = component++;
729 } else {
730 vec->src[i].src.is_ssa = true;
731 vec->src[i].src.ssa = &load->dest.ssa;
732 vec->src[i].swizzle[0] = i;
733 }
734 }
735
736 nir_instr_insert_after_cf_list(this->cf_node_list, &vec->instr);
737
738 src.is_ssa = true;
739 src.ssa = &vec->dest.dest.ssa;
740 }
741
742 nir_intrinsic_instr *store =
743 nir_intrinsic_instr_create(this->shader, nir_intrinsic_store_var);
744 store->num_components = ir->lhs->type->vector_elements;
745 nir_deref *store_deref = nir_copy_deref(store, &lhs_deref->deref);
746 store->variables[0] = nir_deref_as_var(store_deref);
747 store->src[0] = src;
748
749 if (ir->condition) {
750 nir_if *if_stmt = nir_if_create(this->shader);
751 if_stmt->condition = evaluate_rvalue(ir->condition);
752 nir_cf_node_insert_end(this->cf_node_list, &if_stmt->cf_node);
753 nir_instr_insert_after_cf_list(&if_stmt->then_list, &store->instr);
754 } else {
755 nir_instr_insert_after_cf_list(this->cf_node_list, &store->instr);
756 }
757 }
758
759 /*
760 * Given an instruction, returns a pointer to its destination or NULL if there
761 * is no destination.
762 *
763 * Note that this only handles instructions we generate at this level.
764 */
765 static nir_dest *
766 get_instr_dest(nir_instr *instr)
767 {
768 nir_alu_instr *alu_instr;
769 nir_intrinsic_instr *intrinsic_instr;
770 nir_tex_instr *tex_instr;
771
772 switch (instr->type) {
773 case nir_instr_type_alu:
774 alu_instr = nir_instr_as_alu(instr);
775 return &alu_instr->dest.dest;
776
777 case nir_instr_type_intrinsic:
778 intrinsic_instr = nir_instr_as_intrinsic(instr);
779 if (nir_intrinsic_infos[intrinsic_instr->intrinsic].has_dest)
780 return &intrinsic_instr->dest;
781 else
782 return NULL;
783
784 case nir_instr_type_tex:
785 tex_instr = nir_instr_as_tex(instr);
786 return &tex_instr->dest;
787
788 default:
789 unreachable("not reached");
790 }
791
792 return NULL;
793 }
794
795 void
796 nir_visitor::add_instr(nir_instr *instr, unsigned num_components)
797 {
798 nir_dest *dest = get_instr_dest(instr);
799
800 nir_ssa_dest_init(instr, dest, num_components, NULL);
801
802 nir_instr_insert_after_cf_list(this->cf_node_list, instr);
803 this->result = instr;
804 }
805
806 nir_src
807 nir_visitor::evaluate_rvalue(ir_rvalue* ir)
808 {
809 ir->accept(this);
810 if (ir->as_dereference() || ir->as_constant()) {
811 /*
812 * A dereference is being used on the right hand side, which means we
813 * must emit a variable load.
814 */
815
816 nir_intrinsic_instr *load_instr =
817 nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_var);
818 load_instr->num_components = ir->type->vector_elements;
819 load_instr->variables[0] = this->deref_head;
820 ralloc_steal(load_instr, load_instr->variables[0]);
821 add_instr(&load_instr->instr, ir->type->vector_elements);
822 }
823
824 nir_dest *dest = get_instr_dest(this->result);
825
826 assert(dest->is_ssa);
827 nir_src src;
828 src.is_ssa = true;
829 src.ssa = &dest->ssa;
830
831 return src;
832 }
833
834 nir_alu_instr *
835 nir_visitor::emit(nir_op op, unsigned dest_size, nir_src *srcs)
836 {
837 nir_alu_instr *instr = nir_alu_instr_create(this->shader, op);
838 for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++)
839 instr->src[i].src = srcs[i];
840 instr->dest.write_mask = (1 << dest_size) - 1;
841 add_instr(&instr->instr, dest_size);
842 return instr;
843 }
844
845 nir_alu_instr *
846 nir_visitor::emit(nir_op op, unsigned dest_size, nir_src src1)
847 {
848 assert(nir_op_infos[op].num_inputs == 1);
849 return emit(op, dest_size, &src1);
850 }
851
852 nir_alu_instr *
853 nir_visitor::emit(nir_op op, unsigned dest_size, nir_src src1,
854 nir_src src2)
855 {
856 assert(nir_op_infos[op].num_inputs == 2);
857 nir_src srcs[] = { src1, src2 };
858 return emit(op, dest_size, srcs);
859 }
860
861 nir_alu_instr *
862 nir_visitor::emit(nir_op op, unsigned dest_size, nir_src src1,
863 nir_src src2, nir_src src3)
864 {
865 assert(nir_op_infos[op].num_inputs == 3);
866 nir_src srcs[] = { src1, src2, src3 };
867 return emit(op, dest_size, srcs);
868 }
869
870 void
871 nir_visitor::visit(ir_expression *ir)
872 {
873 /* Some special cases */
874 switch (ir->operation) {
875 case ir_binop_ubo_load: {
876 ir_constant *const_index = ir->operands[1]->as_constant();
877
878 nir_intrinsic_op op;
879 if (const_index) {
880 op = nir_intrinsic_load_ubo;
881 } else {
882 op = nir_intrinsic_load_ubo_indirect;
883 }
884 nir_intrinsic_instr *load = nir_intrinsic_instr_create(this->shader, op);
885 load->num_components = ir->type->vector_elements;
886 load->const_index[0] = const_index ? const_index->value.u[0] : 0; /* base offset */
887 load->const_index[1] = 1; /* number of vec4's */
888 load->src[0] = evaluate_rvalue(ir->operands[0]);
889 if (!const_index)
890 load->src[1] = evaluate_rvalue(ir->operands[1]);
891 add_instr(&load->instr, ir->type->vector_elements);
892
893 /*
894 * In UBO's, a true boolean value is any non-zero value, but we consider
895 * a true boolean to be ~0. Fix this up with a != 0 comparison.
896 */
897
898 if (ir->type->base_type == GLSL_TYPE_BOOL) {
899 nir_load_const_instr *const_zero = nir_load_const_instr_create(shader, 1);
900 const_zero->value.u[0] = 0;
901 nir_instr_insert_after_cf_list(this->cf_node_list, &const_zero->instr);
902
903 nir_alu_instr *compare = nir_alu_instr_create(shader, nir_op_ine);
904 compare->src[0].src.is_ssa = true;
905 compare->src[0].src.ssa = &load->dest.ssa;
906 compare->src[1].src.is_ssa = true;
907 compare->src[1].src.ssa = &const_zero->def;
908 for (unsigned i = 0; i < ir->type->vector_elements; i++)
909 compare->src[1].swizzle[i] = 0;
910 compare->dest.write_mask = (1 << ir->type->vector_elements) - 1;
911
912 add_instr(&compare->instr, ir->type->vector_elements);
913 }
914
915 return;
916 }
917
918 case ir_unop_interpolate_at_centroid:
919 case ir_binop_interpolate_at_offset:
920 case ir_binop_interpolate_at_sample: {
921 ir_dereference *deref = ir->operands[0]->as_dereference();
922 ir_swizzle *swizzle = NULL;
923 if (!deref) {
924 /* the api does not allow a swizzle here, but the varying packing code
925 * may have pushed one into here.
926 */
927 swizzle = ir->operands[0]->as_swizzle();
928 assert(swizzle);
929 deref = swizzle->val->as_dereference();
930 assert(deref);
931 }
932
933 deref->accept(this);
934
935 nir_intrinsic_op op;
936 if (this->deref_head->var->data.mode == nir_var_shader_in) {
937 switch (ir->operation) {
938 case ir_unop_interpolate_at_centroid:
939 op = nir_intrinsic_interp_var_at_centroid;
940 break;
941 case ir_binop_interpolate_at_offset:
942 op = nir_intrinsic_interp_var_at_offset;
943 break;
944 case ir_binop_interpolate_at_sample:
945 op = nir_intrinsic_interp_var_at_sample;
946 break;
947 default:
948 unreachable("Invalid interpolation intrinsic");
949 }
950 } else {
951 /* This case can happen if the vertex shader does not write the
952 * given varying. In this case, the linker will lower it to a
953 * global variable. Since interpolating a variable makes no
954 * sense, we'll just turn it into a load which will probably
955 * eventually end up as an SSA definition.
956 */
957 assert(this->deref_head->var->data.mode == nir_var_global);
958 op = nir_intrinsic_load_var;
959 }
960
961 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(shader, op);
962 intrin->num_components = deref->type->vector_elements;
963 intrin->variables[0] = this->deref_head;
964 ralloc_steal(intrin, intrin->variables[0]);
965
966 if (intrin->intrinsic == nir_intrinsic_interp_var_at_offset ||
967 intrin->intrinsic == nir_intrinsic_interp_var_at_sample)
968 intrin->src[0] = evaluate_rvalue(ir->operands[1]);
969
970 add_instr(&intrin->instr, deref->type->vector_elements);
971
972 if (swizzle) {
973 nir_alu_instr *mov = nir_alu_instr_create(shader, nir_op_imov);
974 mov->dest.write_mask = (1 << swizzle->type->vector_elements) - 1;
975 mov->src[0].src.is_ssa = true;
976 mov->src[0].src.ssa = &intrin->dest.ssa;
977
978 mov->src[0].swizzle[0] = swizzle->mask.x;
979 mov->src[0].swizzle[1] = swizzle->mask.y;
980 mov->src[0].swizzle[2] = swizzle->mask.z;
981 mov->src[0].swizzle[3] = swizzle->mask.w;
982 for (unsigned i = deref->type->vector_elements; i < 4; i++)
983 mov->src[0].swizzle[i] = 0;
984
985 add_instr(&mov->instr, swizzle->type->vector_elements);
986 }
987
988 return;
989 }
990
991 default:
992 break;
993 }
994
995 nir_src srcs[4];
996 for (unsigned i = 0; i < ir->get_num_operands(); i++)
997 srcs[i] = evaluate_rvalue(ir->operands[i]);
998
999 glsl_base_type types[4];
1000 for (unsigned i = 0; i < ir->get_num_operands(); i++)
1001 if (supports_ints)
1002 types[i] = ir->operands[i]->type->base_type;
1003 else
1004 types[i] = GLSL_TYPE_FLOAT;
1005
1006 glsl_base_type out_type;
1007 if (supports_ints)
1008 out_type = ir->type->base_type;
1009 else
1010 out_type = GLSL_TYPE_FLOAT;
1011
1012 unsigned dest_size = ir->type->vector_elements;
1013
1014 nir_alu_instr *instr;
1015 nir_op op;
1016
1017 switch (ir->operation) {
1018 case ir_unop_bit_not: emit(nir_op_inot, dest_size, srcs); break;
1019 case ir_unop_logic_not:
1020 emit(supports_ints ? nir_op_inot : nir_op_fnot, dest_size, srcs);
1021 break;
1022 case ir_unop_neg:
1023 instr = emit(types[0] == GLSL_TYPE_FLOAT ? nir_op_fneg : nir_op_ineg,
1024 dest_size, srcs);
1025 break;
1026 case ir_unop_abs:
1027 instr = emit(types[0] == GLSL_TYPE_FLOAT ? nir_op_fabs : nir_op_iabs,
1028 dest_size, srcs);
1029 break;
1030 case ir_unop_saturate:
1031 assert(types[0] == GLSL_TYPE_FLOAT);
1032 instr = emit(nir_op_fsat, dest_size, srcs);
1033 break;
1034 case ir_unop_sign:
1035 emit(types[0] == GLSL_TYPE_FLOAT ? nir_op_fsign : nir_op_isign,
1036 dest_size, srcs);
1037 break;
1038 case ir_unop_rcp: emit(nir_op_frcp, dest_size, srcs); break;
1039 case ir_unop_rsq: emit(nir_op_frsq, dest_size, srcs); break;
1040 case ir_unop_sqrt: emit(nir_op_fsqrt, dest_size, srcs); break;
1041 case ir_unop_exp: unreachable("ir_unop_exp should have been lowered");
1042 case ir_unop_log: emit(nir_op_flog, dest_size, srcs); break;
1043 case ir_unop_exp2: emit(nir_op_fexp2, dest_size, srcs); break;
1044 case ir_unop_log2: emit(nir_op_flog2, dest_size, srcs); break;
1045 case ir_unop_i2f:
1046 emit(supports_ints ? nir_op_i2f : nir_op_fmov, dest_size, srcs);
1047 break;
1048 case ir_unop_u2f:
1049 emit(supports_ints ? nir_op_u2f : nir_op_fmov, dest_size, srcs);
1050 break;
1051 case ir_unop_b2f:
1052 emit(supports_ints ? nir_op_b2f : nir_op_fmov, dest_size, srcs);
1053 break;
1054 case ir_unop_f2i: emit(nir_op_f2i, dest_size, srcs); break;
1055 case ir_unop_f2u: emit(nir_op_f2u, dest_size, srcs); break;
1056 case ir_unop_f2b: emit(nir_op_f2b, dest_size, srcs); break;
1057 case ir_unop_i2b: emit(nir_op_i2b, dest_size, srcs); break;
1058 case ir_unop_b2i: emit(nir_op_b2i, dest_size, srcs); break;
1059 case ir_unop_i2u:
1060 case ir_unop_u2i:
1061 case ir_unop_bitcast_i2f:
1062 case ir_unop_bitcast_f2i:
1063 case ir_unop_bitcast_u2f:
1064 case ir_unop_bitcast_f2u:
1065 /* no-op */
1066 emit(nir_op_imov, dest_size, srcs);
1067 break;
1068 case ir_unop_any:
1069 switch (ir->operands[0]->type->vector_elements) {
1070 case 2:
1071 emit(supports_ints ? nir_op_bany2 : nir_op_fany2,
1072 dest_size, srcs);
1073 break;
1074 case 3:
1075 emit(supports_ints ? nir_op_bany3 : nir_op_fany3,
1076 dest_size, srcs);
1077 break;
1078 case 4:
1079 emit(supports_ints ? nir_op_bany4 : nir_op_fany4,
1080 dest_size, srcs);
1081 break;
1082 default:
1083 unreachable("not reached");
1084 }
1085 break;
1086 case ir_unop_trunc: emit(nir_op_ftrunc, dest_size, srcs); break;
1087 case ir_unop_ceil: emit(nir_op_fceil, dest_size, srcs); break;
1088 case ir_unop_floor: emit(nir_op_ffloor, dest_size, srcs); break;
1089 case ir_unop_fract: emit(nir_op_ffract, dest_size, srcs); break;
1090 case ir_unop_round_even: emit(nir_op_fround_even, dest_size, srcs); break;
1091 case ir_unop_sin: emit(nir_op_fsin, dest_size, srcs); break;
1092 case ir_unop_cos: emit(nir_op_fcos, dest_size, srcs); break;
1093 case ir_unop_dFdx: emit(nir_op_fddx, dest_size, srcs); break;
1094 case ir_unop_dFdy: emit(nir_op_fddy, dest_size, srcs); break;
1095 case ir_unop_dFdx_fine: emit(nir_op_fddx_fine, dest_size, srcs); break;
1096 case ir_unop_dFdy_fine: emit(nir_op_fddy_fine, dest_size, srcs); break;
1097 case ir_unop_dFdx_coarse: emit(nir_op_fddx_coarse, dest_size, srcs); break;
1098 case ir_unop_dFdy_coarse: emit(nir_op_fddy_coarse, dest_size, srcs); break;
1099 case ir_unop_pack_snorm_2x16:
1100 emit(nir_op_pack_snorm_2x16, dest_size, srcs);
1101 break;
1102 case ir_unop_pack_snorm_4x8:
1103 emit(nir_op_pack_snorm_4x8, dest_size, srcs);
1104 break;
1105 case ir_unop_pack_unorm_2x16:
1106 emit(nir_op_pack_unorm_2x16, dest_size, srcs);
1107 break;
1108 case ir_unop_pack_unorm_4x8:
1109 emit(nir_op_pack_unorm_4x8, dest_size, srcs);
1110 break;
1111 case ir_unop_pack_half_2x16:
1112 emit(nir_op_pack_half_2x16, dest_size, srcs);
1113 break;
1114 case ir_unop_unpack_snorm_2x16:
1115 emit(nir_op_unpack_snorm_2x16, dest_size, srcs);
1116 break;
1117 case ir_unop_unpack_snorm_4x8:
1118 emit(nir_op_unpack_snorm_4x8, dest_size, srcs);
1119 break;
1120 case ir_unop_unpack_unorm_2x16:
1121 emit(nir_op_unpack_unorm_2x16, dest_size, srcs);
1122 break;
1123 case ir_unop_unpack_unorm_4x8:
1124 emit(nir_op_unpack_unorm_4x8, dest_size, srcs);
1125 break;
1126 case ir_unop_unpack_half_2x16:
1127 emit(nir_op_unpack_half_2x16, dest_size, srcs);
1128 break;
1129 case ir_unop_unpack_half_2x16_split_x:
1130 emit(nir_op_unpack_half_2x16_split_x, dest_size, srcs);
1131 break;
1132 case ir_unop_unpack_half_2x16_split_y:
1133 emit(nir_op_unpack_half_2x16_split_y, dest_size, srcs);
1134 break;
1135 case ir_unop_bitfield_reverse:
1136 emit(nir_op_bitfield_reverse, dest_size, srcs);
1137 break;
1138 case ir_unop_bit_count:
1139 emit(nir_op_bit_count, dest_size, srcs);
1140 break;
1141 case ir_unop_find_msb:
1142 switch (types[0]) {
1143 case GLSL_TYPE_UINT:
1144 emit(nir_op_ufind_msb, dest_size, srcs);
1145 break;
1146 case GLSL_TYPE_INT:
1147 emit(nir_op_ifind_msb, dest_size, srcs);
1148 break;
1149 default:
1150 unreachable("Invalid type for findMSB()");
1151 }
1152 break;
1153 case ir_unop_find_lsb:
1154 emit(nir_op_find_lsb, dest_size, srcs);
1155 break;
1156
1157 case ir_unop_noise:
1158 switch (ir->type->vector_elements) {
1159 case 1:
1160 switch (ir->operands[0]->type->vector_elements) {
1161 case 1: emit(nir_op_fnoise1_1, dest_size, srcs); break;
1162 case 2: emit(nir_op_fnoise1_2, dest_size, srcs); break;
1163 case 3: emit(nir_op_fnoise1_3, dest_size, srcs); break;
1164 case 4: emit(nir_op_fnoise1_4, dest_size, srcs); break;
1165 default: unreachable("not reached");
1166 }
1167 break;
1168 case 2:
1169 switch (ir->operands[0]->type->vector_elements) {
1170 case 1: emit(nir_op_fnoise2_1, dest_size, srcs); break;
1171 case 2: emit(nir_op_fnoise2_2, dest_size, srcs); break;
1172 case 3: emit(nir_op_fnoise2_3, dest_size, srcs); break;
1173 case 4: emit(nir_op_fnoise2_4, dest_size, srcs); break;
1174 default: unreachable("not reached");
1175 }
1176 break;
1177 case 3:
1178 switch (ir->operands[0]->type->vector_elements) {
1179 case 1: emit(nir_op_fnoise3_1, dest_size, srcs); break;
1180 case 2: emit(nir_op_fnoise3_2, dest_size, srcs); break;
1181 case 3: emit(nir_op_fnoise3_3, dest_size, srcs); break;
1182 case 4: emit(nir_op_fnoise3_4, dest_size, srcs); break;
1183 default: unreachable("not reached");
1184 }
1185 break;
1186 case 4:
1187 switch (ir->operands[0]->type->vector_elements) {
1188 case 1: emit(nir_op_fnoise4_1, dest_size, srcs); break;
1189 case 2: emit(nir_op_fnoise4_2, dest_size, srcs); break;
1190 case 3: emit(nir_op_fnoise4_3, dest_size, srcs); break;
1191 case 4: emit(nir_op_fnoise4_4, dest_size, srcs); break;
1192 default: unreachable("not reached");
1193 }
1194 break;
1195 default:
1196 unreachable("not reached");
1197 }
1198 break;
1199 case ir_binop_add:
1200 case ir_binop_sub:
1201 case ir_binop_mul:
1202 case ir_binop_div:
1203 case ir_binop_mod:
1204 case ir_binop_min:
1205 case ir_binop_max:
1206 case ir_binop_pow:
1207 case ir_binop_bit_and:
1208 case ir_binop_bit_or:
1209 case ir_binop_bit_xor:
1210 case ir_binop_logic_and:
1211 case ir_binop_logic_or:
1212 case ir_binop_logic_xor:
1213 case ir_binop_lshift:
1214 case ir_binop_rshift:
1215 switch (ir->operation) {
1216 case ir_binop_add:
1217 if (out_type == GLSL_TYPE_FLOAT)
1218 op = nir_op_fadd;
1219 else
1220 op = nir_op_iadd;
1221 break;
1222 case ir_binop_sub:
1223 if (out_type == GLSL_TYPE_FLOAT)
1224 op = nir_op_fsub;
1225 else
1226 op = nir_op_isub;
1227 break;
1228 case ir_binop_mul:
1229 if (out_type == GLSL_TYPE_FLOAT)
1230 op = nir_op_fmul;
1231 else
1232 op = nir_op_imul;
1233 break;
1234 case ir_binop_div:
1235 if (out_type == GLSL_TYPE_FLOAT)
1236 op = nir_op_fdiv;
1237 else if (out_type == GLSL_TYPE_INT)
1238 op = nir_op_idiv;
1239 else
1240 op = nir_op_udiv;
1241 break;
1242 case ir_binop_mod:
1243 if (out_type == GLSL_TYPE_FLOAT)
1244 op = nir_op_fmod;
1245 else
1246 op = nir_op_umod;
1247 break;
1248 case ir_binop_min:
1249 if (out_type == GLSL_TYPE_FLOAT)
1250 op = nir_op_fmin;
1251 else if (out_type == GLSL_TYPE_INT)
1252 op = nir_op_imin;
1253 else
1254 op = nir_op_umin;
1255 break;
1256 case ir_binop_max:
1257 if (out_type == GLSL_TYPE_FLOAT)
1258 op = nir_op_fmax;
1259 else if (out_type == GLSL_TYPE_INT)
1260 op = nir_op_imax;
1261 else
1262 op = nir_op_umax;
1263 break;
1264 case ir_binop_bit_and:
1265 op = nir_op_iand;
1266 break;
1267 case ir_binop_bit_or:
1268 op = nir_op_ior;
1269 break;
1270 case ir_binop_bit_xor:
1271 op = nir_op_ixor;
1272 break;
1273 case ir_binop_logic_and:
1274 if (supports_ints)
1275 op = nir_op_iand;
1276 else
1277 op = nir_op_fand;
1278 break;
1279 case ir_binop_logic_or:
1280 if (supports_ints)
1281 op = nir_op_ior;
1282 else
1283 op = nir_op_for;
1284 break;
1285 case ir_binop_logic_xor:
1286 if (supports_ints)
1287 op = nir_op_ixor;
1288 else
1289 op = nir_op_fxor;
1290 break;
1291 case ir_binop_lshift:
1292 op = nir_op_ishl;
1293 break;
1294 case ir_binop_rshift:
1295 if (out_type == GLSL_TYPE_INT)
1296 op = nir_op_ishr;
1297 else
1298 op = nir_op_ushr;
1299 break;
1300 case ir_binop_pow:
1301 op = nir_op_fpow;
1302 break;
1303
1304 default:
1305 unreachable("not reached");
1306 }
1307
1308 instr = emit(op, dest_size, srcs);
1309
1310 if (ir->operands[0]->type->vector_elements != 1 &&
1311 ir->operands[1]->type->vector_elements == 1) {
1312 for (unsigned i = 0; i < ir->operands[0]->type->vector_elements;
1313 i++) {
1314 instr->src[1].swizzle[i] = 0;
1315 }
1316 }
1317
1318 if (ir->operands[1]->type->vector_elements != 1 &&
1319 ir->operands[0]->type->vector_elements == 1) {
1320 for (unsigned i = 0; i < ir->operands[1]->type->vector_elements;
1321 i++) {
1322 instr->src[0].swizzle[i] = 0;
1323 }
1324 }
1325
1326 break;
1327 case ir_binop_imul_high:
1328 emit(out_type == GLSL_TYPE_UINT ? nir_op_umul_high : nir_op_imul_high,
1329 dest_size, srcs);
1330 break;
1331 case ir_binop_carry: emit(nir_op_uadd_carry, dest_size, srcs); break;
1332 case ir_binop_borrow: emit(nir_op_usub_borrow, dest_size, srcs); break;
1333 case ir_binop_less:
1334 if (supports_ints) {
1335 if (types[0] == GLSL_TYPE_FLOAT)
1336 emit(nir_op_flt, dest_size, srcs);
1337 else if (types[0] == GLSL_TYPE_INT)
1338 emit(nir_op_ilt, dest_size, srcs);
1339 else
1340 emit(nir_op_ult, dest_size, srcs);
1341 } else {
1342 emit(nir_op_slt, dest_size, srcs);
1343 }
1344 break;
1345 case ir_binop_greater:
1346 if (supports_ints) {
1347 if (types[0] == GLSL_TYPE_FLOAT)
1348 emit(nir_op_flt, dest_size, srcs[1], srcs[0]);
1349 else if (types[0] == GLSL_TYPE_INT)
1350 emit(nir_op_ilt, dest_size, srcs[1], srcs[0]);
1351 else
1352 emit(nir_op_ult, dest_size, srcs[1], srcs[0]);
1353 } else {
1354 emit(nir_op_slt, dest_size, srcs[1], srcs[0]);
1355 }
1356 break;
1357 case ir_binop_lequal:
1358 if (supports_ints) {
1359 if (types[0] == GLSL_TYPE_FLOAT)
1360 emit(nir_op_fge, dest_size, srcs[1], srcs[0]);
1361 else if (types[0] == GLSL_TYPE_INT)
1362 emit(nir_op_ige, dest_size, srcs[1], srcs[0]);
1363 else
1364 emit(nir_op_uge, dest_size, srcs[1], srcs[0]);
1365 } else {
1366 emit(nir_op_slt, dest_size, srcs[1], srcs[0]);
1367 }
1368 break;
1369 case ir_binop_gequal:
1370 if (supports_ints) {
1371 if (types[0] == GLSL_TYPE_FLOAT)
1372 emit(nir_op_fge, dest_size, srcs);
1373 else if (types[0] == GLSL_TYPE_INT)
1374 emit(nir_op_ige, dest_size, srcs);
1375 else
1376 emit(nir_op_uge, dest_size, srcs);
1377 } else {
1378 emit(nir_op_slt, dest_size, srcs);
1379 }
1380 break;
1381 case ir_binop_equal:
1382 if (supports_ints) {
1383 if (types[0] == GLSL_TYPE_FLOAT)
1384 emit(nir_op_feq, dest_size, srcs);
1385 else
1386 emit(nir_op_ieq, dest_size, srcs);
1387 } else {
1388 emit(nir_op_seq, dest_size, srcs);
1389 }
1390 break;
1391 case ir_binop_nequal:
1392 if (supports_ints) {
1393 if (types[0] == GLSL_TYPE_FLOAT)
1394 emit(nir_op_fne, dest_size, srcs);
1395 else
1396 emit(nir_op_ine, dest_size, srcs);
1397 } else {
1398 emit(nir_op_sne, dest_size, srcs);
1399 }
1400 break;
1401 case ir_binop_all_equal:
1402 if (supports_ints) {
1403 if (types[0] == GLSL_TYPE_FLOAT) {
1404 switch (ir->operands[0]->type->vector_elements) {
1405 case 1: emit(nir_op_feq, dest_size, srcs); break;
1406 case 2: emit(nir_op_ball_fequal2, dest_size, srcs); break;
1407 case 3: emit(nir_op_ball_fequal3, dest_size, srcs); break;
1408 case 4: emit(nir_op_ball_fequal4, dest_size, srcs); break;
1409 default:
1410 unreachable("not reached");
1411 }
1412 } else {
1413 switch (ir->operands[0]->type->vector_elements) {
1414 case 1: emit(nir_op_ieq, dest_size, srcs); break;
1415 case 2: emit(nir_op_ball_iequal2, dest_size, srcs); break;
1416 case 3: emit(nir_op_ball_iequal3, dest_size, srcs); break;
1417 case 4: emit(nir_op_ball_iequal4, dest_size, srcs); break;
1418 default:
1419 unreachable("not reached");
1420 }
1421 }
1422 } else {
1423 switch (ir->operands[0]->type->vector_elements) {
1424 case 1: emit(nir_op_seq, dest_size, srcs); break;
1425 case 2: emit(nir_op_fall_equal2, dest_size, srcs); break;
1426 case 3: emit(nir_op_fall_equal3, dest_size, srcs); break;
1427 case 4: emit(nir_op_fall_equal4, dest_size, srcs); break;
1428 default:
1429 unreachable("not reached");
1430 }
1431 }
1432 break;
1433 case ir_binop_any_nequal:
1434 if (supports_ints) {
1435 if (types[0] == GLSL_TYPE_FLOAT) {
1436 switch (ir->operands[0]->type->vector_elements) {
1437 case 1: emit(nir_op_fne, dest_size, srcs); break;
1438 case 2: emit(nir_op_bany_fnequal2, dest_size, srcs); break;
1439 case 3: emit(nir_op_bany_fnequal3, dest_size, srcs); break;
1440 case 4: emit(nir_op_bany_fnequal4, dest_size, srcs); break;
1441 default:
1442 unreachable("not reached");
1443 }
1444 } else {
1445 switch (ir->operands[0]->type->vector_elements) {
1446 case 1: emit(nir_op_ine, dest_size, srcs); break;
1447 case 2: emit(nir_op_bany_inequal2, dest_size, srcs); break;
1448 case 3: emit(nir_op_bany_inequal3, dest_size, srcs); break;
1449 case 4: emit(nir_op_bany_inequal4, dest_size, srcs); break;
1450 default:
1451 unreachable("not reached");
1452 }
1453 }
1454 } else {
1455 switch (ir->operands[0]->type->vector_elements) {
1456 case 1: emit(nir_op_sne, dest_size, srcs); break;
1457 case 2: emit(nir_op_fany_nequal2, dest_size, srcs); break;
1458 case 3: emit(nir_op_fany_nequal3, dest_size, srcs); break;
1459 case 4: emit(nir_op_fany_nequal4, dest_size, srcs); break;
1460 default:
1461 unreachable("not reached");
1462 }
1463 }
1464 break;
1465 case ir_binop_dot:
1466 switch (ir->operands[0]->type->vector_elements) {
1467 case 2: emit(nir_op_fdot2, dest_size, srcs); break;
1468 case 3: emit(nir_op_fdot3, dest_size, srcs); break;
1469 case 4: emit(nir_op_fdot4, dest_size, srcs); break;
1470 default:
1471 unreachable("not reached");
1472 }
1473 break;
1474
1475 case ir_binop_pack_half_2x16_split:
1476 emit(nir_op_pack_half_2x16_split, dest_size, srcs);
1477 break;
1478 case ir_binop_bfm: emit(nir_op_bfm, dest_size, srcs); break;
1479 case ir_binop_ldexp: emit(nir_op_ldexp, dest_size, srcs); break;
1480 case ir_triop_fma: emit(nir_op_ffma, dest_size, srcs); break;
1481 case ir_triop_lrp:
1482 instr = emit(nir_op_flrp, dest_size, srcs);
1483 if (ir->operands[0]->type->vector_elements != 1 &&
1484 ir->operands[2]->type->vector_elements == 1) {
1485 for (unsigned i = 0; i < ir->operands[0]->type->vector_elements;
1486 i++) {
1487 instr->src[2].swizzle[i] = 0;
1488 }
1489 }
1490 break;
1491 case ir_triop_csel:
1492 if (supports_ints)
1493 emit(nir_op_bcsel, dest_size, srcs);
1494 else
1495 emit(nir_op_fcsel, dest_size, srcs);
1496 break;
1497 case ir_triop_bfi:
1498 instr = emit(nir_op_bfi, dest_size, srcs);
1499 for (unsigned i = 0; i < ir->operands[1]->type->vector_elements; i++) {
1500 instr->src[0].swizzle[i] = 0;
1501 }
1502 break;
1503 case ir_triop_bitfield_extract:
1504 instr = emit(out_type == GLSL_TYPE_INT ? nir_op_ibitfield_extract :
1505 nir_op_ubitfield_extract, dest_size, srcs);
1506 for (unsigned i = 0; i < ir->operands[0]->type->vector_elements; i++) {
1507 instr->src[1].swizzle[i] = 0;
1508 instr->src[2].swizzle[i] = 0;
1509 }
1510 break;
1511 case ir_quadop_bitfield_insert:
1512 instr = emit(nir_op_bitfield_insert, dest_size, srcs);
1513 for (unsigned i = 0; i < ir->operands[0]->type->vector_elements; i++) {
1514 instr->src[2].swizzle[i] = 0;
1515 instr->src[3].swizzle[i] = 0;
1516 }
1517 break;
1518 case ir_quadop_vector:
1519 switch (ir->type->vector_elements) {
1520 case 2: emit(nir_op_vec2, dest_size, srcs); break;
1521 case 3: emit(nir_op_vec3, dest_size, srcs); break;
1522 case 4: emit(nir_op_vec4, dest_size, srcs); break;
1523 default: unreachable("not reached");
1524 }
1525 break;
1526
1527 default:
1528 unreachable("not reached");
1529 }
1530 }
1531
1532 void
1533 nir_visitor::visit(ir_swizzle *ir)
1534 {
1535 nir_alu_instr *instr = emit(supports_ints ? nir_op_imov : nir_op_fmov,
1536 ir->type->vector_elements,
1537 evaluate_rvalue(ir->val));
1538
1539 unsigned swizzle[4] = { ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w };
1540 for (unsigned i = 0; i < ir->type->vector_elements; i++)
1541 instr->src[0].swizzle[i] = swizzle[i];
1542 }
1543
1544 void
1545 nir_visitor::visit(ir_texture *ir)
1546 {
1547 unsigned num_srcs;
1548 nir_texop op;
1549 switch (ir->op) {
1550 case ir_tex:
1551 op = nir_texop_tex;
1552 num_srcs = 1; /* coordinate */
1553 break;
1554
1555 case ir_txb:
1556 case ir_txl:
1557 op = (ir->op == ir_txb) ? nir_texop_txb : nir_texop_txl;
1558 num_srcs = 2; /* coordinate, bias/lod */
1559 break;
1560
1561 case ir_txd:
1562 op = nir_texop_txd; /* coordinate, dPdx, dPdy */
1563 num_srcs = 3;
1564 break;
1565
1566 case ir_txf:
1567 op = nir_texop_txf;
1568 if (ir->lod_info.lod != NULL)
1569 num_srcs = 2; /* coordinate, lod */
1570 else
1571 num_srcs = 1; /* coordinate */
1572 break;
1573
1574 case ir_txf_ms:
1575 op = nir_texop_txf_ms;
1576 num_srcs = 2; /* coordinate, sample_index */
1577 break;
1578
1579 case ir_txs:
1580 op = nir_texop_txs;
1581 if (ir->lod_info.lod != NULL)
1582 num_srcs = 1; /* lod */
1583 else
1584 num_srcs = 0;
1585 break;
1586
1587 case ir_lod:
1588 op = nir_texop_lod;
1589 num_srcs = 1; /* coordinate */
1590 break;
1591
1592 case ir_tg4:
1593 op = nir_texop_tg4;
1594 num_srcs = 1; /* coordinate */
1595 break;
1596
1597 case ir_query_levels:
1598 op = nir_texop_query_levels;
1599 num_srcs = 0;
1600 break;
1601
1602 default:
1603 unreachable("not reached");
1604 }
1605
1606 if (ir->projector != NULL)
1607 num_srcs++;
1608 if (ir->shadow_comparitor != NULL)
1609 num_srcs++;
1610 if (ir->offset != NULL && ir->offset->as_constant() == NULL)
1611 num_srcs++;
1612
1613 nir_tex_instr *instr = nir_tex_instr_create(this->shader, num_srcs);
1614
1615 instr->op = op;
1616 instr->sampler_dim =
1617 (glsl_sampler_dim) ir->sampler->type->sampler_dimensionality;
1618 instr->is_array = ir->sampler->type->sampler_array;
1619 instr->is_shadow = ir->sampler->type->sampler_shadow;
1620 if (instr->is_shadow)
1621 instr->is_new_style_shadow = (ir->type->vector_elements == 1);
1622 switch (ir->type->base_type) {
1623 case GLSL_TYPE_FLOAT:
1624 instr->dest_type = nir_type_float;
1625 break;
1626 case GLSL_TYPE_INT:
1627 instr->dest_type = nir_type_int;
1628 break;
1629 case GLSL_TYPE_UINT:
1630 instr->dest_type = nir_type_unsigned;
1631 break;
1632 default:
1633 unreachable("not reached");
1634 }
1635
1636 instr->sampler = evaluate_deref(&instr->instr, ir->sampler);
1637
1638 unsigned src_number = 0;
1639
1640 if (ir->coordinate != NULL) {
1641 instr->coord_components = ir->coordinate->type->vector_elements;
1642 instr->src[src_number].src = evaluate_rvalue(ir->coordinate);
1643 instr->src[src_number].src_type = nir_tex_src_coord;
1644 src_number++;
1645 }
1646
1647 if (ir->projector != NULL) {
1648 instr->src[src_number].src = evaluate_rvalue(ir->projector);
1649 instr->src[src_number].src_type = nir_tex_src_projector;
1650 src_number++;
1651 }
1652
1653 if (ir->shadow_comparitor != NULL) {
1654 instr->src[src_number].src = evaluate_rvalue(ir->shadow_comparitor);
1655 instr->src[src_number].src_type = nir_tex_src_comparitor;
1656 src_number++;
1657 }
1658
1659 if (ir->offset != NULL) {
1660 /* we don't support multiple offsets yet */
1661 assert(ir->offset->type->is_vector() || ir->offset->type->is_scalar());
1662
1663 ir_constant *const_offset = ir->offset->as_constant();
1664 if (const_offset != NULL) {
1665 for (unsigned i = 0; i < const_offset->type->vector_elements; i++)
1666 instr->const_offset[i] = const_offset->value.i[i];
1667 } else {
1668 instr->src[src_number].src = evaluate_rvalue(ir->offset);
1669 instr->src[src_number].src_type = nir_tex_src_offset;
1670 src_number++;
1671 }
1672 }
1673
1674 switch (ir->op) {
1675 case ir_txb:
1676 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.bias);
1677 instr->src[src_number].src_type = nir_tex_src_bias;
1678 src_number++;
1679 break;
1680
1681 case ir_txl:
1682 case ir_txf:
1683 case ir_txs:
1684 if (ir->lod_info.lod != NULL) {
1685 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.lod);
1686 instr->src[src_number].src_type = nir_tex_src_lod;
1687 src_number++;
1688 }
1689 break;
1690
1691 case ir_txd:
1692 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.grad.dPdx);
1693 instr->src[src_number].src_type = nir_tex_src_ddx;
1694 src_number++;
1695 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.grad.dPdy);
1696 instr->src[src_number].src_type = nir_tex_src_ddy;
1697 src_number++;
1698 break;
1699
1700 case ir_txf_ms:
1701 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.sample_index);
1702 instr->src[src_number].src_type = nir_tex_src_ms_index;
1703 src_number++;
1704 break;
1705
1706 case ir_tg4:
1707 instr->component = ir->lod_info.component->as_constant()->value.u[0];
1708 break;
1709
1710 default:
1711 break;
1712 }
1713
1714 assert(src_number == num_srcs);
1715
1716 add_instr(&instr->instr, nir_tex_instr_dest_size(instr));
1717 }
1718
1719 void
1720 nir_visitor::visit(ir_constant *ir)
1721 {
1722 /*
1723 * We don't know if this variable is an an array or struct that gets
1724 * dereferenced, so do the safe thing an make it a variable with a
1725 * constant initializer and return a dereference.
1726 */
1727
1728 nir_variable *var = ralloc(this->shader, nir_variable);
1729 var->name = ralloc_strdup(var, "const_temp");
1730 var->type = ir->type;
1731 var->data.mode = nir_var_local;
1732 var->data.read_only = true;
1733 var->constant_initializer = constant_copy(ir, var);
1734 exec_list_push_tail(&this->impl->locals, &var->node);
1735
1736 this->deref_head = nir_deref_var_create(this->shader, var);
1737 this->deref_tail = &this->deref_head->deref;
1738 }
1739
1740 void
1741 nir_visitor::visit(ir_dereference_variable *ir)
1742 {
1743 struct hash_entry *entry =
1744 _mesa_hash_table_search(this->var_table, ir->var);
1745 assert(entry);
1746 nir_variable *var = (nir_variable *) entry->data;
1747
1748 nir_deref_var *deref = nir_deref_var_create(this->shader, var);
1749 this->deref_head = deref;
1750 this->deref_tail = &deref->deref;
1751 }
1752
1753 void
1754 nir_visitor::visit(ir_dereference_record *ir)
1755 {
1756 ir->record->accept(this);
1757
1758 int field_index = this->deref_tail->type->field_index(ir->field);
1759 assert(field_index >= 0);
1760
1761 nir_deref_struct *deref = nir_deref_struct_create(this->deref_tail, field_index);
1762 deref->deref.type = ir->type;
1763 this->deref_tail->child = &deref->deref;
1764 this->deref_tail = &deref->deref;
1765 }
1766
1767 void
1768 nir_visitor::visit(ir_dereference_array *ir)
1769 {
1770 nir_deref_array *deref = nir_deref_array_create(this->shader);
1771 deref->deref.type = ir->type;
1772
1773 ir_constant *const_index = ir->array_index->as_constant();
1774 if (const_index != NULL) {
1775 deref->deref_array_type = nir_deref_array_type_direct;
1776 deref->base_offset = const_index->value.u[0];
1777 } else {
1778 deref->deref_array_type = nir_deref_array_type_indirect;
1779 deref->indirect = evaluate_rvalue(ir->array_index);
1780 }
1781
1782 ir->array->accept(this);
1783
1784 this->deref_tail->child = &deref->deref;
1785 ralloc_steal(this->deref_tail, deref);
1786 this->deref_tail = &deref->deref;
1787 }