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