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