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