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