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