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