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