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