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