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