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