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