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