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