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