Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / compiler / 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 "nir_builder.h"
31 #include "compiler/glsl/ir_visitor.h"
32 #include "compiler/glsl/ir_hierarchical_visitor.h"
33 #include "compiler/glsl/ir.h"
34 #include "main/imports.h"
35
36 /*
37 * pass to lower GLSL IR to NIR
38 *
39 * This will lower variable dereferences to loads/stores of corresponding
40 * variables in NIR - the variables will be converted to registers in a later
41 * pass.
42 */
43
44 namespace {
45
46 class nir_visitor : public ir_visitor
47 {
48 public:
49 nir_visitor(nir_shader *shader, gl_shader *sh);
50 ~nir_visitor();
51
52 virtual void visit(ir_variable *);
53 virtual void visit(ir_function *);
54 virtual void visit(ir_function_signature *);
55 virtual void visit(ir_loop *);
56 virtual void visit(ir_if *);
57 virtual void visit(ir_discard *);
58 virtual void visit(ir_loop_jump *);
59 virtual void visit(ir_return *);
60 virtual void visit(ir_call *);
61 virtual void visit(ir_assignment *);
62 virtual void visit(ir_emit_vertex *);
63 virtual void visit(ir_end_primitive *);
64 virtual void visit(ir_expression *);
65 virtual void visit(ir_swizzle *);
66 virtual void visit(ir_texture *);
67 virtual void visit(ir_constant *);
68 virtual void visit(ir_dereference_variable *);
69 virtual void visit(ir_dereference_record *);
70 virtual void visit(ir_dereference_array *);
71 virtual void visit(ir_barrier *);
72
73 void create_function(ir_function_signature *ir);
74
75 private:
76 void add_instr(nir_instr *instr, unsigned num_components);
77 nir_ssa_def *evaluate_rvalue(ir_rvalue *ir);
78
79 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def **srcs);
80 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1);
81 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1,
82 nir_ssa_def *src2);
83 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1,
84 nir_ssa_def *src2, nir_ssa_def *src3);
85
86 bool supports_ints;
87
88 struct gl_shader *sh;
89
90 nir_shader *shader;
91 nir_function_impl *impl;
92 nir_builder b;
93 nir_ssa_def *result; /* result of the expression tree last visited */
94
95 nir_deref_var *evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir);
96
97 /* the head of the dereference chain we're creating */
98 nir_deref_var *deref_head;
99 /* the tail of the dereference chain we're creating */
100 nir_deref *deref_tail;
101
102 nir_variable *var; /* variable created by ir_variable visitor */
103
104 /* whether the IR we're operating on is per-function or global */
105 bool is_global;
106
107 /* map of ir_variable -> nir_variable */
108 struct hash_table *var_table;
109
110 /* map of ir_function_signature -> nir_function_overload */
111 struct hash_table *overload_table;
112 };
113
114 /*
115 * This visitor runs before the main visitor, calling create_function() for
116 * each function so that the main visitor can resolve forward references in
117 * calls.
118 */
119
120 class nir_function_visitor : public ir_hierarchical_visitor
121 {
122 public:
123 nir_function_visitor(nir_visitor *v) : visitor(v)
124 {
125 }
126 virtual ir_visitor_status visit_enter(ir_function *);
127
128 private:
129 nir_visitor *visitor;
130 };
131
132 }; /* end of anonymous namespace */
133
134 nir_shader *
135 glsl_to_nir(const struct gl_shader_program *shader_prog,
136 gl_shader_stage stage,
137 const nir_shader_compiler_options *options)
138 {
139 struct gl_shader *sh = shader_prog->_LinkedShaders[stage];
140
141 nir_shader *shader = nir_shader_create(NULL, stage, options);
142
143 nir_visitor v1(shader, sh);
144 nir_function_visitor v2(&v1);
145 v2.run(sh->ir);
146 visit_exec_list(sh->ir, &v1);
147
148 nir_function *main = NULL;
149 nir_foreach_function(shader, func) {
150 if (strcmp(func->name, "main") == 0) {
151 main = func;
152 break;
153 }
154 }
155 assert(main);
156
157 nir_lower_outputs_to_temporaries(shader, main);
158
159 shader->info.name = ralloc_asprintf(shader, "GLSL%d", shader_prog->Name);
160 if (shader_prog->Label)
161 shader->info.label = ralloc_strdup(shader, shader_prog->Label);
162 shader->info.num_textures = _mesa_fls(sh->Program->SamplersUsed);
163 shader->info.num_ubos = sh->NumUniformBlocks;
164 shader->info.num_abos = shader_prog->NumAtomicBuffers;
165 shader->info.num_ssbos = sh->NumShaderStorageBlocks;
166 shader->info.num_images = sh->NumImages;
167 shader->info.inputs_read = sh->Program->InputsRead;
168 shader->info.outputs_written = sh->Program->OutputsWritten;
169 shader->info.patch_inputs_read = sh->Program->PatchInputsRead;
170 shader->info.patch_outputs_written = sh->Program->PatchOutputsWritten;
171 shader->info.system_values_read = sh->Program->SystemValuesRead;
172 shader->info.uses_texture_gather = sh->Program->UsesGather;
173 shader->info.uses_clip_distance_out =
174 sh->Program->ClipDistanceArraySize != 0;
175 shader->info.separate_shader = shader_prog->SeparateShader;
176 shader->info.has_transform_feedback_varyings =
177 shader_prog->TransformFeedback.NumVarying > 0;
178
179 switch (stage) {
180 case MESA_SHADER_TESS_CTRL:
181 shader->info.tcs.vertices_out = shader_prog->TessCtrl.VerticesOut;
182 break;
183
184 case MESA_SHADER_GEOMETRY:
185 shader->info.gs.vertices_in = shader_prog->Geom.VerticesIn;
186 shader->info.gs.output_primitive = sh->Geom.OutputType;
187 shader->info.gs.vertices_out = sh->Geom.VerticesOut;
188 shader->info.gs.invocations = sh->Geom.Invocations;
189 shader->info.gs.uses_end_primitive = shader_prog->Geom.UsesEndPrimitive;
190 shader->info.gs.uses_streams = shader_prog->Geom.UsesStreams;
191 break;
192
193 case MESA_SHADER_FRAGMENT: {
194 struct gl_fragment_program *fp =
195 (struct gl_fragment_program *)sh->Program;
196
197 shader->info.fs.uses_discard = fp->UsesKill;
198 shader->info.fs.early_fragment_tests = sh->EarlyFragmentTests;
199 shader->info.fs.depth_layout = fp->FragDepthLayout;
200 break;
201 }
202
203 case MESA_SHADER_COMPUTE: {
204 struct gl_compute_program *cp = (struct gl_compute_program *)sh->Program;
205 shader->info.cs.local_size[0] = cp->LocalSize[0];
206 shader->info.cs.local_size[1] = cp->LocalSize[1];
207 shader->info.cs.local_size[2] = cp->LocalSize[2];
208 break;
209 }
210
211 default:
212 break; /* No stage-specific info */
213 }
214
215 return shader;
216 }
217
218 nir_visitor::nir_visitor(nir_shader *shader, gl_shader *sh)
219 {
220 this->supports_ints = shader->options->native_integers;
221 this->shader = shader;
222 this->sh = sh;
223 this->is_global = true;
224 this->var_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
225 _mesa_key_pointer_equal);
226 this->overload_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
227 _mesa_key_pointer_equal);
228 }
229
230 nir_visitor::~nir_visitor()
231 {
232 _mesa_hash_table_destroy(this->var_table, NULL);
233 _mesa_hash_table_destroy(this->overload_table, NULL);
234 }
235
236 nir_deref_var *
237 nir_visitor::evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir)
238 {
239 ir->accept(this);
240 ralloc_steal(mem_ctx, this->deref_head);
241 return this->deref_head;
242 }
243
244 static nir_constant *
245 constant_copy(ir_constant *ir, void *mem_ctx)
246 {
247 if (ir == NULL)
248 return NULL;
249
250 nir_constant *ret = ralloc(mem_ctx, nir_constant);
251
252 unsigned total_elems = ir->type->components();
253 unsigned i;
254
255 ret->num_elements = 0;
256 switch (ir->type->base_type) {
257 case GLSL_TYPE_UINT:
258 for (i = 0; i < total_elems; i++)
259 ret->value.u[i] = ir->value.u[i];
260 break;
261
262 case GLSL_TYPE_INT:
263 for (i = 0; i < total_elems; i++)
264 ret->value.i[i] = ir->value.i[i];
265 break;
266
267 case GLSL_TYPE_FLOAT:
268 for (i = 0; i < total_elems; i++)
269 ret->value.f[i] = ir->value.f[i];
270 break;
271
272 case GLSL_TYPE_BOOL:
273 for (i = 0; i < total_elems; i++)
274 ret->value.b[i] = ir->value.b[i];
275 break;
276
277 case GLSL_TYPE_STRUCT:
278 ret->elements = ralloc_array(mem_ctx, nir_constant *,
279 ir->type->length);
280 ret->num_elements = ir->type->length;
281
282 i = 0;
283 foreach_in_list(ir_constant, field, &ir->components) {
284 ret->elements[i] = constant_copy(field, mem_ctx);
285 i++;
286 }
287 break;
288
289 case GLSL_TYPE_ARRAY:
290 ret->elements = ralloc_array(mem_ctx, nir_constant *,
291 ir->type->length);
292 ret->num_elements = ir->type->length;
293
294 for (i = 0; i < ir->type->length; i++)
295 ret->elements[i] = constant_copy(ir->array_elements[i], mem_ctx);
296 break;
297
298 default:
299 unreachable("not reached");
300 }
301
302 return ret;
303 }
304
305 void
306 nir_visitor::visit(ir_variable *ir)
307 {
308 nir_variable *var = ralloc(shader, nir_variable);
309 var->type = ir->type;
310 var->name = ralloc_strdup(var, ir->name);
311
312 var->data.read_only = ir->data.read_only;
313 var->data.centroid = ir->data.centroid;
314 var->data.sample = ir->data.sample;
315 var->data.patch = ir->data.patch;
316 var->data.invariant = ir->data.invariant;
317 var->data.location = ir->data.location;
318
319 switch(ir->data.mode) {
320 case ir_var_auto:
321 case ir_var_temporary:
322 if (is_global)
323 var->data.mode = nir_var_global;
324 else
325 var->data.mode = nir_var_local;
326 break;
327
328 case ir_var_function_in:
329 case ir_var_function_out:
330 case ir_var_function_inout:
331 case ir_var_const_in:
332 var->data.mode = nir_var_local;
333 break;
334
335 case ir_var_shader_in:
336 if (shader->stage == MESA_SHADER_FRAGMENT &&
337 ir->data.location == VARYING_SLOT_FACE) {
338 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
339 var->data.location = SYSTEM_VALUE_FRONT_FACE;
340 var->data.mode = nir_var_system_value;
341 } else if (shader->stage == MESA_SHADER_GEOMETRY &&
342 ir->data.location == VARYING_SLOT_PRIMITIVE_ID) {
343 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
344 var->data.location = SYSTEM_VALUE_PRIMITIVE_ID;
345 var->data.mode = nir_var_system_value;
346 } else {
347 var->data.mode = nir_var_shader_in;
348 }
349 break;
350
351 case ir_var_shader_out:
352 var->data.mode = nir_var_shader_out;
353 break;
354
355 case ir_var_uniform:
356 var->data.mode = nir_var_uniform;
357 break;
358
359 case ir_var_shader_storage:
360 var->data.mode = nir_var_shader_storage;
361 break;
362
363 case ir_var_system_value:
364 var->data.mode = nir_var_system_value;
365 break;
366
367 default:
368 unreachable("not reached");
369 }
370
371 var->data.interpolation = ir->data.interpolation;
372 var->data.origin_upper_left = ir->data.origin_upper_left;
373 var->data.pixel_center_integer = ir->data.pixel_center_integer;
374 var->data.explicit_location = ir->data.explicit_location;
375 var->data.explicit_index = ir->data.explicit_index;
376 var->data.explicit_binding = ir->data.explicit_binding;
377 var->data.has_initializer = ir->data.has_initializer;
378 var->data.location_frac = ir->data.location_frac;
379 var->data.from_named_ifc_block_array = ir->data.from_named_ifc_block_array;
380 var->data.from_named_ifc_block_nonarray = ir->data.from_named_ifc_block_nonarray;
381
382 switch (ir->data.depth_layout) {
383 case ir_depth_layout_none:
384 var->data.depth_layout = nir_depth_layout_none;
385 break;
386 case ir_depth_layout_any:
387 var->data.depth_layout = nir_depth_layout_any;
388 break;
389 case ir_depth_layout_greater:
390 var->data.depth_layout = nir_depth_layout_greater;
391 break;
392 case ir_depth_layout_less:
393 var->data.depth_layout = nir_depth_layout_less;
394 break;
395 case ir_depth_layout_unchanged:
396 var->data.depth_layout = nir_depth_layout_unchanged;
397 break;
398 default:
399 unreachable("not reached");
400 }
401
402 var->data.index = ir->data.index;
403 var->data.descriptor_set = 0;
404 var->data.binding = ir->data.binding;
405 var->data.offset = ir->data.offset;
406 var->data.image.read_only = ir->data.image_read_only;
407 var->data.image.write_only = ir->data.image_write_only;
408 var->data.image.coherent = ir->data.image_coherent;
409 var->data.image._volatile = ir->data.image_volatile;
410 var->data.image.restrict_flag = ir->data.image_restrict;
411 var->data.image.format = ir->data.image_format;
412 var->data.max_array_access = ir->data.max_array_access;
413
414 var->num_state_slots = ir->get_num_state_slots();
415 if (var->num_state_slots > 0) {
416 var->state_slots = ralloc_array(var, nir_state_slot,
417 var->num_state_slots);
418
419 ir_state_slot *state_slots = ir->get_state_slots();
420 for (unsigned i = 0; i < var->num_state_slots; i++) {
421 for (unsigned j = 0; j < 5; j++)
422 var->state_slots[i].tokens[j] = state_slots[i].tokens[j];
423 var->state_slots[i].swizzle = state_slots[i].swizzle;
424 }
425 } else {
426 var->state_slots = NULL;
427 }
428
429 var->constant_initializer = constant_copy(ir->constant_initializer, var);
430
431 var->interface_type = ir->get_interface_type();
432
433 if (var->data.mode == nir_var_local)
434 nir_function_impl_add_variable(impl, var);
435 else
436 nir_shader_add_variable(shader, var);
437
438 _mesa_hash_table_insert(var_table, ir, var);
439 this->var = var;
440 }
441
442 ir_visitor_status
443 nir_function_visitor::visit_enter(ir_function *ir)
444 {
445 foreach_in_list(ir_function_signature, sig, &ir->signatures) {
446 visitor->create_function(sig);
447 }
448 return visit_continue_with_parent;
449 }
450
451 void
452 nir_visitor::create_function(ir_function_signature *ir)
453 {
454 if (ir->is_intrinsic)
455 return;
456
457 nir_function *func = nir_function_create(shader, ir->function_name());
458
459 unsigned num_params = ir->parameters.length();
460 func->num_params = num_params;
461 func->params = ralloc_array(shader, nir_parameter, num_params);
462
463 unsigned i = 0;
464 foreach_in_list(ir_variable, param, &ir->parameters) {
465 switch (param->data.mode) {
466 case ir_var_function_in:
467 func->params[i].param_type = nir_parameter_in;
468 break;
469
470 case ir_var_function_out:
471 func->params[i].param_type = nir_parameter_out;
472 break;
473
474 case ir_var_function_inout:
475 func->params[i].param_type = nir_parameter_inout;
476 break;
477
478 default:
479 unreachable("not reached");
480 }
481
482 func->params[i].type = param->type;
483 i++;
484 }
485
486 func->return_type = ir->return_type;
487
488 _mesa_hash_table_insert(this->overload_table, ir, func);
489 }
490
491 void
492 nir_visitor::visit(ir_function *ir)
493 {
494 foreach_in_list(ir_function_signature, sig, &ir->signatures)
495 sig->accept(this);
496 }
497
498 void
499 nir_visitor::visit(ir_function_signature *ir)
500 {
501 if (ir->is_intrinsic)
502 return;
503
504 struct hash_entry *entry =
505 _mesa_hash_table_search(this->overload_table, ir);
506
507 assert(entry);
508 nir_function *func = (nir_function *) entry->data;
509
510 if (ir->is_defined) {
511 nir_function_impl *impl = nir_function_impl_create(func);
512 this->impl = impl;
513
514 unsigned num_params = func->num_params;
515 impl->num_params = num_params;
516 impl->params = ralloc_array(this->shader, nir_variable *, num_params);
517 unsigned i = 0;
518 foreach_in_list(ir_variable, param, &ir->parameters) {
519 param->accept(this);
520 impl->params[i] = this->var;
521 i++;
522 }
523
524 if (func->return_type == glsl_type::void_type) {
525 impl->return_var = NULL;
526 } else {
527 impl->return_var = ralloc(this->shader, nir_variable);
528 impl->return_var->name = ralloc_strdup(impl->return_var,
529 "return_var");
530 impl->return_var->type = func->return_type;
531 }
532
533 this->is_global = false;
534
535 nir_builder_init(&b, impl);
536 b.cursor = nir_after_cf_list(&impl->body);
537 visit_exec_list(&ir->body, this);
538
539 this->is_global = true;
540 } else {
541 func->impl = NULL;
542 }
543 }
544
545 void
546 nir_visitor::visit(ir_loop *ir)
547 {
548 nir_loop *loop = nir_loop_create(this->shader);
549 nir_builder_cf_insert(&b, &loop->cf_node);
550
551 b.cursor = nir_after_cf_list(&loop->body);
552 visit_exec_list(&ir->body_instructions, this);
553 b.cursor = nir_after_cf_node(&loop->cf_node);
554 }
555
556 void
557 nir_visitor::visit(ir_if *ir)
558 {
559 nir_src condition =
560 nir_src_for_ssa(evaluate_rvalue(ir->condition));
561
562 nir_if *if_stmt = nir_if_create(this->shader);
563 if_stmt->condition = condition;
564 nir_builder_cf_insert(&b, &if_stmt->cf_node);
565
566 b.cursor = nir_after_cf_list(&if_stmt->then_list);
567 visit_exec_list(&ir->then_instructions, this);
568
569 b.cursor = nir_after_cf_list(&if_stmt->else_list);
570 visit_exec_list(&ir->else_instructions, this);
571
572 b.cursor = nir_after_cf_node(&if_stmt->cf_node);
573 }
574
575 void
576 nir_visitor::visit(ir_discard *ir)
577 {
578 /*
579 * discards aren't treated as control flow, because before we lower them
580 * they can appear anywhere in the shader and the stuff after them may still
581 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
582 * discards will be immediately followed by a return.
583 */
584
585 nir_intrinsic_instr *discard;
586 if (ir->condition) {
587 discard = nir_intrinsic_instr_create(this->shader,
588 nir_intrinsic_discard_if);
589 discard->src[0] =
590 nir_src_for_ssa(evaluate_rvalue(ir->condition));
591 } else {
592 discard = nir_intrinsic_instr_create(this->shader, nir_intrinsic_discard);
593 }
594
595 nir_builder_instr_insert(&b, &discard->instr);
596 }
597
598 void
599 nir_visitor::visit(ir_emit_vertex *ir)
600 {
601 nir_intrinsic_instr *instr =
602 nir_intrinsic_instr_create(this->shader, nir_intrinsic_emit_vertex);
603 instr->const_index[0] = ir->stream_id();
604 nir_builder_instr_insert(&b, &instr->instr);
605 }
606
607 void
608 nir_visitor::visit(ir_end_primitive *ir)
609 {
610 nir_intrinsic_instr *instr =
611 nir_intrinsic_instr_create(this->shader, nir_intrinsic_end_primitive);
612 instr->const_index[0] = ir->stream_id();
613 nir_builder_instr_insert(&b, &instr->instr);
614 }
615
616 void
617 nir_visitor::visit(ir_loop_jump *ir)
618 {
619 nir_jump_type type;
620 switch (ir->mode) {
621 case ir_loop_jump::jump_break:
622 type = nir_jump_break;
623 break;
624 case ir_loop_jump::jump_continue:
625 type = nir_jump_continue;
626 break;
627 default:
628 unreachable("not reached");
629 }
630
631 nir_jump_instr *instr = nir_jump_instr_create(this->shader, type);
632 nir_builder_instr_insert(&b, &instr->instr);
633 }
634
635 void
636 nir_visitor::visit(ir_return *ir)
637 {
638 if (ir->value != NULL) {
639 nir_intrinsic_instr *copy =
640 nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
641
642 copy->variables[0] = nir_deref_var_create(copy, this->impl->return_var);
643 copy->variables[1] = evaluate_deref(&copy->instr, ir->value);
644 }
645
646 nir_jump_instr *instr = nir_jump_instr_create(this->shader, nir_jump_return);
647 nir_builder_instr_insert(&b, &instr->instr);
648 }
649
650 void
651 nir_visitor::visit(ir_call *ir)
652 {
653 if (ir->callee->is_intrinsic) {
654 nir_intrinsic_op op;
655 if (strcmp(ir->callee_name(), "__intrinsic_atomic_read") == 0) {
656 op = nir_intrinsic_atomic_counter_read_var;
657 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_increment") == 0) {
658 op = nir_intrinsic_atomic_counter_inc_var;
659 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
660 op = nir_intrinsic_atomic_counter_dec_var;
661 } else if (strcmp(ir->callee_name(), "__intrinsic_image_load") == 0) {
662 op = nir_intrinsic_image_load;
663 } else if (strcmp(ir->callee_name(), "__intrinsic_image_store") == 0) {
664 op = nir_intrinsic_image_store;
665 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_add") == 0) {
666 op = nir_intrinsic_image_atomic_add;
667 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_min") == 0) {
668 op = nir_intrinsic_image_atomic_min;
669 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_max") == 0) {
670 op = nir_intrinsic_image_atomic_max;
671 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_and") == 0) {
672 op = nir_intrinsic_image_atomic_and;
673 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_or") == 0) {
674 op = nir_intrinsic_image_atomic_or;
675 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
676 op = nir_intrinsic_image_atomic_xor;
677 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
678 op = nir_intrinsic_image_atomic_exchange;
679 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
680 op = nir_intrinsic_image_atomic_comp_swap;
681 } else if (strcmp(ir->callee_name(), "__intrinsic_memory_barrier") == 0) {
682 op = nir_intrinsic_memory_barrier;
683 } else if (strcmp(ir->callee_name(), "__intrinsic_image_size") == 0) {
684 op = nir_intrinsic_image_size;
685 } else if (strcmp(ir->callee_name(), "__intrinsic_image_samples") == 0) {
686 op = nir_intrinsic_image_samples;
687 } else if (strcmp(ir->callee_name(), "__intrinsic_store_ssbo") == 0) {
688 op = nir_intrinsic_store_ssbo;
689 } else if (strcmp(ir->callee_name(), "__intrinsic_load_ssbo") == 0) {
690 op = nir_intrinsic_load_ssbo;
691 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_add_ssbo") == 0) {
692 op = nir_intrinsic_ssbo_atomic_add;
693 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_and_ssbo") == 0) {
694 op = nir_intrinsic_ssbo_atomic_and;
695 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_or_ssbo") == 0) {
696 op = nir_intrinsic_ssbo_atomic_or;
697 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_xor_ssbo") == 0) {
698 op = nir_intrinsic_ssbo_atomic_xor;
699 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_min_ssbo") == 0) {
700 assert(ir->return_deref);
701 if (ir->return_deref->type == glsl_type::int_type)
702 op = nir_intrinsic_ssbo_atomic_imin;
703 else if (ir->return_deref->type == glsl_type::uint_type)
704 op = nir_intrinsic_ssbo_atomic_umin;
705 else
706 unreachable("Invalid type");
707 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_max_ssbo") == 0) {
708 assert(ir->return_deref);
709 if (ir->return_deref->type == glsl_type::int_type)
710 op = nir_intrinsic_ssbo_atomic_imax;
711 else if (ir->return_deref->type == glsl_type::uint_type)
712 op = nir_intrinsic_ssbo_atomic_umax;
713 else
714 unreachable("Invalid type");
715 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_exchange_ssbo") == 0) {
716 op = nir_intrinsic_ssbo_atomic_exchange;
717 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_comp_swap_ssbo") == 0) {
718 op = nir_intrinsic_ssbo_atomic_comp_swap;
719 } else if (strcmp(ir->callee_name(), "__intrinsic_shader_clock") == 0) {
720 op = nir_intrinsic_shader_clock;
721 } else if (strcmp(ir->callee_name(), "__intrinsic_group_memory_barrier") == 0) {
722 op = nir_intrinsic_group_memory_barrier;
723 } else if (strcmp(ir->callee_name(), "__intrinsic_memory_barrier_atomic_counter") == 0) {
724 op = nir_intrinsic_memory_barrier_atomic_counter;
725 } else if (strcmp(ir->callee_name(), "__intrinsic_memory_barrier_buffer") == 0) {
726 op = nir_intrinsic_memory_barrier_buffer;
727 } else if (strcmp(ir->callee_name(), "__intrinsic_memory_barrier_image") == 0) {
728 op = nir_intrinsic_memory_barrier_image;
729 } else if (strcmp(ir->callee_name(), "__intrinsic_memory_barrier_shared") == 0) {
730 op = nir_intrinsic_memory_barrier_shared;
731 } else if (strcmp(ir->callee_name(), "__intrinsic_load_shared") == 0) {
732 op = nir_intrinsic_load_shared;
733 } else if (strcmp(ir->callee_name(), "__intrinsic_store_shared") == 0) {
734 op = nir_intrinsic_store_shared;
735 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_add_shared") == 0) {
736 op = nir_intrinsic_shared_atomic_add;
737 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_and_shared") == 0) {
738 op = nir_intrinsic_shared_atomic_and;
739 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_or_shared") == 0) {
740 op = nir_intrinsic_shared_atomic_or;
741 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_xor_shared") == 0) {
742 op = nir_intrinsic_shared_atomic_xor;
743 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_min_shared") == 0) {
744 assert(ir->return_deref);
745 if (ir->return_deref->type == glsl_type::int_type)
746 op = nir_intrinsic_shared_atomic_imin;
747 else if (ir->return_deref->type == glsl_type::uint_type)
748 op = nir_intrinsic_shared_atomic_umin;
749 else
750 unreachable("Invalid type");
751 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_max_shared") == 0) {
752 assert(ir->return_deref);
753 if (ir->return_deref->type == glsl_type::int_type)
754 op = nir_intrinsic_shared_atomic_imax;
755 else if (ir->return_deref->type == glsl_type::uint_type)
756 op = nir_intrinsic_shared_atomic_umax;
757 else
758 unreachable("Invalid type");
759 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_exchange_shared") == 0) {
760 op = nir_intrinsic_shared_atomic_exchange;
761 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_comp_swap_shared") == 0) {
762 op = nir_intrinsic_shared_atomic_comp_swap;
763 } else {
764 unreachable("not reached");
765 }
766
767 nir_intrinsic_instr *instr = nir_intrinsic_instr_create(shader, op);
768 nir_dest *dest = &instr->dest;
769
770 switch (op) {
771 case nir_intrinsic_atomic_counter_read_var:
772 case nir_intrinsic_atomic_counter_inc_var:
773 case nir_intrinsic_atomic_counter_dec_var: {
774 ir_dereference *param =
775 (ir_dereference *) ir->actual_parameters.get_head();
776 instr->variables[0] = evaluate_deref(&instr->instr, param);
777 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, NULL);
778 nir_builder_instr_insert(&b, &instr->instr);
779 break;
780 }
781 case nir_intrinsic_image_load:
782 case nir_intrinsic_image_store:
783 case nir_intrinsic_image_atomic_add:
784 case nir_intrinsic_image_atomic_min:
785 case nir_intrinsic_image_atomic_max:
786 case nir_intrinsic_image_atomic_and:
787 case nir_intrinsic_image_atomic_or:
788 case nir_intrinsic_image_atomic_xor:
789 case nir_intrinsic_image_atomic_exchange:
790 case nir_intrinsic_image_atomic_comp_swap:
791 case nir_intrinsic_image_samples:
792 case nir_intrinsic_image_size: {
793 nir_ssa_undef_instr *instr_undef =
794 nir_ssa_undef_instr_create(shader, 1);
795 nir_builder_instr_insert(&b, &instr_undef->instr);
796
797 /* Set the image variable dereference. */
798 exec_node *param = ir->actual_parameters.get_head();
799 ir_dereference *image = (ir_dereference *)param;
800 const glsl_type *type =
801 image->variable_referenced()->type->without_array();
802
803 instr->variables[0] = evaluate_deref(&instr->instr, image);
804 param = param->get_next();
805
806 /* Set the intrinsic destination. */
807 if (ir->return_deref) {
808 const nir_intrinsic_info *info =
809 &nir_intrinsic_infos[instr->intrinsic];
810 nir_ssa_dest_init(&instr->instr, &instr->dest,
811 info->dest_components, NULL);
812 }
813
814 if (op == nir_intrinsic_image_size ||
815 op == nir_intrinsic_image_samples) {
816 nir_builder_instr_insert(&b, &instr->instr);
817 break;
818 }
819
820 /* Set the address argument, extending the coordinate vector to four
821 * components.
822 */
823 nir_ssa_def *src_addr =
824 evaluate_rvalue((ir_dereference *)param);
825 nir_ssa_def *srcs[4];
826
827 for (int i = 0; i < 4; i++) {
828 if (i < type->coordinate_components())
829 srcs[i] = nir_channel(&b, src_addr, i);
830 else
831 srcs[i] = &instr_undef->def;
832 }
833
834 instr->src[0] = nir_src_for_ssa(nir_vec(&b, srcs, 4));
835 param = param->get_next();
836
837 /* Set the sample argument, which is undefined for single-sample
838 * images.
839 */
840 if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS) {
841 instr->src[1] =
842 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
843 param = param->get_next();
844 } else {
845 instr->src[1] = nir_src_for_ssa(&instr_undef->def);
846 }
847
848 /* Set the intrinsic parameters. */
849 if (!param->is_tail_sentinel()) {
850 instr->src[2] =
851 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
852 param = param->get_next();
853 }
854
855 if (!param->is_tail_sentinel()) {
856 instr->src[3] =
857 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
858 param = param->get_next();
859 }
860 nir_builder_instr_insert(&b, &instr->instr);
861 break;
862 }
863 case nir_intrinsic_memory_barrier:
864 case nir_intrinsic_group_memory_barrier:
865 case nir_intrinsic_memory_barrier_atomic_counter:
866 case nir_intrinsic_memory_barrier_buffer:
867 case nir_intrinsic_memory_barrier_image:
868 case nir_intrinsic_memory_barrier_shared:
869 nir_builder_instr_insert(&b, &instr->instr);
870 break;
871 case nir_intrinsic_shader_clock:
872 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, NULL);
873 nir_builder_instr_insert(&b, &instr->instr);
874 break;
875 case nir_intrinsic_store_ssbo: {
876 exec_node *param = ir->actual_parameters.get_head();
877 ir_rvalue *block = ((ir_instruction *)param)->as_rvalue();
878
879 param = param->get_next();
880 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
881
882 param = param->get_next();
883 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
884
885 param = param->get_next();
886 ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
887 assert(write_mask);
888
889 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(val));
890 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(block));
891 instr->src[2] = nir_src_for_ssa(evaluate_rvalue(offset));
892 instr->const_index[0] = write_mask->value.u[0];
893 instr->num_components = val->type->vector_elements;
894
895 nir_builder_instr_insert(&b, &instr->instr);
896 break;
897 }
898 case nir_intrinsic_load_ssbo: {
899 exec_node *param = ir->actual_parameters.get_head();
900 ir_rvalue *block = ((ir_instruction *)param)->as_rvalue();
901
902 param = param->get_next();
903 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
904
905 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(block));
906 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(offset));
907
908 const glsl_type *type = ir->return_deref->var->type;
909 instr->num_components = type->vector_elements;
910
911 /* Setup destination register */
912 nir_ssa_dest_init(&instr->instr, &instr->dest,
913 type->vector_elements, NULL);
914
915 /* Insert the created nir instruction now since in the case of boolean
916 * result we will need to emit another instruction after it
917 */
918 nir_builder_instr_insert(&b, &instr->instr);
919
920 /*
921 * In SSBO/UBO's, a true boolean value is any non-zero value, but we
922 * consider a true boolean to be ~0. Fix this up with a != 0
923 * comparison.
924 */
925 if (type->base_type == GLSL_TYPE_BOOL) {
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 =
931 nir_src_for_ssa(nir_imm_int(&b, 0));
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_builder_instr_insert(&b, &load_ssbo_compare->instr);
939 dest = &load_ssbo_compare->dest.dest;
940 }
941 break;
942 }
943 case nir_intrinsic_ssbo_atomic_add:
944 case nir_intrinsic_ssbo_atomic_imin:
945 case nir_intrinsic_ssbo_atomic_umin:
946 case nir_intrinsic_ssbo_atomic_imax:
947 case nir_intrinsic_ssbo_atomic_umax:
948 case nir_intrinsic_ssbo_atomic_and:
949 case nir_intrinsic_ssbo_atomic_or:
950 case nir_intrinsic_ssbo_atomic_xor:
951 case nir_intrinsic_ssbo_atomic_exchange:
952 case nir_intrinsic_ssbo_atomic_comp_swap: {
953 int param_count = ir->actual_parameters.length();
954 assert(param_count == 3 || param_count == 4);
955
956 /* Block index */
957 exec_node *param = ir->actual_parameters.get_head();
958 ir_instruction *inst = (ir_instruction *) param;
959 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
960
961 /* Offset */
962 param = param->get_next();
963 inst = (ir_instruction *) param;
964 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
965
966 /* data1 parameter (this is always present) */
967 param = param->get_next();
968 inst = (ir_instruction *) param;
969 instr->src[2] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
970
971 /* data2 parameter (only with atomic_comp_swap) */
972 if (param_count == 4) {
973 assert(op == nir_intrinsic_ssbo_atomic_comp_swap);
974 param = param->get_next();
975 inst = (ir_instruction *) param;
976 instr->src[3] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
977 }
978
979 /* Atomic result */
980 assert(ir->return_deref);
981 nir_ssa_dest_init(&instr->instr, &instr->dest,
982 ir->return_deref->type->vector_elements, NULL);
983 nir_builder_instr_insert(&b, &instr->instr);
984 break;
985 }
986 case nir_intrinsic_load_shared: {
987 exec_node *param = ir->actual_parameters.get_head();
988 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
989
990 instr->const_index[0] = 0;
991 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(offset));
992
993 const glsl_type *type = ir->return_deref->var->type;
994 instr->num_components = type->vector_elements;
995
996 /* Setup destination register */
997 nir_ssa_dest_init(&instr->instr, &instr->dest,
998 type->vector_elements, NULL);
999
1000 nir_builder_instr_insert(&b, &instr->instr);
1001 break;
1002 }
1003 case nir_intrinsic_store_shared: {
1004 exec_node *param = ir->actual_parameters.get_head();
1005 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
1006
1007 param = param->get_next();
1008 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
1009
1010 param = param->get_next();
1011 ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
1012 assert(write_mask);
1013
1014 instr->const_index[0] = 0;
1015 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(offset));
1016
1017 instr->const_index[1] = write_mask->value.u[0];
1018
1019 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(val));
1020 instr->num_components = val->type->vector_elements;
1021
1022 nir_builder_instr_insert(&b, &instr->instr);
1023 break;
1024 }
1025 case nir_intrinsic_shared_atomic_add:
1026 case nir_intrinsic_shared_atomic_imin:
1027 case nir_intrinsic_shared_atomic_umin:
1028 case nir_intrinsic_shared_atomic_imax:
1029 case nir_intrinsic_shared_atomic_umax:
1030 case nir_intrinsic_shared_atomic_and:
1031 case nir_intrinsic_shared_atomic_or:
1032 case nir_intrinsic_shared_atomic_xor:
1033 case nir_intrinsic_shared_atomic_exchange:
1034 case nir_intrinsic_shared_atomic_comp_swap: {
1035 int param_count = ir->actual_parameters.length();
1036 assert(param_count == 2 || param_count == 3);
1037
1038 /* Offset */
1039 exec_node *param = ir->actual_parameters.get_head();
1040 ir_instruction *inst = (ir_instruction *) param;
1041 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1042
1043 /* data1 parameter (this is always present) */
1044 param = param->get_next();
1045 inst = (ir_instruction *) param;
1046 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1047
1048 /* data2 parameter (only with atomic_comp_swap) */
1049 if (param_count == 3) {
1050 assert(op == nir_intrinsic_shared_atomic_comp_swap);
1051 param = param->get_next();
1052 inst = (ir_instruction *) param;
1053 instr->src[2] =
1054 nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1055 }
1056
1057 /* Atomic result */
1058 assert(ir->return_deref);
1059 nir_ssa_dest_init(&instr->instr, &instr->dest,
1060 ir->return_deref->type->vector_elements, NULL);
1061 nir_builder_instr_insert(&b, &instr->instr);
1062 break;
1063 }
1064 default:
1065 unreachable("not reached");
1066 }
1067
1068 if (ir->return_deref) {
1069 nir_intrinsic_instr *store_instr =
1070 nir_intrinsic_instr_create(shader, nir_intrinsic_store_var);
1071 store_instr->num_components = ir->return_deref->type->vector_elements;
1072 store_instr->const_index[0] = (1 << store_instr->num_components) - 1;
1073
1074 store_instr->variables[0] =
1075 evaluate_deref(&store_instr->instr, ir->return_deref);
1076 store_instr->src[0] = nir_src_for_ssa(&dest->ssa);
1077
1078 nir_builder_instr_insert(&b, &store_instr->instr);
1079 }
1080
1081 return;
1082 }
1083
1084 struct hash_entry *entry =
1085 _mesa_hash_table_search(this->overload_table, ir->callee);
1086 assert(entry);
1087 nir_function *callee = (nir_function *) entry->data;
1088
1089 nir_call_instr *instr = nir_call_instr_create(this->shader, callee);
1090
1091 unsigned i = 0;
1092 foreach_in_list(ir_dereference, param, &ir->actual_parameters) {
1093 instr->params[i] = evaluate_deref(&instr->instr, param);
1094 i++;
1095 }
1096
1097 instr->return_deref = evaluate_deref(&instr->instr, ir->return_deref);
1098 nir_builder_instr_insert(&b, &instr->instr);
1099 }
1100
1101 void
1102 nir_visitor::visit(ir_assignment *ir)
1103 {
1104 unsigned num_components = ir->lhs->type->vector_elements;
1105
1106 if ((ir->rhs->as_dereference() || ir->rhs->as_constant()) &&
1107 (ir->write_mask == (1 << num_components) - 1 || ir->write_mask == 0)) {
1108 /* We're doing a plain-as-can-be copy, so emit a copy_var */
1109 nir_intrinsic_instr *copy =
1110 nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
1111
1112 copy->variables[0] = evaluate_deref(&copy->instr, ir->lhs);
1113 copy->variables[1] = evaluate_deref(&copy->instr, ir->rhs);
1114
1115 if (ir->condition) {
1116 nir_if *if_stmt = nir_if_create(this->shader);
1117 if_stmt->condition = nir_src_for_ssa(evaluate_rvalue(ir->condition));
1118 nir_builder_cf_insert(&b, &if_stmt->cf_node);
1119 nir_instr_insert_after_cf_list(&if_stmt->then_list, &copy->instr);
1120 b.cursor = nir_after_cf_node(&if_stmt->cf_node);
1121 } else {
1122 nir_builder_instr_insert(&b, &copy->instr);
1123 }
1124 return;
1125 }
1126
1127 assert(ir->rhs->type->is_scalar() || ir->rhs->type->is_vector());
1128
1129 ir->lhs->accept(this);
1130 nir_deref_var *lhs_deref = this->deref_head;
1131 nir_ssa_def *src = evaluate_rvalue(ir->rhs);
1132
1133 if (ir->write_mask != (1 << num_components) - 1 && ir->write_mask != 0) {
1134 /* GLSL IR will give us the input to the write-masked assignment in a
1135 * single packed vector. So, for example, if the writemask is xzw, then
1136 * we have to swizzle x -> x, y -> z, and z -> w and get the y component
1137 * from the load.
1138 */
1139 unsigned swiz[4];
1140 unsigned component = 0;
1141 for (unsigned i = 0; i < 4; i++) {
1142 swiz[i] = ir->write_mask & (1 << i) ? component++ : 0;
1143 }
1144 src = nir_swizzle(&b, src, swiz, num_components, !supports_ints);
1145 }
1146
1147 nir_intrinsic_instr *store =
1148 nir_intrinsic_instr_create(this->shader, nir_intrinsic_store_var);
1149 store->num_components = ir->lhs->type->vector_elements;
1150 store->const_index[0] = ir->write_mask;
1151 nir_deref *store_deref = nir_copy_deref(store, &lhs_deref->deref);
1152 store->variables[0] = nir_deref_as_var(store_deref);
1153 store->src[0] = nir_src_for_ssa(src);
1154
1155 if (ir->condition) {
1156 nir_if *if_stmt = nir_if_create(this->shader);
1157 if_stmt->condition = nir_src_for_ssa(evaluate_rvalue(ir->condition));
1158 nir_builder_cf_insert(&b, &if_stmt->cf_node);
1159 nir_instr_insert_after_cf_list(&if_stmt->then_list, &store->instr);
1160 b.cursor = nir_after_cf_node(&if_stmt->cf_node);
1161 } else {
1162 nir_builder_instr_insert(&b, &store->instr);
1163 }
1164 }
1165
1166 /*
1167 * Given an instruction, returns a pointer to its destination or NULL if there
1168 * is no destination.
1169 *
1170 * Note that this only handles instructions we generate at this level.
1171 */
1172 static nir_dest *
1173 get_instr_dest(nir_instr *instr)
1174 {
1175 nir_alu_instr *alu_instr;
1176 nir_intrinsic_instr *intrinsic_instr;
1177 nir_tex_instr *tex_instr;
1178
1179 switch (instr->type) {
1180 case nir_instr_type_alu:
1181 alu_instr = nir_instr_as_alu(instr);
1182 return &alu_instr->dest.dest;
1183
1184 case nir_instr_type_intrinsic:
1185 intrinsic_instr = nir_instr_as_intrinsic(instr);
1186 if (nir_intrinsic_infos[intrinsic_instr->intrinsic].has_dest)
1187 return &intrinsic_instr->dest;
1188 else
1189 return NULL;
1190
1191 case nir_instr_type_tex:
1192 tex_instr = nir_instr_as_tex(instr);
1193 return &tex_instr->dest;
1194
1195 default:
1196 unreachable("not reached");
1197 }
1198
1199 return NULL;
1200 }
1201
1202 void
1203 nir_visitor::add_instr(nir_instr *instr, unsigned num_components)
1204 {
1205 nir_dest *dest = get_instr_dest(instr);
1206
1207 if (dest)
1208 nir_ssa_dest_init(instr, dest, num_components, NULL);
1209
1210 nir_builder_instr_insert(&b, instr);
1211
1212 if (dest) {
1213 assert(dest->is_ssa);
1214 this->result = &dest->ssa;
1215 }
1216 }
1217
1218 nir_ssa_def *
1219 nir_visitor::evaluate_rvalue(ir_rvalue* ir)
1220 {
1221 ir->accept(this);
1222 if (ir->as_dereference() || ir->as_constant()) {
1223 /*
1224 * A dereference is being used on the right hand side, which means we
1225 * must emit a variable load.
1226 */
1227
1228 nir_intrinsic_instr *load_instr =
1229 nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_var);
1230 load_instr->num_components = ir->type->vector_elements;
1231 load_instr->variables[0] = this->deref_head;
1232 ralloc_steal(load_instr, load_instr->variables[0]);
1233 add_instr(&load_instr->instr, ir->type->vector_elements);
1234 }
1235
1236 return this->result;
1237 }
1238
1239 void
1240 nir_visitor::visit(ir_expression *ir)
1241 {
1242 /* Some special cases */
1243 switch (ir->operation) {
1244 case ir_binop_ubo_load: {
1245 nir_intrinsic_instr *load =
1246 nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_ubo);
1247 load->num_components = ir->type->vector_elements;
1248 load->src[0] = nir_src_for_ssa(evaluate_rvalue(ir->operands[0]));
1249 load->src[1] = nir_src_for_ssa(evaluate_rvalue(ir->operands[1]));
1250 add_instr(&load->instr, ir->type->vector_elements);
1251
1252 /*
1253 * In UBO's, a true boolean value is any non-zero value, but we consider
1254 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1255 */
1256
1257 if (ir->type->base_type == GLSL_TYPE_BOOL)
1258 this->result = nir_ine(&b, &load->dest.ssa, nir_imm_int(&b, 0));
1259
1260 return;
1261 }
1262
1263 case ir_unop_interpolate_at_centroid:
1264 case ir_binop_interpolate_at_offset:
1265 case ir_binop_interpolate_at_sample: {
1266 ir_dereference *deref = ir->operands[0]->as_dereference();
1267 ir_swizzle *swizzle = NULL;
1268 if (!deref) {
1269 /* the api does not allow a swizzle here, but the varying packing code
1270 * may have pushed one into here.
1271 */
1272 swizzle = ir->operands[0]->as_swizzle();
1273 assert(swizzle);
1274 deref = swizzle->val->as_dereference();
1275 assert(deref);
1276 }
1277
1278 deref->accept(this);
1279
1280 nir_intrinsic_op op;
1281 if (this->deref_head->var->data.mode == nir_var_shader_in) {
1282 switch (ir->operation) {
1283 case ir_unop_interpolate_at_centroid:
1284 op = nir_intrinsic_interp_var_at_centroid;
1285 break;
1286 case ir_binop_interpolate_at_offset:
1287 op = nir_intrinsic_interp_var_at_offset;
1288 break;
1289 case ir_binop_interpolate_at_sample:
1290 op = nir_intrinsic_interp_var_at_sample;
1291 break;
1292 default:
1293 unreachable("Invalid interpolation intrinsic");
1294 }
1295 } else {
1296 /* This case can happen if the vertex shader does not write the
1297 * given varying. In this case, the linker will lower it to a
1298 * global variable. Since interpolating a variable makes no
1299 * sense, we'll just turn it into a load which will probably
1300 * eventually end up as an SSA definition.
1301 */
1302 assert(this->deref_head->var->data.mode == nir_var_global);
1303 op = nir_intrinsic_load_var;
1304 }
1305
1306 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(shader, op);
1307 intrin->num_components = deref->type->vector_elements;
1308 intrin->variables[0] = this->deref_head;
1309 ralloc_steal(intrin, intrin->variables[0]);
1310
1311 if (intrin->intrinsic == nir_intrinsic_interp_var_at_offset ||
1312 intrin->intrinsic == nir_intrinsic_interp_var_at_sample)
1313 intrin->src[0] = nir_src_for_ssa(evaluate_rvalue(ir->operands[1]));
1314
1315 add_instr(&intrin->instr, deref->type->vector_elements);
1316
1317 if (swizzle) {
1318 unsigned swiz[4] = {
1319 swizzle->mask.x, swizzle->mask.y, swizzle->mask.z, swizzle->mask.w
1320 };
1321
1322 result = nir_swizzle(&b, result, swiz,
1323 swizzle->type->vector_elements, false);
1324 }
1325
1326 return;
1327 }
1328
1329 default:
1330 break;
1331 }
1332
1333 nir_ssa_def *srcs[4];
1334 for (unsigned i = 0; i < ir->get_num_operands(); i++)
1335 srcs[i] = evaluate_rvalue(ir->operands[i]);
1336
1337 glsl_base_type types[4];
1338 for (unsigned i = 0; i < ir->get_num_operands(); i++)
1339 if (supports_ints)
1340 types[i] = ir->operands[i]->type->base_type;
1341 else
1342 types[i] = GLSL_TYPE_FLOAT;
1343
1344 glsl_base_type out_type;
1345 if (supports_ints)
1346 out_type = ir->type->base_type;
1347 else
1348 out_type = GLSL_TYPE_FLOAT;
1349
1350 switch (ir->operation) {
1351 case ir_unop_bit_not: result = nir_inot(&b, srcs[0]); break;
1352 case ir_unop_logic_not:
1353 result = supports_ints ? nir_inot(&b, srcs[0]) : nir_fnot(&b, srcs[0]);
1354 break;
1355 case ir_unop_neg:
1356 result = (types[0] == GLSL_TYPE_FLOAT) ? nir_fneg(&b, srcs[0])
1357 : nir_ineg(&b, srcs[0]);
1358 break;
1359 case ir_unop_abs:
1360 result = (types[0] == GLSL_TYPE_FLOAT) ? nir_fabs(&b, srcs[0])
1361 : nir_iabs(&b, srcs[0]);
1362 break;
1363 case ir_unop_saturate:
1364 assert(types[0] == GLSL_TYPE_FLOAT);
1365 result = nir_fsat(&b, srcs[0]);
1366 break;
1367 case ir_unop_sign:
1368 result = (types[0] == GLSL_TYPE_FLOAT) ? nir_fsign(&b, srcs[0])
1369 : nir_isign(&b, srcs[0]);
1370 break;
1371 case ir_unop_rcp: result = nir_frcp(&b, srcs[0]); break;
1372 case ir_unop_rsq: result = nir_frsq(&b, srcs[0]); break;
1373 case ir_unop_sqrt: result = nir_fsqrt(&b, srcs[0]); break;
1374 case ir_unop_exp: unreachable("ir_unop_exp should have been lowered");
1375 case ir_unop_log: unreachable("ir_unop_log should have been lowered");
1376 case ir_unop_exp2: result = nir_fexp2(&b, srcs[0]); break;
1377 case ir_unop_log2: result = nir_flog2(&b, srcs[0]); break;
1378 case ir_unop_i2f:
1379 result = supports_ints ? nir_i2f(&b, srcs[0]) : nir_fmov(&b, srcs[0]);
1380 break;
1381 case ir_unop_u2f:
1382 result = supports_ints ? nir_u2f(&b, srcs[0]) : nir_fmov(&b, srcs[0]);
1383 break;
1384 case ir_unop_b2f:
1385 result = supports_ints ? nir_b2f(&b, srcs[0]) : nir_fmov(&b, srcs[0]);
1386 break;
1387 case ir_unop_f2i: result = nir_f2i(&b, srcs[0]); break;
1388 case ir_unop_f2u: result = nir_f2u(&b, srcs[0]); break;
1389 case ir_unop_f2b: result = nir_f2b(&b, srcs[0]); break;
1390 case ir_unop_i2b: result = nir_i2b(&b, srcs[0]); break;
1391 case ir_unop_b2i: result = nir_b2i(&b, srcs[0]); break;
1392 case ir_unop_i2u:
1393 case ir_unop_u2i:
1394 case ir_unop_bitcast_i2f:
1395 case ir_unop_bitcast_f2i:
1396 case ir_unop_bitcast_u2f:
1397 case ir_unop_bitcast_f2u:
1398 case ir_unop_subroutine_to_int:
1399 /* no-op */
1400 result = nir_imov(&b, srcs[0]);
1401 break;
1402 case ir_unop_trunc: result = nir_ftrunc(&b, srcs[0]); break;
1403 case ir_unop_ceil: result = nir_fceil(&b, srcs[0]); break;
1404 case ir_unop_floor: result = nir_ffloor(&b, srcs[0]); break;
1405 case ir_unop_fract: result = nir_ffract(&b, srcs[0]); break;
1406 case ir_unop_round_even: result = nir_fround_even(&b, srcs[0]); break;
1407 case ir_unop_sin: result = nir_fsin(&b, srcs[0]); break;
1408 case ir_unop_cos: result = nir_fcos(&b, srcs[0]); break;
1409 case ir_unop_dFdx: result = nir_fddx(&b, srcs[0]); break;
1410 case ir_unop_dFdy: result = nir_fddy(&b, srcs[0]); break;
1411 case ir_unop_dFdx_fine: result = nir_fddx_fine(&b, srcs[0]); break;
1412 case ir_unop_dFdy_fine: result = nir_fddy_fine(&b, srcs[0]); break;
1413 case ir_unop_dFdx_coarse: result = nir_fddx_coarse(&b, srcs[0]); break;
1414 case ir_unop_dFdy_coarse: result = nir_fddy_coarse(&b, srcs[0]); break;
1415 case ir_unop_pack_snorm_2x16:
1416 result = nir_pack_snorm_2x16(&b, srcs[0]);
1417 break;
1418 case ir_unop_pack_snorm_4x8:
1419 result = nir_pack_snorm_4x8(&b, srcs[0]);
1420 break;
1421 case ir_unop_pack_unorm_2x16:
1422 result = nir_pack_unorm_2x16(&b, srcs[0]);
1423 break;
1424 case ir_unop_pack_unorm_4x8:
1425 result = nir_pack_unorm_4x8(&b, srcs[0]);
1426 break;
1427 case ir_unop_pack_half_2x16:
1428 result = nir_pack_half_2x16(&b, srcs[0]);
1429 break;
1430 case ir_unop_unpack_snorm_2x16:
1431 result = nir_unpack_snorm_2x16(&b, srcs[0]);
1432 break;
1433 case ir_unop_unpack_snorm_4x8:
1434 result = nir_unpack_snorm_4x8(&b, srcs[0]);
1435 break;
1436 case ir_unop_unpack_unorm_2x16:
1437 result = nir_unpack_unorm_2x16(&b, srcs[0]);
1438 break;
1439 case ir_unop_unpack_unorm_4x8:
1440 result = nir_unpack_unorm_4x8(&b, srcs[0]);
1441 break;
1442 case ir_unop_unpack_half_2x16:
1443 result = nir_unpack_half_2x16(&b, srcs[0]);
1444 break;
1445 case ir_unop_bitfield_reverse:
1446 result = nir_bitfield_reverse(&b, srcs[0]);
1447 break;
1448 case ir_unop_bit_count:
1449 result = nir_bit_count(&b, srcs[0]);
1450 break;
1451 case ir_unop_find_msb:
1452 switch (types[0]) {
1453 case GLSL_TYPE_UINT:
1454 result = nir_ufind_msb(&b, srcs[0]);
1455 break;
1456 case GLSL_TYPE_INT:
1457 result = nir_ifind_msb(&b, srcs[0]);
1458 break;
1459 default:
1460 unreachable("Invalid type for findMSB()");
1461 }
1462 break;
1463 case ir_unop_find_lsb:
1464 result = nir_find_lsb(&b, srcs[0]);
1465 break;
1466
1467 case ir_unop_noise:
1468 switch (ir->type->vector_elements) {
1469 case 1:
1470 switch (ir->operands[0]->type->vector_elements) {
1471 case 1: result = nir_fnoise1_1(&b, srcs[0]); break;
1472 case 2: result = nir_fnoise1_2(&b, srcs[0]); break;
1473 case 3: result = nir_fnoise1_3(&b, srcs[0]); break;
1474 case 4: result = nir_fnoise1_4(&b, srcs[0]); break;
1475 default: unreachable("not reached");
1476 }
1477 break;
1478 case 2:
1479 switch (ir->operands[0]->type->vector_elements) {
1480 case 1: result = nir_fnoise2_1(&b, srcs[0]); break;
1481 case 2: result = nir_fnoise2_2(&b, srcs[0]); break;
1482 case 3: result = nir_fnoise2_3(&b, srcs[0]); break;
1483 case 4: result = nir_fnoise2_4(&b, srcs[0]); break;
1484 default: unreachable("not reached");
1485 }
1486 break;
1487 case 3:
1488 switch (ir->operands[0]->type->vector_elements) {
1489 case 1: result = nir_fnoise3_1(&b, srcs[0]); break;
1490 case 2: result = nir_fnoise3_2(&b, srcs[0]); break;
1491 case 3: result = nir_fnoise3_3(&b, srcs[0]); break;
1492 case 4: result = nir_fnoise3_4(&b, srcs[0]); break;
1493 default: unreachable("not reached");
1494 }
1495 break;
1496 case 4:
1497 switch (ir->operands[0]->type->vector_elements) {
1498 case 1: result = nir_fnoise4_1(&b, srcs[0]); break;
1499 case 2: result = nir_fnoise4_2(&b, srcs[0]); break;
1500 case 3: result = nir_fnoise4_3(&b, srcs[0]); break;
1501 case 4: result = nir_fnoise4_4(&b, srcs[0]); break;
1502 default: unreachable("not reached");
1503 }
1504 break;
1505 default:
1506 unreachable("not reached");
1507 }
1508 break;
1509 case ir_unop_get_buffer_size: {
1510 nir_intrinsic_instr *load = nir_intrinsic_instr_create(
1511 this->shader,
1512 nir_intrinsic_get_buffer_size);
1513 load->num_components = ir->type->vector_elements;
1514 load->src[0] = nir_src_for_ssa(evaluate_rvalue(ir->operands[0]));
1515 add_instr(&load->instr, ir->type->vector_elements);
1516 return;
1517 }
1518
1519 case ir_binop_add:
1520 result = (out_type == GLSL_TYPE_FLOAT) ? nir_fadd(&b, srcs[0], srcs[1])
1521 : nir_iadd(&b, srcs[0], srcs[1]);
1522 break;
1523 case ir_binop_sub:
1524 result = (out_type == GLSL_TYPE_FLOAT) ? nir_fsub(&b, srcs[0], srcs[1])
1525 : nir_isub(&b, srcs[0], srcs[1]);
1526 break;
1527 case ir_binop_mul:
1528 result = (out_type == GLSL_TYPE_FLOAT) ? nir_fmul(&b, srcs[0], srcs[1])
1529 : nir_imul(&b, srcs[0], srcs[1]);
1530 break;
1531 case ir_binop_div:
1532 if (out_type == GLSL_TYPE_FLOAT)
1533 result = nir_fdiv(&b, srcs[0], srcs[1]);
1534 else if (out_type == GLSL_TYPE_INT)
1535 result = nir_idiv(&b, srcs[0], srcs[1]);
1536 else
1537 result = nir_udiv(&b, srcs[0], srcs[1]);
1538 break;
1539 case ir_binop_mod:
1540 result = (out_type == GLSL_TYPE_FLOAT) ? nir_fmod(&b, srcs[0], srcs[1])
1541 : nir_umod(&b, srcs[0], srcs[1]);
1542 break;
1543 case ir_binop_min:
1544 if (out_type == GLSL_TYPE_FLOAT)
1545 result = nir_fmin(&b, srcs[0], srcs[1]);
1546 else if (out_type == GLSL_TYPE_INT)
1547 result = nir_imin(&b, srcs[0], srcs[1]);
1548 else
1549 result = nir_umin(&b, srcs[0], srcs[1]);
1550 break;
1551 case ir_binop_max:
1552 if (out_type == GLSL_TYPE_FLOAT)
1553 result = nir_fmax(&b, srcs[0], srcs[1]);
1554 else if (out_type == GLSL_TYPE_INT)
1555 result = nir_imax(&b, srcs[0], srcs[1]);
1556 else
1557 result = nir_umax(&b, srcs[0], srcs[1]);
1558 break;
1559 case ir_binop_pow: result = nir_fpow(&b, srcs[0], srcs[1]); break;
1560 case ir_binop_bit_and: result = nir_iand(&b, srcs[0], srcs[1]); break;
1561 case ir_binop_bit_or: result = nir_ior(&b, srcs[0], srcs[1]); break;
1562 case ir_binop_bit_xor: result = nir_ixor(&b, srcs[0], srcs[1]); break;
1563 case ir_binop_logic_and:
1564 result = supports_ints ? nir_iand(&b, srcs[0], srcs[1])
1565 : nir_fand(&b, srcs[0], srcs[1]);
1566 break;
1567 case ir_binop_logic_or:
1568 result = supports_ints ? nir_ior(&b, srcs[0], srcs[1])
1569 : nir_for(&b, srcs[0], srcs[1]);
1570 break;
1571 case ir_binop_logic_xor:
1572 result = supports_ints ? nir_ixor(&b, srcs[0], srcs[1])
1573 : nir_fxor(&b, srcs[0], srcs[1]);
1574 break;
1575 case ir_binop_lshift: result = nir_ishl(&b, srcs[0], srcs[1]); break;
1576 case ir_binop_rshift:
1577 result = (out_type == GLSL_TYPE_INT) ? nir_ishr(&b, srcs[0], srcs[1])
1578 : nir_ushr(&b, srcs[0], srcs[1]);
1579 break;
1580 case ir_binop_imul_high:
1581 result = (out_type == GLSL_TYPE_INT) ? nir_imul_high(&b, srcs[0], srcs[1])
1582 : nir_umul_high(&b, srcs[0], srcs[1]);
1583 break;
1584 case ir_binop_carry: result = nir_uadd_carry(&b, srcs[0], srcs[1]); break;
1585 case ir_binop_borrow: result = nir_usub_borrow(&b, srcs[0], srcs[1]); break;
1586 case ir_binop_less:
1587 if (supports_ints) {
1588 if (types[0] == GLSL_TYPE_FLOAT)
1589 result = nir_flt(&b, srcs[0], srcs[1]);
1590 else if (types[0] == GLSL_TYPE_INT)
1591 result = nir_ilt(&b, srcs[0], srcs[1]);
1592 else
1593 result = nir_ult(&b, srcs[0], srcs[1]);
1594 } else {
1595 result = nir_slt(&b, srcs[0], srcs[1]);
1596 }
1597 break;
1598 case ir_binop_greater:
1599 if (supports_ints) {
1600 if (types[0] == GLSL_TYPE_FLOAT)
1601 result = nir_flt(&b, srcs[1], srcs[0]);
1602 else if (types[0] == GLSL_TYPE_INT)
1603 result = nir_ilt(&b, srcs[1], srcs[0]);
1604 else
1605 result = nir_ult(&b, srcs[1], srcs[0]);
1606 } else {
1607 result = nir_slt(&b, srcs[1], srcs[0]);
1608 }
1609 break;
1610 case ir_binop_lequal:
1611 if (supports_ints) {
1612 if (types[0] == GLSL_TYPE_FLOAT)
1613 result = nir_fge(&b, srcs[1], srcs[0]);
1614 else if (types[0] == GLSL_TYPE_INT)
1615 result = nir_ige(&b, srcs[1], srcs[0]);
1616 else
1617 result = nir_uge(&b, srcs[1], srcs[0]);
1618 } else {
1619 result = nir_slt(&b, srcs[1], srcs[0]);
1620 }
1621 break;
1622 case ir_binop_gequal:
1623 if (supports_ints) {
1624 if (types[0] == GLSL_TYPE_FLOAT)
1625 result = nir_fge(&b, srcs[0], srcs[1]);
1626 else if (types[0] == GLSL_TYPE_INT)
1627 result = nir_ige(&b, srcs[0], srcs[1]);
1628 else
1629 result = nir_uge(&b, srcs[0], srcs[1]);
1630 } else {
1631 result = nir_slt(&b, srcs[0], srcs[1]);
1632 }
1633 break;
1634 case ir_binop_equal:
1635 if (supports_ints) {
1636 if (types[0] == GLSL_TYPE_FLOAT)
1637 result = nir_feq(&b, srcs[0], srcs[1]);
1638 else
1639 result = nir_ieq(&b, srcs[0], srcs[1]);
1640 } else {
1641 result = nir_seq(&b, srcs[0], srcs[1]);
1642 }
1643 break;
1644 case ir_binop_nequal:
1645 if (supports_ints) {
1646 if (types[0] == GLSL_TYPE_FLOAT)
1647 result = nir_fne(&b, srcs[0], srcs[1]);
1648 else
1649 result = nir_ine(&b, srcs[0], srcs[1]);
1650 } else {
1651 result = nir_sne(&b, srcs[0], srcs[1]);
1652 }
1653 break;
1654 case ir_binop_all_equal:
1655 if (supports_ints) {
1656 if (types[0] == GLSL_TYPE_FLOAT) {
1657 switch (ir->operands[0]->type->vector_elements) {
1658 case 1: result = nir_feq(&b, srcs[0], srcs[1]); break;
1659 case 2: result = nir_ball_fequal2(&b, srcs[0], srcs[1]); break;
1660 case 3: result = nir_ball_fequal3(&b, srcs[0], srcs[1]); break;
1661 case 4: result = nir_ball_fequal4(&b, srcs[0], srcs[1]); break;
1662 default:
1663 unreachable("not reached");
1664 }
1665 } else {
1666 switch (ir->operands[0]->type->vector_elements) {
1667 case 1: result = nir_ieq(&b, srcs[0], srcs[1]); break;
1668 case 2: result = nir_ball_iequal2(&b, srcs[0], srcs[1]); break;
1669 case 3: result = nir_ball_iequal3(&b, srcs[0], srcs[1]); break;
1670 case 4: result = nir_ball_iequal4(&b, srcs[0], srcs[1]); break;
1671 default:
1672 unreachable("not reached");
1673 }
1674 }
1675 } else {
1676 switch (ir->operands[0]->type->vector_elements) {
1677 case 1: result = nir_seq(&b, srcs[0], srcs[1]); break;
1678 case 2: result = nir_fall_equal2(&b, srcs[0], srcs[1]); break;
1679 case 3: result = nir_fall_equal3(&b, srcs[0], srcs[1]); break;
1680 case 4: result = nir_fall_equal4(&b, srcs[0], srcs[1]); break;
1681 default:
1682 unreachable("not reached");
1683 }
1684 }
1685 break;
1686 case ir_binop_any_nequal:
1687 if (supports_ints) {
1688 if (types[0] == GLSL_TYPE_FLOAT) {
1689 switch (ir->operands[0]->type->vector_elements) {
1690 case 1: result = nir_fne(&b, srcs[0], srcs[1]); break;
1691 case 2: result = nir_bany_fnequal2(&b, srcs[0], srcs[1]); break;
1692 case 3: result = nir_bany_fnequal3(&b, srcs[0], srcs[1]); break;
1693 case 4: result = nir_bany_fnequal4(&b, srcs[0], srcs[1]); break;
1694 default:
1695 unreachable("not reached");
1696 }
1697 } else {
1698 switch (ir->operands[0]->type->vector_elements) {
1699 case 1: result = nir_ine(&b, srcs[0], srcs[1]); break;
1700 case 2: result = nir_bany_inequal2(&b, srcs[0], srcs[1]); break;
1701 case 3: result = nir_bany_inequal3(&b, srcs[0], srcs[1]); break;
1702 case 4: result = nir_bany_inequal4(&b, srcs[0], srcs[1]); break;
1703 default:
1704 unreachable("not reached");
1705 }
1706 }
1707 } else {
1708 switch (ir->operands[0]->type->vector_elements) {
1709 case 1: result = nir_sne(&b, srcs[0], srcs[1]); break;
1710 case 2: result = nir_fany_nequal2(&b, srcs[0], srcs[1]); break;
1711 case 3: result = nir_fany_nequal3(&b, srcs[0], srcs[1]); break;
1712 case 4: result = nir_fany_nequal4(&b, srcs[0], srcs[1]); break;
1713 default:
1714 unreachable("not reached");
1715 }
1716 }
1717 break;
1718 case ir_binop_dot:
1719 switch (ir->operands[0]->type->vector_elements) {
1720 case 2: result = nir_fdot2(&b, srcs[0], srcs[1]); break;
1721 case 3: result = nir_fdot3(&b, srcs[0], srcs[1]); break;
1722 case 4: result = nir_fdot4(&b, srcs[0], srcs[1]); break;
1723 default:
1724 unreachable("not reached");
1725 }
1726 break;
1727
1728 case ir_binop_ldexp: result = nir_ldexp(&b, srcs[0], srcs[1]); break;
1729 case ir_triop_fma:
1730 result = nir_ffma(&b, srcs[0], srcs[1], srcs[2]);
1731 break;
1732 case ir_triop_lrp:
1733 result = nir_flrp(&b, srcs[0], srcs[1], srcs[2]);
1734 break;
1735 case ir_triop_csel:
1736 if (supports_ints)
1737 result = nir_bcsel(&b, srcs[0], srcs[1], srcs[2]);
1738 else
1739 result = nir_fcsel(&b, srcs[0], srcs[1], srcs[2]);
1740 break;
1741 case ir_triop_bitfield_extract:
1742 result = (out_type == GLSL_TYPE_INT) ?
1743 nir_ibitfield_extract(&b, srcs[0], srcs[1], srcs[2]) :
1744 nir_ubitfield_extract(&b, srcs[0], srcs[1], srcs[2]);
1745 break;
1746 case ir_quadop_bitfield_insert:
1747 result = nir_bitfield_insert(&b, srcs[0], srcs[1], srcs[2], srcs[3]);
1748 break;
1749 case ir_quadop_vector:
1750 result = nir_vec(&b, srcs, ir->type->vector_elements);
1751 break;
1752
1753 default:
1754 unreachable("not reached");
1755 }
1756 }
1757
1758 void
1759 nir_visitor::visit(ir_swizzle *ir)
1760 {
1761 unsigned swizzle[4] = { ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w };
1762 result = nir_swizzle(&b, evaluate_rvalue(ir->val), swizzle,
1763 ir->type->vector_elements, !supports_ints);
1764 }
1765
1766 void
1767 nir_visitor::visit(ir_texture *ir)
1768 {
1769 unsigned num_srcs;
1770 nir_texop op;
1771 switch (ir->op) {
1772 case ir_tex:
1773 op = nir_texop_tex;
1774 num_srcs = 1; /* coordinate */
1775 break;
1776
1777 case ir_txb:
1778 case ir_txl:
1779 op = (ir->op == ir_txb) ? nir_texop_txb : nir_texop_txl;
1780 num_srcs = 2; /* coordinate, bias/lod */
1781 break;
1782
1783 case ir_txd:
1784 op = nir_texop_txd; /* coordinate, dPdx, dPdy */
1785 num_srcs = 3;
1786 break;
1787
1788 case ir_txf:
1789 op = nir_texop_txf;
1790 if (ir->lod_info.lod != NULL)
1791 num_srcs = 2; /* coordinate, lod */
1792 else
1793 num_srcs = 1; /* coordinate */
1794 break;
1795
1796 case ir_txf_ms:
1797 op = nir_texop_txf_ms;
1798 num_srcs = 2; /* coordinate, sample_index */
1799 break;
1800
1801 case ir_txs:
1802 op = nir_texop_txs;
1803 if (ir->lod_info.lod != NULL)
1804 num_srcs = 1; /* lod */
1805 else
1806 num_srcs = 0;
1807 break;
1808
1809 case ir_lod:
1810 op = nir_texop_lod;
1811 num_srcs = 1; /* coordinate */
1812 break;
1813
1814 case ir_tg4:
1815 op = nir_texop_tg4;
1816 num_srcs = 1; /* coordinate */
1817 break;
1818
1819 case ir_query_levels:
1820 op = nir_texop_query_levels;
1821 num_srcs = 0;
1822 break;
1823
1824 case ir_texture_samples:
1825 op = nir_texop_texture_samples;
1826 num_srcs = 0;
1827 break;
1828
1829 case ir_samples_identical:
1830 op = nir_texop_samples_identical;
1831 num_srcs = 1; /* coordinate */
1832 break;
1833
1834 default:
1835 unreachable("not reached");
1836 }
1837
1838 if (ir->projector != NULL)
1839 num_srcs++;
1840 if (ir->shadow_comparitor != NULL)
1841 num_srcs++;
1842 if (ir->offset != NULL && ir->offset->as_constant() == NULL)
1843 num_srcs++;
1844
1845 nir_tex_instr *instr = nir_tex_instr_create(this->shader, num_srcs);
1846
1847 instr->op = op;
1848 instr->sampler_dim =
1849 (glsl_sampler_dim) ir->sampler->type->sampler_dimensionality;
1850 instr->is_array = ir->sampler->type->sampler_array;
1851 instr->is_shadow = ir->sampler->type->sampler_shadow;
1852 if (instr->is_shadow)
1853 instr->is_new_style_shadow = (ir->type->vector_elements == 1);
1854 switch (ir->type->base_type) {
1855 case GLSL_TYPE_FLOAT:
1856 instr->dest_type = nir_type_float;
1857 break;
1858 case GLSL_TYPE_INT:
1859 instr->dest_type = nir_type_int;
1860 break;
1861 case GLSL_TYPE_BOOL:
1862 case GLSL_TYPE_UINT:
1863 instr->dest_type = nir_type_uint;
1864 break;
1865 default:
1866 unreachable("not reached");
1867 }
1868
1869 instr->sampler = evaluate_deref(&instr->instr, ir->sampler);
1870
1871 unsigned src_number = 0;
1872
1873 if (ir->coordinate != NULL) {
1874 instr->coord_components = ir->coordinate->type->vector_elements;
1875 instr->src[src_number].src =
1876 nir_src_for_ssa(evaluate_rvalue(ir->coordinate));
1877 instr->src[src_number].src_type = nir_tex_src_coord;
1878 src_number++;
1879 }
1880
1881 if (ir->projector != NULL) {
1882 instr->src[src_number].src =
1883 nir_src_for_ssa(evaluate_rvalue(ir->projector));
1884 instr->src[src_number].src_type = nir_tex_src_projector;
1885 src_number++;
1886 }
1887
1888 if (ir->shadow_comparitor != NULL) {
1889 instr->src[src_number].src =
1890 nir_src_for_ssa(evaluate_rvalue(ir->shadow_comparitor));
1891 instr->src[src_number].src_type = nir_tex_src_comparitor;
1892 src_number++;
1893 }
1894
1895 if (ir->offset != NULL) {
1896 /* we don't support multiple offsets yet */
1897 assert(ir->offset->type->is_vector() || ir->offset->type->is_scalar());
1898
1899 ir_constant *const_offset = ir->offset->as_constant();
1900 if (const_offset != NULL) {
1901 for (unsigned i = 0; i < const_offset->type->vector_elements; i++)
1902 instr->const_offset[i] = const_offset->value.i[i];
1903 } else {
1904 instr->src[src_number].src =
1905 nir_src_for_ssa(evaluate_rvalue(ir->offset));
1906 instr->src[src_number].src_type = nir_tex_src_offset;
1907 src_number++;
1908 }
1909 }
1910
1911 switch (ir->op) {
1912 case ir_txb:
1913 instr->src[src_number].src =
1914 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.bias));
1915 instr->src[src_number].src_type = nir_tex_src_bias;
1916 src_number++;
1917 break;
1918
1919 case ir_txl:
1920 case ir_txf:
1921 case ir_txs:
1922 if (ir->lod_info.lod != NULL) {
1923 instr->src[src_number].src =
1924 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.lod));
1925 instr->src[src_number].src_type = nir_tex_src_lod;
1926 src_number++;
1927 }
1928 break;
1929
1930 case ir_txd:
1931 instr->src[src_number].src =
1932 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.grad.dPdx));
1933 instr->src[src_number].src_type = nir_tex_src_ddx;
1934 src_number++;
1935 instr->src[src_number].src =
1936 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.grad.dPdy));
1937 instr->src[src_number].src_type = nir_tex_src_ddy;
1938 src_number++;
1939 break;
1940
1941 case ir_txf_ms:
1942 instr->src[src_number].src =
1943 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.sample_index));
1944 instr->src[src_number].src_type = nir_tex_src_ms_index;
1945 src_number++;
1946 break;
1947
1948 case ir_tg4:
1949 instr->component = ir->lod_info.component->as_constant()->value.u[0];
1950 break;
1951
1952 default:
1953 break;
1954 }
1955
1956 assert(src_number == num_srcs);
1957
1958 add_instr(&instr->instr, nir_tex_instr_dest_size(instr));
1959 }
1960
1961 void
1962 nir_visitor::visit(ir_constant *ir)
1963 {
1964 /*
1965 * We don't know if this variable is an an array or struct that gets
1966 * dereferenced, so do the safe thing an make it a variable with a
1967 * constant initializer and return a dereference.
1968 */
1969
1970 nir_variable *var =
1971 nir_local_variable_create(this->impl, ir->type, "const_temp");
1972 var->data.read_only = true;
1973 var->constant_initializer = constant_copy(ir, var);
1974
1975 this->deref_head = nir_deref_var_create(this->shader, var);
1976 this->deref_tail = &this->deref_head->deref;
1977 }
1978
1979 void
1980 nir_visitor::visit(ir_dereference_variable *ir)
1981 {
1982 struct hash_entry *entry =
1983 _mesa_hash_table_search(this->var_table, ir->var);
1984 assert(entry);
1985 nir_variable *var = (nir_variable *) entry->data;
1986
1987 nir_deref_var *deref = nir_deref_var_create(this->shader, var);
1988 this->deref_head = deref;
1989 this->deref_tail = &deref->deref;
1990 }
1991
1992 void
1993 nir_visitor::visit(ir_dereference_record *ir)
1994 {
1995 ir->record->accept(this);
1996
1997 int field_index = this->deref_tail->type->field_index(ir->field);
1998 assert(field_index >= 0);
1999
2000 nir_deref_struct *deref = nir_deref_struct_create(this->deref_tail, field_index);
2001 deref->deref.type = ir->type;
2002 this->deref_tail->child = &deref->deref;
2003 this->deref_tail = &deref->deref;
2004 }
2005
2006 void
2007 nir_visitor::visit(ir_dereference_array *ir)
2008 {
2009 nir_deref_array *deref = nir_deref_array_create(this->shader);
2010 deref->deref.type = ir->type;
2011
2012 ir_constant *const_index = ir->array_index->as_constant();
2013 if (const_index != NULL) {
2014 deref->deref_array_type = nir_deref_array_type_direct;
2015 deref->base_offset = const_index->value.u[0];
2016 } else {
2017 deref->deref_array_type = nir_deref_array_type_indirect;
2018 deref->indirect =
2019 nir_src_for_ssa(evaluate_rvalue(ir->array_index));
2020 }
2021
2022 ir->array->accept(this);
2023
2024 this->deref_tail->child = &deref->deref;
2025 ralloc_steal(this->deref_tail, deref);
2026 this->deref_tail = &deref->deref;
2027 }
2028
2029 void
2030 nir_visitor::visit(ir_barrier *ir)
2031 {
2032 nir_intrinsic_instr *instr =
2033 nir_intrinsic_instr_create(this->shader, nir_intrinsic_barrier);
2034 nir_builder_instr_insert(&b, &instr->instr);
2035 }