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