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