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