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