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