nir: Implement __intrinsic_load_ssbo
[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 nir_ssa_dest_init(instr, dest, num_components, NULL);
1040
1041 nir_instr_insert_after_cf_list(this->cf_node_list, instr);
1042 this->result = instr;
1043 }
1044
1045 nir_src
1046 nir_visitor::evaluate_rvalue(ir_rvalue* ir)
1047 {
1048 ir->accept(this);
1049 if (ir->as_dereference() || ir->as_constant()) {
1050 /*
1051 * A dereference is being used on the right hand side, which means we
1052 * must emit a variable load.
1053 */
1054
1055 nir_intrinsic_instr *load_instr =
1056 nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_var);
1057 load_instr->num_components = ir->type->vector_elements;
1058 load_instr->variables[0] = this->deref_head;
1059 ralloc_steal(load_instr, load_instr->variables[0]);
1060 add_instr(&load_instr->instr, ir->type->vector_elements);
1061 }
1062
1063 nir_dest *dest = get_instr_dest(this->result);
1064 assert(dest->is_ssa);
1065
1066 return nir_src_for_ssa(&dest->ssa);
1067 }
1068
1069 nir_alu_instr *
1070 nir_visitor::emit(nir_op op, unsigned dest_size, nir_src *srcs)
1071 {
1072 nir_alu_instr *instr = nir_alu_instr_create(this->shader, op);
1073 for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++)
1074 instr->src[i].src = srcs[i];
1075 instr->dest.write_mask = (1 << dest_size) - 1;
1076 add_instr(&instr->instr, dest_size);
1077 return instr;
1078 }
1079
1080 nir_alu_instr *
1081 nir_visitor::emit(nir_op op, unsigned dest_size, nir_src src1)
1082 {
1083 assert(nir_op_infos[op].num_inputs == 1);
1084 return emit(op, dest_size, &src1);
1085 }
1086
1087 nir_alu_instr *
1088 nir_visitor::emit(nir_op op, unsigned dest_size, nir_src src1,
1089 nir_src src2)
1090 {
1091 assert(nir_op_infos[op].num_inputs == 2);
1092 nir_src srcs[] = { src1, src2 };
1093 return emit(op, dest_size, srcs);
1094 }
1095
1096 nir_alu_instr *
1097 nir_visitor::emit(nir_op op, unsigned dest_size, nir_src src1,
1098 nir_src src2, nir_src src3)
1099 {
1100 assert(nir_op_infos[op].num_inputs == 3);
1101 nir_src srcs[] = { src1, src2, src3 };
1102 return emit(op, dest_size, srcs);
1103 }
1104
1105 void
1106 nir_visitor::visit(ir_expression *ir)
1107 {
1108 /* Some special cases */
1109 switch (ir->operation) {
1110 case ir_binop_ubo_load: {
1111 ir_constant *const_index = ir->operands[1]->as_constant();
1112
1113 nir_intrinsic_op op;
1114 if (const_index) {
1115 op = nir_intrinsic_load_ubo;
1116 } else {
1117 op = nir_intrinsic_load_ubo_indirect;
1118 }
1119 nir_intrinsic_instr *load = nir_intrinsic_instr_create(this->shader, op);
1120 load->num_components = ir->type->vector_elements;
1121 load->const_index[0] = const_index ? const_index->value.u[0] : 0; /* base offset */
1122 load->src[0] = evaluate_rvalue(ir->operands[0]);
1123 if (!const_index)
1124 load->src[1] = evaluate_rvalue(ir->operands[1]);
1125 add_instr(&load->instr, ir->type->vector_elements);
1126
1127 /*
1128 * In UBO's, a true boolean value is any non-zero value, but we consider
1129 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1130 */
1131
1132 if (ir->type->base_type == GLSL_TYPE_BOOL) {
1133 nir_load_const_instr *const_zero = nir_load_const_instr_create(shader, 1);
1134 const_zero->value.u[0] = 0;
1135 nir_instr_insert_after_cf_list(this->cf_node_list, &const_zero->instr);
1136
1137 nir_alu_instr *compare = nir_alu_instr_create(shader, nir_op_ine);
1138 compare->src[0].src.is_ssa = true;
1139 compare->src[0].src.ssa = &load->dest.ssa;
1140 compare->src[1].src.is_ssa = true;
1141 compare->src[1].src.ssa = &const_zero->def;
1142 for (unsigned i = 0; i < ir->type->vector_elements; i++)
1143 compare->src[1].swizzle[i] = 0;
1144 compare->dest.write_mask = (1 << ir->type->vector_elements) - 1;
1145
1146 add_instr(&compare->instr, ir->type->vector_elements);
1147 }
1148
1149 return;
1150 }
1151
1152 case ir_unop_interpolate_at_centroid:
1153 case ir_binop_interpolate_at_offset:
1154 case ir_binop_interpolate_at_sample: {
1155 ir_dereference *deref = ir->operands[0]->as_dereference();
1156 ir_swizzle *swizzle = NULL;
1157 if (!deref) {
1158 /* the api does not allow a swizzle here, but the varying packing code
1159 * may have pushed one into here.
1160 */
1161 swizzle = ir->operands[0]->as_swizzle();
1162 assert(swizzle);
1163 deref = swizzle->val->as_dereference();
1164 assert(deref);
1165 }
1166
1167 deref->accept(this);
1168
1169 nir_intrinsic_op op;
1170 if (this->deref_head->var->data.mode == nir_var_shader_in) {
1171 switch (ir->operation) {
1172 case ir_unop_interpolate_at_centroid:
1173 op = nir_intrinsic_interp_var_at_centroid;
1174 break;
1175 case ir_binop_interpolate_at_offset:
1176 op = nir_intrinsic_interp_var_at_offset;
1177 break;
1178 case ir_binop_interpolate_at_sample:
1179 op = nir_intrinsic_interp_var_at_sample;
1180 break;
1181 default:
1182 unreachable("Invalid interpolation intrinsic");
1183 }
1184 } else {
1185 /* This case can happen if the vertex shader does not write the
1186 * given varying. In this case, the linker will lower it to a
1187 * global variable. Since interpolating a variable makes no
1188 * sense, we'll just turn it into a load which will probably
1189 * eventually end up as an SSA definition.
1190 */
1191 assert(this->deref_head->var->data.mode == nir_var_global);
1192 op = nir_intrinsic_load_var;
1193 }
1194
1195 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(shader, op);
1196 intrin->num_components = deref->type->vector_elements;
1197 intrin->variables[0] = this->deref_head;
1198 ralloc_steal(intrin, intrin->variables[0]);
1199
1200 if (intrin->intrinsic == nir_intrinsic_interp_var_at_offset ||
1201 intrin->intrinsic == nir_intrinsic_interp_var_at_sample)
1202 intrin->src[0] = evaluate_rvalue(ir->operands[1]);
1203
1204 add_instr(&intrin->instr, deref->type->vector_elements);
1205
1206 if (swizzle) {
1207 nir_alu_instr *mov = nir_alu_instr_create(shader, nir_op_imov);
1208 mov->dest.write_mask = (1 << swizzle->type->vector_elements) - 1;
1209 mov->src[0].src.is_ssa = true;
1210 mov->src[0].src.ssa = &intrin->dest.ssa;
1211
1212 mov->src[0].swizzle[0] = swizzle->mask.x;
1213 mov->src[0].swizzle[1] = swizzle->mask.y;
1214 mov->src[0].swizzle[2] = swizzle->mask.z;
1215 mov->src[0].swizzle[3] = swizzle->mask.w;
1216 for (unsigned i = deref->type->vector_elements; i < 4; i++)
1217 mov->src[0].swizzle[i] = 0;
1218
1219 add_instr(&mov->instr, swizzle->type->vector_elements);
1220 }
1221
1222 return;
1223 }
1224
1225 default:
1226 break;
1227 }
1228
1229 nir_src srcs[4];
1230 for (unsigned i = 0; i < ir->get_num_operands(); i++)
1231 srcs[i] = evaluate_rvalue(ir->operands[i]);
1232
1233 glsl_base_type types[4];
1234 for (unsigned i = 0; i < ir->get_num_operands(); i++)
1235 if (supports_ints)
1236 types[i] = ir->operands[i]->type->base_type;
1237 else
1238 types[i] = GLSL_TYPE_FLOAT;
1239
1240 glsl_base_type out_type;
1241 if (supports_ints)
1242 out_type = ir->type->base_type;
1243 else
1244 out_type = GLSL_TYPE_FLOAT;
1245
1246 unsigned dest_size = ir->type->vector_elements;
1247
1248 nir_alu_instr *instr;
1249 nir_op op;
1250
1251 switch (ir->operation) {
1252 case ir_unop_bit_not: emit(nir_op_inot, dest_size, srcs); break;
1253 case ir_unop_logic_not:
1254 emit(supports_ints ? nir_op_inot : nir_op_fnot, dest_size, srcs);
1255 break;
1256 case ir_unop_neg:
1257 instr = emit(types[0] == GLSL_TYPE_FLOAT ? nir_op_fneg : nir_op_ineg,
1258 dest_size, srcs);
1259 break;
1260 case ir_unop_abs:
1261 instr = emit(types[0] == GLSL_TYPE_FLOAT ? nir_op_fabs : nir_op_iabs,
1262 dest_size, srcs);
1263 break;
1264 case ir_unop_saturate:
1265 assert(types[0] == GLSL_TYPE_FLOAT);
1266 instr = emit(nir_op_fsat, dest_size, srcs);
1267 break;
1268 case ir_unop_sign:
1269 emit(types[0] == GLSL_TYPE_FLOAT ? nir_op_fsign : nir_op_isign,
1270 dest_size, srcs);
1271 break;
1272 case ir_unop_rcp: emit(nir_op_frcp, dest_size, srcs); break;
1273 case ir_unop_rsq: emit(nir_op_frsq, dest_size, srcs); break;
1274 case ir_unop_sqrt: emit(nir_op_fsqrt, dest_size, srcs); break;
1275 case ir_unop_exp: unreachable("ir_unop_exp should have been lowered");
1276 case ir_unop_log: unreachable("ir_unop_log should have been lowered");
1277 case ir_unop_exp2: emit(nir_op_fexp2, dest_size, srcs); break;
1278 case ir_unop_log2: emit(nir_op_flog2, dest_size, srcs); break;
1279 case ir_unop_i2f:
1280 emit(supports_ints ? nir_op_i2f : nir_op_fmov, dest_size, srcs);
1281 break;
1282 case ir_unop_u2f:
1283 emit(supports_ints ? nir_op_u2f : nir_op_fmov, dest_size, srcs);
1284 break;
1285 case ir_unop_b2f:
1286 emit(supports_ints ? nir_op_b2f : nir_op_fmov, dest_size, srcs);
1287 break;
1288 case ir_unop_f2i: emit(nir_op_f2i, dest_size, srcs); break;
1289 case ir_unop_f2u: emit(nir_op_f2u, dest_size, srcs); break;
1290 case ir_unop_f2b: emit(nir_op_f2b, dest_size, srcs); break;
1291 case ir_unop_i2b: emit(nir_op_i2b, dest_size, srcs); break;
1292 case ir_unop_b2i: emit(nir_op_b2i, dest_size, srcs); break;
1293 case ir_unop_i2u:
1294 case ir_unop_u2i:
1295 case ir_unop_bitcast_i2f:
1296 case ir_unop_bitcast_f2i:
1297 case ir_unop_bitcast_u2f:
1298 case ir_unop_bitcast_f2u:
1299 case ir_unop_subroutine_to_int:
1300 /* no-op */
1301 emit(nir_op_imov, dest_size, srcs);
1302 break;
1303 case ir_unop_any:
1304 switch (ir->operands[0]->type->vector_elements) {
1305 case 2:
1306 emit(supports_ints ? nir_op_bany2 : nir_op_fany2,
1307 dest_size, srcs);
1308 break;
1309 case 3:
1310 emit(supports_ints ? nir_op_bany3 : nir_op_fany3,
1311 dest_size, srcs);
1312 break;
1313 case 4:
1314 emit(supports_ints ? nir_op_bany4 : nir_op_fany4,
1315 dest_size, srcs);
1316 break;
1317 default:
1318 unreachable("not reached");
1319 }
1320 break;
1321 case ir_unop_trunc: emit(nir_op_ftrunc, dest_size, srcs); break;
1322 case ir_unop_ceil: emit(nir_op_fceil, dest_size, srcs); break;
1323 case ir_unop_floor: emit(nir_op_ffloor, dest_size, srcs); break;
1324 case ir_unop_fract: emit(nir_op_ffract, dest_size, srcs); break;
1325 case ir_unop_round_even: emit(nir_op_fround_even, dest_size, srcs); break;
1326 case ir_unop_sin: emit(nir_op_fsin, dest_size, srcs); break;
1327 case ir_unop_cos: emit(nir_op_fcos, dest_size, srcs); break;
1328 case ir_unop_dFdx: emit(nir_op_fddx, dest_size, srcs); break;
1329 case ir_unop_dFdy: emit(nir_op_fddy, dest_size, srcs); break;
1330 case ir_unop_dFdx_fine: emit(nir_op_fddx_fine, dest_size, srcs); break;
1331 case ir_unop_dFdy_fine: emit(nir_op_fddy_fine, dest_size, srcs); break;
1332 case ir_unop_dFdx_coarse: emit(nir_op_fddx_coarse, dest_size, srcs); break;
1333 case ir_unop_dFdy_coarse: emit(nir_op_fddy_coarse, dest_size, srcs); break;
1334 case ir_unop_pack_snorm_2x16:
1335 emit(nir_op_pack_snorm_2x16, dest_size, srcs);
1336 break;
1337 case ir_unop_pack_snorm_4x8:
1338 emit(nir_op_pack_snorm_4x8, dest_size, srcs);
1339 break;
1340 case ir_unop_pack_unorm_2x16:
1341 emit(nir_op_pack_unorm_2x16, dest_size, srcs);
1342 break;
1343 case ir_unop_pack_unorm_4x8:
1344 emit(nir_op_pack_unorm_4x8, dest_size, srcs);
1345 break;
1346 case ir_unop_pack_half_2x16:
1347 emit(nir_op_pack_half_2x16, dest_size, srcs);
1348 break;
1349 case ir_unop_unpack_snorm_2x16:
1350 emit(nir_op_unpack_snorm_2x16, dest_size, srcs);
1351 break;
1352 case ir_unop_unpack_snorm_4x8:
1353 emit(nir_op_unpack_snorm_4x8, dest_size, srcs);
1354 break;
1355 case ir_unop_unpack_unorm_2x16:
1356 emit(nir_op_unpack_unorm_2x16, dest_size, srcs);
1357 break;
1358 case ir_unop_unpack_unorm_4x8:
1359 emit(nir_op_unpack_unorm_4x8, dest_size, srcs);
1360 break;
1361 case ir_unop_unpack_half_2x16:
1362 emit(nir_op_unpack_half_2x16, dest_size, srcs);
1363 break;
1364 case ir_unop_unpack_half_2x16_split_x:
1365 emit(nir_op_unpack_half_2x16_split_x, dest_size, srcs);
1366 break;
1367 case ir_unop_unpack_half_2x16_split_y:
1368 emit(nir_op_unpack_half_2x16_split_y, dest_size, srcs);
1369 break;
1370 case ir_unop_bitfield_reverse:
1371 emit(nir_op_bitfield_reverse, dest_size, srcs);
1372 break;
1373 case ir_unop_bit_count:
1374 emit(nir_op_bit_count, dest_size, srcs);
1375 break;
1376 case ir_unop_find_msb:
1377 switch (types[0]) {
1378 case GLSL_TYPE_UINT:
1379 emit(nir_op_ufind_msb, dest_size, srcs);
1380 break;
1381 case GLSL_TYPE_INT:
1382 emit(nir_op_ifind_msb, dest_size, srcs);
1383 break;
1384 default:
1385 unreachable("Invalid type for findMSB()");
1386 }
1387 break;
1388 case ir_unop_find_lsb:
1389 emit(nir_op_find_lsb, dest_size, srcs);
1390 break;
1391
1392 case ir_unop_noise:
1393 switch (ir->type->vector_elements) {
1394 case 1:
1395 switch (ir->operands[0]->type->vector_elements) {
1396 case 1: emit(nir_op_fnoise1_1, dest_size, srcs); break;
1397 case 2: emit(nir_op_fnoise1_2, dest_size, srcs); break;
1398 case 3: emit(nir_op_fnoise1_3, dest_size, srcs); break;
1399 case 4: emit(nir_op_fnoise1_4, dest_size, srcs); break;
1400 default: unreachable("not reached");
1401 }
1402 break;
1403 case 2:
1404 switch (ir->operands[0]->type->vector_elements) {
1405 case 1: emit(nir_op_fnoise2_1, dest_size, srcs); break;
1406 case 2: emit(nir_op_fnoise2_2, dest_size, srcs); break;
1407 case 3: emit(nir_op_fnoise2_3, dest_size, srcs); break;
1408 case 4: emit(nir_op_fnoise2_4, dest_size, srcs); break;
1409 default: unreachable("not reached");
1410 }
1411 break;
1412 case 3:
1413 switch (ir->operands[0]->type->vector_elements) {
1414 case 1: emit(nir_op_fnoise3_1, dest_size, srcs); break;
1415 case 2: emit(nir_op_fnoise3_2, dest_size, srcs); break;
1416 case 3: emit(nir_op_fnoise3_3, dest_size, srcs); break;
1417 case 4: emit(nir_op_fnoise3_4, dest_size, srcs); break;
1418 default: unreachable("not reached");
1419 }
1420 break;
1421 case 4:
1422 switch (ir->operands[0]->type->vector_elements) {
1423 case 1: emit(nir_op_fnoise4_1, dest_size, srcs); break;
1424 case 2: emit(nir_op_fnoise4_2, dest_size, srcs); break;
1425 case 3: emit(nir_op_fnoise4_3, dest_size, srcs); break;
1426 case 4: emit(nir_op_fnoise4_4, dest_size, srcs); break;
1427 default: unreachable("not reached");
1428 }
1429 break;
1430 default:
1431 unreachable("not reached");
1432 }
1433 break;
1434 case ir_unop_get_buffer_size: {
1435 nir_intrinsic_instr *load = nir_intrinsic_instr_create(
1436 this->shader,
1437 nir_intrinsic_get_buffer_size);
1438 load->num_components = ir->type->vector_elements;
1439 load->src[0] = evaluate_rvalue(ir->operands[0]);
1440 add_instr(&load->instr, ir->type->vector_elements);
1441 return;
1442 }
1443
1444 case ir_binop_add:
1445 case ir_binop_sub:
1446 case ir_binop_mul:
1447 case ir_binop_div:
1448 case ir_binop_mod:
1449 case ir_binop_min:
1450 case ir_binop_max:
1451 case ir_binop_pow:
1452 case ir_binop_bit_and:
1453 case ir_binop_bit_or:
1454 case ir_binop_bit_xor:
1455 case ir_binop_logic_and:
1456 case ir_binop_logic_or:
1457 case ir_binop_logic_xor:
1458 case ir_binop_lshift:
1459 case ir_binop_rshift:
1460 switch (ir->operation) {
1461 case ir_binop_add:
1462 if (out_type == GLSL_TYPE_FLOAT)
1463 op = nir_op_fadd;
1464 else
1465 op = nir_op_iadd;
1466 break;
1467 case ir_binop_sub:
1468 if (out_type == GLSL_TYPE_FLOAT)
1469 op = nir_op_fsub;
1470 else
1471 op = nir_op_isub;
1472 break;
1473 case ir_binop_mul:
1474 if (out_type == GLSL_TYPE_FLOAT)
1475 op = nir_op_fmul;
1476 else
1477 op = nir_op_imul;
1478 break;
1479 case ir_binop_div:
1480 if (out_type == GLSL_TYPE_FLOAT)
1481 op = nir_op_fdiv;
1482 else if (out_type == GLSL_TYPE_INT)
1483 op = nir_op_idiv;
1484 else
1485 op = nir_op_udiv;
1486 break;
1487 case ir_binop_mod:
1488 if (out_type == GLSL_TYPE_FLOAT)
1489 op = nir_op_fmod;
1490 else
1491 op = nir_op_umod;
1492 break;
1493 case ir_binop_min:
1494 if (out_type == GLSL_TYPE_FLOAT)
1495 op = nir_op_fmin;
1496 else if (out_type == GLSL_TYPE_INT)
1497 op = nir_op_imin;
1498 else
1499 op = nir_op_umin;
1500 break;
1501 case ir_binop_max:
1502 if (out_type == GLSL_TYPE_FLOAT)
1503 op = nir_op_fmax;
1504 else if (out_type == GLSL_TYPE_INT)
1505 op = nir_op_imax;
1506 else
1507 op = nir_op_umax;
1508 break;
1509 case ir_binop_bit_and:
1510 op = nir_op_iand;
1511 break;
1512 case ir_binop_bit_or:
1513 op = nir_op_ior;
1514 break;
1515 case ir_binop_bit_xor:
1516 op = nir_op_ixor;
1517 break;
1518 case ir_binop_logic_and:
1519 if (supports_ints)
1520 op = nir_op_iand;
1521 else
1522 op = nir_op_fand;
1523 break;
1524 case ir_binop_logic_or:
1525 if (supports_ints)
1526 op = nir_op_ior;
1527 else
1528 op = nir_op_for;
1529 break;
1530 case ir_binop_logic_xor:
1531 if (supports_ints)
1532 op = nir_op_ixor;
1533 else
1534 op = nir_op_fxor;
1535 break;
1536 case ir_binop_lshift:
1537 op = nir_op_ishl;
1538 break;
1539 case ir_binop_rshift:
1540 if (out_type == GLSL_TYPE_INT)
1541 op = nir_op_ishr;
1542 else
1543 op = nir_op_ushr;
1544 break;
1545 case ir_binop_pow:
1546 op = nir_op_fpow;
1547 break;
1548
1549 default:
1550 unreachable("not reached");
1551 }
1552
1553 instr = emit(op, dest_size, srcs);
1554
1555 if (ir->operands[0]->type->vector_elements != 1 &&
1556 ir->operands[1]->type->vector_elements == 1) {
1557 for (unsigned i = 0; i < ir->operands[0]->type->vector_elements;
1558 i++) {
1559 instr->src[1].swizzle[i] = 0;
1560 }
1561 }
1562
1563 if (ir->operands[1]->type->vector_elements != 1 &&
1564 ir->operands[0]->type->vector_elements == 1) {
1565 for (unsigned i = 0; i < ir->operands[1]->type->vector_elements;
1566 i++) {
1567 instr->src[0].swizzle[i] = 0;
1568 }
1569 }
1570
1571 break;
1572 case ir_binop_imul_high:
1573 emit(out_type == GLSL_TYPE_UINT ? nir_op_umul_high : nir_op_imul_high,
1574 dest_size, srcs);
1575 break;
1576 case ir_binop_carry: emit(nir_op_uadd_carry, dest_size, srcs); break;
1577 case ir_binop_borrow: emit(nir_op_usub_borrow, dest_size, srcs); break;
1578 case ir_binop_less:
1579 if (supports_ints) {
1580 if (types[0] == GLSL_TYPE_FLOAT)
1581 emit(nir_op_flt, dest_size, srcs);
1582 else if (types[0] == GLSL_TYPE_INT)
1583 emit(nir_op_ilt, dest_size, srcs);
1584 else
1585 emit(nir_op_ult, dest_size, srcs);
1586 } else {
1587 emit(nir_op_slt, dest_size, srcs);
1588 }
1589 break;
1590 case ir_binop_greater:
1591 if (supports_ints) {
1592 if (types[0] == GLSL_TYPE_FLOAT)
1593 emit(nir_op_flt, dest_size, srcs[1], srcs[0]);
1594 else if (types[0] == GLSL_TYPE_INT)
1595 emit(nir_op_ilt, dest_size, srcs[1], srcs[0]);
1596 else
1597 emit(nir_op_ult, dest_size, srcs[1], srcs[0]);
1598 } else {
1599 emit(nir_op_slt, dest_size, srcs[1], srcs[0]);
1600 }
1601 break;
1602 case ir_binop_lequal:
1603 if (supports_ints) {
1604 if (types[0] == GLSL_TYPE_FLOAT)
1605 emit(nir_op_fge, dest_size, srcs[1], srcs[0]);
1606 else if (types[0] == GLSL_TYPE_INT)
1607 emit(nir_op_ige, dest_size, srcs[1], srcs[0]);
1608 else
1609 emit(nir_op_uge, dest_size, srcs[1], srcs[0]);
1610 } else {
1611 emit(nir_op_slt, dest_size, srcs[1], srcs[0]);
1612 }
1613 break;
1614 case ir_binop_gequal:
1615 if (supports_ints) {
1616 if (types[0] == GLSL_TYPE_FLOAT)
1617 emit(nir_op_fge, dest_size, srcs);
1618 else if (types[0] == GLSL_TYPE_INT)
1619 emit(nir_op_ige, dest_size, srcs);
1620 else
1621 emit(nir_op_uge, dest_size, srcs);
1622 } else {
1623 emit(nir_op_slt, dest_size, srcs);
1624 }
1625 break;
1626 case ir_binop_equal:
1627 if (supports_ints) {
1628 if (types[0] == GLSL_TYPE_FLOAT)
1629 emit(nir_op_feq, dest_size, srcs);
1630 else
1631 emit(nir_op_ieq, dest_size, srcs);
1632 } else {
1633 emit(nir_op_seq, dest_size, srcs);
1634 }
1635 break;
1636 case ir_binop_nequal:
1637 if (supports_ints) {
1638 if (types[0] == GLSL_TYPE_FLOAT)
1639 emit(nir_op_fne, dest_size, srcs);
1640 else
1641 emit(nir_op_ine, dest_size, srcs);
1642 } else {
1643 emit(nir_op_sne, dest_size, srcs);
1644 }
1645 break;
1646 case ir_binop_all_equal:
1647 if (supports_ints) {
1648 if (types[0] == GLSL_TYPE_FLOAT) {
1649 switch (ir->operands[0]->type->vector_elements) {
1650 case 1: emit(nir_op_feq, dest_size, srcs); break;
1651 case 2: emit(nir_op_ball_fequal2, dest_size, srcs); break;
1652 case 3: emit(nir_op_ball_fequal3, dest_size, srcs); break;
1653 case 4: emit(nir_op_ball_fequal4, dest_size, srcs); break;
1654 default:
1655 unreachable("not reached");
1656 }
1657 } else {
1658 switch (ir->operands[0]->type->vector_elements) {
1659 case 1: emit(nir_op_ieq, dest_size, srcs); break;
1660 case 2: emit(nir_op_ball_iequal2, dest_size, srcs); break;
1661 case 3: emit(nir_op_ball_iequal3, dest_size, srcs); break;
1662 case 4: emit(nir_op_ball_iequal4, dest_size, srcs); break;
1663 default:
1664 unreachable("not reached");
1665 }
1666 }
1667 } else {
1668 switch (ir->operands[0]->type->vector_elements) {
1669 case 1: emit(nir_op_seq, dest_size, srcs); break;
1670 case 2: emit(nir_op_fall_equal2, dest_size, srcs); break;
1671 case 3: emit(nir_op_fall_equal3, dest_size, srcs); break;
1672 case 4: emit(nir_op_fall_equal4, dest_size, srcs); break;
1673 default:
1674 unreachable("not reached");
1675 }
1676 }
1677 break;
1678 case ir_binop_any_nequal:
1679 if (supports_ints) {
1680 if (types[0] == GLSL_TYPE_FLOAT) {
1681 switch (ir->operands[0]->type->vector_elements) {
1682 case 1: emit(nir_op_fne, dest_size, srcs); break;
1683 case 2: emit(nir_op_bany_fnequal2, dest_size, srcs); break;
1684 case 3: emit(nir_op_bany_fnequal3, dest_size, srcs); break;
1685 case 4: emit(nir_op_bany_fnequal4, dest_size, srcs); break;
1686 default:
1687 unreachable("not reached");
1688 }
1689 } else {
1690 switch (ir->operands[0]->type->vector_elements) {
1691 case 1: emit(nir_op_ine, dest_size, srcs); break;
1692 case 2: emit(nir_op_bany_inequal2, dest_size, srcs); break;
1693 case 3: emit(nir_op_bany_inequal3, dest_size, srcs); break;
1694 case 4: emit(nir_op_bany_inequal4, dest_size, srcs); break;
1695 default:
1696 unreachable("not reached");
1697 }
1698 }
1699 } else {
1700 switch (ir->operands[0]->type->vector_elements) {
1701 case 1: emit(nir_op_sne, dest_size, srcs); break;
1702 case 2: emit(nir_op_fany_nequal2, dest_size, srcs); break;
1703 case 3: emit(nir_op_fany_nequal3, dest_size, srcs); break;
1704 case 4: emit(nir_op_fany_nequal4, dest_size, srcs); break;
1705 default:
1706 unreachable("not reached");
1707 }
1708 }
1709 break;
1710 case ir_binop_dot:
1711 switch (ir->operands[0]->type->vector_elements) {
1712 case 2: emit(nir_op_fdot2, dest_size, srcs); break;
1713 case 3: emit(nir_op_fdot3, dest_size, srcs); break;
1714 case 4: emit(nir_op_fdot4, dest_size, srcs); break;
1715 default:
1716 unreachable("not reached");
1717 }
1718 break;
1719
1720 case ir_binop_pack_half_2x16_split:
1721 emit(nir_op_pack_half_2x16_split, dest_size, srcs);
1722 break;
1723 case ir_binop_bfm: emit(nir_op_bfm, dest_size, srcs); break;
1724 case ir_binop_ldexp: emit(nir_op_ldexp, dest_size, srcs); break;
1725 case ir_triop_fma: emit(nir_op_ffma, dest_size, srcs); break;
1726 case ir_triop_lrp:
1727 instr = emit(nir_op_flrp, dest_size, srcs);
1728 if (ir->operands[0]->type->vector_elements != 1 &&
1729 ir->operands[2]->type->vector_elements == 1) {
1730 for (unsigned i = 0; i < ir->operands[0]->type->vector_elements;
1731 i++) {
1732 instr->src[2].swizzle[i] = 0;
1733 }
1734 }
1735 break;
1736 case ir_triop_csel:
1737 if (supports_ints)
1738 emit(nir_op_bcsel, dest_size, srcs);
1739 else
1740 emit(nir_op_fcsel, dest_size, srcs);
1741 break;
1742 case ir_triop_bfi:
1743 instr = emit(nir_op_bfi, dest_size, srcs);
1744 for (unsigned i = 0; i < ir->operands[1]->type->vector_elements; i++) {
1745 instr->src[0].swizzle[i] = 0;
1746 }
1747 break;
1748 case ir_triop_bitfield_extract:
1749 instr = emit(out_type == GLSL_TYPE_INT ? nir_op_ibitfield_extract :
1750 nir_op_ubitfield_extract, dest_size, srcs);
1751 for (unsigned i = 0; i < ir->operands[0]->type->vector_elements; i++) {
1752 instr->src[1].swizzle[i] = 0;
1753 instr->src[2].swizzle[i] = 0;
1754 }
1755 break;
1756 case ir_quadop_bitfield_insert:
1757 instr = emit(nir_op_bitfield_insert, dest_size, srcs);
1758 for (unsigned i = 0; i < ir->operands[0]->type->vector_elements; i++) {
1759 instr->src[2].swizzle[i] = 0;
1760 instr->src[3].swizzle[i] = 0;
1761 }
1762 break;
1763 case ir_quadop_vector:
1764 switch (ir->type->vector_elements) {
1765 case 2: emit(nir_op_vec2, dest_size, srcs); break;
1766 case 3: emit(nir_op_vec3, dest_size, srcs); break;
1767 case 4: emit(nir_op_vec4, dest_size, srcs); break;
1768 default: unreachable("not reached");
1769 }
1770 break;
1771
1772 default:
1773 unreachable("not reached");
1774 }
1775 }
1776
1777 void
1778 nir_visitor::visit(ir_swizzle *ir)
1779 {
1780 nir_alu_instr *instr = emit(supports_ints ? nir_op_imov : nir_op_fmov,
1781 ir->type->vector_elements,
1782 evaluate_rvalue(ir->val));
1783
1784 unsigned swizzle[4] = { ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w };
1785 for (unsigned i = 0; i < ir->type->vector_elements; i++)
1786 instr->src[0].swizzle[i] = swizzle[i];
1787 }
1788
1789 void
1790 nir_visitor::visit(ir_texture *ir)
1791 {
1792 unsigned num_srcs;
1793 nir_texop op;
1794 switch (ir->op) {
1795 case ir_tex:
1796 op = nir_texop_tex;
1797 num_srcs = 1; /* coordinate */
1798 break;
1799
1800 case ir_txb:
1801 case ir_txl:
1802 op = (ir->op == ir_txb) ? nir_texop_txb : nir_texop_txl;
1803 num_srcs = 2; /* coordinate, bias/lod */
1804 break;
1805
1806 case ir_txd:
1807 op = nir_texop_txd; /* coordinate, dPdx, dPdy */
1808 num_srcs = 3;
1809 break;
1810
1811 case ir_txf:
1812 op = nir_texop_txf;
1813 if (ir->lod_info.lod != NULL)
1814 num_srcs = 2; /* coordinate, lod */
1815 else
1816 num_srcs = 1; /* coordinate */
1817 break;
1818
1819 case ir_txf_ms:
1820 op = nir_texop_txf_ms;
1821 num_srcs = 2; /* coordinate, sample_index */
1822 break;
1823
1824 case ir_txs:
1825 op = nir_texop_txs;
1826 if (ir->lod_info.lod != NULL)
1827 num_srcs = 1; /* lod */
1828 else
1829 num_srcs = 0;
1830 break;
1831
1832 case ir_lod:
1833 op = nir_texop_lod;
1834 num_srcs = 1; /* coordinate */
1835 break;
1836
1837 case ir_tg4:
1838 op = nir_texop_tg4;
1839 num_srcs = 1; /* coordinate */
1840 break;
1841
1842 case ir_query_levels:
1843 op = nir_texop_query_levels;
1844 num_srcs = 0;
1845 break;
1846
1847 case ir_texture_samples:
1848 op = nir_texop_texture_samples;
1849 num_srcs = 0;
1850 break;
1851
1852 default:
1853 unreachable("not reached");
1854 }
1855
1856 if (ir->projector != NULL)
1857 num_srcs++;
1858 if (ir->shadow_comparitor != NULL)
1859 num_srcs++;
1860 if (ir->offset != NULL && ir->offset->as_constant() == NULL)
1861 num_srcs++;
1862
1863 nir_tex_instr *instr = nir_tex_instr_create(this->shader, num_srcs);
1864
1865 instr->op = op;
1866 instr->sampler_dim =
1867 (glsl_sampler_dim) ir->sampler->type->sampler_dimensionality;
1868 instr->is_array = ir->sampler->type->sampler_array;
1869 instr->is_shadow = ir->sampler->type->sampler_shadow;
1870 if (instr->is_shadow)
1871 instr->is_new_style_shadow = (ir->type->vector_elements == 1);
1872 switch (ir->type->base_type) {
1873 case GLSL_TYPE_FLOAT:
1874 instr->dest_type = nir_type_float;
1875 break;
1876 case GLSL_TYPE_INT:
1877 instr->dest_type = nir_type_int;
1878 break;
1879 case GLSL_TYPE_UINT:
1880 instr->dest_type = nir_type_unsigned;
1881 break;
1882 default:
1883 unreachable("not reached");
1884 }
1885
1886 instr->sampler = evaluate_deref(&instr->instr, ir->sampler);
1887
1888 unsigned src_number = 0;
1889
1890 if (ir->coordinate != NULL) {
1891 instr->coord_components = ir->coordinate->type->vector_elements;
1892 instr->src[src_number].src = evaluate_rvalue(ir->coordinate);
1893 instr->src[src_number].src_type = nir_tex_src_coord;
1894 src_number++;
1895 }
1896
1897 if (ir->projector != NULL) {
1898 instr->src[src_number].src = evaluate_rvalue(ir->projector);
1899 instr->src[src_number].src_type = nir_tex_src_projector;
1900 src_number++;
1901 }
1902
1903 if (ir->shadow_comparitor != NULL) {
1904 instr->src[src_number].src = evaluate_rvalue(ir->shadow_comparitor);
1905 instr->src[src_number].src_type = nir_tex_src_comparitor;
1906 src_number++;
1907 }
1908
1909 if (ir->offset != NULL) {
1910 /* we don't support multiple offsets yet */
1911 assert(ir->offset->type->is_vector() || ir->offset->type->is_scalar());
1912
1913 ir_constant *const_offset = ir->offset->as_constant();
1914 if (const_offset != NULL) {
1915 for (unsigned i = 0; i < const_offset->type->vector_elements; i++)
1916 instr->const_offset[i] = const_offset->value.i[i];
1917 } else {
1918 instr->src[src_number].src = evaluate_rvalue(ir->offset);
1919 instr->src[src_number].src_type = nir_tex_src_offset;
1920 src_number++;
1921 }
1922 }
1923
1924 switch (ir->op) {
1925 case ir_txb:
1926 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.bias);
1927 instr->src[src_number].src_type = nir_tex_src_bias;
1928 src_number++;
1929 break;
1930
1931 case ir_txl:
1932 case ir_txf:
1933 case ir_txs:
1934 if (ir->lod_info.lod != NULL) {
1935 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.lod);
1936 instr->src[src_number].src_type = nir_tex_src_lod;
1937 src_number++;
1938 }
1939 break;
1940
1941 case ir_txd:
1942 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.grad.dPdx);
1943 instr->src[src_number].src_type = nir_tex_src_ddx;
1944 src_number++;
1945 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.grad.dPdy);
1946 instr->src[src_number].src_type = nir_tex_src_ddy;
1947 src_number++;
1948 break;
1949
1950 case ir_txf_ms:
1951 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.sample_index);
1952 instr->src[src_number].src_type = nir_tex_src_ms_index;
1953 src_number++;
1954 break;
1955
1956 case ir_tg4:
1957 instr->component = ir->lod_info.component->as_constant()->value.u[0];
1958 break;
1959
1960 default:
1961 break;
1962 }
1963
1964 assert(src_number == num_srcs);
1965
1966 add_instr(&instr->instr, nir_tex_instr_dest_size(instr));
1967 }
1968
1969 void
1970 nir_visitor::visit(ir_constant *ir)
1971 {
1972 /*
1973 * We don't know if this variable is an an array or struct that gets
1974 * dereferenced, so do the safe thing an make it a variable with a
1975 * constant initializer and return a dereference.
1976 */
1977
1978 nir_variable *var = ralloc(this->shader, nir_variable);
1979 var->name = ralloc_strdup(var, "const_temp");
1980 var->type = ir->type;
1981 var->data.mode = nir_var_local;
1982 var->data.read_only = true;
1983 var->constant_initializer = constant_copy(ir, var);
1984 exec_list_push_tail(&this->impl->locals, &var->node);
1985
1986 this->deref_head = nir_deref_var_create(this->shader, var);
1987 this->deref_tail = &this->deref_head->deref;
1988 }
1989
1990 void
1991 nir_visitor::visit(ir_dereference_variable *ir)
1992 {
1993 struct hash_entry *entry =
1994 _mesa_hash_table_search(this->var_table, ir->var);
1995 assert(entry);
1996 nir_variable *var = (nir_variable *) entry->data;
1997
1998 nir_deref_var *deref = nir_deref_var_create(this->shader, var);
1999 this->deref_head = deref;
2000 this->deref_tail = &deref->deref;
2001 }
2002
2003 void
2004 nir_visitor::visit(ir_dereference_record *ir)
2005 {
2006 ir->record->accept(this);
2007
2008 int field_index = this->deref_tail->type->field_index(ir->field);
2009 assert(field_index >= 0);
2010
2011 nir_deref_struct *deref = nir_deref_struct_create(this->deref_tail, field_index);
2012 deref->deref.type = ir->type;
2013 this->deref_tail->child = &deref->deref;
2014 this->deref_tail = &deref->deref;
2015 }
2016
2017 void
2018 nir_visitor::visit(ir_dereference_array *ir)
2019 {
2020 nir_deref_array *deref = nir_deref_array_create(this->shader);
2021 deref->deref.type = ir->type;
2022
2023 ir_constant *const_index = ir->array_index->as_constant();
2024 if (const_index != NULL) {
2025 deref->deref_array_type = nir_deref_array_type_direct;
2026 deref->base_offset = const_index->value.u[0];
2027 } else {
2028 deref->deref_array_type = nir_deref_array_type_indirect;
2029 deref->indirect = evaluate_rvalue(ir->array_index);
2030 }
2031
2032 ir->array->accept(this);
2033
2034 this->deref_tail->child = &deref->deref;
2035 ralloc_steal(this->deref_tail, deref);
2036 this->deref_tail = &deref->deref;
2037 }
2038
2039 void
2040 nir_visitor::visit(ir_barrier *ir)
2041 {
2042 nir_intrinsic_instr *instr =
2043 nir_intrinsic_instr_create(this->shader, nir_intrinsic_barrier);
2044 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
2045 }