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