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