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