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