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