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