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