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