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